From nobody Sat Feb 7 10:15:29 2026 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+100092+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+100092+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1676301527; cv=none; d=zohomail.com; s=zohoarc; b=nNnpMQz9C6Rlq2uSbJUt8+22ddi7jhOJnlDDoEZMkKHe+vU/OgmTQ5QjVKwCvkaNHF2qBPcNtg/HOXUhDpdjj8ofswQBuUOftmG9W8d2aUxGq6f7Wf6alGV5bDZFGKbtakfECHj2wfEl/+Nq46j4EXJNaWmp8s6pshou0FWBUF4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676301527; 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=WbmXDFDeVtC5eLR4/gkE1fFxvMdD7Waqi04JTCSZIbE=; b=lXOHIk0WN+b5T/A5Q3QCw06yMcmWwsX05fhfFJA58tIcICHkRb0Yz6ulKRCxQALTcvI0MW0PfMspnNOEpP2JlQq5SK/R1okY3yTAF7pbthSfluYTc9GYCyaEQ1iDWZYJ3tLvlb//oSaCmkeGi+Ywsp8rePdlnc3r3AexRToMMDM= 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+100092+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 1676301527062429.12918976582387; Mon, 13 Feb 2023 07:18:47 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id CtV8YY1788612x679w80POqj; Mon, 13 Feb 2023 07:18:46 -0800 X-Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web10.16812.1676301525629520842 for ; Mon, 13 Feb 2023 07:18:46 -0800 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 18BB2B8125E; Mon, 13 Feb 2023 15:18:44 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39F1EC433D2; Mon, 13 Feb 2023 15:18:40 +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 , Matthew Garrett , Peter Jones , Kees Cook Subject: [edk2-devel] [RFC 05/13] MdeModulePkg/DxeIpl AARCH64: Remap DXE core code section before launch Date: Mon, 13 Feb 2023 16:18:02 +0100 Message-Id: <20230213151810.2301480-6-ardb@kernel.org> In-Reply-To: <20230213151810.2301480-1-ardb@kernel.org> References: <20230213151810.2301480-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: DaphuucpyEkMcml923PKJg7Nx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1676301526; bh=WoVOORN/ZpFIdtqQ73aK/IzTmj/ACS8a/3XAx1tjZwM=; h=Cc:Date:From:Reply-To:Subject:To; b=W5vknQqr2km/rlPeCqe6frPqwjcKq+dd6SorT0uojI6pIDjvymxb/i0DoH/HivF4e5f mWidx8IFHjY7LzrZhZ80IY0eFiIcYP5/Yf4aeyeUxdEqTxIRASTOBSNcwIZ6q1zzPK7DL QTLB/ZlMBzONcs6rKwsvg7SAozyldh0Nzug= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1676301527791100009 Content-Type: text/plain; charset="utf-8" To permit the platform to adopt a stricter policy when it comes to memory protections, and map all memory XP by default, add the necessary handling to the DXE IPL PEIM to ensure that the DXE core code section is mapped executable before invoking the DXE core. It is up to the DXE core itself to manage the executable permissions on other DXE and UEFI drivers and applications that it dispatches. Note that this requires that the DXE IPL executes non-shadowed from a FV that is mapped executable. Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c | 69 ++++++++++++++++++++ MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf | 1 + 2 files changed, 70 insertions(+) diff --git a/MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c b/MdeModulePkg/= Core/DxeIplPeim/Arm/DxeLoadFunc.c index f62b6dcb38a7..21eac2851554 100644 --- a/MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c +++ b/MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c @@ -11,6 +11,69 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "DxeIpl.h" =20 #include +#include + +STATIC +VOID +RemapDxeCoreCodeReadOnly ( + IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint, + IN EFI_PEI_HOB_POINTERS HobList + ) +{ + EFI_PEI_HOB_POINTERS Hob; + EFI_HOB_MEMORY_ALLOCATION *ModuleHob; + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + RETURN_STATUS Status; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_SECTION_HEADER *Section; + UINTN Index; + + ImageContext.ImageRead =3D PeCoffLoaderImageReadFromMemory; + ImageContext.Handle =3D NULL; + + // + // Find the module HOB for the DXE core + // + for (Hob.Raw =3D HobList.Raw; !END_OF_HOB_LIST (Hob); Hob.Raw =3D GET_NE= XT_HOB (Hob)) { + if (GET_HOB_TYPE (Hob) =3D=3D EFI_HOB_TYPE_MEMORY_ALLOCATION) { + ModuleHob =3D Hob.MemoryAllocation; + if ((ModuleHob->AllocDescriptor.MemoryBaseAddress <=3D DxeCoreEntryP= oint && + ((ModuleHob->AllocDescriptor.MemoryBaseAddress + ModuleHob->Allo= cDescriptor.MemoryLength) > DxeCoreEntryPoint))) + { + ImageContext.Handle =3D (VOID *)(UINTN)ModuleHob->AllocDescriptor.= MemoryBaseAddress; + break; + } + } + } + + ASSERT (ImageContext.Handle !=3D NULL); + + Status =3D PeCoffLoaderGetImageInfo (&ImageContext); + ASSERT_RETURN_ERROR (Status); + + Hdr.Union =3D (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINT8 *)ImageContext.= Handle + + ImageContext.PeCoffHeade= rOffset); + 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)((UINT8 *)ImageContext.Handle + Section[Index].VirtualAdd= ress), + Section[Index].Misc.VirtualSize + ); + + ArmClearMemoryRegionNoExec ( + (UINTN)((UINT8 *)ImageContext.Handle + Section[Index].VirtualAdd= ress), + Section[Index].Misc.VirtualSize + ); + } + } +} =20 /** Transfers control to DxeCore. @@ -33,6 +96,12 @@ HandOffToDxeCore ( VOID *TopOfStack; EFI_STATUS Status; =20 + // + // DRAM may be mapped with non-executable permissions by default, so + // we'll need to map the DXE core code region executable explicitly. + // + RemapDxeCoreCodeReadOnly (DxeCoreEntryPoint, HobList); + // // Allocate 128KB for the Stack // diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf b/MdeModulePkg/Core/Dx= eIplPeim/DxeIpl.inf index 62821477d012..d85ca79dc0c3 100644 --- a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf +++ b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf @@ -82,6 +82,7 @@ [LibraryClasses] =20 [LibraryClasses.ARM, LibraryClasses.AARCH64] ArmMmuLib + PeCoffLib =20 [Ppis] gEfiDxeIplPpiGuid ## PRODUCES --=20 2.39.1 -=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 (#100092): https://edk2.groups.io/g/devel/message/100092 Mute This Topic: https://groups.io/mt/96937479/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-