fix(relinker): reject executable TLS relocations against an undefined symbol that is not STT_TLS #12341
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12341"
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 roboterspinne on 2026-10-28T10:19:13Z.
What
On
3a40945e62(line numbers below refer to it),RelinkerPipeline::Relinkchecks the symbol of an executableR_X86_64_DTPMOD64,DTPOFF64orTPOFF64relocation indefinedTlsSymbolValue, which rejects a defined symbol that is notSTT_TLS(line 353) but returns early for every undefined one (line 350), so an undefinedSTT_NOTYPE,STT_OBJECTorSTT_FUNCsymbol becomes an import, andSysVDynamicSectionBuilder(tls ? STT_TLS : ref.SymbolType, line 95) writes it typedSTT_TLS, a type the input never declared. On untouched62b3ba2522such a relocation against the undefinedImportedTlswithst_info0x10, 0x11 or 0x12 converts with exit 0 on Linux and--lazy-binding, andDT_RELAof the output holds(0x401000, 18, ImportedTls, STT_TLS, 0)forTPOFF64(likewise 16 and 17, and at 0x1000 with--lazy-binding);--windowsand--macosfail only later withImported TLS relocation N is not supported for this target (offset 0x1000). The guest reader already rejects the same relocation in guest modules asInvalid guest TLS relocation(GuestImageReader.cpp:285-286).definedTlsSymbolValuenow throwsTLS relocation names an undefined symbol that is not STT_TLSat the relocation for an undefined symbol whose type is notSTT_TLS, before any import is recorded, on every target; the defined-symbol message of line 353 is mirrored, and that branch now compares the type read for the new check instead of maskingst_infoagain. The ELF gABI (Symbol Table,STT_TLS) states that thread-local storage relocations can only referenceSTT_TLSsymbols. Both callers (line 404 inextractRela, throughdefinedTlsSymbolat 364-366, and line 628) run only for these three types with a nonzero symbol index, so symbolless TLS relocations and every other relocation are unchanged, and the symbol-bounds check ofsymbolEntryPosstill fails first.STT_SECTIONsymbols are covered too. The guest reader acceptsDTPMOD64andDTPOFF64against a local section symbol (st_info3) with section and value 0 (ownTlsBlock,6f3e4a147f,libSceFios2.prxof CUSA01664), and this change does not touch it; in an executable that unnamed shape already failed on62b3ba2522withUnnamed import symbol is not a null symbol entry (offset 0x4948)for all three types on all four targets, and now fails one check earlier with the new message (offset 0x4700). A named undefinedSTT_SECTIONsymbol (st_info0x13) converted on Linux and--lazy-bindingwith the import typedSTT_TLSand failed on--windowsand--macoswithImported TLS relocation N is not supported for this target; it now fails with the new message on every target. An all-zero symbol entry at a nonzero index isSTT_NOTYPEand now fails as well; before, Linux and--lazy-bindingemitted it as an unnamedSTT_NOTYPEimport, and the guest reader rejects it too. UndefinedSTT_TLSimports, global or weak, are unchanged. #11881 adds the sibling rule, non-TLS relocations againstSTT_TLSsymbols; the two are independent and merge without conflict. With this rule every symbol a TLS relocation imports is alreadySTT_TLS, so the override inSysVDynamicSectionBuilder.cpp:95no longer changes any type; removing it is left for a follow-up.test_executable_tls_relocations.pygets a loop after thefailureslist:DTPMOD64,DTPOFF64andTPOFF64against the undefinedIMPORTEDsymbol withst_info0x10, 0x11, 0x12, 0x13, 0x1a (STT_GNU_IFUNC) and 0x1e and, with the name field zeroed, 0x03, on Linux,--lazy-binding,--windowsand--macos(84 relinks), each expecting exit 2,FAIL: TLS relocation names an undefined symbol that is not STT_TLS (offset 0x4700)and no output. The two types aboveSTT_TLSmake the loop fail on a check written astype < STT_TLS(atlinux-undefined-16-1a) or one masking the type with 7 instead of 15 (atlinux-undefined-16-1e); every otherst_infoin the loop has a type below 6. The existing case withIMPORTEDasSTT_TLSstill expects(0x401020, TPOFF64, ImportedTls, STT_TLS, 0)on Linux.test_call_registry.py(loop at lines 102-117, added by #10679 after this branch's first base) relinks the fixture's undefined symbol with relocation types 16, 17 and 18 and expects exit 0; with this rule it fails at type 16 withFAIL: TLS relocation names an undefined symbol that is not STT_TLS (offset 0x700). The loop now sets the symbol'sst_infoto 0x06 (STT_TLS, binding unchanged) for those three types only; the other four types keep 0x00, because #11881 rejects non-TLS relocations againstSTT_TLSsymbols. The name assertion is unchanged, and the three TLS cases still reach the registry on Linux withR_X86_64_DTPMOD64,R_X86_64_DTPOFF64andR_X86_64_TPOFF64. That loop, which uses the fixture's defaultst_info0x00 (STB_LOCAL,STT_NOTYPE) only to read the relocation's name from the registry, is the only repository test that relinked a TLS relocation against an undefined non-TLS symbol; no debt entry, issue or commit shows that shape.pr_overlap.pyreports no shared lines with open pull requests that merge withmain. It cannot compare this branch with #10861 (RelinkerPipeline.cpp), because that pull request does not merge withmain. Checked by hand at #10861's headbb3f33f1ae: a trial merge of this branch with #10861 conflicts only inRelinkerPipeline.cpp, in the same single block asmainwith #10861 (the#includelist,main's<iterator>at line 8 against #10861's<bit>); none of this change's lines are involved. #11881 adds the converse rule inextractRelaand editstest_executable_tls_relocations.py; the two merge without conflict. #11626 adds a loop totest_call_registry.pybefore thenamesloop (after line 48); it shares no lines with the two lines this change adds after line 107, and the two merge without conflict.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.executable_tls_relocationswith the new loop fails on untouched62b3ba2522at its first case withAssertionError: ('linux-undefined-16-10', 0, ''), and on a first version of this change that also exempted undefinedSTT_SECTIONsymbols, atAssertionError: ('linux-undefined-16-13', 0, ''). On90fb3186bc(871517aaeemerged) with theRelinkerPipeline.cppof871517aaeerestored, it fails again atAssertionError: ('linux-undefined-16-10', 0, '')whilecall_registrypasses. Run one by one on62b3ba2522, all 18 Linux and--lazy-bindingcases withst_info0x10, 0x11 and 0x12 exit 0 with the import typedSTT_TLSinDT_RELA, and all 18--windowsand--macoscases exit 2 withImported TLS relocation N is not supported for this target (offset 0x1000). On that first version, which handlesSTT_SECTIONexactly as62b3ba2522, the named 0x13 case exits 0 on Linux and--lazy-bindingwithDT_RELAholding(0x401000, 16|17|18, ImportedTls, STT_TLS, 0)(0x1000 with--lazy-binding) and exits 2 on--windowsand--macoswithImported TLS relocation N is not supported for this target (offset 0x1000), and the unnamed 0x03 case exits 2 withUnnamed import symbol is not a null symbol entry (offset 0x4948)on all four targets for all three types. With the change all 84 exit 2 with the new message and write no output; undefinedSTT_TLSimports withst_info0x16 and 0x26 still convert on Linux and--lazy-bindingwith the import typedSTT_TLSand fail on--windowsand--macosas before. An all-zero entry at theIMPORTEDindex converted on Linux and--lazy-bindingon62b3ba2522and now fails with the new message on every target. Registry fixture with types 16, 17 and 18 on Linux and--lazy-binding:st_info0x00 exits 2 with the new message, 0x06 and 0x16 exit 0 with the matchingrelocationTypein the registry.53d9015e9d: with the undefined-symbol check written astype < STT_TLS,executable_tls_relocationsfails atAssertionError: ('linux-undefined-16-1a', 0, ''); with the type masked by 7, atAssertionError: ('linux-undefined-16-1e', 0, '').53d9015e9d(3a40945e62merged):ctest -R 'tls|import_symbol|executable_|null_symbol|section_symbol|guest_|call_registry|elf_header|restricted' -j 4: 90 tests passed in 46.9 s; run alone,executable_tls_relocationstook 9.5 s,call_registry3.1 s,import_symbol_types2.9 s.ctest --test-dir build-relinker -j 5on53d9015e9d: 388 tests, 386 passed,long_pathsskipped; the one failure,select_tests(Windows symlink fixture), fails onmainon this machine as well.python tools/check_conventions.py --base upstream/mainwith this title and description passed on53d9015e9d.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)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T10:46:27Z.
Review: APPROVED
Reviewed
2de9d68a58merged ontomain3a40945e62, macOS arm64 (Apple clang), relinker-only Release build.ctest -j 8: 378/378 passed.executable_tls_relocationsfail;call_registrystill passes.STT_SECTION(&& type != 3): killedSTT_NOTYPE: killedtype < STT_TLSinstead oftype != STT_TLS: survives& 7instead of& 15for the type nibble: survivesThe two survivors are there because every non-TLS type in the new loop is below 6 (0x10–0x13, 0x03). So nothing checks an undefined symbol whose type is above
STT_TLS, such asSTT_GNU_IFUNC(10) orSTT_HIPROC(15). Adding(0x1a, IMPORT_NAME)and(0x1e, IMPORT_NAME)to theinfotuple kills both mutants, the head still passes, and the test takes 0.65 s:A small optional cleanup: the defined-symbol branch right below it could reuse
typeinstead of(raw[symOff + 4] & 15).check_conventions.py --base upstream/mainpassed.pr_overlap.pyflags only #10861, which can't be compared; your manual check of #10861 (an#include-block conflict only) agrees with the code. The rule matches the gABISTT_TLSrequirement and the guest reader's existingInvalid guest TLS relocation. Approving; the test addition above isn't required.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T10:52:12Z.
Review: APPROVED
Reviewed the undefined-symbol type check, its bounds/order relative to
symbolEntryPos, and both fixture updates. The new matrix exercises all three executable TLS relocation types against undefined non-TLS symbol types across the target modes; the registry fixture now explicitly usesSTT_TLS, preserving its intended case. Hosted Linux and Windows relinker plus ASAN checks pass. The macOS x86_64 relinker check remains pending.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T11:40:49Z.
Review: COMMENTED
Re-reviewed exact head
53d9015e9dec23d7654b2c3d1480898c47a3cc4a. The added undefined-symbol cases include types aboveSTT_TLS, so thetype != STT_TLSguard is exercised beyond the earlier low-valued types; the call-registry fixture now usesSTT_TLSfor the relocation types it expects to remain valid. I configured and built the relinker-only tree on Linux (cmake -S . -B build -G Ninja -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON;cmake --build build --parallel 8, 293/293 steps), then ranctest --test-dir build -R '^(executable_tls_relocations|call_registry)$' --output-on-failure -V: 2/2 passed (8.57s and 5.07s). The hosted Linux/Windows/macOS relinker checks for this head are still pending, so I have not approved yet.