如何将 patchelf 与 --set-interpreter 一起使用?

uw2*_*2pw 7 linux binary patch libc

我正在尝试执行以下操作:

\n\n

将简单 test.c 的 libc 从系统默认值(Debian 9.11、libc-2.24.so)更改为 libc 2.27。

\n\n

这是我的尝试:

\n\n
user@pc:~/patchelf_test$ cat test.c \n#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n    printf("hello patchelf\\n");\n\n    return 0;\n}\nuser@pc:~/patchelf_test$ gcc test.c -o test\nuser@pc:~/patchelf_test$ ./test\nhello patchelf\nuser@pc:~/patchelf_test$ ldd test\n    linux-vdso.so.1 (0x00007ffd9d1d8000)\n    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7ea0290000)\n    /lib64/ld-linux-x86-64.so.2 (0x00007f7ea0831000)\nuser@pc:~/patchelf_test$ patchelf --set-interpreter ./libc6-amd64_2.27-3ubuntu1_i386.so test\nwarning: working around a Linux kernel bug by creating a hole of 2093056 bytes in \xe2\x80\x98test\xe2\x80\x99\nuser@pc:~/patchelf_test$ ldd test\n    linux-vdso.so.1 (0x00007fff20b9a000)\n    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa46a35e000)\n    ./libc6-amd64_2.27-3ubuntu1_i386.so => /lib64/ld-linux-x86-64.so.2 (0x00007fa46a900000)\nuser@pc:~/patchelf_test$ ./test\nGNU C Library (Ubuntu GLIBC 2.27-3ubuntu1) stable release version 2.27.\nCopyright (C) 2018 Free Software Foundation, Inc.\nThis is free software; see the source for copying conditions.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.\nCompiled by GNU CC version 7.3.0.\nlibc ABIs: UNIQUE IFUNC\nFor bug reporting instructions, please see:\n<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.\nuser@pc:~/patchelf_test$ \n
Run Code Online (Sandbox Code Playgroud)\n\n

这里的预期结果是它应该运行实际的程序。实际结果是它本身运行链接器

\n\n

问题:我需要如何修改命令来解决这个问题?

\n\n

我还尝试使用 libc6-amd64_2.27-3ubuntu1_i386.ld 而不是 .so:

\n\n
user@pc:~/patchelf_test$ !gcc\ngcc test.c -o test\nuser@pc:~/patchelf_test$ patchelf --set-interpreter ./libc6-amd64_2.27-3ubuntu1_i386.ld test\nwarning: working around a Linux kernel bug by creating a hole of 2093056 bytes in \xe2\x80\x98test\xe2\x80\x99\nuser@pc:~/patchelf_test$ ./test\nSegmentation fault\n
Run Code Online (Sandbox Code Playgroud)\n\n

我还尝试使用我的系统默认 libc,并用它进行修补,只是为了确定 patchelf 完全可以工作,但我再次遇到了同样的问题 - 它执行链接器二进制文件本身,而不是测试二进制文件。

\n\n
user@pc:~/patchelf_test$ !gcc\ngcc test.c -o test\nuser@pc:~/patchelf_test$ patchelf --set-interpreter ./libc_default.so test\nwarning: working around a Linux kernel bug by creating a hole of 2093056 bytes in \xe2\x80\x98test\xe2\x80\x99\nuser@pc:~/patchelf_test$ ./test\nGNU C Library (Debian GLIBC 2.24-11+deb9u4) stable release version 2.24, by Roland McGrath et al.\nCopyright (C) 2016 Free Software Foundation, Inc.\nThis is free software; see the source for copying conditions.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.\nCompiled by GNU CC version 6.3.0 20170516.\nAvailable extensions:\n    crypt add-on version 2.1 by Michael Glad and others\n    GNU Libidn by Simon Josefsson\n    Native POSIX Threads Library by Ulrich Drepper et al\n    BIND-8.2.3-T5B\nlibc ABIs: UNIQUE IFUNC\nFor bug reporting instructions, please see:\n<http://www.debian.org/Bugs/>.\n
Run Code Online (Sandbox Code Playgroud)\n\n

跟踪:

\n\n
$ strace ./test\nexecve("./test", ["./test"], [/* 44 vars */]) = 0\nwrite(1, "GNU C Library (Debian GLIBC 2.24"..., 616GNU C Library (Debian GLIBC 2.24-11+deb9u4) stable release version 2.24, by Roland McGrath et al.\nCopyright (C) 2016 Free Software Foundation, Inc.\nThis is free software; see the source for copying conditions.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.\nCompiled by GNU CC version 6.3.0 20170516.\nAvailable extensions:\n    crypt add-on version 2.1 by Michael Glad and others\n    GNU Libidn by Simon Josefsson\n    Native POSIX Threads Library by Ulrich Drepper et al\n    BIND-8.2.3-T5B\nlibc ABIs: UNIQUE IFUNC\nFor bug reporting instructions, please see:\n<http://www.debian.org/Bugs/>.\n) = 616\nexit_group(0)                           = ?\n+++ exited with 0 +++\n
Run Code Online (Sandbox Code Playgroud)\n

Emp*_*ian 4

这个命令没有任何意义:

patchelf --set-interpreter ./libc6-amd64_2.27-3ubuntu1_i386.so test

libc.so.6ld-linux.so.2完全不一样,并且您设置PT_INTERP为(相当于)libc.so.6

这个命令是“正确的”:

patchelf --set-interpreter ./libc6-amd64_2.27-3ubuntu1_i386.ld test

但结果崩溃很可能是由于系统 GLIBC 是不同的版本。正如我在这里所说的,ld-linuxlibc.so.6 必须完全匹配。

为了使这一切正常工作,您应该摆脱有趣的libc-amd64...命名约定,将 2.27 版本完全提取到eg./libc6-amd64_2.27/目录中,然后使用类似以下内容的内容:

patchelf --set-interpreter ./libc6-amd64_2.27/lib64/ld-linux-x86-64.so.2 test
LD_LIBRARY_PATH=./libc6-amd64_2.27/lib64 ./test
Run Code Online (Sandbox Code Playgroud)