b141171
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
46968b6
From: Daniel Axtens <dja@axtens.net>
46968b6
Date: Thu, 1 Oct 2020 13:02:09 +1000
b141171
Subject: [PATCH] appended signatures: documentation
b141171
b141171
This explains how appended signatures can be used to form part of
b141171
a secure boot chain, and documents the commands and variables
b141171
introduced.
b141171
b141171
Signed-off-by: Daniel Axtens <dja@axtens.net>
b141171
---
46968b6
 docs/grub.texi | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
46968b6
 1 file changed, 182 insertions(+), 17 deletions(-)
b141171
b141171
diff --git a/docs/grub.texi b/docs/grub.texi
e622855
index afbde7c1f7..4816be8561 100644
b141171
--- a/docs/grub.texi
b141171
+++ b/docs/grub.texi
13985b0
@@ -3214,6 +3214,7 @@ These variables have special meaning to GRUB.
b141171
 
b141171
 @menu
b141171
 * biosnum::
b141171
+* check_appended_signatures::
b141171
 * check_signatures::
b141171
 * chosen::
b141171
 * cmdpath::
13985b0
@@ -3273,11 +3274,18 @@ For an alternative approach which also changes BIOS drive mappings for the
b141171
 chain-loaded system, @pxref{drivemap}.
b141171
 
b141171
 
b141171
+@node check_appended_signatures
b141171
+@subsection check_appended_signatures
b141171
+
b141171
+This variable controls whether GRUB enforces appended signature validation on
b141171
+certain loaded files. @xref{Using appended signatures}.
b141171
+
b141171
+
b141171
 @node check_signatures
b141171
 @subsection check_signatures
b141171
 
b141171
-This variable controls whether GRUB enforces digital signature
b141171
-validation on loaded files. @xref{Using digital signatures}.
b141171
+This variable controls whether GRUB enforces GPG-style digital signature
b141171
+validation on loaded files. @xref{Using GPG-style digital signatures}.
b141171
 
b141171
 @node chosen
b141171
 @subsection chosen
46968b6
@@ -3994,6 +4002,7 @@ you forget a command, you can run the command @command{help}
b141171
 * date::                        Display or set current date and time
b141171
 * devicetree::                  Load a device tree blob
b141171
 * distrust::                    Remove a pubkey from trusted keys
b141171
+* distrust_certificate::        Remove a certificate from the list of trusted certificates
b141171
 * drivemap::                    Map a drive to another
b141171
 * echo::                        Display a line of text
b141171
 * eval::                        Evaluate agruments as GRUB commands
46968b6
@@ -4010,6 +4019,7 @@ you forget a command, you can run the command @command{help}
b141171
 * keystatus::                   Check key modifier status
b141171
 * linux::                       Load a Linux kernel
b141171
 * linux16::                     Load a Linux kernel (16-bit mode)
b141171
+* list_certificates::           List trusted certificates
b141171
 * list_env::                    List variables in environment block
b141171
 * list_trusted::                List trusted public keys
b141171
 * load_env::                    Load variables from environment block
46968b6
@@ -4047,8 +4057,10 @@ you forget a command, you can run the command @command{help}
b141171
 * test::                        Check file types and compare values
b141171
 * true::                        Do nothing, successfully
b141171
 * trust::                       Add public key to list of trusted keys
b141171
+* trust_certificate::           Add an x509 certificate to the list of trusted certificates
b141171
 * unset::                       Unset an environment variable
b141171
 @comment * vbeinfo::                     List available video modes
b141171
+* verify_appended::             Verify appended digital signature
b141171
 * verify_detached::             Verify detached digital signature
b141171
 * videoinfo::                   List available video modes
b141171
 @comment * xen_*::              Xen boot commands for AArch64
13985b0
@@ -4376,9 +4388,28 @@ These keys are used to validate signatures when environment variable
b141171
 @code{check_signatures} is set to @code{enforce}
b141171
 (@pxref{check_signatures}), and by some invocations of
b141171
 @command{verify_detached} (@pxref{verify_detached}).  @xref{Using
b141171
-digital signatures}, for more information.
b141171
+GPG-style digital signatures}, for more information.
b141171
 @end deffn
b141171
 
b141171
+
b141171
+@node distrust_certificate
b141171
+@subsection distrust_certificate
b141171
+
b141171
+@deffn Command distrust_certificate cert_number
b141171
+Remove the x509 certificate numbered @var{cert_number} from GRUB's keyring of
b141171
+trusted x509 certificates for verifying appended signatures.
b141171
+
b141171
+@var{cert_number} is the certificate number as listed by
b141171
+@command{list_certificates} (@pxref{list_certificates}).
b141171
+
b141171
+These certificates are used to validate appended signatures when environment
46968b6
+variable @code{check_appended_signatures} is set to @code{enforce} or
46968b6
+@code{forced} (@pxref{check_appended_signatures}), and by
46968b6
+@command{verify_appended} (@pxref{verify_appended}). See
46968b6
+@xref{Using appended signatures} for more information.
b141171
+@end deffn
b141171
+
b141171
+
b141171
 @node drivemap
b141171
 @subsection drivemap
b141171
 
13985b0
@@ -4636,6 +4667,21 @@ This command is only available on x86 systems.
b141171
 @end deffn
b141171
 
b141171
 
b141171
+@node list_certificates
b141171
+@subsection list_certificates
b141171
+
b141171
+@deffn Command list_certificates
b141171
+List all x509 certificates trusted by GRUB for validating appended signatures.
b141171
+The output is a numbered list of certificates, showing the certificate's serial
b141171
+number and Common Name.
b141171
+
b141171
+The certificate number can be used as an argument to
b141171
+@command{distrust_certificate} (@pxref{distrust_certificate}).
b141171
+
b141171
+See @xref{Using appended signatures} for more information.
b141171
+@end deffn
b141171
+
b141171
+
b141171
 @node list_env
b141171
 @subsection list_env
b141171
 
13985b0
@@ -4655,7 +4701,7 @@ The output is in GPG's v4 key fingerprint format (i.e., the output of
b141171
 @code{gpg --fingerprint}).  The least significant four bytes (last
b141171
 eight hexadecimal digits) can be used as an argument to
b141171
 @command{distrust} (@pxref{distrust}).
b141171
-@xref{Using digital signatures}, for more information about uses for
b141171
+@xref{Using GPG-style digital signatures}, for more information about uses for
b141171
 these keys.
b141171
 @end deffn
b141171
 
13985b0
@@ -4690,8 +4736,13 @@ When used with care, @option{--skip-sig} and the whitelist enable an
b141171
 administrator to configure a system to boot only signed
b141171
 configurations, but to allow the user to select from among multiple
b141171
 configurations, and to enable ``one-shot'' boot attempts and
b141171
-``savedefault'' behavior.  @xref{Using digital signatures}, for more
b141171
+``savedefault'' behavior.  @xref{Using GPG-style digital signatures}, for more
b141171
 information.
b141171
+
b141171
+Extra care should be taken when combining this command with appended signatures
b141171
+(@pxref{Using appended signatures}), as this file is not validated by an
46968b6
+appended signature and could set @code{check_appended_signatures=no} if GRUB is
46968b6
+not in @pxref{Lockdown} mode.
b141171
 @end deffn
b141171
 
b141171
 
13985b0
@@ -4987,7 +5038,7 @@ read.  It is possible to modify a digitally signed environment block
b141171
 file from within GRUB using this command, such that its signature will
b141171
 no longer be valid on subsequent boots.  Care should be taken in such
b141171
 advanced configurations to avoid rendering the system
b141171
-unbootable. @xref{Using digital signatures}, for more information.
b141171
+unbootable. @xref{Using GPG-style digital signatures}, for more information.
b141171
 @end deffn
b141171
 
b141171
 
13985b0
@@ -5387,11 +5438,32 @@ signatures when environment variable @code{check_signatures} is set to
b141171
 must itself be properly signed.  The @option{--skip-sig} option can be
b141171
 used to disable signature-checking when reading @var{pubkey_file}
b141171
 itself. It is expected that @option{--skip-sig} is useful for testing
b141171
-and manual booting. @xref{Using digital signatures}, for more
b141171
+and manual booting. @xref{Using GPG-style digital signatures}, for more
b141171
 information.
b141171
 @end deffn
b141171
 
b141171
 
b141171
+@node trust_certificate
b141171
+@subsection trust_certificate
b141171
+
b141171
+@deffn Command trust_certificate x509_certificate
b141171
+Read an DER-formatted x509 certificate from the file @var{x509_certificate}
b141171
+and add it to GRUB's internal list of trusted x509 certificates. These
b141171
+certificates are used to validate appended signatures when the environment
46968b6
+variable @code{check_appended_signatures} is set to @code{enforce} or
46968b6
+@code{forced}.
b141171
+
46968b6
+Note that if @code{check_appended_signatures} is set to @code{enforce} or
46968b6
+@code{forced} when @command{trust_certificate} is executed, then
46968b6
+@var{x509_certificate} must itself bear an appended signature. (It is not
46968b6
+sufficient that @var{x509_certificate} be signed by a trusted certificate
46968b6
+according to the x509 rules: grub does not include support for validating
46968b6
+signatures within x509 certificates themselves.)
b141171
+
b141171
+See @xref{Using appended signatures} for more information.
b141171
+@end deffn
b141171
+
b141171
+
b141171
 @node unset
b141171
 @subsection unset
b141171
 
13985b0
@@ -5410,6 +5482,18 @@ only on PC BIOS platforms.
b141171
 @end deffn
b141171
 @end ignore
b141171
 
b141171
+@node verify_appended
b141171
+@subsection verify_appended
b141171
+
b141171
+@deffn Command verify_appended file
b141171
+Verifies an appended signature on @var{file} against the trusted certificates
b141171
+known to GRUB (See @pxref{list_certificates}, @pxref{trust_certificate}, and
b141171
+@pxref{distrust_certificate}).
b141171
+
b141171
+Exit code @code{$?} is set to 0 if the signature validates
b141171
+successfully.  If validation fails, it is set to a non-zero value.
b141171
+See @xref{Using appended signatures}, for more information.
b141171
+@end deffn
b141171
 
b141171
 @node verify_detached
b141171
 @subsection verify_detached
13985b0
@@ -5428,7 +5512,7 @@ tried.
b141171
 
b141171
 Exit code @code{$?} is set to 0 if the signature validates
b141171
 successfully.  If validation fails, it is set to a non-zero value.
b141171
-@xref{Using digital signatures}, for more information.
b141171
+@xref{Using GPG-style digital signatures}, for more information.
b141171
 @end deffn
b141171
 
b141171
 @node videoinfo
13985b0
@@ -5811,13 +5895,14 @@ environment variables and commands are listed in the same order.
b141171
 @chapter Security
b141171
 
b141171
 @menu
b141171
-* Authentication and authorisation:: Users and access control
b141171
-* Using digital signatures::         Booting digitally signed code
b141171
-* UEFI secure boot and shim::        Booting digitally signed PE files
46968b6
-* Secure Boot Advanced Targeting::   Embedded information for generation number based revocation
b141171
-* Measured Boot::                    Measuring boot components
46968b6
-* Lockdown::                         Lockdown when booting on a secure setup
46968b6
-* Signing GRUB itself::              Ensuring the integrity of the GRUB core image
b141171
+* Authentication and authorisation::   Users and access control
b141171
+* Using GPG-style digital signatures:: Booting digitally signed code
b141171
+* Using appended signatures::          An alternative approach to booting digitally signed code
b141171
+* UEFI secure boot and shim::          Booting digitally signed PE files
46968b6
+* Secure Boot Advanced Targeting::     Embedded information for generation number based revocation
b141171
+* Measured Boot::                      Measuring boot components
46968b6
+* Lockdown::                           Lockdown when booting on a secure setup
46968b6
+* Signing GRUB itself::                Ensuring the integrity of the GRUB core image
b141171
 @end menu
b141171
 
b141171
 @node Authentication and authorisation
13985b0
@@ -5891,8 +5976,8 @@ generating configuration files with authentication.  You can use
b141171
 adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2}
b141171
 commands.
b141171
 
b141171
-@node Using digital signatures
b141171
-@section Using digital signatures in GRUB
b141171
+@node Using GPG-style digital signatures
b141171
+@section Using GPG-style digital signatures in GRUB
b141171
 
b141171
 GRUB's @file{core.img} can optionally provide enforcement that all files
b141171
 subsequently read from disk are covered by a valid digital signature.
13985b0
@@ -5985,6 +6070,86 @@ or BIOS) configuration to cause the machine to boot from a different
b141171
 (attacker-controlled) device.  GRUB is at best only one link in a
b141171
 secure boot chain.
b141171
 
b141171
+@node Using appended signatures
b141171
+@section Using appended signatures in GRUB
b141171
+
b141171
+GRUB supports verifying Linux-style 'appended signatures' for secure boot.
b141171
+Appended signatures are PKCS#7 messages containing a signature over the
b141171
+contents of a file, plus some metadata, appended to the end of a file. A file
b141171
+with an appended signature ends with the magic string:
b141171
+
b141171
+@example
b141171
+~Module signature appended~\n
b141171
+@end example
b141171
+
46968b6
+where @code{\n} represents the line-feed character, @code{0x0a}.
b141171
+
b141171
+Certificates can be managed at boot time using the @pxref{trust_certificate},
b141171
+@pxref{distrust_certificate} and @pxref{list_certificates} commands.
b141171
+Certificates can also be built in to the core image using the @code{--x509}
b141171
+parameter to @command{grub-install} or @command{grub-mkimage}.
b141171
+
b141171
+A file can be explictly verified using the @pxref{verify_appended} command.
b141171
+
b141171
+Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported,
b141171
+and only RSA signatures are supported.
b141171
+
b141171
+A file can be signed with the @command{sign-file} utility supplied with the
b141171
+Linux kernel source. For example, if you have @code{signing.key} as the private
b141171
+key and @code{certificate.der} as the x509 certificate containing the public key:
b141171
+
b141171
+@example
b141171
+sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed
b141171
+@end example
b141171
+
b141171
+Enforcement of signature verification is controlled by the
46968b6
+@code{check_appended_signatures} variable.
46968b6
+
46968b6
+@itemize
46968b6
+@item @samp{no}: no verification is performed. This is the default when GRUB
46968b6
+      is not in @pxref{Lockdown} mode.
46968b6
+@item @samp{enforce}: verification is performed. Verification can be disabled
46968b6
+      by setting the variable back to @samp{no}.
46968b6
+@item @samp{forced}: verification is performed and cannot be disabled. This is
46968b6
+      set when GRUB is in Lockdown when the appendedsig module is loaded.
46968b6
+@end itemize
b141171
+
b141171
+Unlike GPG-style signatures, not all files loaded by GRUB are required to be
46968b6
+signed. Once verification is turned on, the following file types will have
46968b6
+appended signatures verified:
b141171
+
46968b6
+@itemize
46968b6
+@item Linux kernels
46968b6
+@item GRUB modules, except those built into the core image
b141171
+@item Any new certificate files to be trusted
46968b6
+@end itemize
b141171
+
b141171
+ACPI tables and Device Tree images will not be checked for appended signatures
b141171
+but must be verified by another mechanism such as GPG-style signatures before
b141171
+they will be loaded.
b141171
+
46968b6
+Unless lockdown mode is enabled, signature checking does @strong{not}
b141171
+stop an attacker with console access from dropping manually to the GRUB
b141171
+console and executing:
b141171
+
b141171
+@example
b141171
+set check_appended_signatures=no
b141171
+@end example
b141171
+
b141171
+Refer to the section on password-protecting GRUB (@pxref{Authentication
b141171
+and authorisation}) for more information on preventing this.
b141171
+
46968b6
+Additionally, unless lockdown mode is enabled:
46968b6
+
46968b6
+@itemize
46968b6
+@item Special care must be taken around the @command{loadenv} command, which
46968b6
+      can be used to turn off @code{check_appended_signature}.
46968b6
+
46968b6
+@item If the grub configuration file is loaded from the disk, anyone who can
46968b6
+      modify the file on disk can turn off @code{check_appended_signature}.
46968b6
+      Consider embedding the configuration into the core grub image.
46968b6
+@end itemize
b141171
+
46968b6
 @node UEFI secure boot and shim
46968b6
 @section UEFI secure boot and shim support
b141171