Search code examples
clinuxlinux-kernelkernelkernel-module

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


I am currently trying to develop a simple linux kernel module. It should just log something, its 1:1 copied from the internet.

I have the following files: 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);

Makefile:

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

I also did the following:

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

For compilation i used:

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'

Executing with 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

The dmesg log gives the following error:

[   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

I am using the following kernel:

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

As you can see in the dmesg log, i only get an error and not the messages i expected. I have no idea what i did wrong/what is missing.

I think the problem is that the module is not signed. I tried signing it using the sign-file but i was not able to generate a private/public key file.


Solution

  • Re-Installing the kernel headers worked for me.

    I used the following commands:

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

    If the error persists after following the steps, look at the kernel version that you're using (uname -r) and uninstall + reinstall those headers specifically. As a quick sanity check, make sure that the kernel module is also on the same kernel version (check the verinfo field after running modinfo module_name.ko).

    For example, if your kernel version is 5.19.0-41-generic, remove the headers (as above) and install them again:

    sudo apt install linux-headers-5.19.0-41-generic