Marking a loadable kernel module as a tree

This question is about the Linux kernel 4.10.

Booting outside the LKM tree causes the kernel to print a warning:

module: loading out-of-tree module taints kernel.

This is confirmed by this check in module.c : if (!get_modinfo(info, "intree")) {

Reading get_modinfo means that "intree" is just a magic line inside the .ko file.

Running readelf on a random LKM found on my system shows this:

readelf -a imon.ko | grep intree 161: 00000000000006c0 9 OBJECT LOCAL DEFAULT 13 __UNIQUE_ID_intree1

When looking for intree in a simple user hello_world, LKM does not return results.

Is this really so?

How are some modules marked as in-tree? Is this done by adding a macro to the module (e.g. MODULE_LICENCE) or by creating a module in a specific way or something else?

+5
source share
1 answer

In short, the build system comes up with to add the line MODULE_INFO(intree, "Y"); to the file "modulename.mod.c" if and only if the module is being built intro.

There is an obvious way to trick the system by adding this line to one of your regular .c files, but I'm not sure why you want it.

Longer version ....

External modules are usually built with a command like this:

 $ make M=`pwd` modules 

or old syntax:

 $ make SUBDIRS=`pwd` modules 

The presence of a non-empty M or SUBDIRS forces the top-level "Makefile" of the kernel to set the variable KBUILD_EXTMOD . It will not be installed for normal kernel assembly.

For stage 2 of building the module (when the message "Building modules, stage 2" is displayed), make runs the makefile "scripts / Makefile.modpost". This works scripts/mod/modpost with various parameters when KBUILD_EXTMOD set. In particular, the -I option is used when KBUILD_EXTMOD set.

Looking at the source of modpost in "scripts / mod / modpost.c", the external_module variable has an initial value of 0, but the -I parameter sets it to 1. The add_intree_flag() function is called with the second is_intree parameter set to !external_module . The add_intree_flag() function writes MODULE_INFO(intree, "Y"); to the file "modulename.mod.c" if and only if the is_intree parameter is true.

Thus, the difference between intree modules and external modules is the presence of the macro MODULE_INFO(intree, "Y"); in the file "modulename.mod.c". This compiles to "modulename.mod.o" and is connected to a module of other object files to form the file "modulename.ko".

+4
source

Source: https://habr.com/ru/post/1265783/


All Articles