nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0230-ibmvtpm-Add-support-for-trusted-boot-using-a-vTPM-2..patch

e28c09
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
e28c09
From: Stefan Berger <stefanb@linux.ibm.com>
e28c09
Date: Sun, 15 Mar 2020 12:37:10 -0400
e28c09
Subject: [PATCH] ibmvtpm: Add support for trusted boot using a vTPM 2.0
e28c09
e28c09
Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275
e28c09
PowerPC platform. With this patch grub now measures text and binary data
e28c09
into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform
e28c09
does.
e28c09
e28c09
This patch requires Daniel Axtens's patches for claiming more memory.
e28c09
e28c09
For vTPM support to work on PowerVM, system driver levels 1010.30
e28c09
or 1020.00 are required.
e28c09
e28c09
Note: Previous versions of firmware levels with the 2hash-ext-log
e28c09
API call have a bug that, once this API call is invoked, has the
e28c09
effect of disabling the vTPM driver under Linux causing an error
e28c09
message to be displayed in the Linux kernel log. Those users will
e28c09
have to update their machines to the firmware levels mentioned
e28c09
above.
e28c09
e28c09
Cc: Eric Snowberg <eric.snowberg@oracle.com>
e28c09
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
e28c09
(cherry picked from commit d3e5a8e6ecb8b87701135d97f45d27bbfbf731a2)
e28c09
---
e28c09
 grub-core/Makefile.core.def           |   7 ++
e28c09
 grub-core/commands/ieee1275/ibmvtpm.c | 152 ++++++++++++++++++++++++++++++++++
e28c09
 include/grub/ieee1275/ieee1275.h      |   3 +
e28c09
 docs/grub.texi                        |   3 +-
e28c09
 4 files changed, 164 insertions(+), 1 deletion(-)
e28c09
 create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
e28c09
e28c09
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
e28c09
index 97abc01f06..407d68f917 100644
e28c09
--- a/grub-core/Makefile.core.def
e28c09
+++ b/grub-core/Makefile.core.def
e28c09
@@ -1172,6 +1172,13 @@ module = {
e28c09
   enable = powerpc_ieee1275;
e28c09
 };
e28c09
 
e28c09
+module = {
e28c09
+  name = tpm;
e28c09
+  common = commands/tpm.c;
e28c09
+  ieee1275 = commands/ieee1275/ibmvtpm.c;
e28c09
+  enable = powerpc_ieee1275;
e28c09
+};
e28c09
+
e28c09
 module = {
e28c09
   name = terminal;
e28c09
   common = commands/terminal.c;
e28c09
diff --git a/grub-core/commands/ieee1275/ibmvtpm.c b/grub-core/commands/ieee1275/ibmvtpm.c
e28c09
new file mode 100644
e28c09
index 0000000000..e68b8448bc
e28c09
--- /dev/null
e28c09
+++ b/grub-core/commands/ieee1275/ibmvtpm.c
e28c09
@@ -0,0 +1,152 @@
e28c09
+/*
e28c09
+ *  GRUB  --  GRand Unified Bootloader
e28c09
+ *  Copyright (C) 2021  Free Software Foundation, Inc.
e28c09
+ *  Copyright (C) 2021  IBM Corporation
e28c09
+ *
e28c09
+ *  GRUB is free software: you can redistribute it and/or modify
e28c09
+ *  it under the terms of the GNU General Public License as published by
e28c09
+ *  the Free Software Foundation, either version 3 of the License, or
e28c09
+ *  (at your option) any later version.
e28c09
+ *
e28c09
+ *  GRUB is distributed in the hope that it will be useful,
e28c09
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
e28c09
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e28c09
+ *  GNU General Public License for more details.
e28c09
+ *
e28c09
+ *  You should have received a copy of the GNU General Public License
e28c09
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
e28c09
+ *
e28c09
+ *  IBM vTPM support code.
e28c09
+ */
e28c09
+
e28c09
+#include <grub/err.h>
e28c09
+#include <grub/types.h>
e28c09
+#include <grub/tpm.h>
e28c09
+#include <grub/ieee1275/ieee1275.h>
e28c09
+#include <grub/mm.h>
e28c09
+#include <grub/misc.h>
e28c09
+
e28c09
+static grub_ieee1275_ihandle_t tpm_ihandle;
e28c09
+static grub_uint8_t tpm_version;
e28c09
+
e28c09
+#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t)0)
e28c09
+
e28c09
+static void
e28c09
+tpm_get_tpm_version (void)
e28c09
+{
e28c09
+  grub_ieee1275_phandle_t vtpm;
e28c09
+  char buffer[20];
e28c09
+
e28c09
+  if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) &&
e28c09
+      !grub_ieee1275_get_property (vtpm, "compatible", buffer,
e28c09
+				   sizeof (buffer), NULL) &&
e28c09
+      !grub_strcmp (buffer, "IBM,vtpm20"))
e28c09
+    tpm_version = 2;
e28c09
+}
e28c09
+
e28c09
+static grub_err_t
e28c09
+tpm_init (void)
e28c09
+{
e28c09
+  static int init_success = 0;
e28c09
+
e28c09
+  if (!init_success)
e28c09
+    {
e28c09
+      if (grub_ieee1275_open ("/vdevice/vtpm", &tpm_ihandle) < 0) {
e28c09
+        tpm_ihandle = IEEE1275_IHANDLE_INVALID;
e28c09
+        return GRUB_ERR_UNKNOWN_DEVICE;
e28c09
+      }
e28c09
+
e28c09
+      init_success = 1;
e28c09
+
e28c09
+      tpm_get_tpm_version ();
e28c09
+    }
e28c09
+
e28c09
+  return GRUB_ERR_NONE;
e28c09
+}
e28c09
+
e28c09
+static int
e28c09
+ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex,
e28c09
+		       grub_uint32_t eventtype,
e28c09
+		       const char *description,
e28c09
+		       grub_size_t description_size,
e28c09
+		       void *buf, grub_size_t size)
e28c09
+{
e28c09
+  struct tpm_2hash_ext_log
e28c09
+  {
e28c09
+    struct grub_ieee1275_common_hdr common;
e28c09
+    grub_ieee1275_cell_t method;
e28c09
+    grub_ieee1275_cell_t ihandle;
e28c09
+    grub_ieee1275_cell_t size;
e28c09
+    grub_ieee1275_cell_t buf;
e28c09
+    grub_ieee1275_cell_t description_size;
e28c09
+    grub_ieee1275_cell_t description;
e28c09
+    grub_ieee1275_cell_t eventtype;
e28c09
+    grub_ieee1275_cell_t pcrindex;
e28c09
+    grub_ieee1275_cell_t catch_result;
e28c09
+    grub_ieee1275_cell_t rc;
e28c09
+  }
e28c09
+  args;
e28c09
+
e28c09
+  INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2);
e28c09
+  args.method = (grub_ieee1275_cell_t) "2hash-ext-log";
e28c09
+  args.ihandle = tpm_ihandle;
e28c09
+  args.pcrindex = pcrindex;
e28c09
+  args.eventtype = eventtype;
e28c09
+  args.description = (grub_ieee1275_cell_t) description;
e28c09
+  args.description_size = description_size;
e28c09
+  args.buf = (grub_ieee1275_cell_t) buf;
e28c09
+  args.size = (grub_ieee1275_cell_t) size;
e28c09
+
e28c09
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
e28c09
+    return -1;
e28c09
+
e28c09
+  /*
e28c09
+   * catch_result is set if firmware does not support 2hash-ext-log
e28c09
+   * rc is GRUB_IEEE1275_CELL_FALSE (0) on failure
e28c09
+   */
e28c09
+  if ((args.catch_result) || args.rc == GRUB_IEEE1275_CELL_FALSE)
e28c09
+    return -1;
e28c09
+
e28c09
+  return 0;
e28c09
+}
e28c09
+
e28c09
+static grub_err_t
e28c09
+tpm2_log_event (unsigned char *buf,
e28c09
+		grub_size_t size, grub_uint8_t pcr,
e28c09
+		const char *description)
e28c09
+{
e28c09
+  static int error_displayed = 0;
e28c09
+  int err;
e28c09
+
e28c09
+  err = ibmvtpm_2hash_ext_log (pcr, EV_IPL,
e28c09
+			       description,
e28c09
+			       grub_strlen(description) + 1,
e28c09
+			       buf, size);
e28c09
+  if (err && !error_displayed)
e28c09
+    {
e28c09
+      error_displayed++;
e28c09
+      return grub_error (GRUB_ERR_BAD_DEVICE,
e28c09
+	      "2HASH-EXT-LOG failed: Firmware is likely too old.\n");
e28c09
+    }
e28c09
+
e28c09
+  return GRUB_ERR_NONE;
e28c09
+}
e28c09
+
e28c09
+grub_err_t
e28c09
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
e28c09
+		  const char *description)
e28c09
+{
e28c09
+  grub_err_t err = tpm_init();
e28c09
+
e28c09
+  /* Absence of a TPM isn't a failure. */
e28c09
+  if (err != GRUB_ERR_NONE)
e28c09
+    return GRUB_ERR_NONE;
e28c09
+
e28c09
+  grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
e28c09
+		pcr, size, description);
e28c09
+
e28c09
+  if (tpm_version == 2)
e28c09
+    return tpm2_log_event (buf, size, pcr, description);
e28c09
+
e28c09
+  return GRUB_ERR_NONE;
e28c09
+}
e28c09
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
e28c09
index e0a6c2ce1e..f4c85265fe 100644
e28c09
--- a/include/grub/ieee1275/ieee1275.h
e28c09
+++ b/include/grub/ieee1275/ieee1275.h
e28c09
@@ -24,6 +24,9 @@
e28c09
 #include <grub/types.h>
e28c09
 #include <grub/machine/ieee1275.h>
e28c09
 
e28c09
+#define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
e28c09
+#define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
e28c09
+
e28c09
 struct grub_ieee1275_mem_region
e28c09
 {
e28c09
   unsigned int start;
e28c09
diff --git a/docs/grub.texi b/docs/grub.texi
e28c09
index a4da9c2a1b..c433240f34 100644
e28c09
--- a/docs/grub.texi
e28c09
+++ b/docs/grub.texi
e28c09
@@ -6221,7 +6221,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built
e28c09
 into @file{core.img} in order to avoid a potential gap in measurement between
e28c09
 @file{core.img} being loaded and the tpm module being loaded.
e28c09
 
e28c09
-Measured boot is currently only supported on EFI platforms.
e28c09
+Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC
e28c09
+platforms.
e28c09
 
e28c09
 @node Lockdown
e28c09
 @section Lockdown when booting on a secure setup