使用 LD_PRELOAD 替换 Linux 系统调用时出现问题

发布于 2024-07-21 06:51:37 字数 149 浏览 5 评论 0原文

我正在尝试编写一个程序,允许运行二进制文件,在请求时用另一个文件替换某个文件。 它是一个对系统调用函数进行简单替换的库,与 LD_PRELOAD 一起使用。 问题是它捕获打开以供读取(而是读取替代文件),但写入总是返回到实际的指定文件。 还有其他我应该了解的“开放”系统调用吗?

I am trying to write a program that allows a binary to be run, substituting a certain file when requested with another. It is a library with simple replacements for the system call functions, that is used with LD_PRELOAD. The problem is that it catches opens for reading (the substitute file is read instead), but writes always go back to the actual, specified file. Are there any other "open" system calls I should know about?

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

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

发布评论

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

评论(4

樱花坊 2024-07-28 06:51:37

没关系——愚蠢的错误。

没有检查绝对路径和相对路径...

Nevermind -- stupid mistake.

Wasn't checking both absolute and relative paths...

此生挚爱伱 2024-07-28 06:51:37

我不确定你的问题的原因是什么,但是在你的程序上使用 strace 可能会给一些洞察力。 它应该是任何正常的 Linux 发行版的一部分。

I am not sure what the cause of your problem is, but using strace on your program might give some insight. It should be part of any sane Linux distribution.

栀梦 2024-07-28 06:51:37

如果它开放供写入,则很可能会通过 creat 函数(我猜测 fopen 会将您重定向到那里)。 检查您的 fcntl.h 以获得完整列表。

If it's open for writing, it's most likely going through the creat function (I'm guessing fopen would be redirecting you there). Check your fcntl.h for a complete list.

葬シ愛 2024-07-28 06:51:37

当请求时用另一个文件替换某个文件

听起来好像您只检查输入文件(您按文件名检查吗?)。 您还需要检查并替换输出文件。

如果您的输出转到标准输出之一,那么您需要在分叉到可执行文件之前关闭并使用输出替代项重新打开它们。

要查找可执行文件发出的所有系统调用,您可以使用 strace
要查找可执行文件进行的所有库调用,您可以使用 ltrace

substituting a certain file when requested with another

Sounds like you check only for the input file (do you check by filename?). You need to check and substitute the output file, too.

If you output goes to one of the standard outputs, then you need to close and reopen them with your output substitute) before you fork into the executable.

To find all system calls that your executable makes you can use strace.
To find all library calls that your executable makes you can use ltrace.

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