tpm: Disable tpm verifier if tpm is not present
PATCH v4 1/1 tpm: Disable tpm verifier if tpm is not present
When the TPM module is loaded, the verifier reads the entire file into memory, measures and extends the hash, and uses the verified content as a backing buffer for disk files. However, this process can result in a high memory utilization cost per file operation, sometimes causing the system to run out of memory, which can lead to boot failure. To address this issue, previous patches have optimized memory management by dynamically allocating heap space to maximize memory usage and reduce the threat of memory exhaustion. But in some cases, problems may still arise, such as when large ISO images are mounted using loopback or when dealing with embedded systems with limited memory resources.
Unfortunately, the current implementation of the TPM module doesn't allow for the elimination of the back buffer once it is loaded, even if no TPM device is present or the device has been explicitly disabled. This can lead to wasted memory. To solve this issue, a patch has been developed to detect the TPM status at the time of loading and skip verifier registration if the device is missing or deactivated. This prevents the allocation of memory for a back buffer, avoiding wasted memory when no real measure boot functionality is performed. This patch also provides users with the option to disable the TPM device to free up memory in scenarios where the system can't afford the high memory utilization cost.
--- grub-core/commands/efi/tpm.c | 37 +++++++++++++++++++++++++++ grub-core/commands/ieee1275/ibmvtpm.c | 20 +++++++-------- grub-core/commands/tpm.c | 11 ++++++++ include/grub/tpm.h | 1 + 4 files changed, 59 insertions(+), 10 deletions(-)
grub-core/commands/efi/tpm.c +37 -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 (void ) +{ +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/ieee1275/ibmvtpm.c +10 -10
@@ -135,16 +135,6 @@ grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, const char *description) { -/* -* Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes -* can be found. -*/ - grub_err_t err = tpm_init (); - -/* Absence of a TPM isn't a failure. */ -if (err !=GRUB_ERR_NONE ) -return GRUB_ERR_NONE ; - grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n", pcr, size, description);
@@ -153,3 +143,13 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,return GRUB_ERR_NONE ; } + +int +grub_tpm_present (void ) +{ +/* +* Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes +* can be found. +*/ +return tpm_init () ==GRUB_ERR_NONE ; +}
grub-core/commands/tpm.c +11 -0
@@ -103,10 +103,21 @@ struct grub_file_verifier grub_tpm_verifier = {GRUB_MOD_INIT (tpm) { +/* +* Even though this now calls ibmvtpm's grub_tpm_present() from +* GRUB_MOD_INIT(), it does seem to call it late enough in the initialization +* sequence so that whatever discovered 'device nodes' before this +* GRUB_MOD_INIT() is called, enables the ibmvtpm driver to see the device +* nodes. +*/ +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
@@ -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.39.2
Quoted 6 lines · Michael Chang via Grub-devel
When the TPM module is loaded, the verifier reads the entire file into memory, measures and extends the hash, and uses the verified content as a backing buffer for disk files. However, this process can result in a high memory utilization cost per file operation, sometimes causing the system to run out of memory, which can lead to boot failure. To address this issue, previous patches have optimized memory management by
I would mention at least commit 887f98f0d (mm: Allow dynamically requesting additional memory regions) here.
Quoted 5 lines
dynamically allocating heap space to maximize memory usage and reduce the threat of memory exhaustion. But in some cases, problems may still arise, such as when large ISO images are mounted using loopback or when dealing with embedded systems with limited memory resources.
Quoted 11 lines
Unfortunately, the current implementation of the TPM module doesn't allow for the elimination of the back buffer once it is loaded, even if no TPM device is present or the device has been explicitly disabled. This can lead to wasted memory. To solve this issue, a patch has been developed to detect the TPM status at the time of loading and skip verifier registration if the device is missing or deactivated. This prevents the allocation of memory for a back buffer, avoiding wasted memory when no real measure boot functionality is performed. This patch also provides users with the option to disable the TPM device to free up memory in scenarios where the system can't afford the high memory utilization cost.
The last sentence is confusing because it gives an impression the patch adds an option to the GRUB to "disable the TPM device". Which of course is not true. I expect you wanted to say something like that: "disabling the TPM device in the system reduces memory usage in the GRUB. This can be useful in scenarios where the system can't afford the high memory utilization cost and nobody cares about the measurements of loaded artifacts."
Otherwise patch LGTM...
Daniel
Quoted 10 lines · Daniel Kiper
Quoted 6 lines · Michael Chang via Grub-devel
When the TPM module is loaded, the verifier reads the entire file into memory, measures and extends the hash, and uses the verified content as a backing buffer for disk files. However, this process can result in a high memory utilization cost per file operation, sometimes causing the system to run out of memory, which can lead to boot failure. To address this issue, previous patches have optimized memory management by
I would mention at least commit
887f98f0d(mm: Allow dynamically requesting additional memory regions) here.
OK. I will do it.
Quoted 18 lines
Quoted 6 lines
dynamically allocating heap space to maximize memory usage and reduce the threat of memory exhaustion. But in some cases, problems may still arise, such as when large ISO images are mounted using loopback or when dealing with embedded systems with limited memory resources.
Quoted 11 lines
Unfortunately, the current implementation of the TPM module doesn't allow for the elimination of the back buffer once it is loaded, even if no TPM device is present or the device has been explicitly disabled. This can lead to wasted memory. To solve this issue, a patch has been developed to detect the TPM status at the time of loading and skip verifier registration if the device is missing or deactivated. This prevents the allocation of memory for a back buffer, avoiding wasted memory when no real measure boot functionality is performed. This patch also provides users with the option to disable the TPM device to free up memory in scenarios where the system can't afford the high memory utilization cost.
Quoted 7 lines
The last sentence is confusing because it gives an impression the patch adds an option to the GRUB to "disable the TPM device". Which of course is not true. I expect you wanted to say something like that: "disabling the TPM device in the system reduces memory usage in the GRUB. This can be useful in scenarios where the system can't afford the high memory utilization cost and nobody cares about the measurements of loaded artifacts."
Initially, I want to emphasize that after this proposed change, disabling TPM will become an option to free up memory for others. However, my previous expression may have been confusing, and I apologize for that.
Thank you for your review, and I have updated the v5 patch to address your comment.
Regards,
Michael
Quoted 9 lines
Otherwise patch LGTM...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel