Skip to contentSkip to conversation

patchelf: Fix e2fsprogs ELF corruption

PATCH New Joshua Watt 1 participant 1 message Jul 31, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 patchelf: Fix e2fsprogs ELF corruption

Patchelf has a nasty bug where calling --set-interpreter repeatedly on the same program will eventually corrupt the program headers in a way that causes a segmentation fault when the loader attempts to load the program. In most cases, we don't reach this limit, but for recipes that make multiple links to the same program with different names (e.g. e2fsprogs-native), this limit can be reached, corrupting the program.

This doesn't fix the underlying issue as it is complicated, but instead prevents the interpreter program from being re-written if it hasn't changed (which is for the best anyway as each call would make the program larger).

---
 ...thing-if-interpreter-has-not-changed.patch | 38 +++++++++++++++++++
 .../patchelf/patchelf_0.18.0.bb               |  4 +-
 2 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 meta/recipes-devtools/patchelf/patchelf/0001-Do-nothing-if-interpreter-has-not-changed.patch
meta/recipes-devtools/patchelf/patchelf/0001-Do-nothing-if-interpreter-has-not-changed.patch new file +38 -0
new file mode 100644
index 00000000000..0183d212259
@@ -0,0 +1,38 @@
+From 09d182d9dfde5d68c4481edfa1bb77453b111380 Mon Sep 17 00:00:00 2001
+From: Joshua Watt <[email protected]>
+Date: Mon, 31 Jul 2023 13:15:27 -0600
+Subject: [PATCH] Do nothing if interpreter has not changed
+
+If there is not change to the interpreter, don't make any modifications
+to the file. This is more efficient than requiring programs to check
+before changing, and also prevents thrashing of the ELF file, since
+changing the interpreter repeatedly will keep growing the ELF file each
+time, even if it's the same value.
+
+This is particularly helpful where the same binary is symlinked or
+hardlinked with multiple names and has patchelf run on it.
+
+Signed-off-by: Joshua Watt <[email protected]>
+Upstream-Status: Submitted [https://github.com/NixOS/patchelf/pull/512]
+---
+ src/patchelf.cc | 4 ++++
+ 1 file changed, 4 insertions(+)
+
+diff --git a/src/patchelf.cc b/src/patchelf.cc
+index 82b4b46..bc40026 100644
+--- a/src/patchelf.cc
++++ b/src/patchelf.cc
+@@ -1454,6 +1454,10 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
+ template<ElfFileParams>
+ void ElfFile<ElfFileParamNames>::setInterpreter(const std::string & newInterpreter)
+ {
++    if (getInterpreter() == newInterpreter) {
++        return;
++    }
++
+     std::string & section = replaceSection(".interp", newInterpreter.size() + 1);
+     setSubstr(section, 0, newInterpreter + '\0');
+     changed = true;
+-- 
+2.33.0
+
meta/recipes-devtools/patchelf/patchelf_0.18.0.bb +3 -1
index e8bc2ff83dd..3aec0030cf4 100644
@@ -4,7 +4,9 @@ HOMEPAGE = "https://github.com/NixOS/patchelf"
 
 LICENSE = "GPL-3.0-only"
 
-SRC_URI = "git://github.com/NixOS/patchelf;protocol=https;branch=master"
+SRC_URI = "git://github.com/NixOS/patchelf;protocol=https;branch=master \
+           file://0001-Do-nothing-if-interpreter-has-not-changed.patch \
+           "
 SRCREV = "99c24238981b7b1084313aca8f5c493bb46f302c"
 
 S = "${WORKDIR}/git"
Signature · 1 line
2.33.0