From nobody Thu Dec 26 13:58:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+101127+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101127+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1678727919; cv=none; d=zohomail.com; s=zohoarc; b=FONHdRxU39qxNW6JbQzOWHT1W7MkiErAVC9pWvUsZm0+hTAnDpV6MD7BCyDiwstDK4r431kM75e7melw6mqcNwsgzX+s9d9eWEYXU8xszUbf56OMRmMUUmvbX33b4t3/EDYoCgDKJevr/ndr+FEPdC6XkGKWQJh371hoF26umlQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678727919; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=TilzSGi8k6/Xy8GpxcUzQ61DMizFeBaIXrVeabf0t3s=; b=m7AF3vFjRKwG54fB//9Td63GM7WjZTWxclKpl03rS2PrABfxC8BNG0FsjtO/px/ZEiJYgCzBAYtTKZb8UFvzeqC4JgLOPYTuKPvfkmABAsRb2FLSaana1hDkFrL2kpsLhSb8ofmtKnSpgIyJi1h7j/pJ7+Ub9yvN3piqQ1GrAE0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101127+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1678727919952372.7798047984654; Mon, 13 Mar 2023 10:18:39 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 8KF2YY1788612xpnsO3ahhyD; Mon, 13 Mar 2023 10:18:38 -0700 X-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 X-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) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31DB0C4339B; Mon, 13 Mar 2023 17:18:32 +0000 (UTC) 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: [edk2-devel] [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> In-Reply-To: <20230313171714.3866151-1-ardb@kernel.org> References: <20230313171714.3866151-1-ardb@kernel.org> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ardb@kernel.org X-Gm-Message-State: 1wOJaxbw6oksMdkGsJzROuJVx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678727918; bh=0W/LgiunQ4+2yIh5CAfaE07HIBcHs58rHRYgrK7amrU=; h=Cc:Date:From:Reply-To:Subject:To; b=u3l/5zJnaqAHuMY2WLnF9dYRyBWfhWFm4NKG+gSuDS/ZsIMo2PxoeF8JV345j9IV2SK RqQESdqtBr47eoCSh5/4DGOy1SjL0sS4DRL3IA300njmlAeyEb4Ml9oTfnXTVmBxaFgxF TDI0yPh7Bi1OcEq4+dQKENJ2sJwzs4ED9Ak= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678727920227100002 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Leif Lindholm --- 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 ); =20 -EFI_STATUS -EFIAPI -LoadPeCoffImage ( - IN VOID *PeCoffImage, - OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, - OUT EFI_PHYSICAL_ADDRESS *EntryPoint - ); - -EFI_STATUS -EFIAPI -LoadDxeCoreFromFfsFile ( - IN EFI_PEI_FILE_HANDLE FileHandle, - IN UINTN StackSize - ); - EFI_STATUS EFIAPI LoadDxeCoreFromFv ( 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 + Copyright (c) 2023, Google LLC. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PrePi.h" + +#include + +/** + Remap the code section of the DXE core with the read-only and executable + permissions. + + @param ImageContext The image context describing the loaded PE/COFF = image + +**/ +VOID +EFIAPI +RemapDxeCore ( + IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext + ) +{ + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_SECTION_HEADER *Section; + UINTN Index; + + Hdr.Union =3D (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINT8 *)ImageContext-= >Handle + + ImageContext->PeCoffHead= erOffset); + ASSERT (Hdr.Pe32->Signature =3D=3D EFI_IMAGE_NT_SIGNATURE); + + Section =3D (EFI_IMAGE_SECTION_HEADER *)((UINT8 *)Hdr.Union + sizeof (UI= NT32) + + sizeof (EFI_IMAGE_FILE_HEADER) + + Hdr.Pe32->FileHeader.SizeOfOption= alHeader + ); + + for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { + if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { + ArmSetMemoryRegionReadOnly ( + (UINTN)(ImageContext->ImageAddress + Section[Index].VirtualAddress= ), + Section[Index].Misc.VirtualSize + ); + + ArmClearMemoryRegionNoExec ( + (UINTN)(ImageContext->ImageAddress + Section[Index].VirtualAddress= ), + Section[Index].Misc.VirtualSize + ); + } + } +} 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))) #define GET_GUID_HOB_DATA_SIZE(GuidHob) (((GuidHob)->Header).HobLength - = sizeof (EFI_HOB_GUID_TYPE)) =20 +/** + Remap the code section of the DXE core with the read-only and executable + permissions. + + @param ImageContext The image context describing the loaded PE/COFF = image + +**/ +VOID +EFIAPI +RemapDxeCore ( + IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext + ); + #endif 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; } =20 +STATIC EFI_STATUS EFIAPI LoadPeCoffImage ( @@ -105,6 +106,8 @@ LoadPeCoffImage ( // InvalidateInstructionCacheRange ((VOID *)(UINTN)*ImageAddress, (UINTN)*I= mageSize); =20 + RemapDxeCore (&ImageContext); + return Status; } =20 @@ -114,6 +117,7 @@ VOID IN VOID *HobStart ); =20 +STATIC EFI_STATUS EFIAPI LoadDxeCoreFromFfsFile ( 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 PrePiLib.c =20 +[Sources.X64, Sources.IA32] + X86/RemapDxeCore.c + +[Sources.AARCH64, Sources.ARM] + Arm/RemapDxeCore.c + [Packages] MdePkg/MdePkg.dec EmbeddedPkg/EmbeddedPkg.dec MdeModulePkg/MdeModulePkg.dec =20 +[Packages.ARM, Packages.AARCH64] + ArmPkg/ArmPkg.dec + [LibraryClasses] BaseLib DebugLib @@ -50,6 +59,9 @@ [LibraryClasses] PerformanceLib HobLib =20 +[LibraryClasses.ARM, LibraryClasses.AARCH64] + ArmMmuLib + [Guids] gEfiMemoryTypeInformationGuid =20 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 + Copyright (c) 2023, Google LLC. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PrePi.h" + +/** + Remap the code section of the DXE core with the read-only and executable + permissions. + + @param ImageContext The image context describing the loaded PE/COFF = image + +**/ +VOID +EFIAPI +RemapDxeCore ( + IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext + ) +{ +} --=20 2.39.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#101127): https://edk2.groups.io/g/devel/message/101127 Mute This Topic: https://groups.io/mt/97586028/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-