LD_PRELOAD 未加载我的库,也没有错误或警告

问题描述 投票:0回答:1

我想在 Oracle Linux 7.9 中运行目标之前使用

LD_PRELOAD
挂钩我的库。
LD_PRELOAD
加载我的库在第一个场景中失败,但在第二个场景和第三个场景中成功。 我不知道如何让
LD_PRELOAD
在第一个场景中工作。

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

孩子.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

测试.cpp

#include <stdio.h>

void myhello(void) {
    printf("hello world!");
}
g++ -fPIC -shared test.cpp -o libtest.so -ldl

我在2和3中尝试成功,但在1中失败了。

linux ld-preload
1个回答
0
投票

我不知道让 LD_PRELOAD 在第一种情况下工作

您不能:由于显而易见的原因,

LD_PRELOAD
setuid
程序会被忽略。
请参阅 

man ld.so

中的“安全执行模式”。

© www.soinside.com 2019 - 2024. All rights reserved.