Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LD_PRELOAD does not loaded on systemd

I am trying to inject a SO into a process that starts using systemd init system (using LD_PRELOAD), but it does not loaded into the new process.

I complied a basic SO (unrandom.c):

int rand(){
    return 42; //the most random number in the universe
}

with the command line:

gcc -shared -fPIC unrandom.c -o unrandom.so

I changed the .service file to include:

Environment="LD_PRELOAD=/tmp/unrandom.so"

After starting the service the LD_PRELOAD environment variable is exist in the process, but the SO does not injected

cat /proc/<PID>/maps

Am I missing something?

My machine is RHEL7

like image 485
Or Smolnik Avatar asked Mar 16 '16 11:03

Or Smolnik


1 Answers

Setuid processes restrict usage of LD_PRELOAD (and some other env. variables) due to security reasons.

Loaded library must be specified via name only and be located in one of the directories listed in /etc/ld.so.conf (see e.g. this link). For example on Debian-based systems

sudo cp library.so /usr/lib/x86_64-linux-gnu
LD_PRELOAD=library.so daemon

Another approach is to put full path to library to /etc/ld.so.preload:

sudo echo path/to/library.so >> /etc/ld.so.preload

but then it'll be preloaded to all new processes (which has a high chance of breaking your system if you are not extremely careful).

like image 151
yugr Avatar answered Oct 30 '22 03:10

yugr