Close uefi protocols after usage
cover Close uefi protocols after usage
From: Khalid Ali <[email protected]>
Currently, Grub when it opens a protocol with OpenProtocol() UEFI services it doesn't close those protocols, this causes internal firmware resources used and allocated by OpenPrototcol() to get leaked.
As per UEFI spec after OpenProtocol() is called and protocol is finished with usage the caller should call CloseProtocol () to free firmware resource and update global databases.
Grub currently has assumption that ExitBootService () will free resources, however it is not granteed to do so. Also, if grub fails it may call Exit () boot services to return to firmware, however in that case then firmware resources will be leaked. On tianocore edk2, this considered a bug and already were serveral patches.
To prevent that, Grub should always close opened protocols after there usage is finished.
This patch set resolves that by calling CloseProtocol () always after protocol is used. Also, it is added two wrappers to close device path and loaded image protocol the opposite of wrappers opened the two protocols.
References of open issues and patches for tianocore edk2 (the upstream reference of UEFI implementation):
Issue: https://github.com/tianocore/edk2/issues/11110
Khalid Ali (2):
efi: Add wrappers to close device path and loaded image
efi: Always close protocols after usage
grub-core/commands/bli.c | 1 + grub-core/commands/efi/lsefi.c | 1 + grub-core/commands/efi/tpm.c | 5 +++-- grub-core/disk/efi/efidisk.c | 3 +++ grub-core/kern/efi/efi.c | 13 +++++++++++++ grub-core/kern/efi/init.c | 1 + grub-core/loader/efi/appleloader.c | 2 +- grub-core/loader/efi/chainloader.c | 5 +++-- grub-core/loader/efi/linux.c | 1 + grub-core/net/drivers/efi/efinet.c | 6 ++++++ grub-core/term/efi/console.c | 2 +- grub-core/term/efi/serial.c | 1 + grub-core/video/efi_gop.c | 4 +++- include/grub/efi/efi.h | 2 ++ 14 files changed, 40 insertions(+), 7 deletions(-)
Signature · 7 lines
2.52.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 1/2 efi: Add wrappers to close device path and loaded image
From: Khalid Ali <[email protected]>
Add two wrappers to close device path and loaded image protocols after they have been used.
Callers who wish to close device path or loaded image protocol should call these wrappers.
--- grub-core/kern/efi/efi.c | 13 +++++++++++++ include/grub/efi/efi.h | 2 ++ 2 files changed, 15 insertions(+)
grub-core/kern/efi/efi.c +13 -0
@@ -163,6 +163,12 @@ grub_efi_get_loaded_image (grub_efi_handle_t image_handle) GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); } +grub_efi_status_t +grub_efi_close_loaded_image (grub_efi_handle_t image_handle) +{ +return grub_efi_close_protocol (image_handle, &loaded_image_guid); +} +void grub_reboot (void ) {
@@ -371,6 +377,7 @@ grub_efi_section_addr (const char *section_name)grub_dprintf ("sections" ,"returning section info for section %d: \"%s\"\n" , i, section->name); +grub_efi_close_loaded_image (grub_efi_image_handle);return (grub_addr_t ) info; }
@@ -494,6 +501,12 @@ grub_efi_get_device_path (grub_efi_handle_t handle) GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); } +grub_efi_status_t +grub_efi_close_device_path (grub_efi_handle_t handle) +{ +return grub_efi_close_protocol (handle, &device_path_guid); +} +/* Return the device path node right before the end node. */ grub_efi_device_path_t *grub_efi_find_last_device_path (const grub_efi_device_path_t *dp)
include/grub/efi/efi.h +2 -0
@@ -72,10 +72,12 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_uint32_t *descriptor_version);void grub_efi_memory_fini (void );grub_efi_loaded_image_t *EXPORT_FUNC (grub_efi_get_loaded_image ) (grub_efi_handle_t image_handle); +grub_efi_status_t EXPORT_FUNC (grub_efi_close_loaded_image ) (grub_efi_handle_t image_handle);void EXPORT_FUNC (grub_efi_print_device_path ) (grub_efi_device_path_t *dp);char *EXPORT_FUNC (grub_efi_get_filename ) (grub_efi_device_path_t *dp);grub_efi_device_path_t *EXPORT_FUNC (grub_efi_get_device_path ) (grub_efi_handle_t handle); +grub_efi_status_t EXPORT_FUNC (grub_efi_close_device_path ) (grub_efi_handle_t handle);grub_efi_device_path_t *EXPORT_FUNC (grub_efi_find_last_device_path ) (const grub_efi_device_path_t *dp); grub_efi_device_path_t *
Signature · 7 lines
2.52.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 2/2 efi: Always close protocols after usage
In reply to Khalid AliFrom: Khalid Ali <[email protected]>
Currently grub doesn't close used protocols, this causes internal firmware resource leak.
So make sure to close all protocols after usage.
--- grub-core/commands/bli.c | 1 + grub-core/commands/efi/lsefi.c | 1 + grub-core/commands/efi/tpm.c | 5 +++-- grub-core/disk/efi/efidisk.c | 3 +++ grub-core/kern/efi/init.c | 1 + grub-core/loader/efi/appleloader.c | 2 +- grub-core/loader/efi/chainloader.c | 5 +++-- grub-core/loader/efi/linux.c | 1 + grub-core/net/drivers/efi/efinet.c | 6 ++++++ grub-core/term/efi/console.c | 2 +- grub-core/term/efi/serial.c | 1 + grub-core/video/efi_gop.c | 4 +++- 12 files changed, 25 insertions(+), 7 deletions(-)
grub-core/commands/bli.c +1 -0
@@ -123,6 +123,7 @@ set_loader_device_part_uuid (void)else grub_error (status,N_ ("unable to determine partition UUID of boot device" )); +grub_efi_close_loaded_image (grub_efi_image_handle);grub_free (part_uuid);grub_free (device_name);return status;
grub-core/commands/efi/lsefi.c +1 -0
@@ -127,6 +127,7 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),grub_printf (" %pG\n" , protocols[j]); } +grub_efi_close_device_path (handle); }grub_free (handles);
grub-core/commands/efi/tpm.c +3 -2
@@ -185,7 +185,7 @@ grub_tpm1_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, status = tpm->log_extend_event (tpm, (grub_addr_t ) buf, (grub_uint64_t ) size, algorithm, event, &eventnum, &lastevent);grub_free (event); - +grub_efi_close_protocol (tpm_handle, &tpm_guid);return grub_efi_log_event_status (status); }
@@ -221,7 +221,7 @@ grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, status = tpm->hash_log_extend_event (tpm,0 , (grub_addr_t ) buf, (grub_uint64_t ) size, event);grub_free (event); - +grub_efi_close_protocol (tpm_handle, &tpm_guid);return grub_efi_log_event_status (status); }
@@ -334,6 +334,7 @@ grub_tpm_present (void) }return grub_tpm2_present (tpm); } + } grub_uint32_t
grub-core/disk/efi/efidisk.c +3 -0
@@ -109,6 +109,7 @@ make_devices (void) d->block_io = bio; d->next = devices; devices = d; +grub_efi_close_protocol (*handle, &block_io_guid); }grub_free (handles);
@@ -908,5 +909,7 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)/* This may be guessed device - floppy, cdrom or entire disk. */ if (!get_diskname_from_path (dp, device_name))return 0 ; + +grub_efi_close_device_path (handle);return grub_strdup (device_name); }
grub-core/kern/efi/init.c +1 -0
@@ -150,6 +150,7 @@ grub_machine_get_bootlocation (char **device, char **path)if (p) *p ='\0' ; } +grub_efi_close_loaded_image (grub_efi_image_handle); }void
grub-core/loader/efi/appleloader.c +1 -1
@@ -214,7 +214,7 @@ grub_cmd_appleloader (grub_command_t cmd __attribute__ ((unused)), }grub_loader_set (grub_appleloader_boot, grub_appleloader_unload,0 ); - +grub_efi_close_loaded_image (image_handle);return 0 ; fail:
grub-core/loader/efi/chainloader.c +3 -2
@@ -57,7 +57,7 @@ grub_chainloader_unload (void *context)grub_free (loaded_image->load_options);grub_efi_unload_image (image_handle); - +grub_efi_close_loaded_image (image_handle);grub_dl_unref (my_mod);return GRUB_ERR_NONE ; }
@@ -397,7 +397,8 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),grub_file_close (file);grub_device_close (dev); - +grub_efi_close_loaded_image (image_handle); +grub_efi_close_device_path (dev_handle);/* We're finished with the source image buffer and file path now. */ b->free_pages (address, pages);grub_free (file_path);
grub-core/loader/efi/linux.c +1 -0
@@ -256,6 +256,7 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) status =grub_efi_start_image (image_handle,0 ,NULL );/* When successful, not reached */ +grub_efi_close_loaded_image (image_handle);grub_error (GRUB_ERR_BAD_OS ,"start_image() returned 0x%" PRIxGRUB_EFI_UINTN_T, status);grub_efi_free_pages ((grub_addr_t ) loaded_image->load_options,GRUB_EFI_BYTES_TO_PAGES (len));
grub-core/net/drivers/efi/efinet.c +6 -0
@@ -210,6 +210,7 @@ open_card (struct grub_net_card *dev) dev->name); } +grub_efi_close_protocol (dev->efi_handle, &net_io_guid);return GRUB_ERR_NONE ; }
@@ -338,6 +339,8 @@ grub_efinet_findcards (void) card->efi_handle = *handle;grub_net_card_register (card); +grub_efi_close_protocol (*handle, &net_io_guid); +grub_efi_close_device_path (*handle); }grub_free (handles); }
@@ -438,6 +441,9 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device, vlan_dp = (grub_efi_device_path_t *) ((grub_efi_uint8_t *) vlan_dp + vlan_dp_len); } } +grub_efi_close_protocol (hnd, &pxe_io_guid); +grub_efi_close_device_path (dp); +grub_efi_close_device_path (cdp);return ; } }
grub-core/term/efi/console.c +1 -1
@@ -369,7 +369,7 @@ grub_efi_console_input_init (struct grub_term_input *term) &text_input_ex_guid, GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); term->data = (void *)text_input; - +grub_efi_close_protocol (grub_efi_system_table->console_in_handler, &text_input_ex_guid);return 0 ; }
grub-core/term/efi/serial.c +1 -0
@@ -187,6 +187,7 @@ grub_efiserial_init (void)grub_print_error ();grub_serial_register (port); +grub_efi_close_protocol (*handle, &serial_io_guid); }grub_free (handles);
grub-core/video/efi_gop.c +3 -1
@@ -94,6 +94,7 @@ check_protocol (void) gop_handle =0 ;grub_dprintf ("video" ,"GOP: no usable mode\n" ); +grub_efi_close_protocol (gop_handle, &graphics_output_guid);grub_free (handles);return 0 ; }
@@ -325,7 +326,8 @@ grub_video_gop_get_edid (struct grub_video_edid_info *edid_info)if (copy_size >sizeof (*edid_info)) copy_size =sizeof (*edid_info);grub_memcpy (edid_info, edid->edid, copy_size); - +grub_efi_close_protocol (gop_handle, &active_edid_guid); +grub_efi_close_protocol (gop_handle, &discovered_edid_guid);return GRUB_ERR_NONE ; }
Signature · 7 lines
2.52.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel