Questions tagged [ld-preload]
LD_PRELOAD is a list of additional ELF shared objects that should be loaded first.
ld-preload
280
questions
51
votes
6
answers
42k
views
Overriding 'malloc' using the LD_PRELOAD mechanism
I'm trying to write a simple shared library that would log malloc calls to stderr (a sort of 'mtrace' if you will).
However, this is not working.
Here's what I do:
/* mtrace.c */
#include <dlfcn....
27
votes
3
answers
5k
views
LD_PRELOAD does not work as expected
Consider the following library which can be preloaded before any program execution:
// g++ -std=c++11 -shared -fPIC preload.cpp -o preload.so
// LD_PRELOAD=./preload.so <command>
#include <...
20
votes
1
answer
17k
views
What is the exact equivalent to LD_PRELOAD on OSX?
I am using LD_PRELOAD to hook a library function, and in Linux it works perfectly. But I cannot figure out how to do the equivalent in OSX.
The setup I have on Linux is as follows:
The code is:
#...
19
votes
2
answers
13k
views
link a static library to a shared library and hide exported symbols
I am having an annoying problem with the linker. I want to link some symbols from a shared library to a static library, but not export its symbols (ie, I cannot simply merge the libraries or link with ...
14
votes
1
answer
3k
views
Binding failure with objcopy --redefine-syms
Preconditions
A third-party has provided a C++ executable fooapp that uses a shared object libfoo.so. The library also comes with a header foo.hpp so developers can build other applications:
/* foo....
11
votes
5
answers
24k
views
LD_PRELOAD with setuid binary
I am trying to use LD_PRELOAD to preload a library with an application that has setuid permissions. Tried LD_PRELOAD at first, and it seemed like it was being ignored with the setuid binary, though it ...
9
votes
2
answers
4k
views
hiding functions in C
I have application that has a function f1 void f1 ()
In addition, I have a library that I load using LD_PRELOAD.
The library has several code files and several header file, and it compiled to .so ...
9
votes
2
answers
2k
views
Dynamically modify symbol table at runtime (in C)
Is it possible to dynamically modify symbol table at runtime in C (in elf format on Linux)?
My eventual goal is the following:
Inside certain function say foo, I want to override malloc function to ...
9
votes
1
answer
6k
views
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(){
...
8
votes
3
answers
6k
views
LD_PRELOAD doesn't affect dlopen() with RTLD_NOW
If I use a function from a shared library directly, i.e. by declaring it in my code and linking during compile time, LD_PRELOAD works fine. But if I use dlopen()/dlsym() instead LD_PRELOAD has no ...
8
votes
2
answers
6k
views
How can I intercept dlsym calls using LD_PRELOAD?
I want to intercept application's calls to dlsym. I have tried declaring inside the .so that I am preloading dlsym , and using dlsym itself to get it's real address, but that for quite obvious reasons ...
7
votes
1
answer
10k
views
ERROR: ld.so: object 'libproxychains.so.3' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored
When I type the command
proxychains chromium on linux terminal
it gives me this error:
ERROR: ld.so: object 'libproxychains.so.3' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ...
7
votes
5
answers
4k
views
Problems with LD_PRELOAD and calloc() interposition for certain executables
Relating to a previous question of mine
I've successfully interposed malloc, but calloc seems to be more problematic.
That is with certain hosts, calloc gets stuck in an infinite loop with a ...
7
votes
1
answer
3k
views
LD_PRELOAD only working for malloc, not free
I'm trying to interpose malloc/free/calloc/realloc etc with some interposers via LD_PRELOAD. In my small test, only malloc seems to be interposed, even though free is detected (see output).
I'd ...
7
votes
2
answers
3k
views
How to make static linked ELF file to load LD_PRELOAD .so
I have static linked binary (ELF file) it doesn't have dynamic segment, .dymsym sections and it doesn't perform LD_PRELOAD command and etc.
How could i create fake dummy dynamic segment to activate ...
7
votes
1
answer
1k
views
why is library loaded via LD_PRELOAD operating before initialization?
In the following minimal example, a library loaded via LD_PRELOAD with functions to intercept fopen and openat is apparently operating before its initialization. (Linux is CentOS 7.3). Why??
library ...
7
votes
1
answer
6k
views
How to check if a linux shared library has been preloaded using LD_PRELOAD
I'm familiar with using dlopen() to check if a shared library has been loaded into a process using a prior call to dlopen() without triggering a load if it isn't present, like so:
void* lib = dlopen(...
7
votes
1
answer
1k
views
Presenting a virtual filesystem to a Linux process without root access
I'm looking for a way to present a userspace filesystem to a specific Linux process but I don't have root access.
The obvious answer is FUSE but without root access I cannot load the kernel module ...
6
votes
2
answers
9k
views
Using LD_PRELOAD mixed 64bit/32bit environment in Linux
I would like to set LD_PRELOAD to point to a shared library where I might run either a 64bit or 32bit application. It is obvious that the shared library and the executable have to match in bit-ness. ...
6
votes
3
answers
3k
views
why doesn't LD_PRELOAD trick catch open() when called by fopen()?
I use the LD_PRELOAD trick to catch open64() calls and I think I know how to do it correctly: with the program foobar compiled from
#include <sys/types.h>
#include <sys/stat.h>
#include &...
6
votes
2
answers
3k
views
How to wrap ioctl(int d, unsigned long request, ...) using LD_PRELOAD?
Here's the template I use for wrapping a function using LD_PRELOAD:
int gettimeofday(struct timeval *tv, struct timezone *tz) {
static int (*gettimeofday_real)(struct timeval *tv, struct timezone *...
6
votes
1
answer
700
views
How to use perf with ld_preload?
I tried to use perf stat with LD_PRELOAD as prefix for executable such as:
perf stat LD_PRELOAD=$PWD/../user/preload.so ./write 1
It seems not work for perf, are there any way to achieve it?
6
votes
5
answers
8k
views
How to override assert macro in C?
I want to create my own version of assert in which it does some log prints in case assert was called in NDEBUG mode.
I tried to do the LD_PRELOAD trick and redefine the assert macro but it seems to ...
6
votes
1
answer
2k
views
What are the differences between LD_PRELOAD and strace?
Both methods are used to gather system calls also parameters and return values of them. When we prefer LD_PRELOAD and why? Maybe we can say that we can only gather syscalls via strace but we can ...
6
votes
3
answers
979
views
How to wrap variadic functions using LD_PRELOAD?
I have to perform dynamic linking on a variadic function of following format:
int foo(char *args, const char *f, ...)
Here the number of arguments are variable.
What I want to achieve is that I ...
6
votes
2
answers
369
views
Replacing the close() function in Linux with my own close() function
I'm trying to provide my own implementation of the close() function in Linux. Why? Because I just found out you could do that and it sounds fun.
Here's myclose.c:
#include <stdio.h>
int close(...
6
votes
1
answer
3k
views
intercepting the openat() system call for GNU tar
I'm trying to intercept the openat() system call on Linux using a custom shared library that I can load via LD_PRELOAD. An example intercept-openat.c has this content:
#define _GNU_SOURCE
#include &...
6
votes
1
answer
4k
views
LD_PRELOAD causing segmentation fault in dynamic library loader
I have written a library which is intended to be loaded via LD_PRELOAD. On some Linux systems, this is causing the dynamic library loader to segfault during initialisation.
I have a simple test case ...
5
votes
1
answer
2k
views
Why doesn't LD_PRELOAD take effect with scripts having no shebang?
If, when I run a script, I use LD_PRELOAD to designate a library to preload, I find that the library is in fact preloaded only if the script has a shebang line. For example, given this script:
# Not ...
5
votes
1
answer
6k
views
Setting the LD_PRELOAD environment variable for commands run without typing the full path
I'm playing around with LD_PRELOAD and have produced a library that simply wraps puts() in a function that converts the string to be printed to uppercase before printing. I then export the LD_PRELOAD ...
5
votes
2
answers
4k
views
How to debug functions in a dynamic library loaded with LD_PRELOAD with gdb?
I'm trying to debug some functions in a dynamic shared library libexecHook.so. This library is preloaded setting LD_PRELOAD in order to intercept and rewrite some calls to execve() and friends. For ...
5
votes
3
answers
4k
views
How to set LD_PRELOAD in systemd
I want to hook some functions in libssl with LD_PRELOAD in systemd.
In systemd file I put
ExecStart=/etc/myscript.sh
and in /etc/myscript.sh I put
#!/bin/sh
LD_PRELOAD=/lib/inject_libssl.so /bin/run
...
5
votes
3
answers
2k
views
LD_PRELOADed libraries and child process
everyone!
Image that i have a program(usemalloc) like this:
#include <stdio.h>
#include <stdlib.h>
#define USER_BYTES_SIZE 100
int main(void){
char* userbytes = (char*)malloc(...
4
votes
4
answers
5k
views
LD_PRELOAD can not intercept syscalls, but only libcalls?
My code works well with malloc, but not with mmap. The code is below:
main.c
#include <stdio.h>
#include <stdlib.h>
int main(){
int * p = (int*) malloc(sizeof(int));
printf("in main(...
4
votes
2
answers
2k
views
Why is LD_PRELOAD usage discouraged?
I came across this piece of advice on the Google's tcmalloc documentation page.
You can use TCMalloc in applications you didn't compile yourself, by using LD_PRELOAD:
$ LD_PRELOAD="/usr/lib/...
4
votes
1
answer
2k
views
Why does LD_PRELOAD work with syscalls?
The idea of LD_PRELOAD is to load a shared library before the original shared library, for example I can compile mylib.so to load it before libc.so, so when process wants to use printf it searches in ...
4
votes
2
answers
752
views
Is it possible for an LD_PRELOAD to only affect the main executable?
The Actual Problem
I have an executable that by default uses EGL and SDL 1.2 to handle graphics and user input respectively. Using LD_PRELOAD, I have replaced both with GLFW.
This works normally ...
4
votes
2
answers
70
views
Will a C compiled .so work with a C++ application?
If I want to dynamically link a shared library (.so) for a C++ application (which was built with g++) using LD_PRELOAD, does it matter if the .so is generated from a C source file (using gcc) or a C++ ...
4
votes
3
answers
889
views
Conflicting types compiling a LD_PRELOAD wrapper
I tried to use LD_PRELOAD to hook sprintf function , so I will print to file the result of buffer:
#define _GNU_SOURCE
#include <stdio.h>
#include<dlfcn.h>
int sprintf (char * src , const ...
4
votes
1
answer
722
views
How can I inject a background thread to an application with LD_PRELOAD?
I know that LD_PRELOAD can be used to intercept calls to functions in shared libraries (if the app is not statically linked). However, I do not know how it can be used to add additional features or ...
4
votes
1
answer
5k
views
Can LD_PRELOAD be used to load different versions of glibc?
Cast of characters
big-old-app is linked to an old version of glibc, say glibc-2.12. I cannot do anything to change this.
cute-new-addon.o is linked to a newer version, glibc-2.23. This glibc-2.23 ...
4
votes
1
answer
820
views
How can I override C functions (like with LD_PRELOAD) at runtime?
I have some Python code that uses a library that implements virtual file systems. For the drivers for those virtual file systems to work a bunch of C functions (like readdir(), opendir(), fseek()) ...
4
votes
2
answers
1k
views
How to defeat framework injections?
Is anyone hardening their code in an attempt to detect injections? For example, if someone is trying to intercept a username/password via NSUrlConnection, they could use LD_PRELOAD/DYLD_LIBRARY_PATH, ...
4
votes
1
answer
4k
views
LD_PRELOAD in docker
When I run docker as following:
docker run -it -e LD_PRELOAD=/bin/xyz.so bash env
It runs as expected, and the output is:
HOSTNAME=2116ac3bae11
_BASH_VERSION=4.4
_BASH_LATEST_PATCH=23
PWD=/
HOME=/...
3
votes
2
answers
3k
views
How to escape spaces in library path appended to LD_PRELOAD?
I'm having a problem with LD_PRELOAD on Linux. I'm trying to load a library existing in a directory with spaces in its name, right before launching my application:
> export LD_PRELOAD='/home/...
3
votes
2
answers
2k
views
LD_PRELOAD and clone()
I'm using a script to run a program with LD_PRELOAD with a library created by me to intercept some calls, it works well but at some point the process calls clone() and I lose the ability to intercept ...
3
votes
1
answer
2k
views
Passing arguments to a library loaded with LD_PRELOAD
Is it possible to pass arguments to my library, which is loaded with LD_PRELOAD:
LD_PRELOAD=lib.so ./program
How can I pass arguments to this library?
3
votes
2
answers
7k
views
Android 4.2 - LD_PRELOAD supported or not?
I wonder if LD_PRELOAD is now supported with the newer Android-versions?
At the time of 4.0 ICS it wasn't, and in the documentation (NDK docs/SYSTEM-ISSUES.html) there's still:
No support for ...
3
votes
1
answer
49
views
How do I debug a constructor function in an LD_PRELOAD library?
When I'm writing a library meant to be used with LD_PRELOAD, how can I debug its __attribute__((__constructor__)) functions? They always seem to run before GDB can stop the process. As an MCVE, run ...
3
votes
1
answer
1k
views
Linux: LD_PRELOAD + -z,initfirst
I'm writing a shared-object which is supposed to be LD_PRELOADed into processes.
In that shared object I have some initialization like
__attribute__((constructor)) void initFunc();
That I'd like to ...