I am trying to hook a system call from a linux kernel custom module.
The module loads but printk
doesn't seem to print anything to dmesg from the new function.
#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");
I try opening/closing files on the system, but no trace in /var/log/kern.log
.
My code is mostly based on the info from this thread.
EDIT: I can also give is the sys_call_table entry before and after module load:
crash> x/1g &sys_call_table[2]
0xffffffffb7c013b0: 0xffffffffb6eda7e0
After module load:
crash> x/1g &sys_call_table[2]
0xffffffffb7c013b0: 0xffffffffc06d508f
So, changes are actually taking place.