18c2ecf20Sopenharmony_ciKernel module signing facility 28c2ecf20Sopenharmony_ci------------------------------ 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci.. CONTENTS 58c2ecf20Sopenharmony_ci.. 68c2ecf20Sopenharmony_ci.. - Overview. 78c2ecf20Sopenharmony_ci.. - Configuring module signing. 88c2ecf20Sopenharmony_ci.. - Generating signing keys. 98c2ecf20Sopenharmony_ci.. - Public keys in the kernel. 108c2ecf20Sopenharmony_ci.. - Manually signing modules. 118c2ecf20Sopenharmony_ci.. - Signed modules and stripping. 128c2ecf20Sopenharmony_ci.. - Loading signed modules. 138c2ecf20Sopenharmony_ci.. - Non-valid signatures and unsigned modules. 148c2ecf20Sopenharmony_ci.. - Administering/protecting the private key. 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci======== 188c2ecf20Sopenharmony_ciOverview 198c2ecf20Sopenharmony_ci======== 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciThe kernel module signing facility cryptographically signs modules during 228c2ecf20Sopenharmony_ciinstallation and then checks the signature upon loading the module. This 238c2ecf20Sopenharmony_ciallows increased kernel security by disallowing the loading of unsigned modules 248c2ecf20Sopenharmony_cior modules signed with an invalid key. Module signing increases security by 258c2ecf20Sopenharmony_cimaking it harder to load a malicious module into the kernel. The module 268c2ecf20Sopenharmony_cisignature checking is done by the kernel so that it is not necessary to have 278c2ecf20Sopenharmony_citrusted userspace bits. 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciThis facility uses X.509 ITU-T standard certificates to encode the public keys 308c2ecf20Sopenharmony_ciinvolved. The signatures are not themselves encoded in any industrial standard 318c2ecf20Sopenharmony_citype. The facility currently only supports the RSA public key encryption 328c2ecf20Sopenharmony_cistandard (though it is pluggable and permits others to be used). The possible 338c2ecf20Sopenharmony_cihash algorithms that can be used are SHA-1, SHA-224, SHA-256, SHA-384, and 348c2ecf20Sopenharmony_ciSHA-512 (the algorithm is selected by data in the signature). 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci========================== 388c2ecf20Sopenharmony_ciConfiguring module signing 398c2ecf20Sopenharmony_ci========================== 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciThe module signing facility is enabled by going to the 428c2ecf20Sopenharmony_ci:menuselection:`Enable Loadable Module Support` section of 438c2ecf20Sopenharmony_cithe kernel configuration and turning on:: 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci CONFIG_MODULE_SIG "Module signature verification" 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ciThis has a number of options available: 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci (1) :menuselection:`Require modules to be validly signed` 508c2ecf20Sopenharmony_ci (``CONFIG_MODULE_SIG_FORCE``) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci This specifies how the kernel should deal with a module that has a 538c2ecf20Sopenharmony_ci signature for which the key is not known or a module that is unsigned. 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci If this is off (ie. "permissive"), then modules for which the key is not 568c2ecf20Sopenharmony_ci available and modules that are unsigned are permitted, but the kernel will 578c2ecf20Sopenharmony_ci be marked as being tainted, and the concerned modules will be marked as 588c2ecf20Sopenharmony_ci tainted, shown with the character 'E'. 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci If this is on (ie. "restrictive"), only modules that have a valid 618c2ecf20Sopenharmony_ci signature that can be verified by a public key in the kernel's possession 628c2ecf20Sopenharmony_ci will be loaded. All other modules will generate an error. 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci Irrespective of the setting here, if the module has a signature block that 658c2ecf20Sopenharmony_ci cannot be parsed, it will be rejected out of hand. 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci (2) :menuselection:`Automatically sign all modules` 698c2ecf20Sopenharmony_ci (``CONFIG_MODULE_SIG_ALL``) 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci If this is on then modules will be automatically signed during the 728c2ecf20Sopenharmony_ci modules_install phase of a build. If this is off, then the modules must 738c2ecf20Sopenharmony_ci be signed manually using:: 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci scripts/sign-file 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci (3) :menuselection:`Which hash algorithm should modules be signed with?` 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci This presents a choice of which hash algorithm the installation phase will 818c2ecf20Sopenharmony_ci sign the modules with: 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci =============================== ========================================== 848c2ecf20Sopenharmony_ci ``CONFIG_MODULE_SIG_SHA1`` :menuselection:`Sign modules with SHA-1` 858c2ecf20Sopenharmony_ci ``CONFIG_MODULE_SIG_SHA224`` :menuselection:`Sign modules with SHA-224` 868c2ecf20Sopenharmony_ci ``CONFIG_MODULE_SIG_SHA256`` :menuselection:`Sign modules with SHA-256` 878c2ecf20Sopenharmony_ci ``CONFIG_MODULE_SIG_SHA384`` :menuselection:`Sign modules with SHA-384` 888c2ecf20Sopenharmony_ci ``CONFIG_MODULE_SIG_SHA512`` :menuselection:`Sign modules with SHA-512` 898c2ecf20Sopenharmony_ci =============================== ========================================== 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci The algorithm selected here will also be built into the kernel (rather 928c2ecf20Sopenharmony_ci than being a module) so that modules signed with that algorithm can have 938c2ecf20Sopenharmony_ci their signatures checked without causing a dependency loop. 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci (4) :menuselection:`File name or PKCS#11 URI of module signing key` 978c2ecf20Sopenharmony_ci (``CONFIG_MODULE_SIG_KEY``) 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci Setting this option to something other than its default of 1008c2ecf20Sopenharmony_ci ``certs/signing_key.pem`` will disable the autogeneration of signing keys 1018c2ecf20Sopenharmony_ci and allow the kernel modules to be signed with a key of your choosing. 1028c2ecf20Sopenharmony_ci The string provided should identify a file containing both a private key 1038c2ecf20Sopenharmony_ci and its corresponding X.509 certificate in PEM form, or — on systems where 1048c2ecf20Sopenharmony_ci the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI as defined by 1058c2ecf20Sopenharmony_ci RFC7512. In the latter case, the PKCS#11 URI should reference both a 1068c2ecf20Sopenharmony_ci certificate and a private key. 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci If the PEM file containing the private key is encrypted, or if the 1098c2ecf20Sopenharmony_ci PKCS#11 token requries a PIN, this can be provided at build time by 1108c2ecf20Sopenharmony_ci means of the ``KBUILD_SIGN_PIN`` variable. 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci (5) :menuselection:`Additional X.509 keys for default system keyring` 1148c2ecf20Sopenharmony_ci (``CONFIG_SYSTEM_TRUSTED_KEYS``) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci This option can be set to the filename of a PEM-encoded file containing 1178c2ecf20Sopenharmony_ci additional certificates which will be included in the system keyring by 1188c2ecf20Sopenharmony_ci default. 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciNote that enabling module signing adds a dependency on the OpenSSL devel 1218c2ecf20Sopenharmony_cipackages to the kernel build processes for the tool that does the signing. 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci======================= 1258c2ecf20Sopenharmony_ciGenerating signing keys 1268c2ecf20Sopenharmony_ci======================= 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ciCryptographic keypairs are required to generate and check signatures. A 1298c2ecf20Sopenharmony_ciprivate key is used to generate a signature and the corresponding public key is 1308c2ecf20Sopenharmony_ciused to check it. The private key is only needed during the build, after which 1318c2ecf20Sopenharmony_ciit can be deleted or stored securely. The public key gets built into the 1328c2ecf20Sopenharmony_cikernel so that it can be used to check the signatures as the modules are 1338c2ecf20Sopenharmony_ciloaded. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ciUnder normal conditions, when ``CONFIG_MODULE_SIG_KEY`` is unchanged from its 1368c2ecf20Sopenharmony_cidefault, the kernel build will automatically generate a new keypair using 1378c2ecf20Sopenharmony_ciopenssl if one does not exist in the file:: 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci certs/signing_key.pem 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ciduring the building of vmlinux (the public part of the key needs to be built 1428c2ecf20Sopenharmony_ciinto vmlinux) using parameters in the:: 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci certs/x509.genkey 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cifile (which is also generated if it does not already exist). 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ciIt is strongly recommended that you provide your own x509.genkey file. 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ciMost notably, in the x509.genkey file, the req_distinguished_name section 1518c2ecf20Sopenharmony_cishould be altered from the default:: 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci [ req_distinguished_name ] 1548c2ecf20Sopenharmony_ci #O = Unspecified company 1558c2ecf20Sopenharmony_ci CN = Build time autogenerated kernel key 1568c2ecf20Sopenharmony_ci #emailAddress = unspecified.user@unspecified.company 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ciThe generated RSA key size can also be set with:: 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci [ req ] 1618c2ecf20Sopenharmony_ci default_bits = 4096 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ciIt is also possible to manually generate the key private/public files using the 1658c2ecf20Sopenharmony_cix509.genkey key generation configuration file in the root node of the Linux 1668c2ecf20Sopenharmony_cikernel sources tree and the openssl command. The following is an example to 1678c2ecf20Sopenharmony_cigenerate the public/private key files:: 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci openssl req -new -nodes -utf8 -sha256 -days 36500 -batch -x509 \ 1708c2ecf20Sopenharmony_ci -config x509.genkey -outform PEM -out kernel_key.pem \ 1718c2ecf20Sopenharmony_ci -keyout kernel_key.pem 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ciThe full pathname for the resulting kernel_key.pem file can then be specified 1748c2ecf20Sopenharmony_ciin the ``CONFIG_MODULE_SIG_KEY`` option, and the certificate and key therein will 1758c2ecf20Sopenharmony_cibe used instead of an autogenerated keypair. 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci========================= 1798c2ecf20Sopenharmony_ciPublic keys in the kernel 1808c2ecf20Sopenharmony_ci========================= 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ciThe kernel contains a ring of public keys that can be viewed by root. They're 1838c2ecf20Sopenharmony_ciin a keyring called ".builtin_trusted_keys" that can be seen by:: 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci [root@deneb ~]# cat /proc/keys 1868c2ecf20Sopenharmony_ci ... 1878c2ecf20Sopenharmony_ci 223c7853 I------ 1 perm 1f030000 0 0 keyring .builtin_trusted_keys: 1 1888c2ecf20Sopenharmony_ci 302d2d52 I------ 1 perm 1f010000 0 0 asymmetri Fedora kernel signing key: d69a84e6bce3d216b979e9505b3e3ef9a7118079: X509.RSA a7118079 [] 1898c2ecf20Sopenharmony_ci ... 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ciBeyond the public key generated specifically for module signing, additional 1928c2ecf20Sopenharmony_citrusted certificates can be provided in a PEM-encoded file referenced by the 1938c2ecf20Sopenharmony_ci``CONFIG_SYSTEM_TRUSTED_KEYS`` configuration option. 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ciFurther, the architecture code may take public keys from a hardware store and 1968c2ecf20Sopenharmony_ciadd those in also (e.g. from the UEFI key database). 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ciFinally, it is possible to add additional public keys by doing:: 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci keyctl padd asymmetric "" [.builtin_trusted_keys-ID] <[key-file] 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cie.g.:: 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci keyctl padd asymmetric "" 0x223c7853 <my_public_key.x509 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ciNote, however, that the kernel will only permit keys to be added to 2078c2ecf20Sopenharmony_ci``.builtin_trusted_keys`` **if** the new key's X.509 wrapper is validly signed by a key 2088c2ecf20Sopenharmony_cithat is already resident in the ``.builtin_trusted_keys`` at the time the key was added. 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci======================== 2128c2ecf20Sopenharmony_ciManually signing modules 2138c2ecf20Sopenharmony_ci======================== 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ciTo manually sign a module, use the scripts/sign-file tool available in 2168c2ecf20Sopenharmony_cithe Linux kernel source tree. The script requires 4 arguments: 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci 1. The hash algorithm (e.g., sha256) 2198c2ecf20Sopenharmony_ci 2. The private key filename or PKCS#11 URI 2208c2ecf20Sopenharmony_ci 3. The public key filename 2218c2ecf20Sopenharmony_ci 4. The kernel module to be signed 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ciThe following is an example to sign a kernel module:: 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci scripts/sign-file sha512 kernel-signkey.priv \ 2268c2ecf20Sopenharmony_ci kernel-signkey.x509 module.ko 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ciThe hash algorithm used does not have to match the one configured, but if it 2298c2ecf20Sopenharmony_cidoesn't, you should make sure that hash algorithm is either built into the 2308c2ecf20Sopenharmony_cikernel or can be loaded without requiring itself. 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ciIf the private key requires a passphrase or PIN, it can be provided in the 2338c2ecf20Sopenharmony_ci$KBUILD_SIGN_PIN environment variable. 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci============================ 2378c2ecf20Sopenharmony_ciSigned modules and stripping 2388c2ecf20Sopenharmony_ci============================ 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ciA signed module has a digital signature simply appended at the end. The string 2418c2ecf20Sopenharmony_ci``~Module signature appended~.`` at the end of the module's file confirms that a 2428c2ecf20Sopenharmony_cisignature is present but it does not confirm that the signature is valid! 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ciSigned modules are BRITTLE as the signature is outside of the defined ELF 2458c2ecf20Sopenharmony_cicontainer. Thus they MAY NOT be stripped once the signature is computed and 2468c2ecf20Sopenharmony_ciattached. Note the entire module is the signed payload, including any and all 2478c2ecf20Sopenharmony_cidebug information present at the time of signing. 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci====================== 2518c2ecf20Sopenharmony_ciLoading signed modules 2528c2ecf20Sopenharmony_ci====================== 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ciModules are loaded with insmod, modprobe, ``init_module()`` or 2558c2ecf20Sopenharmony_ci``finit_module()``, exactly as for unsigned modules as no processing is 2568c2ecf20Sopenharmony_cidone in userspace. The signature checking is all done within the kernel. 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci========================================= 2608c2ecf20Sopenharmony_ciNon-valid signatures and unsigned modules 2618c2ecf20Sopenharmony_ci========================================= 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ciIf ``CONFIG_MODULE_SIG_FORCE`` is enabled or module.sig_enforce=1 is supplied on 2648c2ecf20Sopenharmony_cithe kernel command line, the kernel will only load validly signed modules 2658c2ecf20Sopenharmony_cifor which it has a public key. Otherwise, it will also load modules that are 2668c2ecf20Sopenharmony_ciunsigned. Any module for which the kernel has a key, but which proves to have 2678c2ecf20Sopenharmony_cia signature mismatch will not be permitted to load. 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ciAny module that has an unparseable signature will be rejected. 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci========================================= 2738c2ecf20Sopenharmony_ciAdministering/protecting the private key 2748c2ecf20Sopenharmony_ci========================================= 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ciSince the private key is used to sign modules, viruses and malware could use 2778c2ecf20Sopenharmony_cithe private key to sign modules and compromise the operating system. The 2788c2ecf20Sopenharmony_ciprivate key must be either destroyed or moved to a secure location and not kept 2798c2ecf20Sopenharmony_ciin the root node of the kernel source tree. 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ciIf you use the same private key to sign modules for multiple kernel 2828c2ecf20Sopenharmony_ciconfigurations, you must ensure that the module version information is 2838c2ecf20Sopenharmony_cisufficient to prevent loading a module into a different kernel. Either 2848c2ecf20Sopenharmony_ciset ``CONFIG_MODVERSIONS=y`` or ensure that each configuration has a different 2858c2ecf20Sopenharmony_cikernel release string by changing ``EXTRAVERSION`` or ``CONFIG_LOCALVERSION``. 286