Repository navigation
Fix PT_LOAD overlap/PHDR corruption when interpreter and RPATH both grow - #668
Open
michalsieron wants to merge 2 commits into
Open
michalsieron wants to merge 2 commits into
michalsieron wants to merge 2 commits into
Conversation
rewriteSectionsExecutable() reserves a region at the front of the file for sections it needs to relocate (.interp, notes, .gnu.hash, .dynsym, .dynstr, .dynamic, ...), and either packs them in-place before the next unrelated section, or grows the file (shiftFile()) to make room. Two related bugs let a *second*, unrelated patchelf invocation corrupt a file that a *first* invocation had already caused to grow (e.g. --set-interpreter followed by --set-rpath, when both need extra space): 1. normalizeNoteSegments() can append an extra phdr (splitting a PT_NOTE), growing the program header table *after* an earlier invocation had already sized the small PT_LOAD covering the headers. The code used the post-split header end to look up that PT_LOAD, found no match, and silently skipped extending it -- leaving PT_PHDR sticking out past the end of its covering PT_LOAD (readelf: "the PHDR segment is not covered by a LOAD segment"). 2. Even once PT_PHDR is found, deciding whether to pack in-place or grow the file only compared the reserved area's size against the offset of the next unreplaced *section*. If an earlier invocation had already split the file into a small header PT_LOAD plus the original big PT_LOAD, that next section can lie deep inside the big PT_LOAD, far past where the small header PT_LOAD currently ends. Packing in-place then silently grew the header PT_LOAD so that it overlapped the big PT_LOAD (with conflicting flags, e.g. RW vs R+E), so any writable data relocated into the overlap (typically .dynamic, needed by the dynamic linker at startup) ended up mapped without write permission at runtime, crashing the program. Fix: - Look up the PT_LOAD covering the header area using the header-table end offset as it stood *before* normalizeNoteSegments() runs, since that is the value it was actually last sized for. - Clamp the reserved area's end offset down to the start of the nearest intervening PT_LOAD segment before deciding whether to pack in-place, so the file-growing path is taken whenever doing so is necessary to avoid overlapping that segment. Verified against a real-world binary (cc1 from [1]) that segfaulted after patching in a Yocto environment, and with a new, much smaller regression test (see next commit). Additionally fixes NixOS#524. [1]: https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi.tar.xz Co-authored-by: Cursor <[email protected]>
Add many-syms-main-no-pie: a non-PIE variant of many-syms-main with a large .dynsym/.dynstr (one entry per imported symbol from libmany-syms.so) and no pre-existing RPATH/RUNPATH. grow-interpreter-and-rpath.sh uses it to reproduce, on a small binary, the bug fixed in the previous commit: 1. Growing the interpreter (by prepending extra leading slashes, which the OS collapses so it still resolves to the same file) is enough to make rewriteSectionsExecutable() split the file into a small header PT_LOAD plus the original big PT_LOAD. 2. Adding an RPATH afterwards adds a new DT_RUNPATH entry, forcing .dynamic and .dynstr to be replaced/relocated into the reserved area at the front of the file -- an area whose required size (driven by the large .dynsym/.dynstr) exceeds what the small header PT_LOAD from step 1 can hold without overlapping the next PT_LOAD segment. Without the fix, this reliably segfaults on startup; confirmed the new test fails (signal 11) when run against the pre-fix code, and passes with it. Co-authored-by: Cursor <[email protected]>
Ten0
pushed a commit
to nix-community/nixos-vscode-server
that referenced
this pull request
Nov 7, 2026
After updating Windows VS Code, Server 1.144.1 (Node 24.22.1) segfaulted on NixOS-WSL with patchelf 0.19.2. Use the same patching order as [VS Code's server launcher](https://github.com/microsoft/vscode/blob/261c84cb1eddf11235f9e2c921425e9663fbe556/resources/server/bin/code-server-linux.sh), following [patchelf #524](NixOS/patchelf#524). See also [patchelf #668](NixOS/patchelf#668).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I found that patching binaries from https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi.tar.xz done in a Yocto environment makes them segfault. After minimizing the reproduction to simple:
(LOADER="/lib64/ld-linux-x86-64.so.2"; cp cc1.orig cc1.patched; ./src/patchelf --set-interpreter "/${LOADER}" --set-rpath "${LOADER}/../" cc1.patched; ./cc1.patched --version)I threw it at Cursor and here are the results.Seems my case was identical (or very similar) to #524 as changing the order of operations did fix my reproduction, so the assumption is that this PR fixes #524.