audk/MdeModulePkg/Core/Dxe
Oliver Smith-Denny 00b51e0d78 MdeModulePkg: HeapGuard: Don't Assume Pool Head Allocated In First Page
Currently, HeapGuard, when in the GuardAlignedToTail mode, assumes that
the pool head has been allocated in the first page of memory that was
allocated. This is not the case for ARM64 platforms when allocating
runtime pools, as RUNTIME_PAGE_ALLOCATION_GRANULARITY is 64k, unlike
X64, which has RUNTIME_PAGE_ALLOCATION_GRANULARITY as 4k.

When a runtime pool is allocated on ARM64, the minimum number of pages
allocated is 16, to match the runtime granularity. When a small pool is
allocated and GuardAlignedToTail is true, HeapGuard instructs the pool
head to be placed as (MemoryAllocated + EFI_PAGES_TO_SIZE(Number of Pages)
- SizeRequiredForPool).

This gives this scenario:

|Head Guard|Large Free Number of Pages|PoolHead|TailGuard|

When this pool goes to be freed, HeapGuard instructs the pool code to
free from (PoolHead & ~EFI_PAGE_MASK). However, this assumes that the
PoolHead is in the first page allocated, which as shown above is not true
in this case. For the 4k granularity case (i.e. where the correct number of
pages are allocated for this pool), this logic does work.

In this failing case, HeapGuard then instructs the pool code to free 16
(or more depending) pages from the page the pool head was allocated on,
which as seen above means we overrun the pool and attempt to free memory
far past the pool. We end up running into the tail guard and getting an
access flag fault.

This causes ArmVirtQemu to fail to boot with an access flag fault when
GuardAlignedToTail is set to true (and pool guard enabled for runtime
memory). It should also cause all ARM64 platforms to fail in this
configuration, for exactly the same reason, as this is core code making
the assumption.

This patch removes HeapGuard's assumption that the pool head is allocated
on the first page and instead undoes the same logic that HeapGuard did
when allocating the pool head in the first place.

With this patch in place, ArmVirtQemu boots with GuardAlignedToTail
set to true (and when it is false, also).

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4521
Github PR: https://github.com/tianocore/edk2/pull/4731

Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Dandan Bi <dandan.bi@intel.com>

Signed-off-by: Oliver Smith-Denny <osde@linux.microsoft.com>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Leif Lindholm <quic_llindhol@quicinc.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2023-08-19 03:18:50 +00:00
..
Dispatcher MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
DxeMain MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
Event MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
FwVol MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
FwVolBlock MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
Gcd MdeModulePkg: Update Dxe to handle unaccepted memory type 2022-11-01 10:08:10 +00:00
Hand MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
Image MdeModulePkg: Enable forward edge CFI in mem attributes table 2023-04-07 13:18:38 +00:00
Library MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
Mem MdeModulePkg: HeapGuard: Don't Assume Pool Head Allocated In First Page 2023-08-19 03:18:50 +00:00
Misc MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
SectionExtraction MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
DxeCore.uni MdeModulePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:08 -07:00
DxeCoreExtra.uni MdeModulePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:08 -07:00
DxeMain.h MdeModulePkg: Enable forward edge CFI in mem attributes table 2023-04-07 13:18:38 +00:00
DxeMain.inf MdeModulePkg: Notify BeforeExitBootServices in CoreExitBootServices 2023-01-12 16:03:30 +00:00