[英]can I do nested calls for system functions loaded by LD_PRELOAD
Hi I am trying to override sscanf()
which gets called from localtime()
, But its not calling sscanf()
of my library loaded rather its calling sscanf()
of glibc.嗨,我正在尝试覆盖从
localtime()
sscanf()
sscanf()
,而是调用 glibc 的sscanf()
。
Is there something I am missing?有什么我想念的吗?
Files content mentioned below:文件内容如下:
//preload_localtime.c
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <dlfcn.h>
__attribute__((force_align_arg_pointer)) int sscanf(const char *str, const char *format, ...)
{
int ret;
va_list ap;
printf("test\n");
va_start(ap, format);
ret = vsscanf(str, format, ap);
va_end(ap);
return ret;
}
__attribute__((force_align_arg_pointer)) int __isoc99_sscanf(const char *str, const char *format, ...)
{
int ret;
va_list ap;
printf("test\n");
va_start(ap, format);
ret = vsscanf(str, format, ap);
va_end(ap);
return ret;
}
__attribute__((force_align_arg_pointer)) struct tm *
localtime(const time_t *timep)
{
static struct tm *(*real_localtime)(const time_t *) = (void*)0;
printf("my_local\n");
if (!real_localtime)
real_localtime = dlsym(RTLD_NEXT, "localtime");
return real_localtime(timep);
}
//foo.c
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
//sscanf("42", "%d", &i);
//printf("%d\n", i);
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));
return 0;
}
Steps I am doing:我正在做的步骤:
# gcc -fPIC -shared preload_localtime.c -o preload_localtime.so -ldl -D_GNU_SOURCE=1
#export LD_PRELOAD=$PWD/preload_localtime.so
gcc foo.c -o foo
output I am getting: output 我得到:
# ./foo
my_local
Current local time and date: Wed Mar 10 18:20:47 2021
What is expected output: It should print test
as well, from printf("test\n");
预期的 output:它也应该打印
test
,从printf("test\n");
of sscanf()
overriden. sscanf()
被覆盖。 Since localtime()
internally has call to sscanf()
and I have overridden and instructed to use sscanf()
as well.由于
localtime()
内部调用了sscanf()
并且我已经覆盖并指示使用sscanf()
。
IS there any way to override another system call like in this example sscanf()
which is internally gets called from localtime()
.是否有任何方法可以覆盖另一个系统调用,例如本例中的
sscanf()
,它在内部从localtime()
调用。
Call hierarchy as below:调用层次结构如下:
localtime()->__tz_convert() ->__tzfile_compute() -->__tzset_parse_tz() -->parse_offset() -->sscanf()
LD_PRELOAD
works by being the first symbol of the same name found by the dynamic linker - so if a symbol usage can be resolved in compile time, the dynamic linking won't affect it because localtime
and sscanf
exist in the same binary the call may very well be a static one - the call can be resolved in compile time and the dynamic linker isn't involved in this call chain. LD_PRELOAD
通过成为动态 linker 找到的第一个同名符号来工作 - 因此,如果可以在编译时解决符号使用,则动态链接不会影响它,因为localtime
和sscanf
存在于同一个二进制文件中,调用可能非常很好是 static 之一 - 调用可以在编译时解决,动态 linker 不参与此调用链。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.