The LD_PRELOAD environment variable in Unix-like systems that allows users to specify a shared library to be loaded before others when a program is executed. This can be used to override functions in existing libraries or inject custom code into applications. The dynamic linker is responsible for loading the program image and shared libraries, and then starts the execution. On Linux, the dynamic linker is ld-linux.so, while on FreeBSD, it's ld-elf.so. LD_PRELOAD adds a list of shared libraries to be loaded by the dynamic linker, allowing function overriding. Examples of using LD_PRELOAD include transparent decompression of files and overriding malloc and read/write functions. LD_PRELOAD can be used for testing, hot-fixes, observability, and alternative implementations. However, it has security implications, such as overriding system calls, and Linux and FreeBSD have mitigations in place to prevent misuse. LD_PRELOAD can be used in alternative ways, such as the --preload command-line option and the /etc/ld.so.preload file. Finally, eBPF is an alternative to LD_PRELOAD, but it's more restricted and doesn't introduce the same security issues.
dev.to
dev.to
Create attached notes ...