0

I have the following setup that I try to debug. I could not find anything using the search, so I hope that someone here is able to guide me how I can do the following:

I have a binary that is executed (./binary) and also a local version of glibc that was manipulated on an assembly level (./libc_patched). This patched library now contains a dynamic call to an own written shared library (./shared.so).

So, what I do now is something similar to:

$ LD_LIBRARY_PATH="./path/to/libc_patched:/path/to/shared" LD_PRELOAD="/path/to/libasan.so path/to/libc_patched" ./binary

Now, my problem is that libasan throws an error that something went wrong with malloc (either a free on a not malloc()-ed address, or that the malloc-free pairs do not match.

The binaries are compiled with the flags "-g", "-fsanitize=address" and libasan is linked to the binary. My glibC version is 2.28 and I'm working on a fresh install of Debian10.

What I try to do now: I want to debug where the error occurs using gdb. But I cannot find a way to start gdb using the global libc (unpatched) but executing the "to-be-debugged binary" with the patched libc. So basically the follwing:

$ gdb "LD_LIBRARY_PATH="./path/to/libc_patched:/path/to/shared" LD_PRELOAD="/path/to/libasan.so path/to/libc_patched" ./binary"

Is something like this possible? As I have an error in my patched libc I cannot reliably start gdb with it. Thanks for any hints!

MajorasKid
  • 733
  • 2
  • 5
  • 24

1 Answers1

1

This answer explains why you can't use LD_LIBRARY_PATH to select a different GLIBC.

The easiest fix is to make the program use non-default GLIBC without environment manipulation. Assuming patched GLIBC is installed in /path/to/libc_patched, build your program like so:

gcc -Wl,--dynamic-linker=/path/to/libc_patched/lib/ld-linux-x86-64.so.2 \
  -Wl,-rpath=/path/to/libc_patched/lib main.o ...

If you can't easily rebuild the binary, you could also use

patchelf --set-interpreter /path/to/libc_patched/lib/ld-linux-x86-64.so.2 \
  --set-rpath /path/to/libc_patched/lib ./a.out

Then debug it like so:

gdb -ex 'set env LD_PRELOAD /path/to/libasan.so' a.out
Employed Russian
  • 199,314
  • 34
  • 295
  • 362