fix(relinker): reject a program header entry size other than 56 bytes

ReadProgramHeaders stepped through the table by e_phentsize without
checking it. With 0 it read the first header e_phnum times, and with
any other size it read misaligned fields, so a damaged header failed
much later as "No PT_DYNAMIC segment found", after building up to
65535 copies of one segment. A non-empty table must now use 56-byte
entries, as the guest module reader already requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Hashaam Zafar
2026-10-07 12:08:14 -07:00
parent cea1ef13c3
commit 33ab7c90f9
2 changed files with 14 additions and 0 deletions
@@ -102,6 +102,9 @@ ElfHeader ElfReader::ReadHeader() const {
std::vector<ProgramHeader> ElfReader::ReadProgramHeaders() const {
const ElfHeader header = ReadHeader();
if (header.ProgramHeaderCount != 0 && header.ProgramHeaderEntrySize != 56) {
throw RelinkerException("Invalid ELF program header entry size: expected 56 bytes", 0x36);
}
std::vector<ProgramHeader> headers;
FileByteOffset offset = header.ProgramHeaderOffset;
@@ -25,6 +25,17 @@ def main():
result = subprocess.run([str(relinker), "--skip-sce-module", *mode, str(source), str(output)],
capture_output=True, text=True, timeout=20)
assert result.returncode == 2 and error in result.stderr and not output.exists(), (name, mode, result)
for size in (0, 55, 64):
source = Path(directory) / f"program-header-entry-size-{size}.elf"
data = fixture()
struct.pack_into("<H", data, 0x36, size)
source.write_bytes(data)
for mode in ([], ["--windows"]):
output = source.with_suffix(".out")
result = subprocess.run([str(relinker), "--skip-sce-module", *mode, str(source), str(output)],
capture_output=True, text=True, timeout=20)
error = "Invalid ELF program header entry size: expected 56 bytes (offset 0x36)"
assert result.returncode == 2 and error in result.stderr and not output.exists(), (size, mode, result)
print("ELF offset tests passed")