[Ubuntu] insmod和modprobe加-f参数导致Invalid module format错误

insmod和modprobe加-f参数导致Invalid module format错误

这几天做Linux驱动相关实验遇到的一个奇怪的现象。

在Ubuntu系统下使用insmodmodprobe可以正常加载的模块,在加上-f选项后就会出现Invalid module format错误。并且这个问题似乎是Ubuntu特有的,因为我在Arch Linux上无论是否加-f选项都能正常载入模块。

探究

按照国外论坛上的说法,关闭了安全启动和AppArmor服务依旧没有解决,猜测可能和Ubuntu本身模块加载的策略有关。

由于不带-f选项的insmodmodprobe是可以正常加载模块的,因此主要的问题肯定出在这-f选项上。而man modprobe中对于-f选项的描述如下:

--force-vermagic
Every module contains a small string containing important information, such as the kernel and compiler versions. If a module fails to load and the kernel complains that the "version magic" doesn't match, you can use this option to remove it. Naturally, this check is there for your protection, so using this option is dangerous unless you kNow what you're doing.

--force-modversion
When modules are compiled with CONfig_MODVERSIONS set, a section detailing the versions of every interfaced used by (or supplied by) the module is created. If a module fails to load and the kernel complains that the module disagrees about a version of some interface, you can use "--force-modversion" to remove the version information altogether. Naturally, this check is there for your protection, so using this option is dangerous unless you kNow what you're doing.

-f, --force
Try to strip any versioning information from the module which might otherwise stop it from loading: this is the same as using both --force-vermagic and --force-modversion. Naturally, these checks are there for your protection, so using this option is dangerous unless you kNow what you are doing.

可以看出-f, --force选项其实是--force-vermagic--force-modversion选项的结合体,而从下面的翻译中也可以看出,他们的作用并非所谓的“强制加载”,而是删除模块中的版本信息

而在Ubuntu系统下将原来的-f替换成二者中的任意一个同样会导致Invalid module format错误,也证明了是缺少对应的版本信息导致的。

--force-vermagic
每个模块都包含一个小字符串,其中包含重要信息,例如内核和编译器版本。如果模块加载失败并且内核抱怨"version magic"不匹配,您可以使用此选项将其删除。当然,此检查是为了保护您,因此使用此选项是危险的,除非您知道自己在做什么。

--force-modversion
当使用 CONfig_MODVERSIONS 设置编译模块时,将创建一个详细说明模块使用(或提供)的每个接口的版本的部分。如果模块加载失败并且内核抱怨该模块不同意某些接口的版本,则可以使用“--force-modversion”完全删除版本信息。当然,此检查是为了保护您,因此使用此选项是危险的,除非您知道自己在做什么。

结论

综上所述,个人推测是因为Ubuntu在加载模块时会强制对模块的版本信息进行检查,由于insmodmodprobe中的-f移除了模块的版本信息,导致Ubuntu读取不到对应内容,从而出现Invalid module format错误。而Arch Linux则会根据-f选项相应地跳过版本检查,从而能够正常加载。

二编

在关于内核编译的文章中看到了如下描述:

CONfig_MODULE_FORCE_LOAD允许使用"modprobe --force"在不校验版本信息的情况下强制加载模块,这绝对是个坏主意!建议关闭.

也许是Ubuntu内核编译时关闭了该选项导致的。


本文发布于2024年5月15日

最后修改于2024年5月16日

相关文章

insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...