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