Skip to contentSkip to conversation

tpm: Don't propagate measurement failures to the verifiers layer

PATCH Superseded Robbie Harwood 2 participants 3 messages Oct 31, 2022 – Nov 3, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover tpm: Don't propagate measurement failures to the verifiers layer

Address Daniel's and James's feedback on previous version by adding an environment variable to restore the TPM hard failure behavior. Interdiff attached.

Be well,
--Robbie

Robbie Harwood (1):
tpm: Don't propagate measurement failures to the verifiers layer

 docs/grub.texi           |  9 +++++++++
 grub-core/commands/tpm.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)

Interdiff against v2:

docs/grub.texi +9 -0
index 2d6cd83580..eb43d8970d 100644
@@ -3318,6 +3318,7 @@ These variables have special meaning to GRUB.
 * theme::
 * timeout::
 * timeout_style::
+* tpm_fail_fatal::
 @end menu
 
 
@@ -3825,6 +3826,14 @@ displaying the menu.  See the documentation of @samp{GRUB_TIMEOUT_STYLE}
 (@pxref{Simple configuration}) for details.
 
 
+@node tpm_fail_fatal
+@subsection tpm_fail_fatal
+
+If this variable is enabled, TPM measurements that fail will be treated
+as fatal.  Otherwise, they will merely be debug-logged and boot will
+continue.
+
+
 @node Environment block
 @section The GRUB environment block
 
grub-core/commands/tpm.c +23 -6
index 24874ffacb..ca088055dd 100644
@@ -18,6 +18,7 @@
  *  Core TPM support code.
  */
 
+#include <grub/env.h>
 #include <grub/err.h>
 #include <grub/i18n.h>
 #include <grub/misc.h>
@@ -26,6 +27,7 @@
 #include <grub/term.h>
 #include <grub/verify.h>
 #include <grub/dl.h>
+#include <stdbool.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -39,14 +41,27 @@ grub_tpm_verify_init (grub_file_t io,
   return GRUB_ERR_NONE;
 }
 
+static inline bool
+is_tpm_fail_fatal (void)
+{
+  const char *val = grub_env_get ("tpm_fail_fatal");
+
+  if (val == NULL || grub_strlen (val) < 1 || grub_strcmp (val, "0") == 0 ||
+      grub_strcmp (val, "false") == 0)
+    return false;
+  return true;
+}
+
 static grub_err_t
 grub_tpm_verify_write (void *context, void *buf, grub_size_t size)
 {
   grub_err_t status = grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
 
-  if (status)
-    grub_dprintf ("tpm", "Measuring buffer failed: %d\n", status);
-  return GRUB_ERR_NONE;
+  if (status == GRUB_ERR_NONE)
+    return GRUB_ERR_NONE;
+
+  grub_dprintf ("tpm", "Measuring buffer failed: %d\n", status);
+  return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
 }
 
 static grub_err_t
@@ -77,10 +92,12 @@ grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
   status =
     grub_tpm_measure ((unsigned char *) str, grub_strlen (str),
 		      GRUB_STRING_PCR, description);
-  if (status)
-    grub_dprintf ("tpm", "Measuring string %s failed: %d\n", str, status);
   grub_free (description);
-  return GRUB_ERR_NONE;
+  if (status == GRUB_ERR_NONE)
+    return GRUB_ERR_NONE;
+
+  grub_dprintf ("tpm", "Measuring string %s failed: %d\n", str, status);
+  return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
 }
 
 struct grub_file_verifier grub_tpm_verifier = {
Signature · 1 line
2.35.1
#1 lore raw

PATCH v3 1/1 tpm: Don't propagate measurement failures to the verifiers layer

Currently if an EFI firmware fails to do a TPM measurement for a file, the error will be propagated to the verifiers framework which will prevent it to be opened. This mean that buggy firmwares will lead to the system not booting because files won't be allowed to be loaded. But a failure to do a TPM measurement isn't expected to be a fatal error that causes the system to be unbootable.

To avoid this, don't return errors from .write and .verify_string callbacks and just print a debug message in the case of a TPM measurement failure. Add an environment variable (tpm_fail_fatal) to restore the previous behavior.

---
 docs/grub.texi           |  9 +++++++++
 grub-core/commands/tpm.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)
docs/grub.texi +9 -0
index 2d6cd83580..eb43d8970d 100644
@@ -3318,6 +3318,7 @@ These variables have special meaning to GRUB.
 * theme::
 * timeout::
 * timeout_style::
+* tpm_fail_fatal::
 @end menu
 
 
@@ -3825,6 +3826,14 @@ displaying the menu.  See the documentation of @samp{GRUB_TIMEOUT_STYLE}
 (@pxref{Simple configuration}) for details.
 
 
+@node tpm_fail_fatal
+@subsection tpm_fail_fatal
+
+If this variable is enabled, TPM measurements that fail will be treated
+as fatal.  Otherwise, they will merely be debug-logged and boot will
+continue.
+
+
 @node Environment block
 @section The GRUB environment block
 
grub-core/commands/tpm.c +26 -3
index 2052c36eab..ca088055dd 100644
@@ -18,6 +18,7 @@
  *  Core TPM support code.
  */
 
+#include <grub/env.h>
 #include <grub/err.h>
 #include <grub/i18n.h>
 #include <grub/misc.h>
@@ -26,6 +27,7 @@
 #include <grub/term.h>
 #include <grub/verify.h>
 #include <grub/dl.h>
+#include <stdbool.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -39,10 +41,27 @@ grub_tpm_verify_init (grub_file_t io,
   return GRUB_ERR_NONE;
 }
 
+static inline bool
+is_tpm_fail_fatal (void)
+{
+  const char *val = grub_env_get ("tpm_fail_fatal");
+
+  if (val == NULL || grub_strlen (val) < 1 || grub_strcmp (val, "0") == 0 ||
+      grub_strcmp (val, "false") == 0)
+    return false;
+  return true;
+}
+
 static grub_err_t
 grub_tpm_verify_write (void *context, void *buf, grub_size_t size)
 {
-  return grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
+  grub_err_t status = grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
+
+  if (status == GRUB_ERR_NONE)
+    return GRUB_ERR_NONE;
+
+  grub_dprintf ("tpm", "Measuring buffer failed: %d\n", status);
+  return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
 }
 
 static grub_err_t
@@ -66,7 +85,7 @@ grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
     }
   description = grub_malloc (grub_strlen (str) + grub_strlen (prefix) + 1);
   if (!description)
-    return grub_errno;
+    return GRUB_ERR_NONE;
   grub_memcpy (description, prefix, grub_strlen (prefix));
   grub_memcpy (description + grub_strlen (prefix), str,
 	       grub_strlen (str) + 1);
@@ -74,7 +93,11 @@ grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
     grub_tpm_measure ((unsigned char *) str, grub_strlen (str),
 		      GRUB_STRING_PCR, description);
   grub_free (description);
-  return status;
+  if (status == GRUB_ERR_NONE)
+    return GRUB_ERR_NONE;
+
+  grub_dprintf ("tpm", "Measuring string %s failed: %d\n", str, status);
+  return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
 }
 
 struct grub_file_verifier grub_tpm_verifier = {
Signature · 1 line
2.35.1
#2 lore raw
Quoted 33 lines · Robbie Harwood

Currently if an EFI firmware fails to do a TPM measurement for a file, the error will be propagated to the verifiers framework which will prevent it to be opened. This mean that buggy firmwares will lead to the system not booting because files won't be allowed to be loaded. But a failure to do a TPM measurement isn't expected to be a fatal error that causes the system to be unbootable.

To avoid this, don't return errors from .write and .verify_string callbacks and just print a debug message in the case of a TPM measurement failure. Add an environment variable (tpm_fail_fatal) to restore the previous behavior.

---
 docs/grub.texi           |  9 +++++++++
 grub-core/commands/tpm.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)
docs/grub.texi +1 -0
index 2d6cd83580..eb43d8970d 100644
@@ -3318,6 +3318,7 @@ These variables have special meaning to GRUB.
 * theme::
 * timeout::
 * timeout_style::
+* tpm_fail_fatal::
 @end menu
 
 

docs/grub.texi displaying the menu. See the documentation of @samp{GRUB_TIMEOUT_STYLE} · line 3832

Quoted 7 lines
docs/grub.texi +4 -0
@@ -3825,6 +3826,14 @@ displaying the menu.  See the documentation of @samp{GRUB_TIMEOUT_STYLE}
 (@pxref{Simple configuration}) for details.
 
 
+@node tpm_fail_fatal
+@subsection tpm_fail_fatal
+
+If this variable is enabled, TPM measurements that fail will be treated

What do you mean by enabled? Please align description in doc with the code below.

Quoted 28 lines
docs/grub.texi +4 -0
+as fatal.  Otherwise, they will merely be debug-logged and boot will
+continue.
+
+
 @node Environment block
 @section The GRUB environment block
 
grub-core/commands/tpm.c +2 -0
index 2052c36eab..ca088055dd 100644
@@ -18,6 +18,7 @@
  *  Core TPM support code.
  */
 
+#include <grub/env.h>
 #include <grub/err.h>
 #include <grub/i18n.h>
 #include <grub/misc.h>
@@ -26,6 +27,7 @@
 #include <grub/term.h>
 #include <grub/verify.h>
 #include <grub/dl.h>
+#include <stdbool.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 

grub-core/commands/tpm.c grub_tpm_verify_init (grub_file_t io, · line 50

Quoted 10 lines
grub-core/commands/tpm.c +7 -0
@@ -39,10 +41,27 @@ grub_tpm_verify_init (grub_file_t io,
   return GRUB_ERR_NONE;
 }
 
+static inline bool
+is_tpm_fail_fatal (void)
+{
+  const char *val = grub_env_get ("tpm_fail_fatal");
+
+  if (val == NULL || grub_strlen (val) < 1 || grub_strcmp (val, "0") == 0 ||
+      grub_strcmp (val, "false") == 0)

I would add "disable" and "no" to the list.

Quoted 19 lines
grub-core/commands/tpm.c +11 -1
+    return false;
+  return true;
+}
+
 static grub_err_t
 grub_tpm_verify_write (void *context, void *buf, grub_size_t size)
 {
-  return grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
+  grub_err_t status = grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
+
+  if (status == GRUB_ERR_NONE)
+    return GRUB_ERR_NONE;
+
+  grub_dprintf ("tpm", "Measuring buffer failed: %d\n", status);
+  return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
 }
 
 static grub_err_t

grub-core/commands/tpm.c grub_tpm_verify_string (char *str, enum grub_verify_string_type type) · line 88

Quoted 5 lines
grub-core/commands/tpm.c +1 -1
@@ -66,7 +85,7 @@ grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
     }
   description = grub_malloc (grub_strlen (str) + grub_strlen (prefix) + 1);
   if (!description)
-    return grub_errno;
+    return GRUB_ERR_NONE;

This change seems wrong to me. I think it should be dropped.

Daniel