fix(relinker): reject malformed dynamic segments #930

Merged
ebarr24 merged 1 commit from upstream-pr/930 into main 2026-10-07 02:16:45 +00:00
ebarr24 commented 2026-10-06 18:28:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ebarr24 on 2026-10-06T18:28:30Z.


What

The executable relinker's dynamic reader accepted a table without DT_NULL and
silently stopped at the file boundary or a wrapped end offset. Linux conversion
could produce an output executable for a truncated or unterminated PT_DYNAMIC.
Windows detected some of these inputs later during code analysis, after parsing.

Validate the entire declared segment using the existing subtraction-based range
check, require whole 16-byte entries, and require DT_NULL within the segment.
Return at the first DT_NULL so valid trailing padding is still ignored. Errors
identify the segment offset before conversion starts. The termination requirement
follows the ELF dynamic section specification.

The new integration test exercises both Linux and Windows output modes with 11
synthetic fixtures: valid input, ignored entries after DT_NULL, a table ending
exactly at EOF, offsets beyond EOF and at UINT64_MAX, wrapped offset/size ranges,
a truncated declaration after DT_NULL, a partial entry, a missing terminator and
an empty table. The existing invalid-offset test uses the new segment diagnostic.

Tested

Windows 11 x64, WinLibs MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), CMake 4.4.4,
Ninja 1.13.2, Python 3.12.14. Base: 5895ea721a.
Change: cd0e534d.

  • Regression fails against a separately linked executable with the base ElfReader
    object; malformed Linux inputs return success and produce output. It passes
    with this change in both output modes.
  • Focused CTest: dynamic_segment, elf_offsets, elf_header, string_table_bounds
    and strict_nid_filter all pass.
  • Full Release build with BUILD_TESTING and ANYPS5_ENABLE_SPIRV_TOOLS passes,
    including the libs target.
  • Complete CTest suite: 358/358 pass, zero skips, under the normal Windows user
    account (162.58 seconds). Includes GPU execution tests on the RTX 3080.
  • Generated synthetic PE fixtures used a dedicated temporary folder excluded from
    Defender scanning, with Smart App Control off. Defender real-time protection
    remained enabled.
  • No console or commercial-title testing.

Checklist

  • Based on inspected main; no other open PR implements this correction
  • One topic per PR; follow-ups go in a new PR
  • No comments added in code
  • Invalid input throws; no silent stubs added
  • No notes, investigation files or images added to the upstream repository
  • No third-party dependencies added
  • Generic behaviour, independent of a title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4763005136 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/930). Originally posted by [ebarr24](https://github.com/ebarr24) on 2026-10-06T18:28:30Z. --- ### What The executable relinker's dynamic reader accepted a table without DT_NULL and silently stopped at the file boundary or a wrapped end offset. Linux conversion could produce an output executable for a truncated or unterminated PT_DYNAMIC. Windows detected some of these inputs later during code analysis, after parsing. Validate the entire declared segment using the existing subtraction-based range check, require whole 16-byte entries, and require DT_NULL within the segment. Return at the first DT_NULL so valid trailing padding is still ignored. Errors identify the segment offset before conversion starts. The termination requirement follows the [ELF dynamic section specification](https://refspecs.linuxfoundation.org/elf/gabi4+/ch5.dynamic.html). The new integration test exercises both Linux and Windows output modes with 11 synthetic fixtures: valid input, ignored entries after DT_NULL, a table ending exactly at EOF, offsets beyond EOF and at UINT64_MAX, wrapped offset/size ranges, a truncated declaration after DT_NULL, a partial entry, a missing terminator and an empty table. The existing invalid-offset test uses the new segment diagnostic. ### Tested Windows 11 x64, WinLibs MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), CMake 4.4.4, Ninja 1.13.2, Python 3.12.14. Base: 5895ea721a43f48547cc996aee0bb65b603622ac. Change: cd0e534d. - Regression fails against a separately linked executable with the base ElfReader object; malformed Linux inputs return success and produce output. It passes with this change in both output modes. - Focused CTest: dynamic_segment, elf_offsets, elf_header, string_table_bounds and strict_nid_filter all pass. - Full Release build with BUILD_TESTING and ANYPS5_ENABLE_SPIRV_TOOLS passes, including the libs target. - Complete CTest suite: 358/358 pass, zero skips, under the normal Windows user account (162.58 seconds). Includes GPU execution tests on the RTX 3080. - Generated synthetic PE fixtures used a dedicated temporary folder excluded from Defender scanning, with Smart App Control off. Defender real-time protection remained enabled. - No console or commercial-title testing. ### Checklist - [x] Based on inspected main; no other open PR implements this correction - [x] One topic per PR; follow-ups go in a new PR - [x] No comments added in code - [x] Invalid input throws; no silent stubs added - [x] No notes, investigation files or images added to the upstream repository - [x] No third-party dependencies added - [x] Generic behaviour, independent of a title - [x] Depends on: none - [x] AI-assisted: yes
011010 commented 2026-10-06 22:27:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-10-06T22:27:46Z.


Independent verification (relinker-only, builds anywhere).

Built the relinker from pr930 standalone on macOS 26.6.2 (arm64), Apple Clang 21, C++20:

  • test_dynamic_segment.py passes, test_elf_offsets.py passes, and the other 19 relinker Python tests in the tree pass (21/21).

To confirm the test exercises the fix rather than passing vacuously, I ran it against main (c1241ad): it fails, and the failures are the behaviour this PR corrects. On main, with Linux output the malformed inputs are accepted:

  • truncated-after-null: rc 0, output file produced
  • partial-entry: rc 0, output file produced
  • missing-null: rc 0, output file produced

The test expects them rejected. On Windows output, main also accepted truncated-after-null from the dynamic reader and stopped later in code analysis, as the description says. With this change both modes reject them at parse time with a segment diagnostic.

Limits: no Windows host here, so I only observed the Linux path and the parse-time rejection; the Windows execution side is covered by your run. No console or commercial title involved.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6026603292 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/930#issuecomment-6026603292). Originally posted by [011010](https://github.com/011010) on 2026-10-06T22:27:46Z. --- Independent verification (relinker-only, builds anywhere). Built the relinker from `pr930` standalone on macOS 26.6.2 (arm64), Apple Clang 21, C++20: - `test_dynamic_segment.py` passes, `test_elf_offsets.py` passes, and the other 19 relinker Python tests in the tree pass (21/21). To confirm the test exercises the fix rather than passing vacuously, I ran it against `main` (`c1241ad`): it fails, and the failures are the behaviour this PR corrects. On `main`, with Linux output the malformed inputs are accepted: - `truncated-after-null`: rc 0, output file produced - `partial-entry`: rc 0, output file produced - `missing-null`: rc 0, output file produced The test expects them rejected. On Windows output, `main` also accepted `truncated-after-null` from the dynamic reader and stopped later in code analysis, as the description says. With this change both modes reject them at parse time with a segment diagnostic. Limits: no Windows host here, so I only observed the Linux path and the parse-time rejection; the Windows execution side is covered by your run. No console or commercial title involved.
ebarr24 commented 2026-10-06 23:49:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ebarr24 on 2026-10-06T23:49:11Z.


@011010 Thanks for the independent macOS relinker verification and for checking that the same regression fails on c1241ad for the intended malformed-segment behavior. Your 21/21 result adds coverage beyond my Windows runs. I'll keep the reported Linux-output and parse-time checks distinct from Windows guest execution and console/title verification.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6027634271 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/930#issuecomment-6027634271). Originally posted by [ebarr24](https://github.com/ebarr24) on 2026-10-06T23:49:11Z. --- @011010 Thanks for the independent macOS relinker verification and for checking that the same regression fails on `c1241ad` for the intended malformed-segment behavior. Your 21/21 result adds coverage beyond my Windows runs. I'll keep the reported Linux-output and parse-time checks distinct from Windows guest execution and console/title verification.
Sign in to join this conversation.
No reviewers
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!930
No description provided.