From nobody Wed Feb 5 04:56:24 2025 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+101136+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+101136+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1678727942; cv=none; d=zohomail.com; s=zohoarc; b=ESwKcV+PBAXBbFWmxrynVdrpZ4LUpaMQrDMxd49t2F2qh8UrV+uU0GzQVi5VmMXBv1XUIxKpcysxfM2DJMrzS4KOMPiuoJOgl95E4d3qAsK3ON8qDU8oZ86iPEGXlKQk2rMFrVrdZBUOLOyCl3VjG4FvLxfErRmgqSMeaLbL1ho= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678727942; 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=eBP+kHjg9HytJV3MyZliIbZS7w9mutEnTS0r0Xi8WSQ=; b=hkxqpqOnaAOme75YEyrtBB7Wq24YhcDczqeiaHKFviqgGA3OwtuoQiyAqSKwceW3aMXOjzicwUy8Od6LzIH9ilLmNegoXHxiA67cYPva5sGzkg66IC2ZztQITeTFxQRk/cz6u8Bk/+6AD3TxZ3js01PQ4zdeZ/OSibcmJSyzL+4= 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+101136+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 1678727942222538.2362331581134; Mon, 13 Mar 2023 10:19:02 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id bVGCYY1788612xiNTntzPlaS; Mon, 13 Mar 2023 10:19:01 -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.25828.1678727941027614545 for ; Mon, 13 Mar 2023 10:19:01 -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 6EE60B8119D; Mon, 13 Mar 2023 17:18:59 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF414C433A7; Mon, 13 Mar 2023 17:18:55 +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 32/38] MdeModulePkg/DxeCore: Update memory protections before freeing a region Date: Mon, 13 Mar 2023 18:17:08 +0100 Message-Id: <20230313171714.3866151-33-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: zLTHQL5RAvpH13roZTdX2uv3x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678727941; bh=b1ukQ6NqIjTiIx859DwUi6cHyDdQMdrKFEFtRbNtiuw=; h=Cc:Date:From:Reply-To:Subject:To; b=GAI52XZEK3MvF6SytpXHadIbVxaEXe6STyfDXpKoQa+AstG+4zDgrwo6gCMQZ70q3HC 3zussx6kB/zUHZaY6xWAeYeZY+nTFAg7FIgIcbN+ZeR2rM7ZCH7YGultL4U5Z3Z3DOG0o nnl8PJAHGAx04w0FDaDlQkCkfNC//PxjbvQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678727942424100009 Content-Type: text/plain; charset="utf-8" Currently, we invoke ApplyMemoryProtectionPolicy() after CoreInternalFreePages() has returned successfully, in order to update the memory permission attributes of the region to match the policy for EfiConventionalMemory. There are two problems with that: - CoreInternalFreePages() will round up the size of the allocation to the appropriate alignment of the memory type, but we only remap the number of pages that was passed by the caller, leaving the remaining pages freed but mapped with the old permissions; - in DEBUG builds, we may attempt to clear the entire region while it is still mapped with read-only or read-protect attributes. Let's address both issues, by updating the permissions before performing the actual conversion. Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/Mem/Page.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/P= age.c index 5903ce7ab525..f5b940bbc25b 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -1519,8 +1519,8 @@ CoreAllocatePages ( @return EFI_SUCCESS -Pages successfully freed. =20 **/ +STATIC EFI_STATUS -EFIAPI CoreInternalFreePages ( IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages, @@ -1574,6 +1574,13 @@ CoreInternalFreePages ( NumberOfPages +=3D EFI_SIZE_TO_PAGES (Alignment) - 1; NumberOfPages &=3D ~(EFI_SIZE_TO_PAGES (Alignment) - 1); =20 + ApplyMemoryProtectionPolicy ( + Entry->Type, + EfiConventionalMemory, + Memory, + EFI_PAGES_TO_SIZE (NumberOfPages) + ); + if (MemoryType !=3D NULL) { *MemoryType =3D Entry->Type; } @@ -1628,12 +1635,6 @@ CoreFreePages ( NULL ); InstallMemoryAttributesTableOnMemoryAllocation (MemoryType); - ApplyMemoryProtectionPolicy ( - MemoryType, - EfiConventionalMemory, - Memory, - EFI_PAGES_TO_SIZE (NumberOfPages) - ); } =20 return Status; --=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 (#101136): https://edk2.groups.io/g/devel/message/101136 Mute This Topic: https://groups.io/mt/97586052/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-