希望 execve() 运行的可执行文件使用我的预加载库
我正在执行一个程序,比如另一个程序,首先进行 fork,然后执行 execve()。现在的问题是我希望 A 使用我的库,而我通常会使用 LD_PRELOAD 来实现。我如何在 execve() 中执行此操作?
谢谢
I am executing a program say A from another by first fork-ing followed by execve(). Now the problem is I would want A to use my library that I would generaly do by using LD_PRELOAD. How do I do it within execve().
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 envp execve 的参数中传递 LD_PRELOAD:
被执行的程序,名为“run”:
执行 execve 的程序,名为“ex”:
运行它:
编辑:
显示的错误被抛出,因为“caca”lib 无法预加载运行,所以它可以工作。 (为了清楚起见,我跳过了 fork() 部分,但用法是相同的)
编辑:
做类似的事情:
如果您不通过 envp execve() 的参数传递它,则在 execve()-ing 运行时不会自动预加载 caca lib
you can pass the LD_PRELOAD in envp execve's argument:
the program that gets execved, named "run":
the program that does the execve, named "ex":
running it:
EDIT:
the error shown gets thrown because "caca" lib can't be preloaded for run, so it works. (I skipped the fork() part for clarity but the usage is the same)
EDIT:
doing something like:
will not automagically preload caca lib when execve()-ing run if you're not passing it via envp execve()'s argument
如果您只想对程序 A(而不是其父程序)使用 LD_PRELOAD,您可以通过 shell 加载它;将要执行的程序的名称传递给 shell 并将 LD_PRELOAD 添加到环境中。
If you want to use LD_PRELOAD just for program A (and not for its parent) you could load it via the shell; pass to the shell the name of the program to execute and add LD_PRELOAD to the environment.
更新
阅读问题中添加的信息后,我猜您可能必须指定完整路径,或者也设置 LD_LIBRARY_PATH ?由于加载程序正在确认已订购预加载的事实。
否则,我可以想象存在安全限制(尽管它必须与从登录 shell 调用运行相关联,这似乎很难检测)。尽管如此,您可能希望尝试以 root 身份运行(使用 sudo -E 来保持您的环境)
它会出现在 这个较早的问题表明这种行为是默认的
LD_PRELOAD 即使在 unsetenv("LD_PRELOAD") 之后也会影响新的子进程
你测试过吗?
Update
After reading the added info from the question, I'm guessing that you might have to specify a complete path, or set LD_LIBRARY_PATH as well? Since the loader is acknowledging the fact that the preload is ordered.
Otherwise, I can imagine there being a security restriction (allthough it would have to be tied to being run invoked from a login shell, which seems quite brittle to detect). Nonetheless, you may wish to try running as root (use
sudo -E
to keep your environment)It would appear from this earlier question that such behaviour is the default
LD_PRELOAD affects new child even after unsetenv("LD_PRELOAD")
Have you tested it?