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

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