Excuse the ads! We need some help to keep our site up.

List

Hello world!

OS Information

lazenca0x0@ubuntu:~$ uname -a
Linux ubuntu 4.18.0-11-generic #12-Ubuntu SMP Tue Oct 23 19:22:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

lazenca0x0@ubuntu:~$ gcc --version
gcc (Ubuntu 8.2.0-7ubuntu1) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
lazenca0x0@ubuntu:~$ 

Installing the Development Environment

apt-get install build-essential make

Simple Linux Kernel Module

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */

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

void cleanup_module(void) { 
	printk(KERN_INFO "Goodbye world - Lazenca0x0.\n");
}
obj-m += sample.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
lazenca0x0@ubuntu:~/Kernel/Module$ make
make -C /lib/modules/4.18.0-10-generic/build M=/home/lazenca0x0/Kernel/Module modules
make[1]: Entering directory '/usr/src/linux-headers-4.18.0-10-generic'
Makefile:982: "Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel"
  CC [M]  /home/lazenca0x0/Kernel/Module/sample.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: modpost: missing MODULE_LICENSE() in /home/lazenca0x0/Kernel/Module/sample.o
see include/linux/module.h for more information
  CC      /home/lazenca0x0/Kernel/Module/sample.mod.o
  LD [M]  /home/lazenca0x0/Kernel/Module/sample.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.18.0-10-generic'
lazenca0x0@ubuntu:~/Kernel/Module$ ls
Makefile  modules.order  Module.symvers  sample.c  sample.ko  sample.mod.c  sample.mod.o  sample.o
lazenca0x0@ubuntu:~/Kernel/Module$
lazenca0x0@ubuntu:~/Kernel/Module$ modinfo ./sample.ko 
filename:       /home/lazenca0x0/Kernel/Module/./sample.ko
srcversion:     EA26170C58DCB24AA000831
depends:        
retpoline:      Y
name:           sample
vermagic:       4.18.0-10-generic SMP mod_unload 
lazenca0x0@ubuntu:~/Kernel/Module$ 

insmod - simple program to insert a module into the Linux Kernel

lazenca0x0@ubuntu:~/Kernel/Module$ sudo insmod ./sample.ko 
lazenca0x0@ubuntu:~/Kernel/Module$ lsmod | grep sample
sample                 16384  0
lazenca0x0@ubuntu:~/Kernel/Module$ 
lazenca0x0@ubuntu:~/Kernel/Module$ dmesg | tail
[   60.542706] [drm:vmw_stdu_crtc_page_flip [vmwgfx]] *ERROR* Page flip error -16.
[  934.447473] Bluetooth: RFCOMM TTY layer initialized
[  934.447477] Bluetooth: RFCOMM socket layer initialized
[  934.447483] Bluetooth: RFCOMM ver 1.11
[  936.725345] rfkill: input handler disabled
[ 1645.755164] sample: loading out-of-tree module taints kernel.
[ 1645.755166] sample: module license 'unspecified' taints kernel.
[ 1645.755167] Disabling lock debugging due to kernel taint
[ 1645.755250] sample: module verification failed: signature and/or required key missing - tainting kernel
[ 1821.132892] Hello world - Lazenca0x0.
lazenca0x0@ubuntu:~/Kernel/Module$

rmmod - simple program to remove a module from the Linux Kernel

lazenca0x0@ubuntu:~/Kernel/Module$ sudo rmmod sample 
lazenca0x0@ubuntu:~/Kernel/Module$ dmesg | tail
[   60.542706] [drm:vmw_stdu_crtc_page_flip [vmwgfx]] *ERROR* Page flip error -16.
[  934.447473] Bluetooth: RFCOMM TTY layer initialized
[  934.447477] Bluetooth: RFCOMM socket layer initialized
[  934.447483] Bluetooth: RFCOMM ver 1.11
[  936.725345] rfkill: input handler disabled
[ 1645.755164] sample: loading out-of-tree module taints kernel.
[ 1645.755166] sample: module license 'unspecified' taints kernel.
[ 1645.755167] Disabling lock debugging due to kernel taint
[ 1645.755250] sample: module verification failed: signature and/or required key missing - tainting kernel
[ 1821.132892] Hello world - Lazenca0x0.
[ 1897.711100] Goodbye world - Lazenca0x0.
lazenca0x0@ubuntu:~/Kernel/Module$

References