All of lore.kernel.org
 help / color / mirror / Atom feed
* Support for TPM measurements on UEFI systems
@ 2017-01-24  0:38 Matthew Garrett
  2017-01-24  0:38 ` [PATCH 1/7] Core TPM support Matthew Garrett
                   ` (7 more replies)
  0 siblings, 8 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel

This patchset adds support for measuring components of grub and what it's
loading into the TPM. It supports both TPM 1.2 and 2.0 devices via the
standard UEFI protocols.



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

* [PATCH 1/7] Core TPM support
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  0:38 ` [PATCH 2/7] Rework linux command Matthew Garrett
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 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 and BIOS systems, but
will measure all modules as they're loaded.
---
 grub-core/Makefile.am        |   1 +
 grub-core/Makefile.core.def  |   3 +
 grub-core/kern/dl.c          |   4 +
 grub-core/kern/efi/tpm.c     | 282 +++++++++++++++++++++++++++++++++++++++++++
 grub-core/kern/i386/pc/tpm.c | 145 ++++++++++++++++++++++
 grub-core/kern/tpm.c         |  19 +++
 include/grub/efi/tpm.h       | 153 +++++++++++++++++++++++
 include/grub/tpm.h           |  94 +++++++++++++++
 8 files changed, 701 insertions(+)
 create mode 100644 grub-core/kern/efi/tpm.c
 create mode 100644 grub-core/kern/i386/pc/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 04e9395..23ce9e9 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
 
 if COND_i386_pc
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index f6b6f38..90a43bb 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -126,6 +126,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
+  common = kern/tpm.c;
 
   noemu = kern/compiler-rt.c;
   noemu = kern/mm.c;
@@ -173,6 +174,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;
@@ -219,6 +221,7 @@ kernel = {
 
   i386_pc = kern/i386/pc/init.c;
   i386_pc = kern/i386/pc/mmap.c;
+  i386_pc = kern/i386/pc/tpm.c;
   i386_pc = term/i386/pc/console.c;
 
   i386_qemu = bus/pci.c;
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
index e394cd9..29ee00a 100644
--- a/grub-core/kern/dl.c
+++ b/grub-core/kern/dl.c
@@ -32,6 +32,7 @@
 #include <grub/env.h>
 #include <grub/cache.h>
 #include <grub/i18n.h>
+#include <grub/tpm.h>
 
 /* Platforms where modules are in a readonly area of memory.  */
 #if defined(GRUB_MACHINE_QEMU)
@@ -712,6 +713,9 @@ grub_dl_load_file (const char *filename)
      opens of the same device.  */
   grub_file_close (file);
 
+  grub_tpm_measure(core, size, GRUB_BINARY_PCR, "grub_module", filename);
+  grub_print_error();
+
   mod = grub_dl_load_core (core, size);
   grub_free (core);
   if (! mod)
diff --git a/grub-core/kern/efi/tpm.c b/grub-core/kern/efi/tpm.c
new file mode 100644
index 0000000..c9fb3c1
--- /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/pc/tpm.c b/grub-core/kern/i386/pc/tpm.c
new file mode 100644
index 0000000..1c57f6f
--- /dev/null
+++ b/grub-core/kern/i386/pc/tpm.c
@@ -0,0 +1,145 @@
+#include <grub/err.h>
+#include <grub/i18n.h>
+#include <grub/mm.h>
+#include <grub/tpm.h>
+#include <grub/misc.h>
+#include <grub/i386/pc/int.h>
+
+#define TCPA_MAGIC 0x41504354
+
+static int tpm_presence = -1;
+
+int tpm_present(void);
+
+int tpm_present(void)
+{
+  struct grub_bios_int_registers regs;
+
+  if (tpm_presence != -1)
+    return tpm_presence;
+
+  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+  regs.eax = 0xbb00;
+  regs.ebx = TCPA_MAGIC;
+  grub_bios_interrupt (0x1a, &regs);
+
+  if (regs.eax == 0)
+    tpm_presence = 1;
+  else
+    tpm_presence = 0;
+
+  return tpm_presence;
+}
+
+grub_err_t
+grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
+		 PassThroughToTPM_OutputParamBlock *outbuf)
+{
+  struct grub_bios_int_registers regs;
+  grub_addr_t inaddr, outaddr;
+
+  if (!tpm_present())
+    return 0;
+
+  inaddr = (grub_addr_t) inbuf;
+  outaddr = (grub_addr_t) outbuf;
+  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+  regs.eax = 0xbb02;
+  regs.ebx = TCPA_MAGIC;
+  regs.ecx = 0;
+  regs.edx = 0;
+  regs.es = (inaddr & 0xffff0000) >> 4;
+  regs.edi = inaddr & 0xffff;
+  regs.ds = outaddr >> 4;
+  regs.esi = outaddr & 0xf;
+
+  grub_bios_interrupt (0x1a, &regs);
+
+  if (regs.eax)
+    {
+      tpm_presence = 0;
+      return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
+    }
+
+  return 0;
+}
+
+typedef struct {
+	grub_uint32_t pcrindex;
+	grub_uint32_t eventtype;
+	grub_uint8_t digest[20];
+	grub_uint32_t eventdatasize;
+	grub_uint8_t event[0];
+} GRUB_PACKED Event;
+
+typedef struct {
+	grub_uint16_t ipblength;
+	grub_uint16_t reserved;
+	grub_uint32_t hashdataptr;
+	grub_uint32_t hashdatalen;
+	grub_uint32_t pcr;
+	grub_uint32_t reserved2;
+	grub_uint32_t logdataptr;
+	grub_uint32_t logdatalen;
+} GRUB_PACKED EventIncoming;
+
+typedef struct {
+	grub_uint16_t opblength;
+	grub_uint16_t reserved;
+	grub_uint32_t eventnum;
+	grub_uint8_t  hashvalue[20];
+} GRUB_PACKED EventOutgoing;
+
+grub_err_t
+grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+		   const char *description)
+{
+	struct grub_bios_int_registers regs;
+	EventIncoming incoming;
+	EventOutgoing outgoing;
+	Event *event;
+	grub_uint32_t datalength;
+
+	if (!tpm_present())
+		return 0;
+
+	datalength = grub_strlen(description);
+	event = grub_zalloc(datalength + sizeof(Event));
+	if (!event)
+		return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+				   N_("cannot allocate TPM event buffer"));
+
+	event->pcrindex = pcr;
+	event->eventtype = 0x0d;
+	event->eventdatasize = grub_strlen(description);
+	grub_memcpy(event->event, description, datalength);
+
+	incoming.ipblength = sizeof(incoming);
+	incoming.hashdataptr = (grub_uint32_t)buf;
+	incoming.hashdatalen = size;
+	incoming.pcr = pcr;
+	incoming.logdataptr = (grub_uint32_t)event;
+	incoming.logdatalen = datalength + sizeof(Event);
+
+	regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+	regs.eax = 0xbb01;
+	regs.ebx = TCPA_MAGIC;
+	regs.ecx = 0;
+	regs.edx = 0;
+	regs.es = (((grub_addr_t) &incoming) & 0xffff0000) >> 4;
+	regs.edi = ((grub_addr_t) &incoming) & 0xffff;
+	regs.ds = (((grub_addr_t) &outgoing) & 0xffff0000) >> 4;
+	regs.esi = ((grub_addr_t) &outgoing) & 0xffff;
+
+	grub_bios_interrupt (0x1a, &regs);
+
+	grub_free(event);
+
+	if (regs.eax)
+	  {
+	    tpm_presence = 0;
+	    return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
+	  }
+
+	return 0;
+}
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
new file mode 100644
index 0000000..e5e8fce
--- /dev/null
+++ b/grub-core/kern/tpm.c
@@ -0,0 +1,19 @@
+#include <grub/err.h>
+#include <grub/i18n.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/tpm.h>
+#include <grub/term.h>
+
+grub_err_t
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+		  const char *kind, const char *description)
+{
+  grub_err_t ret;
+  char *desc = grub_xasprintf("%s %s", kind, description);
+  if (!desc)
+    return GRUB_ERR_OUT_OF_MEMORY;
+  ret = grub_tpm_log_event(buf, size, pcr, desc);
+  grub_free(desc);
+  return ret;
+}
diff --git a/include/grub/efi/tpm.h b/include/grub/efi/tpm.h
new file mode 100644
index 0000000..e2aff4a
--- /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 0000000..972a5ed
--- /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 GRUB_ASCII_PCR 8
+#define GRUB_BINARY_PCR 9
+
+#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 *kind,
+					  const char *description);
+#if defined (GRUB_MACHINE_EFI) || defined (GRUB_MACHINE_PCBIOS)
+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_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.9.3



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

* [PATCH 2/7] Rework linux command
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
  2017-01-24  0:38 ` [PATCH 1/7] Core TPM support Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  0:38 ` [PATCH 3/7] Rework linux16 command Matthew Garrett
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

We want a single buffer that contains the entire kernel image in order to
perform a TPM measurement. Allocate one and copy the entire kernel into it
before pulling out the individual blocks later on.
---
 grub-core/loader/i386/linux.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index b15b8cc..5e54ec9 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -680,12 +680,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file = 0;
   struct linux_kernel_header lh;
   grub_uint8_t setup_sects;
-  grub_size_t real_size, prot_size, prot_file_size;
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
   grub_ssize_t len;
   int i;
   grub_size_t align, min_align;
   int relocatable;
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
+  grub_uint8_t *kernel = NULL;
 
   grub_dl_ref (my_mod);
 
@@ -699,7 +700,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   if (! file)
     goto fail;
 
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
+  len = grub_file_size (file);
+  kernel = grub_malloc (len);
+  if (!kernel)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
+      goto fail;
+    }
+
+  if (grub_file_read (file, kernel, len) != len)
     {
       if (!grub_errno)
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@@ -707,6 +716,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  grub_memcpy (&lh, kernel, sizeof (lh));
+  kernel_offset = sizeof (lh);
+
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
     {
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
@@ -806,13 +818,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   linux_params.ps_mouse = linux_params.padding10 =  0;
 
   len = sizeof (linux_params) - sizeof (lh);
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
-    {
-      if (!grub_errno)
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
-		    argv[0]);
-      goto fail;
-    }
+
+  grub_memcpy (&linux_params + sizeof (lh), kernel + kernel_offset, len);
+  kernel_offset += len;
 
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
 
@@ -871,7 +879,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
 
   /* The other parameters are filled when booting.  */
 
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
 
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
 		(unsigned) real_size, (unsigned) prot_size);
@@ -1016,9 +1024,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
 			      - (sizeof (LINUX_IMAGE) - 1));
 
   len = prot_file_size;
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
-		argv[0]);
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
 
   if (grub_errno == GRUB_ERR_NONE)
     {
@@ -1029,6 +1035,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
 
  fail:
 
+  grub_free (kernel);
+
   if (file)
     grub_file_close (file);
 
-- 
2.9.3



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

* [PATCH 3/7] Rework linux16 command
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
  2017-01-24  0:38 ` [PATCH 1/7] Core TPM support Matthew Garrett
  2017-01-24  0:38 ` [PATCH 2/7] Rework linux command Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  0:38 ` [PATCH 4/7] Measure kernel and initrd Matthew Garrett
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

We want a single buffer that contains the entire kernel image in order to
perform a TPM measurement. Allocate one and copy the entire kernel int it
before pulling out the individual blocks later on.
---
 grub-core/loader/i386/pc/linux.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
index a293b17..1ac9cd1 100644
--- a/grub-core/loader/i386/pc/linux.c
+++ b/grub-core/loader/i386/pc/linux.c
@@ -123,13 +123,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file = 0;
   struct linux_kernel_header lh;
   grub_uint8_t setup_sects;
-  grub_size_t real_size;
+  grub_size_t real_size, kernel_offset = 0;
   grub_ssize_t len;
   int i;
   char *grub_linux_prot_chunk;
   int grub_linux_is_bzimage;
   grub_addr_t grub_linux_prot_target;
   grub_err_t err;
+  grub_uint8_t *kernel = NULL;
 
   grub_dl_ref (my_mod);
 
@@ -143,7 +144,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   if (! file)
     goto fail;
 
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
+  len = grub_file_size (file);
+  kernel = grub_malloc (len);
+  if (!kernel)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
+      goto fail;
+    }
+
+  if (grub_file_read (file, kernel, len) != len)
     {
       if (!grub_errno)
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@@ -151,6 +160,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  grub_memcpy (&lh, kernel, sizeof (lh));
+  kernel_offset = sizeof (lh);
+
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
     {
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
@@ -314,13 +326,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
 
   len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
-  if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
-    {
-      if (!grub_errno)
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
-		    argv[0]);
-      goto fail;
-    }
+  grub_memcpy (grub_linux_real_chunk + sizeof (lh), kernel + kernel_offset,
+	       len);
+  kernel_offset += len;
 
   if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
       || grub_le_to_cpu16 (lh.version) < 0x0200)
@@ -355,10 +363,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   }
 
   len = grub_linux16_prot_size;
-  if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
-      != (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
-		argv[0]);
+  grub_memcpy (grub_linux_prot_chunk, kernel + kernel_offset, len);
+  kernel_offset += len;
 
   if (grub_errno == GRUB_ERR_NONE)
     {
@@ -368,6 +374,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
 
  fail:
 
+  grub_free (kernel);
+
   if (file)
     grub_file_close (file);
 
-- 
2.9.3



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

* [PATCH 4/7] Measure kernel and initrd
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
                   ` (2 preceding siblings ...)
  2017-01-24  0:38 ` [PATCH 3/7] Rework linux16 command Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  0:38 ` [PATCH 5/7] Measure the kernel commandline Matthew Garrett
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

Measure the kernel and initrd at load time
---
 grub-core/loader/i386/linux.c    | 6 ++++++
 grub-core/loader/i386/pc/linux.c | 4 ++++
 grub-core/loader/linux.c         | 3 +++
 3 files changed, 13 insertions(+)

diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index 5e54ec9..6d8d3d6 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -35,6 +35,7 @@
 #include <grub/i18n.h>
 #include <grub/lib/cmdline.h>
 #include <grub/linux.h>
+#include <grub/tpm.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -716,7 +717,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux", "Linux Kernel");
+  grub_print_error();
+
   grub_memcpy (&lh, kernel, sizeof (lh));
+
   kernel_offset = sizeof (lh);
 
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
@@ -1025,6 +1030,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
 
   len = prot_file_size;
   grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
+  kernel_offset += len;
 
   if (grub_errno == GRUB_ERR_NONE)
     {
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
index 1ac9cd1..c6197a1 100644
--- a/grub-core/loader/i386/pc/linux.c
+++ b/grub-core/loader/i386/pc/linux.c
@@ -35,6 +35,7 @@
 #include <grub/i386/floppy.h>
 #include <grub/lib/cmdline.h>
 #include <grub/linux.h>
+#include <grub/tpm.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -160,6 +161,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux16", "BIOS Linux Kernel");
+  grub_print_error();
+
   grub_memcpy (&lh, kernel, sizeof (lh));
   kernel_offset = sizeof (lh);
 
diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
index be6fa0f..8b5e6e0 100644
--- a/grub-core/loader/linux.c
+++ b/grub-core/loader/linux.c
@@ -4,6 +4,7 @@
 #include <grub/misc.h>
 #include <grub/file.h>
 #include <grub/mm.h>
+#include <grub/tpm.h>
 
 struct newc_head
 {
@@ -288,6 +289,8 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
 	  grub_initrd_close (initrd_ctx);
 	  return grub_errno;
 	}
+      grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_initrd", "Linux Initrd");
+      grub_print_error();
       ptr += cursize;
     }
   if (newc)
-- 
2.9.3



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

* [PATCH 5/7] Measure the kernel commandline
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
                   ` (3 preceding siblings ...)
  2017-01-24  0:38 ` [PATCH 4/7] Measure kernel and initrd Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  0:38 ` [PATCH 6/7] Measure commands Matthew Garrett
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

Measure the kernel commandline to ensure that it hasn't been modified
---
 grub-core/lib/cmdline.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
index d5e10ee..055b7aa 100644
--- a/grub-core/lib/cmdline.c
+++ b/grub-core/lib/cmdline.c
@@ -19,6 +19,7 @@
 
 #include <grub/lib/cmdline.h>
 #include <grub/misc.h>
+#include <grub/tpm.h>
 
 static unsigned int check_arg (char *c, int *has_space)
 {
@@ -67,7 +68,7 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
 {
   int i, space;
   unsigned int arg_size;
-  char *c;
+  char *c, *orig = buf;
 
   for (i = 0; i < argc; i++)
     {
@@ -104,5 +105,8 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
 
   *buf = 0;
 
+  grub_tpm_measure ((void *)orig, grub_strlen (orig), GRUB_BINARY_PCR, "grub_kernel_cmdline", "Kernel Commandline");
+  grub_print_error();
+
   return i;
 }
-- 
2.9.3



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

* [PATCH 6/7] Measure commands
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
                   ` (4 preceding siblings ...)
  2017-01-24  0:38 ` [PATCH 5/7] Measure the kernel commandline Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  0:38 ` [PATCH 7/7] Measure multiboot images and modules Matthew Garrett
  2017-01-24  1:29 ` Support for TPM measurements on UEFI systems Vladimir 'phcoder' Serbinenko
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

From: Matthew Garrett <mjg59@srcf.ucam.org>

Measure each command executed by grub, which includes script execution.
---
 grub-core/script/execute.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
index a8502d9..cf99410 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/tpm.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,25 @@ 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_tpm_measure ((unsigned char *)cmdstring, cmdlen, GRUB_ASCII_PCR, "grub_cmd", cmdstring);
+  grub_print_error();
+  grub_free(cmdstring);
   invert = 0;
   argc = argv.argc - 1;
   args = argv.args + 1;
-- 
2.9.3



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

* [PATCH 7/7] Measure multiboot images and modules
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
                   ` (5 preceding siblings ...)
  2017-01-24  0:38 ` [PATCH 6/7] Measure commands Matthew Garrett
@ 2017-01-24  0:38 ` Matthew Garrett
  2017-01-24  1:29 ` Support for TPM measurements on UEFI systems Vladimir 'phcoder' Serbinenko
  7 siblings, 0 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

---
 grub-core/loader/i386/multiboot_mbi.c | 4 ++++
 grub-core/loader/multiboot.c          | 3 +++
 grub-core/loader/multiboot_mbi2.c     | 4 ++++
 3 files changed, 11 insertions(+)

diff --git a/grub-core/loader/i386/multiboot_mbi.c b/grub-core/loader/i386/multiboot_mbi.c
index fd7b41b..42372bf 100644
--- a/grub-core/loader/i386/multiboot_mbi.c
+++ b/grub-core/loader/i386/multiboot_mbi.c
@@ -36,6 +36,7 @@
 #include <grub/net.h>
 #include <grub/i18n.h>
 #include <grub/lib/cmdline.h>
+#include <grub/tpm.h>
 
 #ifdef GRUB_MACHINE_EFI
 #include <grub/efi/efi.h>
@@ -173,6 +174,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
       return grub_errno;
     }
 
+  grub_tpm_measure((unsigned char*)buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
+  grub_print_error();
+
   header = find_header (buffer, len);
 
   if (header == 0)
diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c
index bd9d5b3..a97ed87 100644
--- a/grub-core/loader/multiboot.c
+++ b/grub-core/loader/multiboot.c
@@ -42,6 +42,7 @@
 #include <grub/video.h>
 #include <grub/memory.h>
 #include <grub/i18n.h>
+#include <grub/tpm.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -424,6 +425,8 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
     }
 
   grub_file_close (file);
+  grub_tpm_measure (module, size, GRUB_BINARY_PCR, "grub_multiboot", argv[0]);
+  grub_print_error();
   return GRUB_ERR_NONE;
 }
 
diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c
index b0679a9..c62ced3 100644
--- a/grub-core/loader/multiboot_mbi2.c
+++ b/grub-core/loader/multiboot_mbi2.c
@@ -36,6 +36,7 @@
 #include <grub/i18n.h>
 #include <grub/net.h>
 #include <grub/lib/cmdline.h>
+#include <grub/tpm.h>
 
 #if defined (GRUB_MACHINE_EFI)
 #include <grub/efi/efi.h>
@@ -131,6 +132,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 
   COMPILE_TIME_ASSERT (MULTIBOOT_HEADER_ALIGN % 4 == 0);
 
+  grub_tpm_measure ((unsigned char *)mld.buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
+  grub_print_error();
+
   header = find_header (mld.buffer, len);
 
   if (header == 0)
-- 
2.9.3



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

* Re: Support for TPM measurements on UEFI systems
  2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
                   ` (6 preceding siblings ...)
  2017-01-24  0:38 ` [PATCH 7/7] Measure multiboot images and modules Matthew Garrett
@ 2017-01-24  1:29 ` Vladimir 'phcoder' Serbinenko
  2017-01-24  1:33   ` Matthew Garrett
  7 siblings, 1 reply; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-01-24  1:29 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 763 bytes --]

For policy reasons we can't put any TPM code into GNU project. Can we use
verifiers framework for this rather than custom hooks? This would allow
your code to be a single module that can be put into a separate repo rather
than a complex patch set. Verifiers framework is in separate branch
verifiers. I didn't look into details of patches. This has to be postponed
after release

On Tue, 24 Jan 2017, 03:39 Matthew Garrett <mjg59@coreos.com> wrote:

This patchset adds support for measuring components of grub and what it's
loading into the TPM. It supports both TPM 1.2 and 2.0 devices via the
standard UEFI protocols.


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

[-- Attachment #2: Type: text/html, Size: 1531 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-01-24  1:29 ` Support for TPM measurements on UEFI systems Vladimir 'phcoder' Serbinenko
@ 2017-01-24  1:33   ` Matthew Garrett
  2017-01-24  2:03     ` Vladimir 'phcoder' Serbinenko
  0 siblings, 1 reply; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  1:33 UTC (permalink / raw)
  To: The development of GNU GRUB

On Mon, Jan 23, 2017 at 5:29 PM, Vladimir 'phcoder' Serbinenko
<phcoder@gmail.com> wrote:
> For policy reasons we can't put any TPM code into GNU project. Can we use
> verifiers framework for this rather than custom hooks? This would allow your
> code to be a single module that can be put into a separate repo rather than
> a complex patch set. Verifiers framework is in separate branch verifiers. I
> didn't look into details of patches. This has to be postponed after release

From https://www.gnu.org/philosophy/can-you-trust.html

"Therefore, we conclude that the “Trusted Platform Modules” available
for PCs are not dangerous, and there is no reason not to include one
in a computer or support it in system software."

There's no problem including TPM support in GNU projects.


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

* Re: Support for TPM measurements on UEFI systems
  2017-01-24  1:33   ` Matthew Garrett
@ 2017-01-24  2:03     ` Vladimir 'phcoder' Serbinenko
  2017-01-24  2:14       ` Matthew Garrett
  0 siblings, 1 reply; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-01-24  2:03 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 1192 bytes --]

Ok. Good. Still, can we use verifiers framework ? We can adjust it if
needed. Also it's still post-release material

On Tue, 24 Jan 2017, 04:34 Matthew Garrett <mjg59@coreos.com> wrote:

> On Mon, Jan 23, 2017 at 5:29 PM, Vladimir 'phcoder' Serbinenko
> <phcoder@gmail.com> wrote:
> > For policy reasons we can't put any TPM code into GNU project. Can we use
> > verifiers framework for this rather than custom hooks? This would allow
> your
> > code to be a single module that can be put into a separate repo rather
> than
> > a complex patch set. Verifiers framework is in separate branch
> verifiers. I
> > didn't look into details of patches. This has to be postponed after
> release
>
> From https://www.gnu.org/philosophy/can-you-trust.html
>
> "Therefore, we conclude that the “Trusted Platform Modules” available
> for PCs are not dangerous, and there is no reason not to include one
> in a computer or support it in system software."
>
> There's no problem including TPM support in GNU projects.
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 2244 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-01-24  2:03     ` Vladimir 'phcoder' Serbinenko
@ 2017-01-24  2:14       ` Matthew Garrett
  2017-01-27 21:08         ` Vladimir 'phcoder' Serbinenko
  0 siblings, 1 reply; 24+ messages in thread
From: Matthew Garrett @ 2017-01-24  2:14 UTC (permalink / raw)
  To: The development of GNU GRUB

On Mon, Jan 23, 2017 at 6:03 PM, Vladimir 'phcoder' Serbinenko
<phcoder@gmail.com> wrote:
> Ok. Good. Still, can we use verifiers framework ? We can adjust it if
> needed. Also it's still post-release material

Where's the branch? I wasn't able to find it on Savannah.


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

* Re: Support for TPM measurements on UEFI systems
  2017-01-24  2:14       ` Matthew Garrett
@ 2017-01-27 21:08         ` Vladimir 'phcoder' Serbinenko
  2017-02-04 21:24           ` Matthew Garrett
  0 siblings, 1 reply; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-01-27 21:08 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 634 bytes --]

On Tue, 24 Jan 2017, 05:14 Matthew Garrett <mjg59@coreos.com> wrote:

> On Mon, Jan 23, 2017 at 6:03 PM, Vladimir 'phcoder' Serbinenko
> <phcoder@gmail.com> wrote:
> > Ok. Good. Still, can we use verifiers framework ? We can adjust it if
> > needed. Also it's still post-release material
>
> Where's the branch? I wasn't able to find it on Savannah.
>
I must have accidentally deleted it on the server. I'll reupload it when
I'll have access to the laptop in question on Monday

>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 1545 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-01-27 21:08         ` Vladimir 'phcoder' Serbinenko
@ 2017-02-04 21:24           ` Matthew Garrett
  2017-02-05 13:27             ` Vladimir 'phcoder' Serbinenko
  0 siblings, 1 reply; 24+ messages in thread
From: Matthew Garrett @ 2017-02-04 21:24 UTC (permalink / raw)
  To: The development of GNU GRUB

On Fri, Jan 27, 2017 at 09:08:33PM +0000, Vladimir 'phcoder' Serbinenko wrote:

> I must have accidentally deleted it on the server. I'll reupload it when
> I'll have access to the laptop in question on Monday

Hi,

Did you have any luck digging this up?

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


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

* Re: Support for TPM measurements on UEFI systems
  2017-02-04 21:24           ` Matthew Garrett
@ 2017-02-05 13:27             ` Vladimir 'phcoder' Serbinenko
  2017-02-05 13:28               ` Vladimir 'phcoder' Serbinenko
  0 siblings, 1 reply; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-02-05 13:27 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 627 bytes --]

Rebased and uploaded to branch phcoder/verifiers.

Le Sat, Feb 4, 2017 à 10:24 PM, Matthew Garrett <mjg59@srcf.ucam.org> a
écrit :

> On Fri, Jan 27, 2017 at 09:08:33PM +0000, Vladimir 'phcoder' Serbinenko
> wrote:
>
> > I must have accidentally deleted it on the server. I'll reupload it when
> > I'll have access to the laptop in question on Monday
>
> Hi,
>
> Did you have any luck digging this up?
>
> --
> Matthew Garrett | mjg59@srcf.ucam.org
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 1472 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-02-05 13:27             ` Vladimir 'phcoder' Serbinenko
@ 2017-02-05 13:28               ` Vladimir 'phcoder' Serbinenko
  2017-02-06 16:43                 ` Matthew Garrett
  0 siblings, 1 reply; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-02-05 13:28 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 826 bytes --]

See verify.h for the interface. Obviously if you need changes in the API,
please say.

Le Sun, Feb 5, 2017 à 2:27 PM, Vladimir 'phcoder' Serbinenko <
phcoder@gmail.com> a écrit :

> Rebased and uploaded to branch phcoder/verifiers.
>
> Le Sat, Feb 4, 2017 à 10:24 PM, Matthew Garrett <mjg59@srcf.ucam.org> a
> écrit :
>
> On Fri, Jan 27, 2017 at 09:08:33PM +0000, Vladimir 'phcoder' Serbinenko
> wrote:
>
> > I must have accidentally deleted it on the server. I'll reupload it when
> > I'll have access to the laptop in question on Monday
>
> Hi,
>
> Did you have any luck digging this up?
>
> --
> Matthew Garrett | mjg59@srcf.ucam.org
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>
>

[-- Attachment #2: Type: text/html, Size: 2027 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-02-05 13:28               ` Vladimir 'phcoder' Serbinenko
@ 2017-02-06 16:43                 ` Matthew Garrett
  2017-02-06 17:53                   ` Jon McCune
  2017-02-06 19:58                   ` Vladimir 'phcoder' Serbinenko
  0 siblings, 2 replies; 24+ messages in thread
From: Matthew Garrett @ 2017-02-06 16:43 UTC (permalink / raw)
  To: The development of GNU GRUB

On Sun, Feb 05, 2017 at 01:28:20PM +0000, Vladimir 'phcoder' Serbinenko wrote:
> See verify.h for the interface. Obviously if you need changes in the API,
> please say.

I think that's a starting point, but it doesn't seem sufficient for some 
of the cases I care about. For instance, measuring boot state isn't just 
about the files that are read - we also need to measure the commands 
that grub runs and the command line passed to the kernel, for instance. 
Ideally we'd also have more context available in order to make a better 
decision about which PCR to measure something into, but I can't think of 
a good way to do that simply by hooking open. That also seems to make it 
difficult to implement a handler that should only be verifying some 
objects - for instance, a UEFI secure boot handler only wants to verify 
the kernel (or something that's chainloaded) and ignore everything else.

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


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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 16:43                 ` Matthew Garrett
@ 2017-02-06 17:53                   ` Jon McCune
  2017-02-06 22:04                     ` Matthew Garrett
  2017-02-07  8:39                     ` Vladimir 'phcoder' Serbinenko
  2017-02-06 19:58                   ` Vladimir 'phcoder' Serbinenko
  1 sibling, 2 replies; 24+ messages in thread
From: Jon McCune @ 2017-02-06 17:53 UTC (permalink / raw)
  To: The development of GNU GRUB


[-- Attachment #1.1: Type: text/plain, Size: 1832 bytes --]

Matthew,

On Mon, Feb 6, 2017 at 8:43 AM, Matthew Garrett <mjg59@srcf.ucam.org> wrote:

> On Sun, Feb 05, 2017 at 01:28:20PM +0000, Vladimir 'phcoder' Serbinenko
> wrote:
> > See verify.h for the interface. Obviously if you need changes in the API,
> > please say.
>
> I think that's a starting point, but it doesn't seem sufficient for some
> of the cases I care about. For instance, measuring boot state isn't just
> about the files that are read - we also need to measure the commands
> that grub runs


I'm not sure about measuring the commands that GRUB runs. GRUB's config
file is a shell-like language, and measuring that file should give a pretty
good indication of its behavior. In the grey area between "what is code?"
and "what is data?", making the case that grub.cfg is code seems feasible,
which greatly simplifies the work of whatever verifies attestations or
binds/seals data. Although, implementations for these two don't really seem
to be in conflict so maybe GRUB could be configured one way or the other.

(I'm coming at this from the perspective of "gathering golden measurements
by just-run-it-and-see considered harmful".)


> and the command line passed to the kernel, for instance.
>

Agreed.


> Ideally we'd also have more context available in order to make a better
> decision about which PCR to measure something into,


This is definitely interesting. Re: the scenario above, files could get
measured into one place. Commands executed could get measured into another.


> but I can't think of
> a good way to do that simply by hooking open. That also seems to make it
> difficult to implement a handler that should only be verifying some
> objects - for instance, a UEFI secure boot handler only wants to verify
> the kernel (or something that's chainloaded) and ignore everything else.
>
>
Best,
-Jon

[-- Attachment #1.2: Type: text/html, Size: 2908 bytes --]

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4849 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 16:43                 ` Matthew Garrett
  2017-02-06 17:53                   ` Jon McCune
@ 2017-02-06 19:58                   ` Vladimir 'phcoder' Serbinenko
  2017-02-06 22:10                     ` Matthew Garrett
  1 sibling, 1 reply; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-02-06 19:58 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 1441 bytes --]

On Mon, 6 Feb 2017, 17:44 Matthew Garrett <mjg59@srcf.ucam.org> wrote:

> On Sun, Feb 05, 2017 at 01:28:20PM +0000, Vladimir 'phcoder' Serbinenko
> wrote:
> > See verify.h for the interface. Obviously if you need changes in the API,
> > please say.
>
> I think that's a starting point, but it doesn't seem sufficient for some
> of the cases I care about. For instance, measuring boot state isn't just
> about the files that are read - we also need to measure the commands
> that grub runs and the command line passed to the kernel, for instance.
>
Those can be added as separate non-file verification hooks if they are
needed.

> Ideally we'd also have more context available in order to make a better
> decision about which PCR to measure something into, but I can't think of
> a good way to do that simply by hooking open. That also seems to make it
> difficult to implement a handler that should only be verifying some
> objects - for instance, a UEFI secure boot handler only wants to verify
> the kernel (or something that's chainloaded) and ignore everything else.
>
This branch adds additional parameter to open that indicates what's the
file will be used for (kernel, initrd, ...). In which cases doesn't it
provide enough context?

>
> --
> Matthew Garrett | mjg59@srcf.ucam.org
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 2744 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 17:53                   ` Jon McCune
@ 2017-02-06 22:04                     ` Matthew Garrett
  2017-02-07  3:56                       ` Jon McCune
  2017-02-07  8:39                     ` Vladimir 'phcoder' Serbinenko
  1 sibling, 1 reply; 24+ messages in thread
From: Matthew Garrett @ 2017-02-06 22:04 UTC (permalink / raw)
  To: The development of GNU GRUB

On Mon, Feb 06, 2017 at 09:53:57AM -0800, Jon McCune wrote:

> I'm not sure about measuring the commands that GRUB runs. GRUB's config
> file is a shell-like language, and measuring that file should give a pretty
> good indication of its behavior. In the grey area between "what is code?"
> and "what is data?", making the case that grub.cfg is code seems feasible,
> which greatly simplifies the work of whatever verifies attestations or
> binds/seals data. Although, implementations for these two don't really seem
> to be in conflict so maybe GRUB could be configured one way or the other.

I'm concerned that the language gives enough flexibility that we don't 
know that for sure - for instance, if a regularly used command is 
vulnerable to a buffer overflow, there's no way to determine whether 
that occurred. Measuring each command before it's executed gives us some 
further assurance in that respect. Calculating the expected values is 
still pretty easy, and if they're logged then you can have a regex-based 
engine for remote validation.

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


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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 19:58                   ` Vladimir 'phcoder' Serbinenko
@ 2017-02-06 22:10                     ` Matthew Garrett
  2017-02-07  1:12                       ` Vladimir 'phcoder' Serbinenko
  0 siblings, 1 reply; 24+ messages in thread
From: Matthew Garrett @ 2017-02-06 22:10 UTC (permalink / raw)
  To: The development of GNU GRUB

On Mon, Feb 06, 2017 at 07:58:37PM +0000, Vladimir 'phcoder' Serbinenko wrote:
> On Mon, 6 Feb 2017, 17:44 Matthew Garrett <mjg59@srcf.ucam.org> wrote:
> 
> > On Sun, Feb 05, 2017 at 01:28:20PM +0000, Vladimir 'phcoder' Serbinenko
> > wrote:
> > > See verify.h for the interface. Obviously if you need changes in the API,
> > > please say.
> >
> > I think that's a starting point, but it doesn't seem sufficient for some
> > of the cases I care about. For instance, measuring boot state isn't just
> > about the files that are read - we also need to measure the commands
> > that grub runs and the command line passed to the kernel, for instance.
> >
> Those can be added as separate non-file verification hooks if they are
> needed.

Ok. In that case I think this can probably work. I'll try porting it 
over.

> > Ideally we'd also have more context available in order to make a better
> > decision about which PCR to measure something into, but I can't think of
> > a good way to do that simply by hooking open. That also seems to make it
> > difficult to implement a handler that should only be verifying some
> > objects - for instance, a UEFI secure boot handler only wants to verify
> > the kernel (or something that's chainloaded) and ignore everything else.
> >
> This branch adds additional parameter to open that indicates what's the
> file will be used for (kernel, initrd, ...). In which cases doesn't it
> provide enough context?

Sorry, yes, I missed the previous commit. I think that's enough.

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


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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 22:10                     ` Matthew Garrett
@ 2017-02-07  1:12                       ` Vladimir 'phcoder' Serbinenko
  0 siblings, 0 replies; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-02-07  1:12 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 2022 bytes --]

Le Mon, Feb 6, 2017 à 11:11 PM, Matthew Garrett <mjg59@srcf.ucam.org> a
écrit :

> On Mon, Feb 06, 2017 at 07:58:37PM +0000, Vladimir 'phcoder' Serbinenko
> wrote:
> > On Mon, 6 Feb 2017, 17:44 Matthew Garrett <mjg59@srcf.ucam.org> wrote:
> >
> > > On Sun, Feb 05, 2017 at 01:28:20PM +0000, Vladimir 'phcoder' Serbinenko
> > > wrote:
> > > > See verify.h for the interface. Obviously if you need changes in the
> API,
> > > > please say.
> > >
> > > I think that's a starting point, but it doesn't seem sufficient for
> some
> > > of the cases I care about. For instance, measuring boot state isn't
> just
> > > about the files that are read - we also need to measure the commands
> > > that grub runs and the command line passed to the kernel, for instance.
> > >
> > Those can be added as separate non-file verification hooks if they are
> > needed.
>
> Ok. In that case I think this can probably work. I'll try porting it
> over.
>
I added string verification. Now it verifies kernel command line but can be
extended to other stuff.

>
> > > Ideally we'd also have more context available in order to make a better
> > > decision about which PCR to measure something into, but I can't think
> of
> > > a good way to do that simply by hooking open. That also seems to make
> it
> > > difficult to implement a handler that should only be verifying some
> > > objects - for instance, a UEFI secure boot handler only wants to verify
> > > the kernel (or something that's chainloaded) and ignore everything
> else.
> > >
> > This branch adds additional parameter to open that indicates what's the
> > file will be used for (kernel, initrd, ...). In which cases doesn't it
> > provide enough context?
>
> Sorry, yes, I missed the previous commit. I think that's enough.
>
> --
> Matthew Garrett | mjg59@srcf.ucam.org
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 3680 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 22:04                     ` Matthew Garrett
@ 2017-02-07  3:56                       ` Jon McCune
  0 siblings, 0 replies; 24+ messages in thread
From: Jon McCune @ 2017-02-07  3:56 UTC (permalink / raw)
  To: The development of GNU GRUB


[-- Attachment #1.1: Type: text/plain, Size: 1267 bytes --]

On Mon, Feb 6, 2017 at 2:04 PM, Matthew Garrett <mjg59@srcf.ucam.org> wrote:

> On Mon, Feb 06, 2017 at 09:53:57AM -0800, Jon McCune wrote:
>
> > I'm not sure about measuring the commands that GRUB runs. GRUB's config
> > file is a shell-like language, and measuring that file should give a
> pretty
> > good indication of its behavior. In the grey area between "what is code?"
> > and "what is data?", making the case that grub.cfg is code seems
> feasible,
> > which greatly simplifies the work of whatever verifies attestations or
> > binds/seals data. Although, implementations for these two don't really
> seem
> > to be in conflict so maybe GRUB could be configured one way or the other.
>
> I'm concerned that the language gives enough flexibility that we don't
> know that for sure - for instance, if a regularly used command is
> vulnerable to a buffer overflow, there's no way to determine whether
> that occurred. Measuring each command before it's executed gives us some
> further assurance in that respect.


This is a good point. I'd still like to express a preference that it be
optional.


> Calculating the expected values is
> still pretty easy, and if they're logged then you can have a regex-based
> engine for remote validation.
>
>
Thanks,
-Jon

[-- Attachment #1.2: Type: text/html, Size: 1944 bytes --]

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4849 bytes --]

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

* Re: Support for TPM measurements on UEFI systems
  2017-02-06 17:53                   ` Jon McCune
  2017-02-06 22:04                     ` Matthew Garrett
@ 2017-02-07  8:39                     ` Vladimir 'phcoder' Serbinenko
  1 sibling, 0 replies; 24+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-02-07  8:39 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 2330 bytes --]

On Mon, 6 Feb 2017, 18:55 Jon McCune <jonmccune@google.com> wrote:

> Matthew,
>
> On Mon, Feb 6, 2017 at 8:43 AM, Matthew Garrett <mjg59@srcf.ucam.org>
> wrote:
>
> On Sun, Feb 05, 2017 at 01:28:20PM +0000, Vladimir 'phcoder' Serbinenko
> wrote:
> > See verify.h for the interface. Obviously if you need changes in the API,
> > please say.
>
> I think that's a starting point, but it doesn't seem sufficient for some
> of the cases I care about. For instance, measuring boot state isn't just
> about the files that are read - we also need to measure the commands
> that grub runs
>
>
> I'm not sure about measuring the commands that GRUB runs. GRUB's config
> file is a shell-like language, and measuring that file should give a pretty
> good indication of its behavior. In the grey area between "what is code?"
> and "what is data?", making the case that grub.cfg is code seems feasible,
> which greatly simplifies the work of whatever verifies attestations or
> binds/seals data. Although, implementations for these two don't really seem
> to be in conflict so maybe GRUB could be configured one way or the other.
>
Independently of this, it's still useful to have Linux command line to be
exposed to verifier if it wants to see it. I can imagine verifier wanting
to check Linux command line but not wanting to verify the grub config file.

>
> (I'm coming at this from the perspective of "gathering golden measurements
> by just-run-it-and-see considered harmful".)
>
>
> and the command line passed to the kernel, for instance.
>
>
> Agreed.
>
>
> Ideally we'd also have more context available in order to make a better
> decision about which PCR to measure something into,
>
>
> This is definitely interesting. Re: the scenario above, files could get
> measured into one place. Commands executed could get measured into another.
>
>
> but I can't think of
> a good way to do that simply by hooking open. That also seems to make it
> difficult to implement a handler that should only be verifying some
> objects - for instance, a UEFI secure boot handler only wants to verify
> the kernel (or something that's chainloaded) and ignore everything else.
>
>
> Best,
> -Jon
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 6005 bytes --]

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

end of thread, other threads:[~2017-02-07  8:40 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-24  0:38 Support for TPM measurements on UEFI systems Matthew Garrett
2017-01-24  0:38 ` [PATCH 1/7] Core TPM support Matthew Garrett
2017-01-24  0:38 ` [PATCH 2/7] Rework linux command Matthew Garrett
2017-01-24  0:38 ` [PATCH 3/7] Rework linux16 command Matthew Garrett
2017-01-24  0:38 ` [PATCH 4/7] Measure kernel and initrd Matthew Garrett
2017-01-24  0:38 ` [PATCH 5/7] Measure the kernel commandline Matthew Garrett
2017-01-24  0:38 ` [PATCH 6/7] Measure commands Matthew Garrett
2017-01-24  0:38 ` [PATCH 7/7] Measure multiboot images and modules Matthew Garrett
2017-01-24  1:29 ` Support for TPM measurements on UEFI systems Vladimir 'phcoder' Serbinenko
2017-01-24  1:33   ` Matthew Garrett
2017-01-24  2:03     ` Vladimir 'phcoder' Serbinenko
2017-01-24  2:14       ` Matthew Garrett
2017-01-27 21:08         ` Vladimir 'phcoder' Serbinenko
2017-02-04 21:24           ` Matthew Garrett
2017-02-05 13:27             ` Vladimir 'phcoder' Serbinenko
2017-02-05 13:28               ` Vladimir 'phcoder' Serbinenko
2017-02-06 16:43                 ` Matthew Garrett
2017-02-06 17:53                   ` Jon McCune
2017-02-06 22:04                     ` Matthew Garrett
2017-02-07  3:56                       ` Jon McCune
2017-02-07  8:39                     ` Vladimir 'phcoder' Serbinenko
2017-02-06 19:58                   ` Vladimir 'phcoder' Serbinenko
2017-02-06 22:10                     ` Matthew Garrett
2017-02-07  1:12                       ` Vladimir 'phcoder' Serbinenko

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.