Skip to content

Commit 3fdbfff

Browse files
WenhuaChanglsandov1
authored andcommitted
tpm: Disable the tpm verifier if the TPM device is not present
When the tpm module is loaded, the verifier reads entire file into memory, measures it and uses verified content as a backing buffer for file accesses. However, this process may result in high memory utilization for file operations, sometimes causing a system to run out of memory which may finally lead to boot failure. To address this issue, among others, the commit 887f98f (mm: Allow dynamically requesting additional memory regions) have optimized memory management by dynamically allocating heap space to maximize memory usage and reduce threat of memory exhaustion. But in some cases problems may still arise, e.g., when large ISO images are mounted using loopback or when dealing with embedded systems with limited memory resources. Unfortunately current implementation of the tpm module doesn't allow elimination of the back buffer once it is loaded. Even if the TPM device is not present or it has been explicitly disabled. This may unnecessary allocate a lot memory. To solve this issue, a patch has been developed to detect the TPM status at module load and skip verifier registration if the device is missing or deactivated. This prevents allocation of memory for the back buffer, avoiding wasting memory when no real measure boot functionality is performed. Disabling the TPM device in the system can reduce memory usage in the GRUB. It is useful in scenarios where high memory utilization is a concern and measurements of loaded artifacts are not necessary. Signed-off-by: Michael Chang <[email protected]> Signed-off-by: Stefan Berger <[email protected]> Reviewed-by: Daniel Kiper <[email protected]> (cherry picked from commit 30708df)
1 parent 49d027e commit 3fdbfff

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

grub-core/commands/efi/tpm.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,40 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
287287
else
288288
return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description);
289289
}
290+
291+
int
292+
grub_tpm_present (void)
293+
{
294+
grub_efi_handle_t tpm_handle;
295+
grub_efi_uint8_t protocol_version;
296+
297+
if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
298+
return 0;
299+
300+
if (protocol_version == 1)
301+
{
302+
grub_efi_tpm_protocol_t *tpm;
303+
304+
tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
305+
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
306+
if (!tpm)
307+
{
308+
grub_dprintf ("tpm", "Cannot open TPM protocol\n");
309+
return 0;
310+
}
311+
return grub_tpm1_present (tpm);
312+
}
313+
else
314+
{
315+
grub_efi_tpm2_protocol_t *tpm;
316+
317+
tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
318+
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
319+
if (!tpm)
320+
{
321+
grub_dprintf ("tpm", "Cannot open TPM protocol\n");
322+
return 0;
323+
}
324+
return grub_tpm2_present (tpm);
325+
}
326+
}

grub-core/commands/ieee1275/ibmvtpm.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,6 @@ grub_err_t
135135
grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
136136
const char *description)
137137
{
138-
/*
139-
* Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes
140-
* can be found.
141-
*/
142-
grub_err_t err = tpm_init ();
143-
144-
/* Absence of a TPM isn't a failure. */
145-
if (err != GRUB_ERR_NONE)
146-
return GRUB_ERR_NONE;
147-
148138
grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
149139
pcr, size, description);
150140

@@ -153,3 +143,13 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
153143

154144
return GRUB_ERR_NONE;
155145
}
146+
147+
int
148+
grub_tpm_present (void)
149+
{
150+
/*
151+
* Call tpm_init() "late" rather than from GRUB_MOD_INIT() so that device nodes
152+
* can be found.
153+
*/
154+
return tpm_init() == GRUB_ERR_NONE;
155+
}

grub-core/commands/tpm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ struct grub_file_verifier grub_tpm_verifier = {
8686

8787
GRUB_MOD_INIT (tpm)
8888
{
89+
/*
90+
* Even though this now calls ibmvtpm's grub_tpm_present() from GRUB_MOD_INIT(),
91+
* it does seem to call it late enough in the initialization sequence so
92+
* that whatever discovered "device nodes" before this GRUB_MOD_INIT() is
93+
* called, enables the ibmvtpm driver to see the device nodes.
94+
*/
95+
if (!grub_tpm_present())
96+
return;
8997
grub_verifier_register (&grub_tpm_verifier);
9098
}
9199

92100
GRUB_MOD_FINI (tpm)
93101
{
102+
if (!grub_tpm_present())
103+
return;
94104
grub_verifier_unregister (&grub_tpm_verifier);
95105
}

include/grub/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636

3737
grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size,
3838
grub_uint8_t pcr, const char *description);
39+
int grub_tpm_present (void);
3940
#endif

0 commit comments

Comments
 (0)