Blame SOURCES/0139-Make-TPM-errors-less-fatal.patch

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