Tôi đang chơi với LD_PRELOAD để chặn cuộc gọi libc, có vẻ như cuộc gọi ghi không bị chặn với wc, mặc dù nó dường như hoạt động với con mèo. Phiên bản rút gọn của sự cố xuất hiện bên dưới.Tại sao LD_PRELOAD dường như không hoạt động để viết với wc
RedHat Linux 2.6.9-42.ELsmp
Makefile
writelib:
gcc -Wall -rdynamic -fPIC -c write.c
gcc -shared -Wl,-soname,libwrite.so -Wl,-export-dynamic -o libwrite.so write.o -ldl
write.c:
#include <stdio.h>
#include <string.h>
#ifndef __USE_GNU
#define __USE_GNU
#define __USE_GNU_DEFINED
#endif
#include <dlfcn.h>
#ifdef __USE_GNU_DEFINED
#undef __USE_GNU
#undef __USE_GNU_DEFINED
#endif
#include <unistd.h>
#include <stdlib.h>
static ssize_t (*libc_write)(int fd, const void *buf, size_t len);
ssize_t
write(int fd, const void *buf, size_t len)
{
static int already;
ssize_t ret;
if (!already) {
if ((libc_write = dlsym(RTLD_NEXT, "write")) == NULL) {
exit(1);
}
already = 1;
}
ret = (*libc_write)(fd,"LD_PRELOAD\n",11);
return len; // not ret so cat doesn't take forever
}
Output:
prompt: make
gcc -Wall -rdynamic -fPIC -c write.c
gcc -shared -Wl,-soname,libwrite.so -Wl,-export-dynamic -o libwrite.so write.o -ldl
prompt: LD_PRELOAD=./libwrite.so /bin/cat write.c
LD_PRELOAD
prompt: LD_PRELOAD=./libwrite.so /usr/bin/wc write.c
32 70 572 write.c
Bất kỳ lời giải thích?
cũng xem: http://stackoverflow.com/questions/6538501/linking-two-shared-libraries-with-some-of-the-same-symbols/6540059#6540059 để biết biểu tượng có thể bị ràng buộc như thế nào bản sao nội bộ trong thư viện. – ninjalj
Cảm ơn con trỏ tới ltrace. Tôi đã nhầm lẫn giữa cuộc gọi viết libc với cuộc gọi viết hệ thống đã xuất hiện khi tôi chạy strace trên lệnh wc. –