fix(relinker): validate the PT_TLS layout when reading program headers #7098
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7098"
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 jotitadev on 2026-10-17T11:20:48Z.
What
Linux conversion accepts a
PT_TLSthat the Windows and macOS targets refuse, and writes it unchanged into the output program header table. Four layouts, each on a minimal executable (theConversionTestsfixture plus thePT_TLSheaders in the table),--skip-sce-module:PT_TLS--windows/--macosp_vaddr0x900000,p_filesz8, outside everyPT_LOADELF address is not contained in PT_LOAD memoryp_filesz16 >p_memsz8Invalid or unsupported ELF TLS layoutp_align3Invalid or unsupported ELF TLS layoutPT_TLSheadersMultiple ELF TLS segmentsThe dynamic loader takes the TLS initialization image from
p_vaddr(relative to the load base) and copiesp_fileszbytes of it, then clearsp_memsz - p_filesz, so with the first layout it reads memory the image does not map and with the second it clears a wrapped length. Its TLS offset rounding assumes a power-of-two alignment, and with two headers it keeps only one of them. Guest modules (GuestImageReader) already refuse the size, alignment and duplicate cases but not the address one.ElfReader::ReadProgramHeadersnow rejects a secondPT_TLS, aPT_TLSwhose file size exceeds its memory size or whose alignment is not 0 or a power of two (same messages as the Windows and macOS writers), and a non-empty initialization image that is not inside the memory of onePT_LOAD(after the uncoveredPT_SCE_RELROpromotion). An emptyPT_TLSplaceholder (p_filesz0) is still accepted wherever it points, astest_empty_tlsexpects.elf_reader_boundshas six new cases: images inside load memory plus empty placeholders (accepted), a non-empty image inside aPT_SCE_RELROthat noPT_LOADcovers (accepted, which pins the TLS check after the RELRO promotion), images below, across the end of, and past the load (rejected), file size over memory size, alignment 3, and a secondPT_TLS. The four rejection cases fail without theElfReaderchange.Found by fuzzing the input parsing (
Relinker::Conversionunder libFuzzer with ASan and UBSan, executables and guest modules, Linux/Windows/macOS targets). No sanitizer reports came out of that run; this was the clearest accepted-invalid layout.Tested
macOS 27.2 on an Apple M2 Max, Xcode 27, relinker-only Release build with
BUILD_TESTING=ON, arm64, onmain5d9dcc42d:elf_reader_boundsincluded (173/173 onmaintoo).maince991f1ec).tools/check_conventions.py --base origin/mainpasses.Linux x86_64 (Ubuntu 26.04, GCC 15.2, CMake 4.2.3), relinker-only Release build with
BUILD_TESTING=ON, onmain5d9dcc42d:ElfReaderchange):elf_reader_boundsfails on exactly the four rejection cases (TLS image outside load memory,TLS file size exceeds memory size,TLS alignment is not a power of two,second TLS segment: "Invalid program header was accepted"); the accepted-layouts case passes.PT_SCE_RELROcase (added after review): passes on this branch, and fails withPT_TLS initialization image is outside PT_LOAD memorywhen the TLS loop is moved before the RELRO promotion.elf_reader_boundsandelf_offsetspass; the full suite is 180/182 with and without this change (the same twofile_writer_binary/file_writer_textfailures, which come from the host's coreutils and also fail onmain). No new compiler warnings.Not tested on a Windows host, and the loader behaviour above comes from the glibc sources, not from running the old output.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by OskarLGS on 2026-10-17T21:20:33Z.
Review: COMMENTED
Reviewed on Linux x86-64 (GCC 13, relinker-only build), merged into current main: no conflicts, all 187 ctest tests pass and check_conventions.py passes. With the ElfReader.cpp change reverted, the four new rejection cases in elf_reader_bounds fail, so the tests cover the fix.
Checked:
Suggestion: no test covers the PS4 case above, which relies on the RELRO conversion loop running first. A fixture with a non-empty PT_TLS inside a PT_SCE_RELRO that no PT_LOAD covers, expected to be accepted, would keep that ordering from regressing.
Review done with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by Baconzido on 2026-10-17T21:41:42Z.
macOS result for the relinker-only build, since the description lists Windows as untested.
Apple M5 Pro (arm64), macOS 27.0.1, AppleClang 21.0.0, CMake 4.4.4, Python 3.14.7, Release relinker-only,
BUILD_TESTING=ON. Headce316bfcemerged ontomain922dc8999(clean merge).elf_reader_boundsandelf_offsetspass.ElfReader.cppreverted tomain, test file kept:elf_reader_boundsfails on exactly the four rejection cases (TLS image outside load memory,TLS file size exceeds memory size,TLS alignment is not a power of two,second TLS segment: "Invalid program header was accepted"). Restoring the change passes again.mainalone: 178/178.Not checked: Windows and Linux; I did not run the four conversion layouts through the CLI on the three targets, only the unit cases.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T23:07:46Z.
@OskarLGS thanks, added in
9f000e861:TLS image inside uncovered SCE RELROinelf_reader_bounds. It turns the fixture's onlyPT_LOADinto aPT_SCE_RELROand puts a non-emptyPT_TLS(p_vaddr0x1008,p_filesz8,p_memsz16) inside it, then expects the headers to be accepted with the RELRO promoted toPT_LOAD.Linux x86_64 (Ubuntu 26.04, GCC 15.2), relinker-only Release,
BUILD_TESTING=ON, onmain5d9dcc42d:main(the twofile_writer_binary/file_writer_texthost coreutils failures).PT_TLS initialization image is outside PT_LOAD memory. Restoring the order passes again.tools/check_conventions.pypasses; the branch still merges cleanly into currentmain.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T15:04:13Z.
Cross-validation on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, PR head
9f000e861scratch-merged onto maina62c724c3(clean; #7037 merges on top of it cleanly too). Relinker, its tests andlibsbuilt; no full-tree build, no GPU involved.elf_reader_bounds,elf_offsets,empty_tls,entry_point_bounds,init_array_roots,executable_segments,tls_diagnostics,guest_tls,relinker_conversion: 3 of 3 with--repeat until-fail:3.ElfReader.cppfrom main, test file kept:elf_reader_boundsfails on exactlyTLS image outside load memory,TLS file size exceeds memory size,TLS alignment is not a power of twoandsecond TLS segment("Invalid program header was accepted"); the accepted-layout and uncovered-RELRO cases pass. With the change all pass.windows_import_modulesneeds the helper DLLs, which my targeted build lacked at first, and passes once built.eboot.elf) with main's relinker and with this PR:--windowsgives identical SHA-256 for all 312 output files (50 rc 0, PPSA99995 rc 2 in both with the sameUnsupported Windows guest TLS instructionfailure), and the Linux target gives identical SHA-256 for all 122 files, all rc 0. No local title has aPT_TLSthe new checks refuse.PT_LOADcovering 0..0x1000,--skip-sce-module): main accepts outside-load, filesz > memsz, align 3 and two-PT_TLSon Linux and refuses them on Windows/macOS; with the PR all four are refused (rc 2) on the Linux, Windows and macOS targets. APT_TLSinside the load and an empty placeholder at 0x900000 give the same output as main.tools/check_conventions.py --base origin/mainandgit diff --check: pass.One small thing in the diff:
PT_TLS initialization image is outside PT_LOAD memoryprintstls.Offset(0x800 for my case), while the Windows/macOS message it replaces printed the address (ELF address is not contained in PT_LOAD memory (offset 0x900000)).tls.MappedAddresswould be the more useful value there; not blocking.Not checked: macOS host, running a relinked executable, full-tree build, the non-GPU suite.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).