From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web10.46509.1685355458568862230 for ; Mon, 29 May 2023 03:17:38 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BY+4k7oD; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 205DC60AF5; Mon, 29 May 2023 10:17:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24E3EC433EF; Mon, 29 May 2023 10:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685355457; bh=qzDGQkgpvTtxJHdB7XbORzlYlbe2Q0u9gVJo9RAYQoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BY+4k7oD1sXUZR5UfyHL2v+xAWbJrE/8GTaFpiNb3Jv8qqjkjC6IUNkm8t20K/HRI rY20HuReakqBTNxdxgk0cyASe8Xt3+5m6uTm9KR/m9GqBxynXfLIEoRxBHGaGW6UQH G+nPrXPk6KkcD6qR7WpDhEb9IbYGyBnz3PVVFOpa+3q1Z2uo1UbImthWhANZejwiYX odzGFuJXx5POySnDzJaXq771f9PADKFBZKypeWcjIuFUexMRZhSfufIRhi0/y2C7Li WpntLgGr57xMNy57jLwJCk4i8E1r4xYjwDrZExkE4K6kASHQjYNUiW7h7OVUmRra2B hgR/BF1xpVHvQ== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Ray Ni , Jiewen Yao , Gerd Hoffmann , Taylor Beebe , Oliver Smith-Denny , Dandan Bi , Liming Gao , "Kinney, Michael D" , Leif Lindholm , Michael Kubacki Subject: [RFC PATCH 07/11] MdeModulePkg/DxeCore: Execute loaded images in place if possible Date: Mon, 29 May 2023 12:17:01 +0200 Message-Id: <20230529101705.2476949-8-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529101705.2476949-1-ardb@kernel.org> References: <20230529101705.2476949-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 --- 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/Im= age/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=0D CoreLoadPeImage (=0D IN BOOLEAN BootPolicy,=0D - IN VOID *Pe32Handle,=0D + IN IMAGE_FILE_HANDLE *Pe32Handle,=0D IN LOADED_IMAGE_PRIVATE_DATA *Image,=0D IN UINT32 Attribute=0D )=0D @@ -630,10 +630,16 @@ CoreLoadPeImage ( return EFI_UNSUPPORTED;=0D }=0D =0D + //=0D + // Check whether the loaded image can be executed in place=0D + //=0D + if (Image->ImageContext.ImageAddress =3D=3D (PHYSICAL_ADDRESS)(UINTN)Pe3= 2Handle->Source) {=0D + goto ExecuteInPlace;=0D + }=0D +=0D //=0D // Allocate Destination Buffer as caller did not pass it in=0D //=0D -=0D if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {=0D Size =3D (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.Se= ctionAlignment;=0D } else {=0D @@ -704,6 +710,7 @@ CoreLoadPeImage ( //=0D // Load the image from the file into the allocated memory=0D //=0D +ExecuteInPlace:=0D Status =3D PeCoffLoaderLoadImage (&Image->ImageContext);=0D if (EFI_ERROR (Status)) {=0D goto Done;=0D --=20 2.39.2