15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Matthew Garrett <mjg59@srcf.ucam.org>
15a207
Date: Mon, 10 Aug 2015 15:27:12 -0700
15a207
Subject: [PATCH] Measure commands
15a207
15a207
Measure each command executed by grub, which includes script execution.
15a207
---
15a207
 grub-core/script/execute.c | 25 +++++++++++++++++++++++--
15a207
 include/grub/tpm.h         |  1 +
15a207
 2 files changed, 24 insertions(+), 2 deletions(-)
15a207
15a207
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
15a207
index cf6cd6601d6..9ae04a05160 100644
15a207
--- a/grub-core/script/execute.c
15a207
+++ b/grub-core/script/execute.c
15a207
@@ -30,6 +30,7 @@
15a207
 #ifdef GRUB_MACHINE_IEEE1275
15a207
 #include <grub ieee1275="" ieee1275.h="">
15a207
 #endif
15a207
+#include <grub tpm.h="">
15a207
 
15a207
 /* Max digits for a char is 3 (0xFF is 255), similarly for an int it
15a207
    is sizeof (int) * 3, and one extra for a possible -ve sign.  */
15a207
@@ -967,8 +968,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
15a207
   grub_err_t ret = 0;
15a207
   grub_script_function_t func = 0;
15a207
   char errnobuf[18];
15a207
-  char *cmdname;
15a207
-  int argc;
15a207
+  char *cmdname, *cmdstring;
15a207
+  int argc, offset = 0, cmdlen = 0;
15a207
+  unsigned int i;
15a207
   char **args;
15a207
   int invert;
15a207
   struct grub_script_argv argv = { 0, 0, 0 };
15a207
@@ -977,6 +979,25 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
15a207
   if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
15a207
     return grub_errno;
15a207
 
15a207
+  for (i = 0; i < argv.argc; i++) {
15a207
+	  cmdlen += grub_strlen (argv.args[i]) + 1;
15a207
+  }
15a207
+
15a207
+  cmdstring = grub_malloc (cmdlen);
15a207
+  if (!cmdstring)
15a207
+  {
15a207
+	  return grub_error (GRUB_ERR_OUT_OF_MEMORY,
15a207
+			     N_("cannot allocate command buffer"));
15a207
+  }
15a207
+
15a207
+  for (i = 0; i < argv.argc; i++) {
15a207
+	  offset += grub_snprintf (cmdstring + offset, cmdlen - offset, "%s ",
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_free(cmdstring);
15a207
   invert = 0;
15a207
   argc = argv.argc - 1;
15a207
   args = argv.args + 1;
15a207
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
15a207
index 40d3cf65ba6..7fc9d77d277 100644
15a207
--- a/include/grub/tpm.h
15a207
+++ b/include/grub/tpm.h
15a207
@@ -30,6 +30,7 @@
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
 
15a207
 #define TPM_TAG_RQU_COMMAND 0x00C1
15a207
 #define TPM_ORD_Extend 0x14