I override the libc open()
function like this and compile it to a shared library called libc_custom.so:
struct sys_funcs_t
{
sys_funcs_t()
{
libc_hdl = dlopen("/usr/lib64/libc.so.6", RTLD_NOW);
sys_open = (int (*)(const char *str, int flags, ...))dlsym(libc_hdl, "open");
}
~sys_funcs_t()
{
dlclose(libc_hdl);
}
int (*sys_open)(const char *, int, ...);
private:
void *libc_hdl;
};
static sys_funcs_t g_sys_funcs;
static int realopen(const char *path, int flags, va_list args)
{
if (flags & (O_CREAT | O_TMPFILE))
{
mode_t mode = va_arg(args, mode_t);
return g_sys_funcs.sys_open(path, flags, mode);
}
else
{
return g_sys_funcs.sys_open(path, flags);
}
}
extern "C" __attribute__((visibility ("default")))
int open(const char *pathname, int flags, ...)
{
printf("---- custom open %s\n", pathname);
va_list args;
va_start(args, flags);
int fd = realopen(pathname, flags, args);
va_end(args);
return fd;
}
It is quite strange that the following simple python code hangs with LD_PRELOAD=libc_custom.so
:
import os
ret=os.popen("uname -p").read()
print(ret)
the uname -p
can be any other command, the gdb shows that it hangs at __read_nocancel()
:
(gdb) bt
#0 0x00007f4064e91700 in __read_nocancel () from /lib64/libpthread.so.0
#1 0x0000000000439876 in _Py_read ()
#2 0x00000000005b837e in ?? ()
#3 0x00000000004bded3 in _PyCFunction_FastCallDict ()
#4 0x0000000000455ff1 in _PyObject_FastCallDict ()
#5 0x00000000004573f4 in PyObject_CallMethodObjArgs ()
#6 0x00000000005bf95e in ?? ()
#7 0x00000000004bde8b in _PyCFunction_FastCallDict ()
#8 0x000000000044ee2e in ?? ()
#9 0x0000000000457278 in _PyObject_CallMethodId_SizeT ()
#10 0x00000000005c55fa in ?? ()
#11 0x00000000004be28d in _PyCFunction_FastCallKeywords ()
#12 0x0000000000545f34 in ?? ()
#13 0x000000000054aa3e in _PyEval_EvalFrameDefault ()
#14 0x0000000000545b31 in ?? ()
#15 0x0000000000546b03 in PyEval_EvalCode ()
#16 0x00000000004272f5 in PyRun_FileExFlags ()
#17 0x00000000004274c5 in PyRun_SimpleFileExFlags ()
#18 0x000000000043e9e5 in Py_Main ()
#19 0x000000000041e160 in main ()
when I press "Enter" 4 times, the code can continue executing.
python version: 3.6.10
libc version: 2.17