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