efi: Shim LoadImage improvements
PATCH 1/3 efi: Fix line length in init_secure_boot_mode
In reply to Gerald Elder-VassCommit cb41b4ce14a9 introduced init_secure_boot_mode but one line was not wrapped appropriately.
---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- xen/common/efi/boot.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/efi/boot.c +2 -1
@@ -923,7 +923,8 @@ static void __init init_secure_boot_mode(void)if ( status ==EFI_NOT_FOUND || (status ==EFI_SUCCESS && - attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS ) && size ==1 && data ==0 ) )/* Platform does not support Secure Boot or it's disabled. */ efi_secure_boot = false;
Signature · 1 line
2.47.3
PATCH 2/3 efi: Protect against unnecessary image unloading
In reply to Gerald Elder-VassCommit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.
---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- xen/common/efi/boot.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
xen/common/efi/boot.c +6 -5
@@ -1062,7 +1062,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)static EFI_GUID __initdata shim_image_guid =SHIM_IMAGE_LOADER_GUID ;static EFI_GUID __initdata shim_lock_guid =SHIM_LOCK_PROTOCOL_GUID ;SHIM_IMAGE_LOADER *shim_loader; -EFI_HANDLE loaded_kernel; +EFI_HANDLE loaded_kernel =NULL ;EFI_SHIM_LOCK_PROTOCOL *shim_lock;EFI_STATUS status;bool verified = false;
@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) verified = true;/* -* Always unload the image. We only needed LoadImage() to perform -* verification anyway, and in the case of a failure there may still -* be cleanup needing to be performed. +* If the kernel was loaded, unload it. We only needed LoadImage() to +* perform verification anyway, and in the case of a failure there may +* still be cleanup needing to be performed. */ - shim_loader->UnloadImage (loaded_kernel); +if ( loaded_kernel ) + shim_loader->UnloadImage (loaded_kernel); }/* Otherwise, fall back to SHIM_LOCK. */
Signature · 1 line
2.47.3
PATCH 3/3 efi: Limit Shim's Verify success to EFI_SUCCESS
In reply to Gerald Elder-VassCommit 59a1d6d3ea1e replaced the Verify status check with !EFI_ERROR(...), this changed the behaviour to consider any warnings (EFI_WARN_) to be considered a successful verification.
This commit reverts that behaviour change.
---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- xen/common/efi/boot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/efi/boot.c +1 -1
@@ -1090,7 +1090,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)if ( !verified && !EFI_ERROR (efi_bs->LocateProtocol (&shim_lock_guid,NULL , (void **)&shim_lock)) && - !EFI_ERROR (shim_lock->Verify (kernel.ptr, kernel.size)) ) + shim_lock->Verify (kernel.ptr, kernel.size) ==EFI_SUCCESS ) verified = true;if ( !verified )
Signature · 1 line
2.47.3
cover efi: Shim LoadImage improvements
A previous series for supporting Shim's LoadImage protocol had some outstanding comments after it had been accepted, and internal review revealed an improvement to be safe when unloading the image.
This patch series includes those changes.
https://gitlab.com/xen-project/people/geraldev/xen/-/pipelines/2032454096
Gerald Elder-Vass (3):
efi: Fix line length in init_secure_boot_mode
efi: Protect against unnecessary image unloading
efi: Limit Shim's Verify success to EFI_SUCCESS
xen/common/efi/boot.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.3
Re: [PATCH 2/3] efi: Protect against unnecessary image unloading
In reply to Gerald Elder-VassQuoted 5 lines · Gerald Elder-Vass
xen/common/efi/boot.c +0
-1@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) verified = true;/* -* Always unload the image. We only needed LoadImage() to perform
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1087
Quoted 10 lines
xen/common/efi/boot.c +5
-3-* verification anyway, and in the case of a failure there may still -* be cleanup needing to be performed. +* If the kernel was loaded, unload it. We only needed LoadImage() to +* perform verification anyway, and in the case of a failure there may +* still be cleanup needing to be performed. */ - shim_loader->UnloadImage (loaded_kernel); +if ( loaded_kernel ) + shim_loader->UnloadImage (loaded_kernel); }
To me this looks as odd as the earlier unconditional unloading. How would a halfway sane implementation of LoadImage() return an error, but require subsequent cleanup (and set what the last function argument points at to non-NULL)? Unless explicitly specified otherwise, my expectation would be that upon failure loaded_kernel could have any arbitrary value, possibly entirely unsuitable to pass to UnloadImage().
Jan
Re: [PATCH 3/3] efi: Limit Shim's Verify success to EFI_SUCCESS
In reply to Gerald Elder-VassQuoted 5 lines · Gerald Elder-Vass
Commit
59a1d6d3ea1ereplaced the Verify status check with !EFI_ERROR(...), this changed the behaviour to consider any warnings (EFI_WARN_) to be considered a successful verification.This commit reverts that behaviour change.
Quoted 1 line
Signed-off-by Gerald Elder-Vass <[email protected]>
Re: [PATCH 2/3] efi: Protect against unnecessary image unloading
In reply to Jan BeulichQuoted 1 line
Review static void __init efi_verify_kernel(EFI_HANDLE
Quoted 1 line
(quoted diff) +0
-0@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE
ImageHandle)
Quoted 4 lines
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
Quoted 4 lines
xen/common/efi/boot.c +0
-1verified = true;/* -* Always unload the image. We only needed LoadImage() to
perform
Quoted 1 line
Review static void __init efi_verify_kernel(EFI_HANDLE · line 1082
Quoted 1 line
(quoted diff) +0
-1- * verification anyway, and in the case of a failure there may
still
Quoted 2 lines
Review static void __init efi_verify_kernel(EFI_HANDLE · line 1081
Quoted 2 lines
(quoted diff) +1
-1- * be cleanup needing to be performed. + * If the kernel was loaded, unload it. We only needed
LoadImage() to
Quoted 1 line
Review static void __init efi_verify_kernel(EFI_HANDLE · line 1082
Quoted 1 line
(quoted diff) +1
-0+ * perform verification anyway, and in the case of a failure
there may
Quoted 7 lines
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1087
Quoted 6 lines
xen/common/efi/boot.c +3
-1+* still be cleanup needing to be performed. */ - shim_loader->UnloadImage (loaded_kernel); +if ( loaded_kernel ) + shim_loader->UnloadImage (loaded_kernel); }
Quoted 6 lines
To me this looks as odd as the earlier unconditional unloading. How would a halfway sane implementation of
LoadImage()return an error, but require subsequent cleanup (and set what the last function argument points at to non-NULL)? Unless explicitly specified otherwise, my expectation would be that upon failureloaded_kernelcould have any arbitrary value, possibly entirely unsuitable to pass toUnloadImage().
This is because LoadImage performs the verification step after the loading. They are independent operations but the output conflates the two, so we can receive a successfully loaded image and an EFI_SECURITY_VIOLATION status code, in this particular case the image will need to be unloaded. The generalised check for the EFI_IMAGE_HANDLE before unloading (as opposed to checking this specific status code) protects against any future changes in the protocol.
I've linked the specification which states that the ImageHandle is created in this particular case.
https://uefi.org/specs/UEFI/2.10/07_Services_Boot_Services.html#efi-boot-services-loadimage
*Gerald Elder-Vass*
Senior Software Engineer
XenServer
Cambridge, UK
Quoted 31 lines · Jan Beulich
Review static void __init efi_verify_kernel(EFI_HANDLE
Quoted 1 line · Gerald Elder-Vass
(quoted diff) +0
-0@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLEImageHandle)
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
Quoted 4 lines
xen/common/efi/boot.c +0
-1verified = true;/* -* Always unload the image. We only needed LoadImage() to perform
Review static void __init efi_verify_kernel(EFI_HANDLE · line 1082
Quoted 1 line
(quoted diff) +0
-1- * verification anyway, and in the case of a failure there maystill
Review static void __init efi_verify_kernel(EFI_HANDLE · line 1081
Quoted 2 lines
(quoted diff) +1
-1- * be cleanup needing to be performed. + * If the kernel was loaded, unload it. We only needed
LoadImage()toReview static void __init efi_verify_kernel(EFI_HANDLE · line 1082
Quoted 1 line
(quoted diff) +1
-0+ * perform verification anyway, and in the case of a failurethere may
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1087
Quoted 6 lines
xen/common/efi/boot.c +3
-1+* still be cleanup needing to be performed. */ - shim_loader->UnloadImage (loaded_kernel); +if ( loaded_kernel ) + shim_loader->UnloadImage (loaded_kernel); }To me this looks as odd as the earlier unconditional unloading. How would a halfway sane implementation of
LoadImage()return an error, but require subsequent cleanup (and set what the last function argument points at to non-NULL)? Unless explicitly specified otherwise, my expectation would be that upon failureloaded_kernelcould have any arbitrary value, possibly entirely unsuitable to pass toUnloadImage().Jan
Re: [PATCH 1/3] efi: Fix line length in init_secure_boot_mode
In reply to Gerald Elder-VassQuoted 23 lines · Gerald Elder-Vass
Commit
cb41b4ce14a9introducedinit_secure_boot_modebut one line was not wrapped appropriately.Signed-off-by Gerald Elder-Vass <[email protected]>---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>--- xen/common/efi/boot.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)xen/common/efi/boot.c +0
-0
xen/common/efi/boot.c static void __init init_secure_boot_mode(void) · line 930
Quoted 9 lines
xen/common/efi/boot.c +2
-1@@ -923,7 +923,8 @@ static void __init init_secure_boot_mode(void)if ( status ==EFI_NOT_FOUND || (status ==EFI_SUCCESS && - attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS ) && size ==1 && data ==0 ) )/* Platform does not support Secure Boot or it's disabled. */ efi_secure_boot = false;
You're not wrong, but it feels a bit excessive having a patch just for this.
Oh, well.
Cheers,
Alejandro
Re: [PATCH 2/3] efi: Protect against unnecessary image unloading
In reply to Gerald Elder-VassQuoted 5 lines · Gerald Elder-Vass
Commit
59a1d6d3ea1eintroduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.
Any what memory in use? The function loads an image, it's not a consumer.
It can't free anything because it doesn't know where it came from. It might've been embedded in your own binary, and it can't compromise its integrity like that.
Quoted 21 lines
Signed-off-by Gerald Elder-Vass <[email protected]>---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>--- xen/common/efi/boot.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)xen/common/efi/boot.c +0
-0
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1065
Quoted 5 lines
xen/common/efi/boot.c +1
-1@@ -1062,7 +1062,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)static EFI_GUID __initdata shim_image_guid =SHIM_IMAGE_LOADER_GUID ;static EFI_GUID __initdata shim_lock_guid =SHIM_LOCK_PROTOCOL_GUID ;SHIM_IMAGE_LOADER *shim_loader; -EFI_HANDLE loaded_kernel; +EFI_HANDLE loaded_kernel =NULL ;
This isn't required if unloading checks for the success case or the only error case that has a successful load. See below.
Furthermore, you don't really know if the callee clobbered it.
Quoted 6 lines
xen/common/efi/boot.c +0
-0EFI_SHIM_LOCK_PROTOCOL *shim_lock;EFI_STATUS status;bool verified = false;@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) verified = true;
xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1085
Quoted 10 lines
xen/common/efi/boot.c +4
-4/* -* Always unload the image. We only needed LoadImage() to perform -* verification anyway, and in the case of a failure there may still -* be cleanup needing to be performed. +* If the kernel was loaded, unload it. We only needed LoadImage() to +* perform verification anyway, and in the case of a failure there may +* still be cleanup needing to be performed. */ - shim_loader->UnloadImage (loaded_kernel); +if ( loaded_kernel )
Not sure this is what you want. The image needs unloading only when there's no error OR the error is EFI_SECURITY_VIOLATION. See section 7.4.1:
https://uefi.org/specs/UEFI/2.11/07_Services_Boot_Services.html#efi-boot-services-loadimage
... and shim being a drop-in replacement, it's meant to be spec-compliant.
Trying to unload based on the assumption that loaded_image will remain undisturbed is an assumption waiting to be broken.
IMO, this wants to be instead:
if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) )
// unloadCheers,
Alejandro
Ping: [PATCH 0/3] efi: Shim LoadImage improvements
In reply to Gerald Elder-VassQuoted 4 lines · Gerald Elder-Vass
A previous series for supporting Shim's LoadImage protocol had some outstanding comments after it had been accepted, and internal review revealed an improvement to be safe when unloading the image.
Quoted 8 lines
This patch series includes those changes.
https://gitlab.com/xen-project/people/geraldev/xen/-/pipelines/2032454096
Gerald Elder-Vass (3):
efi: Fix line length ininit_secure_boot_mode
efi: Protect against unnecessary image unloading
efi: Limit Shim's Verify success toEFI_SUCCESS
Can there please be timely responses to small fixes like these ones? Especially patch 3 wants to go in rather sooner than later.
Thanks, Jan
Re: [PATCH 2/3] efi: Protect against unnecessary image unloading
In reply to Alejandro VallejoQuoted 69 lines · Alejandro Vallejo
Quoted 5 lines · Gerald Elder-Vass
Commit
59a1d6d3ea1eintroduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.Any what memory in use? The function loads an image, it's not a consumer.
It can't free anything because it doesn't know where it came from. It might've been embedded in your own binary, and it can't compromise its integrity like that.
Quoted 21 lines
Signed-off-by Gerald Elder-Vass <[email protected]>---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>--- xen/common/efi/boot.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)xen/common/efi/boot.c +0
-0xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1065
Quoted 5 lines
xen/common/efi/boot.c +1
-1@@ -1062,7 +1062,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)static EFI_GUID __initdata shim_image_guid =SHIM_IMAGE_LOADER_GUID ;static EFI_GUID __initdata shim_lock_guid =SHIM_LOCK_PROTOCOL_GUID ;SHIM_IMAGE_LOADER *shim_loader; -EFI_HANDLE loaded_kernel; +EFI_HANDLE loaded_kernel =NULL ;This isn't required if unloading checks for the success case or the only error case that has a successful load. See below.
Furthermore, you don't really know if the callee clobbered it.
Quoted 6 lines
xen/common/efi/boot.c +0
-0EFI_SHIM_LOCK_PROTOCOL *shim_lock;EFI_STATUS status;bool verified = false;@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) verified = true;xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1085
Quoted 10 lines
xen/common/efi/boot.c +4
-4/* -* Always unload the image. We only needed LoadImage() to perform -* verification anyway, and in the case of a failure there may still -* be cleanup needing to be performed. +* If the kernel was loaded, unload it. We only needed LoadImage() to +* perform verification anyway, and in the case of a failure there may +* still be cleanup needing to be performed. */ - shim_loader->UnloadImage (loaded_kernel); +if ( loaded_kernel )Not sure this is what you want. The image needs unloading only when there's no error OR the error is
EFI_SECURITY_VIOLATION. See section 7.4.1:https://uefi.org/specs/UEFI/2.11/07_Services_Boot_Services.html#efi-boot-services-loadimage
... and shim being a drop-in replacement, it's meant to be spec-compliant.
Quoted 7 lines
Trying to unload based on the assumption that
loaded_imagewill remain undisturbed is an assumption waiting to be broken.IMO, this wants to be instead:
if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) ) // unload
I agree, this version would be better.
Signature · 3 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab
Re: [PATCH 1/3] efi: Fix line length in init_secure_boot_mode
In reply to Gerald Elder-VassQuoted 4 lines · Gerald Elder-Vass
Commit
cb41b4ce14a9introducedinit_secure_boot_modebut one line was not wrapped appropriately.Signed-off-by Gerald Elder-Vass <[email protected]>
Quoted 31 lines
---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>--- xen/common/efi/boot.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)xen/common/efi/boot.c +2
-1@@ -923,7 +923,8 @@ static void __init init_secure_boot_mode(void)if ( status ==EFI_NOT_FOUND || (status ==EFI_SUCCESS && - attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS ) && size ==1 && data ==0 ) )/* Platform does not support Secure Boot or it's disabled. */ efi_secure_boot = false;Signature · 1 line
2.47.3
Signature · 3 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab
Marek Marczykowski-Górecki
Acked-byRe: [PATCH 3/3] efi: Limit Shim's Verify success to EFI_SUCCESS
In reply to Jan BeulichQuoted 5 lines · Jan Beulich
Quoted 4 lines · Gerald Elder-Vass
Commit
59a1d6d3ea1ereplaced the Verify status check with !EFI_ERROR(...), this changed the behaviour to consider any warnings (EFI_WARN_) to be considered a successful verification.
Quoted 8 lines
Quoted 1 line
This commit reverts that behaviour change.
Reported-by Jan Beulich <[email protected]>Fixes ...Quoted 1 line
Signed-off-by Gerald Elder-Vass <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Signature · 3 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab