Reputation: 1
I want to use LD_PRELOAD
to hook my library before target running in Oracle Linux 7.9.
LD_PRELOAD
load my library failed in first scenario, but success in second scenario and third scenario.
I have no idea to make LD_PRELOAD
work in first scenario.
1
[test@k8s-node2 hook]$ ls -l /usr/lib64/libtest.so
-rwsr-sr-x 1 test test2 501328 Jan 31 07:04 /usr/lib64/libtest.so
`[test@k8s-node2 hook]$ ls -l child`
-rwsr-sr-x 1 test test2 12848 Jan 31 16:08 child
[test@k8s-node2 hook]$ LD_PRELOAD=/usr/lib64/libtest.so ./child &
[test@k8s-node2 hook]$ pmap -X `pidof child` | grep libtest
[test@k8s-node2 hook]$
2
[test@k8s-node2 hook]$ ls -l /usr/lib64/libtest.so
-rwsr-sr-x 1 test test 501328 Jan 31 07:04 /usr/lib64/libtest.so
`[test@k8s-node2 hook]$ ls -l child`
-rwsr-sr-x 1 test test 12848 Jan 31 16:08 child
[test@k8s-node2 hook]$ LD_PRELOAD=/usr/lib64/libtest.so ./child &
[test@k8s-node2 hook]$ pmap -X `pidof child` | grep libtest
7fded2929000 r-xp 00000000 fd:02 40593442 4 4 2 4 0 0 0 libtest.so
7fded292a000 ---p 00001000 fd:02 40593442 2044 0 0 0 0 0 0 libtest.so
7fded2b29000 r--p 00000000 fd:02 40593442 4 4 4 4 4 0 0 libtest.so
7fded2b2a000 rw-p 00001000 fd:02 40593442 4 4 4 4 4 0 0 libtest.so
3
bash-4.2# chmod u-s,g-s child; chmod u-s,g-s /usr/lib64/libtest.so
bash-4.2# ls -l child
-rwxr-xr-x 1 test test2 8544 Feb 1 05:51 child
bash-4.2# ls -l /usr/lib64/libtest.so
-rwxr-xr-x 1 test test2 8048 Feb 1 05:55 /usr/lib64/libtest.so
bash-4.2# su - test
[test@k8s-node2 hook]$ LD_PRELOAD=/usr/lib64/libtest.so ./child &
[test@k8s-node2 hook]$ pmap -X `pidof child` | grep libtest
7f6df60c0000 r-xp 00000000 fd:02 40593442 4 4 2 4 0 0 0 libtest.so
7f6df60c1000 ---p 00001000 fd:02 40593442 2044 0 0 0 0 0 0 libtest.so
7f6df62c0000 r--p 00000000 fd:02 40593442 4 4 4 4 4 0 0 libtest.so
7f6df62c1000 rw-p 00001000 fd:02 40593442 4 4 4 4 4 0 0 libtest.so
child.cpp
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[], char* envp[]) {
for(int i=0;i<argc;i++) {
printf("child argv%d:%s\n",i, argv[i]);
}
int i = 0;
while(envp[i] != NULL) {
printf("child envp%d: %s\n", i, envp[i]);
i++;
}
while(1) {
sleep(1);
}
return 0;
}
g++ child.cpp -o child
test.cpp
#include <stdio.h>
void myhello(void) {
printf("hello world!");
}
g++ -fPIC -shared test.cpp -o libtest.so -ldl
I try success in 2 and 3, but failed in 1.
Upvotes: 0
Views: 66
Reputation: 213879
I have no idea to make LD_PRELOAD work in first scenario
You can't: LD_PRELOAD
is ignored for setuid
and setgid
programs for obvious reasons.
See "secure execution mode" in man ld.so.
Upvotes: 0