Skip to contentSkip to conversation

tpm: Disable tpm verifier if tpm is not present

PATCH Superseded Michael Chang 2 participants 3 messages Sep 8, 2022 – Oct 7, 2022

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: Disable tpm verifier if tpm is not present

This helps to prevent out of memory error when reading large files via disablig tpm device as verifier has to read all content into memory in one chunk to measure the hash and extend to tpm.

---
 grub-core/commands/efi/tpm.c | 37 ++++++++++++++++++++++++++++++++++++
 grub-core/commands/tpm.c     |  4 ++++
 include/grub/tpm.h           |  1 +
 3 files changed, 42 insertions(+)
grub-core/commands/efi/tpm.c +37 -0
index ae09c1bf8b..4f0011f6f5 100644
@@ -287,3 +287,40 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
   else
     return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description);
 }
+
+int
+grub_tpm_present ()
+{
+  grub_efi_handle_t tpm_handle;
+  grub_efi_uint8_t protocol_version;
+
+  if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
+    return 0;
+
+  if (protocol_version == 1)
+    {
+      grub_efi_tpm_protocol_t *tpm;
+
+      tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
+				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+      if (!tpm)
+	{
+	  grub_dprintf ("tpm", "Cannot open TPM protocol\n");
+	  return 0;
+	}
+      return grub_tpm1_present (tpm);
+    }
+  else
+    {
+      grub_efi_tpm2_protocol_t *tpm;
+
+      tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
+				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+      if (!tpm)
+	{
+	  grub_dprintf ("tpm", "Cannot open TPM protocol\n");
+	  return 0;
+	}
+      return grub_tpm2_present (tpm);
+    }
+}
grub-core/commands/tpm.c +4 -0
index 2052c36eab..cb8ed6b949 100644
@@ -86,10 +86,14 @@ struct grub_file_verifier grub_tpm_verifier = {
 
 GRUB_MOD_INIT (tpm)
 {
+  if (!grub_tpm_present())
+    return;
   grub_verifier_register (&grub_tpm_verifier);
 }
 
 GRUB_MOD_FINI (tpm)
 {
+  if (!grub_tpm_present())
+    return;
   grub_verifier_unregister (&grub_tpm_verifier);
 }
include/grub/tpm.h +1 -0
index 5c285cbc52..c19fcbd0a6 100644
@@ -36,4 +36,5 @@
 
 grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size,
 			     grub_uint8_t pcr, const char *description);
+int grub_tpm_present (void);
 #endif
Signature · 1 line
2.35.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Michael Chang via Grub-devel

This helps to prevent out of memory error when reading large files via disablig tpm device as verifier has to read all content into memory in one chunk to measure the hash and extend to tpm.

---
  grub-core/commands/efi/tpm.c | 37 ++++++++++++++++++++++++++++++++++++
  grub-core/commands/tpm.c     |  4 ++++
  include/grub/tpm.h           |  1 +
  3 files changed, 42 insertions(+)
grub-core/commands/efi/tpm.c +0 -0
index ae09c1bf8b..4f0011f6f5 100644

grub-core/commands/efi/tpm.c grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, · line 292

Quoted 6 lines
grub-core/commands/efi/tpm.c +3 -0
@@ -287,3 +287,40 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
    else
      return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description);
  }
+
+int
+grub_tpm_present ()

nit: void

Quoted 58 lines
grub-core/commands/efi/tpm.c +34 -0
+{
+  grub_efi_handle_t tpm_handle;
+  grub_efi_uint8_t protocol_version;
+
+  if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
+    return 0;
+
+  if (protocol_version == 1)
+    {
+      grub_efi_tpm_protocol_t *tpm;
+
+      tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
+				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+      if (!tpm)
+	{
+	  grub_dprintf ("tpm", "Cannot open TPM protocol\n");
+	  return 0;
+	}
+      return grub_tpm1_present (tpm);
+    }
+  else
+    {
+      grub_efi_tpm2_protocol_t *tpm;
+
+      tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
+				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+      if (!tpm)
+	{
+	  grub_dprintf ("tpm", "Cannot open TPM protocol\n");
+	  return 0;
+	}
+      return grub_tpm2_present (tpm);
+    }
+}
grub-core/commands/tpm.c +4 -0
index 2052c36eab..cb8ed6b949 100644
@@ -86,10 +86,14 @@ struct grub_file_verifier grub_tpm_verifier = {
 
  GRUB_MOD_INIT (tpm)
  {
+  if (!grub_tpm_present())
+    return;
    grub_verifier_register (&grub_tpm_verifier);
  }
 
  GRUB_MOD_FINI (tpm)
  {
+  if (!grub_tpm_present())
+    return;
    grub_verifier_unregister (&grub_tpm_verifier);
  }
include/grub/tpm.h +0 -0
index 5c285cbc52..c19fcbd0a6 100644

include/grub/tpm.h line 40

Quoted 5 lines
include/grub/tpm.h +1 -0
@@ -36,4 +36,5 @@
 
  grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size,
  			     grub_uint8_t pcr, const char *description);
+int grub_tpm_present (void);
  #endif
#2 lore raw
Quoted 14 lines · Stefan Berger
Quoted 11 lines · Michael Chang via Grub-devel

This helps to prevent out of memory error when reading large files via disablig tpm device as verifier has to read all content into memory in one chunk to measure the hash and extend to tpm.

---
  grub-core/commands/efi/tpm.c | 37 ++++++++++++++++++++++++++++++++++++
  grub-core/commands/tpm.c     |  4 ++++
  include/grub/tpm.h           |  1 +
  3 files changed, 42 insertions(+)
Quoted 13 lines

grub-core/commands/efi/tpm.c grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, · line 292

Quoted 11 lines
grub-core/commands/efi/tpm.c +3 -0
index ae09c1bf8b..4f0011f6f5 100644
@@ -287,3 +287,40 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
    else
      return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description);
  }
+
+int
+grub_tpm_present ()

nit: void

I'll fix in next version.

Quoted 55 lines
Quoted 55 lines
grub-core/commands/efi/tpm.c +34 -0
+{
+  grub_efi_handle_t tpm_handle;
+  grub_efi_uint8_t protocol_version;
+
+  if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
+    return 0;
+
+  if (protocol_version == 1)
+    {
+      grub_efi_tpm_protocol_t *tpm;
+
+      tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
+				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+      if (!tpm)
+	{
+	  grub_dprintf ("tpm", "Cannot open TPM protocol\n");
+	  return 0;
+	}
+      return grub_tpm1_present (tpm);
+    }
+  else
+    {
+      grub_efi_tpm2_protocol_t *tpm;
+
+      tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
+				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+      if (!tpm)
+	{
+	  grub_dprintf ("tpm", "Cannot open TPM protocol\n");
+	  return 0;
+	}
+      return grub_tpm2_present (tpm);
+    }
+}
grub-core/commands/tpm.c +4 -0
index 2052c36eab..cb8ed6b949 100644
@@ -86,10 +86,14 @@ struct grub_file_verifier grub_tpm_verifier = {
  GRUB_MOD_INIT (tpm)
  {
+  if (!grub_tpm_present())
+    return;
    grub_verifier_register (&grub_tpm_verifier);
  }
  GRUB_MOD_FINI (tpm)
  {
+  if (!grub_tpm_present())
+    return;
    grub_verifier_unregister (&grub_tpm_verifier);
  }
include/grub/tpm.h +0 -1
index 5c285cbc52..c19fcbd0a6 100644
--- a/include/grub/tpm.h
Quoted 8 lines

include/grub/tpm.h line 39

Quoted 6 lines

+++ b/include/grub/tpm.h

include/grub/tpm.h +1 -0
@@ -36,4 +36,5 @@
  grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size,
  			     grub_uint8_t pcr, const char *description);
+int grub_tpm_present (void);
  #endif

I will add your Reviewed-by in next version too.

Thanks,
Michael