Linux 内核模块开发 "module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1"

Linux Kernel Module Development "module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1"

我目前正在尝试开发一个简单的 linux 内核模块。它应该只是记录一些东西,它 1:1 从互联网上复制的。

我有以下文件: lkm_example.c

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Robert W. Oliver II");
MODULE_DESCRIPTION("A simple example Linux module.");
MODULE_VERSION("0.01");

static int __init lkm_example_init(void) {
 printk(KERN_INFO "Hello, World!\n");
 return 0;
}

static void __exit lkm_example_exit(void) {
 printk(KERN_INFO "Goodbye, World!\n");
}

module_init(lkm_example_init);
module_exit(lkm_example_exit);

生成文件:

obj-m += lkm_example.o

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

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

我还做了以下事情:

sudo apt-get install build-essential linux-headers-`uname -r`

为了编译我使用了:

stbau@kernel-dev-vm:~/src/lkm_example$ sudo make
make -C /lib/modules/5.13.0-39-generic/build M=/home/stbau/src/lkm_example modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-39-generic'
  CC [M]  /home/stbau/src/lkm_example/lkm_example.o
  MODPOST /home/stbau/src/lkm_example/Module.symvers
  CC [M]  /home/stbau/src/lkm_example/lkm_example.mod.o
  LD [M]  /home/stbau/src/lkm_example/lkm_example.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-39-generic'

正在使用 insmod 执行:

stbau@kernel-dev-vm:~/src/lkm_example$ sudo insmod lkm_example.ko
insmod: ERROR: could not insert module lkm_example.ko: Invalid module format

dmesg 日志给出以下错误:

[   49.272618] lkm_example: module verification failed: signature and/or required key missing - tainting kernel
[   49.272630] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 0000000054f3f1c5, val ffffffffc0a0a000

我正在使用以下内核:

stbau@kernel-dev-vm:~/src/lkm_example$ uname -a
Linux kernel-dev-vm 5.13.0-39-generic #44-Ubuntu SMP Thu Mar 24 15:35:05 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

正如您在 dmesg 日志中看到的那样,我只收到一个错误,而不是我预期的消息。我不知道我做了什么 wrong/what 不见了。

我认为问题是模块没有签名。我尝试使用签名文件对其进行签名,但无法生成 private/public 密钥文件。

Re-Installing 内核头文件对我有用。

我使用了以下命令:

sudo apt update && sudo apt upgrade
sudo apt remove --purge linux-headers-*
sudo apt autoremove && sudo apt autoclean
sudo apt install linux-headers-generic