Bitdefender Hypervisor Memory Introspection
hook_gpa.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_GPA_H_
6 #define _HOOK_GPA_H_
7 
8 #include "queue.h"
9 
10 typedef struct _HOOK_HEADER HOOK_HEADER;
11 
29 typedef INTSTATUS
31  _In_opt_ void *Context,
32  _In_ void *Hook,
33  _In_ QWORD Address,
34  _Out_ INTRO_ACTION *Action
35  );
36 
37 
41 typedef struct _HOOK_GPA
42 {
45 
49 
51 
54 
55 
60 typedef struct _HOOK_SPP_ENTRY
61 {
64  DWORD SppCount[32];
66 
67 
72 typedef struct _HOOK_EPT_ENTRY
73 {
76 
82  HOOK_SPP_ENTRY *Spp;
85 
86 
87 #define GPA_HOOK_TABLE_SIZE 1024
88 #define GPA_HOOK_ID(addr) (((addr) >> 12) & (GPA_HOOK_TABLE_SIZE - 1))
89 
90 #define GPA_EPT_TABLE_SIZE 4096
91 #define GPA_EPT_ID(addr) (((addr) >> 12) & (GPA_EPT_TABLE_SIZE - 1))
92 
93 #define GPA_REF_COUNT(epte) (((QWORD)((epte)->ReadCount) + \
94  (QWORD)((epte)->WriteCount) + \
95  ((QWORD)(epte)->ExecuteCount) + \
96  ((QWORD)(epte)->PtCount)))
97 
98 #define MAX_HOOK_COUNT UINT32_MAX
99 
100 
104 typedef struct _HOOK_GPA_STATE
105 {
108  LIST_HEAD GpaHooksExecute[GPA_HOOK_TABLE_SIZE];
109 
113 
116 
118 
124 
125 
126 //
127 // API
128 //
129 PHOOK_EPT_ENTRY
132  );
133 
134 INTSTATUS
136  _In_ QWORD Gpa,
137  _In_ DWORD Length,
138  _In_ BYTE Type,
140  _In_opt_ void *Context,
141  _In_opt_ void *ParentHook,
142  _In_opt_ DWORD Flags,
143  _Out_opt_ HOOK_GPA **Hook
144  );
145 
146 INTSTATUS
148  _Inout_ HOOK_GPA **Hook,
149  _In_ DWORD Flags
150  );
151 
152 INTSTATUS
154  _In_ HOOK_GPA **Hook,
155  _In_ DWORD Flags
156  );
157 
158 INTSTATUS
160  _In_ HOOK_GPA *Hook
161  );
162 
163 INTSTATUS
165  _In_ HOOK_GPA *Hook
166  );
167 
168 INTSTATUS
170  void
171  );
172 
173 INTSTATUS
175  _In_ QWORD Gpa,
176  _Out_ BYTE *Read,
177  _Out_ BYTE *Write,
178  _Out_ BYTE *Execute
179  );
180 
181 INTSTATUS
183  void
184  );
185 
186 INTSTATUS
188  void
189  );
190 
191 INTSTATUS
193  void
194  );
195 
196 void
198  void
199  );
200 
201 INTSTATUS
203  void
204  );
205 
206 INTSTATUS
208  void
209  );
210 
211 INTSTATUS
213  _In_ DWORD EptIndex,
214  _In_ QWORD Address,
215  _Out_ BYTE *Read,
216  _Out_ BYTE *Write,
217  _Out_ BYTE *Execute
218  );
219 
220 INTSTATUS
222  void
223  );
224 
225 #endif // _HOOK_GPA_H_
#define _In_opt_
Definition: intro_sal.h:16
_Bool BOOLEAN
Definition: intro_types.h:58
#define _Out_
Definition: intro_sal.h:22
#define GPA_HOOK_TABLE_SIZE
Size of the GPA hook hash.
Definition: hook_gpa.h:87
LIST_ENTRY Link
List entry element.
Definition: hook_gpa.h:44
HOOK_HEADER Header
Hook header.
Definition: hook_gpa.h:43
DWORD ReadCount
Number of read EPT hooks.
Definition: hook_gpa.h:77
long long INT64
Definition: intro_types.h:45
QUEUE_HEAD RemovedHooksExecute
List of removed execute hooks.
Definition: hook_gpa.h:112
void * Context
User-defined data that will be supplied to the callback.
Definition: hook.h:74
uint8_t BYTE
Definition: intro_types.h:47
INTSTATUS IntHookGpaDisableVe(void)
Disable VE filtering.
Definition: hook_gpa.c:1430
#define _In_
Definition: intro_sal.h:21
struct _HOOK_SPP_ENTRY * PHOOK_SPP_ENTRY
INTSTATUS IntHookGpaRemoveHook(HOOK_GPA **Hook, DWORD Flags)
Remove a GPA hook.
Definition: hook_gpa.c:738
uint16_t WORD
Definition: intro_types.h:48
INTSTATUS IntHookGpaDisablePtCache(void)
Disable PT filtering.
Definition: hook_gpa.c:1442
QWORD OldSpp
Old SPP value. Usually indicates full write access to the entire page.
Definition: hook_gpa.h:62
INTSTATUS IntHookGpaIsPageHooked(QWORD Gpa, BYTE *Read, BYTE *Write, BYTE *Execute)
Get the read, write and execute access for the given guest physical page.
Definition: hook_gpa.c:1006
INTSTATUS IntHookGpaGetEPTPageProtection(DWORD EptIndex, QWORD Address, BYTE *Read, BYTE *Write, BYTE *Execute)
Get the EPT page protection for the indicated guest physical address.
Definition: hook_gpa.c:1454
struct _HOOK_SPP_ENTRY HOOK_SPP_ENTRY
DWORD WriteCount
Number of write EPT hooks.
Definition: hook_gpa.h:78
INTSTATUS IntHookGpaEnableHook(HOOK_GPA *Hook)
Enable a GPA hook.
Definition: hook_gpa.c:977
int INTSTATUS
The status data type.
Definition: introstatus.h:24
INTSTATUS IntHookGpaFindConvertible(void)
Displays all convertible pages.
Definition: hook_gpa.c:1499
QUEUE_ENTRY LinkRemoved
Link element for the removed hooks list.
Definition: hook_gpa.h:52
INTSTATUS IntHookGpaSetHook(QWORD Gpa, DWORD Length, BYTE Type, PFUNC_EptViolationCallback Callback, void *Context, void *ParentHook, DWORD Flags, HOOK_GPA **Hook)
Places an EPT hook on the indicated memory range.
Definition: hook_gpa.c:193
INTSTATUS IntHookGpaInit(void)
Initialize the GPA hook system. This function should be called only once, during introspection init...
Definition: hook_gpa.c:1097
struct _HOOK_GPA * PHOOK_GPA
QUEUE_HEAD RemovedHooksWrite
List of removed write hooks.
Definition: hook_gpa.h:110
#define _Inout_
Definition: intro_sal.h:20
PHOOK_EPT_ENTRY IntHookGpaGetExistingEptEntry(QWORD GpaPage)
Get the EPT entry associated with the provided guest physical page.
Definition: hook_gpa.c:161
#define _Out_opt_
Definition: intro_sal.h:30
void IntHookGpaDump(void)
Dump the entire contents of the GPA hook system, listing each hook.
Definition: hook_gpa.c:1156
INTSTATUS IntHookGpaEnablePtCache(void)
Enable PT filtering.
Definition: hook_gpa.c:1418
struct _HOOK_EPT_ENTRY HOOK_EPT_ENTRY
unsigned long long QWORD
Definition: intro_types.h:53
WORD Offset
The offset within the page where the hook starts. 0-4095 valid.
Definition: hook_gpa.h:47
QWORD GpaPage
The page where the hook is set.
Definition: hook_gpa.h:46
QWORD CurSpp
Current SPP permissions.
Definition: hook_gpa.h:63
struct _HOOK_GPA_STATE * PHOOK_GPA_STATE
INTSTATUS IntHookGpaEnableVe(void)
Enable VE filtering.
Definition: hook_gpa.c:1406
QWORD GpaPage
Guest physical page address.
Definition: hook_gpa.h:75
INTSTATUS IntHookGpaDeleteHook(HOOK_GPA **Hook, DWORD Flags)
Permanently delete a GPA hook.
Definition: hook_gpa.c:830
QUEUE_HEAD RemovedHooksRead
List of removed read hooks.
Definition: hook_gpa.h:111
INT64 HooksCount
Total number of hooks set.
Definition: hook_gpa.h:117
uint32_t DWORD
Definition: intro_types.h:49
BOOLEAN SppEnabled
True if SPP support is present and enabled.
Definition: hook_gpa.h:122
struct _HOOK_GPA HOOK_GPA
enum _INTRO_ACTION INTRO_ACTION
Event actions.
DWORD PtCount
Number of PT hooks.
Definition: hook_gpa.h:80
DWORD ConvCount
Definition: hook_gpa.h:81
Definition: queue.h:10
struct _HOOK_GPA_STATE HOOK_GPA_STATE
WORD Length
The length, in bytes, of the hook. 1-4096 valid.
Definition: hook_gpa.h:48
LIST_ENTRY Link
List entry element.
Definition: hook_gpa.h:74
INTSTATUS(* PFUNC_EptViolationCallback)(void *Context, void *Hook, QWORD Address, INTRO_ACTION *Action)
EPT callback handler.
Definition: hook_gpa.h:30
BOOLEAN VeEnabled
True if VE filtering is enabled.
Definition: hook_gpa.h:120
BOOLEAN PtCacheEnabled
True if the PT cache is active inside the guest.
Definition: hook_gpa.h:121
#define GPA_EPT_TABLE_SIZE
Size of the EPT entries hash.
Definition: hook_gpa.h:90
DWORD ExecuteCount
Number of execute EPT hooks.
Definition: hook_gpa.h:79
PFUNC_EptViolationCallback Callback
The callback for this hook.
Definition: hook_gpa.h:50
struct _HOOK_EPT_ENTRY * PHOOK_EPT_ENTRY
INTSTATUS IntHookGpaDisableHook(HOOK_GPA *Hook)
Disable a GPA hook.
Definition: hook_gpa.c:951
BOOLEAN HooksRemoved
True if hooks were removed, and we must do the cleanup..
Definition: hook_gpa.h:119
INTSTATUS IntHookGpaCommitHooks(void)
Commit existing modified hooks.
Definition: hook_gpa.c:876