Because module_init and your hook code work in different processes. And between different processes, there are different privileges.
Typically, the code should be executed in the process.
module_init always works during the period of the insmoding module (see the sys_init_module function). When you enter the kernel module, you must be root. And this process is also the root. So it works well.
But when you put the code in the IDT, it can run in the user process, as the user process triggers an interrupt. It happened -EPERM.
You can check euid, uid, pid and comm in your code. Like this:
int hook_func() { printk(KERN_INFO"Code Called in hook_func. My pid: %d, comm: %s, uid: %d, euid: %d\n", current->tgid, current->comm, current->cred->uid, current->cred->euid); ... } int my_init() { printk(KERN_INFO"Code Called in my_init. My pid: %d, comm: %s, uid: %d, euid: %d\n", current->tgid, current->comm, current->cred->uid, current->cred->euid); ... } module_init(my_init);
source share