strangerRidingCaml

2. Development of Kernel Module 본문

Linux kernel exploit

2. Development of Kernel Module

woddlwoddl 2024. 5. 12. 02:56
728x90
Development of Kernel Module

Development of Kernel Module

Basics of kernel module development

Kernel modules are pieces of code that can be dynamically loaded and unloaded into the Linux kernel without rebooting the system.

  • Module structure: A kernel module typically consists of initialization and cleanup functions along with the necessary code to perform its intended functionality.
  • Module compilation: Kernel modules are usually written in C and compiled separately from the kernel source code using appropriate build tools.
  • Module dependencies: Modules can depend on other modules or kernel features and must be compiled accordingly to ensure proper functionality.

Kernel module loading and unloading

Loading and unloading kernel modules is a fundamental aspect of kernel module development.

  • Loading modules: Modules can be loaded into the kernel using the insmod command or automatically at system boot through configuration files.
  • Unloading modules: Modules can be unloaded from the kernel using the rmmod command, freeing up system resources and memory.

Simple kernel module examples

Here are some simple examples of kernel modules:

Example 1: Hello World Kernel Module


#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void) {
    printk(KERN_INFO "Hello world!\n");
    return 0;
}

void cleanup_module(void) {
    printk(KERN_INFO "Goodbye world!\n");
}

Example 2: Parameterized Kernel Module


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/moduleparam.h>

static char *name = "world";
module_param(name, charp, S_IRUGO);

int init_module(void) {
    printk(KERN_INFO "Hello %s!\n", name);
    return 0;
}

void cleanup_module(void) {
    printk(KERN_INFO "Goodbye %s!\n", name);
}