relinker: init/fini array and code analysis file offsets wrap for a PT_LOAD whose file range is outside the file #6392
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by midasmatterhorn on 2026-10-15T13:56:05Z.
Two analysis passes form a virtual-address-to-file-offset sum before validating it, so a
PT_LOADwhosep_offsetis near 2^64 wraps the addition and an unrelated in-file offset passes the later range check:EntryPointCollector::collectArray(core/relinker/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp:133):CodeInstructionCollector::fileOffset(core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:51), reached from the init/fini array tags,addFunctionand theDT_HASH/GNU-hash reads:With a
PT_LOAD(Flags == 0, so the read-write data-region scan skips it and never validates its file range) carryingp_offset = 2^64 - 0x40,p_vaddr = 0x8000,p_filesz = 0x410, andDT_INIT_ARRAY = 0x8400(0x400 into the segment), the sum wraps to0x3C0, the range check passes, and the 8 bytes at file offset0x3C0are read as if they were the init-array slot. The value found there becomes a CFG entry point, so the import-filtering verdict — the observable output — flips on a byte at an unrelated file offset. The repo already rejects this shape elsewhere:RelativeRelocationIndex.cpp:49subtracts before adding, andb1599a94efixed the same pattern for header/symbol reads.Reproduced with a relinker-only build of
main92cdc7b62on macOS 27.0.1 arm64 (Apple clang 21.0.0), the same minimal image three ways (--skip-sce-module --windows unused-filter=1; the array slot word is the only difference):1 -> 0; filtered=1p_offset = 2^64-0x40, wrapped read returns 01 -> 0; filtered=10x3C0hold a pointer to the GOT-reading function1 -> 1; filtered=0The same image converts against the Linux target with exit code 0, so the wrong verdict is not masked there. Under ASan, an instrumented build of the collector confirms the read lands at the wrapped offset; with the same
p_offsetshape,CodeInstructionCollectoraccepts an otherwise-unreachable instruction (0x280) as a code root through itsDT_INIT_ARRAYpath.Suggested fix: mirror
RelativeRelocationIndex::vaddrToFileOffset— subtract first and bound each part againstelfBytes.size()before forming the sum, in both places.Found by an AI-assisted audit (DeepSeek V4.1 Flash via OpenCode); the runs above were executed against the real
relinkerbinary. I can send the fix with regression tests.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T14:08:13Z.
I tried the reported shape against current
mainand it is rejected before either analysis collector runs:ElfReader::ReadProgramHeaders()checks everyPT_LOADfile range and returnsSegment offset out of boundsfor thep_offset = 2^64 - 0x40segment. That validation is present on current main, so the reported input no longer reaches the wrapped offset on this branch. I did not open a duplicate fix. If you have a current-main reproducer that passes this parser check, please share it and I can investigate that case.Imported from boykopovar/AnyPS5. Originally posted by midasmatterhorn on 2026-10-15T14:24:02Z.
You are right that
ElfReader::ReadProgramHeaders()validates the file ranges — my audit exercised the collectors against a directly-linked image instead of through the full conversion entry path, so the wrap was reachable only in that harness. The end-to-end run in the report (thebogus/phantomrow) did drive the real binary, so if you saw an early rejection there, I would like to understand which check fired in your run — happy to close this as invalid either way. Thanks for the fast verification.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T15:20:29Z.
The guard is in
ElfReader::ReadProgramHeaders()before the program headers are returned to either analysis collector. For eachPT_LOAD, it first rejectsp_filesz > p_memsz, then calls_rangeFits(ph.Offset, ph.FileSize, _fileBuffer.size());_rangeFitsfails for the reportedp_offset = 2^64 - 0x40andp_filesz = 0x410, raisingRelinkerException("Segment offset out of bounds", ph.Offset). So in the current full conversion path, the malformed segment is rejected beforeEntryPointCollectororCodeInstructionCollectorcan form the wrapped sum. The direct-collector harness demonstrates a missing local guard, but not an end-to-end current-main path for this input.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T20:13:37Z.
Both observations are consistent: the guard @MarcZX100 points to landed after the reporter's base.
main92cdc7b62predates #5741 (merged as0c32e8dd0, 2026-10-15 13:07Z), which added thePT_LOADrange check toElfReader::ReadProgramHeaders()(d7ebcf771). I built the relinker-only target at both commits (macOS 27.2 arm64, M2 Max, Apple clang) and ran the same input through the real binary: thetests/test_optional_plt.pyfixture plus a thirdPT_LOAD(Flags = 0,p_offset = 2^64 - 0x40,p_vaddr = 0x8000,p_filesz = 0x410) andDT_INIT_ARRAY = 0x8400,DT_INIT_ARRAYSZ = 8;--skip-sce-module, withunused-filter=0and=1(same result for both):92cdc7b6244df21632(current main)Segment offset out of bounds (offset 0xffffffffffffffc0)--windowsInvalid PT_LOAD file range(WindowsLoadImage.cpp:18)Segment offset out of bounds--macosInvalid PT_LOAD file rangeSegment offset out of boundsSo the end-to-end path was reachable on the Linux target at the reported base, and it is closed on current main for all three targets before either collector runs. The existing
elf_offsetstest already covers aPT_LOADwithp_offset = 0xffffffffffffffffon both Linux and Windows (passes here). What is left is only the missing local guard in the two collectors when called directly.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T03:41:37Z.
The PT_LOAD range check from #5741 (
0c32e8dd0) is on main: the input from this report now stops with 'Segment offset out of bounds' on all three targets before either collector runs, as MarcZX100 and jotitadev both measured. Closing as done; if you want the direct-call guard in the two collectors, open a separate issue for it.