Excuse the ads! We need some help to keep our site up.
List
Hello world!
OS Information
Ubuntu 18.10
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
- Linux Kernel Module을 만들기 위해 아래 패키지들이 필요합니다.
apt-get install build-essential make
Simple Linux Kernel Module
- 다음 코드는 제일 간단한 Kernel Module 코드 입니다.
init_module() 함수는 Module이 Kernel에 삽입될 때 동작 해야하는 코드를 포함합니다.
- cleanup_module() 함수는 Module이 Kernel에 제거될 때 동작 해야하는 코드를 포함합니다.
- 해당 코드에서는 printk() 함수를 이용하여 Module이 삽입,제거 될 때 메시지를 출력합니다.
sample.c
#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"); }
- 다음과 같이 Makefile을 이용하여 Module을 빌드 할 수 있습니다.
Makefile
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
- 다음과 같이 make를 실행하면 Kernel module인 ".ko" 확장자를 가지는 파일이 생성됩니다.
- Kernel module : sample.ko
make
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$
- modinfo 명령어를 이용하여 생성된 Kernel module의 정보를 확인 할 수 있습니다.
modinfo ./sample.ko
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
- insmod 명령어를 이용하여 Linux Kernel에 Module을 등록할 수 있습니다.
- lsmod 명령어를 이용하여 Linux Kernel에 등록된 Module들을 확인 할 수 있습니다.
sudo insmod ./sample.ko
lazenca0x0@ubuntu:~/Kernel/Module$ sudo insmod ./sample.ko lazenca0x0@ubuntu:~/Kernel/Module$ lsmod | grep sample sample 16384 0 lazenca0x0@ubuntu:~/Kernel/Module$
- dmesg 명령어를 이용하여 Module에서 출력하는 메시지를 확인 할 수 있습니다.
- "sample.ko" Module이 Kenel에 삽입된 후 "Hello world - Lazenca0x0." 메시지가 출력되었습니다.
dmesg | tail
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
- rmmod 명령어를 이용하여 Linux Kernel에 등록된 Module을 제거 할 수 있습니다.
- "sample.ko" Module이 Kenel에 제거된 후 "Goodbye world - Lazenca0x0." 메시지가 출력되었습니다.
sudo rmmod sample
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
- N/a