LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Distributions (https://www.linuxquestions.org/questions/linux-distributions-5/)
-   -   Compile Linux Kernel Module - Cannot build simple kernel module (https://www.linuxquestions.org/questions/linux-distributions-5/compile-linux-kernel-module-cannot-build-simple-kernel-module-4175713092/)

z3r0_1993 06-06-2022 11:23 PM

Compile Linux Kernel Module - Cannot build simple kernel module
 
I'm trying to compile a kernel module driver but I get the following errors: (the operation generate some files, but not a ".ko" file)



Code:

make -C /lib/modules/5.16.0-kali7-amd64/build M=/home/z3r0/Scrivania/kernel_modules/start_stop modules
    make[1]: ingresso nella directory «/usr/src/linux-headers-5.16.0-kali7-amd64»
    /bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
   
      ERROR: Kernel configuration is invalid.
            include/generated/autoconf.h or include/config/auto.conf are missing.
            Run 'make oldconfig && make prepare' on kernel src to fix it.
   
    make[1]: *** [/usr/src/linux-headers-5.16.0-kali7-common/Makefile:751: include/config/auto.conf] Errore 1
    make[1]: uscita dalla directory «/usr/src/linux-headers-5.16.0-kali7-amd64»
    make: *** [Makefile:3: all] Errore 2

When I try to execute "make oldconfig && make prepare" (in the directory /usr/src/linux-headers-5.16.0-kali7-amd64) I get the following error:

Code:

/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
      GEN    Makefile
    /bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
    /usr/src/linux-headers-5.16.0-kali7-common/scripts/Makefile.build:44: /usr/src/linux-headers-5.16.0-kali7-common/scripts/basic/Makefile: File o directory non esistente
    make[1]: ***  Nessuna regola per generare l'obiettivo «/usr/src/linux-headers-5.16.0-kali7-common/scripts/basic/Makefile».  Arresto.
    make: *** [/usr/src/linux-headers-5.16.0-kali7-common/Makefile:566: scripts_basic] Errore 2

If I try in my computer and not in the Virtual Machine I get the following error (but it generate some files, but not the .ko file):
Code:

make -C /lib/modules/5.16.0-kali7-amd64/build M=/home/z3r0/Scrivania/All/Programming/C++/Exercises_Linux/kernel_modules/hello_world modules
make[1]: ingresso nella directory «/usr/src/linux-headers-5.16.0-kali7-amd64»
/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: gcc-11 (Debian 11.2.0-19) 11.2.0
  You are using:          gcc-11 (Debian 11.3.0-3) 11.3.0
/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
  CC [M]  /home/z3r0/Scrivania/All/Programming/C++/Exercises_Linux/kernel_modules/hello_world/hello_world.o
/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
  MODPOST /home/z3r0/Scrivania/All/Programming/C++/Exercises_Linux/kernel_modules/hello_world/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /home/z3r0/Scrivania/All/Programming/C++/Exercises_Linux/kernel_modules/hello_world/hello_world.o
make[2]: *** [/usr/src/linux-headers-5.16.0-kali7-common/scripts/Makefile.modpost:134: /home/z3r0/Scrivania/All/Programming/C++/Exercises_Linux/kernel_modules/hello_world/Module.symvers] Errore 1
make[2]: *** Eliminazione del file «/home/z3r0/Scrivania/All/Programming/C++/Exercises_Linux/kernel_modules/hello_world/Module.symvers»
make[1]: *** [/usr/src/linux-headers-5.16.0-kali7-common/Makefile:1785: modules] Errore 2
make[1]: uscita dalla directory «/usr/src/linux-headers-5.16.0-kali7-amd64»
make: *** [Makefile:3: all] Errore 2



The file is really simple (hello_world.c):

Code:

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
   
    static int __init hello_start(void)
    {
            printk(KERN_INFO "Loading hello module...\n");
            printk(KERN_INFO "Hello world\n");
            return 0;
    }
   
    static void __exit hello_end(void)
    {
            printk(KERN_INFO "Goodbye Mr.\n");
    }
   
    module_init(hello_start);
    module_exit(hello_end);

makefile:

Code:

    obj-m = foo.o
    KVERSION = $(shell uname -r)
    all:
            make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
    clean:
            make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Can someone explain where is the error? Thank you

pan64 06-07-2022 12:17 AM

probably:
Code:

      ERROR: Kernel configuration is invalid.
            include/generated/autoconf.h or include/config/auto.conf are missing.
            Run 'make oldconfig && make prepare' on kernel src to fix it.


z3r0_1993 06-07-2022 02:01 AM

Already Tryed
 
Quote:

Originally Posted by pan64 (Post 6359203)
probably:
Code:

      ERROR: Kernel configuration is invalid.
            include/generated/autoconf.h or include/config/auto.conf are missing.
            Run 'make oldconfig && make prepare' on kernel src to fix it.




Hello, Thank you for ur answer
but as I said in my request I already tryed these commands and I get the error I wrote on the question above :/

Code:

When I try to execute "make oldconfig && make prepare" (in the directory /usr/src/linux-headers-5.16.0-kali7-amd64) I get the following error:

/bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
      GEN    Makefile
    /bin/sh: 1: /usr/src/linux-headers-5.16.0-kali7-common/scripts/pahole-flags.sh: not found
    /usr/src/linux-headers-5.16.0-kali7-common/scripts/Makefile.build:44: /usr/src/linux-headers-5.16.0-kali7-common/scripts/basic/Makefile: File o directory non esistente
    make[1]: ***  Nessuna regola per generare l'obiettivo «/usr/src/linux-headers-5.16.0-kali7-common/scripts/basic/Makefile».  Arresto.
    make: *** [/usr/src/linux-headers-5.16.0-kali7-common/Makefile:566: scripts_basic] Errore 2


pan64 06-07-2022 02:24 AM

So make oldconfig failed. I guess your kernel source tree is either incomplete or corrupted.
How did you create it?

z3r0_1993 06-07-2022 02:33 AM

Quote:

Originally Posted by pan64 (Post 6359217)
So make oldconfig failed. I guess your kernel source tree is either incomplete or corrupted.
How did you create it?

Mh sorry created what? xD

pan64 06-07-2022 02:46 AM

So you use kali, which is recommended only for advanced users
you want to build a kernel module, which is again not really suggested for beginners.
and finally I guess you have a guide or a set of instructions to explain how do you need to prepare your environment (if you want to build kernel modules).
I guess you missed a step or just failed something during that preparation.
What is in /usr/src/linux-headers-5.16.0-kali7-common ? And how was this directory created?

z3r0_1993 06-07-2022 03:02 AM

Quote:

Originally Posted by pan64 (Post 6359222)
So you use kali, which is recommended only for advanced users
you want to build a kernel module, which is again not really suggested for beginners.
and finally I guess you have a guide or a set of instructions to explain how do you need to prepare your environment (if you want to build kernel modules).
I guess you missed a step or just failed something during that preparation.
What is in /usr/src/linux-headers-5.16.0-kali7-common ? And how was this directory created?

Mh in the guide I was following I didn't see any instructions to set up the environment. I will search for it, thanks.
I though the environment was already set xD

Anyway but is normal that the commands "sudo make oldconfig && sudo make prepare" don't work? I though it wasn't,
Later I'll try on my Ubuntu machine to see if I get the same error.

I think everyone can do anything if they apply intensively, so despite being raccomended for advaced users I'll learn it (:
Obviously I'm not good as you are, but I will learn :D
I want to learn to hook syscalls and I want to analyze (disassembly and debugging) the program that hook the syscalls.


"linux-headers-5.16.0-kali7-common" I think it's about linux headers, it contains some tools and some scripts and also headers for different kind of processor.
It was already installed, I didn't need to get the linux-header package or upgrade it ^^

pan64 06-07-2022 04:15 AM

Quote:

Originally Posted by z3r0_1993 (Post 6359226)
I think everyone can do anything if they apply intensively, so despite being raccomended for advaced users I'll learn it (:

I don't think so. As it was mentioned several times: learn to walk first, then run.
Quote:

Originally Posted by z3r0_1993 (Post 6359226)
Obviously I'm not good as you are, but I will learn :D

I am not good at everything but what I have learned already.

Quote:

Originally Posted by z3r0_1993 (Post 6359226)
I want to learn to hook syscalls and I want to analyze (disassembly and debugging) the program that hook the syscalls.

That is great, but first you need to be able to compile simple programs and debug them, not the kernel itself (or kernel modules).

uteck 06-07-2022 09:07 AM

Kali is heavily customized and not intended for doing development work, you will have better results and learn more/faster using a normal disto.
Using a distro specialized at penetration testing to learn disassembly and debugging is like using a PlayStation to learn Excel.


All times are GMT -5. The time now is 03:48 PM.