nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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