diff mbox series

patchelf: Fix e2fsprogs ELF corruption

Message ID 20230731202638.2291210-1-JPEWhacker@gmail.com
State New
Headers show
Series patchelf: Fix e2fsprogs ELF corruption | expand

Commit Message

Joshua Watt July 31, 2023, 8:26 p.m. UTC
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).

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
---
 ...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
diff mbox series

Patch

diff --git a/meta/recipes-devtools/patchelf/patchelf/0001-Do-nothing-if-interpreter-has-not-changed.patch b/meta/recipes-devtools/patchelf/patchelf/0001-Do-nothing-if-interpreter-has-not-changed.patch
new file mode 100644
index 00000000000..0183d212259
--- /dev/null
+++ b/meta/recipes-devtools/patchelf/patchelf/0001-Do-nothing-if-interpreter-has-not-changed.patch
@@ -0,0 +1,38 @@ 
+From 09d182d9dfde5d68c4481edfa1bb77453b111380 Mon Sep 17 00:00:00 2001
+From: Joshua Watt <JPEWhacker@gmail.com>
+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 <JPEWhacker@gmail.com>
+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
+
diff --git a/meta/recipes-devtools/patchelf/patchelf_0.18.0.bb b/meta/recipes-devtools/patchelf/patchelf_0.18.0.bb
index e8bc2ff83dd..3aec0030cf4 100644
--- a/meta/recipes-devtools/patchelf/patchelf_0.18.0.bb
+++ b/meta/recipes-devtools/patchelf/patchelf_0.18.0.bb
@@ -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"