From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web10.25816.1678727917200574175 for ; Mon, 13 Mar 2023 10:18:37 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UzkyXl5u; spf=pass (domain: kernel.org, ip: 145.40.68.75, 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 ams.source.kernel.org (Postfix) with ESMTPS id 9700CB811C0; Mon, 13 Mar 2023 17:18:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31DB0C4339B; Mon, 13 Mar 2023 17:18:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678727914; bh=11UjkhPiYyupRGtqdQcDwPHRnRgJUxyaOMkE5YWRX0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UzkyXl5uaAvdmsHaD18Yv9Bwk2OdfJq3j5urJoVcZmNV8zak3CvyLJFGphMiiTdCh 1wSnDiuLk3uvlMZpKvTW9D316F3d2F8zuI/J4O4D4I3XBkYc/8YErm0mB26cOHoiFc sgxtn7TaQ2Q2DUDstxwya5BT2VJHIM3xniSgHM1v1CSjsshJXFHOsDRN4zrBV0acLz DOqnf9fi+s6IBlrF/GqUu0g+UgTsPnuGUoIOJ/NpdNVhhAm+5ukW095CIJTZYa4K76 bnJ06XP8lWp0rMrQJ1RnEqyBb5N/71Kbu9oiNakEfStlOVqGQK41y+v8PuJ78Iyvst El1XwkhAgQ3Ag== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe Subject: [PATCH v5 23/38] EmbeddedPkg/PrePiLib AARCH64: Remap DXE core before execution Date: Mon, 13 Mar 2023 18:16:59 +0100 Message-Id: <20230313171714.3866151-24-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230313171714.3866151-1-ardb@kernel.org> References: <20230313171714.3866151-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Deal with DRAM memory potentially being mapped with non-executable permissions, by mapping the DXE core code sections explicitly before launch. Signed-off-by: Ard Biesheuvel --- EmbeddedPkg/Include/Library/PrePiLib.h | 16 ------ EmbeddedPkg/Library/PrePiLib/Arm/RemapDxeCore.c | 51 ++++++++++++++++++++ EmbeddedPkg/Library/PrePiLib/PrePi.h | 13 +++++ EmbeddedPkg/Library/PrePiLib/PrePiLib.c | 4 ++ EmbeddedPkg/Library/PrePiLib/PrePiLib.inf | 12 +++++ EmbeddedPkg/Library/PrePiLib/X86/RemapDxeCore.c | 23 +++++++++ 6 files changed, 103 insertions(+), 16 deletions(-) diff --git a/EmbeddedPkg/Include/Library/PrePiLib.h b/EmbeddedPkg/Include/L= ibrary/PrePiLib.h index 93a9115eac2d..14f2bbc38dae 100644 --- a/EmbeddedPkg/Include/Library/PrePiLib.h +++ b/EmbeddedPkg/Include/Library/PrePiLib.h @@ -758,22 +758,6 @@ AllocateAlignedPages ( IN UINTN Alignment=0D );=0D =0D -EFI_STATUS=0D -EFIAPI=0D -LoadPeCoffImage (=0D - IN VOID *PeCoffImage,=0D - OUT EFI_PHYSICAL_ADDRESS *ImageAddress,=0D - OUT UINT64 *ImageSize,=0D - OUT EFI_PHYSICAL_ADDRESS *EntryPoint=0D - );=0D -=0D -EFI_STATUS=0D -EFIAPI=0D -LoadDxeCoreFromFfsFile (=0D - IN EFI_PEI_FILE_HANDLE FileHandle,=0D - IN UINTN StackSize=0D - );=0D -=0D EFI_STATUS=0D EFIAPI=0D LoadDxeCoreFromFv (=0D diff --git a/EmbeddedPkg/Library/PrePiLib/Arm/RemapDxeCore.c b/EmbeddedPkg/= Library/PrePiLib/Arm/RemapDxeCore.c new file mode 100644 index 000000000000..40d4ed9d77bd --- /dev/null +++ b/EmbeddedPkg/Library/PrePiLib/Arm/RemapDxeCore.c @@ -0,0 +1,51 @@ +/** @file=0D + Copyright (c) 2023, Google LLC. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "PrePi.h"=0D +=0D +#include =0D +=0D +/**=0D + Remap the code section of the DXE core with the read-only and executable= =0D + permissions.=0D +=0D + @param ImageContext The image context describing the loaded PE/COFF = image=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RemapDxeCore (=0D + IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D + )=0D +{=0D + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;=0D + EFI_IMAGE_SECTION_HEADER *Section;=0D + UINTN Index;=0D +=0D + Hdr.Union =3D (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINT8 *)ImageContext-= >Handle +=0D + ImageContext->PeCoffHead= erOffset);=0D + ASSERT (Hdr.Pe32->Signature =3D=3D EFI_IMAGE_NT_SIGNATURE);=0D +=0D + Section =3D (EFI_IMAGE_SECTION_HEADER *)((UINT8 *)Hdr.Union + sizeof (UI= NT32) +=0D + sizeof (EFI_IMAGE_FILE_HEADER) += =0D + Hdr.Pe32->FileHeader.SizeOfOption= alHeader=0D + );=0D +=0D + for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) {=0D + if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) = {=0D + ArmSetMemoryRegionReadOnly (=0D + (UINTN)(ImageContext->ImageAddress + Section[Index].VirtualAddress= ),=0D + Section[Index].Misc.VirtualSize=0D + );=0D +=0D + ArmClearMemoryRegionNoExec (=0D + (UINTN)(ImageContext->ImageAddress + Section[Index].VirtualAddress= ),=0D + Section[Index].Misc.VirtualSize=0D + );=0D + }=0D + }=0D +}=0D diff --git a/EmbeddedPkg/Library/PrePiLib/PrePi.h b/EmbeddedPkg/Library/Pre= PiLib/PrePi.h index a00c946512f4..a0f8837d1d37 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePi.h +++ b/EmbeddedPkg/Library/PrePiLib/PrePi.h @@ -37,4 +37,17 @@ #define GET_GUID_HOB_DATA(GuidHob) ((VOID *) (((UINT8 *) &((GuidHob)= ->Name)) + sizeof (EFI_GUID)))=0D #define GET_GUID_HOB_DATA_SIZE(GuidHob) (((GuidHob)->Header).HobLength - = sizeof (EFI_HOB_GUID_TYPE))=0D =0D +/**=0D + Remap the code section of the DXE core with the read-only and executable= =0D + permissions.=0D +=0D + @param ImageContext The image context describing the loaded PE/COFF = image=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RemapDxeCore (=0D + IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D + );=0D +=0D #endif=0D diff --git a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c b/EmbeddedPkg/Library/= PrePiLib/PrePiLib.c index 3cf866dab248..188ad5c518a8 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c +++ b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c @@ -54,6 +54,7 @@ AllocateCodePages ( return NULL;=0D }=0D =0D +STATIC=0D EFI_STATUS=0D EFIAPI=0D LoadPeCoffImage (=0D @@ -105,6 +106,8 @@ LoadPeCoffImage ( //=0D InvalidateInstructionCacheRange ((VOID *)(UINTN)*ImageAddress, (UINTN)*I= mageSize);=0D =0D + RemapDxeCore (&ImageContext);=0D +=0D return Status;=0D }=0D =0D @@ -114,6 +117,7 @@ VOID IN VOID *HobStart=0D );=0D =0D +STATIC=0D EFI_STATUS=0D EFIAPI=0D LoadDxeCoreFromFfsFile (=0D diff --git a/EmbeddedPkg/Library/PrePiLib/PrePiLib.inf b/EmbeddedPkg/Librar= y/PrePiLib/PrePiLib.inf index 090bfe888f52..2df5928c51d5 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePiLib.inf +++ b/EmbeddedPkg/Library/PrePiLib/PrePiLib.inf @@ -31,11 +31,20 @@ [Sources.common] FwVol.c=0D PrePiLib.c=0D =0D +[Sources.X64, Sources.IA32]=0D + X86/RemapDxeCore.c=0D +=0D +[Sources.AARCH64, Sources.ARM]=0D + Arm/RemapDxeCore.c=0D +=0D [Packages]=0D MdePkg/MdePkg.dec=0D EmbeddedPkg/EmbeddedPkg.dec=0D MdeModulePkg/MdeModulePkg.dec=0D =0D +[Packages.ARM, Packages.AARCH64]=0D + ArmPkg/ArmPkg.dec=0D +=0D [LibraryClasses]=0D BaseLib=0D DebugLib=0D @@ -50,6 +59,9 @@ [LibraryClasses] PerformanceLib=0D HobLib=0D =0D +[LibraryClasses.ARM, LibraryClasses.AARCH64]=0D + ArmMmuLib=0D +=0D [Guids]=0D gEfiMemoryTypeInformationGuid=0D =0D diff --git a/EmbeddedPkg/Library/PrePiLib/X86/RemapDxeCore.c b/EmbeddedPkg/= Library/PrePiLib/X86/RemapDxeCore.c new file mode 100644 index 000000000000..1899c050fdec --- /dev/null +++ b/EmbeddedPkg/Library/PrePiLib/X86/RemapDxeCore.c @@ -0,0 +1,23 @@ +/** @file=0D + Copyright (c) 2023, Google LLC. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "PrePi.h"=0D +=0D +/**=0D + Remap the code section of the DXE core with the read-only and executable= =0D + permissions.=0D +=0D + @param ImageContext The image context describing the loaded PE/COFF = image=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RemapDxeCore (=0D + IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D + )=0D +{=0D +}=0D --=20 2.39.2