我正在尝试编写一些简单的测试代码,以演示如何钩取系统调用表。
"sys_call_table" 在2.6中不再被导出,所以我只能从 System.map 文件中获取地址。经过查看,我可以确认这个地址是正确的(通过查看我找到的地址处的内存,我可以看到指向系统调用的指针)。
然而,当我尝试修改这个表时,内核会报错并显示 "unable to handle kernel paging request at virtual address c061e4f4",接着机器就会重新启动。
这是运行2.6.18-164.10.1.el5版本的 CentOS 5.4。是否存在某种保护措施,或者我只是遇到了一个 bug?我知道它带有 SELinux,并已尝试将其设置为宽容模式,但没有任何改变。
这是我的代码:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>
void **sys_call_table;
asmlinkage int (*original_call) (const char*, int, int);
asmlinkage int our_sys_open(const char* file, int flags, int mode)
{
printk("A file was opened\n");
return original_call(file, flags, mode);
}
int init_module()
{
// sys_call_table address in System.map
sys_call_table = (void*)0xc061e4e0;
original_call = sys_call_table[__NR_open];
// Hook: Crashes here
sys_call_table[__NR_open] = our_sys_open;
}
void cleanup_module()
{
// Restore the original call
sys_call_table[__NR_open] = original_call;
}
LD_PRELOAD
或者ptrace
吗?它们不能满足你的需求吗? - ezpz