15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Matthew Garrett <mjg59@coreos.com>
15a207
Date: Thu, 13 Oct 2016 13:55:26 -0700
15a207
Subject: [PATCH] Make TPM errors less fatal
15a207
15a207
Handle TPM errors, and stop trying to use the TPM once we hit one.
15a207
---
15a207
 grub-core/kern/dl.c                   |  1 +
15a207
 grub-core/kern/i386/pc/tpm.c          | 21 +++++++++++++++++----
15a207
 grub-core/lib/cmdline.c               |  1 +
15a207
 grub-core/loader/i386/efi/linux.c     |  2 ++
15a207
 grub-core/loader/i386/linux.c         |  1 +
15a207
 grub-core/loader/i386/multiboot_mbi.c |  1 +
15a207
 grub-core/loader/i386/pc/linux.c      |  1 +
15a207
 grub-core/loader/linux.c              |  2 ++
15a207
 grub-core/loader/multiboot.c          |  1 +
15a207
 grub-core/loader/multiboot_mbi2.c     |  1 +
15a207
 grub-core/script/execute.c            |  1 +
15a207
 11 files changed, 29 insertions(+), 4 deletions(-)
15a207
15a207
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
15a207
index d0989573866..91105bc4677 100644
15a207
--- a/grub-core/kern/dl.c
15a207
+++ b/grub-core/kern/dl.c
15a207
@@ -831,6 +831,7 @@ grub_dl_load_file (const char *filename)
15a207
   grub_file_close (file);
15a207
 
15a207
   grub_tpm_measure(core, size, GRUB_BINARY_PCR, "grub_module", filename);
15a207
+  grub_print_error();
15a207
 
15a207
   mod = grub_dl_load_core (core, size);
15a207
   grub_free (core);
15a207
diff --git a/grub-core/kern/i386/pc/tpm.c b/grub-core/kern/i386/pc/tpm.c
15a207
index 8c6c1e6ece2..f6f264aff2e 100644
15a207
--- a/grub-core/kern/i386/pc/tpm.c
15a207
+++ b/grub-core/kern/i386/pc/tpm.c
15a207
@@ -7,21 +7,28 @@
15a207
 
15a207
 #define TCPA_MAGIC 0x41504354
15a207
 
15a207
+static int tpm_presence = -1;
15a207
+
15a207
 int tpm_present(void);
15a207
 
15a207
 int tpm_present(void)
15a207
 {
15a207
   struct grub_bios_int_registers regs;
15a207
 
15a207
+  if (tpm_presence != -1)
15a207
+    return tpm_presence;
15a207
+
15a207
   regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
15a207
   regs.eax = 0xbb00;
15a207
   regs.ebx = TCPA_MAGIC;
15a207
   grub_bios_interrupt (0x1a, ®s);
15a207
 
15a207
   if (regs.eax == 0)
15a207
-    return 1;
15a207
+    tpm_presence = 1;
15a207
+  else
15a207
+    tpm_presence = 0;
15a207
 
15a207
-  return 0;
15a207
+  return tpm_presence;
15a207
 }
15a207
 
15a207
 grub_err_t
15a207
@@ -49,7 +56,10 @@ grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
15a207
   grub_bios_interrupt (0x1a, ®s);
15a207
 
15a207
   if (regs.eax)
15a207
-    return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
15a207
+    {
15a207
+	tpm_presence = 0;
15a207
+	return grub_error (GRUB_ERR_IO, N_("TPM error %x, disabling TPM"), regs.eax);
15a207
+    }
15a207
 
15a207
   return 0;
15a207
 }
15a207
@@ -126,7 +136,10 @@ grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
15a207
 	grub_free(event);
15a207
 
15a207
 	if (regs.eax)
15a207
-		return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
15a207
+	  {
15a207
+		tpm_presence = 0;
15a207
+		return grub_error (GRUB_ERR_IO, N_("TPM error %x, disabling TPM"), regs.eax);
15a207
+	  }
15a207
 
15a207
 	return 0;
15a207
 }
15a207
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
15a207
index 178f7382f07..d5c12957cad 100644
15a207
--- a/grub-core/lib/cmdline.c
15a207
+++ b/grub-core/lib/cmdline.c
15a207
@@ -128,6 +128,7 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
15a207
 
15a207
   grub_tpm_measure ((void *)orig, grub_strlen (orig), GRUB_ASCII_PCR,
15a207
 		    "grub_kernel_cmdline", orig);
15a207
+  grub_print_error();
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 074dbd6513e..ea9f5134e67 100644
15a207
--- a/grub-core/loader/i386/efi/linux.c
15a207
+++ b/grub-core/loader/i386/efi/linux.c
15a207
@@ -133,6 +133,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
15a207
           goto fail;
15a207
         }
15a207
       grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_linuxefi", "Initrd");
15a207
+      grub_print_error();
15a207
       ptr += cursize;
15a207
       grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
15a207
       ptr += ALIGN_UP_OVERHEAD (cursize, 4);
15a207
@@ -198,6 +199,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
15a207
     }
15a207
 
15a207
   grub_tpm_measure (kernel, filelen, GRUB_BINARY_PCR, "grub_linuxefi", "Kernel");
15a207
+  grub_print_error();
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 273f48a6c88..76304f05700 100644
15a207
--- a/grub-core/loader/i386/linux.c
15a207
+++ b/grub-core/loader/i386/linux.c
15a207
@@ -726,6 +726,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
15a207
     }
15a207
 
15a207
   grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux", "Kernel");
15a207
+  grub_print_error();
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 2ce424a997b..ca85358f771 100644
15a207
--- a/grub-core/loader/i386/multiboot_mbi.c
15a207
+++ b/grub-core/loader/i386/multiboot_mbi.c
15a207
@@ -175,6 +175,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
15a207
     }
15a207
 
15a207
   grub_tpm_measure((unsigned char*)buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
15a207
+  grub_print_error();
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 4f8c02288ec..cfff25c21b5 100644
15a207
--- a/grub-core/loader/i386/pc/linux.c
15a207
+++ b/grub-core/loader/i386/pc/linux.c
15a207
@@ -163,6 +163,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
15a207
     }
15a207
 
15a207
   grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux16", "Kernel");
15a207
+  grub_print_error();
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 78c41e33416..c2c7cfcd0fd 100644
15a207
--- a/grub-core/loader/linux.c
15a207
+++ b/grub-core/loader/linux.c
15a207
@@ -290,6 +290,8 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
15a207
 	  return grub_errno;
15a207
 	}
15a207
       grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_initrd", "Initrd");
15a207
+      grub_print_error();
15a207
+
15a207
       ptr += cursize;
15a207
     }
15a207
   if (newc)
15a207
diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c
15a207
index 3b87dc4fab3..9a8dae5565b 100644
15a207
--- a/grub-core/loader/multiboot.c
15a207
+++ b/grub-core/loader/multiboot.c
15a207
@@ -442,6 +442,7 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
15a207
 
15a207
   grub_file_close (file);
15a207
   grub_tpm_measure (module, size, GRUB_BINARY_PCR, "grub_multiboot", argv[0]);
15a207
+  grub_print_error();
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 60ae4606fda..54078455e2f 100644
15a207
--- a/grub-core/loader/multiboot_mbi2.c
15a207
+++ b/grub-core/loader/multiboot_mbi2.c
15a207
@@ -133,6 +133,7 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
15a207
   COMPILE_TIME_ASSERT (MULTIBOOT_HEADER_ALIGN % 4 == 0);
15a207
 
15a207
   grub_tpm_measure ((unsigned char *)mld.buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
15a207
+  grub_print_error();
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 976643c47b0..93965777138 100644
15a207
--- a/grub-core/script/execute.c
15a207
+++ b/grub-core/script/execute.c
15a207
@@ -997,6 +997,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
15a207
   cmdstring[cmdlen-1]= '\0';
15a207
   grub_tpm_measure ((unsigned char *)cmdstring, cmdlen, GRUB_ASCII_PCR,
15a207
 		    "grub_cmd", cmdstring);
15a207
+  grub_print_error();
15a207
   grub_free(cmdstring);
15a207
   invert = 0;
15a207
   argc = argv.argc - 1;