15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Matthew Garrett <mjg59@coreos.com>
15a207
Date: Wed, 23 Mar 2016 17:03:43 -0700
15a207
Subject: [PATCH] Rework TPM measurements
15a207
15a207
Rework TPM measurements to use fewer PCRs. After discussion with upstream,
15a207
it's preferable to avoid using so many PCRs. Instead, measure into PCRs 8
15a207
and 9 but use a prefix in the event log to indicate which subsystem carried
15a207
out the measurements.
15a207
15a207
(cherry picked from commit bb3473d7c8741ad5ef7cf8aafbbcf094df08bfc9)
15a207
---
15a207
 grub-core/kern/dl.c                   |  2 +-
15a207
 grub-core/kern/tpm.c                  | 10 ++++++++--
15a207
 grub-core/lib/cmdline.c               |  4 ++--
15a207
 grub-core/loader/i386/efi/linux.c     |  4 ++--
15a207
 grub-core/loader/i386/linux.c         |  2 +-
15a207
 grub-core/loader/i386/multiboot_mbi.c |  2 +-
15a207
 grub-core/loader/i386/pc/linux.c      |  2 +-
15a207
 grub-core/loader/linux.c              |  2 +-
15a207
 grub-core/loader/multiboot.c          |  2 +-
15a207
 grub-core/loader/multiboot_mbi2.c     |  2 +-
15a207
 grub-core/script/execute.c            |  4 ++--
15a207
 include/grub/tpm.h                    |  9 +++------
15a207
 12 files changed, 24 insertions(+), 21 deletions(-)
15a207
15a207
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
15a207
index 387d1e6446d..d0989573866 100644
15a207
--- a/grub-core/kern/dl.c
15a207
+++ b/grub-core/kern/dl.c
15a207
@@ -830,7 +830,7 @@ grub_dl_load_file (const char *filename)
15a207
      opens of the same device.  */
15a207
   grub_file_close (file);
15a207
 
15a207
-  grub_tpm_measure(core, size, GRUB_TPM_PCR, filename);
15a207
+  grub_tpm_measure(core, size, GRUB_BINARY_PCR, "grub_module", filename);
15a207
 
15a207
   mod = grub_dl_load_core (core, size);
15a207
   grub_free (core);
15a207
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
15a207
index 1a991876c83..cb5a812035d 100644
15a207
--- a/grub-core/kern/tpm.c
15a207
+++ b/grub-core/kern/tpm.c
15a207
@@ -7,7 +7,13 @@
15a207
 
15a207
 grub_err_t
15a207
 grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
15a207
-		  const char *description)
15a207
+		  const char *kind, const char *description)
15a207
 {
15a207
-  return grub_tpm_log_event(buf, size, pcr, description);
15a207
+  grub_err_t ret;
15a207
+  char *desc = grub_xasprintf("%s %s", kind, description);
15a207
+  if (!desc)
15a207
+    return GRUB_ERR_OUT_OF_MEMORY;
15a207
+  ret = grub_tpm_log_event(buf, size, pcr, description);
15a207
+  grub_free(desc);
15a207
+  return ret;
15a207
 }
15a207
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
15a207
index 6b56304d4a7..178f7382f07 100644
15a207
--- a/grub-core/lib/cmdline.c
15a207
+++ b/grub-core/lib/cmdline.c
15a207
@@ -126,8 +126,8 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
15a207
 
15a207
   *buf = 0;
15a207
 
15a207
-  grub_tpm_measure ((void *)orig, grub_strlen (orig), GRUB_CMDLINE_PCR,
15a207
-		    "Kernel Commandline");
15a207
+  grub_tpm_measure ((void *)orig, grub_strlen (orig), GRUB_ASCII_PCR,
15a207
+		    "grub_kernel_cmdline", orig);
15a207
 
15a207
   return i;
15a207
 }
15a207
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
15a207
index d837249b4a1..074dbd6513e 100644
15a207
--- a/grub-core/loader/i386/efi/linux.c
15a207
+++ b/grub-core/loader/i386/efi/linux.c
15a207
@@ -132,7 +132,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
15a207
                         argv[i]);
15a207
           goto fail;
15a207
         }
15a207
-      grub_tpm_measure (ptr, cursize, GRUB_INITRD_PCR, "UEFI Linux initrd");
15a207
+      grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_linuxefi", "Initrd");
15a207
       ptr += cursize;
15a207
       grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
15a207
       ptr += ALIGN_UP_OVERHEAD (cursize, 4);
15a207
@@ -197,7 +197,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
15a207
       goto fail;
15a207
     }
15a207
 
15a207
-  grub_tpm_measure (kernel, filelen, GRUB_KERNEL_PCR, "UEFI Linux kernel");
15a207
+  grub_tpm_measure (kernel, filelen, GRUB_BINARY_PCR, "grub_linuxefi", "Kernel");
15a207
 
15a207
   rc = grub_linuxefi_secure_validate (kernel, filelen);
15a207
   if (rc < 0)
15a207
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
15a207
index 94526966e8a..273f48a6c88 100644
15a207
--- a/grub-core/loader/i386/linux.c
15a207
+++ b/grub-core/loader/i386/linux.c
15a207
@@ -725,7 +725,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
15a207
       goto fail;
15a207
     }
15a207
 
15a207
-  grub_tpm_measure (kernel, len, GRUB_KERNEL_PCR, "Linux Kernel");
15a207
+  grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux", "Kernel");
15a207
 
15a207
   grub_memcpy (&lh, kernel, sizeof (lh));
15a207
 
15a207
diff --git a/grub-core/loader/i386/multiboot_mbi.c b/grub-core/loader/i386/multiboot_mbi.c
15a207
index 1c5b0ac25ce..2ce424a997b 100644
15a207
--- a/grub-core/loader/i386/multiboot_mbi.c
15a207
+++ b/grub-core/loader/i386/multiboot_mbi.c
15a207
@@ -174,7 +174,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
15a207
       return grub_errno;
15a207
     }
15a207
 
15a207
-  grub_tpm_measure((unsigned char*)buffer, len, GRUB_KERNEL_PCR, filename);
15a207
+  grub_tpm_measure((unsigned char*)buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
15a207
 
15a207
   header = find_header (buffer, len);
15a207
 
15a207
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
15a207
index 15544230763..4f8c02288ec 100644
15a207
--- a/grub-core/loader/i386/pc/linux.c
15a207
+++ b/grub-core/loader/i386/pc/linux.c
15a207
@@ -162,7 +162,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
15a207
       goto fail;
15a207
     }
15a207
 
15a207
-  grub_tpm_measure (kernel, len, GRUB_KERNEL_PCR, "BIOS Linux Kernel");
15a207
+  grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux16", "Kernel");
15a207
 
15a207
   grub_memcpy (&lh, kernel, sizeof (lh));
15a207
   kernel_offset = sizeof (lh);
15a207
diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
15a207
index 3005c0d19c9..78c41e33416 100644
15a207
--- a/grub-core/loader/linux.c
15a207
+++ b/grub-core/loader/linux.c
15a207
@@ -289,7 +289,7 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
15a207
 	  grub_initrd_close (initrd_ctx);
15a207
 	  return grub_errno;
15a207
 	}
15a207
-      grub_tpm_measure (ptr, cursize, GRUB_INITRD_PCR, "Linux Initrd");
15a207
+      grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_initrd", "Initrd");
15a207
       ptr += cursize;
15a207
     }
15a207
   if (newc)
15a207
diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c
15a207
index 93e026c53db..3b87dc4fab3 100644
15a207
--- a/grub-core/loader/multiboot.c
15a207
+++ b/grub-core/loader/multiboot.c
15a207
@@ -441,7 +441,7 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
15a207
     }
15a207
 
15a207
   grub_file_close (file);
15a207
-  grub_tpm_measure (module, size, GRUB_KERNEL_PCR, argv[0]);
15a207
+  grub_tpm_measure (module, size, GRUB_BINARY_PCR, "grub_multiboot", argv[0]);
15a207
   return GRUB_ERR_NONE;
15a207
 }
15a207
 
15a207
diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c
15a207
index d5ad02a33a1..60ae4606fda 100644
15a207
--- a/grub-core/loader/multiboot_mbi2.c
15a207
+++ b/grub-core/loader/multiboot_mbi2.c
15a207
@@ -132,7 +132,7 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
15a207
 
15a207
   COMPILE_TIME_ASSERT (MULTIBOOT_HEADER_ALIGN % 4 == 0);
15a207
 
15a207
-  grub_tpm_measure ((unsigned char *)buffer, len, GRUB_KERNEL_PCR, filename);
15a207
+  grub_tpm_measure ((unsigned char *)mld.buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
15a207
 
15a207
   header = find_header (mld.buffer, len);
15a207
   if (header == 0)
15a207
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
15a207
index 9ae04a05160..976643c47b0 100644
15a207
--- a/grub-core/script/execute.c
15a207
+++ b/grub-core/script/execute.c
15a207
@@ -995,8 +995,8 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
15a207
 				   argv.args[i]);
15a207
   }
15a207
   cmdstring[cmdlen-1]= '\0';
15a207
-  grub_tpm_measure ((unsigned char *)cmdstring, cmdlen, GRUB_COMMAND_PCR,
15a207
-		    cmdstring);
15a207
+  grub_tpm_measure ((unsigned char *)cmdstring, cmdlen, GRUB_ASCII_PCR,
15a207
+		    "grub_cmd", cmdstring);
15a207
   grub_free(cmdstring);
15a207
   invert = 0;
15a207
   argc = argv.argc - 1;
15a207
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
15a207
index 7fc9d77d277..ecb2d09ff5d 100644
15a207
--- a/include/grub/tpm.h
15a207
+++ b/include/grub/tpm.h
15a207
@@ -26,11 +26,8 @@
15a207
 #define TPM_AUTHFAIL (TPM_BASE + 0x1)
15a207
 #define TPM_BADINDEX (TPM_BASE + 0x2)
15a207
 
15a207
-#define GRUB_TPM_PCR 9
15a207
-#define GRUB_KERNEL_PCR 10
15a207
-#define GRUB_INITRD_PCR 11
15a207
-#define GRUB_CMDLINE_PCR 12
15a207
-#define GRUB_COMMAND_PCR 13
15a207
+#define GRUB_ASCII_PCR 8
15a207
+#define GRUB_BINARY_PCR 9
15a207
 
15a207
 #define TPM_TAG_RQU_COMMAND 0x00C1
15a207
 #define TPM_ORD_Extend 0x14
15a207
@@ -70,7 +67,7 @@ typedef struct {
15a207
 } GRUB_PACKED ExtendOutgoing;
15a207
 
15a207
 grub_err_t EXPORT_FUNC(grub_tpm_measure) (unsigned char *buf, grub_size_t size,
15a207
-					  grub_uint8_t pcr,
15a207
+					  grub_uint8_t pcr, const char *kind,
15a207
 					  const char *description);
15a207
 #if defined (GRUB_MACHINE_EFI) || defined (GRUB_MACHINE_PCBIOS)
15a207
 grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,