In the image loader, check whether an image has already been relocated
to the address from which it is being loaded. This is not something that
can happen by accident, and so we can assume that this means that the
image was intended to be executed in place.

This removes a redundant copy of the image contents, and also permits
the image to be mapped with restricted permissions even before the CPU
arch protocol has been dispatched.

Signed-off-by: Ard Biesheuvel <a...@kernel.org>
---
 MdeModulePkg/Core/Dxe/Image/Image.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c 
b/MdeModulePkg/Core/Dxe/Image/Image.c
index 3dfab4829b3ca17f..621637e869daf62d 100644
--- a/MdeModulePkg/Core/Dxe/Image/Image.c
+++ b/MdeModulePkg/Core/Dxe/Image/Image.c
@@ -573,7 +573,7 @@ STATIC
 EFI_STATUS
 CoreLoadPeImage (
   IN BOOLEAN                    BootPolicy,
-  IN VOID                       *Pe32Handle,
+  IN IMAGE_FILE_HANDLE          *Pe32Handle,
   IN LOADED_IMAGE_PRIVATE_DATA  *Image,
   IN  UINT32                    Attribute
   )
@@ -630,10 +630,16 @@ CoreLoadPeImage (
       return EFI_UNSUPPORTED;
   }
 
+  //
+  // Check whether the loaded image can be executed in place
+  //
+  if (Image->ImageContext.ImageAddress == 
(PHYSICAL_ADDRESS)(UINTN)Pe32Handle->Source) {
+    goto ExecuteInPlace;
+  }
+
   //
   // Allocate Destination Buffer as caller did not pass it in
   //
-
   if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {
     Size = (UINTN)Image->ImageContext.ImageSize + 
Image->ImageContext.SectionAlignment;
   } else {
@@ -704,6 +710,7 @@ CoreLoadPeImage (
   //
   // Load the image from the file into the allocated memory
   //
+ExecuteInPlace:
   Status = PeCoffLoaderLoadImage (&Image->ImageContext);
   if (EFI_ERROR (Status)) {
     goto Done;
-- 
2.39.2



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#105369): https://edk2.groups.io/g/devel/message/105369
Mute This Topic: https://groups.io/mt/99197140/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to