TianoCore EDK2 master
|
Go to the source code of this file.
Data Structures | |
struct | HEAP_GUARD_NODE |
Macros | |
#define | BYTE_LENGTH_SHIFT 3 |
#define | GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT (EFI_PAGE_SHIFT - BYTE_LENGTH_SHIFT) |
#define | GUARDED_HEAP_MAP_TABLE_DEPTH 5 |
#define | GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT 6 |
#define | GUARDED_HEAP_MAP_ENTRY_BITS (1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT) |
#define | GUARDED_HEAP_MAP_ENTRY_BYTES (GUARDED_HEAP_MAP_ENTRY_BITS / 8) |
#define | GUARDED_HEAP_MAP_ENTRY_SHIFT |
#define | GUARDED_HEAP_MAP_ENTRY_MASK ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1) |
#define | GUARDED_HEAP_MAP_SIZE ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) * GUARDED_HEAP_MAP_ENTRY_BYTES) |
#define | GUARDED_HEAP_MAP_UNIT_SIZE (GUARDED_HEAP_MAP_SIZE * 8 * EFI_PAGE_SIZE) |
#define | GUARDED_HEAP_MAP_ENTRIES_PER_UNIT (GUARDED_HEAP_MAP_SIZE / GUARDED_HEAP_MAP_ENTRY_BYTES) |
#define | GUARDED_HEAP_MAP_ENTRY_INDEX(Address) |
#define | GUARDED_HEAP_MAP_ENTRY_BIT_INDEX(Address) |
#define | GUARDED_HEAP_MAP_BITS |
#define | GUARDED_HEAP_MAP_BIT_INDEX(Address) |
#define | GUARDED_HEAP_MAP_TABLE_SHIFT |
#define | GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS |
#define | GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS |
#define | GUARD_HEAP_TYPE_PAGE BIT0 |
#define | GUARD_HEAP_TYPE_POOL BIT1 |
#define | GUARD_HEAP_TYPE_FREED BIT4 |
#define | GUARD_HEAP_TYPE_ALL (GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED) |
#define | HEAP_GUARD_DEBUG_LEVEL (DEBUG_POOL|DEBUG_PAGE) |
Functions | |
EFI_STATUS | CoreConvertPages (IN UINT64 Start, IN UINT64 NumberOfPages, IN EFI_MEMORY_TYPE NewType) |
EFI_STATUS | CoreConvertPagesWithGuard (IN UINT64 Start, IN UINTN NumberOfPages, IN EFI_MEMORY_TYPE NewType) |
VOID | SetGuardForMemory (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages) |
VOID | UnsetGuardForMemory (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages) |
VOID | AdjustMemoryA (IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN OUT UINTN *NumberOfPages) |
VOID | AdjustMemoryF (IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN OUT UINTN *NumberOfPages) |
UINT64 | AdjustMemoryS (IN UINT64 Start, IN UINT64 Size, IN UINT64 SizeRequested) |
BOOLEAN | IsPoolTypeToGuard (IN EFI_MEMORY_TYPE MemoryType) |
BOOLEAN | IsPageTypeToGuard (IN EFI_MEMORY_TYPE MemoryType, IN EFI_ALLOCATE_TYPE AllocateType) |
BOOLEAN EFIAPI | IsMemoryGuarded (IN EFI_PHYSICAL_ADDRESS Address) |
BOOLEAN EFIAPI | IsGuardPage (IN EFI_PHYSICAL_ADDRESS Address) |
VOID EFIAPI | DumpGuardedMemoryBitmap (VOID) |
VOID * | AdjustPoolHeadA (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NoPages, IN UINTN Size) |
VOID * | AdjustPoolHeadF (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NoPages, IN UINTN Size) |
BOOLEAN | IsHeapGuardEnabled (UINT8 GuardType) |
VOID | HeapGuardCpuArchProtocolNotify (VOID) |
VOID | MergeGuardPages (IN EFI_MEMORY_DESCRIPTOR *MemoryMapEntry, IN EFI_PHYSICAL_ADDRESS MaxAddress) |
VOID EFIAPI | GuardFreedPagesChecked (IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINTN Pages) |
BOOLEAN | PromoteGuardedFreePages (OUT EFI_PHYSICAL_ADDRESS *StartAddress, OUT EFI_PHYSICAL_ADDRESS *EndAddress) |
STATIC_ASSERT (RUNTIME_PAGE_ALLOCATION_GRANULARITY==EFI_PAGE_SIZE||(((FixedPcdGet64(PcdHeapGuardPageType) &0x461)==0) &&((FixedPcdGet64(PcdHeapGuardPoolType) &0x461)==0)), "Unsupported Heap Guard configuration on system with greater than EFI_PAGE_SIZE RUNTIME_PAGE_ALLOCATION_GRANULARITY") | |
Variables | |
BOOLEAN | mOnGuarding |
Data type, macros and function prototypes of heap guard feature.
Copyright (c) 2017-2018, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
Definition in file HeapGuard.h.
#define BYTE_LENGTH_SHIFT 3 |
Definition at line 54 of file HeapGuard.h.
#define GUARD_HEAP_TYPE_ALL (GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED) |
Definition at line 158 of file HeapGuard.h.
#define GUARD_HEAP_TYPE_FREED BIT4 |
Definition at line 157 of file HeapGuard.h.
#define GUARD_HEAP_TYPE_PAGE BIT0 |
Definition at line 155 of file HeapGuard.h.
#define GUARD_HEAP_TYPE_POOL BIT1 |
Definition at line 156 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_BIT_INDEX | ( | Address | ) |
Definition at line 114 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_BITS |
Definition at line 107 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRIES_PER_UNIT (GUARDED_HEAP_MAP_SIZE / GUARDED_HEAP_MAP_ENTRY_BYTES) |
Definition at line 90 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_BIT_INDEX | ( | Address | ) |
Definition at line 100 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT 6 |
Definition at line 62 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_BITS (1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT) |
Definition at line 64 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_BYTES (GUARDED_HEAP_MAP_ENTRY_BITS / 8) |
Definition at line 67 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_INDEX | ( | Address | ) |
Definition at line 94 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_MASK ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1) |
Definition at line 78 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_ENTRY_SHIFT |
Definition at line 71 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_SIZE ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) * GUARDED_HEAP_MAP_ENTRY_BYTES) |
Definition at line 82 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_TABLE_DEPTH 5 |
Definition at line 59 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS |
Definition at line 143 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS |
Definition at line 130 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT (EFI_PAGE_SHIFT - BYTE_LENGTH_SHIFT) |
Definition at line 56 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_TABLE_SHIFT |
Definition at line 122 of file HeapGuard.h.
#define GUARDED_HEAP_MAP_UNIT_SIZE (GUARDED_HEAP_MAP_SIZE * 8 * EFI_PAGE_SIZE) |
Definition at line 86 of file HeapGuard.h.
#define HEAP_GUARD_DEBUG_LEVEL (DEBUG_POOL|DEBUG_PAGE) |
Definition at line 164 of file HeapGuard.h.
Adjust the base and number of pages to really allocate according to Guard.
[in,out] | Memory | Base address of free memory. |
[in,out] | NumberOfPages | Size of memory to allocate. |
Definition at line 982 of file HeapGuard.c.
Adjust the start address and number of pages to free according to Guard.
The purpose of this function is to keep the shared Guard page with adjacent memory block if it's still in guard, or free it if no more sharing. Another is to reserve pages as Guard pages in partial page free situation.
[in,out] | Memory | Base address of memory to free. |
[in,out] | NumberOfPages | Size of memory to free. |
Definition at line 884 of file HeapGuard.c.
Adjust address of free memory according to existing and/or required Guard.
This function will check if there're existing Guard pages of adjacent memory blocks, and try to use it as the Guard page of the memory to be allocated.
[in] | Start | Start address of free memory block. |
[in] | Size | Size of free memory block. |
[in] | SizeRequested | Size of memory to allocate. |
Definition at line 825 of file HeapGuard.c.
Adjust the pool head position to make sure the Guard page is adjavent to pool tail or pool head.
[in] | Memory | Base address of memory allocated. |
[in] | NoPages | Number of pages actually allocated. |
[in] | Size | Size of memory requested. (plus pool head/tail overhead) |
Adjust the pool head position to make sure the Guard page is adjavent to pool tail or pool head.
[in] | Memory | Base address of memory allocated. |
[in] | NoPages | Number of pages actually allocated. |
[in] | Size | Size of memory requested. (plus pool head/tail overhead) |
Definition at line 1016 of file HeapGuard.c.
Get the page base address according to pool head address.
[in] | Memory | Head address of pool to free. |
[in] | NoPages | Number of pages actually allocated. |
[in] | Size | Size of memory requested. (plus pool head/tail overhead) |
Definition at line 1047 of file HeapGuard.c.
EFI_STATUS CoreConvertPages | ( | IN UINT64 | Start, |
IN UINT64 | NumberOfPages, | ||
IN EFI_MEMORY_TYPE | NewType | ||
) |
Internal function. Converts a memory range to the specified type. The range must exist in the memory map.
Start | The first address of the range Must be page aligned. |
NumberOfPages | The number of pages to convert. |
NewType | The new type for the memory range. |
EFI_INVALID_PARAMETER | Invalid parameter. |
EFI_NOT_FOUND | Could not find a descriptor cover the specified range or convertion not allowed. |
EFI_SUCCESS | Successfully converts the memory range to the specified type. |
Internal function. Converts a memory range to the specified type. The range must exist in the memory map.
Start | The first address of the range Must be page aligned |
NumberOfPages | The number of pages to convert |
NewType | The new type for the memory range |
EFI_INVALID_PARAMETER | Invalid parameter |
EFI_NOT_FOUND | Could not find a descriptor cover the specified range or convertion not allowed. |
EFI_SUCCESS | Successfully converts the memory range to the specified type. |
EFI_STATUS CoreConvertPagesWithGuard | ( | IN UINT64 | Start, |
IN UINTN | NumberOfPages, | ||
IN EFI_MEMORY_TYPE | NewType | ||
) |
Allocate or free guarded memory.
[in] | Start | Start address of memory to allocate or free. |
[in] | NumberOfPages | Memory size in pages. |
[in] | NewType | Memory type to convert to. |
Definition at line 1079 of file HeapGuard.c.
VOID EFIAPI DumpGuardedMemoryBitmap | ( | VOID | ) |
Dump the guarded memory bit map.
Definition at line 1659 of file HeapGuard.c.
VOID EFIAPI GuardFreedPagesChecked | ( | IN EFI_PHYSICAL_ADDRESS | BaseAddress, |
IN UINTN | Pages | ||
) |
Record freed pages as well as mark them as not-present, if enabled.
[in] | BaseAddress | Base address of just freed pages. |
[in] | Pages | Number of freed pages. |
Definition at line 1350 of file HeapGuard.c.
VOID HeapGuardCpuArchProtocolNotify | ( | VOID | ) |
Notify function used to set all Guard pages after CPU Arch Protocol installed.
Notify function used to set all Guard pages before CPU Arch Protocol installed.
Definition at line 1604 of file HeapGuard.c.
BOOLEAN EFIAPI IsGuardPage | ( | IN EFI_PHYSICAL_ADDRESS | Address | ) |
Check to see if the page at the given address is a Guard page or not.
[in] | Address | The address to check for. |
Definition at line 461 of file HeapGuard.c.
BOOLEAN IsHeapGuardEnabled | ( | UINT8 | GuardType | ) |
Check to see if the heap guard is enabled for page and/or pool allocation.
[in] | GuardType | Specify the sub-type(s) of Heap Guard. |
Definition at line 674 of file HeapGuard.c.
BOOLEAN EFIAPI IsMemoryGuarded | ( | IN EFI_PHYSICAL_ADDRESS | Address | ) |
Check to see if the page at the given address is guarded or not.
[in] | Address | The address to check for. |
Definition at line 486 of file HeapGuard.c.
BOOLEAN IsPageTypeToGuard | ( | IN EFI_MEMORY_TYPE | MemoryType, |
IN EFI_ALLOCATE_TYPE | AllocateType | ||
) |
Check to see if the page at the given address should be guarded or not.
[in] | MemoryType | Page type to check. |
[in] | AllocateType | Allocation type to check. |
Definition at line 658 of file HeapGuard.c.
BOOLEAN IsPoolTypeToGuard | ( | IN EFI_MEMORY_TYPE | MemoryType | ) |
Check to see if the pool at the given address should be guarded or not.
[in] | MemoryType | Pool type to check. |
Definition at line 637 of file HeapGuard.c.
VOID MergeGuardPages | ( | IN EFI_MEMORY_DESCRIPTOR * | MemoryMapEntry, |
IN EFI_PHYSICAL_ADDRESS | MaxAddress | ||
) |
This function checks to see if the given memory map descriptor in a memory map can be merged with any guarded free pages.
MemoryMapEntry | A pointer to a descriptor in MemoryMap. |
MaxAddress | Maximum address to stop the merge. |
Definition at line 1476 of file HeapGuard.c.
BOOLEAN PromoteGuardedFreePages | ( | OUT EFI_PHYSICAL_ADDRESS * | StartAddress, |
OUT EFI_PHYSICAL_ADDRESS * | EndAddress | ||
) |
Put part (at most 64 pages a time) guarded free pages back to free page pool.
Freed memory guard is used to detect Use-After-Free (UAF) memory issue, which makes use of 'Used then throw away' way to detect any illegal access to freed memory. The thrown-away memory will be marked as not-present so that any access to those memory (after free) will be caught by page-fault exception.
The problem is that this will consume lots of memory space. Once no memory left in pool to allocate, we have to restore part of the freed pages to their normal function. Otherwise the whole system will stop functioning.
StartAddress | Start address of promoted memory. |
EndAddress | End address of promoted memory. |
Definition at line 1531 of file HeapGuard.c.
VOID SetGuardForMemory | ( | IN EFI_PHYSICAL_ADDRESS | Memory, |
IN UINTN | NumberOfPages | ||
) |
Set head Guard and tail Guard for the given memory range.
[in] | Memory | Base address of memory to set guard for. |
[in] | NumberOfPages | Memory size in pages. |
Set head Guard and tail Guard for the given memory range.
[in] | Memory | Base address of memory to set guard for. |
[in] | NumberOfPages | Memory size in pages. |
Definition at line 690 of file HeapGuard.c.
VOID UnsetGuardForMemory | ( | IN EFI_PHYSICAL_ADDRESS | Memory, |
IN UINTN | NumberOfPages | ||
) |
Unset head Guard and tail Guard for the given memory range.
[in] | Memory | Base address of memory to unset guard for. |
[in] | NumberOfPages | Memory size in pages. |
Unset head Guard and tail Guard for the given memory range.
[in] | Memory | Base address of memory to unset guard for. |
[in] | NumberOfPages | Memory size in pages. |
Definition at line 726 of file HeapGuard.c.
|
extern |
Definition at line 17 of file HeapGuard.c.