/*
* kernel_module_new_syscall.c
*
* Educational kernel module that dynamically patches the system call table
* to add a new system call "sys_hellokernel" (assigned to syscall number 359,
* which is typically unused on x86_64 Linux kernels).
*
* WARNINGS:
* - This is a HACK for educational purposes ONLY. It modifies kernel memory
* and can CRASH YOUR SYSTEM, cause data corruption, or lead to instability.
* - Do NOT use in production. Proper syscall addition requires kernel recompilation.
* - Requires a kernel with CONFIG_KALLSYMS=y (most distros have it).
* - Load only on x86_64 (tested on recent Ubuntu/Fedora).
* - Disable kernel module signing if enforced (e.g., mokutil --disable-validation).
* - Backup your system before testing.
* - Unload with rmmod IMMEDIATELY after testing to avoid issues.
*
* Syscall prototype: long sys_hellokernel(char *msg, int len)
* - Copies 'len' bytes from user-space 'msg' to kernel and prints it via printk.
* - Returns 0 on success, -EFAULT on copy error, -EINVAL if len > 256.
*
* User-space test program included at the end.
*
* Build:
* $ make
* Load:
* $ sudo insmod kernel_module_new_syscall.ko
* Test:
* $ ./test_hellokernel "Hello from user-space!"
* Unload:
* $ sudo rmmod kernel_module_new_syscall
*
* Check dmesg for output: dmesg | tail
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kallsyms.h>
#include <linux/uaccess.h> /* For copy_from_user */
#include <asm/unistd.h> /* For __NR_* */
#include <asm/syscall.h> /* Not strictly needed, but for completeness */
#define MY_SYSCALL_NR 359 /* Unused on x86_64 (check your arch's syscall table) */
#define MAX_MSG_LEN 256
/* Our new system call */
asmlinkage long sys_hellokernel(char __user *msg, int len)
{
char kmsg[MAX_MSG_LEN + 1];
if (len <= 0 || len > MAX_MSG_LEN)
return -EINVAL;
if (copy_from_user(kmsg, msg, len))
return -EFAULT;
kmsg[len] = '\0'; /* Null-terminate */
printk(KERN_INFO "HELLOKERNEL: User message: '%s' (len=%d, pid=%d)\n",
kmsg, len, current->pid);
return 0;
}
/* System call table pointer */
static unsigned long **sys_call_table;
/* Backup of original syscall pointer */
static asmlinkage long (*orig_call)(char __user *, int);
/* Disable write protection on CR0 */
static inline void disable_wp(void)
{
unsigned long cr0 = read_cr0();
clear_bit(16, &cr0); /* CR0.WP bit */
write_cr0(cr0);
}
/* Re-enable write protection on CR0 */
static inline void enable_wp(void)
{
unsigned long cr0 = read_cr0();
set_bit(16, &cr0); /* CR0.WP bit */
write_cr0(cr0);
}
static int __init syscall_init(void)
{
printk(KERN_INFO "HELLOKERNEL: Loading module...\n");
/* Lookup sys_call_table symbol */
sys_call_table = (unsigned long **)kallsyms_lookup_name("sys_call_table");
if (!sys_call_table) {
printk(KERN_ERR "HELLOKERNEL: Could not find sys_call_table!\n");
return -EINVAL;
}
printk(KERN_INFO "HELLOKERNEL: sys_call_table found at %px\n", sys_call_table);
/* Backup original */
orig_call = (void *)sys_call_table[MY_SYSCALL_NR];
printk(KERN_INFO "HELLOKERNEL: Original handler at %px\n", orig_call);
/* Patch the table */
disable_wp();
sys_call_table[MY_SYSCALL_NR] = (unsigned long)sys_hellokernel;
enable_wp();
printk(KERN_INFO "HELLOKERNEL: Syscall %d patched successfully!\n", MY_SYSCALL_NR);
return 0;
}
static void __exit syscall_exit(void)
{
/* Restore original */
if (sys_call_table) {
disable_wp();
sys_call_table[MY_SYSCALL_NR] = (unsigned long)orig_call;
enable_wp();
printk(KERN_INFO "HELLOKERNEL: Syscall %d restored.\n", MY_SYSCALL_NR);
}
printk(KERN_INFO "HELLOKERNEL: Module unloaded.\n");
}
module_init(syscall_init);
module_exit(syscall_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sherlock @ Trailblazer Labs");
MODULE_DESCRIPTION("Educational kernel module adding a new syscall via table patching");
MODULE_VERSION("1.0");