[system-administrators-guide] Signing Kernel Modules for Secure Boot

stephenw stephenw at fedoraproject.org
Sat Dec 13 08:19:13 UTC 2014


commit b5d1e67efcbe76a2258604f5087fef4c40ea4917
Author: Stephen Wadeley <swadeley at redhat.com>
Date:   Sat Dec 13 09:08:27 2014 +0100

    Signing Kernel Modules for Secure Boot

 en-US/Working_with_Kernel_Modules.xml |  445 +++++++++++++++++++++++++++++++++
 1 files changed, 445 insertions(+), 0 deletions(-)
---
diff --git a/en-US/Working_with_Kernel_Modules.xml b/en-US/Working_with_Kernel_Modules.xml
index 790d15f..182ee4c 100644
--- a/en-US/Working_with_Kernel_Modules.xml
+++ b/en-US/Working_with_Kernel_Modules.xml
@@ -476,6 +476,451 @@ virtio-net</screen>
 
   </section>
 
+  <section id="sect-signing-kernel-modules-for-secure-boot">
+  	<title>Signing Kernel Modules for Secure Boot</title>
+  	<para>
+  		&PRODUCT; includes support for the UEFI Secure Boot feature, which means that &PRODUCT; can be installed and run on systems where UEFI Secure Boot is enabled. <footnote><para>&PRODUCT; does not require the use of Secure Boot on UEFI systems.</para></footnote> When Secure Boot is enabled, the EFI operating system boot loaders, the &PRODUCT; kernel, and all kernel modules must be signed with a private key and authenticated with the corresponding public key. The &PRODUCT; distribution includes signed boot loaders, signed kernels, and signed kernel modules. In addition, the signed first-stage boot loader and the signed kernel include embedded &PRODUCT; public keys. These signed executable binaries and embedded keys enable &PRODUCT; to install, boot, and run with the Microsoft UEFI Secure Boot CA keys that are provided by the UEFI firmware on systems that support UEFI Secure Boot.<footnote><para>Not all UEFI-based systems include support for Secure Boot.</para></footnote>
+  	</para>
+  	<para>
+  		The information provided in the following sections describes steps necessary to enable you to self-sign privately built kernel modules for use with &PRODUCT; on UEFI-based systems where Secure Boot is enabled. These sections also provide an overview of available options for getting your public key onto the target system where you want to deploy your kernel module.
+  	</para>
+  	
+  	<section id="sect-prerequisites">
+  		<title>Prerequisites</title>
+  		<para>
+  			In order to enable signing of externally built modules, the tools listed in the following table are required to be installed on the system.
+  		</para>
+  		
+			<table frame='all' id="table-required-tools"><title>Required Tools</title>
+				<tgroup cols='4' align='left'>
+					<thead>
+						<row>
+							<entry>Tool</entry>
+							<entry>Provided by Package</entry>
+							<entry>Used on</entry>
+							<entry>Purpose</entry>
+						</row>
+					</thead>
+					<tbody>
+						<row>
+							<entry><command>openssl</command></entry>
+							<entry><package>openssl</package></entry>
+							<entry>Build system</entry>
+							<entry>Generates public and private X.509 key pair</entry>
+						</row>
+						<row>
+							<entry><command>sign-file</command></entry>
+							<entry><package>kernel-devel</package></entry>
+							<entry>Build system</entry>
+							<entry>Perl script used to sign kernel modules</entry>
+						</row>
+						<row>
+							<entry><command>perl</command></entry>
+							<entry><package>perl</package></entry>
+							<entry>Build system</entry>
+							<entry>Perl interpreter used to run the signing script</entry>
+						</row>
+						<row>
+							<entry><command>mokutil</command></entry>
+							<entry><package>mokutil</package></entry>
+							<entry>Target system</entry>
+							<entry>Optional tool used to manually enroll the public key</entry>
+						</row>
+						<row>
+							<entry><command>keyctl</command></entry>
+							<entry><package>keyutils</package></entry>
+							<entry>Target system</entry>
+							<entry>Optional tool used to display public keys in the system key ring</entry>
+						</row>
+					</tbody>
+				</tgroup>
+			</table>
+			<note>
+				<para>
+					Note that the build system, where you build and sign your kernel module, does not need to have UEFI Secure Boot enabled and does not even need to be a UEFI-based system.
+				</para>
+			</note>
+  	</section>
+  	
+  	<section id="sect-kernel-module-authentication">
+  		<title>Kernel Module Authentication</title>
+  		<para>
+  			In &PRODUCT;, when a kernel module is loaded, the module's signature is checked using the public X.509 keys on the kernel's system key ring, excluding those keys that are on the kernel's system black list key ring.
+  		</para>
+
+  		<section id="sect-sources-for-public-keys-used-to-authenticate-kernel-modules">
+  			<title>Sources For Public Keys Used To Authenticate Kernel Modules</title>
+  			<para>
+  				During boot, the kernel loads X.509 keys into the system key ring or the system black list key ring from a set of persistent key stores as shown in <xref linkend="table-sources-for-system-key-rings" />
+  			</para>
+  			
+			<table frame='all' id="table-sources-for-system-key-rings"><title>Sources For System Key Rings</title>
+				<tgroup cols='4' align='left'>
+					<thead>
+						<row>
+							<entry>Source of X.509 Keys</entry>
+							<entry>User Ability to Add Keys</entry>
+							<entry>UEFI Secure Boot State</entry>
+							<entry>Keys Loaded During Boot</entry>
+						</row>
+					</thead>
+					<tbody>
+						<row>
+							<entry>Embedded in kernel</entry>
+							<entry>No</entry>
+							<entry>-</entry>
+							<entry><systemitem>.system_keyring</systemitem></entry>
+						</row>
+						<row>
+							<entry morerows="1">UEFI Secure Boot "db"</entry>
+							<entry morerows="1">Limited</entry>
+							<entry>Not enabled</entry>
+							<entry>No</entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry><systemitem>.system_keyring</systemitem></entry>
+						</row>
+						<row>
+							<entry morerows="1">UEFI Secure Boot "dbx"</entry>
+							<entry morerows="1">Limited</entry>
+							<entry>Not enabled</entry>
+							<entry>No</entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry><systemitem>.system_keyring</systemitem></entry>
+						</row>
+						<row>
+							<entry morerows="1">Embedded in <systemitem>shim.efi</systemitem> boot loader</entry>
+							<entry morerows="1">No</entry>
+							<entry>Not enabled</entry>
+							<entry>No</entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry><systemitem>.system_keyring</systemitem></entry>
+						</row>
+						<row>
+							<entry morerows="1">Machine Owner Key (MOK) list</entry>
+							<entry morerows="1">Yes</entry>
+							<entry>Not enabled</entry>
+							<entry>No</entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry><systemitem>.system_keyring</systemitem></entry>
+						</row>
+					</tbody>
+				</tgroup>
+			</table>
+  			
+  			<para>
+  				Note that if the system is not UEFI-based or if UEFI Secure Boot is not enabled, then only the keys that are embedded in the kernel are loaded onto the system key ring and you have no ability to augment that set of keys without rebuilding the kernel. The system black list key ring is a list of X.509 keys which have been revoked. If your module is signed by a key on the black list then it will fail authentication even if your public key is in the system key ring.
+  			</para>
+  			<para>
+  				You can display information about the keys on the system key rings using the <command>keyctl</command> utility. The following is abbreviated example output from a &PRODUCT; system where UEFI Secure Boot is not enabled.
+  			</para>
+<screen>~]#&nbsp;<command>keyctl list %:.system_keyring</command>
+1 key in keyring:
+ 61139991: ---lswrv     0     0 asymmetric: Fedora kernel signing key: 1fc9e68f7419556348fdee2fdeb7ff9da6337b</screen>
+				<para>
+					The following is abbreviated example output from a &PRODUCT; system where UEFI Secure Boot is enabled.
+				</para>
+<screen>~]#&nbsp;<command>keyctl list %:.system_keyring</command>
+6 keys in keyring:
+...asymmetric: Red Hat Enterprise Linux Driver Update Program (key 3): bf57f3e87...
+...asymmetric: Red Hat Secure Boot (CA key 1): 4016841644ce3a810408050766e8f8a29...
+...asymmetric: Microsoft Corporation UEFI CA 2011: 13adbf4309bd82709c8cd54f316ed...
+...asymmetric: Microsoft Windows Production PCA 2011: a92902398e16c49778cd90f99e...
+...asymmetric: Red Hat Enterprise Linux kernel signing key: 4249689eefc77e95880b...
+...asymmetric: Red Hat Enterprise Linux kpatch signing key: 4d38fd864ebe18c5f0b7...</screen>
+				<para>
+					The above output shows the addition of two keys from the UEFI Secure Boot "db" keys plus the <computeroutput>Red&nbsp;Hat Secure Boot (CA key 1)</computeroutput> which is embedded in the <systemitem>shim.efi</systemitem> boot loader. You can also look for the kernel console messages that identify the keys with an UEFI Secure Boot related source, that is UEFI Secure Boot db, embedded shim, and MOK list.
+				</para>
+<screen>~]#&nbsp;<command>dmesg | grep 'EFI: Loaded cert'</command>
+[5.160660] EFI: Loaded cert 'Microsoft Windows Production PCA 2011: a9290239...
+[5.160674] EFI: Loaded cert 'Microsoft Corporation UEFI CA 2011: 13adbf4309b...
+[5.165794] EFI: Loaded cert 'Red Hat Secure Boot (CA key 1): 4016841644ce3a8...</screen>
+  		</section>
+  		<section id="sect-kernel-module-authentication-requirements">
+  			<title>
+  				Kernel Module Authentication Requirements
+  			</title>
+  			<para>
+  				If UEFI Secure Boot is enabled or if the <option>module.sig_enforce</option> kernel parameter has been specified, then only signed kernel modules that are authenticated using a key on the system key ring can be successfully loaded.<footnote><para>Provided that the public key is not on the system black list key ring.</para></footnote> If UEFI Secure Boot is disabled and if the <option>module.sig_enforce</option> kernel parameter has not been specified, then unsigned kernel modules and signed kernel modules without a public key can be successfully loaded. This is summarized in <xref linkend="table-kernel-module-authentication-requirements-for-loading"/>.
+  			</para>
+
+			<table frame='all' id="table-kernel-module-authentication-requirements-for-loading"><title>Kernel Module Authentication Requirements for Loading</title>
+				<tgroup cols='6' align='left'>
+					<thead>
+						<row>
+							<entry>Module Signed</entry>
+							<entry>Public Key Found and Signature Valid</entry>
+							<entry>UEFI Secure Boot State</entry>
+							<entry>module.sig_enforce</entry>
+							<entry>Module Load</entry>
+							<entry>Kernel Tainted</entry>
+						</row>
+					</thead>
+					<tbody>
+						<row>
+							<entry morerows="2">Unsigned</entry>
+							<entry morerows="2">-</entry>
+							<entry>Not enabled</entry>
+							<entry>Not enabled</entry>
+							<entry>Succeeds</entry>
+							<entry>Yes</entry>
+						</row>
+						<row>
+							<entry>Not enabled</entry>
+							<entry>Enabled</entry>
+							<entry>Fails</entry>
+							<entry></entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry>-</entry>
+							<entry>Fails</entry>
+							<entry>-</entry>
+						</row>
+						
+						<row>
+							<entry morerows="2">Signed</entry>
+							<entry morerows="2">No</entry>
+							<entry>Not enabled</entry>
+							<entry>Not enabled</entry>
+							<entry>Succeeds</entry>
+							<entry>Yes</entry>
+						</row>
+						<row>
+							<entry>Not enabled</entry>
+							<entry>Enabled</entry>
+							<entry>Fails</entry>
+							<entry>-</entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry>-</entry>
+							<entry>Fails</entry>
+							<entry>-</entry>
+						</row>
+						
+						<row>
+							<entry morerows="2">Signed</entry>
+							<entry morerows="2">Yes</entry>
+							<entry>Not enabled</entry>
+							<entry>Not enabled</entry>
+							<entry>Succeeds</entry>
+							<entry>No</entry>
+						</row>
+						<row>
+							<entry>Not enabled</entry>
+							<entry>Enabled</entry>
+							<entry>Succeeds</entry>
+							<entry>No</entry>
+						</row>
+						<row>
+							<entry>Enabled</entry>
+							<entry>-</entry>
+							<entry>Succeeds</entry>
+							<entry>No</entry>
+						</row>					
+					</tbody>
+					</tgroup>
+				</table>
+
+  			<para>
+  				Subsequent sections will describe how to generate a public and private X.509 key pair, how to use the private key to sign a kernel module, and how to enroll the public key into a source for the system key ring.
+				</para>
+  	</section>
+  	</section>
+  	
+  	<section id="sect-generating-a-public-private-x509-key-pair">
+  		<title>Generating a Public and Private X.509 Key Pair</title>
+  		<para>
+  			You need to generate a public and private X.509 key pair that will be used to sign a kernel module after it has been built. The corresponding public key will be used to authenticate the kernel module when it is loaded.
+  		</para>
+  		<procedure>
+  			<step>
+					<para>
+						The <command>openssl</command> tool can be used to generate a key pair that satisfies the requirements for kernel module signing in &PRODUCT;. Some of the parameters for this key generation request are best specified with a configuration file; follow the example below to create your own configuration file.</para>
+<screen>~]#&nbsp;<command>cat &lt;&lt; EOF &gt; <replaceable>configuration_file</replaceable>.config</command>
+[ req ]
+default_bits = 4096
+distinguished_name = req_distinguished_name
+prompt = no
+string_mask = utf8only
+x509_extensions = myexts
+
+[ req_distinguished_name ]
+O = <replaceable>Organization</replaceable>
+CN = <replaceable>Organization signing key</replaceable>
+emailAddress = <replaceable>E-mail address</replaceable>
+
+[ myexts ]
+basicConstraints=critical,CA:FALSE
+keyUsage=digitalSignature
+subjectKeyIdentifier=hash
+authorityKeyIdentifier=keyid
+EOF</screen>
+			</step>
+			<step>
+				<para>
+					After you have created the configuration file, you can create an X.509 public and private key pair. The public key will be written to the <filename><replaceable>public_key</replaceable>.der</filename> file and the private key will be written to the <filename><replaceable>private_key</replaceable>.priv</filename> file.
+				</para>
+
+<screen>~]#&nbsp;<command>openssl req -x509 -new -nodes -utf8 -sha256 -days 36500 \
+> -batch -config <replaceable>configuration_file</replaceable>.config -outform DER \
+> -out <replaceable>public_key</replaceable>.der \
+> -keyout <replaceable>private_key</replaceable>.priv</command></screen>		
+			</step>
+			<step>
+				<para>
+					Enroll your public key on all systems where you want to authenticate and load your kernel module.
+				</para>
+			</step>
+		</procedure>
+<warning>
+	<para>
+		Take proper care to guard the contents of your private key. In the wrong hands, the key could be used to compromise any system which has your public key.
+	</para>
+</warning>
+  	</section>
+  	
+  	<section id="sect-enrolling-public-key-on-target-system"><title>Enrolling Public Key on Target System</title>
+  		<para>
+  			When &PRODUCT; boots on a UEFI-based system with Secure Boot enabled, all keys that are in the Secure Boot db key database, but not in the dbx database of revoked keys, are loaded onto the system keyring by the kernel. The system keyring is used to authenticate kernel modules.
+  		</para>
+  		<section id="sect-factory-firmware-image-including-public-key"><title>Factory Firmware Image Including Public Key</title>
+  			<para>
+  				To facilitate authentication of your kernel module on your systems, consider requesting your system vendor to incorporate your public key into the UEFI Secure Boot key  database in their factory firmware image.
+  			</para>
+  		</section>
+  		<section id="sect-executable-key-enrollment-image-adding-public-key"><title>Executable Key Enrollment Image Adding Public Key</title>
+  			<para>
+  				It is possible to add a key to an existing populated and active Secure Boot key database. This can be done by writing and providing an EFI executable <emphasis>enrollment</emphasis> image. Such an enrollment image contains a properly formed request to append a key to the Secure Boot key database. This request must include data that is properly signed by the private key that corresponds to a public key that is already in the system's Secure Boot Key Exchange Key (KEK) database. Additionally, this EFI image must be signed by a private key that corresponds to a public key that is already in the key database.
+  			</para>
+  			<para>
+  				It is also possible to write an enrollment image that runs under &PRODUCT;. However, the &PRODUCT; image must be properly signed by a private key that corresponds to a public key that is already in the KEK database.
+  			</para>
+  			<para>
+  				The construction of either type of key enrollment images requires assistance from the platform vendor.
+  			</para>
+  		</section>
+  		<section id="sect-system-administrator-manually-adding-public-key-to-the-mok-list"><title>System Administrator Manually Adding Public Key to the MOK List</title>
+  			<para>
+  				The Machine Owner Key (MOK) facility is a feature that is supported by &PRODUCT; and can be used to augment the UEFI Secure Boot key database. When &PRODUCT; boots on a UEFI-enabled system with Secure Boot enabled, the keys on the MOK list are also added to the system keyring in addition to the keys from the key database. The MOK list keys are also stored persistently and securely in the same fashion as the Secure Boot key database keys, but these are two separate facilities. The MOK facility is supported by shim.efi, MokManager.efi, grubx64.efi, and the &PRODUCT; <command>mokutil</command> utility.
+  			</para>
+  			<para>
+  				The major capability provided by the MOK facility is the ability to add public keys to the MOK list without needing to have the key chain back to another key that is already in the KEK database. However, enrolling a MOK key requires manual interaction by a <emphasis>physically present</emphasis> user at the UEFI system console on each target system. Nevertheless, the MOK facility provides an excellent method for testing newly generated key pairs and testing kernel modules signed with them.
+  			</para>
+  			<para>
+  				Follow these steps to add your public key to the MOK list:
+  			</para>
+  			<procedure>
+  				<step>
+  					<para>
+  						Request addition of your public key to the MOK list using a &PRODUCT; userspace utility:
+  					</para>
+<screen>~]#&nbsp;<command>mokutil <option>--import</option> <filename>my_signing_key_pub.der</filename></command></screen>
+						<para>
+							You will be asked to enter and confirm a password for this MOK enrollment request.
+						</para>
+  				</step>
+  				<step>
+  					<para>
+  						Reboot the machine.
+  					</para>
+  				</step>
+  				<step>
+  					<para>
+  						The pending MOK key enrollment request will be noticed by <systemitem>shim.efi</systemitem> and it will launch <systemitem>MokManager.efi</systemitem> to allow you to complete the enrollment from the UEFI console. You will need to enter the password you previously associated with this request and confirm the enrollment. Your public key is added to the MOK list, which is persistent.
+  					</para>
+  				</step>
+  			</procedure>
+  		<para>
+  			Once a key is on the MOK list, it will be automatically propagated to the system key ring on this and subsequent boots when UEFI Secure Boot is enabled.
+  		</para>
+  		</section>
+  	</section>
+  		<section id="sect-signing-kernel-module-with-the-private-key">
+  			<title>Signing Kernel Module with the Private Key</title>
+  			<para>
+  				There are no extra steps required to prepare your kernel module for signing. You build your kernel module normally. Assuming an appropriate Makefile and corresponding sources, follow these steps to build your module and sign it:
+  			</para>
+  			
+  		<procedure>
+  			<step>
+  				<para>
+  					Build your <filename>my_module.ko</filename> module the standard way:
+  				</para>
+<screen>~]#&nbsp;<command>make -C /usr/src/kernels/$(uname -r) M=$PWD modules</command></screen>
+  			</step>
+  			<step>
+  				<para>
+  					Sign your kernel module with your private key. This is done with a Perl script. Note that the script requires that you provide both the files that contain your private and the public key as well as the kernel module file that you want to sign.
+  				</para>
+<screen>~]#&nbsp;<command>perl /usr/src/kernels/$(uname -r)/scripts/sign-file \
+> sha256 \
+> my_signing_key.priv \
+> my_signing_key_pub.der \
+> my_module.ko</command></screen>
+  			</step>
+  		</procedure>
+  			<para>
+  				Your kernel module is in ELF image format and this script computes and appends the signature directly to the ELF image in your <filename>my_module.ko</filename> file. The <command>modinfo</command> utility can be used to display information about the kernel module's signature, if it is present. For information on using the utility, see <xref linkend="sec-Displaying_Information_About_a_Module" />.
+  			</para>
+  			<para>
+  				Note that this appended signature is not contained in an ELF image section and is not a formal part of the ELF image. Therefore, tools such as <command>readelf</command> will not be able to display the signature on your kernel module.
+  			</para>
+  			<para>
+  				Your kernel module is now ready for loading. Note that your signed kernel module is also loadable on systems where UEFI Secure Boot is disabled or on a non-UEFI system. That means you do not need to provide both a signed and unsigned version of your kernel module.
+				</para>
+  		</section>
+  		<section id="sect-loading-signed-kernel-module">
+  			<title>Loading Signed Kernel Module</title>
+  			<para>
+  				Once your public key is enrolled and is in the system keyring, the normal kernel module loading mechanisms will work transparently. In the following example, you will use <command>mokutil</command> to add your public key to the MOK list and you will manually load your kernel module with <command>modprobe</command>.
+  			</para>
+  			<procedure>
+  				<step>
+  					<para>
+  						Optionally, you can verify that your kernel module will not load before you have enrolled your public key. First, verify what keys have been added to the system key ring on the current boot by running the <command>keyctl list %:.system_keyring</command> as root. Since your public key has not been enrolled yet, it should not be displayed in the output of the command.
+  					</para>
+  				</step>
+  				<step>
+  					<para>
+  						Request enrollment of your public key.
+  					</para>
+<screen>~]#&nbsp;<command>mokutil --import <replaceable>my_signing_key_pub</replaceable>.der</command></screen>
+  				</step>
+  				<step>
+  					<para>
+  						Reboot, and complete the enrollment at the UEFI console.
+  					</para>
+<screen>~]#&nbsp;<command>reboot</command></screen>
+  				</step>
+
+  				<step>
+  					<para>
+  						After the system reboots, verify the keys on the system key ring again.
+  					</para>
+<screen>~]#&nbsp;<command>keyctl list %:.system_keyring</command></screen>
+  				</step>
+  				<step>
+  					<para>
+  						You should now be able to load your kernel module successfully.
+  					</para>
+<screen>~]#&nbsp;<command>modprobe -v <replaceable>my_module</replaceable></command>
+insmod /lib/modules/3.17.4-302.fc21.x86_64/extra/<replaceable>my_module</replaceable>.ko
+~]#&nbsp;<command>lsmod | grep <replaceable>my_module</replaceable></command>
+<replaceable>my_module</replaceable> 12425 0</screen>
+					 </step>
+  			</procedure>
+  		</section>
+  </section>
+
   <section
     id="s1-kernel-modules-additional-resources">
     <title>Additional Resources</title>


More information about the docs-commits mailing list