Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LD_PRELOAD causing Segmentation fault for linux commands

Tags:

c++

c

linux

I have a .so file which is used for finding memory leaks. I set the LD_PRELOAD env variable in the session in which I want to execute my target binary. export LD_PRELOAD=./memleakfinder.so But once this environment variable is set, even a simple linux command like ls or ps is causing a Segmentation fault (core dumped). Why is that happening?I've overloaded versions of malloc(),calloc(),realloc(),free(),new,new[] etc. in that .so file. The automatic bug reporting tool of CentOS6 is saying: source:coreutils Problem:process /bin/ls was killed by signal 11(SIGSEGV). Clearly the library injection is causing the problem here. How can I avoid it?

like image 391
Rajarshi Bhowmik Avatar asked Sep 13 '25 23:09

Rajarshi Bhowmik


1 Answers

I had this same problem when trying to override the malloc function.

For me, I was using printf() in my function declaration of malloc() (to see if it was working).

When I switched to write() instead or printf(), it worked. This is because some library functions, such as printf, may allocate memory for their operation. Consequently, if you use any of those functions in your library you risk getting into an infinite recursion.

like image 167
Juan Pablo Avatar answered Sep 15 '25 14:09

Juan Pablo