Code Entry - Solaris & Linux

I have an executable module created by a third party. I would like to β€œenter” my code (a kind of watchdog running in a separate thread) into this process.

There are still two possible ways: one - to run my code as an executable file and dynamically load it on top of it (it seems very difficult and difficult) or make my code a shared object, load it through LD_PRELOAD and initialize from some static variable constructor.

Are there any more convenient ways to do this? My OS is Linux x86 and Solaris-SPARC.

Update. If possible, I would not want to fix this process, but load my code dynamically.

+4
source share
4 answers

Looks like you're looking for InjectSo . There's a Powerpoint presentation that explains how this works. I have not tried to verify this yet.

+4
source

Hotpatch should do it for you. It is more effective than injection.

+2
source

Rob Kennedy told you about InjectSo - maybe what you need.

Beware that introducing a thread into a non-threaded process will cause synchronization problems. The problems are less serious if the application is already loaded, but even then the application may object to a thread that it is not aware of.

0
source

I did not use the mentioned InjectSo, but this is noteworthy information. If you are looking for alternatives, this is an easy way to enter the code:

#include <stdio.h> #include <sys/types.h> #include <pwd.h> int main() { struct passwd* pswd = getpwuid(1000); if(pswd) printf("%s\n", pswd->pw_name); return 0; } 

gcc test.c -o test

 #define _GNU_SOURCE #include <dlfcn.h> #include <sys/types.h> #include <pwd.h> #include <stdlib.h> #include <stdio.h> static char* hocus = "hocus pocus"; struct passwd *getpwuid(uid_t uid) { static struct passwd *(*orig_getpwuid)(uid_t uid); if(!orig_getpwuid) { orig_getpwuid = (struct passwd* (*)(uid_t))dlsym(RTLD_NEXT, "getpwuid"); } struct passwd* original_passwd = (*orig_getpwuid)(uid); if(original_passwd) { original_passwd->pw_name = hocus; } // your code here return original_passwd; } 

gcc inject.c -shared -o libinject.so

do LD_LIBRARY_PATH=. LD_PRELOAD=libinject.so ./test LD_LIBRARY_PATH=. LD_PRELOAD=libinject.so ./test

Must say hocus pocus . You can override arbitrary libc functions like printf , snprintf - just find what this module uses.

In "your code here" you can run arbitrary threads, watchdogs, etc.

0
source

All Articles