From 928716f422a3164a68d26d2b05d3a84d543a6f72 Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Mon, 25 Dec 2023 15:36:55 +0300 Subject: [PATCH] Ring3: Added EFI_MEMORY_USER attribute. --- MdeModulePkg/Core/Dxe/DxeMain.h | 14 +++++++++++++ MdeModulePkg/Core/Dxe/Image/Image.c | 4 ++++ MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c | 2 +- MdePkg/Include/Uefi/UefiSpec.h | 8 ++++++- UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c | 21 +++++++++++++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h index 679dd67bf6..35538445be 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -2791,4 +2791,18 @@ MergeMemoryMap ( IN UINTN DescriptorSize ); +/** + Set UEFI image memory attributes. + + @param[in] BaseAddress Specified start address + @param[in] Length Specified length + @param[in] Attributes Specified attributes +**/ +VOID +SetUefiImageMemoryAttributes ( + IN UINT64 BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes + ); + #endif diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index 8afc6a8d30..5ae7c1c7e0 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -812,6 +812,10 @@ CoreLoadPeImage ( // if (!EFI_ERROR (Status)) { DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); + + if (AsciiStrStr (EfiFileName, "Ext4") != NULL) { + SetUefiImageMemoryAttributes (BufferAddress, EFI_PAGES_TO_SIZE (DstBufPages), EFI_MEMORY_USER); + } } DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n")); diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c index 0a946ae535..e797697df7 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -78,7 +78,7 @@ SetUefiImageMemoryAttributes ( Status = CoreGetMemorySpaceDescriptor (BaseAddress, &Descriptor); ASSERT_EFI_ERROR (Status); - FinalAttributes = (Descriptor.Attributes & EFI_CACHE_ATTRIBUTE_MASK) | (Attributes & EFI_MEMORY_ATTRIBUTE_MASK); + FinalAttributes = (Descriptor.Attributes & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_USER)) | (Attributes & EFI_MEMORY_ATTRIBUTE_MASK); DEBUG ((DEBUG_INFO, "SetUefiImageMemoryAttributes - 0x%016lx - 0x%016lx (0x%016lx)\n", BaseAddress, Length, FinalAttributes)); diff --git a/MdePkg/Include/Uefi/UefiSpec.h b/MdePkg/Include/Uefi/UefiSpec.h index 7dfe35b499..97bfc6fb2c 100644 --- a/MdePkg/Include/Uefi/UefiSpec.h +++ b/MdePkg/Include/Uefi/UefiSpec.h @@ -105,6 +105,12 @@ typedef enum { // #define EFI_MEMORY_CPU_CRYPTO 0x0000000000080000ULL +// +// If this flag is set, the memory region contains user code or data. +// If this flag is clear, the memory region contains supervisor code or data. +// +#define EFI_MEMORY_USER 0x0000000000100000ULL + // // Runtime memory attribute // @@ -115,7 +121,7 @@ typedef enum { // #define EFI_CACHE_ATTRIBUTE_MASK (EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_WP) #define EFI_MEMORY_ACCESS_MASK (EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_RO) -#define EFI_MEMORY_ATTRIBUTE_MASK (EFI_MEMORY_ACCESS_MASK | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO) +#define EFI_MEMORY_ATTRIBUTE_MASK (EFI_MEMORY_ACCESS_MASK | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_USER) /// /// Memory descriptor version number. diff --git a/UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c b/UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c index ad2c79cd64..fb684de8a1 100644 --- a/UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c +++ b/UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c @@ -467,6 +467,27 @@ ConvertPageEntryAttribute ( } } + if ((Attributes & EFI_MEMORY_USER) != 0) { + switch (PageAction) { + case PageActionAssign: + case PageActionSet: + NewPageEntry |= IA32_PG_U; + break; + case PageActionClear: + NewPageEntry &= ~(UINT64)IA32_PG_U; + break; + } + } else { + switch (PageAction) { + case PageActionAssign: + NewPageEntry &= ~(UINT64)IA32_PG_U; + break; + case PageActionSet: + case PageActionClear: + break; + } + } + GetPagingDetails (&PagingContext->ContextData, NULL, &PageAttributes); if ((*PageAttributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED) != 0) {