dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0371-ieee1275-link-appended-signature-enforcement-to-ibm-.patch

5975ab
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5975ab
From: Daniel Axtens <dja@axtens.net>
5975ab
Date: Mon, 28 Sep 2020 11:11:17 +1000
5975ab
Subject: [PATCH] ieee1275: link appended-signature enforcement to
5975ab
 /ibm,secure-boot
5975ab
5975ab
If the 'ibm,secure-boot' property of the root node is 2 or greater,
5975ab
require that the kernel pass appended-signature verification.
5975ab
5975ab
Do not consider the presence of a certificate to enforce verification.
5975ab
5975ab
Signed-off-by: Daniel Axtens <dja@axtens.net>
5975ab
---
5975ab
 grub-core/commands/appendedsig/appendedsig.c | 44 +++++++++++++++++++++-------
5975ab
 grub-core/kern/ieee1275/init.c               | 26 ++++++++++++++++
5975ab
 2 files changed, 60 insertions(+), 10 deletions(-)
5975ab
5975ab
diff --git a/grub-core/commands/appendedsig/appendedsig.c b/grub-core/commands/appendedsig/appendedsig.c
5975ab
index 5d8897be5c8..4ef2ec2893c 100644
5975ab
--- a/grub-core/commands/appendedsig/appendedsig.c
5975ab
+++ b/grub-core/commands/appendedsig/appendedsig.c
5975ab
@@ -95,10 +95,24 @@ static char *
5975ab
 grub_env_write_sec (struct grub_env_var *var __attribute__((unused)),
5975ab
 		    const char *val)
5975ab
 {
5975ab
+  if (check_sigs == 2)
5975ab
+    return grub_strdup ("forced");
5975ab
   check_sigs = (*val == '1') || (*val == 'e');
5975ab
   return grub_strdup (check_sigs ? "enforce" : "no");
5975ab
 }
5975ab
 
5975ab
+static const char *
5975ab
+grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),
5975ab
+                         const char *val __attribute__ ((unused)))
5975ab
+{
5975ab
+  if (check_sigs == 2)
5975ab
+    return "forced";
5975ab
+  else if (check_sigs == 1)
5975ab
+    return "enforce";
5975ab
+  else
5975ab
+    return "no";
5975ab
+}
5975ab
+
5975ab
 static grub_err_t
5975ab
 read_cert_from_file (grub_file_t f, struct x509_certificate *certificate)
5975ab
 {
5975ab
@@ -552,14 +566,20 @@ GRUB_MOD_INIT (appendedsig)
5975ab
   val = grub_env_get ("check_appended_signatures");
5975ab
   grub_dprintf ("appendedsig", "check_appended_signatures='%s'\n", val);
5975ab
 
5975ab
-  if (val && (val[0] == '1' || val[0] == 'e'))
5975ab
-    check_sigs = 1;
5975ab
-  else
5975ab
-    check_sigs = 0;
5975ab
+  if (val)
5975ab
+  {
5975ab
+    if (val[0] == '2' || val[0] == 'f')
5975ab
+      check_sigs = 2;
5975ab
+    else if (val[0] == '1' || val[0] == 'e')
5975ab
+      check_sigs = 1;
5975ab
+    else
5975ab
+      check_sigs = 0;
5975ab
+  }
5975ab
 
5975ab
   grub_trusted_key = NULL;
5975ab
 
5975ab
-  grub_register_variable_hook ("check_appended_signatures", 0,
5975ab
+  grub_register_variable_hook ("check_appended_signatures",
5975ab
+  			       grub_env_read_sec,
5975ab
 			       grub_env_write_sec);
5975ab
   grub_env_export ("check_appended_signatures");
5975ab
 
5975ab
@@ -603,11 +623,15 @@ GRUB_MOD_INIT (appendedsig)
5975ab
     grub_trusted_key = pk;
5975ab
   }
5975ab
 
5975ab
-  if (!val || val[0] == '\0')
5975ab
-    {
5975ab
-      grub_env_set ("check_appended_signatures",
5975ab
-		    grub_trusted_key ? "enforce" : "no");
5975ab
-    }
5975ab
+  /*
5975ab
+   * When controlled by ibm,secure-boot, we don't want the presence of
5975ab
+   * a certificate to enforce secure boot.
5975ab
+   * if (!val || val[0] == '\0')
5975ab
+   * {
5975ab
+   *    grub_env_set ("check_appended_signatures",
5975ab
+   *		      grub_trusted_key ? "enforce" : "no");
5975ab
+   * }
5975ab
+   */
5975ab
 
5975ab
   cmd_trust =
5975ab
     grub_register_command ("trust_certificate", grub_cmd_trust,
5975ab
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
5975ab
index f8a4f8f4214..137a343bca7 100644
5975ab
--- a/grub-core/kern/ieee1275/init.c
5975ab
+++ b/grub-core/kern/ieee1275/init.c
5975ab
@@ -266,6 +266,30 @@ grub_parse_cmdline (void)
5975ab
     }
5975ab
 }
5975ab
 
5975ab
+static void
5975ab
+grub_get_ieee1275_secure_boot (void)
5975ab
+{
5975ab
+  grub_ieee1275_phandle_t root;
5975ab
+  int rc;
5975ab
+  grub_uint32_t is_sb;
5975ab
+
5975ab
+  grub_ieee1275_finddevice ("/", &root);
5975ab
+
5975ab
+  rc = grub_ieee1275_get_integer_property (root, "ibm,secure-boot", &is_sb,
5975ab
+                                           sizeof (is_sb), 0);
5975ab
+
5975ab
+  /* ibm,secure-boot:
5975ab
+   * 0 - disabled
5975ab
+   * 1 - audit
5975ab
+   * 2 - enforce
5975ab
+   * 3 - enforce + OS-specific behaviour
5975ab
+   *
5975ab
+   * We only support enforce.
5975ab
+   */
5975ab
+  if (rc >= 0 && is_sb >= 2)
5975ab
+    grub_env_set("check_appended_signatures", "forced");
5975ab
+}
5975ab
+
5975ab
 grub_addr_t grub_modbase;
5975ab
 
5975ab
 void
5975ab
@@ -288,6 +312,8 @@ grub_machine_init (void)
5975ab
 #else
5975ab
   grub_install_get_time_ms (grub_rtc_get_time_ms);
5975ab
 #endif
5975ab
+
5975ab
+  grub_get_ieee1275_secure_boot ();
5975ab
 }
5975ab
 
5975ab
 void