内核模块编程中的insmod错误

3
我是一个能够翻译文本的助手。

我刚开始学习模块化编程。

以上是我的两个文件:

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

这是我的终端输出,显示insmod命令中出现错误,请帮忙。

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
通常只有root用户拥有插入/移除内核模块的权限。要么使用 su 切换到 root 用户,要么使用 sudo(如果适用)以 root 用户身份运行命令。 - lsowen
@lsowen 我尝试了"su"和"su -"。但是在提供密码后,我收到了"su:身份验证失败"的消息。有其他方法可以继续吗? - anubhav
这条消息意味着您输入了错误的密码(或者不是允许成为root的用户组的成员)。在su中需要使用的密码是root密码,而不是您的用户密码。 - lsowen
1
@lsowen Ubuntu网站上说,用户可以在需要以root身份执行的任何命令前加上sudo命令。我的insmod命令已经成功执行,并且我通过dmesg得到了消息。谢谢。 - anubhav
1
你没有看到 printk(KERN_ALERT "TEST: Good Bye"); 是因为你还没有执行 rmmod hello,对吧? - Milind Dumbare
3个回答

3
如果您启用了安全启动,则较新的内核将不允许插入任意内核模块。因此,您可以在BIOS中禁用安全启动,或者您需要签署要安装的内核模块。 安全签署内核模块的步骤:
1.创建一个可以在固件中导入的X509证书。
2.注册刚刚创建的公钥。
3.签署要安装的模块。
4.安装模块。
步骤2和4需要root权限。详细过程在Ubuntu博客中进行了描述。

1
只有root用户才能加载或卸载模块,当您执行“insmod hello”时,您会在“hello_init()”中看到打印输出,当您执行“rmmod hello”时,您会在“hello_exit()”中看到打印输出。请注意保留HTML标签。

0
执行 cat /proc/sys/kernel/modules_disabled 命令,如果你看到结果是 1,那么执行 echo 'kernel.modules_disabled=1' >> /etc/sysctl.d/99-custom.conf 命令,然后重新启动并再次尝试。 ;) BR nu11secur1ty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接