relinker: init/fini array and code analysis file offsets wrap for a PT_LOAD whose file range is outside the file #6392

Closed
opened 2026-10-15 13:56:05 +00:00 by midasmatterhorn · 5 comments
midasmatterhorn commented 2026-10-15 13:56:05 +00:00 (Migrated from github.com)
Contributor

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_LOAD whose p_offset is 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):

    const std::uint64_t arrayFileOff = container->fileOffset + (arrayVa - container->vaddr);
    if (arrayFileOff > elfBytes.size()) { ... }
    
  • CodeInstructionCollector::fileOffset (core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:51), reached from the init/fini array tags, addFunction and the DT_HASH/GNU-hash reads:

    const auto offset = header.Offset + address - header.MappedAddress;
    

With a PT_LOAD (Flags == 0, so the read-write data-region scan skips it and never validates its file range) carrying p_offset = 2^64 - 0x40, p_vaddr = 0x8000, p_filesz = 0x410, and DT_INIT_ARRAY = 0x8400 (0x400 into the segment), the sum wraps to 0x3C0, the range check passes, and the 8 bytes at file offset 0x3C0 are 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:49 subtracts before adding, and b1599a94e fixed the same pattern for header/symbol reads.

Reproduced with a relinker-only build of main 92cdc7b62 on 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):

image CFG/GOT filtering result
array in the real segment, slot = 0 1 -> 0; filtered=1 import dropped (intended)
bogus p_offset = 2^64-0x40, wrapped read returns 0 1 -> 0; filtered=1 import dropped
same, but bytes at file offset 0x3C0 hold a pointer to the GOT-reading function 1 -> 1; filtered=0 import kept — verdict driven by the wrapped read

The 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_offset shape, CodeInstructionCollector accepts an otherwise-unreachable instruction (0x280) as a code root through its DT_INIT_ARRAY path.

Suggested fix: mirror RelativeRelocationIndex::vaddrToFileOffset — subtract first and bound each part against elfBytes.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 relinker binary. I can send the fix with regression tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5867513528 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6392). Originally posted by [midasmatterhorn](https://github.com/midasmatterhorn) on 2026-10-15T13:56:05Z. --- Two analysis passes form a virtual-address-to-file-offset sum before validating it, so a `PT_LOAD` whose `p_offset` is 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): ```cpp const std::uint64_t arrayFileOff = container->fileOffset + (arrayVa - container->vaddr); if (arrayFileOff > elfBytes.size()) { ... } ``` - `CodeInstructionCollector::fileOffset` (core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:51), reached from the init/fini array tags, `addFunction` and the `DT_HASH`/GNU-hash reads: ```cpp const auto offset = header.Offset + address - header.MappedAddress; ``` With a `PT_LOAD` (`Flags == 0`, so the read-write data-region scan skips it and never validates its file range) carrying `p_offset = 2^64 - 0x40`, `p_vaddr = 0x8000`, `p_filesz = 0x410`, and `DT_INIT_ARRAY = 0x8400` (0x400 into the segment), the sum wraps to `0x3C0`, the range check passes, and the 8 bytes at file offset `0x3C0` are 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:49` subtracts before adding, and `b1599a94e` fixed the same pattern for header/symbol reads. Reproduced with a relinker-only build of `main` `92cdc7b62` on 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): | image | CFG/GOT filtering | result | | --- | --- | --- | | array in the real segment, slot = 0 | `1 -> 0; filtered=1` | import dropped (intended) | | bogus `p_offset = 2^64-0x40`, wrapped read returns 0 | `1 -> 0; filtered=1` | import dropped | | same, but bytes at file offset `0x3C0` hold a pointer to the GOT-reading function | `1 -> 1; filtered=0` | import kept — verdict driven by the wrapped read | The 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_offset` shape, `CodeInstructionCollector` accepts an otherwise-unreachable instruction (`0x280`) as a code root through its `DT_INIT_ARRAY` path. Suggested fix: mirror `RelativeRelocationIndex::vaddrToFileOffset` — subtract first and bound each part against `elfBytes.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 `relinker` binary. I can send the fix with regression tests.
MarcZX10 commented 2026-10-15 14:08:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T14:08:13Z.


I tried the reported shape against current main and it is rejected before either analysis collector runs: ElfReader::ReadProgramHeaders() checks every PT_LOAD file range and returns Segment offset out of bounds for the p_offset = 2^64 - 0x40 segment. 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194065578 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6392#issuecomment-6194065578). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T14:08:13Z. --- I tried the reported shape against current `main` and it is rejected before either analysis collector runs: `ElfReader::ReadProgramHeaders()` checks every `PT_LOAD` file range and returns `Segment offset out of bounds` for the `p_offset = 2^64 - 0x40` segment. 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.
midasmatterhorn commented 2026-10-15 14:24:02 +00:00 (Migrated from github.com)
Author
Contributor

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 (the bogus/phantom row) 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194398449 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6392#issuecomment-6194398449). Originally posted by [midasmatterhorn](https://github.com/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 (the `bogus/phantom` row) 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.
MarcZX10 commented 2026-10-15 15:20:29 +00:00 (Migrated from github.com)
Author
Contributor

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 each PT_LOAD, it first rejects p_filesz > p_memsz, then calls _rangeFits(ph.Offset, ph.FileSize, _fileBuffer.size()); _rangeFits fails for the reported p_offset = 2^64 - 0x40 and p_filesz = 0x410, raising RelinkerException("Segment offset out of bounds", ph.Offset). So in the current full conversion path, the malformed segment is rejected before EntryPointCollector or CodeInstructionCollector can 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6195620931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6392#issuecomment-6195620931). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T15:20:29Z. --- The guard is in `ElfReader::ReadProgramHeaders()` before the program headers are returned to either analysis collector. For each `PT_LOAD`, it first rejects `p_filesz > p_memsz`, then calls `_rangeFits(ph.Offset, ph.FileSize, _fileBuffer.size())`; `_rangeFits` fails for the reported `p_offset = 2^64 - 0x40` and `p_filesz = 0x410`, raising `RelinkerException("Segment offset out of bounds", ph.Offset)`. So in the current full conversion path, the malformed segment is rejected before `EntryPointCollector` or `CodeInstructionCollector` can 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.
jotitadev commented 2026-10-15 20:13:37 +00:00 (Migrated from github.com)
Author
Contributor

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.

main 92cdc7b62 predates #5741 (merged as 0c32e8dd0, 2026-10-15 13:07Z), which added the PT_LOAD range check to ElfReader::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: the tests/test_optional_plt.py fixture plus a third PT_LOAD (Flags = 0, p_offset = 2^64 - 0x40, p_vaddr = 0x8000, p_filesz = 0x410) and DT_INIT_ARRAY = 0x8400, DT_INIT_ARRAYSZ = 8; --skip-sce-module, with unused-filter=0 and =1 (same result for both):

target 92cdc7b62 44df21632 (current main)
Linux exit 0, output written exit 2, Segment offset out of bounds (offset 0xffffffffffffffc0)
--windows exit 2, Invalid PT_LOAD file range (WindowsLoadImage.cpp:18) exit 2, Segment offset out of bounds
--macos exit 2, Invalid PT_LOAD file range exit 2, Segment offset out of bounds

So 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_offsets test already covers a PT_LOAD with p_offset = 0xffffffffffffffff on 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6201344862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6392#issuecomment-6201344862). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-15T20:13:37Z. --- Both observations are consistent: the guard @MarcZX100 points to landed after the reporter's base. `main` `92cdc7b62` predates #5741 (merged as `0c32e8dd0`, 2026-10-15 13:07Z), which added the `PT_LOAD` range check to `ElfReader::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: the `tests/test_optional_plt.py` fixture plus a third `PT_LOAD` (`Flags = 0`, `p_offset = 2^64 - 0x40`, `p_vaddr = 0x8000`, `p_filesz = 0x410`) and `DT_INIT_ARRAY = 0x8400`, `DT_INIT_ARRAYSZ = 8`; `--skip-sce-module`, with `unused-filter=0` and `=1` (same result for both): | target | `92cdc7b62` | `44df21632` (current main) | | --- | --- | --- | | Linux | exit 0, output written | exit 2, `Segment offset out of bounds (offset 0xffffffffffffffc0)` | | `--windows` | exit 2, `Invalid PT_LOAD file range` (`WindowsLoadImage.cpp:18`) | exit 2, `Segment offset out of bounds` | | `--macos` | exit 2, `Invalid PT_LOAD file range` | exit 2, `Segment offset out of bounds` | So 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_offsets` test already covers a `PT_LOAD` with `p_offset = 0xffffffffffffffff` on 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.
oneandonlydean commented 2026-10-17 03:41:37 +00:00 (Migrated from github.com)
Author
Contributor

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6229169453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6392#issuecomment-6229169453). Originally posted by [oneandonlydean](https://github.com/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.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#6392
No description provided.