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 7FD3B940275 for ; Mon, 9 Oct 2023 00:07:58 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=VcJif1xS3q56Ubs0q750ABwn7nrI4SHgfFo2vPv810c=; 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=1696810077; v=1; b=pJXQP/+9ZcjXus76ez4IE/iTA6tj4ql6hC/EyYGgfpi+qCd/DLvZsyZZCyWEh9jLGkOnPbr1 zJlA/EhPsXTSR+Vsvn/KrHY/JTYwlyRvtf57HKmsryTLNL1qPminN0VqhINNCCbf0CrQlbjKTHN EO9IYtNdqycWeW1ut90RuDUQ= X-Received: by 127.0.0.2 with SMTP id KaPxYY7687511xI4aSSsSYlV; Sun, 08 Oct 2023 17:07:57 -0700 X-Received: from mail-oa1-f44.google.com (mail-oa1-f44.google.com [209.85.160.44]) by mx.groups.io with SMTP id smtpd.web10.50209.1696810076513247756 for ; Sun, 08 Oct 2023 17:07:56 -0700 X-Received: by mail-oa1-f44.google.com with SMTP id 586e51a60fabf-1e12f41e496so2763766fac.3 for ; Sun, 08 Oct 2023 17:07:56 -0700 (PDT) X-Gm-Message-State: 6lPF9Omqd7Y5M4gRkD48Cx7ax7686176AA= X-Google-Smtp-Source: AGHT+IGGXFlhxEVKvfluMVElGbEkiSEF/lw0BA4LYlgQMPS3rXBFFhHTVq7bgs/5HjIO3Xpi/TL95Q== X-Received: by 2002:a05:6870:e307:b0:1bb:a227:7008 with SMTP id z7-20020a056870e30700b001bba2277008mr15594981oad.3.1696810075358; Sun, 08 Oct 2023 17:07:55 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id t20-20020a62ea14000000b0068fcc7f6b00sm5048320pfh.74.2023.10.08.17.07.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 17:07:54 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao Subject: [edk2-devel] [PATCH v5 01/28] MdeModulePkg: Add DXE and MM Memory Protection Settings Definitions Date: Sun, 8 Oct 2023 17:07:13 -0700 Message-ID: <20231009000742.1792-2-taylor.d.beebe@gmail.com> In-Reply-To: <20231009000742.1792-1-taylor.d.beebe@gmail.com> References: <20231009000742.1792-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="pJXQP/+9"; 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 These headers provide settings definitions for memory protections, settings profiles for easily enabling memory protections, and the GUIDs used for producing the memory protection HOB entry. The settings options are functionally 1:1 with the existing PCD bitfield definitions. Instead of setting a fixed at build PCD, memory protections will be set via a HOB at runtime. Signed-off-by: Taylor Beebe Cc: Jian J Wang Cc: Liming Gao --- MdeModulePkg/Include/Guid/MemoryProtectionSettings.h | 216 ++++++++++++++++++++ MdeModulePkg/MdeModulePkg.dec | 5 + 2 files changed, 221 insertions(+) diff --git a/MdeModulePkg/Include/Guid/MemoryProtectionSettings.h b/MdeModulePkg/Include/Guid/MemoryProtectionSettings.h new file mode 100644 index 000000000000..889e87011fbf --- /dev/null +++ b/MdeModulePkg/Include/Guid/MemoryProtectionSettings.h @@ -0,0 +1,216 @@ +/** @file +Defines memory protection settings guid and struct for DXE and MM. + +Copyright (C) Microsoft Corporation. All rights reserved. +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef MEMORY_PROTECTION_SETTINGS_H_ +#define MEMORY_PROTECTION_SETTINGS_H_ + +#define OEM_RESERVED_MPS_MEMORY_TYPE EfiMaxMemoryType +#define OS_RESERVED_MPS_MEMORY_TYPE (EfiMaxMemoryType + 1) +#define MAX_MPS_MEMORY_TYPE (EfiMaxMemoryType + 2) +#define MPS_MEMORY_TYPE_BUFFER_SIZE (MAX_MPS_MEMORY_TYPE * sizeof (BOOLEAN)) + +// Current DXE iteration of MEMORY_PROTECTION_SETTINGS +#define DXE_MEMORY_PROTECTION_SETTINGS_CURRENT_VERSION 1 + +// Current MM iteration of MEMORY_PROTECTION_SETTINGS +#define MM_MEMORY_PROTECTION_SETTINGS_CURRENT_VERSION 1 + +#define DXE_MEMORY_PROTECTION_SIGNATURE SIGNATURE_32('D', 'M', 'P', 'S') +#define MM_MEMORY_PROTECTION_SIGNATURE SIGNATURE_32('M', 'M', 'P', 'S') + +typedef UINT8 MEMORY_PROTECTION_SETTINGS_VERSION; +typedef UINT32 MEMORY_PROTECTION_SETTINGS_SIGNATURE; + +typedef struct { + BOOLEAN Enabled : 1; + BOOLEAN DisableEndOfDxe : 1; + BOOLEAN NonstopModeEnabled : 1; +} DXE_NULL_DETECTION_POLICY; + +typedef struct { + BOOLEAN ProtectImageFromUnknown : 1; + BOOLEAN ProtectImageFromFv : 1; +} DXE_IMAGE_PROTECTION_POLICY; + +typedef struct { + BOOLEAN PageGuardEnabled : 1; + BOOLEAN PoolGuardEnabled : 1; + BOOLEAN FreedMemoryGuardEnabled : 1; + BOOLEAN NonstopModeEnabled : 1; + BOOLEAN GuardAlignedToTail : 1; +} DXE_HEAP_GUARD_POLICY; + +typedef struct { + BOOLEAN Enabled : 1; + BOOLEAN NonstopModeEnabled : 1; +} MM_NULL_DETECTION_POLICY; + +typedef struct { + BOOLEAN PageGuardEnabled : 1; + BOOLEAN PoolGuardEnabled : 1; + BOOLEAN NonstopModeEnabled : 1; + BOOLEAN GuardAlignedToTail : 1; +} MM_HEAP_GUARD_POLICY; + +typedef struct { + BOOLEAN EnabledForType[MAX_MPS_MEMORY_TYPE]; +} MPS_MEMORY_TYPES; + +// +// Memory Protection Settings struct +// +typedef struct { + // This signature is used to identify the memory protection settings structure. + MEMORY_PROTECTION_SETTINGS_SIGNATURE Signature; + + // The current version of the structure definition. This is used to ensure there isn't a + // definition mismatch if modules have differing iterations of this header. When creating + // this struct, use the DXE_MEMORY_PROTECTION_SETTINGS_CURRENT_VERSION macro. + MEMORY_PROTECTION_SETTINGS_VERSION StructVersion; + + // If enabled, the page at the top of the stack will be invalidated to catch stack overflow. + BOOLEAN CpuStackGuardEnabled; + + // If enabled, the stack will be marked non-executable. + BOOLEAN StackExecutionProtectionEnabled; + + // If enabled, accessing the NULL address in UEFI will be caught by marking + // the NULL page as not present. + // .NullDetectionEnabled : Enable NULL pointer detection. + // .DisableEndOfDxe : Disable NULL pointer detection just after EndOfDxe. + // This is a workaround for those unsolvable NULL access issues in + // OptionROM, boot loader, etc. It can also help to avoid unnecessary + // exception caused by legacy memory (0-4095) access after EndOfDxe, + // such as Windows 7 boot on Qemu. + // .NonstopModeEnabled : If enabled the debug flag will be raised when a fault occurs + // to break into debugger. + DXE_NULL_DETECTION_POLICY NullPointerDetection; + + // Set image protection policy. + // + // .ProtectImageFromUnknown : If set, images from unknown devices will be protected by + // DxeCore if they are aligned. The code section becomes + // read-only, and the data section becomes non-executable. + // .ProtectImageFromFv : If set, images from firmware volumes will be protected by + // DxeCore if they are aligned. The code section becomes + // read-only, and the data section becomes non-executable. + DXE_IMAGE_PROTECTION_POLICY ImageProtection; + + // If a bit is set, memory regions of the associated type will be mapped non-executable. + // + // The execution protection setting for EfiBootServicesData and EfiConventionalMemory must + // be the same. + MPS_MEMORY_TYPES ExecutionProtection; + + // Configures general heap guard behavior. + // + // .PageGuardEnabled : Enable page guard. + // .PoolGuardEnabled : Enable pool guard. + // .FreedMemoryGuardEnabled : Enable freed-memory guard (Use-After-Free memory detection). + // .NonstopModeEnabled : If enabled the debug flag will be raised when a fault occurs + // to break into debugger. + // .GuardAlignedToTail : TRUE if the pool is aligned to tail guard page. If FALSE, the + // pool is aligned to head guard page. + // + // Note: + // a) Due to the limit of pool memory implementation and the alignment + // requirement of UEFI spec, HeapGuard.GuardAlignedToTail is a try-best + // setting which cannot guarantee that the returned pool is exactly + // adjacent to head or tail guard page. + // b) Freed-memory guard and pool/page guard cannot be enabled + // at the same time. + DXE_HEAP_GUARD_POLICY HeapGuard; + + // Indicates which type allocation need guard page. + // + // If bit is set, a head guard page and a tail guard page will be added just + // before and after corresponding type of pages which the allocated pool occupies, + // if there's enough free memory for all of them. + // + // These settings are only valid if HeapGuard.PoolGuardEnabled is TRUE. + MPS_MEMORY_TYPES PoolGuard; + + // Indicates which type allocation need guard page. + // + // If a bit is set, a head guard page and a tail guard page will be added just + // before and after corresponding type of pages allocated if there's enough + // free pages for all of them. + // + // These settings are only valid if HeapGuard.PageGuardEnabled is TRUE. + MPS_MEMORY_TYPES PageGuard; +} DXE_MEMORY_PROTECTION_SETTINGS; + +// +// Memory Protection Settings struct +// +typedef struct { + // This signature is used to identify the memory protection settings structure. + MEMORY_PROTECTION_SETTINGS_SIGNATURE Signature; + + // The current version of the structure definition. This is used to ensure there isn't a + // definition mismatch if modules have differing iterations of this header. When creating + // this struct, use the MM_MEMORY_PROTECTION_SETTINGS_CURRENT_VERSION macro. + MEMORY_PROTECTION_SETTINGS_VERSION StructVersion; + + // If enabled, accessing the NULL address in MM will be caught by marking + // the NULL page as not present. + // .NullDetectionEnabled : Enable NULL pointer detection. + // .NonstopModeEnabled : If enabled the debug flag will be raised when a fault occurs + // to break into debugger. + MM_NULL_DETECTION_POLICY NullPointerDetection; + + // Configures general heap guard behavior. + // + // Note: + // a) Due to the limit of pool memory implementation and the alignment + // requirement of UEFI spec, HeapGuard.GuardAlignedToTail is a try-best + // setting which cannot guarantee that the returned pool is exactly + // adjacent to head or tail guard page. + // + // .PageGuardEnabled : Enable page guard. + // .PoolGuardEnabled : Enable pool guard. + // .NonstopModeEnabled : If enabled the debug flag will be raised when a fault occurs + // to break into debugger. + // .GuardAlignedToTail : TRUE if the pool is aligned to tail guard page. If FALSE, the + // pool is aligned to head guard page. + MM_HEAP_GUARD_POLICY HeapGuard; + + // Indicates which type allocation need guard page. + // + // If bit is set, a head guard page and a tail guard page will be added just + // before and after corresponding type of pages which the allocated pool occupies, + // if there's enough free memory for all of them. + // + // These settings are only valid if PoolGuardEnabled is TRUE in HeapGuard. + MPS_MEMORY_TYPES PoolGuard; + + // Indicates which type allocation need guard page. + // + // If a bit is set, a head guard page and a tail guard page will be added just + // before and after corresponding type of pages allocated if there's enough + // free pages for all of them. + // + // This bitfield is only valid if PageGuardEnabled is TRUE in HeapGuard. + MPS_MEMORY_TYPES PageGuard; +} MM_MEMORY_PROTECTION_SETTINGS; + +typedef struct { + // The memory protection settings in the SMM and Standalone MM environment + MM_MEMORY_PROTECTION_SETTINGS Mm; + // The memory protection settings in the DXE environment + DXE_MEMORY_PROTECTION_SETTINGS Dxe; +} MEMORY_PROTECTION_SETTINGS; + +#define MEMORY_PROTECTION_SETTINGS_GUID \ + { \ + { 0x9ABFD639, 0xD1D0, 0x4EFF, { 0xBD, 0xB6, 0x7E, 0xC4, 0x19, 0x0D, 0x17, 0xD5 } } \ + } + +extern GUID gMemoryProtectionSettingsGuid; + +#endif diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index dd182c02fdf6..5e1a0388bed3 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -399,6 +399,11 @@ [Guids] ## Include/Guid/EndofS3Resume.h gEdkiiEndOfS3ResumeGuid = { 0x96f5296d, 0x05f7, 0x4f3c, {0x84, 0x67, 0xe4, 0x56, 0x89, 0x0e, 0x0c, 0xb5 } } + ## Memory Protection Settings Guid. Used to create and fetch the memory protection settings HOB entry. + # + # Include/Guid/MemoryProtectionSettings + gMemoryProtectionSettingsGuid = { 0x9ABFD639, 0xD1D0, 0x4EFF, { 0xBD, 0xB6, 0x7E, 0xC4, 0x19, 0x0D, 0x17, 0xD5 }} + ## Used (similar to Variable Services) to communicate policies to the enforcement engine. # {DA1B0D11-D1A7-46C4-9DC9-F3714875C6EB} gVarCheckPolicyLibMmiHandlerGuid = { 0xda1b0d11, 0xd1a7, 0x46c4, { 0x9d, 0xc9, 0xf3, 0x71, 0x48, 0x75, 0xc6, 0xeb }} -- 2.42.0.windows.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#109406): https://edk2.groups.io/g/devel/message/109406 Mute This Topic: https://groups.io/mt/101843341/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-