194e980ccSMauro Carvalho ChehabKernel module signing facility 294e980ccSMauro Carvalho Chehab------------------------------ 394e980ccSMauro Carvalho Chehab 494e980ccSMauro Carvalho Chehab.. CONTENTS 594e980ccSMauro Carvalho Chehab.. 694e980ccSMauro Carvalho Chehab.. - Overview. 794e980ccSMauro Carvalho Chehab.. - Configuring module signing. 894e980ccSMauro Carvalho Chehab.. - Generating signing keys. 994e980ccSMauro Carvalho Chehab.. - Public keys in the kernel. 1094e980ccSMauro Carvalho Chehab.. - Manually signing modules. 1194e980ccSMauro Carvalho Chehab.. - Signed modules and stripping. 1294e980ccSMauro Carvalho Chehab.. - Loading signed modules. 1394e980ccSMauro Carvalho Chehab.. - Non-valid signatures and unsigned modules. 1494e980ccSMauro Carvalho Chehab.. - Administering/protecting the private key. 1594e980ccSMauro Carvalho Chehab 1694e980ccSMauro Carvalho Chehab 1794e980ccSMauro Carvalho Chehab======== 1894e980ccSMauro Carvalho ChehabOverview 1994e980ccSMauro Carvalho Chehab======== 2094e980ccSMauro Carvalho Chehab 2194e980ccSMauro Carvalho ChehabThe kernel module signing facility cryptographically signs modules during 2294e980ccSMauro Carvalho Chehabinstallation and then checks the signature upon loading the module. This 2394e980ccSMauro Carvalho Chehaballows increased kernel security by disallowing the loading of unsigned modules 2494e980ccSMauro Carvalho Chehabor modules signed with an invalid key. Module signing increases security by 2594e980ccSMauro Carvalho Chehabmaking it harder to load a malicious module into the kernel. The module 2694e980ccSMauro Carvalho Chehabsignature checking is done by the kernel so that it is not necessary to have 2794e980ccSMauro Carvalho Chehabtrusted userspace bits. 2894e980ccSMauro Carvalho Chehab 2994e980ccSMauro Carvalho ChehabThis facility uses X.509 ITU-T standard certificates to encode the public keys 3094e980ccSMauro Carvalho Chehabinvolved. The signatures are not themselves encoded in any industrial standard 3194e980ccSMauro Carvalho Chehabtype. The facility currently only supports the RSA public key encryption 3294e980ccSMauro Carvalho Chehabstandard (though it is pluggable and permits others to be used). The possible 3394e980ccSMauro Carvalho Chehabhash algorithms that can be used are SHA-1, SHA-224, SHA-256, SHA-384, and 3494e980ccSMauro Carvalho ChehabSHA-512 (the algorithm is selected by data in the signature). 3594e980ccSMauro Carvalho Chehab 3694e980ccSMauro Carvalho Chehab 3794e980ccSMauro Carvalho Chehab========================== 3894e980ccSMauro Carvalho ChehabConfiguring module signing 3994e980ccSMauro Carvalho Chehab========================== 4094e980ccSMauro Carvalho Chehab 4194e980ccSMauro Carvalho ChehabThe module signing facility is enabled by going to the 4294e980ccSMauro Carvalho Chehab:menuselection:`Enable Loadable Module Support` section of 4394e980ccSMauro Carvalho Chehabthe kernel configuration and turning on:: 4494e980ccSMauro Carvalho Chehab 4594e980ccSMauro Carvalho Chehab CONFIG_MODULE_SIG "Module signature verification" 4694e980ccSMauro Carvalho Chehab 4794e980ccSMauro Carvalho ChehabThis has a number of options available: 4894e980ccSMauro Carvalho Chehab 4994e980ccSMauro Carvalho Chehab (1) :menuselection:`Require modules to be validly signed` 5094e980ccSMauro Carvalho Chehab (``CONFIG_MODULE_SIG_FORCE``) 5194e980ccSMauro Carvalho Chehab 5294e980ccSMauro Carvalho Chehab This specifies how the kernel should deal with a module that has a 5394e980ccSMauro Carvalho Chehab signature for which the key is not known or a module that is unsigned. 5494e980ccSMauro Carvalho Chehab 5594e980ccSMauro Carvalho Chehab If this is off (ie. "permissive"), then modules for which the key is not 5694e980ccSMauro Carvalho Chehab available and modules that are unsigned are permitted, but the kernel will 5794e980ccSMauro Carvalho Chehab be marked as being tainted, and the concerned modules will be marked as 5894e980ccSMauro Carvalho Chehab tainted, shown with the character 'E'. 5994e980ccSMauro Carvalho Chehab 6094e980ccSMauro Carvalho Chehab If this is on (ie. "restrictive"), only modules that have a valid 6194e980ccSMauro Carvalho Chehab signature that can be verified by a public key in the kernel's possession 6294e980ccSMauro Carvalho Chehab will be loaded. All other modules will generate an error. 6394e980ccSMauro Carvalho Chehab 6494e980ccSMauro Carvalho Chehab Irrespective of the setting here, if the module has a signature block that 6594e980ccSMauro Carvalho Chehab cannot be parsed, it will be rejected out of hand. 6694e980ccSMauro Carvalho Chehab 6794e980ccSMauro Carvalho Chehab 6894e980ccSMauro Carvalho Chehab (2) :menuselection:`Automatically sign all modules` 6994e980ccSMauro Carvalho Chehab (``CONFIG_MODULE_SIG_ALL``) 7094e980ccSMauro Carvalho Chehab 7194e980ccSMauro Carvalho Chehab If this is on then modules will be automatically signed during the 7294e980ccSMauro Carvalho Chehab modules_install phase of a build. If this is off, then the modules must 7394e980ccSMauro Carvalho Chehab be signed manually using:: 7494e980ccSMauro Carvalho Chehab 7594e980ccSMauro Carvalho Chehab scripts/sign-file 7694e980ccSMauro Carvalho Chehab 7794e980ccSMauro Carvalho Chehab 7894e980ccSMauro Carvalho Chehab (3) :menuselection:`Which hash algorithm should modules be signed with?` 7994e980ccSMauro Carvalho Chehab 8094e980ccSMauro Carvalho Chehab This presents a choice of which hash algorithm the installation phase will 8194e980ccSMauro Carvalho Chehab sign the modules with: 8294e980ccSMauro Carvalho Chehab 8394e980ccSMauro Carvalho Chehab =============================== ========================================== 8494e980ccSMauro Carvalho Chehab ``CONFIG_MODULE_SIG_SHA1`` :menuselection:`Sign modules with SHA-1` 8594e980ccSMauro Carvalho Chehab ``CONFIG_MODULE_SIG_SHA224`` :menuselection:`Sign modules with SHA-224` 8694e980ccSMauro Carvalho Chehab ``CONFIG_MODULE_SIG_SHA256`` :menuselection:`Sign modules with SHA-256` 8794e980ccSMauro Carvalho Chehab ``CONFIG_MODULE_SIG_SHA384`` :menuselection:`Sign modules with SHA-384` 8894e980ccSMauro Carvalho Chehab ``CONFIG_MODULE_SIG_SHA512`` :menuselection:`Sign modules with SHA-512` 8994e980ccSMauro Carvalho Chehab =============================== ========================================== 9094e980ccSMauro Carvalho Chehab 9194e980ccSMauro Carvalho Chehab The algorithm selected here will also be built into the kernel (rather 9294e980ccSMauro Carvalho Chehab than being a module) so that modules signed with that algorithm can have 9394e980ccSMauro Carvalho Chehab their signatures checked without causing a dependency loop. 9494e980ccSMauro Carvalho Chehab 9594e980ccSMauro Carvalho Chehab 9694e980ccSMauro Carvalho Chehab (4) :menuselection:`File name or PKCS#11 URI of module signing key` 9794e980ccSMauro Carvalho Chehab (``CONFIG_MODULE_SIG_KEY``) 9894e980ccSMauro Carvalho Chehab 9994e980ccSMauro Carvalho Chehab Setting this option to something other than its default of 10094e980ccSMauro Carvalho Chehab ``certs/signing_key.pem`` will disable the autogeneration of signing keys 10194e980ccSMauro Carvalho Chehab and allow the kernel modules to be signed with a key of your choosing. 10294e980ccSMauro Carvalho Chehab The string provided should identify a file containing both a private key 10394e980ccSMauro Carvalho Chehab and its corresponding X.509 certificate in PEM form, or — on systems where 10494e980ccSMauro Carvalho Chehab the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI as defined by 10594e980ccSMauro Carvalho Chehab RFC7512. In the latter case, the PKCS#11 URI should reference both a 10694e980ccSMauro Carvalho Chehab certificate and a private key. 10794e980ccSMauro Carvalho Chehab 10894e980ccSMauro Carvalho Chehab If the PEM file containing the private key is encrypted, or if the 109751d5b27SAndrew Klychkov PKCS#11 token requires a PIN, this can be provided at build time by 11094e980ccSMauro Carvalho Chehab means of the ``KBUILD_SIGN_PIN`` variable. 11194e980ccSMauro Carvalho Chehab 11294e980ccSMauro Carvalho Chehab 11394e980ccSMauro Carvalho Chehab (5) :menuselection:`Additional X.509 keys for default system keyring` 11494e980ccSMauro Carvalho Chehab (``CONFIG_SYSTEM_TRUSTED_KEYS``) 11594e980ccSMauro Carvalho Chehab 11694e980ccSMauro Carvalho Chehab This option can be set to the filename of a PEM-encoded file containing 11794e980ccSMauro Carvalho Chehab additional certificates which will be included in the system keyring by 11894e980ccSMauro Carvalho Chehab default. 11994e980ccSMauro Carvalho Chehab 12094e980ccSMauro Carvalho ChehabNote that enabling module signing adds a dependency on the OpenSSL devel 12194e980ccSMauro Carvalho Chehabpackages to the kernel build processes for the tool that does the signing. 12294e980ccSMauro Carvalho Chehab 12394e980ccSMauro Carvalho Chehab 12494e980ccSMauro Carvalho Chehab======================= 12594e980ccSMauro Carvalho ChehabGenerating signing keys 12694e980ccSMauro Carvalho Chehab======================= 12794e980ccSMauro Carvalho Chehab 12894e980ccSMauro Carvalho ChehabCryptographic keypairs are required to generate and check signatures. A 12994e980ccSMauro Carvalho Chehabprivate key is used to generate a signature and the corresponding public key is 13094e980ccSMauro Carvalho Chehabused to check it. The private key is only needed during the build, after which 13194e980ccSMauro Carvalho Chehabit can be deleted or stored securely. The public key gets built into the 13294e980ccSMauro Carvalho Chehabkernel so that it can be used to check the signatures as the modules are 13394e980ccSMauro Carvalho Chehabloaded. 13494e980ccSMauro Carvalho Chehab 13594e980ccSMauro Carvalho ChehabUnder normal conditions, when ``CONFIG_MODULE_SIG_KEY`` is unchanged from its 13694e980ccSMauro Carvalho Chehabdefault, the kernel build will automatically generate a new keypair using 13794e980ccSMauro Carvalho Chehabopenssl if one does not exist in the file:: 13894e980ccSMauro Carvalho Chehab 13994e980ccSMauro Carvalho Chehab certs/signing_key.pem 14094e980ccSMauro Carvalho Chehab 14194e980ccSMauro Carvalho Chehabduring the building of vmlinux (the public part of the key needs to be built 14294e980ccSMauro Carvalho Chehabinto vmlinux) using parameters in the:: 14394e980ccSMauro Carvalho Chehab 14494e980ccSMauro Carvalho Chehab certs/x509.genkey 14594e980ccSMauro Carvalho Chehab 14694e980ccSMauro Carvalho Chehabfile (which is also generated if it does not already exist). 14794e980ccSMauro Carvalho Chehab 14894e980ccSMauro Carvalho ChehabIt is strongly recommended that you provide your own x509.genkey file. 14994e980ccSMauro Carvalho Chehab 15094e980ccSMauro Carvalho ChehabMost notably, in the x509.genkey file, the req_distinguished_name section 15194e980ccSMauro Carvalho Chehabshould be altered from the default:: 15294e980ccSMauro Carvalho Chehab 15394e980ccSMauro Carvalho Chehab [ req_distinguished_name ] 15494e980ccSMauro Carvalho Chehab #O = Unspecified company 15594e980ccSMauro Carvalho Chehab CN = Build time autogenerated kernel key 15694e980ccSMauro Carvalho Chehab #emailAddress = unspecified.user@unspecified.company 15794e980ccSMauro Carvalho Chehab 15894e980ccSMauro Carvalho ChehabThe generated RSA key size can also be set with:: 15994e980ccSMauro Carvalho Chehab 16094e980ccSMauro Carvalho Chehab [ req ] 16194e980ccSMauro Carvalho Chehab default_bits = 4096 16294e980ccSMauro Carvalho Chehab 16394e980ccSMauro Carvalho Chehab 16494e980ccSMauro Carvalho ChehabIt is also possible to manually generate the key private/public files using the 16594e980ccSMauro Carvalho Chehabx509.genkey key generation configuration file in the root node of the Linux 16694e980ccSMauro Carvalho Chehabkernel sources tree and the openssl command. The following is an example to 16794e980ccSMauro Carvalho Chehabgenerate the public/private key files:: 16894e980ccSMauro Carvalho Chehab 16994e980ccSMauro Carvalho Chehab openssl req -new -nodes -utf8 -sha256 -days 36500 -batch -x509 \ 17094e980ccSMauro Carvalho Chehab -config x509.genkey -outform PEM -out kernel_key.pem \ 17194e980ccSMauro Carvalho Chehab -keyout kernel_key.pem 17294e980ccSMauro Carvalho Chehab 17394e980ccSMauro Carvalho ChehabThe full pathname for the resulting kernel_key.pem file can then be specified 17494e980ccSMauro Carvalho Chehabin the ``CONFIG_MODULE_SIG_KEY`` option, and the certificate and key therein will 17594e980ccSMauro Carvalho Chehabbe used instead of an autogenerated keypair. 17694e980ccSMauro Carvalho Chehab 17794e980ccSMauro Carvalho Chehab 17894e980ccSMauro Carvalho Chehab========================= 17994e980ccSMauro Carvalho ChehabPublic keys in the kernel 18094e980ccSMauro Carvalho Chehab========================= 18194e980ccSMauro Carvalho Chehab 18294e980ccSMauro Carvalho ChehabThe kernel contains a ring of public keys that can be viewed by root. They're 183dddc7231SPhilipp Hahnin a keyring called ".builtin_trusted_keys" that can be seen by:: 18494e980ccSMauro Carvalho Chehab 18594e980ccSMauro Carvalho Chehab [root@deneb ~]# cat /proc/keys 18694e980ccSMauro Carvalho Chehab ... 187dddc7231SPhilipp Hahn 223c7853 I------ 1 perm 1f030000 0 0 keyring .builtin_trusted_keys: 1 18894e980ccSMauro Carvalho Chehab 302d2d52 I------ 1 perm 1f010000 0 0 asymmetri Fedora kernel signing key: d69a84e6bce3d216b979e9505b3e3ef9a7118079: X509.RSA a7118079 [] 18994e980ccSMauro Carvalho Chehab ... 19094e980ccSMauro Carvalho Chehab 19194e980ccSMauro Carvalho ChehabBeyond the public key generated specifically for module signing, additional 19294e980ccSMauro Carvalho Chehabtrusted certificates can be provided in a PEM-encoded file referenced by the 19394e980ccSMauro Carvalho Chehab``CONFIG_SYSTEM_TRUSTED_KEYS`` configuration option. 19494e980ccSMauro Carvalho Chehab 19594e980ccSMauro Carvalho ChehabFurther, the architecture code may take public keys from a hardware store and 19694e980ccSMauro Carvalho Chehabadd those in also (e.g. from the UEFI key database). 19794e980ccSMauro Carvalho Chehab 19894e980ccSMauro Carvalho ChehabFinally, it is possible to add additional public keys by doing:: 19994e980ccSMauro Carvalho Chehab 200dddc7231SPhilipp Hahn keyctl padd asymmetric "" [.builtin_trusted_keys-ID] <[key-file] 20194e980ccSMauro Carvalho Chehab 20294e980ccSMauro Carvalho Chehabe.g.:: 20394e980ccSMauro Carvalho Chehab 20494e980ccSMauro Carvalho Chehab keyctl padd asymmetric "" 0x223c7853 <my_public_key.x509 20594e980ccSMauro Carvalho Chehab 20694e980ccSMauro Carvalho ChehabNote, however, that the kernel will only permit keys to be added to 20729b26ae4SPhilipp Hahn``.builtin_trusted_keys`` **if** the new key's X.509 wrapper is validly signed by a key 20829b26ae4SPhilipp Hahnthat is already resident in the ``.builtin_trusted_keys`` at the time the key was added. 20994e980ccSMauro Carvalho Chehab 21094e980ccSMauro Carvalho Chehab 21194e980ccSMauro Carvalho Chehab======================== 21294e980ccSMauro Carvalho ChehabManually signing modules 21394e980ccSMauro Carvalho Chehab======================== 21494e980ccSMauro Carvalho Chehab 21594e980ccSMauro Carvalho ChehabTo manually sign a module, use the scripts/sign-file tool available in 21694e980ccSMauro Carvalho Chehabthe Linux kernel source tree. The script requires 4 arguments: 21794e980ccSMauro Carvalho Chehab 21894e980ccSMauro Carvalho Chehab 1. The hash algorithm (e.g., sha256) 21994e980ccSMauro Carvalho Chehab 2. The private key filename or PKCS#11 URI 22094e980ccSMauro Carvalho Chehab 3. The public key filename 22194e980ccSMauro Carvalho Chehab 4. The kernel module to be signed 22294e980ccSMauro Carvalho Chehab 22394e980ccSMauro Carvalho ChehabThe following is an example to sign a kernel module:: 22494e980ccSMauro Carvalho Chehab 22594e980ccSMauro Carvalho Chehab scripts/sign-file sha512 kernel-signkey.priv \ 22694e980ccSMauro Carvalho Chehab kernel-signkey.x509 module.ko 22794e980ccSMauro Carvalho Chehab 22894e980ccSMauro Carvalho ChehabThe hash algorithm used does not have to match the one configured, but if it 22994e980ccSMauro Carvalho Chehabdoesn't, you should make sure that hash algorithm is either built into the 23094e980ccSMauro Carvalho Chehabkernel or can be loaded without requiring itself. 23194e980ccSMauro Carvalho Chehab 23294e980ccSMauro Carvalho ChehabIf the private key requires a passphrase or PIN, it can be provided in the 23394e980ccSMauro Carvalho Chehab$KBUILD_SIGN_PIN environment variable. 23494e980ccSMauro Carvalho Chehab 23594e980ccSMauro Carvalho Chehab 23694e980ccSMauro Carvalho Chehab============================ 23794e980ccSMauro Carvalho ChehabSigned modules and stripping 23894e980ccSMauro Carvalho Chehab============================ 23994e980ccSMauro Carvalho Chehab 24094e980ccSMauro Carvalho ChehabA signed module has a digital signature simply appended at the end. The string 24194e980ccSMauro Carvalho Chehab``~Module signature appended~.`` at the end of the module's file confirms that a 24294e980ccSMauro Carvalho Chehabsignature is present but it does not confirm that the signature is valid! 24394e980ccSMauro Carvalho Chehab 24494e980ccSMauro Carvalho ChehabSigned modules are BRITTLE as the signature is outside of the defined ELF 24594e980ccSMauro Carvalho Chehabcontainer. Thus they MAY NOT be stripped once the signature is computed and 24694e980ccSMauro Carvalho Chehabattached. Note the entire module is the signed payload, including any and all 24794e980ccSMauro Carvalho Chehabdebug information present at the time of signing. 24894e980ccSMauro Carvalho Chehab 24994e980ccSMauro Carvalho Chehab 25094e980ccSMauro Carvalho Chehab====================== 25194e980ccSMauro Carvalho ChehabLoading signed modules 25294e980ccSMauro Carvalho Chehab====================== 25394e980ccSMauro Carvalho Chehab 25494e980ccSMauro Carvalho ChehabModules are loaded with insmod, modprobe, ``init_module()`` or 25594e980ccSMauro Carvalho Chehab``finit_module()``, exactly as for unsigned modules as no processing is 25694e980ccSMauro Carvalho Chehabdone in userspace. The signature checking is all done within the kernel. 25794e980ccSMauro Carvalho Chehab 25894e980ccSMauro Carvalho Chehab 25994e980ccSMauro Carvalho Chehab========================================= 26094e980ccSMauro Carvalho ChehabNon-valid signatures and unsigned modules 26194e980ccSMauro Carvalho Chehab========================================= 26294e980ccSMauro Carvalho Chehab 26394e980ccSMauro Carvalho ChehabIf ``CONFIG_MODULE_SIG_FORCE`` is enabled or module.sig_enforce=1 is supplied on 26494e980ccSMauro Carvalho Chehabthe kernel command line, the kernel will only load validly signed modules 26594e980ccSMauro Carvalho Chehabfor which it has a public key. Otherwise, it will also load modules that are 26694e980ccSMauro Carvalho Chehabunsigned. Any module for which the kernel has a key, but which proves to have 26794e980ccSMauro Carvalho Chehaba signature mismatch will not be permitted to load. 26894e980ccSMauro Carvalho Chehab 269*d56b699dSBjorn HelgaasAny module that has an unparsable signature will be rejected. 27094e980ccSMauro Carvalho Chehab 27194e980ccSMauro Carvalho Chehab 27294e980ccSMauro Carvalho Chehab========================================= 27394e980ccSMauro Carvalho ChehabAdministering/protecting the private key 27494e980ccSMauro Carvalho Chehab========================================= 27594e980ccSMauro Carvalho Chehab 27694e980ccSMauro Carvalho ChehabSince the private key is used to sign modules, viruses and malware could use 27794e980ccSMauro Carvalho Chehabthe private key to sign modules and compromise the operating system. The 27894e980ccSMauro Carvalho Chehabprivate key must be either destroyed or moved to a secure location and not kept 27994e980ccSMauro Carvalho Chehabin the root node of the kernel source tree. 28094e980ccSMauro Carvalho Chehab 28194e980ccSMauro Carvalho ChehabIf you use the same private key to sign modules for multiple kernel 28294e980ccSMauro Carvalho Chehabconfigurations, you must ensure that the module version information is 28394e980ccSMauro Carvalho Chehabsufficient to prevent loading a module into a different kernel. Either 28494e980ccSMauro Carvalho Chehabset ``CONFIG_MODVERSIONS=y`` or ensure that each configuration has a different 28594e980ccSMauro Carvalho Chehabkernel release string by changing ``EXTRAVERSION`` or ``CONFIG_LOCALVERSION``. 286