我使用下面的模块代码来挂钩系统调用(代码记入其他人,例如Linux Kernel: System call hooking example)。系统调用挂钩的问题
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>
#include <asm/semaphore.h>
#include <asm/cacheflush.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(KERN_ALERT "A file was opened\n");
return original_call(file, flags, mode);
}
int set_page_rw(long unsigned int _addr)
{
struct page *pg;
pgprot_t prot;
pg = virt_to_page(_addr);
prot.pgprot = VM_READ | VM_WRITE;
return change_page_attr(pg, 1, prot);
}
int init_module()
{
// sys_call_table address in System.map
sys_call_table = (void*)0xffffffff804a1ba0;
original_call = sys_call_table[1024];
set_page_rw(sys_call_table);
sys_call_table[1024] = our_sys_open;
return 0;
}
void cleanup_module()
{
// Restore the original call
sys_call_table[1024] = original_call;
}
当insmod的编译.ko文件,终端抛出 “杀死”。当查看'cat/proc/modules'文件时,我会看到加载状态。
my_module 10512 1 - Loading 0xffffffff882e7000 (P)
正如预期的那样,我不能勉强这个模块,因为它抱怨它正在使用。系统重新启动以获得清除状态。
之后,在注释上述源sys_call_table[1024] = our_sys_open;
和sys_call_table[1024] = original_call;
中的两条代码行后,它可以成功执行insmod。更有意思的是,当取消注释这两行(更改回原始代码)时,已编译的模块可以成功执行insmod。我不太明白为什么会发生这种情况?有什么办法可以成功编译代码并直接进行insmod吗?
我在Linux内核2.6.24.6的Redhat上做过这些。