How to compile a linux kernel module for different linux kernel - linux

I am sort of new to kernel programming, but i have been struggling a ton with this issue for days now. I have a machine with linux kernel '5.10.0-kali7-amd64' and im using it for development of a linux kernel module for Ubutnu 16.04.4 '4.4.0-119-generic', but i can't figure out any way that i can compile it on my machine for that version and for it to actually work on the 4.4.0 kernel machine.
The closest i've got is this:
I downloaded source from https://launchpad.net/ubuntu/xenial/+package/linux-headers-4.4.0-119
and installed with dpkg
I then downloaded and installed the 4.4.0-119-generic from https://www.ubuntuupdates.org/package/core/xenial/main/updates/linux-image-4.4.0-119-generic
Both of them installed with no issue.
I compiled my module by using in my Makefile make -C /lib/modules/4.4.0-119-generic/build M=$(PWD) modules which also worked and compiled my hello world module.
However when uploaded to the 4.4.0 machine the insmod errored saying insmod: ERROR: could not insert module rootkitMy.ko: Invalid module format. The dmesg says: module: rootkit: Unknown rela relocation: 4 I then compiled my source code on the 4.4.0 machine and created a module with literally the exact same modinfo, but that one did work.
here are the modinfos for both:
filename: /rootkit.ko
version: 0.01
description: Rootkit hook
author: Bl4ckC4t
license: GPL
srcversion: 46604268C8D1B7FA5115CB4
depends:
vermagic: 4.4.0-119-generic SMP mod_unload modversions retpoline
filename: /rootkitMy.ko
version: 0.01
description: Rootkit hook
author: Bl4ckC4t
license: GPL
srcversion: 46604268C8D1B7FA5115CB4
depends:
vermagic: 4.4.0-119-generic SMP mod_unload modversions retpoline
rootkitMy.ko was compiled on the 5.10 machine and didn't work while the rootkit.ko was compiled on the 4.4.0 machine and did work properly when injected with insmod What can I do to compile a working module from my 5.10 machine?

I managed to resolve the issue. Unknown rela relocation: 4
is an insmod error you get due to a change in the way the kernel handles PLT, more specifically the R_X86_64_PC32 and R_X86_64_PLT32. With binutils >= 2.31, the linker has decided to use R_X86_64_PLT32 relocations, which aren't supported in the older kernel.
To fix this:
I downloaded an older version of binutils (2.26.1) from https://ftp.gnu.org/gnu/binutils/
extracted the folder from the archive
compiled the binutils to /usr/local/binutils-2.6 by running
./configure --prefix=/usr/local/binutils-2.6
make
sudo make install
exported the new binutils to my path and recompiled my module export PATH=/usr/local/binutils-2.6/bin:$PATH
And now it works!

Related

ERROR: could not insert module When load driver module on ubuntu 14.04

When i want to load driver module of Wo Mic on Ubuntu 14.04 i get this error in Terminal :
insmod: ERROR: could not insert module wo_snd_capture-x86_64.ko: Invalid parameters
I follow the Wo Mic instruction for load module like the official website of Wo Mic.
In Wo Mic Official websit :
Change client program to be executable
$ chmod a+x micclient-ubuntu-x86_64
Load driver module
$ sudo insmod wo_snd_capture-x86_64.ko
But when i enter this sudo insmod wo_snd_capture-x86_64.ko in Terminal i get above Error .
How can i solve this problem ?
This is because wo_snd_capture-x86_64.ko module was compiled for different kernel version than yours.
$ modinfo ./wo_snd_capture-x86_64.ko
filename: .../wo_snd_capture-x86_64.ko
license: GPL
srcversion: E535A4C547FF773D30C0B16
depends: snd-pcm,snd
vermagic: 4.4.0-62-generic SMP mod_unload modversions
Note that version in vermagic. You can check your kernel version with uname -a. Your version is different.
As you can see, it is GPL licenced, so just ask authors of this module for source code and you will be able to recompile this module by yourself every time you upgrade your kernel.

Learn kernel module, module.ko cause module is already loaded?

I am learning kernel module now, so I setup a Ubuntu 16.04 with kernel 4.4.0-28-generic in a virtual machine.
I installed this packages
# dpkg -l | grep linux
ii console-setup-linux 1.108ubuntu15 all Linux specific part of console-setup
ii libselinux1:amd64 2.4-3build2 amd64 SELinux runtime shared libraries
ii linux-base 4.0ubuntu1 all Linux image base package
ii linux-firmware 1.157.2 all Firmware for Linux kernel drivers
ii linux-generic 4.4.0.28.30 amd64 Complete Generic Linux kernel and headers
ii linux-headers-4.4.0-28 4.4.0-28.47 all Header files related to Linux kernel version 4.4.0
ii linux-headers-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii linux-headers-generic 4.4.0.28.30 amd64 Generic Linux kernel headers
ii linux-image-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-generic 4.4.0.28.30 amd64 Generic Linux kernel image
ii linux-libc-dev:amd64 4.4.0-28.47 amd64 Linux Kernel Headers for development
ii linux-sound-base 1.0.25+dfsg-0ubuntu5 all base package for ALSA and OSS sound systems
ii linux-source 4.4.0.28.30 all Linux kernel source with Ubuntu patches
ii linux-source-4.4.0 4.4.0-28.47 all Linux kernel source for version 4.4.0 with Ubuntu patches
ii util-linux 2.27.1-6ubuntu3.1 amd64 miscellaneous system utilities
I already decompresses the kernel source package /usr/src/linux-source-4.4.0.tar.bz2 to /home/test/WorkSpace/Kernel/linux-source-4.4.0.
My system uname is
# uname -a
Linux ubuntu-ldm 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
and I write a test module hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init (void) {
printk (KERN_ALERT "Hello, World\n");
return 0;
}
static void hello_exit (void) {
printk (KERN_ALERT "Goodbye, cruel world\n");
}
module_init (hello_init);
module_exit (hello_exit);
Makefile
obj-m += module.o
module-objs := hello.o
all:
make modules M=`pwd` -C /home/test/WorkSpace/Kernel/linux-source-4.4.0
clean:
make modules clean M=`pwd` -C /home/test/WorkSpace/Kernel/linux-source-4.4.0
But something confused me. I make my module and try to insmod, and I got an error
# sudo insmod module.ko
insmod: ERROR: could not insert module module.ko: Invalid module format
What cause this error?
Did I use wrong version of kernel source?
for osgx
There is no additional line in output of dmesg after I try to insmod. The modinfo shows this
# modinfo module.ko
filename: /home/test/WorkSpace/LDM/hello/module.ko
license: Dual BSD/GPL
depends:
vermagic: 4.4.13 SMP mod_unload
I try to change the source path in the Makefile, remake it, and I get the new .ko
The modinfo of the new .ko is
# modinfo module.ko
filename: /home/joshua/WorkSpace/LDM/hello/module.ko
license: Dual BSD/GPL
srcversion: 82C361DBCB1C9BB5CA1DB07
depends:
vermagic: 4.4.0-28-generic SMP mod_unload modversions
But the problem still there, and the dmesg log looks like this
[ 28.540701] module: module verification failed: signature and/or required key missing - tainting kernel
[ 28.540879] module: module is already loaded
It looks like there is a module named module already in the system, so I try to change the target name in the Makefile to hello.o, make the new target name as hello.ko, after that the module works.
But when I ran lsmod | grep module, there is no module named module?
One possibility is that the module is loaded statically.
Then it won't show under lsmod, but you can still see it with:
ls /sys/module
See also: https://unix.stackexchange.com/questions/364956/how-can-insmod-fail-with-kernel-module-is-already-loaded-even-is-lsmod-does-not

Invalid module format with matching vermagic value

I have a problem with one of my kernel modules for Linux 4 tegra on the Jetson tk1. I'm trying to integrate a Camera driver into the kernel
I'm trying to insert a module into the kernel and I've compiled everything on the target machine. Here's the modinfo of the kernel module.
filename: /home/ubuntu/mymodule.ko
license: GPL v2
author: John Doe
description: SoC Camera driver
alias: of:N*T*Cnvidia,mymodule*
alias: i2c:mymodule
depends:
vermagic: 3.10.40-svn469 SMP preempt mod_unload ARMv7 p2v8
parm: test_pattern:int
The uname -r command outputs this :
3.10.40-svn469
I still get this output when I am running sudo insmod mymodule.ko
insmod: ERROR: could not insert module mymodule.ko: Invalid module format
I've tried looking into the dmesg/syslog/kern log files but there's no information about why my module insertion is failing. I also looked around on other threads and it usually says to verify that the module was compiled using the correct headers. From my understanding, is the "vermagic" and the "uname -r" are similar I'm assuming that the versions are correct (I might be very wrong about that).
I'm running out of ideas and I'd like to know if I missed something.
Cheers.

Error when loading cross compiled kernel module

I have cross compiled a simple helloworld kernel module, the host is a x86 machine and the target an ARM board. When I do modprobe to install the module in the target i get this message:
FATAL: Could not load /lib/modules/3.14.0-xilinx-13567-g906a2c9-dirty/modules.dep: No such file or directory
I have make sure that the module is compiled with the same version as the target.
uname -a : 3.14.0-xilinx-13567-g906a2c9-dirty
modinfo: vermagic: 3.14.0-xilinx-13567-g906a2c9-dirty SMP preempt mod_unload modversions ARMv7 p2v8
What can be the problem? What does that error means?
Apparently, you are missing the file specifying module dependencies (generated at build time and installed with make module_install).
The simplest solution is, if your mdule does not have external dependencies, insert it with insmod rather than with modprobe.
Try to run:
depmod -a
on the ARM board.
it should solve your problem.
I would suggest the following steps.
Do insmod $module-name
Check the dmesg commands output. If you see the following message
version magic '3.14.0-xilinx-13567-g906a2c9-dirty xxxxxxxx' should be
'3.14.0-xilinx-13567-g906a2c9-dirty xxxxxxxxxx'
then the problem is because of the changes made to the kernel.
Commit the changes to the git repository and re-build the kernel.
Create a new kernel image and then boot the target with the updated kernel.

Cross compiling a kernel module: how to set configs right

I'm trying to cross-compile an external module for the Beaglebone (Linux, ARM).To avoid kernel version issues I grabbed a kernel tarball and cross-compiled for ARM with the CodeSourcery toolchain on the host machine (x86). Then I cross-compiled an external hello-world module with the exact same toolchain against the exact same kernel sources. I used this simple makefile:
obj-m += hello-1.o
all:
make -C /home/***/****/linux-3.2.0-beaglebone-20120411.00
M=$(PWD) modules
clean:
make -C /home/***/****/linux-3.2.0-beaglebone-20120411.00 M=$(PWD) clean
When I copy the ko-file over to target and try to insmod it I get: "insmod: error inserting 'hello-1.ko': -1 Invalid module format" which (from what I learned in this group and elsewhere) usually stems from conflicting kernel versions, and indeed uname -a on the target gives:
Linux beaglebone 3.2.18 #1 Wed May 30 14:21:54 CEST 2012 armv7l GNU/Linux
while modinfo hello-1.ko gives:
srcversion: 140276773A3090F6F33891F
depends:
vermagic: 3.2.0+ mod_unload modversions ARMv5 p2v8
So version 3.2.18 vs. 3.2.0+ (why the +?) and armv7l vs. ARMv5!
Does anyone know why I get different versions albeit I compiled against the same kernel-sources (maybe some configs)?
Any suggestions are appreciated!
best,
Chris
answer is, you should run 'the kernel you compiled on beagle board.
'+' means that you modified your source tree.
And finally make sure that you're using correct defconfig. 'make beagle_defconfig' should work.

Resources