Gentoo Archives: gentoo-dev

From: Georgy Yakovlev <ya@×××××××.net>
To: "gentoo-dev@l.g.o" <gentoo-dev@l.g.o>
Cc: gentoo-kernel@l.g.o
Subject: [gentoo-dev] [PATCH] linux-mod.eclass: support module signing
Date: Sat, 14 Apr 2018 21:25:19
Message-Id: 1523741109.12403.28.camel@sysdump.net
1 Hi,
2
3 There is an old bug[1] to support
4 linux kernel module signing at install.
5
6 And here is my first attempt to modify an eclass.
7 Need proper input on it and a kick in the right direction.
8
9 Add 3 variables, settable by users if they keep keys somewhere safe.
10 Otherwise it just works with the auto-generated keys
11 if CONFIG_MODULE_SIG=y and vars are unset.
12
13 eclass will die if kernel requires a signed module,
14 but signing is not requested.
15
16
17 Known problems:
18
19 Packages that do not use linux-mod_src_install() will not sign
20 the modules,
21 But those packages will still inherit module-sign useflag.
22 It's misleading and I'm not sure how to fix that.
23 Examples : sys-kernel/spl, sys-fs/zfs-kmod
24
25 May need additional handling of KBUILD_SIGN_PIN variable[2],
26 which can be set to hold the passphrase to the key. But it may end up
27 in vdb environment files, not sure how to handle that or if it worth it
28
29 not eapi-7 ready because of STRIP_MASK usage.
30 will need to cover this case as well, probably later.
31
32 older (<4.3.3) kernels use perl to sign modules, not sure if it's worth
33 supporting old kernels, there is no gentoo-sources in the tree old
34 enough, except masked 4.1
35 there are old vanilla-sources that will be affected by this.
36
37
38 [1] https://bugs.gentoo.org/447352
39 [2] https://www.kernel.org/doc/html/v4.16/admin-guide/module-signing.html
40
41 diff --git a/eclass/linux-mod.eclass b/eclass/linux-mod.eclass
42 index bf580cf4cfa9..211b0496f528 100644
43 --- a/eclass/linux-mod.eclass
44 +++ b/eclass/linux-mod.eclass
45 @@ -14,7 +14,7 @@
46 # required to install external modules against a kernel source
47 # tree.
48
49 -# A Couple of env vars are available to effect usage of this eclass
50 +# Several env vars are available to effect usage of this eclass
51 # These are as follows:
52
53 # @ECLASS-VARIABLE: MODULES_OPTIONAL_USE
54 @@ -132,6 +132,31 @@
55 # @DESCRIPTION:
56 # It's a read-only variable. It contains the extension of the kernel modules.
57
58 +# @ECLASS-VARIABLE: KERNEL_MODULE_SIG_HASH
59 +# @DEFAULT_UNSET
60 +# @DESCRIPTION:
61 +# A string to control signing algorithm
62 +# Possible values: sha1:sha224:sha256:sha384:sha512
63 +# Defaults to value extracted from .config
64 +# Can be set by user in make.conf, as it can differ from kernel's.
65 +# In case of overriding this it's users responsibility to make sure
66 +# that kernel supports desired hash algo
67 +
68 +# @ECLASS-VARIABLE: KERNEL_MODULE_SIG_PEM
69 +# @DEFAULT_UNSET
70 +# @DESCRIPTION:
71 +# A string, containing path to the private key filename or PKCS#11 URI
72 +# Defaults to ${KV_DIR}/certs/signing_key.pem} if unset.
73 +# Can be set by user in make.conf
74 +
75 +# @ECLASS-VARIABLE: KERNEL_MODULE_SIG_X509
76 +# @DEFAULT_UNSET
77 +# @DESCRIPTION:
78 +# A string, containing path to the public key filename
79 +# Defaults to ${KV_DIR}/certs/signing_key.x509} if unset.
80 +# Can be set by user in make.conf
81 +
82 +
83 inherit eutils linux-info multilib
84 EXPORT_FUNCTIONS pkg_setup pkg_preinst pkg_postinst src_install src_compile pkg_postrm
85
86 @@ -144,12 +169,13 @@ esac
87 0) die "EAPI=${EAPI} is not supported with MODULES_OPTIONAL_USE_IUSE_DEFAULT due to lack of IUSE defaults" ;;
88 esac
89
90 -IUSE="kernel_linux ${MODULES_OPTIONAL_USE:+${_modules_optional_use_iuse_default}}${MODULES_OPTIONAL_USE}"
91 +IUSE="module-sign kernel_linux ${MODULES_OPTIONAL_USE:+${_modules_optional_use_iuse_default}}${MODULES_OPTIONAL_USE}"
92 SLOT="0"
93 RDEPEND="${MODULES_OPTIONAL_USE}${MODULES_OPTIONAL_USE:+? (} kernel_linux? ( virtual/modutils ) ${MODULES_OPTIONAL_USE:+)}"
94 DEPEND="${RDEPEND}
95 ${MODULES_OPTIONAL_USE}${MODULES_OPTIONAL_USE:+? (}
96 sys-apps/sed
97 + module-sign? ( || ( dev-libs/openssl dev-libs/libressl ) )
98 kernel_linux? ( virtual/linux-sources )
99 ${MODULES_OPTIONAL_USE:+)}"
100
101 @@ -196,6 +222,25 @@ check_vermagic() {
102 fi
103 }
104
105 +# @FUNCTION: check_sig_force
106 +# @INTERNAL
107 +# @DESCRIPTION:
108 +# Check if kernel requires module signing and die
109 +# if module is not going to be signed.
110 +check_sig_force() {
111 + debug-print-function ${FUNCNAME} $*
112 +
113 + if linux_chkconfig_present MODULE_SIG_FORCE; then
114 + if use !module-sign; then
115 + ewarn ""
116 + ewarn "Kernel requires all modules to be signed and verified"
117 + ewarn "please enable USE=\"module-sign\""
118 + ewarn "otherwise loading the module will fail"
119 + die "signature required"
120 + fi
121 + fi
122 +}
123 +
124 # @FUNCTION: use_m
125 # @RETURN: true or false
126 # @DESCRIPTION:
127 @@ -352,6 +397,28 @@ get-KERNEL_CC() {
128 echo "${kernel_cc}"
129 }
130
131 +# @FUNCTION: sign_module
132 +# @DESCRIPTION:
133 +# Sign a kernel module if enabled and supported, or just silently ignore the request and do nothing.
134 +# @USAGE: <filename>
135 +sign_module() {
136 + debug-print-function ${FUNCNAME} $*
137 +
138 + if use module-sign; then
139 + local sig_hash sig_pem sig_x509 modulename
140 + sig_hash=$(linux_chkconfig_string MODULE_SIG_HASH)
141 + sig_pem="${KV_DIR}/certs/signing_key.pem"
142 + sig_x509="${KV_DIR}/certs/signing_key.x509"
143 + modulename=$(basename "${1}")
144 +
145 + einfo "Signing ${modulename}"
146 + "${KV_DIR}"/scripts/sign-file \
147 + "${KERNEL_MODULE_SIG_HASH:-${sig_hash//\"/}}" \
148 + "${KERNEL_MODULE_SIG_PEM:-${sig_pem}}" \
149 + "${KERNEL_MODULE_SIG_X509:-${sig_x509}}" \
150 + "${1}" || die "Signing ${modulename} failed"
151 + fi
152 +}
153 # internal function
154 #
155 # FUNCTION:
156 @@ -583,12 +650,17 @@ linux-mod_pkg_setup() {
157 # External modules use kernel symbols (bug #591832)
158 CONFIG_CHECK+=" !TRIM_UNUSED_KSYMS"
159
160 + # if signature is requested, check if kernel actually supports it
161 + use module-sign && CONFIG_CHECK+=" MODULE_SIG"
162 +
163 linux-info_pkg_setup;
164 require_configured_kernel
165 check_kernel_built;
166 strip_modulenames;
167 [[ -n ${MODULE_NAMES} ]] && check_modules_supported
168 set_kvobj;
169 + use module-sign && export STRIP_MASK="*.${KV_OBJ}";
170 + check_sig_force;
171 # Commented out with permission from johnm until a fixed version for arches
172 # who intentionally use different kernel and userland compilers can be
173 # introduced - Jason Wever <weeve@g.o>, 23 Oct 2005
174 @@ -716,8 +788,9 @@ linux-mod_src_install() {
175
176 einfo "Installing ${modulename} module"
177 cd "${objdir}" || die "${objdir} does not exist"
178 - insinto /lib/modules/${KV_FULL}/${libdir}
179 - doins ${modulename}.${KV_OBJ} || die "doins ${modulename}.${KV_OBJ} failed"
180 + sign_module "${modulename}.${KV_OBJ}"
181 + insinto /lib/modules/"${KV_FULL}/${libdir}"
182 + doins "${modulename}.${KV_OBJ}" || die "doins ${modulename}.${KV_OBJ} failed"
183 cd "${OLDPWD}"
184
185 generate_modulesd "${objdir}/${modulename}"

Replies