我目前正在使用LD_PRELOAD
技巧并使用链接器版本脚本as detailed in an article on another website。我的MCVE代码包含在下面。
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#define BUFFER_SIZE (1024)
int __printf__(const char *fmt, ...)
{
char buf[BUFFER_SIZE] = { 0 };
int ret;
int len;
va_list args;
va_start(args, fmt);
vsnprintf(buf, BUFFER_SIZE - 1, fmt, args);
#if 1
//typeof(vsnprintf) *real_func = dlsym(RTLD_NEXT, "vsnprintf");
//(*real_func)(buf, BUFFER_SIZE - 1, fmt, args);
#endif
len = strlen(buf);
ret = write(STDOUT_FILENO, buf, len);
va_end(args);
return ret;
}
asm(".symver __printf__, __printf_chk@GLIBC_2.3.4");
如果我修改我的自定义printf
函数来简单地写一个静态字符串,没问题。但是,我想通过printf
修改发送到控制台的数据(添加前缀,后缀,并将某些字符设置为UPPERCASE等)。似乎每当我尝试使用任何其他printf
- 族函数来生成用户提供的字符串的副本时,我会得到一个段错误,如下所示。
Program received signal SIGSEGV, Segmentation fault.
strchrnul () at ../sysdeps/x86_64/strchr.S:32
32 ../sysdeps/x86_64/strchr.S: No such file or directory.
(gdb) bt
#0 strchrnul () at ../sysdeps/x86_64/strchr.S:32
#1 0x00007ffff78591c8 in __find_specmb (format=0x1 <error: Cannot access memory at address 0x1>) at printf-parse.h:108
#2 _IO_vfprintf_internal (s=s@entry=0x7fffffffc380, format=format@entry=0x1 <error: Cannot access memory at address 0x1>, ap=ap@entry=0x7fffffffc4f8) at vfprintf.c:1312
#3 0x00007ffff7882989 in _IO_vsnprintf (string=0x7fffffffc510 "", maxlen=<optimized out>, format=0x1 <error: Cannot access memory at address 0x1>, args=0x7fffffffc4f8)
at vsnprintf.c:114
#4 0x00007ffff7bd58a1 in __printf__ (fmt=0x1 <error: Cannot access memory at address 0x1>) at libfakeprintf.c:19
#5 0x00000000004004aa in printf (__fmt=0x400644 "%s received %d args\n") at /usr/include/x86_64-linux-gnu/bits/stdio2.h:104
#6 main (argc=<optimized out>, argv=<optimized out>) at print_args.c:5
(gdb) quit
导致此次崩溃的原因是什么?
谢谢。
答案 0 :(得分:2)
你已经覆盖了glibc内部函数__printf_chk
,但是这个函数没有匹配printf的原型。它的原型是:
int __printf_chk(int flag, const char * format, ...);
因此,请确保您自己的__printf__
函数也具有该原型。
有__printf_chk
here