插入自定义模块污点内核

时间:2018-07-30 23:21:51

标签: linux-device-driver

我只想插入一个不污染内核的模块。

这是文件test1.c

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

MODULE_LICENSE("GPL")
MODULE_AUTHOR("AUTHOR")
MODULE_DESCRIPTION("DESCRIPTION")

static int __init module_hello(void) {
    printk(KERN_ALERT "Hello");
    return 0;
}

static void __exit module_bye(void) {
    printk(KERN_ALERT "Bye");
}

module_init(module_hello);
module_exit(module_bye);

在同一文件夹中,如kernel.org第3节中所述,文件Makefile

ifneq ($(KERNELRELEASE),)

obj-m  := test1.o

else

KDIR ?= /lib/modules/`uname -r`/build

default:
    $(MAKE) -C $(KDIR) M=$$PWD

endif

执行make会正确创建必要的文件,但是当我插入文件时,会收到消息Loading out-of-tree modules taints kernel,直到删除模块后我才得到module_hello的输出。 ,然后,如果我再次插入它,我会从module_bye函数中收到消息,但不是问候消息。

3 个答案:

答案 0 :(得分:2)

消息延迟是由于每次打印末尾缺少\n\n将数据从内核消息缓冲区放到文件中。

有关受污染的内核,请参考this link

答案 1 :(得分:0)

kernel.org第1节中一样,

  

[...]所有模块都是最初开发和构建的   树外。

因此,起初内核将被污染。

答案 2 :(得分:0)

我正在使用下一个Makefile对新创建的模块进行签名: 工作流程是:“ make clean; make; make key; make cp; make sign; make install”

CONFIG_MODULE_SIG:=y

#https://stackoverflow.com/questions/24975377/kvm-module-verification-failed-signature-and-or-required-key-missing-taintin
kernel:=$(shell uname -r)
KSRC:=/usr/src/kernels/${kernel}
KBDIR:=/usr/lib/modules/${kernel}/build
KROOT:=/usr/lib/modules/${kernel}
#CONFIG_MODULE_SIG:=y
#/usr/lib/modules/4.18.0-193.19.1.el8_2.x86_64/extra/kvedemo.ko
#  /lib/modules/4.18.0-193.19.1.el8_2.x86_64/kernel/lib/kvedemo.ko
INSTALLPATH:=${KROOT}/kernel/lib
INSTALLPATH2:=${KROOT}/extra
#CONFIG_MODULE_SIG=y
# obj-m is a list of what kernel modules to build.  The .o and other
# objects will be automatically built from the corresponding .c file -
# no need to list the source files explicitly.
driver:=kvedemo
obj-m:= ${driver}.o

# KSRC is the location of the kernel source.  The current standard is
# to link to the associated source tree from the directory containing
# the compiled modules.

    # PWD is the current working directory and the location of our module
    # source files.
    PWD:= $(shell pwd)
    test:
        echo    ${KROOT}
        ls  ${KROOT}
    ${driver}.ko:   ${driver}.c
        $(MAKE) -C $(KSRC) M=$(PWD) 
    clean:
    #   rm *.o *.ko
        $(MAKE) -C $(KBDIR) M=$(PWD) clean
    key:
        openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 -config x509.genkey -outform DER -out signing_key.x509 -keyout signing_key.priv
        openssl req -x509 -newkey rsa:4096 -nodes -utf8 -sha512 -days 36500 \
    -batch -config ./configuration_file.config -outform DER \
    -out my_signing_key_pub.der \
    -keyout kernel-signing_key.priv
    cp:
        sudo cp signing_key.x509 ${KSRC}/certs/
        sudo cp signing_key.priv ${KSRC}/certs/
        sudo cp signing_key.pem ${KSRC}/certs/
    
    pem:
        openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 -config x509.genkey -outform PEM -out kernel_key.pem -keyout ./signing_key.pem
    priv:
        openssl req -x509 -newkey rsa:4096 -nodes -utf8 -sha256 -days 36500 \
    -batch -config configuration_file.config -outform DER \
    -out my_kernel-signing_key_pub.der \
    -keyout kernel-signing_key.priv
    #   openssl req -newkey  rsa:4096 -nodes -utf8 sha512 -days 3650 -batch -x509 -config ./kernel-signing_key.x509 -outform PEM -out kernel-signing_key.x509  -keyout ./kernel-signkey.priv
    sign:${driver}.ko
    #/usr/src/kernels/4.18.0-193.19.1.el8_2.x86_64/scripts/sign-file sha512 ./kernel-signing_key.priv ./kernel-signing_key.x509 kvedemo.ko
    ${KSRC}/scripts/sign-file sha512 ./signing_key.priv ./signing_key.x509 ${driver}.ko
install:${driver}.ko
    sudo cp ${driver}.ko ${INSTALLPATH}/
    sudo  depmod -a 
#   sudo ls -l  ${INSTALLPATH}
modules_install:${driver}.ko
#   hexdump -C /usr/lib/modules/4.18.0-193.19.1.el8_2.x86_64/extra/kvedemo.ko 
    sudo   $(MAKE) -C $(KBDIR) M=$(PWD) modules_install   CONFIG_MODULE_SIG=y
    sudo  depmod -a
    
# default is the default make target.  The rule here says to run make
# with a working directory of the directory containing the kernel
# source and compile only the modules in the PWD (local) directory.
#default:
#   $(MAKE) -C $(KSRC) M=$(PWD) modules
after make clean ; make you have to generate key with `make key`then sign your module by`make sign`then install it `make install`