为什么 LD_PRELOAD 不能与 Python 一起使用?
在 Python 中使用 open()
的函数插入在前几次调用后似乎不起作用。我怀疑 Python 正在进行某种初始化,或者某些东西暂时绕过了我的函数。
这里的 open
调用显然被钩住了:
$ cat a
hi
$ LD_PRELOAD=./libinterpose_python.so cat a
sandbox_init()
open()
hi
这里它在 Python 初始化期间发生了一次:
$ LD_PRELOAD=./libinterpose_python.so python
sandbox_init()
Python 2.7.2 (default, Jun 12 2011, 20:20:34)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
open()
>>>
sandbox_fini()
这里它根本没有发生,并且没有错误表明文件句柄已删除写入权限:
$ LD_PRELOAD=./libinterpose_python.so python3 -c 'b = open("a", "w"); b.write("hi\n"); b.flush()'
sandbox_init()
sandbox_fini()
代码是此处。使用 make -f Makefile.interpose_python 进行构建。
此处给出了完整的解决方案。
Using function interposition for open()
with Python doesn't seem to work after the first few calls. I suspect Python is doing some kind of initialization, or something is temporarily bypassing my function.
Here the open
call is clearly hooked:
$ cat a
hi
$ LD_PRELOAD=./libinterpose_python.so cat a
sandbox_init()
open()
hi
Here it happens once during Python initialization:
$ LD_PRELOAD=./libinterpose_python.so python
sandbox_init()
Python 2.7.2 (default, Jun 12 2011, 20:20:34)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
open()
>>>
sandbox_fini()
Here it doesn't happen at all, and there's no error to indicate the file handle had write privileges removed:
$ LD_PRELOAD=./libinterpose_python.so python3 -c 'b = open("a", "w"); b.write("hi\n"); b.flush()'
sandbox_init()
sandbox_fini()
The code is here. Build with make -f Makefile.interpose_python
.
A full solution is given here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有 open() 和 open64() 函数,您可能需要重新定义这两个函数。
There are open() and open64() functions, you might need to redefine both.
您应该能够通过在
strace
下运行您的 python 进程来找出它实际上在做什么(可能没有预加载)。我的 python3.1(在 AMD64 上)似乎使用
open
:You should be able to find out what your python process is actually doing by running it under
strace
(probably without your pre-load).My python3.1 (on AMD64) does appear to use
open
:事实证明有一个
open64 ()
函数:运行未注释的 open64 部分的示例代码会得到:
它清楚地显示了所有 Python 的 open 调用,以及由于从调用中剥离 write 标志而导致的几个传播错误。
It turns out there is an
open64()
function:Running the example code with the
open64
section uncommented gives:Which clearly shows all of Python's
open
calls, and several propagated errors due to the write flag being stripped from the calls.