Calls within libc are generally pre-linked. As a result, they can't be interposed using LD_PRELOAD
. This makes it tricky to use LD_PRELOAD
to intercept, e.g., all calls to write
, but there are some workarounds.
Doubling all writes to standard output¶
Suppose we want to use LD_PRELOAD
to force any output to stdout
to happen twice. We'll start with a simple program that writes to stdout
once:
cat ./call_write.c
When we run ./call_write
we get the expected output:
./call_write
If we examine the compiled binary, we can see that the call is made through the PLT (Procedure Linkage Table):
objdump -D call_write | grep 'call.*write'
This means that at run-time, the dynamic linker is responsible for finding the address of write
and patching it into the PLT. This call
instruction will then use the patched table to call the correct function. We can enable debugging-output in the dynamic linker to see that it resolves the write
symbol to libc, as expected:
LD_DEBUG=bindings ./call_write 2>&1 | grep 'symbol.*write'
Interposing a direct call to write works¶
We can interpose this call to write
by using the LD_PRELOAD
environment variable to tell the dynamic linker to try resolving symbols in our library that we provide before any other libraries.
Let's create a small library with our own implementation of write
. We'll simply get the symbol for libc's write
, and then call it twice.
cat interpose_write.c
Using LD_PRELOAD
, this works as expected. The call to write
from our call_fwrite
program is intercepted, and we see the output twice:
LD_PRELOAD=$PWD/interpose_write.so ./call_write
We can also examine the dynamic linker's debug output again to see that it resolves write
twice. First when patching the PLT it finds our write
. At run-time when we ask it for the "next" write
, it finds libc's version:
LD_DEBUG=bindings LD_PRELOAD=$PWD/interpose_write.so ./call_write 2>&1 | grep 'symbol.*write'
Interposing all the writes¶
So far so good. What happens though if the program we're interposing calls some other libc function to write to standard output? They'll all end up eventually needing to make a write
system call, so our wrapper should still work... right?
Let's see what happens if the program calls fwrite
instead of write
:
cat call_fwrite.c
LD_PRELOAD=$PWD/interpose_write.so ./call_fwrite
It didn't work. To find out why, let's look at calls to write
from within libc:
objdump -D /lib/x86_64-linux-gnu/libc.so.6 | grep 'call.*__write@@'
It turns out that none of the internal calls to write
actually use the write
symbol. They use __write
, which is an alias:
__write_address=`nm -D /lib/x86_64-linux-gnu/libc.so.6 | awk '/__write/ {print $1}'`
nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep $__write_address
Unfortunately, simply overriding __write
doesn't work either:
cat interpose_underbar_write.c
LD_PRELOAD=$PWD/interpose_underbar_write.so ./call_fwrite
Note that in the call
instructions above, the symbols don't have the @plt
suffix. The real issue is that none of these calls are via the PLT. When glibc itself is built and linked, the linker can see the call sites and intended destinations at the same time, so it can and does link them at that time. The dynamic linker isn't involved, so LD_PRELOAD
has no effect on these call sites.
Looking at the dynamic linker's debug output again, we can see it never looks up write
or __write
at all; only fwrite
:
LD_DEBUG=bindings LD_PRELOAD=$PWD/interpose_underbar_write.so ./call_fwrite 2>&1 | grep 'symbol.*write'
Can we interpose some other function?¶
Using strace
, we can get the call stack when the write
system call is made. Maybe we can interpose one of these other functions?
strace -k -e write ./call_fwrite 2>&1
Maybe one of these ends up getting called via the PLT. Let's see what function are called via the PLT inside the glibc library:
objdump -D /lib/x86_64-linux-gnu/libc.so.6 | grep -o '<.*@plt>' | sort | uniq
As it turns out, in a normal glibc build, only the memory allocation functions and some opaque implementation details are called via the PLT. Presumably the memory allocation functions are called via the PLT because they're explicitly meant to be overridable. I'm not sure what the other things are. Notably absent are any of the symbols in the __write
call stack, above.
So what can we do?¶
There are a few way around this.
- Interpose every entry point into glibc that could end up calling the functionality we want to modify. This could work, but developing and maintaining 100% coverage is difficult and fragile. We'd also end up having to reimplement parts of libc. e.g. if we write a wrapper for
fwrite
, and want to interpose on the individualwrite
system calls, we'd have to reimplement the in-memory buffering thatfwrite
puts in between the two ourselves (or forgo it). - Patch glibc to make calls via the PLT, so that we can interpose them via
LD_PRELOAD
. We could then inject our custom glibc as anotherLD_PRELOAD
library as done here. This seems like a promising shortcut, but could result in subtle issues if our patched glibc isn't binary-compatible with the headers that the target code was compiled against; e.g. uses different data type or constant definitions. We should be able to prevent those difficulties by being very careful to build our libc consistently with our distribution's libc, but we'd need to do so for every platform we intend to run on. - Use a different interposition mechanism than
LD_PRELOAD
. In particular,ptrace
tells the OS kernel to intercept the system calls themselves, ensuring that we see every system call regardless of what function it's coming from, whether the program is statically linked, or even if thesyscall
instructions themselves are inlined. This works best when the behavior we're trying to modify is at the syscall level. It's a bit more work to implement, though. It may also have different performance characteristics, especially if there are other syscalls we're not interested in intercepting. We also can't attach a debugger to a program that is already beingptrace
'd, making debugging more difficult.