Live data from Hacker News

PatchELF: Simple utility for modifying existing ELF executables and libraries

github.com

11–20 of 42 posts

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#11

Apart from adapting proprietary or hard-to-compile software to non-FHS distros like NixOS or Guix, what are the usecases of patchelf?

Needing a LD_PRELOAD on a system where LD_PRELOAD is blocked.

Sure it still requires you to have rights to create+run a executable.

But it's a much better model then LD_PRELOAD as it can only be used on executable you can write to, which normally means you your user/group but not other user/group owned executables. Which is especially relevant with suid/sgid.

You can also use this, to patch a library which is compiled in a way where LD_PRELOAD doesn't work. Like suid binaries. Through you have to potentially make a copy and set the suid bit again.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#12
post #7

Apart from adapting proprietary or hard-to-compile software to non-FHS distros like NixOS or Guix, what are the usecases of patchelf?

Lots of Python packages containing native code are optionally distributed with pre-compiled libraries that have been modified with patchelf. I believe both auditwheel ( https://pypi.org/project/auditwheel/ ) and conda-build do this.

Yes. Auditwheel inspects the native python parts and shoves any external .so dependencies into the wheel, making it hermetic.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#13

Apart from adapting proprietary or hard-to-compile software to non-FHS distros like NixOS or Guix, what are the usecases of patchelf?

We use this at my workplace to add $ORIGIN-relative rpaths to third-party dependencies of internal code, since we want to distribute/version those along with the code, not with the OS, and we also don't want to distribute them in the medium of OS packages (so you don't have to be root, so we don't risk messing up the base OS, etc.). In many senses that's just a "non-FHS distro," but it's arguably not a distro at all.

Even for easy-to-compile third-party code, squeezing -Wl,-rpath into all the right places is more of a pain than you'd hope, so we just run patchelf on everything in the third-party directory at the end of the build, regardless of whether the "build" is an actual build or just an untar of proprietary software.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#14
post #3

Apart from adapting proprietary or hard-to-compile software to non-FHS distros like NixOS or Guix, what are the usecases of patchelf?

Some of the things it does are extremely handy for simple privilege escalation attacks.

Got a link to more reading on this? Super interested.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#16
Patchelf is almost always broken, and when they merge fixes they don't release a new version right away. Between 2014-2019 running patchelf+strip would corrupt binaries, after the fix running patchelf twice on the same binary would corrupt it. And now patchelf doesn't work on all binaries, there's a bugfix merged https://github.com/NixOS/patchelf/pull/230 merged Nov 19, 2020, but no release since then.

Also patchelf exposed a bug in ldconfig noted 11 years ago and only fixed in glibc 2.31: https://nix-dev.science.uu.narkive.com/q6Ww5fyO/ldconfig-pro...

And currently patchelf still has a bug: https://github.com/NixOS/patchelf/pull/275

Apparently elf files are hard.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#17

Patchelf is almost always broken, and when they merge fixes they don't release a new version right away. Between 2014-2019 running patchelf+strip would corrupt binaries, after the fix running patchelf twice on the same binary would corrupt it. And now patchelf doesn't work on all binaries, there's a bugfix merged https://github.com/NixOS/patchelf/pull/230 merged Nov 19, 2020, but no release since then. Also patchelf…

To be fair, patching existing binaries isn’t all that easy to begin with. Once you need to start moving around structures, a lot of bets are off. In case of NixOS, I’m sure the long nix store paths added often overflow structures and require workarounds to avoid breaking images. Hacking around Win32 PE images, I’ve run into a lot of tricky subtle issues over time...

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#18
post #17

Patchelf is almost always broken, and when they merge fixes they don't release a new version right away. Between 2014-2019 running patchelf+strip would corrupt binaries, after the fix running patchelf twice on the same binary would corrupt it. And now patchelf doesn't work on all binaries, there's a bugfix merged https://github.com/NixOS/patchelf/pull/230 merged Nov 19, 2020, but no release since then. Also patchelf…

To be fair, patching existing binaries isn’t all that easy to begin with. Once you need to start moving around structures, a lot of bets are off. In case of NixOS, I’m sure the long nix store paths added often overflow structures and require workarounds to avoid breaking images. Hacking around Win32 PE images, I’ve run into a lot of tricky subtle issues over time...

Yeah, it wasn't meant to criticize the project as a whole; I'm happy that at least someone took on the project of "growing" the rpath section... My only point of critique is they should immediately tag a new version after a bug fix, cause many distro's have buggy versions of patchelf.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#19
post #3

Earlier quoted context omitted.

Some of the things it does are extremely handy for simple privilege escalation attacks.

Got a link to more reading on this? Super interested.

It’s an easy way to modify the binary to load extra code, which can alter how it behaves.

Re: PatchELF: Simple utility for modifying existing ELF executables and libraries

#20
post #5

Apart from adapting proprietary or hard-to-compile software to non-FHS distros like NixOS or Guix, what are the usecases of patchelf?

I've used it to edit a binary's rpath as part of relocating binaries that really want to be installed at a certain path. You can do this yourself with e.g. hexedit if your new path is shorter but if it's longer you need something smarter, like patchelf.

You don't even need to patch it to include a certain path, you can use (and I have used) it to include $ORIGIN or $ORIGIN/../lib in programs' RPATHs.

Be careful about when this is and isn't okay though. When used wrong this can be a security risk, such as when your browser auto-downloads files to a Downloads folder, and the program you are patching is also in there.

Post reply on HN