Skip to content

patchelf --set-rpath corrupts this executable #152

Closed

Description

@ezquat

I'm running patchelf --set-rpath on the executable from a stock apt package, influxdb version 1.5.3, and it puts the executable into a broken state: it segfaults on startup.

Running it in gdb, I discovered that ld-linux.so is trying to write to a location in the .dynamic section (specifically the DEBUG entry in that section), which is located at 0x400300. The memory exists and was mapped in by ld-linux.so and is perfectly readable.

The problem seems to be that the memory was mapped as read-only (using mmap with the argument PROT_READ|PROT_EXEC, as shown by strace). Now, the reason for that seems to be that the ELF program headers have a LOAD segment that encompasses the .dynamic section (and a lot else), and that segment is marked "read and execute" (but not write). The original binary (and indeed, the transformed one) have another LOAD segment that starts where the .dynamic section starts, and which is read-write. So, I believe that patchelf chose to relocate the .dynamic section from inside a read-write LOAD segment into a read-only one, which would seem to be invalid.

I don't know the algorithms of patchelf well enough to attempt a fix.

Attached are the relevant readelf outputs from before and after the change. You can see that the original has in its sections:

  [16] .dynamic          DYNAMIC          00000000012bf000  00ebf000
       0000000000000130  0000000000000010  WA      10     0     8

and then in its segments:

  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000814d60 0x0000000000814d60  R E    1000
  LOAD           0x0000000000ebf000 0x00000000012bf000 0x00000000012bf000
                 0x000000000005b620 0x0000000000081538  RW     1000

But the patched one has in its sections

  [ 1] .dynamic          DYNAMIC          0000000000400270  00000270
       0000000000000140  0000000000000010  WA       2     0     8

and then in its segments

  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000814d60 0x0000000000814d60  R E    1000
  LOAD           0x0000000000ebf000 0x00000000012bf000 0x00000000012bf000
                 0x000000000005b620 0x0000000000081538  RW     1000

influxd-rpath.readelf.txt
influxd.readelf.txt

Please help!

Activity

  1. am813nt commented on Aug 2, 2018

    @am813nt
    Contributor

    @ezquat Can you please give more info about the binary: where to get it, OS, what desired rpath should be etc?

  2. ezquat commented on Aug 2, 2018

    @ezquat
    ContributorAuthor

    Yes, the binary is influxd from the influxdb Ubuntu package (version 1.5.3): https://packages.ubuntu.com/xenial/influxdb

    A crucial fact is that influxdb is written in Go, and the Go linker has produced some other unusual ELF layouts.

    In my case, my desired rpath was, I think, $ORIGIN/../lib. It's possible that that matters inasfar as its length might force patchelf to relocate sections. If you have any trouble reproing, let me know, I'll see if I can bring back any more details. Thanks!

  3. ezquat commented on Aug 2, 2018

    @ezquat
    ContributorAuthor

    I think this is a duplicate of #146.

  4. mwhudson commented on Aug 5, 2018

    @mwhudson

    Yes, this looks like exactly the same bug I reported.

  5. domenkozar commented on Jun 9, 2020

    @domenkozar
    Member

    Duplicate of #146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions