在 Linux 中运行时更改系统调用函数指针

发布于 2025-01-09 21:47:52 字数 642 浏览 0 评论 0原文

我有一个巨大的项目,正在创建很多我想跟踪它们的文件和文件夹。 为了调试代码,我想替换系统调用行为来检查发生了什么。

我的想法是在使用系统调用的同一位置挂接一个新函数,并在应用程序启动后查看应用程序的行为。更清楚地说,这是我需要的示例:

该应用程序正在创建一个烦人的文件夹,例如 /tmp/annoying_folder。所以我想拦截每个 mkdir 系统调用并检查它的参数是否是烦人的文件夹,如果是这种情况,则强制它返回错误,这样我就可以找到哪个进程正在执行此操作,并且还知道它是堆栈调用。

我到目前为止所尝试的是使用 LD_PRELOAD,这在此应用程序的情况下不起作用,因为它正在执行直接系统调用,而不是通过 libc。

我在使用 gdb 时遇到问题,因为我不确定哪个进程正在执行这些调用,因为应用程序是由调用多个其他进程的脚本启动的。

通过 strace 我可以看到我正在寻找的 mkdir 调用,但这对我没有多大帮助,因为我还需要知道应用程序的堆栈跟踪调用来找出正在生成的代码在哪里这。

因此,一个有趣的选择是使用 LD_PRELOAD 加载带有构造函数的库,该函数将更改 mkdir 的挂钩点并将其重定向到我的自定义函数。但我需要有关如何对 Linux 系统调用执行此操作的指导。

有人知道如何在运行时更改系统调用函数指针吗?

I have a huge project that is creating a lot of files and folders that I want to track them.
In order to debug the code, I would like to replace a system call behavior to check what is going on.

My idea is to hook a new function in the same place where the system call is being used and see the behavior of the application, after it has started. To be more clear, here is an example of what I need:

The application is creating a annoying folder like /tmp/annoying_folder. So I would like to intercept every mkdir system call and check if the it's argument is the annoying_folder and if it is the case, force it to return an error, so I can locate which process is doing this and also know it's stack call.

What I have tried up to now is using LD_PRELOAD, which is not working in the case of this application, because it is doing direct system calls, instead of going through libc.

I'm having trouble using gdb, because I'm not sure which process is doing these calls, because the application is started by a script that calls multiple other processes.

Through strace I'm able to see the mkdir call that I'm looking for, but it doesn't help me much, because I need to also know the stack trace call of the application to figure out where is the code that is generating this.

So one option that thought to be interesting is to use LD_PRELOAD to load a library with a constructor function that would change the hook point of mkdir and redirect it to my custom function. But I need directions on how to do that for Linux system calls.

Do someone knows how to change System calls function pointers at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洛阳烟雨空心柳 2025-01-16 21:47:52

我无法按照预期拦截这些系统调用,但我发现了 stap 实用程序的一个有趣的解决方法。

我创建了以下脚本:

#! /bin/stap -g

probe nd_syscall.mkdir.return {
  folder_name = user_string(@entry(pointer_arg(1)), "-");
  folder_name_prefix = substr(folder_name, 0, 9);
  if(folder_name_prefix == "/tmp/test") {
    printf("[%d] [%d] [%16s] [%s]\n", uid(), pid(), execname(), folder_name);
    raise(%{ SIGSTOP %});
}

然后我能够向进程发送停止信号,然后连接 gdb 来分析应用程序堆栈跟踪。

I wasn't able to intercept those system calls as I expected, but I've found an interesting workaround with stap utility.

I've created the following script:

#! /bin/stap -g

probe nd_syscall.mkdir.return {
  folder_name = user_string(@entry(pointer_arg(1)), "-");
  folder_name_prefix = substr(folder_name, 0, 9);
  if(folder_name_prefix == "/tmp/test") {
    printf("[%d] [%d] [%16s] [%s]\n", uid(), pid(), execname(), folder_name);
    raise(%{ SIGSTOP %});
}

Then I was able to send a signal stop to the process and after that connect gdb to analyze the application stack trace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文