From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 4BD4BAC1AE6 for ; Fri, 18 Aug 2023 22:57:56 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=dlh7mjNyrD6aNKLWa9KaAV2z8Tq4PAMVfhTHv2uGkR4=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1692399475; v=1; b=hHU799P6sWeByyazrM5YLuMJaEfZU75q/Ob/kds/MfsAByEbL3c5rkuY+/Dlzyp3O5HH+vpO PtxVgxEp8huIYe5drh3TT/cKbZiLeaRb3GSBArlDsW1tLnujVJcmeS6zv0hSc7QOWYggmZZ9pW6 iJ84xqXSNra7hMisBNyVHtxY= X-Received: by 127.0.0.2 with SMTP id YxG6YY7687511xcorFMFOiar; Fri, 18 Aug 2023 15:57:55 -0700 X-Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by mx.groups.io with SMTP id smtpd.web10.3185.1692397948436592478 for ; Fri, 18 Aug 2023 15:32:28 -0700 X-Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-686be3cbea0so1777025b3a.0 for ; Fri, 18 Aug 2023 15:32:28 -0700 (PDT) X-Gm-Message-State: 3b9ClV7jcxpoMkWJ4uFOSMyfx7686176AA= X-Google-Smtp-Source: AGHT+IEJkD5polVJw0ooG2wvRVP4tAZ8gDrsfth4toDIY/tUQwvw1p/ccbWayScJGu7xl96mAV7vOQ== X-Received: by 2002:a05:6a20:a11a:b0:132:ce08:1e28 with SMTP id q26-20020a056a20a11a00b00132ce081e28mr819959pzk.22.1692397947613; Fri, 18 Aug 2023 15:32:27 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id 7-20020aa79207000000b0068779015507sm1989330pfo.194.2023.08.18.15.32.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Aug 2023 15:32:27 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v2 05/25] MdeModulePkg: Apply Protections to the HOB List Date: Fri, 18 Aug 2023 15:31:37 -0700 Message-ID: <20230818223159.1073-6-taylor.d.beebe@gmail.com> In-Reply-To: <20230818223159.1073-1-taylor.d.beebe@gmail.com> References: <20230818223159.1073-1-taylor.d.beebe@gmail.com> MIME-Version: 1.0 Precedence: Bulk 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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=hHU799P6; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Because the platform memory protection settings will be stored in the HOB, the HOB list should be marked read-only and non-executable as soon as possible in boot. This patch page-aligns the allocated HOB list in DXE and marks it RO/NX during memory protection initialization. Signed-off-by: Taylor Beebe Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi --- MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 18 ++++++------ MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c | 29 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c index 792cd2e0af23..72bd036eab1e 100644 --- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c +++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c @@ -2764,21 +2764,21 @@ CoreInitializeGcdServices ( } // - // Relocate HOB List to an allocated pool buffer. + // Relocate HOB List to allocated pages. // The relocation should be at after all the tested memory resources added // (except the memory space that covers HOB List) to the memory services, // because the memory resource found in CoreInitializeMemoryServices() // may have not enough remaining resource for HOB List. // - NewHobList = AllocateCopyPool ( - (UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart), - *HobStart - ); - ASSERT (NewHobList != NULL); - - *HobStart = NewHobList; - gHobList = NewHobList; + NewHobList = AllocatePages (EFI_SIZE_TO_PAGES ((UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart))); + if (NewHobList != NULL) { + CopyMem (NewHobList, *HobStart, (UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart)); + *HobStart = NewHobList; + } else { + ASSERT (NewHobList != NULL); + } + gHobList = *HobStart; if (MemorySpaceMapHobList != NULL) { // // Add and allocate the memory space that covers HOB List to the memory services diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c index 7cc829b17402..6c1c17a5c205 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -967,6 +967,32 @@ InitializeDxeNxMemoryProtectionPolicy ( } } +/** + Mark the HOB list as read-only and non-executable. +**/ +STATIC +VOID +ProtectHobList ( + VOID + ) +{ + EFI_PEI_HOB_POINTERS Hob; + + Hob.Raw = GetHobList (); + + // Find the end of the HOB list. + while (!END_OF_HOB_LIST (Hob)) { + Hob.Raw = GET_NEXT_HOB (Hob); + } + + // Protect the HOB list. + SetUefiImageMemoryAttributes ( + (UINTN)gHobList, + ALIGN_VALUE (((UINTN)Hob.Raw + GET_HOB_LENGTH (Hob)) - (UINTN)GetHobList (), EFI_PAGE_SIZE), + EFI_MEMORY_XP | EFI_MEMORY_RO + ); +} + /** A notification for CPU_ARCH protocol. @@ -995,6 +1021,9 @@ MemoryProtectionCpuArchProtocolNotify ( goto Done; } + // Mark the HOB list XP and RO. + ProtectHobList (); + // // Apply the memory protection policy on non-BScode/RTcode regions. // -- 2.41.0.windows.3 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#107860): https://edk2.groups.io/g/devel/message/107860 Mute This Topic: https://groups.io/mt/100830903/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-