# 可加载内核模块

可加载内核模块（LKM）是一种目标文件，通常是 ELF 二进制文件，其代码可以被链接到正在运行的操作系统内核中，以便在运行时扩展内核功能，并在不再需要时卸载。<sup>[1](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)</sup><sup> • </sup><sup>[2](https://linux-kernel-labs.github.io/refs/pull/222/merge/labs/kernel_modules.html)</sup> 典型用途包括新硬件的设备驱动程序、文件系统支持和网络协议。模块让发行版可以只发布一个支持广泛硬件的内核，而每台机器只加载自己需要的驱动，也让开发者无需重启即可测试内核改动。<sup>[3](https://man.freebsd.org/cgi/man.cgi?apropos=0&manpath=FreeBSD+13.1-RELEASE&query=kld&sektion=4)</sup><sup> • </sup><sup>[4](https://www.kernel.org/doc/local/hotplug-history.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 本质 | 一种 ELF 二进制文件，加载后扩展正在运行的内核；典型用途是设备驱动和文件系统<sup>[1](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)</sup> |
| Linux 历史 | 可加载内核模块至少自 Linux 1.2（1995 年）起就已存在；modversions 支持出现在 1.1.85 版本（1995 年 1 月）<sup>[5](https://tldp.org/HOWTO/pdf/Module-HOWTO.pdf)</sup><sup> • </sup><sup>[6](https://lwn.net/Articles/986892/)</sup> |
| FreeBSD 历史 | 仿照 SunOS 4.1.3 的 LKM 系统随 FreeBSD 2.0 引入；Doug Rabson 在 FreeBSD 3.0 中以 KLD 动态内核链接器和 newbus 将其取代<sup>[7](https://people.freebsd.org/~brooks/pubs/eurobsdcon2004/dynif.pdf)</sup> |
| 加载阶段（Linux） | 权限检查、复制到内核空间、vermagic 检查、内存分配、符号解析与重定位，然后调用模块的初始化函数<sup>[1](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)</sup> |
| 安全控制 | Linux 的模块签名设施在安装时对模块签名，并在加载时验证签名；启用 CONFIG_MODULE_SIG_FORCE 时，未签名或签名无效的模块会被拒绝<sup>[8](https://kernel.org/doc/html/v5.19/admin-guide/module-signing.html)</sup> |
| 许可影响 | 标记为“Proprietary”的模块会以内核污染标志“P”标记内核，并且不能链接由 EXPORT_SYMBOL_GPL() 导出的符号<sup>[9](https://docs.kernel.org/process/license-rules.html)</sup> |
| 用户工具 | Linux：modprobe、insmod、rmmod、lsmod；FreeBSD：kldload、kldunload、kldstat<sup>[1](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)</sup><sup> • </sup><sup>[10](https://docs.freebsd.org/en/books/handbook/kernelconfig/)</sup> |

## 目的与权衡

模块的主要动机是减少内核的内存占用。通用配置的内核（例如 Linux 发行版所发布的内核）否则需要包含它可能遇到的所有硬件的驱动，而大多数系统只使用其中一小部分。内核代码始终驻留在物理内存中，因此编译进内核但未使用的驱动会永久占用本可供应用程序使用的内存。<sup>[4](https://www.kernel.org/doc/local/hotplug-history.html)</sup><sup> • </sup><sup>[10](https://docs.freebsd.org/en/books/handbook/kernelconfig/)</sup>

模块也是 Linux 中第一个真正实用的热插拔机制，因为它们允许设备驱动在内核启动后加载，并在关机前卸载。如果某个模块找不到其驱动所支持的任何设备，它可以拒绝加载，因此尝试加载所有模块是一种探测可用硬件的简单方法。这种方法有局限：一个驱动实例可以管理多个设备，因此重新插入模块以查找新接入的设备会使第一个实例下线。<sup>[4](https://www.kernel.org/doc/local/hotplug-history.html)</sup>

权衡在于，模块与内核共享地址空间，与内核核心之间没有任何隔离。模块中的故障可能破坏整个内核的完整性，而包含漏洞的模块会直接威胁操作系统，因为模块运行在内核空间时没有任何保护机制起作用。<sup>[11](https://www.sciencedirect.com/science/article/abs/pii/S0167404811001143)</sup><sup> • </sup><sup>[12](https://pure.psu.edu/en/publications/a-policy-centric-approach-to-protecting-os-kernel-from-vulnerable/)</sup> 因此，一些安全环境完全禁止加载和卸载模块，并要求所有需要的功能都静态编译进内核；例如 FreeBSD 的安全模式禁止加载或卸载内核模块。<sup>[10](https://docs.freebsd.org/en/books/handbook/kernelconfig/)</sup><sup> • </sup><sup>[13](https://docs.freebsd.org/en/books/handbook/book/index.html)</sup>

## Linux 中的加载过程

加载由 init_module 系统调用发起，通常由 insmod 或 modprobe 调用。该过程分六个阶段：检查用户是否有权加载模块，将模块二进制文件从用户空间复制到临时的内核空间缓冲区，检查模块与内核的 vermagic 字符串是否匹配，分配实际的内核内存，执行符号解析和重定位，最后调用模块的初始化函数（module_init）。<sup>[1](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)</sup> 模块只有在其内核版本和配置与正在运行的内核匹配时才能加载；vermagic 检查强制执行这一点。<sup>[1](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)</sup>

卸载通过 delete_module 系统调用进行。如果其他已加载模块依赖于该模块定义的符号，调用会失败。否则，如果模块的[引用计数](https://www.edgepedia.org/zh/reference-counting)为零，它会立即被卸载；如果计数非零，调用者要么收到错误，要么休眠直到计数归零，要么使用 O_TRUNC 标志强制卸载。有初始化函数但没有退出函数的模块默认无法移除。强制移除会污染内核。<sup>[14](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.15.pdf)</sup>

模块作者使用 module_init() 和 module_exit() 宏，使同一份源代码既可以构建为模块，也可以编译进内核；编译进内核时，module_init() 变为在启动时运行的 initcall。标记为 __init 的函数和标记为 __initdata 的数据被放在一个特殊段中，内核在初始化后释放该段，模块同样在其初始化例程完成后丢弃这块内存。<sup>[15](https://docs.kernel.org/kernel-hacking/hacking.html)</sup> 通过 try_module_get() 和 module_put() 进行的引用计数，可防止模块在其代码执行期间被移除。<sup>[15](https://docs.kernel.org/kernel-hacking/hacking.html)</sup>

## FreeBSD 中的加载过程

FreeBSD 的模块系统是 kld（动态内核链接器）设施，它在 FreeBSD 3.0 中取代了较旧的 LKM 接口。与其前身一样，它允许管理员在运行中的系统上动态添加和移除功能，并支持若干预定义模块类型，以及一种由模块自行处理加载和卸载的通用类型。系统为大多数文件系统以及 NFS 客户端和服务器提供了可加载版本。<sup>[3](https://man.freebsd.org/cgi/man.cgi?apropos=0&manpath=FreeBSD+13.1-RELEASE&query=kld&sektion=4)</sup> KLD 接口基于 ELF 二进制文件的动态链接，与旧的 LKM 系统不同，内核无需用户程序帮助即可加载文件；引导加载程序也可以预加载模块，因此在根磁盘可用之前所需的设备可以动态加载，而不必静态链接。<sup>[7](https://people.freebsd.org/~brooks/pubs/eurobsdcon2004/dynif.pdf)</sup><sup> • </sup><sup>[16](https://people.freebsd.org/~dfr/devices.html)</sup>

内核模块位于 /boot/kernel 中，通过 kldload(8) 加载到正在运行的内核中；大多数内核驱动都有一个可加载模块和一个手册页。要在启动时自动加载驱动，可在 /boot/loader.conf 中放置一行，例如 if_ath_load="YES"。<sup>[10](https://docs.freebsd.org/en/books/handbook/kernelconfig/)</sup> 模块的初始化和关闭分派由 SYSINIT 框架处理，该框架使用两个链接器集对函数排序，并在系统启动和模块加载时、以及关闭和模块卸载时分派它们。<sup>[17](https://docs.freebsd.org/en/books/arch-handbook/sysinit/)</sup>

## 安全控制

由于将代码加载到内核是一种具有全内核影响的特权操作，内核对此提供了若干控制。

**签名验证。** Linux 的模块签名设施在安装期间对模块进行密码学签名，并在加载时检查签名，使用 X.509 证书存放公钥，并使用 RSA 签名以及包括 SHA-256 和 SHA-512 在内的散列算法。如果设置了 CONFIG_MODULE_SIG_FORCE，则只加载具有有效签名的模块，该签名可由内核持有的密钥验证；在宽松模式下，未签名的模块会加载，但会以标志“E”污染内核。公钥内置于内核中，因此签名检查不需要可信的用户空间组件。<sup>[8](https://kernel.org/doc/html/v5.19/admin-guide/module-signing.html)</sup> 在内核锁定（kernel lockdown）状态下，只能加载有效签名的模块。<sup>[18](https://man7.org/linux/man-pages/man7/kernel_lockdown.7.html)</sup>

**许可标签与污染。** 每个 Linux 模块都带有一个 MODULE_LICENSE() 标签，其唯一目的是告诉模块加载器和用户空间工具该模块是自由软件还是专有软件。标记为“Proprietary”的模块在加载时以标志“P”污染内核，并且加载器拒绝将其链接到由 EXPORT_SYMBOL_GPL() 导出的符号。内核的污染标志还记录“O”表示树外模块，“E”表示未签名模块。<sup>[9](https://docs.kernel.org/process/license-rules.html)</sup><sup> • </sup><sup>[19](https://kernel.org/doc/html/v6.7/admin-guide/tainted-kernels.html)</sup>

**排除非特权加载。** delete_module 调用需要 CAP_SYS_MODULE 能力，并且可以通过 /proc/sys/kernel/modules_disabled 在系统范围内禁用卸载。<sup>[14](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.15.pdf)</sup>

保护内核的同一套机制也使模块对攻击者具有吸引力：可加载内核模块攻击通过标准的模块插入路径将恶意代码注入内核，而隐匿工具包已利用 LKM 隐藏进程和操纵系统调用，有时还会删除 /sys/module 下模块自身的条目以逃避检测。<sup>[20](https://cnds.constructor.university/courses/sysec-2024/sysec-notes.pdf)</sup><sup> • </sup><sup>[21](https://doi.org/10.1145/3688808)</sup>

## 构建模块

在 Linux 上，树外（外部）模块使用内核的 kbuild 系统构建。开发者提供 makefile 并运行 make -C $KDIR M=$PWD，其中 $KDIR 是准备好的内核构建树的目录；kbuild 生成一个 <module_name>.ko 文件，modules_install 默认将其安装到 /lib/modules/<kernel_release>/updates/ 下。<sup>[22](https://docs.kernel.org/kbuild/modules.html)</sup> 模块参数可以在内核命令行上提供：modprobe 读取 /proc/cmdline 并收集它所加载模块的参数。<sup>[23](https://docs.kernel.org/6.10/admin-guide/kernel-parameters.html)</sup>

在 FreeBSD 上，大多数功能包含在模块中，可以按需动态加载和卸载，项目称之为模块化内核；对于与内核绑定过紧而无法做成可加载的功能，仍需要静态配置。<sup>[10](https://docs.freebsd.org/en/books/handbook/kernelconfig/)</sup>

## References

1. [Angelakopoulos et al., FirmSolo paper, USENIX Security 2023 (Section 2.2, Loadable Kernel Modules)](https://www.usenix.org/system/files/sec23summer_190-angelakopoulos-prepub.pdf)
2. [Kernel modules, The Linux Kernel documentation (kernel labs)](https://linux-kernel-labs.github.io/refs/pull/222/merge/labs/kernel_modules.html)
3. [kld(4), FreeBSD 13.1-RELEASE Manual Pages](https://man.freebsd.org/cgi/man.cgi?apropos=0&manpath=FreeBSD+13.1-RELEASE&query=kld&sektion=4)
4. [The history of hotplug, kernel.org](https://www.kernel.org/doc/local/hotplug-history.html)
5. [Linux Loadable Kernel Module HOWTO, TLDP](https://tldp.org/HOWTO/pdf/Module-HOWTO.pdf)
6. [A new version of modversions, LWN.net](https://lwn.net/Articles/986892/)
7. [Brooks Davis, Dynamic Interfaces for FreeBSD (EuroBSDCon 2004)](https://people.freebsd.org/~brooks/pubs/eurobsdcon2004/dynif.pdf)
8. [Kernel module signing facility, The Linux Kernel documentation](https://kernel.org/doc/html/v5.19/admin-guide/module-signing.html)
9. [Linux kernel licensing rules, The Linux Kernel documentation](https://docs.kernel.org/process/license-rules.html)
10. [Chapter 10. Configuring the FreeBSD Kernel, FreeBSD Handbook](https://docs.freebsd.org/en/books/handbook/kernelconfig/)
11. [Countering unauthorized code execution on commodity kernels, Computers & Security](https://www.sciencedirect.com/science/article/abs/pii/S0167404811001143)
12. [Tian, Xiong, Hu, and Liu, A policy-centric approach to protecting OS kernel from vulnerable LKMs, Software: Practice and Experience 48(6), 2018](https://pure.psu.edu/en/publications/a-policy-centric-approach-to-protecting-os-kernel-from-vulnerable/)
13. [FreeBSD Handbook](https://docs.freebsd.org/en/books/handbook/book/index.html)
14. [delete_module(2), GNU/Linux Programmer's Manual, man-pages 6.15](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.15.pdf)
15. [Unreliable Guide To Hacking The Linux Kernel, The Linux Kernel documentation](https://docs.kernel.org/kernel-hacking/hacking.html)
16. [Doug Rabson, A New Device Framework for FreeBSD](https://people.freebsd.org/~dfr/devices.html)
17. [Chapter 5. The SYSINIT Framework, FreeBSD Architecture Handbook](https://docs.freebsd.org/en/books/arch-handbook/sysinit/)
18. [kernel_lockdown(7), Linux manual page](https://man7.org/linux/man-pages/man7/kernel_lockdown.7.html)
19. [Tainted kernels, The Linux Kernel documentation](https://kernel.org/doc/html/v6.7/admin-guide/tainted-kernels.html)
20. [Loadable Kernel Module Attacks, course notes, Constructor University](https://cnds.constructor.university/courses/sysec-2024/sysec-notes.pdf)
21. [The Hidden Threat: Analysis of Linux Rootkit Techniques, Digital Threats: Research and Practice](https://doi.org/10.1145/3688808)
22. [Building External Modules, The Linux Kernel documentation](https://docs.kernel.org/kbuild/modules.html)
23. [The kernel's command-line parameters, The Linux Kernel documentation](https://docs.kernel.org/6.10/admin-guide/kernel-parameters.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Operating system concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/loadable-kernel-module

English original: https://www.edgepedia.org/loadable-kernel-module
