All of lore.kernel.org
 help / color / mirror / Atom feed
* Add TPM support
@ 2017-06-15  0:20 Matthew Garrett
  2017-06-15  0:21 ` [PATCH 1/3] Move verifiers to the kernel Matthew Garrett
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Matthew Garrett @ 2017-06-15  0:20 UTC (permalink / raw)
  To: grub-devel

Port the TPM code to use the verifiers framework. I'm only including UEFI
support since it's still unclear what the copyright situation is over the
BIOS code, and demand for BIOS support for this has somewhat tailed off
anyway.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/3] Move verifiers to the kernel
  2017-06-15  0:20 Add TPM support Matthew Garrett
@ 2017-06-15  0:21 ` Matthew Garrett
  2017-06-15  0:21 ` [PATCH 2/3] Verify commands executed by grub Matthew Garrett
  2017-06-15  0:21 ` [PATCH 3/3] Core TPM support Matthew Garrett
  2 siblings, 0 replies; 9+ messages in thread
From: Matthew Garrett @ 2017-06-15  0:21 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

We want to be able to measure stuff right from the very beginning of
grub execution, so it makes sense for the core verifiers code to be
present in-kernel rather than having it as an external module.
---
 grub-core/Makefile.am                              |  1 +
 grub-core/Makefile.core.def                        |  6 +---
 grub-core/kern/main.c                              |  3 ++
 .../{commands/verify_helper.c => kern/verifiers.c} | 35 +++++++++++++++++-----
 include/grub/verify.h                              | 16 +++-------
 5 files changed, 36 insertions(+), 25 deletions(-)
 rename grub-core/{commands/verify_helper.c => kern/verifiers.c} (78%)

diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index 104513847..ef2b66e0f 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -93,6 +93,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
 
 if COND_i386_pc
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 16c4d0ea5..ab0f29960 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -129,6 +129,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
+  common = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
   noemu = kern/mm.c;
@@ -899,11 +900,6 @@ module = {
   cppflags = '-I$(srcdir)/lib/posix_wrap';
 };
 
-module = {
-  name = verify_helper;
-  common = commands/verify_helper.c;
-};
-
 module = {
   name = hdparm;
   common = commands/hdparm.c;
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
index 9cad0c448..b5a43c155 100644
--- a/grub-core/kern/main.c
+++ b/grub-core/kern/main.c
@@ -29,6 +29,7 @@
 #include <grub/command.h>
 #include <grub/reader.h>
 #include <grub/parser.h>
+#include <grub/verify.h>
 
 #ifdef GRUB_MACHINE_PCBIOS
 #include <grub/machine/memory.h>
@@ -274,6 +275,8 @@ grub_main (void)
   grub_printf ("Welcome to GRUB!\n\n");
   grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
 
+  grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verify_helper_open);
+
   grub_load_config ();
 
   grub_boot_time ("Before loading embedded modules.");
diff --git a/grub-core/commands/verify_helper.c b/grub-core/kern/verifiers.c
similarity index 78%
rename from grub-core/commands/verify_helper.c
rename to grub-core/kern/verifiers.c
index 5a55927e3..f8e47b009 100644
--- a/grub-core/commands/verify_helper.c
+++ b/grub-core/kern/verifiers.c
@@ -1,8 +1,25 @@
+/* verifiers.c - core verifiers support code */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <grub/file.h>
 #include <grub/verify.h>
-#include <grub/dl.h>
-
-GRUB_MOD_LICENSE ("GPLv3+");
+#include <grub/misc.h>
 
 struct grub_file_verifier *grub_file_verifiers;
 
@@ -55,7 +72,7 @@ struct grub_fs verified_fs =
   .close = verified_close
 };
 
-static grub_file_t
+grub_file_t
 grub_verify_helper_open (grub_file_t io, enum grub_file_type type)
 {
   grub_verified_t verified = 0;
@@ -175,12 +192,14 @@ grub_verify_string (char *str, enum grub_verify_string_type type)
   return GRUB_ERR_NONE;
 }
 
-GRUB_MOD_INIT(verify_helper)
+void
+grub_verifier_register (struct grub_file_verifier *ver)
 {
-  grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verify_helper_open);
+  grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST (ver));
 }
 
-GRUB_MOD_FINI(verify_helper)
+void
+grub_verifier_unregister (struct grub_file_verifier *ver)
 {
-  grub_file_filter_unregister (GRUB_FILE_FILTER_VERIFY);
+  grub_list_remove (GRUB_AS_LIST (ver));
 }
diff --git a/include/grub/verify.h b/include/grub/verify.h
index acab4f437..891036eb4 100644
--- a/include/grub/verify.h
+++ b/include/grub/verify.h
@@ -37,17 +37,9 @@ struct grub_file_verifier
 
 extern struct grub_file_verifier *grub_file_verifiers;
 
-static inline void
-grub_verifier_register (struct grub_file_verifier *ver)
-{
-  grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST (ver));
-}
-
-static inline void
-grub_verifier_unregister (struct grub_file_verifier *ver)
-{
-  grub_list_remove (GRUB_AS_LIST (ver));
-}
+grub_file_t grub_verify_helper_open(grub_file_t io, enum grub_file_type type);
 
+void EXPORT_FUNC(grub_verifier_register) (struct grub_file_verifier *ver);
+void EXPORT_FUNC(grub_verifier_unregister) (struct grub_file_verifier *ver);
 grub_err_t
-grub_verify_string (char *str, enum grub_verify_string_type type);
+EXPORT_FUNC(grub_verify_string) (char *str, enum grub_verify_string_type type);
-- 
2.13.1.518.g3df882009-goog



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/3] Verify commands executed by grub
  2017-06-15  0:20 Add TPM support Matthew Garrett
  2017-06-15  0:21 ` [PATCH 1/3] Move verifiers to the kernel Matthew Garrett
@ 2017-06-15  0:21 ` Matthew Garrett
  2017-06-15  0:21 ` [PATCH 3/3] Core TPM support Matthew Garrett
  2 siblings, 0 replies; 9+ messages in thread
From: Matthew Garrett @ 2017-06-15  0:21 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

Pass commands to the verification code. We want to be able to log these
in the TPM verification case.
---
 grub-core/script/execute.c | 27 ++++++++++++++++++++++++---
 include/grub/verify.h      |  1 +
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
index a8502d907..ee299fd0e 100644
--- a/grub-core/script/execute.c
+++ b/grub-core/script/execute.c
@@ -27,6 +27,7 @@
 #include <grub/normal.h>
 #include <grub/extcmd.h>
 #include <grub/i18n.h>
+#include <grub/verify.h>
 
 /* Max digits for a char is 3 (0xFF is 255), similarly for an int it
    is sizeof (int) * 3, and one extra for a possible -ve sign.  */
@@ -929,8 +930,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
   grub_err_t ret = 0;
   grub_script_function_t func = 0;
   char errnobuf[18];
-  char *cmdname;
-  int argc;
+  char *cmdname, *cmdstring;
+  int argc, offset = 0, cmdlen = 0;
+  unsigned int i;
   char **args;
   int invert;
   struct grub_script_argv argv = { 0, 0, 0 };
@@ -939,6 +941,26 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
   if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
     return grub_errno;
 
+  for (i = 0; i < argv.argc; i++)
+    {
+      cmdlen += grub_strlen (argv.args[i]) + 1;
+    }
+
+  cmdstring = grub_malloc (cmdlen);
+  if (!cmdstring)
+    {
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+			 N_("cannot allocate command buffer"));
+    }
+
+  for (i = 0; i < argv.argc; i++)
+    {
+      offset += grub_snprintf (cmdstring + offset, cmdlen - offset, "%s ",
+			       argv.args[i]);
+    }
+  cmdstring[cmdlen - 1] = '\0';
+  grub_verify_string (cmdstring, GRUB_VERIFY_COMMAND);
+  grub_free (cmdstring);
   invert = 0;
   argc = argv.argc - 1;
   args = argv.args + 1;
@@ -1163,4 +1185,3 @@ grub_script_execute (struct grub_script *script)
 
   return grub_script_execute_cmd (script->cmd);
 }
-
diff --git a/include/grub/verify.h b/include/grub/verify.h
index 891036eb4..317b6efaf 100644
--- a/include/grub/verify.h
+++ b/include/grub/verify.h
@@ -11,6 +11,7 @@ enum grub_verify_string_type
   {
     GRUB_VERIFY_KERNEL_CMDLINE,
     GRUB_VERIFY_MODULE_CMDLINE,
+    GRUB_VERIFY_COMMAND,
   };
 
 struct grub_file_verifier
-- 
2.13.1.518.g3df882009-goog



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/3] Core TPM support
  2017-06-15  0:20 Add TPM support Matthew Garrett
  2017-06-15  0:21 ` [PATCH 1/3] Move verifiers to the kernel Matthew Garrett
  2017-06-15  0:21 ` [PATCH 2/3] Verify commands executed by grub Matthew Garrett
@ 2017-06-15  0:21 ` Matthew Garrett
  2017-06-16 13:51   ` Javier Martinez Canillas
  2 siblings, 1 reply; 9+ messages in thread
From: Matthew Garrett @ 2017-06-15  0:21 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

Add support for performing basic TPM measurements. Right now this only
supports extending PCRs statically and only on UEFI.
---
 grub-core/Makefile.am          |   1 +
 grub-core/Makefile.core.def    |   2 +
 grub-core/kern/efi/tpm.c       | 282 +++++++++++++++++++++++++++++++++++++++++
 grub-core/kern/i386/efi/init.c |   2 +
 grub-core/kern/tpm.c           |  81 ++++++++++++
 include/grub/efi/tpm.h         | 153 ++++++++++++++++++++++
 include/grub/tpm.h             |  94 ++++++++++++++
 7 files changed, 615 insertions(+)
 create mode 100644 grub-core/kern/efi/tpm.c
 create mode 100644 grub-core/kern/tpm.c
 create mode 100644 include/grub/efi/tpm.h
 create mode 100644 include/grub/tpm.h

diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index ef2b66e0f..cc641bc36 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -92,6 +92,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
 
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index ab0f29960..2bdb6cafd 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -129,6 +129,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
+  common = kern/tpm.c;
   common = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
@@ -192,6 +193,7 @@ kernel = {
   efi = term/efi/console.c;
   efi = kern/acpi.c;
   efi = kern/efi/acpi.c;
+  efi = kern/efi/tpm.c;
   i386_coreboot = kern/i386/pc/acpi.c;
   i386_multiboot = kern/i386/pc/acpi.c;
   i386_coreboot = kern/acpi.c;
diff --git a/grub-core/kern/efi/tpm.c b/grub-core/kern/efi/tpm.c
new file mode 100644
index 000000000..c9fb3c133
--- /dev/null
+++ b/grub-core/kern/efi/tpm.c
@@ -0,0 +1,282 @@
+#include <grub/err.h>
+#include <grub/i18n.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/tpm.h>
+#include <grub/mm.h>
+#include <grub/tpm.h>
+#include <grub/term.h>
+
+static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
+static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
+
+static grub_efi_boolean_t grub_tpm_present(grub_efi_tpm_protocol_t *tpm)
+{
+  grub_efi_status_t status;
+  TCG_EFI_BOOT_SERVICE_CAPABILITY caps;
+  grub_uint32_t flags;
+  grub_efi_physical_address_t eventlog, lastevent;
+
+  caps.Size = (grub_uint8_t)sizeof(caps);
+
+  status = efi_call_5(tpm->status_check, tpm, &caps, &flags, &eventlog,
+		      &lastevent);
+
+  if (status != GRUB_EFI_SUCCESS || caps.TPMDeactivatedFlag
+      || !caps.TPMPresentFlag)
+    return 0;
+
+  return 1;
+}
+
+static grub_efi_boolean_t grub_tpm2_present(grub_efi_tpm2_protocol_t *tpm)
+{
+  grub_efi_status_t status;
+  EFI_TCG2_BOOT_SERVICE_CAPABILITY caps;
+
+  caps.Size = (grub_uint8_t)sizeof(caps);
+
+  status = efi_call_2(tpm->get_capability, tpm, &caps);
+
+  if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
+    return 0;
+
+  return 1;
+}
+
+static grub_efi_boolean_t grub_tpm_handle_find(grub_efi_handle_t *tpm_handle,
+					       grub_efi_uint8_t *protocol_version)
+{
+  grub_efi_handle_t *handles;
+  grub_efi_uintn_t num_handles;
+
+  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm_guid, NULL,
+				    &num_handles);
+  if (handles && num_handles > 0) {
+    *tpm_handle = handles[0];
+    *protocol_version = 1;
+    return 1;
+  }
+
+  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm2_guid, NULL,
+				    &num_handles);
+  if (handles && num_handles > 0) {
+    *tpm_handle = handles[0];
+    *protocol_version = 2;
+    return 1;
+  }
+
+  return 0;
+}
+
+static grub_err_t
+grub_tpm1_execute(grub_efi_handle_t tpm_handle,
+		  PassThroughToTPM_InputParamBlock *inbuf,
+		  PassThroughToTPM_OutputParamBlock *outbuf)
+{
+  grub_efi_status_t status;
+  grub_efi_tpm_protocol_t *tpm;
+  grub_uint32_t inhdrsize = sizeof(*inbuf) - sizeof(inbuf->TPMOperandIn);
+  grub_uint32_t outhdrsize = sizeof(*outbuf) - sizeof(outbuf->TPMOperandOut);
+
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+  if (!grub_tpm_present(tpm))
+    return 0;
+
+  /* UEFI TPM protocol takes the raw operand block, no param block header */
+  status = efi_call_5 (tpm->pass_through_to_tpm, tpm,
+		       inbuf->IPBLength - inhdrsize, inbuf->TPMOperandIn,
+		       outbuf->OPBLength - outhdrsize, outbuf->TPMOperandOut);
+
+  switch (status) {
+  case GRUB_EFI_SUCCESS:
+    return 0;
+  case GRUB_EFI_DEVICE_ERROR:
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
+  case GRUB_EFI_INVALID_PARAMETER:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
+  case GRUB_EFI_BUFFER_TOO_SMALL:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
+  case GRUB_EFI_NOT_FOUND:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
+  default:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
+  }
+}
+
+static grub_err_t
+grub_tpm2_execute(grub_efi_handle_t tpm_handle,
+		  PassThroughToTPM_InputParamBlock *inbuf,
+		  PassThroughToTPM_OutputParamBlock *outbuf)
+{
+  grub_efi_status_t status;
+  grub_efi_tpm2_protocol_t *tpm;
+  grub_uint32_t inhdrsize = sizeof(*inbuf) - sizeof(inbuf->TPMOperandIn);
+  grub_uint32_t outhdrsize = sizeof(*outbuf) - sizeof(outbuf->TPMOperandOut);
+
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+  if (!grub_tpm2_present(tpm))
+    return 0;
+
+  /* UEFI TPM protocol takes the raw operand block, no param block header */
+  status = efi_call_5 (tpm->submit_command, tpm,
+		       inbuf->IPBLength - inhdrsize, inbuf->TPMOperandIn,
+		       outbuf->OPBLength - outhdrsize, outbuf->TPMOperandOut);
+
+  switch (status) {
+  case GRUB_EFI_SUCCESS:
+    return 0;
+  case GRUB_EFI_DEVICE_ERROR:
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
+  case GRUB_EFI_INVALID_PARAMETER:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
+  case GRUB_EFI_BUFFER_TOO_SMALL:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
+  case GRUB_EFI_NOT_FOUND:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
+  default:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
+  }
+}
+
+grub_err_t
+grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
+		 PassThroughToTPM_OutputParamBlock *outbuf)
+{
+  grub_efi_handle_t tpm_handle;
+   grub_uint8_t protocol_version;
+
+  /* It's not a hard failure for there to be no TPM */
+  if (!grub_tpm_handle_find(&tpm_handle, &protocol_version))
+    return 0;
+
+  if (protocol_version == 1) {
+    return grub_tpm1_execute(tpm_handle, inbuf, outbuf);
+  } else {
+    return grub_tpm2_execute(tpm_handle, inbuf, outbuf);
+  }
+}
+
+typedef struct {
+	grub_uint32_t pcrindex;
+	grub_uint32_t eventtype;
+	grub_uint8_t digest[20];
+	grub_uint32_t eventsize;
+	grub_uint8_t event[1];
+} Event;
+
+
+static grub_err_t
+grub_tpm1_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
+		    grub_size_t size, grub_uint8_t pcr,
+		    const char *description)
+{
+  Event *event;
+  grub_efi_status_t status;
+  grub_efi_tpm_protocol_t *tpm;
+  grub_efi_physical_address_t lastevent;
+  grub_uint32_t algorithm;
+  grub_uint32_t eventnum = 0;
+
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+  if (!grub_tpm_present(tpm))
+    return 0;
+
+  event = grub_zalloc(sizeof (Event) + grub_strlen(description) + 1);
+  if (!event)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+		       N_("cannot allocate TPM event buffer"));
+
+  event->pcrindex = pcr;
+  event->eventtype = EV_IPL;
+  event->eventsize = grub_strlen(description) + 1;
+  grub_memcpy(event->event, description, event->eventsize);
+
+  algorithm = TCG_ALG_SHA;
+  status = efi_call_7 (tpm->log_extend_event, tpm, buf, (grub_uint64_t) size,
+		       algorithm, event, &eventnum, &lastevent);
+
+  switch (status) {
+  case GRUB_EFI_SUCCESS:
+    return 0;
+  case GRUB_EFI_DEVICE_ERROR:
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
+  case GRUB_EFI_INVALID_PARAMETER:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
+  case GRUB_EFI_BUFFER_TOO_SMALL:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
+  case GRUB_EFI_NOT_FOUND:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
+  default:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
+  }
+}
+
+static grub_err_t
+grub_tpm2_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
+		   grub_size_t size, grub_uint8_t pcr,
+		   const char *description)
+{
+  EFI_TCG2_EVENT *event;
+  grub_efi_status_t status;
+  grub_efi_tpm2_protocol_t *tpm;
+
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+  if (!grub_tpm2_present(tpm))
+    return 0;
+
+  event = grub_zalloc(sizeof (EFI_TCG2_EVENT) + grub_strlen(description) + 1);
+  if (!event)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+		       N_("cannot allocate TPM event buffer"));
+
+  event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
+  event->Header.HeaderVersion = 1;
+  event->Header.PCRIndex = pcr;
+  event->Header.EventType = EV_IPL;
+  event->Size = sizeof(*event) - sizeof(event->Event) + grub_strlen(description) + 1;
+  grub_memcpy(event->Event, description, grub_strlen(description) + 1);
+
+  status = efi_call_5 (tpm->hash_log_extend_event, tpm, 0, buf,
+		       (grub_uint64_t) size, event);
+
+  switch (status) {
+  case GRUB_EFI_SUCCESS:
+    return 0;
+  case GRUB_EFI_DEVICE_ERROR:
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
+  case GRUB_EFI_INVALID_PARAMETER:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
+  case GRUB_EFI_BUFFER_TOO_SMALL:
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
+  case GRUB_EFI_NOT_FOUND:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
+  default:
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
+  }
+}
+
+grub_err_t
+grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+		   const char *description)
+{
+  grub_efi_handle_t tpm_handle;
+  grub_efi_uint8_t protocol_version;
+
+  if (!grub_tpm_handle_find(&tpm_handle, &protocol_version))
+    return 0;
+
+  if (protocol_version == 1) {
+    return grub_tpm1_log_event(tpm_handle, buf, size, pcr, description);
+  } else {
+    return grub_tpm2_log_event(tpm_handle, buf, size, pcr, description);
+  }
+}
diff --git a/grub-core/kern/i386/efi/init.c b/grub-core/kern/i386/efi/init.c
index a28316cc6..7306df91e 100644
--- a/grub-core/kern/i386/efi/init.c
+++ b/grub-core/kern/i386/efi/init.c
@@ -27,12 +27,14 @@
 #include <grub/efi/efi.h>
 #include <grub/i386/tsc.h>
 #include <grub/loader.h>
+#include <grub/tpm.h>
 
 void
 grub_machine_init (void)
 {
   grub_efi_init ();
   grub_tsc_init ();
+  grub_tpm_init ();
 }
 
 void
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
new file mode 100644
index 000000000..62972ec36
--- /dev/null
+++ b/grub-core/kern/tpm.c
@@ -0,0 +1,81 @@
+#include <grub/err.h>
+#include <grub/i18n.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/tpm.h>
+#include <grub/term.h>
+#include <grub/verify.h>
+
+grub_err_t
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+		  const char *description)
+{
+  return grub_tpm_log_event (buf, size, pcr, description);
+}
+
+static grub_err_t
+grub_tpm_verify_init (grub_file_t io,
+		      enum grub_file_type type __attribute__ ((unused)),
+		      void **context, enum grub_verify_flags *flags)
+{
+  *context = io->name;
+  *flags |= GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_tpm_verify_write (void *context, void *buf, grub_size_t size)
+{
+  return grub_tpm_measure (buf, size, 9, context);
+}
+
+static void
+grub_tpm_verify_close (void *ctxt __attribute__ ((unused)))
+{
+  return;
+}
+
+static grub_err_t
+grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
+{
+  const char *prefix = NULL;
+  char *description;
+  grub_err_t status;
+
+  switch (type)
+    {
+    case GRUB_VERIFY_KERNEL_CMDLINE:
+      prefix = "kernel_cmdline: ";
+      break;
+    case GRUB_VERIFY_MODULE_CMDLINE:
+      prefix = "module_cmdline: ";
+      break;
+    case GRUB_VERIFY_COMMAND:
+      prefix = "grub_cmd: ";
+      break;
+    }
+  description = grub_malloc(grub_strlen(str) + grub_strlen(prefix) + 1);
+  if (!description)
+    return grub_errno;
+  grub_memcpy(description, prefix, grub_strlen(prefix));
+  grub_memcpy(description + grub_strlen(prefix), str, grub_strlen(str) + 1);
+  status = grub_tpm_measure ((unsigned char *) str, grub_strlen (str), 8,
+			     description);
+  grub_free(description);
+  return status;
+}
+
+struct grub_file_verifier grub_tpm_verifier = {
+  .name = "tpm",
+  .init = grub_tpm_verify_init,
+  .write = grub_tpm_verify_write,
+  .close = grub_tpm_verify_close,
+  .verify_string = grub_tpm_verify_string,
+};
+
+grub_err_t
+grub_tpm_init (void)
+{
+  grub_verifier_register (&grub_tpm_verifier);
+  return GRUB_ERR_NONE;
+}
diff --git a/include/grub/efi/tpm.h b/include/grub/efi/tpm.h
new file mode 100644
index 000000000..e2aff4a3c
--- /dev/null
+++ b/include/grub/efi/tpm.h
@@ -0,0 +1,153 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_EFI_TPM_HEADER
+#define GRUB_EFI_TPM_HEADER 1
+
+#define EFI_TPM_GUID {0xf541796d, 0xa62e, 0x4954, {0xa7, 0x75, 0x95, 0x84, 0xf6, 0x1b, 0x9c, 0xdd }};
+#define EFI_TPM2_GUID {0x607f766c, 0x7455, 0x42be, {0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f }};
+
+typedef struct {
+  grub_efi_uint8_t Major;
+  grub_efi_uint8_t Minor;
+  grub_efi_uint8_t RevMajor;
+  grub_efi_uint8_t RevMinor;
+} TCG_VERSION;
+
+typedef struct _TCG_EFI_BOOT_SERVICE_CAPABILITY {
+  grub_efi_uint8_t          Size;                /// Size of this structure.
+  TCG_VERSION    StructureVersion;
+  TCG_VERSION    ProtocolSpecVersion;
+  grub_efi_uint8_t          HashAlgorithmBitmap; /// Hash algorithms .
+  char        TPMPresentFlag;      /// 00h = TPM not present.
+  char        TPMDeactivatedFlag;  /// 01h = TPM currently deactivated.
+} TCG_EFI_BOOT_SERVICE_CAPABILITY;
+
+typedef struct {
+  grub_efi_uint32_t PCRIndex;
+  grub_efi_uint32_t EventType;
+  grub_efi_uint8_t digest[20];
+  grub_efi_uint32_t EventSize;
+  grub_efi_uint8_t  Event[1];
+} TCG_PCR_EVENT;
+
+struct grub_efi_tpm_protocol
+{
+  grub_efi_status_t (*status_check) (struct grub_efi_tpm_protocol *this,
+				     TCG_EFI_BOOT_SERVICE_CAPABILITY *ProtocolCapability,
+				     grub_efi_uint32_t *TCGFeatureFlags,
+				     grub_efi_physical_address_t *EventLogLocation,
+				     grub_efi_physical_address_t *EventLogLastEntry);
+  grub_efi_status_t (*hash_all) (struct grub_efi_tpm_protocol *this,
+				 grub_efi_uint8_t *HashData,
+				 grub_efi_uint64_t HashLen,
+				 grub_efi_uint32_t AlgorithmId,
+				 grub_efi_uint64_t *HashedDataLen,
+				 grub_efi_uint8_t **HashedDataResult);
+  grub_efi_status_t (*log_event) (struct grub_efi_tpm_protocol *this,
+				  TCG_PCR_EVENT *TCGLogData,
+				  grub_efi_uint32_t *EventNumber,
+				  grub_efi_uint32_t Flags);
+  grub_efi_status_t (*pass_through_to_tpm) (struct grub_efi_tpm_protocol *this,
+					    grub_efi_uint32_t TpmInputParameterBlockSize,
+					    grub_efi_uint8_t *TpmInputParameterBlock,
+					    grub_efi_uint32_t TpmOutputParameterBlockSize,
+					    grub_efi_uint8_t *TpmOutputParameterBlock);
+  grub_efi_status_t (*log_extend_event) (struct grub_efi_tpm_protocol *this,
+					 grub_efi_physical_address_t HashData,
+					 grub_efi_uint64_t HashDataLen,
+					 grub_efi_uint32_t AlgorithmId,
+					 TCG_PCR_EVENT *TCGLogData,
+					 grub_efi_uint32_t *EventNumber,
+					 grub_efi_physical_address_t *EventLogLastEntry);
+};
+
+typedef struct grub_efi_tpm_protocol grub_efi_tpm_protocol_t;
+
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_LOG_BITMAP;
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_LOG_FORMAT;
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_ALGORITHM_BITMAP;
+
+typedef struct tdEFI_TCG2_VERSION {
+  grub_efi_uint8_t Major;
+  grub_efi_uint8_t Minor;
+} GRUB_PACKED EFI_TCG2_VERSION;
+
+typedef struct tdEFI_TCG2_BOOT_SERVICE_CAPABILITY {
+  grub_efi_uint8_t Size;
+  EFI_TCG2_VERSION StructureVersion;
+  EFI_TCG2_VERSION ProtocolVersion;
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap;
+  EFI_TCG2_EVENT_LOG_BITMAP SupportedEventLogs;
+  grub_efi_boolean_t TPMPresentFlag;
+  grub_efi_uint16_t MaxCommandSize;
+  grub_efi_uint16_t MaxResponseSize;
+  grub_efi_uint32_t ManufacturerID;
+  grub_efi_uint32_t NumberOfPcrBanks;
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP ActivePcrBanks;
+} EFI_TCG2_BOOT_SERVICE_CAPABILITY;
+
+typedef grub_efi_uint32_t TCG_PCRINDEX;
+typedef grub_efi_uint32_t TCG_EVENTTYPE;
+
+typedef struct tdEFI_TCG2_EVENT_HEADER {
+  grub_efi_uint32_t HeaderSize;
+  grub_efi_uint16_t HeaderVersion;
+  TCG_PCRINDEX PCRIndex;
+  TCG_EVENTTYPE EventType;
+} GRUB_PACKED EFI_TCG2_EVENT_HEADER;
+
+typedef struct tdEFI_TCG2_EVENT {
+  grub_efi_uint32_t Size;
+  EFI_TCG2_EVENT_HEADER Header;
+  grub_efi_uint8_t Event[1];
+} GRUB_PACKED EFI_TCG2_EVENT;
+
+struct grub_efi_tpm2_protocol
+{
+  grub_efi_status_t (*get_capability) (struct grub_efi_tpm2_protocol *this,
+				       EFI_TCG2_BOOT_SERVICE_CAPABILITY *ProtocolCapability);
+  grub_efi_status_t (*get_event_log) (struct grub_efi_tpm2_protocol *this,
+				      EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
+				      grub_efi_physical_address_t *EventLogLocation,
+				      grub_efi_physical_address_t *EventLogLastEntry,
+				      grub_efi_boolean_t *EventLogTruncated);
+  grub_efi_status_t (*hash_log_extend_event) (struct grub_efi_tpm2_protocol *this,
+					      grub_efi_uint64_t Flags,
+					      grub_efi_physical_address_t *DataToHash,
+					      grub_efi_uint64_t DataToHashLen,
+					      EFI_TCG2_EVENT *EfiTcgEvent);
+  grub_efi_status_t (*submit_command) (struct grub_efi_tpm2_protocol *this,
+				       grub_efi_uint32_t InputParameterBlockSize,
+				       grub_efi_uint8_t *InputParameterBlock,
+				       grub_efi_uint32_t OutputParameterBlockSize,
+				       grub_efi_uint8_t *OutputParameterBlock);
+  grub_efi_status_t (*get_active_pcr_blanks) (struct grub_efi_tpm2_protocol *this,
+					      grub_efi_uint32_t *ActivePcrBanks);
+  grub_efi_status_t (*set_active_pcr_banks) (struct grub_efi_tpm2_protocol *this,
+					     grub_efi_uint32_t ActivePcrBanks);
+  grub_efi_status_t (*get_result_of_set_active_pcr_banks) (struct grub_efi_tpm2_protocol *this,
+							   grub_efi_uint32_t *OperationPresent,
+							   grub_efi_uint32_t *Response);
+};
+
+typedef struct grub_efi_tpm2_protocol grub_efi_tpm2_protocol_t;
+
+#define TCG_ALG_SHA 0x00000004
+
+#endif
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
new file mode 100644
index 000000000..dd905b829
--- /dev/null
+++ b/include/grub/tpm.h
@@ -0,0 +1,94 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_TPM_HEADER
+#define GRUB_TPM_HEADER 1
+
+#define SHA1_DIGEST_SIZE 20
+
+#define TPM_BASE 0x0
+#define TPM_SUCCESS TPM_BASE
+#define TPM_AUTHFAIL (TPM_BASE + 0x1)
+#define TPM_BADINDEX (TPM_BASE + 0x2)
+
+#define TPM_TAG_RQU_COMMAND 0x00C1
+#define TPM_ORD_Extend 0x14
+
+#define EV_IPL 0x0d
+
+/* TCG_PassThroughToTPM Input Parameter Block */
+typedef struct {
+        grub_uint16_t IPBLength;
+        grub_uint16_t Reserved1;
+        grub_uint16_t OPBLength;
+        grub_uint16_t Reserved2;
+        grub_uint8_t TPMOperandIn[1];
+} GRUB_PACKED PassThroughToTPM_InputParamBlock;
+
+/* TCG_PassThroughToTPM Output Parameter Block */
+typedef struct {
+        grub_uint16_t OPBLength;
+        grub_uint16_t Reserved;
+        grub_uint8_t TPMOperandOut[1];
+} GRUB_PACKED PassThroughToTPM_OutputParamBlock;
+
+typedef struct {
+        grub_uint16_t tag;
+        grub_uint32_t paramSize;
+        grub_uint32_t ordinal;
+        grub_uint32_t pcrNum;
+        grub_uint8_t inDigest[SHA1_DIGEST_SIZE];                /* The 160 bit value representing the event to be recorded. */
+} GRUB_PACKED ExtendIncoming;
+
+/* TPM_Extend Outgoing Operand */
+typedef struct {
+        grub_uint16_t tag;
+        grub_uint32_t paramSize;
+        grub_uint32_t returnCode;
+        grub_uint8_t outDigest[SHA1_DIGEST_SIZE];               /* The PCR value after execution of the command. */
+} GRUB_PACKED ExtendOutgoing;
+
+grub_err_t EXPORT_FUNC(grub_tpm_measure) (unsigned char *buf, grub_size_t size,
+					  grub_uint8_t pcr,
+					  const char *description);
+#if defined (GRUB_MACHINE_EFI)
+grub_err_t grub_tpm_init(void);
+grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
+			    PassThroughToTPM_OutputParamBlock *outbuf);
+grub_err_t grub_tpm_log_event(unsigned char *buf, grub_size_t size,
+			      grub_uint8_t pcr, const char *description);
+#else
+static inline grub_err_t grub_tpm_init(void) {};
+
+static inline grub_err_t grub_tpm_execute(
+	PassThroughToTPM_InputParamBlock *inbuf __attribute__ ((unused)),
+	PassThroughToTPM_OutputParamBlock *outbuf __attribute__ ((unused)))
+{
+	return 0;
+};
+static inline grub_err_t grub_tpm_log_event(
+	unsigned char *buf __attribute__ ((unused)),
+	grub_size_t size __attribute__ ((unused)),
+	grub_uint8_t pcr __attribute__ ((unused)),
+	const char *description __attribute__ ((unused)))
+{
+	return 0;
+};
+#endif
+
+#endif
-- 
2.13.1.518.g3df882009-goog



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3] Core TPM support
  2017-06-15  0:21 ` [PATCH 3/3] Core TPM support Matthew Garrett
@ 2017-06-16 13:51   ` Javier Martinez Canillas
  2017-06-19  3:38     ` Matthew Garrett
  2017-06-21 11:46     ` Javier Martinez Canillas
  0 siblings, 2 replies; 9+ messages in thread
From: Javier Martinez Canillas @ 2017-06-16 13:51 UTC (permalink / raw)
  To: The development of GNU GRUB, Matthew Garrett

Hello Matthew,

I've tested your patches (plus the verify framework) today on a Lenovo
Thinkpad X1 Carbon with 2 different setups:

a) Infineon TPM1.2 chip

b) Intel PTT firmware-based TPM2.0

It works correctly in both cases, there are measurements made by grub2
on both PCR{8,9} as expected:

(a) $ grep PCR-0[8,9] /sys/devices/pnp0/00:08/pcrs

     PCR-08: 37 71 AD AB A9 10 83 D9 B2 63 B1 27 41 E6 33 F5 42 88 96 94 
     PCR-09: 18 46 A1 D9 31 D0 C4 66 FA 26 78 A2 B2 BA AF 80 E8 0E 8A 5D

(b) $ tpm2_listpcrs -L 0x4:8,9

    Bank/Algorithm: TPM_ALG_SHA1(0x0004)
    PCR_08: 37 71 ad ab a9 10 83 d9 b2 63 b1 27 41 e6 33 f5 42 88 96 94
    PCR_09: 18 46 a1 d9 31 d0 c4 66 fa 26 78 a2 b2 ba af 80 e8 0e 8a 5d

Before your patches, I only saw the measurement made by shim on PCR9:

(a) $ grep PCR-0[8,9] /sys/devices/pnp0/00:08/pcrs

    PCR-08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    PCR-09: C9 EC 5F CF D2 1C 25 F0 EA 9D DF 51 FF 0C BE 20 3A 93 4E 2D

(b) $ tpm2_listpcrs -L 0x4:8,9

    Bank/Algorithm: TPM_ALG_SHA1(0x0004)
    PCR_08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    PCR_09: c9 ec 5f cf d2 1c 25 f0 ea 9d df 51 ff 0c be 20 3a 93 4e 2d

I've a couple of questions though, I'm new to TPM and trusted computing
in general so please forgive me if I say something wrong/silly :)

On 06/15/2017 02:21 AM, Matthew Garrett wrote:

> +
> +static grub_efi_boolean_t grub_tpm2_present(grub_efi_tpm2_protocol_t *tpm)
> +{
> +  grub_efi_status_t status;
> +  EFI_TCG2_BOOT_SERVICE_CAPABILITY caps;
> +
> +  caps.Size = (grub_uint8_t)sizeof(caps);
> +
> +  status = efi_call_2(tpm->get_capability, tpm, &caps);
> +
> +  if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
> +    return 0;
> +

The TCG EFI Protocol Specification (rev 00.13, March 2016) mentions that the
tdEFI_TCG2_BOOT_SERVICE_CAPABILITY StructureVersion Major and Minor should
be checked to determine the EFI_TCG2_BOOT_SERVICE_CAPABILITY struct version.

In fact, shim checks for this and instead use tdTREE_BOOT_SERVICE_CAPABILITY
if Major == 1 && Minor == 0. The EFI firmware on my Lenovo X1 Carbon reports
these values for the FW TPM2.0 so I first was expecting the code to fail. But
it works and I now see that the structures layout are equal so doesn't matter.

Do you think that we should be more strict on this? Or instead the shim code
could be simplified as you did here and avoid distinguish between the two?

> +
> +static grub_err_t
> +grub_tpm2_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
> +		   grub_size_t size, grub_uint8_t pcr,
> +		   const char *description)
> +{
> +  EFI_TCG2_EVENT *event;
> +  grub_efi_status_t status;
> +  grub_efi_tpm2_protocol_t *tpm;
> +
> +  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
> +				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> +
> +  if (!grub_tpm2_present(tpm))
> +    return 0;
> +
> +  event = grub_zalloc(sizeof (EFI_TCG2_EVENT) + grub_strlen(description) + 1);
> +  if (!event)
> +    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
> +		       N_("cannot allocate TPM event buffer"));
> +
> +  event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
> +  event->Header.HeaderVersion = 1;
> +  event->Header.PCRIndex = pcr;
> +  event->Header.EventType = EV_IPL;
> +  event->Size = sizeof(*event) - sizeof(event->Event) + grub_strlen(description) + 1;
> +  grub_memcpy(event->Event, description, grub_strlen(description) + 1);
> +
> +  status = efi_call_5 (tpm->hash_log_extend_event, tpm, 0, buf,
> +		       (grub_uint64_t) size, event);
> +

Have you looked at how to get the TPM2.0 event logs from Linux? The TCG EFI Protocol
Specification mentions that all events generated after a EFI_TCG2_GET_EVENT_LOG call
shall be stored in a EFI_CONFIGURATION_TABLE that could be retrieved by the OS before
a call to ExitBootServices().

I see that shim calls GetEventLogs() to trigger this switch and your patch doesn't.
But Linux still doesn't have support to lookup this table anyways, so I think it's OK.

It's also mentioned in the TCG ACPI (1.2 rev 8, February 2017) and TCG PC Client PFP
(rev 00.49, January 2017) specifications, that the TPM2 ACPI table has optional fields
for the Log Area Start Address (LASA) and Log Area Minimum Length (LAML). So that would
be similar to the TPM1.2 TCPA ACPI table. I guess Linux should need support for both?

Thanks a lot and best regards,
-- 
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3] Core TPM support
  2017-06-16 13:51   ` Javier Martinez Canillas
@ 2017-06-19  3:38     ` Matthew Garrett
  2017-06-19 22:01       ` Javier Martinez Canillas
  2017-06-21 11:46     ` Javier Martinez Canillas
  1 sibling, 1 reply; 9+ messages in thread
From: Matthew Garrett @ 2017-06-19  3:38 UTC (permalink / raw)
  To: The development of GNU GRUB

On Fri, Jun 16, 2017 at 03:51:38PM +0200, Javier Martinez Canillas wrote:

> The TCG EFI Protocol Specification (rev 00.13, March 2016) mentions that the
> tdEFI_TCG2_BOOT_SERVICE_CAPABILITY StructureVersion Major and Minor should
> be checked to determine the EFI_TCG2_BOOT_SERVICE_CAPABILITY struct version.
> 
> In fact, shim checks for this and instead use tdTREE_BOOT_SERVICE_CAPABILITY
> if Major == 1 && Minor == 0. The EFI firmware on my Lenovo X1 Carbon reports
> these values for the FW TPM2.0 so I first was expecting the code to fail. But
> it works and I now see that the structures layout are equal so doesn't matter.
> 
> Do you think that we should be more strict on this? Or instead the shim code
> could be simplified as you did here and avoid distinguish between the two?

There's an argument for improving the correctness of this, yeah. As you 
say I don't think it actually makes any meaningful difference, but it'll 
help anyone who's comparing it to the spec.

> Have you looked at how to get the TPM2.0 event logs from Linux? The TCG EFI Protocol
> Specification mentions that all events generated after a EFI_TCG2_GET_EVENT_LOG call
> shall be stored in a EFI_CONFIGURATION_TABLE that could be retrieved by the OS before
> a call to ExitBootServices().
> 
> I see that shim calls GetEventLogs() to trigger this switch and your patch doesn't.
> But Linux still doesn't have support to lookup this table anyways, so I think it's OK.

I think this is a bug in shim. The right place to do this is in the EFI 
boot stub in the kernel - otherwise the kernel has to source this event 
information from multiple locations. But now that it's out there, we'll 
probably have to handle the configurationt able as well.

> It's also mentioned in the TCG ACPI (1.2 rev 8, February 2017) and TCG PC Client PFP
> (rev 00.49, January 2017) specifications, that the TPM2 ACPI table has optional fields
> for the Log Area Start Address (LASA) and Log Area Minimum Length (LAML). So that would
> be similar to the TPM1.2 TCPA ACPI table. I guess Linux should need support for both?

I really hope that there are no implementations where there's a 
difference between the information in ACPI and any other source, but I 
guess we'll find out. That's going to end up being a problem for the 
kernel rather than grub, anyway.

-- 
Matthew Garrett | mjg59@srcf.ucam.org


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3] Core TPM support
  2017-06-19  3:38     ` Matthew Garrett
@ 2017-06-19 22:01       ` Javier Martinez Canillas
  0 siblings, 0 replies; 9+ messages in thread
From: Javier Martinez Canillas @ 2017-06-19 22:01 UTC (permalink / raw)
  To: The development of GNU GRUB, Matthew Garrett


On 06/19/2017 05:38 AM, Matthew Garrett wrote:
> On Fri, Jun 16, 2017 at 03:51:38PM +0200, Javier Martinez Canillas wrote:
> 
>> The TCG EFI Protocol Specification (rev 00.13, March 2016) mentions that the
>> tdEFI_TCG2_BOOT_SERVICE_CAPABILITY StructureVersion Major and Minor should
>> be checked to determine the EFI_TCG2_BOOT_SERVICE_CAPABILITY struct version.
>>
>> In fact, shim checks for this and instead use tdTREE_BOOT_SERVICE_CAPABILITY
>> if Major == 1 && Minor == 0. The EFI firmware on my Lenovo X1 Carbon reports
>> these values for the FW TPM2.0 so I first was expecting the code to fail. But
>> it works and I now see that the structures layout are equal so doesn't matter.
>>
>> Do you think that we should be more strict on this? Or instead the shim code
>> could be simplified as you did here and avoid distinguish between the two?
> 
> There's an argument for improving the correctness of this, yeah. As you 
> say I don't think it actually makes any meaningful difference, but it'll 
> help anyone who's comparing it to the spec.
>

Ok.
 
>> Have you looked at how to get the TPM2.0 event logs from Linux? The TCG EFI Protocol
>> Specification mentions that all events generated after a EFI_TCG2_GET_EVENT_LOG call
>> shall be stored in a EFI_CONFIGURATION_TABLE that could be retrieved by the OS before
>> a call to ExitBootServices().
>>
>> I see that shim calls GetEventLogs() to trigger this switch and your patch doesn't.
>> But Linux still doesn't have support to lookup this table anyways, so I think it's OK.
> 
> I think this is a bug in shim. The right place to do this is in the EFI 
> boot stub in the kernel - otherwise the kernel has to source this event 
> information from multiple locations. But now that it's out there, we'll 
> probably have to handle the configurationt able as well.
>

Agreed. Let's see how this could be handled once the kernel has support to get the logs
from the EFI_TCG2_FINAL_EVENTS_TABLE and the TPM2 ACPI table.

>> It's also mentioned in the TCG ACPI (1.2 rev 8, February 2017) and TCG PC Client PFP
>> (rev 00.49, January 2017) specifications, that the TPM2 ACPI table has optional fields
>> for the Log Area Start Address (LASA) and Log Area Minimum Length (LAML). So that would
>> be similar to the TPM1.2 TCPA ACPI table. I guess Linux should need support for both?
> 
> I really hope that there are no implementations where there's a 
> difference between the information in ACPI and any other source, but I 
> guess we'll find out. That's going to end up being a problem for the 
> kernel rather than grub, anyway.
> 

Yes. I would guess that there are firmwares out there that are following the old specs,
so not filling these new TPM2 LASA && LAML fields and instead storing the logs in the
EFI_TCG2_FINAL_EVENTS_TABLE. But we can later find out what the firmwares are actually
doing and as you said this is a problem for Linux and not grub2 anyways. I just asked in
case you had already looked how to get the TPM2.0 event logs from the OS.

Best regards,
-- 
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3] Core TPM support
  2017-06-16 13:51   ` Javier Martinez Canillas
  2017-06-19  3:38     ` Matthew Garrett
@ 2017-06-21 11:46     ` Javier Martinez Canillas
  1 sibling, 0 replies; 9+ messages in thread
From: Javier Martinez Canillas @ 2017-06-21 11:46 UTC (permalink / raw)
  To: The development of GNU GRUB, Matthew Garrett

Hello Matthew,

On 06/16/2017 03:51 PM, Javier Martinez Canillas wrote:
> 
> I've tested your patches (plus the verify framework) today on a Lenovo
> Thinkpad X1 Carbon with 2 different setups:
> 

I've tested today building your patches --with-platform=pc instead of efi,
and found a build error due the grub_tpm_init() function being defined twice.
I'm using the following patch [0] on top of yours to avoid this issue.

But I noticed that you have a new verifier_tpm_module branch [1], that instead
of having the TPM support in-kernel, has it as an external module. And is only
enabled for efi, so this problem won't happen on that new branch.

You should remove though the grub_tpm_init() prototype, stub and in fact all
the GRUB_MACHINE_EFI #ifdefery in that new branch since it's unused now IIUC.

[0]:
From 8e8ae3d0fb3b9b5a914359b48bb9e893b6113aa4 Mon Sep 17 00:00:00 2001
From: Javier Martinez Canillas <javierm@redhat.com>
Date: Wed, 21 Jun 2017 12:29:38 +0200
Subject: [PATCH 1/1] Only build TPM support for x86 EFI platforms
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

There's only TPM support for x86 EFI platforms for now. So instead of
unconditionally add the TPM headers and source files, add it on a per
platform basis if TPM is supported. This also prevents the following
compile error when building a x86 pc platform:

In file included from kern/tpm.c:5:0:
../include/grub/tpm.h: In function ‘grub_tpm_init’:
../include/grub/tpm.h:76:1: warning: no return statement in function returning non-void [-Wreturn-type]
 static inline grub_err_t grub_tpm_init(void) {};
 ^~~~~~
kern/tpm.c: At top level:
kern/tpm.c:77:1: error: redefinition of ‘grub_tpm_init’
 grub_tpm_init (void)
 ^~~~~~~~~~~~~
In file included from kern/tpm.c:5:0:
../include/grub/tpm.h:76:26: note: previous definition of ‘grub_tpm_init’ was here
 static inline grub_err_t grub_tpm_init(void) {};

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---
 grub-core/Makefile.am       |  3 ++-
 grub-core/Makefile.core.def |  2 +-
 include/grub/tpm.h          | 20 --------------------
 3 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index cc641bc36a0f..36f006f135a8 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -92,7 +92,6 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
 
@@ -110,6 +109,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pmtimer.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
 endif
 
 if COND_i386_coreboot
@@ -166,6 +166,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pmtimer.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
 endif
 
 if COND_ia64_efi
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 2bdb6cafde2d..bfbe7c97a6cb 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -129,7 +129,6 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
-  common = kern/tpm.c;
   common = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
@@ -194,6 +193,7 @@ kernel = {
   efi = kern/acpi.c;
   efi = kern/efi/acpi.c;
   efi = kern/efi/tpm.c;
+  efi = kern/tpm.c;
   i386_coreboot = kern/i386/pc/acpi.c;
   i386_multiboot = kern/i386/pc/acpi.c;
   i386_coreboot = kern/acpi.c;
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
index dd905b8299d9..320b6d0a5897 100644
--- a/include/grub/tpm.h
+++ b/include/grub/tpm.h
@@ -66,29 +66,9 @@ typedef struct {
 grub_err_t EXPORT_FUNC(grub_tpm_measure) (unsigned char *buf, grub_size_t size,
 					  grub_uint8_t pcr,
 					  const char *description);
-#if defined (GRUB_MACHINE_EFI)
 grub_err_t grub_tpm_init(void);
 grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
 			    PassThroughToTPM_OutputParamBlock *outbuf);
 grub_err_t grub_tpm_log_event(unsigned char *buf, grub_size_t size,
 			      grub_uint8_t pcr, const char *description);
-#else
-static inline grub_err_t grub_tpm_init(void) {};
-
-static inline grub_err_t grub_tpm_execute(
-	PassThroughToTPM_InputParamBlock *inbuf __attribute__ ((unused)),
-	PassThroughToTPM_OutputParamBlock *outbuf __attribute__ ((unused)))
-{
-	return 0;
-};
-static inline grub_err_t grub_tpm_log_event(
-	unsigned char *buf __attribute__ ((unused)),
-	grub_size_t size __attribute__ ((unused)),
-	grub_uint8_t pcr __attribute__ ((unused)),
-	const char *description __attribute__ ((unused)))
-{
-	return 0;
-};
-#endif
-
 #endif
-- 
2.13.0

[1]: https://github.com/mjg59/grub/commits/verifier_tpm_module

Best regards,
-- 
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Add TPM support
@ 2017-06-15  0:42 Matthew Garrett
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Garrett @ 2017-06-15  0:42 UTC (permalink / raw)
  To: grub-devel

This patchset reworks my earlier TPM support to use the verifiers framework.
It only includes UEFI support right now due to the unclear copyright situation
on the BIOS code from trusted-grub.



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-06-21 11:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-15  0:20 Add TPM support Matthew Garrett
2017-06-15  0:21 ` [PATCH 1/3] Move verifiers to the kernel Matthew Garrett
2017-06-15  0:21 ` [PATCH 2/3] Verify commands executed by grub Matthew Garrett
2017-06-15  0:21 ` [PATCH 3/3] Core TPM support Matthew Garrett
2017-06-16 13:51   ` Javier Martinez Canillas
2017-06-19  3:38     ` Matthew Garrett
2017-06-19 22:01       ` Javier Martinez Canillas
2017-06-21 11:46     ` Javier Martinez Canillas
2017-06-15  0:42 Add " Matthew Garrett

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.