Draft: Support for shim loader protocol
cover Draft: Support for shim loader protocol
Julian Andres Klode (1):
efi: Provide wrappers for load_image, start_image, unload_image
Mate Kukri (1):
efi: Use shim's loader protocol for EFI image verification and loading
grub-core/kern/efi/efi.c | 57 ++++++++++++++++++++++++++++++ grub-core/kern/efi/sb.c | 45 ++++++++++++----------- grub-core/loader/efi/chainloader.c | 30 ++++++++-------- grub-core/loader/efi/linux.c | 36 ++++++------------- include/grub/efi/api.h | 5 +++ include/grub/efi/efi.h | 42 ++++++++++++++++++++++ include/grub/efi/sb.h | 5 +-- 7 files changed, 158 insertions(+), 62 deletions(-)
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 1/2 efi: Provide wrappers for load_image, start_image, unload_image
From: Julian Andres Klode <[email protected]>
These can be used to register a different implementation later, for example, when shim provides a protocol with those functions.
--- grub-core/kern/efi/efi.c | 57 ++++++++++++++++++++++++++++++ grub-core/loader/efi/chainloader.c | 13 +++---- grub-core/loader/efi/linux.c | 12 +++---- include/grub/efi/efi.h | 37 +++++++++++++++++++ 4 files changed, 104 insertions(+), 15 deletions(-)
grub-core/kern/efi/efi.c +57 -0
@@ -1049,3 +1049,60 @@ grub_efi_find_configuration_table (const grub_guid_t *target_guid)return 0 ; } + +static const grub_efi_loader_t *override_loader =NULL ; + +grub_err_t +grub_efi_register_loader (const grub_efi_loader_t *loader) +{ +if (override_loader !=NULL ) +return grub_error (GRUB_ERR_BUG ,"trying to register different loader" ); + override_loader = loader; +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_efi_unregister_loader (const grub_efi_loader_t *loader) +{ +if (loader != override_loader) +return grub_error (GRUB_ERR_BUG ,"trying to unregister different loader" ); + + override_loader =NULL ; +return GRUB_ERR_NONE ; +} + +grub_efi_status_t +grub_efi_load_image (grub_efi_boolean_t boot_policy, +grub_efi_handle_t parent_image_handle, +grub_efi_device_path_t *file_path,void *source_buffer, +grub_efi_uintn_t source_size, +grub_efi_handle_t *image_handle) +{ +if (override_loader !=NULL ) +return override_loader->load_image (boot_policy, parent_image_handle, + file_path, source_buffer, source_size, + image_handle); +return grub_efi_system_table->boot_services->load_image ( + boot_policy, parent_image_handle, file_path, source_buffer, source_size, + image_handle); +} + +grub_efi_status_t +grub_efi_start_image (grub_efi_handle_t image_handle, +grub_efi_uintn_t *exit_data_size, +grub_efi_char16_t **exit_data) +{ +if (override_loader !=NULL ) +return override_loader->start_image (image_handle, exit_data_size, + exit_data); +return grub_efi_system_table->boot_services->start_image ( + image_handle, exit_data_size, exit_data); +} + +grub_efi_status_t +grub_efi_unload_image (grub_efi_handle_t image_handle) +{ +if (override_loader !=NULL ) +return override_loader->unload_image (image_handle); +return grub_efi_system_table->boot_services->unload_image (image_handle); +}
grub-core/loader/efi/chainloader.c +5 -8
@@ -50,14 +50,12 @@ grub_chainloader_unload (void *context) {grub_efi_handle_t image_handle = (grub_efi_handle_t ) context;grub_efi_loaded_image_t *loaded_image; -grub_efi_boot_services_t *b; loaded_image =grub_efi_get_loaded_image (image_handle);if (loaded_image !=NULL )grub_free (loaded_image->load_options); - b = grub_efi_system_table->boot_services; - b->unload_image (image_handle); +grub_efi_unload_image (image_handle);grub_dl_unref (my_mod);return GRUB_ERR_NONE ;
@@ -73,7 +71,7 @@ grub_chainloader_boot (void *context)grub_efi_char16_t *exit_data =NULL ; b = grub_efi_system_table->boot_services; - status = b->start_image (image_handle, &exit_data_size, &exit_data); + status =grub_efi_start_image (image_handle, &exit_data_size, &exit_data);if (status !=GRUB_EFI_SUCCESS ) {if (exit_data)
@@ -339,9 +337,8 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), }#endif - status = b->load_image (0 , grub_efi_image_handle, file_path, - boot_image, size, - &image_handle); + status =grub_efi_load_image (0 , grub_efi_image_handle, file_path, + boot_image, size, &image_handle);if (status !=GRUB_EFI_SUCCESS ) {if (status ==GRUB_EFI_OUT_OF_RESOURCES )
@@ -418,7 +415,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), b->free_pages (address, pages);if (image_handle !=NULL ) - b->unload_image (image_handle); +grub_efi_unload_image (image_handle);grub_dl_unref (my_mod);
grub-core/loader/efi/linux.c +5 -7
@@ -187,7 +187,6 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) {grub_efi_memory_mapped_device_path_t *mempath;grub_efi_handle_t image_handle; -grub_efi_boot_services_t *b;grub_efi_status_t status;grub_efi_loaded_image_t *loaded_image;int len;
@@ -207,10 +206,9 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) mempath[1 ].header.subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; mempath[1 ].header.length =sizeof (grub_efi_device_path_t); - b = grub_efi_system_table->boot_services; - status = b->load_image (0 , grub_efi_image_handle, - (grub_efi_device_path_t *) mempath, - (void *) addr, size, &image_handle); + status =grub_efi_load_image (0 , grub_efi_image_handle, + (grub_efi_device_path_t *)mempath, + (void *)addr, size, &image_handle);if (status !=GRUB_EFI_SUCCESS )return grub_error (GRUB_ERR_BAD_OS ,"cannot load image" );
@@ -235,7 +233,7 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) (grub_uint8_t *) args, len, NULL);grub_dprintf ("linux" ,"starting image %p\n" , image_handle); - status = b->start_image (image_handle,0 ,NULL ); + status =grub_efi_start_image (image_handle,0 ,NULL );/* When successful, not reached */ grub_error (GRUB_ERR_BAD_OS ,"start_image() returned 0x%" PRIxGRUB_EFI_UINTN_T, status);
@@ -243,7 +241,7 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) GRUB_EFI_BYTES_TO_PAGES (len)); loaded_image->load_options =NULL ; unload: - b->unload_image (image_handle); +grub_efi_unload_image (image_handle);return grub_errno; }
include/grub/efi/efi.h +37 -0
@@ -132,6 +132,43 @@ grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file,grub_err_t grub_arch_efi_linux_boot_image (grub_addr_t addr,grub_size_t size,char *args); +grub_efi_status_t +EXPORT_FUNC (grub_efi_load_image ) (grub_efi_boolean_t boot_policy, +grub_efi_handle_t parent_image_handle, +grub_efi_device_path_t *file_path, +void *source_buffer,grub_efi_uintn_t source_size, +grub_efi_handle_t *image_handle); + +grub_efi_status_t +EXPORT_FUNC (grub_efi_start_image ) (grub_efi_handle_t image_handle, +grub_efi_uintn_t *exit_data_size, +grub_efi_char16_t **exit_data); + +grub_efi_status_t +EXPORT_FUNC (grub_efi_unload_image ) (grub_efi_handle_t image_handle); + +typedef struct grub_efi_loader +{ + grub_efi_status_t (__grub_efi_api *load_image) (grub_efi_boolean_t boot_policy, + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, + void *source_buffer, + grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle); + + grub_efi_status_t (__grub_efi_api *start_image) (grub_efi_handle_t image_handle, + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data); + + grub_efi_status_t (__grub_efi_api *unload_image) (grub_efi_handle_t image_handle); +}grub_efi_loader_t ; + +grub_err_t +EXPORT_FUNC (grub_efi_register_loader ) (const grub_efi_loader_t *loader); + +grub_err_t +EXPORT_FUNC (grub_efi_unregister_loader ) (const grub_efi_loader_t *loader); +grub_addr_t grub_efi_section_addr (const char *section);void grub_efi_mm_init (void );
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 2/2 efi: Use shim's loader protocol for EFI image verification and loading
In reply to Mate Kukri- Use shim loader protocol to verify images in the shim_lock verifier.
- Add API to allow downstream consumers to re-use image handles produced by the verifier. This is necessary to avoid having images measured twice to the TPM.
- Register shim loader protocol as an image loader.
--- grub-core/kern/efi/sb.c | 45 ++++++++++++++++-------------- grub-core/loader/efi/chainloader.c | 21 ++++++++------ grub-core/loader/efi/linux.c | 30 ++++++-------------- include/grub/efi/api.h | 5 ++++ include/grub/efi/efi.h | 19 ++++++++----- include/grub/efi/sb.h | 5 ++-- 6 files changed, 66 insertions(+), 59 deletions(-)
grub-core/kern/efi/sb.c +24 -21
@@ -31,8 +31,11 @@#include <grub/verify.h> static grub_guid_t shim_lock_guid =GRUB_EFI_SHIM_LOCK_GUID ; +static grub_guid_t shim_loader_guid =GRUB_EFI_SHIM_IMAGE_LOADER_GUID ; -static bool shim_lock_enabled = false; +static grub_efi_loader_t *shim_loader =NULL ; + +static grub_efi_handle_t last_verified_image_handle;/* * Determine whether we're in secure boot mode.
@@ -95,14 +98,6 @@ grub_efi_get_secureboot (void)if (!(attr &GRUB_EFI_VARIABLE_RUNTIME_ACCESS ) && *moksbstate ==1 ) { secureboot =GRUB_EFI_SECUREBOOT_MODE_DISABLED ; -/* -* TODO: Replace this all with shim's LoadImage protocol, delegating policy to it. -* -* We need to set shim_lock_enabled here because we disabled secure boot -* validation *inside* shim but not in the firmware, so we set this variable -* here to trigger that code path, whereas the actual verifier is not enabled. -*/ - shim_lock_enabled = true; goto out; }
@@ -183,14 +178,18 @@ shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),static grub_err_t shim_lock_verifier_write (void *context __attribute__ ((unused)),void *buf,grub_size_t size) { -grub_efi_shim_lock_protocol_t *sl =grub_efi_locate_protocol (&shim_lock_guid,0 ); +grub_efi_handle_t image_handle; -if (!sl) -return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim_lock protocol not found" )); +if (last_verified_image_handle) + { + shim_loader->unload_image (last_verified_image_handle); + last_verified_image_handle =NULL ; + } -if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) +if (shim_loader->load_image (false, grub_efi_image_handle,NULL , buf, size, &image_handle) !=GRUB_EFI_SUCCESS )return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); + last_verified_image_handle = image_handle;return GRUB_ERR_NONE ; }
@@ -205,11 +204,11 @@ voidgrub_shim_lock_verifier_setup (void) {struct grub_module_header *header; -grub_efi_shim_lock_protocol_t *sl = -grub_efi_locate_protocol (&shim_lock_guid,0 ); -/* shim_lock is missing, check if GRUB image is built with --disable-shim-lock. */ -if (!sl) + shim_loader =grub_efi_locate_protocol (&shim_loader_guid,0 ); + +/* shim is missing, check if GRUB image is built with --disable-shim-lock. */ +if (!shim_loader) {FOR_MODULES (header) {
@@ -225,14 +224,18 @@ grub_shim_lock_verifier_setup (void)/* Enforce shim_lock_verifier. */ grub_verifier_register (&shim_lock_verifier); - shim_lock_enabled = true; +/* Register shim loader if supported. */ +grub_efi_register_loader (shim_loader);grub_env_set ("shim_lock" ,"y" );grub_env_export ("shim_lock" ); } -bool -grub_is_shim_lock_enabled (void ) + +grub_efi_handle_t +grub_efi_get_last_verified_image_handle (void ) { -return shim_lock_enabled; +grub_efi_handle_t tmp = last_verified_image_handle; + last_verified_image_handle =NULL ; +return tmp; }
grub-core/loader/efi/chainloader.c +13 -8
@@ -33,6 +33,7 @@#include <grub/efi/efi.h> #include <grub/efi/disk.h> #include <grub/efi/memory.h> +#include <grub/efi/sb.h> #include <grub/command.h> #include <grub/i18n.h> #include <grub/net.h>
@@ -337,16 +338,20 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), }#endif - status =grub_efi_load_image (0 , grub_efi_image_handle, file_path, - boot_image, size, &image_handle); -if (status !=GRUB_EFI_SUCCESS ) + image_handle =grub_efi_get_last_verified_image_handle (); +if (image_handle ==NULL ) { -if (status ==GRUB_EFI_OUT_OF_RESOURCES ) -grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of resources" ); -else -grub_error (GRUB_ERR_BAD_OS ,"cannot load image" ); + status =grub_efi_load_image (0 , grub_efi_image_handle, file_path, + boot_image, size, &image_handle); +if (status !=GRUB_EFI_SUCCESS ) + { +if (status ==GRUB_EFI_OUT_OF_RESOURCES ) +grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of resources" ); +else +grub_error (GRUB_ERR_BAD_OS ,"cannot load image" ); - goto fail; + goto fail; + } }/* LoadImage does not set a device handler when the image is
grub-core/loader/efi/linux.c +9 -21
@@ -206,11 +206,15 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) mempath[1 ].header.subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; mempath[1 ].header.length =sizeof (grub_efi_device_path_t); - status =grub_efi_load_image (0 , grub_efi_image_handle, - (grub_efi_device_path_t *)mempath, - (void *)addr, size, &image_handle); -if (status !=GRUB_EFI_SUCCESS ) -return grub_error (GRUB_ERR_BAD_OS ,"cannot load image" ); + image_handle =grub_efi_get_last_verified_image_handle (); +if (image_handle ==NULL ) + { + status =grub_efi_load_image (0 , grub_efi_image_handle, + (grub_efi_device_path_t *)mempath, + (void *)addr, size, &image_handle); +if (status !=GRUB_EFI_SUCCESS ) +return grub_error (GRUB_ERR_BAD_OS ,"cannot load image" ); + }grub_dprintf ("linux" ,"linux command line: '%s'\n" , args);
@@ -461,22 +465,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),grub_dl_ref (my_mod); -if (grub_is_shim_lock_enabled () == true) - { -#if defined(__i386__) || defined(__x86_64__) -grub_dprintf ("linux" ,"shim_lock enabled, falling back to legacy Linux kernel loader\n" ); - - err =grub_cmd_linux_x86_legacy (cmd, argc, argv); - -if (err ==GRUB_ERR_NONE ) -return GRUB_ERR_NONE ; -else - goto fail; -#else -grub_dprintf ("linux" ,"shim_lock enabled, trying Linux kernel EFI stub loader\n" ); -#endif - } -if (argc ==0 ) {grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("filename expected" ));
include/grub/efi/api.h +5 -0
@@ -364,6 +364,11 @@ { 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } \ } +#define GRUB_EFI_SHIM_IMAGE_LOADER_GUID \ + { 0x1f492041, 0xfadb, 0x4e59, \ + {0x9e, 0x57, 0x7c, 0xaf, 0xe7, 0x3a, 0x55, 0xab } \ + } +#define GRUB_EFI_RNG_PROTOCOL_GUID \ { 0x3152bca5, 0xeade, 0x433d, \ { 0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 } \
include/grub/efi/efi.h +12 -7
@@ -150,15 +150,20 @@ EXPORT_FUNC (grub_efi_unload_image) (grub_efi_handle_t image_handle);typedef struct grub_efi_loader { grub_efi_status_t (__grub_efi_api *load_image) (grub_efi_boolean_t boot_policy, - grub_efi_handle_t parent_image_handle, - grub_efi_device_path_t *file_path, - void *source_buffer, - grub_efi_uintn_t source_size, - grub_efi_handle_t *image_handle); + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, + void *source_buffer, + grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle); grub_efi_status_t (__grub_efi_api *start_image) (grub_efi_handle_t image_handle, - grub_efi_uintn_t *exit_data_size, - grub_efi_char16_t **exit_data); + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data); + + grub_efi_status_t (__grub_efi_api *exit) (grub_efi_handle_t image_handle, + grub_efi_status_t exit_status, + grub_efi_uintn_t exit_data_size, + grub_efi_char16_t *exit_data); grub_efi_status_t (__grub_efi_api *unload_image) (grub_efi_handle_t image_handle); }grub_efi_loader_t ;
include/grub/efi/sb.h +3 -2
@@ -21,6 +21,7 @@#include <grub/types.h> #include <grub/dl.h> +#include <grub/efi/api.h> #define GRUB_EFI_SECUREBOOT_MODE_UNSET 0#define GRUB_EFI_SECUREBOOT_MODE_UNKNOWN 1
@@ -31,8 +32,8 @@extern grub_uint8_t EXPORT_FUNC (grub_efi_get_secureboot ) (void ); -extern bool -EXPORT_FUNC (grub_is_shim_lock_enabled ) (void ); +extern grub_efi_handle_t +EXPORT_FUNC (grub_efi_get_last_verified_image_handle ) (void );extern void grub_shim_lock_verifier_setup (void );
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel