Bitdefender Hypervisor Memory Introspection
winselfmap.h File Reference
#include "guests.h"

Go to the source code of this file.

Macros

#define SELF_MAP_ENTRY(Cr3)   (CLEAN_PHYS_ADDRESS64(((QWORD)(Cr3))) + gGuest.Mm.SelfMapIndex * 8ull)
 Computes the self map entry physical address based on a given Cr3. More...
 
#define SELF_MAP_ENTRY_IS_DETECTION(entry)   (((entry) & PT_P) != 0 && ((entry) & PT_US) != 0)
 Decides if a self map entry value is malicious or not. More...
 
#define SELF_MAP_ENTRY_VA
 Computes the virtual address at which the self map entry is mapped for this guest. More...
 

Typedefs

typedef struct _WIN_PROCESS_OBJECT WIN_PROCESS_OBJECT
 
typedef struct _WIN_PROCESS_OBJECTPWIN_PROCESS_OBJECT
 

Functions

INTSTATUS IntWinSelfMapValidateSelfMapEntries (void)
 Validates the self map entries for every process in the system. More...
 
INTSTATUS IntWinSelfMapUnprotectSelfMapIndex (WIN_PROCESS_OBJECT *Process)
 Removes the EPT protection for the self map entry index of a process. More...
 
INTSTATUS IntWinSelfMapProtectSelfMapIndex (WIN_PROCESS_OBJECT *Process)
 Protects the self map index of a process by placing an EPT write hook on it. More...
 
INTSTATUS IntWinSelfMapDisableSelfMapEntryProtection (void)
 Disables the self map entry protection for all the processes on the system. More...
 
INTSTATUS IntWinSelfMapEnableSelfMapEntryProtection (void)
 Enables the self map protection mechanism for the entire system. More...
 
INTSTATUS IntWinSelfMapGetAndCheckSelfMapEntry (WIN_PROCESS_OBJECT *Process)
 Sets and validates the self map entry values for a process. More...
 

Macro Definition Documentation

◆ SELF_MAP_ENTRY

#define SELF_MAP_ENTRY (   Cr3)    (CLEAN_PHYS_ADDRESS64(((QWORD)(Cr3))) + gGuest.Mm.SelfMapIndex * 8ull)

Computes the self map entry physical address based on a given Cr3.

This is done using the self map index value used by the guest for the self mapping mechanism.

Parameters
[in]Cr3The Cr3 for which the self map entry physical address is calculated
Returns
The physical address of the self map entry for the given Cr3

Definition at line 21 of file winselfmap.h.

Referenced by IntWinSelfMapCheckSelfMapEntry(), IntWinSelfMapGetAndCheckSelfMapEntry(), IntWinSelfMapHandleCr3SelfMapModification(), and IntWinSelfMapProtectSelfMapIndex().

◆ SELF_MAP_ENTRY_IS_DETECTION

#define SELF_MAP_ENTRY_IS_DETECTION (   entry)    (((entry) & PT_P) != 0 && ((entry) & PT_US) != 0)

Decides if a self map entry value is malicious or not.

If the entry is present and has the user/supervisor bit set, it is considered to be malicious, as it can be accessed from user mode code

Parameters
[in]entryThe self map entry value
Return values
Trueif the value of the entry is suspicious
Falseif the value of the entry is not suspicious

Definition at line 34 of file winselfmap.h.

Referenced by IntWinSelfMapCheckSelfMapEntry(), and IntWinSelfMapHandleCr3SelfMapWrite().

◆ SELF_MAP_ENTRY_VA

#define SELF_MAP_ENTRY_VA
Value:
(0xFFFF800000000000 | ((QWORD)gGuest.Mm.SelfMapIndex << 39) | \
unsigned long long QWORD
Definition: intro_types.h:53
DWORD SelfMapIndex
The self map index.
Definition: guests.h:220
MM Mm
Guest memory information, such as paging mode, system Cr3 value, etc.
Definition: guests.h:374
GUEST_STATE gGuest
The current guest state.
Definition: guests.c:50

Computes the virtual address at which the self map entry is mapped for this guest.

This is done using the self map index value used by the guest for the self mapping mechanism

Definition at line 39 of file winselfmap.h.

Referenced by IntWinSelfMapHandleCr3SelfMapModification(), and IntWinSelfMapHandleCr3SelfMapWrite().

Typedef Documentation

◆ PWIN_PROCESS_OBJECT

Definition at line 10 of file winselfmap.h.

◆ WIN_PROCESS_OBJECT

Definition at line 10 of file winselfmap.h.

Function Documentation

◆ IntWinSelfMapDisableSelfMapEntryProtection()

INTSTATUS IntWinSelfMapDisableSelfMapEntryProtection ( void  )

Disables the self map entry protection for all the processes on the system.

This will deactivate protection and will remove any hooks set by IntWinSelfMapProtectSelfMapIndex

Return values
INT_STATUS_SUCCESSin case of success
INT_STATUS_NOT_INITIALIZED_HINTif the guest is not initialized or the protection is not active
INT_STATUS_NOT_NEEDED_HINTif the guest is not Windows, not using 4- or 5-level paging (the other paging modes do not use the self map mechanism)

Definition at line 656 of file winselfmap.c.

Referenced by IntGuestUpdateCoreOptions().

◆ IntWinSelfMapEnableSelfMapEntryProtection()

INTSTATUS IntWinSelfMapEnableSelfMapEntryProtection ( void  )

Enables the self map protection mechanism for the entire system.

It will first check the self map index of every process using IntWinSelfMapCheckSelfMapEntry, then the actual protection will be activated using IntWinSelfMapProtectSelfMapIndex.

Return values
INT_STATUS_SUCCESSin case of success
INT_STATUS_NOT_INITIALIZED_HINTif the guest is not initialized or the protection is not active
INT_STATUS_NOT_NEEDED_HINTif the guest is not Windows, not using 4- or 5-level paging (the other paging modes do not use the self map mechanism)

Definition at line 516 of file winselfmap.c.

Referenced by IntGuestUpdateCoreOptions().

◆ IntWinSelfMapGetAndCheckSelfMapEntry()

INTSTATUS IntWinSelfMapGetAndCheckSelfMapEntry ( WIN_PROCESS_OBJECT Process)

Sets and validates the self map entry values for a process.

If KPTI enabled for Process, this will read and validate both the kernel and the user Cr3. If not, only the kernel Cr3 will be considered, as the user Cr3 will be 0. The values are obtained from the _KPROCESS kernel structure. If the INTRO_OPT_PROT_KM_SELF_MAP_ENTRY option was used, and a malicious change of the self map value is detected, an alert will eventually be sent. If the process is swapped-out, the function does nothing.

Parameters
[in,out]ProcessThe process for which the values are read and validated
Return values
INT_STATUS_SUCCESSin case of success
INT_STATUS_INVALID_PARAMETER_1if Process is NULL
INT_STATUS_NOT_INITIALIZED_HINTif the guest is not initialized or the protection is not active
INT_STATUS_NOT_NEEDED_HINTif the guest is not Windows, not using 4- or 5-level paging (the other paging modes do not use the self map mechanism) or the process is swapped-out.

Definition at line 579 of file winselfmap.c.

Referenced by IntWinProcCreateProcessObject(), and IntWinProcDeleteProcessObject().

◆ IntWinSelfMapProtectSelfMapIndex()

INTSTATUS IntWinSelfMapProtectSelfMapIndex ( WIN_PROCESS_OBJECT Process)

Protects the self map index of a process by placing an EPT write hook on it.

Essentially, this will protect the user/supervisor bit, in order to make sure that user mode code can not access the page tables. Currently, this is enabled only for the system process, as other processes may swap out their Cr3 contents. Processes that have their self map entry protected in this way will have an EPT hook set on the page of their kernel and user Cr3 (if KPTI is enabled). Because of this, care should be taken when activating this for processes, as it may have a negative impact on performance because the kernel may do a lot of writes on those pages. This is manageable for processes that are already protected, as we have other hooks placed on their page tables already. The EPT hook handler used is IntWinSelfMapHandleCr3SelfMapWrite.

Parameters
[in]ProcessThe process for which the protection is activated
Return values
INT_STATUS_SUCCESSin case of success
INT_STATUS_INVALID_PARAMETER_1if Process is NULL
INT_STATUS_NOT_NEEDED_HINTif the guest is not Windows, not using 4- or 5-level paging (the other paging modes do not use the self map mechanism)

Definition at line 710 of file winselfmap.c.

Referenced by IntWinProcChangeProtectionFlags(), IntWinProcCreateProcessObject(), and IntWinSelfMapEnableSelfMapEntryProtection().

◆ IntWinSelfMapUnprotectSelfMapIndex()

INTSTATUS IntWinSelfMapUnprotectSelfMapIndex ( WIN_PROCESS_OBJECT Process)

Removes the EPT protection for the self map entry index of a process.

This removes the EPT hooks set by IntWinSelfMapProtectSelfMapIndex.

Parameters
[in]ProcessThe process for which the protection is removed.
Return values
INT_STATUS_SUCCESSin case of success
INT_STATUS_INVALID_PARAMETER_1if Process is NULL

Definition at line 802 of file winselfmap.c.

Referenced by IntWinProcChangeProtectionFlags(), IntWinProcDeleteProcessObject(), and IntWinSelfMapDisableSelfMapEntryProtection().

◆ IntWinSelfMapValidateSelfMapEntries()

INTSTATUS IntWinSelfMapValidateSelfMapEntries ( void  )

Validates the self map entries for every process in the system.

This function is used by the integrity mechanism in order to perform self map entry validations. Due to performance reasons, we can't hook the self map entry for every process in the system. For processes that are already protected this is not a problem, as we already have hooks placed inside their page tables. For the other processes we delegate the check to the periodic integrity callback. It uses the IntWinSelfMapCheckSelfMapEntry function to check the self map entry for every process on the system.

Return values
INT_STATUS_SUCCESSin case of success
INT_STATUS_NOT_INITIALIZED_HINTif the guest is not initialized or the protection is not active
INT_STATUS_NOT_NEEDED_HINTif the guest is not Windows, not using 4- or 5-level paging (the other paging modes do not use the self map mechanism), or if the INTRO_OPT_PROT_KM_SELF_MAP_ENTRY activation option was not provided

Definition at line 453 of file winselfmap.c.

Referenced by IntHandleTimer().