Between a rock and a crazy place

Inject Environment variables into running processes

2013-10-11

tl;dr: Using gdb to manipulate a running process is fun and just the right amount of danger to be exiting

Just to document this (a friend asked me): If you ever wanted for example to globally change your $PATH, or add a global $LD_PRELOAD (for example to use insulterr ;) ), without restarting your session, gdb is your friend.

You can call arbitrary functions in the context of any process (that you are priviledged to attach a debugger, it has to run under your uid or you have to be root, see ptrace(2) for specifics), as long as they are linked. Almost everything is linked to libld, so with enough effort this actually means every function.

For example, suppose you are running i3wm and want to add /home/user/insulterr/insulterr.so to your $LD_PRELOAD in every process started by i3:

$ gdb -p `pidof i3` `which i3`
<lots of output of gdb>
gdb $ call setenv("LD_PRELOAD", "/home/user/insulterr/insulterr.so")
gdb $ quit
A debugging session is active.

    Inferior 1 [process 2] will be detached.

Quit anyway? (y or n) y
Detaching from program: /usr/bin/i3, process 2

This is of course a terrible hack, by high standards. Things to look out for are (off the top of my head):

Have fun!