Compile Linux kernel modules with KASAN - linux

I'm trying to use the Linux kernel address sanitizer (KASAN) with a loadable module, but so far haven't been able to make it work. I compiled a kernel with KASAN and was able to verify that it properly detects errors caused by the statically linked body of the kernel, but when I compile a loadable module with that kernel, the module is not configured for KASAN (e.g. KASAN CFLAGS switches are missing). I noticed that the lib/include/config/auto.conf file in the /lib/modules/xxx directory is different from the one in the kernel build directory, so I tried copying the auto.conf file from the kernel directory to lib/modules/xxx. At this point the individual files appear to compile with the same KASAN flags as the kernel, but the module build fails during the link step with missing symbols such as __asan_handle_no_return.
Is KASAN known to work with modules? If so, is any additional configuration needed for a module to use KASAN?

Answering my own question: KASAN actually works great for modules. Somehow I managed to botch the installation of the KASAN-ified kernel so that the module was compiling with old info. After reinstallation, the module compiled cleanly with KASAN and errors in the module are properly detected. Sorry for the false alarm here.

Related

Linux Kernel : Building Out-of-tree module for kernel , but no linux-headers in apt-cache search

I am working on linux kernel development, and was trying to make my own modules for testing purposes. However, for the latest kernel source codes, the linux-headers don't exist. It only exists for 4.9.0-7 .
I googled and found 1 method where we make the module against the kernel source tree that we want (eg. 4.18)
make -C /home/prasad/linux-4.18/ M=$(PWD)
which does generate the .ko for my module. However, when I load up my kernel and insmod it , it says
insmod: ERROR: could not insert module test.ko: Unknown symbol in module
So how exactly do I generate a .ko file from my host machine that can be inserted for the 4.18 kernel ?
PS: My doubt is not a duplicate as I cannot "install" the 4.18 kernel in my vm, Im trying to avoid doing that. My question is more specific to generating an insertable .ko module, and not finding any other way to insert it in 4.18.
You cant use a module built for 4.18 in a 4.9 kernel.
If the headers for the kernel version you are building for are not available via apt/yum/etc, you will need to download the source manually from: https://www.kernel.org/
In the makefile for your module, you will specify the path to the kernel source code that you have downloaded and extracted.
That should allow you to build the module for the desired kernel version.

Compile Kernel module on Chromebook

I am running an ARMv7 Chromebook with crouton. I would like to get CIFS shares mounted, but it appears that CIFS is not in the kernel. So I downloaded the same kernel version source as I am on, compiled the cifs.ko module, and attempted to load it. But I received this error:
# insmod cifs.ko
insmod: ERROR: could not insert module cifs.ko: Operation not permitted
The module is compiled as an ARM module, I checked with file:
# file cifs.kocifs.ko: ELF 32-bit LSB relocatable, ARM, version 1, BuildID[sha1]=e14d1772583fae478e2b113b57ce81c214e511af, not stripped
What gives?
Chromium OS does not allow adding kernel modules by default. Use this script to disable module locking. https://github.com/divx118/crouton-packages/blob/master/README.md
More information on modifying the Chromium OS kernel can be found here:
https://github.com/dnschneid/crouton/wiki/Build-kernel-headers-and-install-Virtualbox-(x86) Generally the entire crouton repository / wiki is a lot of help.

How to build kernel module without full kernel source tree?

I would like to build kernel module without kernel source tree.
Instead that, I specified the kernel header directory only.
This reference link tell me it should be workable :
build kernel module w/o source source tree
But some other reference links tell me should build module with full kernel source tree!
My question is :
1. Shall I build kernel module link with full kernel source tree ?
2. Is it necessary to do a full build under kernel source tree before build my single module ?
Not it's not necessary. You need a header files that contains function and types declarations. You also kernel tree with Makefiles and Kconfig but without sources. This kernel tree is needed by kbuild - kernel building system.
Absolutely not. You can build a single module without building whole kernel regardless of it's out-of-tree or in-tree module.
No to both questions. You're talking about out-of-tree (or "external") modules.
Here is a tutorial of Building External Modules.
And here is a simple sample of a Makefile.
obj-m += your-module.o
KDIR=/lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules

Cross compiled ARM Kernel instead of ARMHF

However I Cross Compiled ARM Kernel instead of ARMHF(for my Cubietruck). I followed this tutorial:
https://romanrm.net/a10/cross-compile-kernel
How can I determine for which architecture I´m cross-compiling?
i got a new error that /linux/utsrealease.h is not found
from above comment as you mentioned.. from that its clear that kernel module which your building must match with running kernel version . As kernel modules loading mechanism doesn't allow loading modules that were not compiled against the running kernel, due to mismatch error is coming.
The macro UTS_RELEASE is required by your driver in order to rebuild
kernel modules from source.
retrieving the version string constant,
older versions require you to include<linux/version.h>,
others <linux/utsrelease.h>,
and newer ones <generated/utsrelease.h>
So my suggestion you do workaround by doing
you can find utsrelease.h in kernel source code make sure your running kernel must match with your source-code
copy linux-x.x.x/include/generated/utsrelease.h to installed header i.e ../include/linux/utsrelease.h
Im not sure give a try .
If above doesnot work pls update your question with
1)which kernel sourcode version you have
2)Whats the kernel version running on target
When you compile your kernel, mention the architecture you are compiling for in:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- EXTRAVERSION=-custom1 uImage
For eg, here ARCH=arm, so you are compiling for ARM, if it's x86, then you'll replace it with x86. Check what architecture your target board is on.
EDIT: gnueabihf is for armhf.

missing modversions.h on linux

I have configured and built linux kernel 2.6.27 successfully, with module supported enabled. But when I am trying to build another program (kernel module) that needs include/linux/modversions.h, it cannot find the file.
my question is: besides enabling the module support in linux kernel config, is there anything else I need to do to generate/get the modversions.h?
Thanks.
Run a find /path/to/kernel -name modversions.h, you'll probably find one in a config dir and another on a linux dir, if so it was compiled with your kernel.
If that module was compiled then it can be installed with make modules_install, though that will install all the compiled modules, not their headers.
To tell gcc to search for the kernel source when compiling use the -I option -I/path/to/kernel.
You can also use the INCLUDE_DIRS var to tell gcc to include that dir, export INCLUDE_DIRS=$INCLUDE_DIRS:/path/to/kernel.

Resources