服务器

版本:25.03

内核模块签名

概述

内核模块签名机制是保护Linux内核安全的重要机制,通过在内核模块文件末尾按照一定格式追加签名信息,并在内核模块加载时检查签名是否与内核预置的公钥匹配,从而保障内核模块文件的真实性和完整性。

前置条件

  1. 准备openEuler内核编译环境,可参考:https://gitee.com/openeuler/kernel/wikis/kernel;
  2. 内核模块签名支持商密算法在openEuler 5.10内核支持,建议选取最新5.10内核源码进行编译;
  3. 生成用于内核模块签名的SM2私钥和证书。使用openssl生成的参考命令如下:
bash
# 生成证书配置文件(配置文件其他字段可按需定义)
$ echo 'subjectKeyIdentifier=hash' > mod.cfg
# 生成SM2签名私钥
$ openssl ecparam -genkey -name SM2 -out mod.key
# 生成签名请求
$ openssl req -new -sm3 -key mod.key -out mod.csr
# 生成SM2证书
$ openssl x509 -req -days 3650 -extfile mod.cfg -signkey mod.key -in mod.csr -out mod.crt

如何使用

场景1:自动签名

将证书和私钥写入到mod.pem文件中:

bash
$ cat /path/to/mod.key > mod.pem
$ cat /path/to/mod.crt >> mod.pem

在内核编译选项中配置使用SM3算法进行内核模块签名,参考步骤如下:

bash
$ make openeuler_defconfig
$ make menuconfig

在图形界面中配置 Enable loadable module support -> Sign modules with SM3:

conf
Which hash algorithm should modules be signed with? (Sign modules with SM3)

指定内核签名使用的私钥和证书从mod.pem中读取 Cryptographic API -> Certificates for signature checking:

conf
(mod.pem) File name or PKCS#11 URI of module signing key

编译内核:

bash
$ make -j64
$ make modules_install
$ make install

通过modinfo检查内核模块的签名信息:

bash
$ modinfo /usr/lib/modules/5.10.0/kernel/crypto/sm4.ko 
filename:       /usr/lib/modules/5.10.0/kernel/crypto/sm4.ko
license:        GPL v2
description:    Generic SM4 library
srcversion:     371050FDB8BF9878D9B5B9B
depends:        
retpoline:      Y
intree:         Y
name:           sm4
vermagic:       5.10.0 SMP mod_unload modversions 
sig_id:         PKCS#7
signer:         Internet Widgits Pty Ltd
sig_key:        33:0B:96:3E:1F:C1:CA:28:98:72:F5:AE:FF:3F:A4:F3:50:5D:E1:87
sig_hashalgo:   sm3
signature:      30:45:02:21:00:81:96:8D:40:CE:7F:7D:AE:3A:4B:CC:DC:9A:F2:B4:
  16:87:3E:C3:DC:77:ED:BC:6E:F5:D8:F3:DD:77:2B:D4:05:02:20:3B:
  39:5A:89:9D:DC:27:83:E8:D8:B4:75:86:FF:33:2B:34:33:D0:90:76:
  32:4D:36:88:84:34:31:5C:83:63:6B

场景2:手动签名

在内核源码目录下调用sign_file对指定内核模块进行签名:

bash
$ ./scripts/sign-file sm3 /path/to/mod.key /path/to/mod.crt <module_file>

其余步骤与场景1相同。

场景3:模块加载校验

在内核启动参数中添加module.sig_enforce,开启内核模块强制签名校验:

bash
linux   /vmlinuz-5.10.0-106.1.0.55.oe2209.x86_64 root=/dev/mapper/openeuler-root ro resume=/dev/mapper/openeuler-swap rd.lvm.lv=openeuler/root rd.lvm.lv=openeuler/swap crashkernel=512M module.sig_enforce

重启系统后,只有通过指定证书校验的内核模块才能被正常加载:

bash
# insmod /usr/lib/modules/5.10.0/kernel/crypto/sm4.ko