Skip to content

Commit

Permalink
Ring3: Added EFI_MEMORY_USER attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Jan 9, 2024
1 parent c22e277 commit 81e5198
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ CoreLoadPeImage (
if (DstBufAlocated) {
ZeroMem ((VOID *)(UINTN)BufferAddress, EFI_PAGES_TO_SIZE (Image->NumberOfPages));
FreeAlignedPages ((VOID *)(UINTN)BufferAddress, Image->NumberOfPages);
Image->ImageBasePage = 0;
Image->ImageBasePage = 0;
}

if (RelocationData != NULL) {
Expand Down
13 changes: 9 additions & 4 deletions MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ SetUefiImageMemoryAttributes (
**/
VOID
SetUefiImageProtectionAttributes (
IN UEFI_IMAGE_RECORD *ImageRecord
IN UEFI_IMAGE_RECORD *ImageRecord,
IN BOOLEAN IsUser
)
{
UEFI_IMAGE_RECORD_SEGMENT *ImageRecordSegment;
Expand All @@ -106,7 +107,7 @@ SetUefiImageProtectionAttributes (
SetUefiImageMemoryAttributes (
SectionAddress,
ImageRecordSegment->Size,
ImageRecordSegment->Attributes
IsUser ? ImageRecordSegment->Attributes | (UINT32)EFI_MEMORY_USER : ImageRecordSegment->Attributes
);

SectionAddress += ImageRecordSegment->Size;
Expand Down Expand Up @@ -229,7 +230,11 @@ ProtectUefiImage (
//
// CPU ARCH present. Update memory attribute directly.
//
SetUefiImageProtectionAttributes (ImageRecord);
if (AsciiStrStr (PdbPointer, "Ntfs") != NULL) {
SetUefiImageProtectionAttributes (ImageRecord, TRUE);
} else {
SetUefiImageProtectionAttributes (ImageRecord, FALSE);
}
}

Finish:
Expand Down Expand Up @@ -660,7 +665,7 @@ MemoryProtectionCpuArchProtocolNotify (
//
// CPU ARCH present. Update memory attribute directly.
//
SetUefiImageProtectionAttributes (ImageRecord);
SetUefiImageProtectionAttributes (ImageRecord, FALSE);
}

Done:
Expand Down
8 changes: 7 additions & 1 deletion MdePkg/Include/Uefi/UefiSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 81e5198

Please sign in to comment.