aaa
aaa

Reputation: 433

Can we set LD_PRELOAD environ var for a running program?

I want to write a program(like gdb) which will set LD_PRELOAD for a program which is already running . I haven't got concrete answer for this. Is it possible? I am ok if the program needs to be privileged for setting the env var.

Upvotes: 0

Views: 1097

Answers (2)

anshkun
anshkun

Reputation: 115

Another method but of no use

There will be one root process P1 which will poke running process P2 1)Using ptrace library P1 can modify other process memory P2 and to get the address of environment variable you need to see the objdump/nm of running process (P2)using argv[]/argc address.

2) After getting the address as well as value of argc/argv[], you can calculate env start and traverse each environment variable until you get LD_PRELOAD, P1 will peek data of P2

3) Change the contents of P2 process memory using PTRACE (poke data) system call by P1

4) Check objdump of P2 binary and linked dynamic linker library to get the address of dl_start of equivalent function which initializes the dynamic linker environment.

5) check pmap of P2 to get the actual load address of dl_start

6) P1 will set the IC of P2 to _dl_start address using ptrace system call, It is required to identify what other registers are expecting before calling dl_start()

7) The process P2 is restarted from function _dl_start()

Process need to be reinitialized in any case to update symbol address to other libraries accordingly

Upvotes: 0

Michał Kosmulski
Michał Kosmulski

Reputation: 10020

No, it's not possible.

First off, there is no API to modify the environment for a running program.

Second, even if you could modify the environment variable for a running program instance by using some low-level hacks (it's supposedly possible to do it using a debugger), it would not work the way you expect. LD_PRELOAD is used only by the Linux linker when it starts a program, so modifying the value of this variable once the program was started would not modify its behavior.

Upvotes: 0

Related Questions