linux - [Solved-6 Solutions] What is the LD_PRELOAD trick ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

What is the LD_PRELOAD trick ?

Linux - Solution 1:

  • LD_PRELOAD is an environment variable.
  • check for the LD_PRELOAD environment variable, and complain (though the attacker could also LD_PRELOAD the function that lets you read environment variables.

Linux - Solution 2:

  • If you set LD_PRELOAD to the path of a shared object, that file will be loaded before any other library (including the C runtime, libc.so).
  • To run ls with your special malloc() implementation, do this:
$ LD_PRELOAD=/path/to/my/malloc.so /bin/ls
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

  • You can override symbols in the stock libraries by creating a library with the same symbols and specifying the library in LD_PRELOAD.

Linux - Solution 4:

LD_PRELOAD you can give libraries priority.

For instance you can write a library which implement malloc and free. Also, by Loading these with LD_PRELOAD your malloc and free will be executed rather than the standard ones.

Linux - Solution 5:

Using LD_PRELOAD to preload library. You can check if the setting is available by ldd command.

Example: suppose you need to preload your own libselinux.so.1.

> ldd /bin/ls
    ...
    libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f3927b1d000)
    libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f3927914000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f392754f000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f3927311000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f392710c000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f3927d65000)
    libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f3926f07000)
click below button to copy the code. By - Linux tutorial - team

Set your preload environment:

export LD_PRELOAD=/home/patric/libselinux.so.1
click below button to copy the code. By - Linux tutorial - team

Again Check your library:

>ldd /bin/ls
    ...
    libselinux.so.1 =>
    /home/patric/libselinux.so.1 (0x00007fb9245d8000)
    ...
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 6:

Export mylib.so to env:

$ export LD_PRELOAD=/path/mylib.so
$ ./mybin
click below button to copy the code. By - Linux tutorial - team

To disable :

$ export LD_PRELOAD=
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - What is the LD_PRELOAD trick ?