尝试挂接Linux内核系统调用

时间:2020-09-22 17:42:00

标签: c linux-kernel hook

我正在尝试从linux内核定制模块中挂接系统调用。

该模块已加载,但是printk似乎没有从新功能中打印任何内容到dmesg。

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/unistd.h>
#include <linux/kallsyms.h>

void **sys_call_table;

int (*original_call) (const char*, int, mode_t);

int our_sys_open(const char* file, int flags, mode_t mode)
{
   printk(KERN_INFO "A file was opened\n");

   return original_call(file, flags, mode);
}

void set_addr_rw(unsigned long addr)
{
    unsigned int level;
    pte_t *pte = lookup_address(addr, &level);

    if (pte->pte &~ _PAGE_RW) pte->pte |= _PAGE_RW;
}

void set_addr_ro(unsigned long addr)
{
    unsigned int level;
    pte_t *pte = lookup_address(addr, &level);

    pte->pte = pte->pte &~_PAGE_RW;
}

int init_module()
{
    printk("Loading custom module\n");

    sys_call_table = (void *) kallsyms_lookup_name("sys_call_table");
    original_call = sys_call_table[__NR_open];

    set_addr_rw((unsigned long) sys_call_table);
    sys_call_table[__NR_open] = our_sys_open;

    return 0;
}

void cleanup_module()
{
    printk("Unloading custom module\n");

    // Restore the original call
    sys_call_table[__NR_open] = original_call;

    set_addr_ro((unsigned long) sys_call_table);
}

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("OTO");
MODULE_VERSION("0.1");

我尝试打开/关闭系统上的文件,但在/var/log/kern.log中没有跟踪。

我的代码主要基于此thread中的信息。

编辑:我还可以给出模块加载前后的sys_call_table条目:

crash> x/1g &sys_call_table[2]
0xffffffffb7c013b0:     0xffffffffb6eda7e0

模块加载后:

crash> x/1g &sys_call_table[2]
0xffffffffb7c013b0:     0xffffffffc06d508f

因此,更改实际上正在发生。

1 个答案:

答案 0 :(得分:0)

原来,我不得不替换openat而不是open。而您要做的是:

#include <linux/module.h>
#include <linux/kallsyms.h>
#include <asm/uaccess.h>
#include <linux/ratelimit.h>
​
MODULE_LICENSE("GPL");
​
asmlinkage int (*old_openat)(const struct pt_regs *);
static void **sys_call_table;
​
static asmlinkage long my_openat(const struct pt_regs *regs)
{
    printk_ratelimited("%s. proc:%s, pid:%d\n", __func__, current->group_leader->comm, current->tgid);
​
    return old_openat(regs);
}
​
void disable_write_protect(void)
{
    unsigned long value;
    asm volatile("mov %%cr0,%0" : "=r" (value));
    if (value & 0x00010000)
    {
        value &= ~0x00010000;
        asm volatile("mov %0,%%cr0": : "r" (value));
    }
}
​
void enable_write_protect(void)
{
    unsigned long value;
    asm volatile("mov %%cr0,%0" : "=r" (value));
    if (!(value & 0x00010000))
    {
        value |= 0x00010000;
        asm volatile("mov %0,%%cr0": : "r" (value));
    }
}
​
static int __init test_init(void)
{
    sys_call_table = (void *)kallsyms_lookup_name("sys_call_table");
    old_openat = sys_call_table[__NR_openat];
​
    printk("[info] %s. old_openat:0x%p\n", __func__, old_openat);
​
    disable_write_protect();
    sys_call_table[__NR_openat] = my_openat;
    enable_write_protect();
​
    printk("%s inserted.\n",__func__);
​
    return 0;
}
​
static void __exit test_exit(void)
{
    disable_write_protect();
    sys_call_table[__NR_openat] = old_openat;
    enable_write_protect();
​
    printk("%s removed.\n",__func__);
}
​
module_init(test_init);
module_exit(test_exit);