Skip to content

Commit

Permalink
MdeModulePkg: Added PROTECT_IF_ALIGNED_ELSE_DISALLOW option for PcdIm…
Browse files Browse the repository at this point in the history
…ageProtectionPolicy.
  • Loading branch information
Mikhail Krichanov committed Nov 6, 2023
1 parent 3dff9d7 commit 7fbdeda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion MdeModulePkg/Core/Dxe/DxeMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFwVolDxeMaxEncapsulationDepth ## CONSUMES

gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask ## CONSUMES

# [Hob]
# RESOURCE_DESCRIPTOR ## CONSUMES
# MEMORY_ALLOCATION ## CONSUMES
Expand Down
36 changes: 24 additions & 12 deletions MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
//
// Protection policy bit definition
//
#define DO_NOT_PROTECT 0x00000000
#define PROTECT_IF_ALIGNED_ELSE_ALLOW 0x00000001
#define DO_NOT_PROTECT 0x00000000
#define PROTECT_IF_ALIGNED_ELSE_ALLOW 0x00000001
#define PROTECT_IF_ALIGNED_ELSE_DISALLOW 0x00000002

#define MEMORY_TYPE_OS_RESERVED_MIN 0x80000000
#define MEMORY_TYPE_OEM_RESERVED_MIN 0x70000000
Expand Down Expand Up @@ -78,11 +79,15 @@ GetProtectionPolicyFromImageType (
{
ASSERT (ImageIsFromFv == FALSE || ImageIsFromFv == TRUE);

if ((mImageProtectionPolicy & 4U) != 0) {
return PROTECT_IF_ALIGNED_ELSE_DISALLOW;
}

if (((ImageIsFromFv + 1) & mImageProtectionPolicy) == 0) {
return DO_NOT_PROTECT;
} else {
return PROTECT_IF_ALIGNED_ELSE_ALLOW;
}

return PROTECT_IF_ALIGNED_ELSE_ALLOW;
}

/**
Expand Down Expand Up @@ -233,13 +238,13 @@ ProtectUefiImage (
UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
)
{
RETURN_STATUS PdbStatus;
UINT32 SectionAlignment;
UEFI_IMAGE_RECORD *ImageRecord;
CONST CHAR8 *PdbPointer;
UINT32 PdbSize;
BOOLEAN IsAligned;
UINT32 ProtectionPolicy;
RETURN_STATUS PdbStatus;
UINT32 SectionAlignment;
UEFI_IMAGE_RECORD *ImageRecord;
CONST CHAR8 *PdbPointer;
UINT32 PdbSize;
BOOLEAN IsAligned;
UINT32 ProtectionPolicy;

DEBUG ((DEBUG_INFO, "ProtectUefiImageCommon - 0x%x\n", LoadedImage));
DEBUG ((DEBUG_INFO, " - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)LoadedImage->ImageBase, LoadedImage->ImageSize));
Expand All @@ -250,8 +255,10 @@ ProtectUefiImage (
return;
case PROTECT_IF_ALIGNED_ELSE_ALLOW:
break;
case PROTECT_IF_ALIGNED_ELSE_DISALLOW:
break;
default:
ASSERT (FALSE);
DEBUG_RAISE ();
return;
}

Expand All @@ -275,6 +282,11 @@ ProtectUefiImage (
DEBUG ((DEBUG_VERBOSE, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer));
}

if (ProtectionPolicy == PROTECT_IF_ALIGNED_ELSE_DISALLOW) {
DEBUG_RAISE ();
return;
}

goto Finish;
}

Expand Down
6 changes: 4 additions & 2 deletions MdeModulePkg/MdeModulePkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -1367,11 +1367,13 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfileDriverPath|{0x0}|VOID*|0x00001043

## Set image protection policy. The policy is bitwise.
# If a bit is set, the image will be protected by DxeCore if it is aligned.
# If a bit is set, the image will be protected by DxeCore if it is aligned,
# The code section becomes read-only, and the data section becomes non-executable.
# If a bit is clear, nothing will be done to image code/data sections.<BR><BR>
# If a bit is clear, nothing will be done to image code/data sections.
# If BIT2 is set unaligned images will stop boot process regardless of their origin.<BR><BR>
# BIT0 - Image from unknown device. <BR>
# BIT1 - Image from firmware volume.<BR>
# BIT2 - Forbid loading of unaligned images.<BR>
# <BR>
# Note: If a bit is cleared, the data section could be still non-executable if
# PcdDxeNxMemoryProtectionPolicy is enabled for EfiLoaderData, EfiBootServicesData
Expand Down

0 comments on commit 7fbdeda

Please sign in to comment.