内核模块编程中的insmod错误
insmod error in kernel module programming
我刚刚开始模块化编程。
以上是我的两个文件:
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);
生成文件
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
正如 isowen 提到的,只有 root 可以加载或卸载模块。
当您执行 insmod hello
时,您会在 hello_init()
中看到印刷品;当您执行 rmmod hello
时,您会在 hello_exit()
中看到印刷品。
如果您启用了安全启动,较新的内核将不允许插入任意内核模块。因此,您可以在 BIOS 中禁用安全启动,或者您需要签署要安装的内核模块。
安全签署内核模块的步骤:
- 创建可以在固件中导入的X509证书
- 正在注册刚刚创建的 public 密钥
- 签署您要安装的模块
- 安装模块
您需要成为 root 用户才能执行第 2 步和第 4 步。详细过程在 Ubuntu blog.
中有详细描述
执行cat /proc/sys/kernel/modules_disabled
,如果你看到结果
1
然后执行 echo 'kernel.modules_disabled=1' >> /etc/sysctl.d/99-custom.conf
然后重新启动并重试。 ;) BR nu11secur1ty
我刚刚开始模块化编程。
以上是我的两个文件:
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);
生成文件
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
正如 isowen 提到的,只有 root 可以加载或卸载模块。
当您执行 insmod hello
时,您会在 hello_init()
中看到印刷品;当您执行 rmmod hello
时,您会在 hello_exit()
中看到印刷品。
如果您启用了安全启动,较新的内核将不允许插入任意内核模块。因此,您可以在 BIOS 中禁用安全启动,或者您需要签署要安装的内核模块。
安全签署内核模块的步骤:
- 创建可以在固件中导入的X509证书
- 正在注册刚刚创建的 public 密钥
- 签署您要安装的模块
- 安装模块
您需要成为 root 用户才能执行第 2 步和第 4 步。详细过程在 Ubuntu blog.
中有详细描述执行cat /proc/sys/kernel/modules_disabled
,如果你看到结果
1
然后执行 echo 'kernel.modules_disabled=1' >> /etc/sysctl.d/99-custom.conf
然后重新启动并重试。 ;) BR nu11secur1ty