Overriding functionality with modules in Linux kernel

后端 未结 13 2045
鱼传尺愫
鱼传尺愫 2020-12-24 13:59

Without getting into the details of why, I\'m looking for a clean (as possible) way to replace kernel functions and system calls from a loadable module. My initial

13条回答
  •  梦谈多话
    2020-12-24 14:35

    According to KernelTrap.org you can do a simple patch and recompile of your kernel to export the sys_call_table variable:

    // add the following in the file arch/i386/kernel/i386_ksyms.c
    extern void* sys_call_table[];
    EXPORT_SYMBOL(sys_call_table);
    

    Then just follow this procedure for replacing system calls from the Linux Kernel Module Programming Guide:

    The source code here is an example of such a kernel module. We want to 'spy' on a certain user, and to printk() a message whenever that user opens a file. Towards this end, we replace the system call to open a file with our own function, called our_sys_open. This function checks the uid (user's id) of the current process, and if it's equal to the uid we spy on, it calls printk() to display the name of the file to be opened. Then, either way, it calls the original open() function with the same parameters, to actually open the file.

    The init_module function replaces the appropriate location in sys_call_table and keeps the original pointer in a variable. The cleanup_module function uses that variable to restore everything back to normal. This approach is dangerous, because of the possibility of two kernel modules changing the same system call. Imagine we have two kernel modules, A and B. A's open system call will be A_open and B's will be B_open. Now, when A is inserted into the kernel, the system call is replaced with A_open, which will call the original sys_open when it's done. Next, B is inserted into the kernel, which replaces the system call with B_open, which will call what it thinks is the original system call, A_open, when it's done.

提交回复
热议问题