Bitdefender Hypervisor Memory Introspection
hook_pts.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _HOOK_PTS_H_
6 #define _HOOK_PTS_H_
7 
8 #include "hook_ptwh.h"
9 
10 
11 //
12 // Definitions, flags & constants.
13 //
14 #define HOOK_PTS_FLG_DELETE_PT_HOOK 0x00000100
15 #define HOOK_PTS_FLG_DELETE_PD_HOOK 0x00000200
16 
19 #define HOOK_PTS_MONITORED_BITS (PT_P | PD_PS | PT_US | PT_RW | 0x000FFFFFFFFFF000)
20 
21 #define HOOK_PT_HASH_SIZE 64
22 #define HOOK_PT_HASH_ID(x) (((x) >> 12) & (HOOK_PT_HASH_SIZE - 1))
23 #define HOOK_PT_PAE_ROOT_HASH_ID(x) (((x) >> 5) & (HOOK_PT_HASH_SIZE - 1))
24 
25 
40 typedef INTSTATUS
42  _In_ void *Context,
43  _In_ QWORD VirtualAddress,
44  _In_ QWORD OldEntry,
45  _In_ QWORD NewEntry,
46  _In_ QWORD OldPageSize,
47  _In_ QWORD NewPageSize
48  );
49 
50 
57 typedef struct _HOOK_PTS_ENTRY
58 {
77 
78 
85 typedef struct _HOOK_PTS
86 {
96  PHOOK_PTS_ENTRY Parent;
100 
101 
105 typedef struct _HOOK_PTS_STATE
106 {
109  LIST_HEAD HooksRootList[HOOK_PT_HASH_SIZE];
119 
120 
121 
122 //
123 // API
124 //
125 INTSTATUS
127  _In_ QWORD Cr3,
128  _In_ QWORD VirtualAddress,
129  _In_ PFUNC_SwapCallback Callback,
130  _In_opt_ void *Context,
131  _In_opt_ void *Parent,
132  _In_ DWORD Flags,
133  _Out_ PHOOK_PTS *Hook
134  );
135 
136 INTSTATUS
138  _Inout_ HOOK_PTS **Hook,
139  _In_ DWORD Flags
140  );
141 
142 INTSTATUS
144  _Inout_ HOOK_PTS **Hook,
145  _In_ DWORD Flags
146  );
147 
148 INTSTATUS
150  void
151  );
152 
153 INTSTATUS
155  void
156  );
157 
158 INTSTATUS
160  _In_ PHOOK_PTS_ENTRY Entry,
161  _In_ QWORD OldValue,
162  _In_ QWORD NewValue
163  );
164 
165 INTSTATUS
167  void
168  );
169 
170 void
172  void
173  );
174 
175 #endif // _HOOK_PTS_H
#define _In_opt_
Definition: intro_sal.h:16
#define _Out_
Definition: intro_sal.h:22
_Bool BOOLEAN
Definition: intro_types.h:58
LIST_HEAD RemovedHooksPdpList
List of removed page-directory pointer entry hooks.
Definition: hook_pts.h:114
BYTE EntrySize
4 (32 bit paging) or 8 (PAE or 64 bit paging)
Definition: hook_pts.h:68
uint8_t BYTE
Definition: intro_types.h:47
LIST_ENTRY Link
List element.
Definition: hook_pts.h:94
QWORD OldEntry
Previous page-table entry.
Definition: hook_pts.h:91
#define _In_
Definition: intro_sal.h:21
BOOLEAN IsLeaf
Definition: hook_pts.h:64
INTSTATUS IntHookPtsWriteEntry(PHOOK_PTS_ENTRY Entry, QWORD OldValue, QWORD NewValue)
Tests the translation modification handler.
Definition: hook_pts.c:2216
uint16_t WORD
Definition: intro_types.h:48
LIST_ENTRY PtsLink
Link inside the HooksPtsList.
Definition: hook_pts.h:95
LIST_HEAD ChildrenEntries
Children entries. Will be empty for leafs. Each entry is a HOOK_PTS_ENTRY.
Definition: hook_pts.h:72
struct _HOOK_PTS_ENTRY * PHOOK_PTS_ENTRY
QWORD OldPageSize
Previous page size.
Definition: hook_pts.h:93
QWORD Cr3
Virtual address space where the address is monitored.
Definition: hook_pts.h:88
LIST_HEAD RemovedHooksPml4List
List of removed PML4 entry hooks.
Definition: hook_pts.h:115
struct _HOOK_PTS_STATE * PHOOK_PTS_STATE
BOOLEAN IsPs
True if this entry is a page size extension, and points to a 2M/4M/1G page.
Definition: hook_pts.h:67
int INTSTATUS
The status data type.
Definition: introstatus.h:24
INTSTATUS IntHookPtsCommitHooks(void)
Commit all PTS hook modifications.
Definition: hook_pts.c:2084
LIST_HEAD HooksPtsList
List of swap hooks.
Definition: hook_pts.h:108
QWORD CurPageSize
Current page size.
Definition: hook_pts.h:92
INTSTATUS IntHookPtsSetHook(QWORD Cr3, QWORD VirtualAddress, PFUNC_SwapCallback Callback, void *Context, void *Parent, DWORD Flags, PHOOK_PTS *Hook)
Start monitoring translation modifications for the given VirtualAddress.
Definition: hook_pts.c:1535
BOOLEAN IntegrityCheckFailed
True if integrity checks failed on this translation.
Definition: hook_pts.h:98
#define _Inout_
Definition: intro_sal.h:20
WORD EntryOffset
Entry offset inside the monitored page-table.
Definition: hook_pts.h:75
struct _HOOK_PTS_STATE HOOK_PTS_STATE
unsigned long long QWORD
Definition: intro_types.h:53
LIST_HEAD RemovedHooksPtList
List of removed page-table entry hooks.
Definition: hook_pts.h:112
QWORD CurEntry
Current page-table entry.
Definition: hook_pts.h:90
PHOOK_PTM PtPaHook
Definition: hook_pts.h:60
HOOK_HEADER Header
Hook header - must be present for every hook.
Definition: hook_pts.h:59
HOOK_HEADER Header
Hook header - must be present for every hook.
Definition: hook_pts.h:87
INTSTATUS IntHookPtsRemoveHook(HOOK_PTS **Hook, DWORD Flags)
Remove a PTS hook.
Definition: hook_pts.c:1944
INTSTATUS(* PFUNC_SwapCallback)(void *Context, QWORD VirtualAddress, QWORD OldEntry, QWORD NewEntry, QWORD OldPageSize, QWORD NewPageSize)
Callback invoked on translation modifications.
Definition: hook_pts.h:41
DWORD RefCount
Number of references.
Definition: hook_pts.h:74
QWORD PtPaAddress
Physical address of the PT/PD/PDP/PML4/PML5 entry associated to this particular page.
Definition: hook_pts.h:62
BOOLEAN HooksRemoved
True if any hook has been removed.
Definition: hook_pts.h:117
LIST_HEAD ContextEntries
The actual contexts. Each context will be a HOOK_PTS structure.
Definition: hook_pts.h:73
LIST_HEAD RemovedHooksPtsList
List of removed PTS entries.
Definition: hook_pts.h:111
uint32_t DWORD
Definition: intro_types.h:49
INTSTATUS IntHookPtsDeleteHook(HOOK_PTS **Hook, DWORD Flags)
Permanently delete the PTS hook.
Definition: hook_pts.c:1993
PHOOK_PTS_ENTRY Parent
The leaf page-table entry hook associated with this address.
Definition: hook_pts.h:96
struct _HOOK_PTS HOOK_PTS
BOOLEAN PtPaHookSet
True if a hook is placed on the PT entry.
Definition: hook_pts.h:63
INTSTATUS IntHookPtsCheckIntegrity(void)
Checks the integrity of the existing page-table hooks. Used for debugging the PT filter.
Definition: hook_pts.c:2236
PFUNC_SwapCallback Callback
Swap callback.
Definition: hook_pts.h:97
BOOLEAN IsValid
This referrers to the entry contained by this PTE. If true, it points to a valid table.
Definition: hook_pts.h:66
struct _HOOK_PTS_ENTRY HOOK_PTS_ENTRY
LIST_HEAD RemovedHooksPdList
List of removed page-directory entry hooks.
Definition: hook_pts.h:113
BYTE Level
Page table level (1 - PT, 5 - PML5)
Definition: hook_pts.h:69
LIST_HEAD RemovedHooksPml5List
List of removed PML5 entry hooks.
Definition: hook_pts.h:116
struct _HOOK_PTS * PHOOK_PTS
HOOK_PTEWS WriteState
Write state.
Definition: hook_pts.h:70
LIST_HEAD * CallbacksList
List of callbacks.
Definition: hook_pts.h:107
LIST_ENTRY Link
Link inside the containing list.
Definition: hook_pts.h:71
LIST_HEAD RemovedHooksRootList
List of removed root entries.
Definition: hook_pts.h:110
INTSTATUS IntHookPtsInit(void)
Initializes the PTS hooks system.
Definition: hook_pts.c:2183
QWORD VirtualAddress
The monitored virtual address.
Definition: hook_pts.h:89
#define HOOK_PT_HASH_SIZE
Definition: hook_pts.h:21
void IntHookPtsDump(void)
Prints all the page table hooks.
Definition: hook_pts.c:2452