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
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, calledour_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 callsprintk()to display the name of the file to be opened. Then, either way, it calls the originalopen()function with the same parameters, to actually open the file.The
init_modulefunction replaces the appropriate location insys_call_tableand keeps the original pointer in a variable. Thecleanup_modulefunction 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'sopensystem call will beA_openand B's will beB_open. Now, when A is inserted into the kernel, the system call is replaced withA_open, which will call the originalsys_openwhen it's done. Next, B is inserted into the kernel, which replaces the system call withB_open, which will call what it thinks is the original system call,A_open, when it's done.