md.*_*mal 4 c security x86 system-calls linux-kernel
我想更新系统调用表以使用我的自定义打开函数。我写了以下代码:
#include <linux/module.h>
#include <linux/kallsyms.h>
MODULE_LICENSE("GPL");
char *sym_name = "sys_call_table";
typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *);
static sys_call_ptr_t *sys_call_table;
typedef asmlinkage long (*custom_open) (const char __user *filename, int flags, umode_t mode);
custom_open old_open;
static asmlinkage long my_open(const char __user *filename, int flags, umode_t mode)
{
pr_info("%s\n",__func__);
return old_open(filename, flags, mode);
}
static int __init hello_init(void)
{
sys_call_table = (sys_call_ptr_t *)kallsyms_lookup_name(sym_name);
old_open = (custom_open)sys_call_table[__NR_open];
sys_call_table[__NR_open] = (sys_call_ptr_t)my_open;
return 0;
}
static void __exit hello_exit(void)
{
sys_call_table[__NR_open] = (sys_call_ptr_t)old_open;
}
module_init(hello_init);
module_exit(hello_exit);
Run Code Online (Sandbox Code Playgroud)
当我加载模块时,出现以下错误:
[69736.192438] BUG: unable to handle page fault for address: ffffffff98e001d0
[69736.192441] #PF: supervisor write access in kernel mode
[69736.192442] #PF: error_code(0x0003) - permissions violation
[69736.192443] PGD 10460e067 P4D 10460e067 PUD 10460f063 PMD 80000001040000e1
[69736.192461] Oops: 0003 [#1] SMP PTI
[69736.192463] CPU: 0 PID: 45249 Comm: insmod Tainted: G OE 5.2.8 #6
Run Code Online (Sandbox Code Playgroud)
我可以更新系统调用表吗?我该如何解决这样的错误?
您快到了。在 Intel x86 CPU 中,控制寄存器CR0有一个特殊位(称为写保护位),用于控制 CPU 在特权级别 0 运行时是否可以写入只读页面(内核代码确实在特权级别 0 运行)。
由于系统调用表位于只读页面内,并且默认情况下设置了“写保护”位,因此您无法对其进行写入。因此,如果您尝试执行以下操作:
sys_call_table[__NR_open] = (sys_call_ptr_t)my_open;
Run Code Online (Sandbox Code Playgroud)
你会崩溃的一切。
为了正确劫持系统调用,您需要在覆盖表条目之前通过将CR0寄存器的“写保护”位设置为禁用写保护0,并在完成后重新启用它。这两个宏read_cr0()和write_cr0()正是用于操作所述寄存器。
这是正确的代码:
// Temporarily disable write protection
write_cr0(read_cr0() & (~0x10000));
// Overwrite the syscall table entry
sys_call_table[__NR_open] = /* whatever */;
// Re-enable write protection
write_cr0(read_cr0() | 0x10000);
Run Code Online (Sandbox Code Playgroud)
0x10000上面使用了掩码,因为“写保护”位是寄存器的第 17 个最低有效位。
请注意,上述步骤需要在模块的init和exit函数中完成。
| 归档时间: |
|
| 查看次数: |
947 次 |
| 最近记录: |