From nobody Fri Dec 13 12:03:58 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+101122+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+101122+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1678727905; cv=none; d=zohomail.com; s=zohoarc; b=MMF8zBlp3Pk3RJpLdSGMRVAtiBo0Rim6r+CrsB6/NyJMs+1VPA9vXE1tzdZksA4ZCDPjlxEYYJQWlOIQ0HPf+IDmpq0hJnhTNdAeaDs/LmN2w2w9Tq4EmVnKwmGUuYPHXj7jbZ5IZzUndALPx1akO7C97qwZgMbXvceC96g26rs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678727905; 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=sAZHDxU7TIvFXETEFGlZAm6xSQjMWzUKlewPShaM6xY=; b=T+Bh7pRf2ohpHdVsb3cmFax6laln1yV7GGiH+52JAAn+pPVuDqhSzFTkhQLL9jtgvWEMliyqPlkY1KrlKZA+codLW2rbI2buNM62lY5AvTZMnbiv5/pte8gEKSpTNUUwLR0xD4dsF0f6H2emwB7BxjjjZI6dqMCipxGO/DPJzsQ= 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+101122+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 1678727905045331.13861020774186; Mon, 13 Mar 2023 10:18:25 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id raKmYY1788612xEYh9ATxz0A; Mon, 13 Mar 2023 10:18:24 -0700 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web10.25808.1678727903975659556 for ; Mon, 13 Mar 2023 10:18:24 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 8250F6144D; Mon, 13 Mar 2023 17:18:23 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02E8CC4339B; Mon, 13 Mar 2023 17:18:18 +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 18/38] MdeModulePkg/DxeIpl AARCH64: Remap DXE core code section before launch Date: Mon, 13 Mar 2023 18:16:54 +0100 Message-Id: <20230313171714.3866151-19-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: Cr3OjyyyK3gM65OM0E13Awegx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678727904; bh=RR1s7izGl/QmRn8Hb0/3f8JPMGGkLybobZrjqSGYBzw=; h=Cc:Date:From:Reply-To:Subject:To; b=PQ3ZeZLQhXJt6Dor1p71EvKbuv1wWPQH99qZPHSrRDniCxqadqitHLyxIjbr/T2ZJE5 m82vRBEsISBzwMVPEp8bXIcj52NKkE0GjeMJj/ZeyBDJvPcNU6chLD7nTeAh1kCEJBo1Q Icy6j/mgcu/DKfalDoYvXFQnk6iPdtzhnf8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678727906134100002 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 | 73 ++++++++++++++++++++ MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf | 1 + 2 files changed, 74 insertions(+) diff --git a/MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c b/MdeModulePkg/= Core/DxeIplPeim/Arm/DxeLoadFunc.c index f62b6dcb38a7..c57ffa87e30f 100644 --- a/MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c +++ b/MdeModulePkg/Core/DxeIplPeim/Arm/DxeLoadFunc.c @@ -11,6 +11,73 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "DxeIpl.h" =20 #include +#include + +/** + Discover the code sections of the DXE core, and remap them read-only + and executable. + + @param DxeCoreEntryPoint The entrypoint of the DXE core executable. + @param HobList The list of HOBs passed to the DXE core from P= EI. +**/ +STATIC +VOID +RemapDxeCoreCodeReadOnly ( + IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint, + IN EFI_PEI_HOB_POINTERS HobList + ) +{ + EFI_PEI_HOB_POINTERS Hob; + 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) && + (CompareGuid (&Hob.MemoryAllocation->AllocDescriptor.Name, &gEfiHo= bMemoryAllocModuleGuid)) && + (Hob.MemoryAllocationModule->EntryPoint =3D=3D DxeCoreEntryPoint)) + { + ImageContext.Handle =3D (VOID *)(UINTN)Hob.MemoryAllocation->AllocDe= scriptor.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].VirtualAddre= ss), + Section[Index].Misc.VirtualSize + ); + + ArmClearMemoryRegionNoExec ( + (UINTN)((UINT8 *)ImageContext.Handle + Section[Index].VirtualAddre= ss), + Section[Index].Misc.VirtualSize + ); + } + } +} =20 /** Transfers control to DxeCore. @@ -33,6 +100,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.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 (#101122): https://edk2.groups.io/g/devel/message/101122 Mute This Topic: https://groups.io/mt/97586019/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-