3
votes

I am just starting with modular programming.

Above are my two files:

hello.c

#include <linux/init.h>
#include <linux/module.h>

static int hello_init(void)
{
    printk(KERN_ALERT "TEST: Hello world\n");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "TEST: Good Bye");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile

obj-m += hello.o

KDIR = /usr/src/linux-headers-3.13.0-46-generic

all:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
    rm -rf *.o *.ko *.mod.* *.symvers *.order

And here's my terminal output showing error in insmod command, kindly help.

anubhav@anubhav-Inspiron-3421:~/Desktop/os$ make
make -C /usr/src/linux-headers-3.13.0-46-generic  SUBDIRS=/home/anubhav/Desktop/os modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-46-generic'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-46-generic'
anubhav@anubhav-Inspiron-3421:~/Desktop/os$ insmod hello.ko
insmod: ERROR: could not insert module hello.ko: Operation not permitted
3
Only the root user typically has permissions to insert/remove kernel modules. Either su to root, or use sudo (if applicable) to run the command as root.lsowen
@lsowen I tried "su" and "su -". But after providing password, i get the msg "su: Authentication failure". Any other way to move aheadanubhav
This message means you put in the wrong password (or are not a member of a user group allowed to become root). The password you need to use at su is the root password, not your user password.lsowen
@lsowen ubuntu website says that user can prepend any command that needs to be executed as root with sudo command. My insmod command worked and i got the message using dmesg. Thanks.anubhav
And you dont see printk(KERN_ALERT "TEST: Good Bye"); because you have not done rmmod hello have you?Milind Dumbare

3 Answers

3
votes

If you have secure boot enabled, the newer kernels won't allow inserting arbitrary kernel modules. So, either you can disable secure boot in your BIOS or you need to sign the kernel modules you want to install.

Steps to securely sign your kernel modules:

  1. Create a X509 certificate that can be imported in firmware
  2. Enrolling the public key just created
  3. Sign the module you want to install
  4. Install the module

You need to be root to do steps 2 & 4. The detailed process is described in a nice Ubuntu blog.

1
votes

As isowen mentioned only root can load or unload the module.

You see the print in hello_init() when you do insmod hello and you see the print in hello_exit() when you do rmmod hello.

0
votes

execute cat /proc/sys/kernel/modules_disabled and if you see the result 1 then execute echo 'kernel.modules_disabled=1' >> /etc/sysctl.d/99-custom.conf then reboot and try again. ;) BR nu11secur1ty