我有一个可加载的内核模块,它的init如下所示
static int __init id_init(void)
{
struct identity *temp;
/* some code which is not relevant to the question */
temp = identity_find(3);
pr_debug("id 3 = %s\n", temp->name);
temp = identity_find(42);
if (temp == NULL)
pr_debug("id 42 not found\n");
/* some code which is not relevant to the question */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也在我正在使用的内核版本上启用了动态调试 - 即CONFIG_DYNAMIC_DEBUG=y.
而在模块的Makefile文件我已经添加了一行CFLAGS_[id].o := -DDEBUG这里id.c是文件名.
现在我检查/sys/kernel/debug/dynamic_debug/control了这个模块的insmod之后,我在其中找到了下面的行
/home/pauldc/Programming/Kernel/id/id.c:69 [id]id_init =_ "id 42 not found\012"
/home/pauldc/Programming/Kernel/id/id.c:65 [id]id_init =_ "id 3 …Run Code Online (Sandbox Code Playgroud) 我一直试图在内核级拦截系统调用.我从这个问题中得到了基本的想法.我试图拦截的系统调用是fork().所以我从System.map中找到了sys_fork()的地址,结果证明是0xc1010e0c.现在我编写了如下模块.
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/unistd.h>
#include<linux/semaphore.h>
#include<asm/cacheflush.h>
MODULE_LICENSE("GPL");
void **sys_call_table;
asmlinkage int (*original_call)(struct pt_regs);
asmlinkage int our_call(struct pt_regs regs)
{
printk("Intercepted sys_fork");
return original_call(regs);
}
static int __init p_entry(void)
{
printk(KERN_ALERT "Module Intercept inserted");
sys_call_table=(void *)0xc1010e0c;
original_call=sys_call_table[__NR_open];
set_memory_rw((long unsigned int)sys_call_table,1);
sys_call_table[__NR_open]=our_call;
return 0;
}
static void __exit p_exit(void)
{
sys_call_table[__NR_open]=original_call;
set_memory_ro((long unsigned int)sys_call_table,1);
printk(KERN_ALERT "Module Intercept removed");
}
module_init(p_entry);
module_exit(p_exit);
Run Code Online (Sandbox Code Playgroud)
但是,在编译模块后,当我尝试将其插入内核时,我从dmesg输出中得到以下内容.

当然它不会拦截系统调用.你能帮我解决问题吗?我使用的是3.2.0-4-686版本的Linux内核.
我一直试图在内核级别挂钩系统调用.我从这个问题得到了基本的想法.我试图拦截的系统调用是fork().所以我发现了sys_call_tablefrom 的地址,System.map结果证明是0xc12c9e90.现在我写的模块如下.
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/unistd.h>
#include<linux/semaphore.h>
#include<asm/cacheflush.h>
MODULE_LICENSE("GPL");
void **sys_call_table;
unsigned long addr;
asmlinkage int (*original_call)(struct pt_regs);
asmlinkage int our_call(struct pt_regs regs)
{
printk("Intercepted sys_fork");
return original_call(regs);
}
static int __init p_entry(void)
{
struct page *pg;
printk(KERN_ALERT "Module Intercept inserted");
sys_call_table=(void *)0xc12c9e90;
pg=virt_to_page(sys_call_table);
addr=(unsigned long)page_address(pg);
set_memory_rw(addr,1);
original_call=sys_call_table[__NR_fork];
sys_call_table[__NR_fork]=our_call;
set_memory_ro(addr,1);
return 0;
}
static void __exit p_exit(void)
{
sys_call_table[__NR_fork]=original_call;
set_memory_ro(addr,1);
printk(KERN_ALERT "Module Intercept removed");
}
module_init(p_entry);
module_exit(p_exit);
Run Code Online (Sandbox Code Playgroud)
我编译了模块并尝试将其插入内核.不幸的是,dmesg输出给了我一条消息如下BUG:无法在c12c9e98处理内核分页请求,这里是ellaborate dmesg out put

作为一个找出问题的实验,我只是评论了这一行
sys_call_table[__NR_fork]=our_call; …Run Code Online (Sandbox Code Playgroud)