shim loader protocol changes
v3 cover shim loader protocol changes
Julian Andres Klode (1):
efi: Provide wrappers for load_image, start_image, unload_image
Mate Kukri (4):
efi/sb: Add support for the shim loader protocol
efi/sb: Add API for retrieving shim loader image handles loader/efi/chainloader: Use shim loader image handle where available loader/efi/linux: Use shim loader image handle where available
grub-core/kern/efi/efi.c | 57 +++++++++++++++++++++++ grub-core/kern/efi/sb.c | 74 +++++++++++++++++++----------- grub-core/loader/efi/chainloader.c | 30 ++++++------ grub-core/loader/efi/linux.c | 26 ++++++----- include/grub/efi/api.h | 5 ++ include/grub/efi/efi.h | 42 +++++++++++++++++ include/grub/efi/sb.h | 6 ++- 7 files changed, 187 insertions(+), 53 deletions(-)
Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 1/5 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 | 42 ++++++++++++++++++++++ 4 files changed, 109 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 +42 -0
48 lines not on this page · raw message
Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 1/5] efi: Provide wrappers for load_image, start_image, unload_image
Quoted 6 lines · Mate Kukri
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.
Signed-off-by Mate Kukri <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 2/5 efi/sb: Add support for the shim loader protocol
In reply to Mate KukriUse loader protocol for image verification where available, otherwise fall back to the old shim lock protocol.
--- grub-core/kern/efi/sb.c | 58 ++++++++++++++++++++---------------- grub-core/loader/efi/linux.c | 6 ++-- include/grub/efi/api.h | 5 ++++ include/grub/efi/sb.h | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-)
grub-core/kern/efi/sb.c +32 -26
@@ -31,8 +31,10 @@#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_shim_lock_protocol_t *shim_lock =NULL ;/* * Determine whether we're in secure boot mode.
@@ -95,14 +97,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,15 +177,24 @@ 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 (shim_loader) + { + +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" )); -if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) -return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } +if (shim_lock) + { +if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } -return GRUB_ERR_NONE ; +return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim protocols not found" )); } struct grub_file_verifier shim_lock_verifier =
@@ -205,11 +208,17 @@ 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) +/* Secure Boot is off. Ignore shim. */ +if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) +return ; + +/* Find both shim protocols */ + shim_loader =grub_efi_locate_protocol (&shim_loader_guid,0 ); + shim_lock =grub_efi_locate_protocol (&shim_lock_guid,0 ); + +/* shim is missing, check if GRUB image is built with --disable-shim-lock. */ +if (!shim_loader && !shim_lock) {FOR_MODULES (header) {
@@ -218,21 +227,18 @@ grub_shim_lock_verifier_setup (void) } } -/* Secure Boot is off. Do not load shim_lock. */ -if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) -return ; -/* 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_is_using_legacy_shim_lock_protocol (void ) { -return shim_lock_enabled; +return !shim_loader && shim_lock; }
grub-core/loader/efi/linux.c +3 -3
@@ -461,10 +461,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),grub_dl_ref (my_mod); -if (grub_is_shim_lock_enabled () == true) +if (grub_is_using_legacy_shim_lock_protocol () == true) {#if defined(__i386__) || defined(__x86_64__) -grub_dprintf ("linux" ,"shim_lock enabled, falling back to legacy Linux kernel loader\n" ); +grub_dprintf ("linux" ,"using legacy shim_lock protocol, falling back to legacy Linux kernel loader\n" ); err =grub_cmd_linux_x86_legacy (cmd, argc, argv);
@@ -473,7 +473,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), else goto fail;#else -grub_dprintf ("linux" ,"shim_lock enabled, trying Linux kernel EFI stub loader\n" ); +grub_dprintf ("linux" ,"using legacy shim_lock protocol on non-x86, only db verifiable kernels will work\n" );#endif }
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/sb.h +1 -1
@@ -32,7 +32,7 @@ extern grub_uint8_tEXPORT_FUNC (grub_efi_get_secureboot) (void);extern bool -EXPORT_FUNC (grub_is_shim_lock_enabled ) (void ); +EXPORT_FUNC (grub_is_using_legacy_shim_lock_protocol ) (void );extern void grub_shim_lock_verifier_setup (void );
Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 2/5] efi/sb: Add support for the shim loader protocol
Quoted 54 lines · Mate Kukri
Use loader protocol for image verification where available, otherwise fall back to the old shim lock protocol.
Signed-off-by Mate Kukri <[email protected]>--- grub-core/kern/efi/sb.c | 58 ++++++++++++++++++++---------------- grub-core/loader/efi/linux.c | 6 ++-- include/grub/efi/api.h | 5 ++++ include/grub/efi/sb.h | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-)grub-core/kern/efi/sb.c +7
-12@@ -31,8 +31,10 @@#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_shim_lock_protocol_t *shim_lock =NULL ;/* * Determine whether we're in secure boot mode. @@ -95,14 +97,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,15 +177,24 @@ 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 (shim_loader) + { +
grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 192
Quoted 10 lines
grub-core/kern/efi/sb.c +7
-2+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" )); -if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) -return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } +if (shim_lock) + { +if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS )
Should it be s/sl/shim_lock/ ?
Thanks,
Michael
Quoted 120 lines
grub-core/kern/efi/sb.c +18
-12+return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } -return GRUB_ERR_NONE ; +return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim protocols not found" )); } struct grub_file_verifier shim_lock_verifier =@@ -205,11 +208,17 @@ 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) +/* Secure Boot is off. Ignore shim. */ +if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) +return ; + +/* Find both shim protocols */ + shim_loader =grub_efi_locate_protocol (&shim_loader_guid,0 ); + shim_lock =grub_efi_locate_protocol (&shim_lock_guid,0 ); + +/* shim is missing, check if GRUB image is built with --disable-shim-lock. */ +if (!shim_loader && !shim_lock) {FOR_MODULES (header) {@@ -218,21 +227,18 @@ grub_shim_lock_verifier_setup (void) } } -/* Secure Boot is off. Do not load shim_lock. */ -if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) -return ; -/* 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_is_using_legacy_shim_lock_protocol (void ) { -return shim_lock_enabled; +return !shim_loader && shim_lock; }grub-core/loader/efi/linux.c +3
-3@@ -461,10 +461,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),grub_dl_ref (my_mod); -if (grub_is_shim_lock_enabled () == true) +if (grub_is_using_legacy_shim_lock_protocol () == true) {#if defined(__i386__) || defined(__x86_64__) -grub_dprintf ("linux" ,"shim_lock enabled, falling back to legacy Linux kernel loader\n" ); +grub_dprintf ("linux" ,"using legacy shim_lock protocol, falling back to legacy Linux kernel loader\n" ); err =grub_cmd_linux_x86_legacy (cmd, argc, argv);@@ -473,7 +473,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), else goto fail;#else -grub_dprintf ("linux" ,"shim_lock enabled, trying Linux kernel EFI stub loader\n" ); +grub_dprintf ("linux" ,"using legacy shim_lock protocol on non-x86, only db verifiable kernels will work\n" );#endif }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/sb.h +1
-1@@ -32,7 +32,7 @@ extern grub_uint8_tEXPORT_FUNC (grub_efi_get_secureboot) (void);extern bool -EXPORT_FUNC (grub_is_shim_lock_enabled ) (void ); +EXPORT_FUNC (grub_is_using_legacy_shim_lock_protocol ) (void );extern void grub_shim_lock_verifier_setup (void );Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 2/5] efi/sb: Add support for the shim loader protocol
Yes, sorry...
Quoted 188 lines · Michael Chang
Quoted 54 lines · Mate Kukri
Use loader protocol for image verification where available, otherwise fall back to the old shim lock protocol.
Signed-off-by Mate Kukri <[email protected]>--- grub-core/kern/efi/sb.c | 58 ++++++++++++++++++++---------------- grub-core/loader/efi/linux.c | 6 ++-- include/grub/efi/api.h | 5 ++++ include/grub/efi/sb.h | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-)grub-core/kern/efi/sb.c +7
-12@@ -31,8 +31,10 @@#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_shim_lock_protocol_t *shim_lock =NULL ;/* * Determine whether we're in secure boot mode. @@ -95,14 +97,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,15 +177,24 @@ 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 (shim_loader) + { +grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 192
Quoted 10 lines
grub-core/kern/efi/sb.c +7
-2+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" )); -if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) -return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } +if (shim_lock) + { +if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS )Should it be s/sl/shim_lock/ ?
Thanks,
MichaelQuoted 116 lines
grub-core/kern/efi/sb.c +18
-12+return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } -return GRUB_ERR_NONE ; +return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim protocols not found" )); } struct grub_file_verifier shim_lock_verifier =@@ -205,11 +208,17 @@ 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) +/* Secure Boot is off. Ignore shim. */ +if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) +return ; + +/* Find both shim protocols */ + shim_loader =grub_efi_locate_protocol (&shim_loader_guid,0 ); + shim_lock =grub_efi_locate_protocol (&shim_lock_guid,0 ); + +/* shim is missing, check if GRUB image is built with --disable-shim-lock. */ +if (!shim_loader && !shim_lock) {FOR_MODULES (header) {@@ -218,21 +227,18 @@ grub_shim_lock_verifier_setup (void) } } -/* Secure Boot is off. Do not load shim_lock. */ -if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) -return ; -/* 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_is_using_legacy_shim_lock_protocol (void ) { -return shim_lock_enabled; +return !shim_loader && shim_lock; }grub-core/loader/efi/linux.c +3
-3@@ -461,10 +461,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),grub_dl_ref (my_mod); -if (grub_is_shim_lock_enabled () == true) +if (grub_is_using_legacy_shim_lock_protocol () == true) {#if defined(__i386__) || defined(__x86_64__) -grub_dprintf ("linux" ,"shim_lock enabled, falling back to legacy Linux kernel loader\n" ); +grub_dprintf ("linux" ,"using legacy shim_lock protocol, falling back to legacy Linux kernel loader\n" ); err =grub_cmd_linux_x86_legacy (cmd, argc, argv);@@ -473,7 +473,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), else goto fail;#else -grub_dprintf ("linux" ,"shim_lock enabled, trying Linux kernel EFI stub loader\n" ); +grub_dprintf ("linux" ,"using legacy shim_lock protocol on non-x86, only db verifiable kernels will work\n" );#endif }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/sb.h +1
-2@@ -32,7 +32,7 @@ extern grub_uint8_tEXPORT_FUNC (grub_efi_get_secureboot) (void);extern bool -EXPORT_FUNC (grub_is_shim_lock_enabled ) (void ); +EXPORT_FUNC (grub_is_using_legacy_shim_lock_protocol ) (void );extern void grub_shim_lock_verifier_setup (void ); --2.39.5
Quoted 4 lines
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 2/5] efi/sb: Add support for the shim loader protocol
In reply to Mate KukriQuoted 43 lines · Mate Kukri
Use loader protocol for image verification where available, otherwise fall back to the old shim lock protocol.
Signed-off-by Mate Kukri <[email protected]>--- grub-core/kern/efi/sb.c | 58 ++++++++++++++++++++---------------- grub-core/loader/efi/linux.c | 6 ++-- include/grub/efi/api.h | 5 ++++ include/grub/efi/sb.h | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-)grub-core/kern/efi/sb.c +3
-9@@ -31,8 +31,10 @@#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_shim_lock_protocol_t *shim_lock =NULL ;/* * Determine whether we're in secure boot mode. @@ -95,14 +97,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; }
grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 182
Quoted 9 lines
grub-core/kern/efi/sb.c +2
-3@@ -183,15 +177,24 @@ 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 (shim_loader)
if (shim_loader != NULL)
grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 186
Quoted 4 lines
grub-core/kern/efi/sb.c +4
-0+ { + +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" ));
s/bad shim signature/bad shim loader signature/
... and should not you unload the image after that?
grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 190
Quoted 5 lines
grub-core/kern/efi/sb.c +3
-2-if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) -return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); +return GRUB_ERR_NONE ; + } +if (shim_lock)
if (shim_lock != NULL)
grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 191
Quoted 2 lines
grub-core/kern/efi/sb.c +2
-0+ { +if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS )
This suggests that you did not test the code. Please do that before posting the patches next time...
grub-core/kern/efi/sb.c shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)), · line 192
Quoted 1 line
grub-core/kern/efi/sb.c +1
-0+return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" ));
s/bad shim signature/bad shim lock signature/
Quoted 17 lines
grub-core/kern/efi/sb.c +3
-5+return GRUB_ERR_NONE ; + } -return GRUB_ERR_NONE ; +return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim protocols not found" )); } struct grub_file_verifier shim_lock_verifier =@@ -205,11 +208,17 @@ 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)
grub-core/kern/efi/sb.c void · line 221
Quoted 10 lines
grub-core/kern/efi/sb.c +10
-0+/* Secure Boot is off. Ignore shim. */ +if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) +return ; + +/* Find both shim protocols */ + shim_loader =grub_efi_locate_protocol (&shim_loader_guid,0 ); + shim_lock =grub_efi_locate_protocol (&shim_lock_guid,0 ); + +/* shim is missing, check if GRUB image is built with --disable-shim-lock. */ +if (!shim_loader && !shim_lock)
if (shim_loader == NULL && shim_lock == NULL)
Quoted 18 lines
grub-core/kern/efi/sb.c +2
-5{FOR_MODULES (header) {@@ -218,21 +227,18 @@ grub_shim_lock_verifier_setup (void) } } -/* Secure Boot is off. Do not load shim_lock. */ -if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED ) -return ; -/* 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-core/kern/efi/sb.c grub_shim_lock_verifier_setup (void) · line 243
Quoted 10 lines
grub-core/kern/efi/sb.c +2
-2grub_env_set ("shim_lock" ,"y" );grub_env_export ("shim_lock" ); }bool -grub_is_shim_lock_enabled (void ) +grub_is_using_legacy_shim_lock_protocol (void ) { -return shim_lock_enabled; +return !shim_loader && shim_lock;
return (shim_loader == NULL && shim_lock != NULL) ? true : false;
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 3/5 efi/sb: Add API for retrieving shim loader image handles
In reply to Mate KukriNot reusing these handles will result in image measurements showing up twice in the event log.
--- grub-core/kern/efi/sb.c | 16 ++++++++++++++++ include/grub/efi/sb.h | 4 ++++ 2 files changed, 20 insertions(+)
grub-core/kern/efi/sb.c +16 -0
@@ -36,6 +36,8 @@ static grub_guid_t shim_loader_guid = GRUB_EFI_SHIM_IMAGE_LOADER_GUID;static grub_efi_loader_t *shim_loader =NULL ;static grub_efi_shim_lock_protocol_t *shim_lock =NULL ; +static grub_efi_handle_t last_verified_image_handle; +/* * Determine whether we're in secure boot mode. *
@@ -181,10 +183,16 @@ shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, gruif (shim_loader) { +if (last_verified_image_handle) + { + shim_loader->unload_image (last_verified_image_handle); + last_verified_image_handle =NULL ; + }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 ; }if (shim_lock)
@@ -242,3 +250,11 @@ grub_is_using_legacy_shim_lock_protocol (void) {return !shim_loader && shim_lock; } + +grub_efi_handle_t +grub_efi_get_last_verified_image_handle (void ) +{ +grub_efi_handle_t tmp = last_verified_image_handle; + last_verified_image_handle =NULL ; +return tmp; +}
include/grub/efi/sb.h +4 -0
@@ -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
@@ -34,6 +35,9 @@ EXPORT_FUNC (grub_efi_get_secureboot) (void);extern bool EXPORT_FUNC (grub_is_using_legacy_shim_lock_protocol ) (void ); +extern grub_efi_handle_t +EXPORT_FUNC (grub_efi_get_last_verified_image_handle ) (void ); +extern void grub_shim_lock_verifier_setup (void );#else
Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 3/5] efi/sb: Add API for retrieving shim loader image handles
Quoted 14 lines · Mate Kukri
Not reusing these handles will result in image measurements showing up twice in the event log.
Signed-off-by Mate Kukri <[email protected]>--- grub-core/kern/efi/sb.c | 16 ++++++++++++++++ include/grub/efi/sb.h | 4 ++++ 2 files changed, 20 insertions(+)grub-core/kern/efi/sb.c +0
-0
grub-core/kern/efi/sb.c static grub_guid_t shim_loader_guid = GRUB_EFI_SHIM_IMAGE_LOADER_GUID; · line 39
Quoted 4 lines
grub-core/kern/efi/sb.c +1
-0@@ -36,6 +36,8 @@ static grub_guid_t shim_loader_guid = GRUB_EFI_SHIM_IMAGE_LOADER_GUID;static grub_efi_loader_t *shim_loader =NULL ;static grub_efi_shim_lock_protocol_t *shim_lock =NULL ; +static grub_efi_handle_t last_verified_image_handle;
static grub_efi_handle_t last_verified_image_handle = NULL;
grub-core/kern/efi/sb.c shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, gru · line 186
Quoted 8 lines
grub-core/kern/efi/sb.c +1
-0/* * Determine whether we're in secure boot mode. * @@ -181,10 +183,16 @@ shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, gruif (shim_loader) { +if (last_verified_image_handle)
if (last_verified_image_handle != NULL)
Please fix similar issues everywhere...
grub-core/kern/efi/sb.c shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, gru · line 189
Quoted 3 lines
grub-core/kern/efi/sb.c +3
-0+ { + shim_loader->unload_image (last_verified_image_handle); + last_verified_image_handle =NULL ;
I am not entirely sure why you unload the image here. If it is really needed I think it requires an explanation.
Quoted 10 lines
grub-core/kern/efi/sb.c +2
-0+ }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 ; }if (shim_lock)
grub-core/kern/efi/sb.c grub_is_using_legacy_shim_lock_protocol (void) · line 258
Quoted 9 lines
grub-core/kern/efi/sb.c +6
-0@@ -242,3 +250,11 @@ grub_is_using_legacy_shim_lock_protocol (void) {return !shim_loader && shim_lock; } + +grub_efi_handle_t +grub_efi_get_last_verified_image_handle (void ) +{ +grub_efi_handle_t tmp = last_verified_image_handle; + last_verified_image_handle =NULL ;
Why do you reset last_verified_image_handle here? The "get" function should not do that. And if it is needed then it has to be explained why.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 4/5 loader/efi/chainloader: Use shim loader image handle where available
In reply to Mate Kukri--- grub-core/loader/efi/chainloader.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
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
Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 4/5] loader/efi/chainloader: Use shim loader image handle where available
On Tue, Apr 01, 2025 at 11:26:44AM +0100, Mate Kukri wrote:
I think you should shortly explain here why this patch is needed.
Quoted 1 line
Signed-off-by Mate Kukri <[email protected]>
Otherwise patch LGTM...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 5/5 loader/efi/linux: Use shim loader image handle where available
In reply to Mate Kukri--- grub-core/loader/efi/linux.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
grub-core/loader/efi/linux.c +9 -5
@@ -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);
Signature · 7 lines
2.39.5 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 5/5] loader/efi/linux: Use shim loader image handle where available
On Tue, Apr 01, 2025 at 11:26:45AM +0100, Mate Kukri wrote:
I think you should shortly explain here why this patch is needed.
Quoted 1 line
Signed-off-by Mate Kukri <[email protected]>
Otherwise patch LGTM...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
NOTE that leaving in the shim lock fallback has a downside, which is that the following contrived scenario will let all checks pass, and create unavoidable crashes:
1. you are running a system with a real NX mode
2. you have signed an NX_COMPAT + NX_REQUIRE (no one should _ever_ sign NX_COMPAT without NX_REQUIRE) shim 15.8 (this will switch NX mode on in the firmware)
3. you have signed an NX_COMPAT GRUB (shim will let the GRUB pass and the GRUB will run)
4. you have signed a NX_COMPAT kernel (shim_lock->verify() will allow the kernel, but GRUB will use the legacy loader due to the presence of only shim 15.8)
The reasons I am calling said crashes unavoidable is due to:
- for 1. no api to query the firmware NX mode being on or not
- for 2., no api to query shim NX_COMPAT or NX_REQUIRE:
NX_COMPAT is only available via gross hacks that parse the shim PE
header in memory,
and the MokPolicy variable (and NX_REQUIRE) is only exposed via EFI
vars or config tables if
using non-default value.
- so grub in 3. has no reliable way to tell if it can ever allow the legacy loader fallback, hence why I initially removed it.
Quoted 19 lines · Mate Kukri
Julian Andres Klode (1):
efi: Provide wrappers forload_image,start_image,unload_imageMate Kukri (4):
efi/sb: Add support for the shim loader protocol
efi/sb: Add API for retrieving shim loader image handles loader/efi/chainloader: Use shim loader image handle where available loader/efi/linux: Use shim loader image handle where availablegrub-core/kern/efi/efi.c | 57 +++++++++++++++++++++++ grub-core/kern/efi/sb.c | 74 +++++++++++++++++++----------- grub-core/loader/efi/chainloader.c | 30 ++++++------ grub-core/loader/efi/linux.c | 26 ++++++----- include/grub/efi/api.h | 5 ++ include/grub/efi/efi.h | 42 +++++++++++++++++ include/grub/efi/sb.h | 6 ++- 7 files changed, 187 insertions(+), 53 deletions(-)
Quoted 3 lines
--
2.39.5
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel