Skip to content

Commit

Permalink
StandaloneMmPkg/Core: Limit FwVol encapsulation section recursion
Browse files Browse the repository at this point in the history
MmCoreFfsFindMmDriver() is called recursively for encapsulation sections.
Currently this recursion is not limited. Introduce a new PCD
(fixed-at-build, or patchable-in-module), and make MmCoreFfsFindMmDriver()
track the section nesting depth against that PCD.

Cc: Laszlo Ersek <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Sami Mujawar <[email protected]>
Cc: Ray Ni <[email protected]>
Signed-off-by: Wei6 Xu <[email protected]>
Reviewed-by: Ray Ni <[email protected]>
Reviewed-by: Laszlo Ersek <[email protected]>
  • Loading branch information
xuweiintel authored and mergify[bot] committed Dec 19, 2023
1 parent 74daede commit c012284
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
5 changes: 0 additions & 5 deletions StandaloneMmPkg/Core/Dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ typedef struct {
// Function Prototypes
//

EFI_STATUS
MmCoreFfsFindMmDriver (
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
);

/**
Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
must add any driver with a before dependency on InsertedDriverEntry first.
Expand Down
16 changes: 14 additions & 2 deletions StandaloneMmPkg/Core/FwVol.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ FvIsBeingProcessed (
MM driver and return its PE32 image.
@param [in] FwVolHeader Pointer to memory mapped FV
@param [in] Depth Nesting depth of encapsulation sections. Callers
different from MmCoreFfsFindMmDriver() are
responsible for passing in a zero Depth.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_NOT_FOUND Could not find section data.
@retval EFI_OUT_OF_RESOURCES Out of resources.
@retval EFI_VOLUME_CORRUPTED Firmware volume is corrupted.
@retval EFI_UNSUPPORTED Operation not supported.
@retval EFI_ABORTED Recursion aborted because Depth has been
greater than or equal to
PcdFwVolMmMaxEncapsulationDepth.
**/
EFI_STATUS
MmCoreFfsFindMmDriver (
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
IN UINT32 Depth
)
{
EFI_STATUS Status;
Expand All @@ -84,6 +91,11 @@ MmCoreFfsFindMmDriver (

DEBUG ((DEBUG_INFO, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader));

if (Depth >= PcdGet32 (PcdFwVolMmMaxEncapsulationDepth)) {
DEBUG ((DEBUG_ERROR, "%a: recursion aborted due to nesting depth\n", __func__));
return EFI_ABORTED;
}

if (FvHasBeenProcessed (FwVolHeader)) {
return EFI_SUCCESS;
}
Expand Down Expand Up @@ -172,7 +184,7 @@ MmCoreFfsFindMmDriver (
}

InnerFvHeader = (VOID *)(Section + 1);
Status = MmCoreFfsFindMmDriver (InnerFvHeader);
Status = MmCoreFfsFindMmDriver (InnerFvHeader, Depth + 1);
if (EFI_ERROR (Status)) {
goto FreeDstBuffer;
}
Expand Down
7 changes: 1 addition & 6 deletions StandaloneMmPkg/Core/StandaloneMmCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

#include "StandaloneMmCore.h"

EFI_STATUS
MmCoreFfsFindMmDriver (
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
);

EFI_STATUS
MmDispatcher (
VOID
Expand Down Expand Up @@ -643,7 +638,7 @@ StandaloneMmMain (
//
DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate->StandaloneBfvAddress));
if (gMmCorePrivate->StandaloneBfvAddress != 0) {
MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress);
MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress, 0);
MmDispatcher ();
}

Expand Down
26 changes: 26 additions & 0 deletions StandaloneMmPkg/Core/StandaloneMmCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,32 @@ DumpMmramInfo (
VOID
);

/**
Given the pointer to the Firmware Volume Header find the
MM driver and return its PE32 image.
@param [in] FwVolHeader Pointer to memory mapped FV
@param [in] Depth Nesting depth of encapsulation sections. Callers
different from MmCoreFfsFindMmDriver() are
responsible for passing in a zero Depth.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_NOT_FOUND Could not find section data.
@retval EFI_OUT_OF_RESOURCES Out of resources.
@retval EFI_VOLUME_CORRUPTED Firmware volume is corrupted.
@retval EFI_UNSUPPORTED Operation not supported.
@retval EFI_ABORTED Recursion aborted because Depth has been
greater than or equal to
PcdFwVolMmMaxEncapsulationDepth.
**/
EFI_STATUS
MmCoreFfsFindMmDriver (
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
IN UINT32 Depth
);

extern UINTN mMmramRangeCount;
extern EFI_MMRAM_DESCRIPTOR *mMmramRanges;
extern EFI_SYSTEM_TABLE *mEfiSystemTable;
Expand Down
3 changes: 3 additions & 0 deletions StandaloneMmPkg/Core/StandaloneMmCore.inf
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
gEfiEventExitBootServicesGuid
gEfiEventReadyToBootGuid

[Pcd]
gStandaloneMmPkgTokenSpaceGuid.PcdFwVolMmMaxEncapsulationDepth ##CONSUMES

#
# This configuration fails for CLANGPDB, which does not support PIE in the GCC
# sense. Such however is required for ARM family StandaloneMmCore
Expand Down
5 changes: 5 additions & 0 deletions StandaloneMmPkg/StandaloneMmPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@
gEfiStandaloneMmNonSecureBufferGuid = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
gEfiArmTfCpuDriverEpDescriptorGuid = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}

[PcdsFixedAtBuild, PcdsPatchableInModule]
## Maximum permitted encapsulation levels of sections in a firmware volume,
# in the MM phase. Minimum value is 1. Sections nested more deeply are rejected.
# @Prompt Maximum permitted FwVol section nesting depth (exclusive) in MM.
gStandaloneMmPkgTokenSpaceGuid.PcdFwVolMmMaxEncapsulationDepth|0x10|UINT32|0x00000001

0 comments on commit c012284

Please sign in to comment.