Bitdefender Hypervisor Memory Introspection
winvad.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _WINVAD_H_
6 #define _WINVAD_H_
7 
8 #include "detours.h"
9 #include "winprocess.h"
10 
12 #define PROT_READ 1
13 #define PROT_WRITE 2
15 #define PROT_EXEC 4
17 
24 typedef enum
25 {
28  VAD_PROT_EXECUTE = 0x0002,
34  VAD_PROT_NOCACHE = 0x0008,
35  VAD_PROT_GUARD = 0x0010,
37 } WIN_VAD_PROT;
38 
41 typedef struct _VAD_PAGE
42 {
45 
55 
57  struct _VAD *Vad;
59  void *ExecHook;
60 
71 
75 
80 typedef struct _VAD
81 {
125 
134 
140 
145 
148 
149  struct
150  {
166  DWORD Unused : 25;
167  };
168 } VAD, *PVAD;
169 
183  _In_ QWORD VadNodeGva,
184  _In_ DWORD Level,
185  _In_opt_ void *Context
186  );
187 
188 void
190  _Inout_ WIN_PROCESS_OBJECT *Process
191  );
192 
193 VAD *
195  _In_ WIN_PROCESS_OBJECT *Process,
196  _In_ QWORD Va
197  );
198 
199 INTSTATUS
201  _In_ void const *Detour
202  );
203 
204 INTSTATUS
206  _In_ void const *Detour
207  );
208 
209 INTSTATUS
211  _In_ void const *Detour
212  );
213 
214 INTSTATUS
216  _In_ void const *Detour
217  );
218 
219 INTSTATUS
221  _In_ void const *Detour
222  );
223 
224 INTSTATUS
226  _In_ void const *Detour
227  );
228 
229 INTSTATUS
231  _Inout_ WIN_PROCESS_OBJECT *Process
232  );
233 
234 INTSTATUS
236  _Inout_ WIN_PROCESS_OBJECT *Process
237  );
238 
239 void
241  _Inout_ WIN_PROCESS_OBJECT *Process
242  );
243 
244 INTSTATUS
246  _In_ QWORD VadNodeGva,
247  _In_ DWORD Level,
248  _In_opt_ void *Context
249  );
250 
251 QWORD
253  _In_ QWORD VadRoot,
254  _In_ QWORD StartPage,
255  _In_ QWORD EndPage,
256  _In_ DWORD Level,
257  _In_ QWORD OldStartPage,
258  _In_ BOOLEAN LastBranchRight
259  );
260 
261 INTSTATUS
263  _In_ QWORD VadNodeGva,
264  _In_ DWORD Level,
266  _In_opt_ void *Context
267  );
268 
270 BOOLEAN
272  _In_ VAD const *Vad,
273  _In_ void *Context
274  );
275 
276 INTSTATUS
278  _In_ PWIN_PROCESS_OBJECT Process,
280  );
281 
282 INTSTATUS
284  _In_ void const *Detour
285  );
286 
287 VAD *
289  _Inout_ WIN_PROCESS_OBJECT *Process,
290  _In_ QWORD StartHint,
291  _In_ QWORD LengthHint
292  );
293 
295 INTSTATUS
297  _In_ QWORD FunctionAddress,
298  _Inout_ API_HOOK_HANDLER *Handler,
299  _In_ void *Descriptor
300  );
301 
302 BOOLEAN
304  _In_ const VAD *Vad
305  );
306 
308 INTSTATUS
310  _In_ QWORD FunctionAddress,
311  _Inout_ API_HOOK_HANDLER *Handler,
312  _In_ void *Descriptor
313  );
314 
316 INTSTATUS
318  _In_ QWORD FunctionAddress,
319  _Inout_ API_HOOK_HANDLER *Handler,
320  _In_ void *Descriptor
321  );
322 
324 INTSTATUS
326  _In_ QWORD FunctionAddress,
327  _Inout_ API_HOOK_HANDLER *Handler,
328  _In_ void *Descriptor
329  );
330 
332 INTSTATUS
334  _In_ QWORD FunctionAddress,
335  _Inout_ API_HOOK_HANDLER *Handler,
336  _In_ void *Descriptor
337  );
338 
340 INTSTATUS
342  _In_ QWORD FunctionAddress,
343  _Inout_ API_HOOK_HANDLER *Handler,
344  _In_ void *Descriptor
345  );
346 
348 INTSTATUS
350  _In_ QWORD FunctionAddress,
351  _Inout_ API_HOOK_HANDLER *Handler,
352  _In_ void *Descriptor
353  );
354 
355 INTSTATUS
357  _Inout_ WIN_PROCESS_OBJECT *Process
358  );
359 
360 void
362  _Inout_ VAD **Vad
363  );
364 
365 INTSTATUS
367  _In_ QWORD VadRoot,
368  _In_ QWORD StartPage,
369  _In_ QWORD EndPage,
370  _Out_ VAD *Vad
371  );
372 
373 #endif
INTSTATUS IntWinVadPatchInsert(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiInsertVad guest API detour.It will be invoked before th...
Definition: winvad.c:3770
#define _In_opt_
Definition: intro_sal.h:16
void * PathSwapHandle
Definition: winvad.h:133
#define _Out_
Definition: intro_sal.h:22
_Bool BOOLEAN
Definition: intro_types.h:58
Exposes the types, constants and functions used to handle Windows processes events (creation...
INTSTATUS IntWinVadHandleInsert(void const *Detour)
The detour handler that will be invoked when the guest inserts a new VAD in the tree.This is the detour handler for the MiInsertVad guest API.
Definition: winvad.c:2825
struct _VAD * PVAD
struct _VAD_PAGE * PVAD_PAGE
INTSTATUS IntWinVadHandleDeleteVaRange(void const *Detour)
The detour handler that will be invoked when a memory range contained by a VAD is deleted...
Definition: winvad.c:3197
INTSTATUS IntWinVadPatchInsertMap(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiGetWsAndInsertVad guest API detour.It will be invoked b...
Definition: winvad.c:3572
#define _In_
Definition: intro_sal.h:21
INTSTATUS IntWinVadImportProcessTree(WIN_PROCESS_OBJECT *Process)
Scans the guest VAD tree and imports the nodes into our VAD tree.
Definition: winvad.c:2768
void IntWinVadStopExploitMonitor(WIN_PROCESS_OBJECT *Process)
Disables the exploit monitoring for a process.
Definition: winvad.c:1811
Described a detour handler.
Definition: detours.h:283
INTSTATUS IntWinVadRemoveProcessTree(WIN_PROCESS_OBJECT *Process)
Removes the VAD tree from a process.
Definition: winvad.c:1351
INTSTATUS IntWinVadProcImportMainModuleVad(WIN_PROCESS_OBJECT *Process)
Imports the VAD that describes the main module of a process.
Definition: winvad.c:3816
BOOLEAN IntWinVadDump(VAD const *Vad, void *Context)
Prints a VAD structure.
Definition: winvad.c:1706
Definition: rbtree.h:34
QWORD Right
Definition: winvad.h:93
INTSTATUS IntWinVadHandleVirtualProtect(void const *Detour)
The detour handler that will be invoked when a memory range contained by a VAD has the protection rig...
Definition: winvad.c:3339
struct _VAD VAD
A representation of a Windows VAD structure.
int INTSTATUS
The status data type.
Definition: introstatus.h:24
QWORD RangeStart
Definition: winvad.h:65
DWORD VmProtection
Definition: winvad.h:50
INTSTATUS IntWinVadHandleCommit(void const *Detour)
The detour handler that will be invoked when an existing VAD is committed by the guest.This is the detour handler for the MiCommitExistingVad guest API. Due to the way we ignore certain VADs, this can be invoked either when protection is changed for a known VAD, in which case we have to adjust our protection; or, when protection is changed for a previously unknown VAD in a way that makes it relevant for Introcore, in which case we treat as a newly created VAD.
Definition: winvad.c:3026
BOOLEAN Legitimate
True if an execution from this page was attempted and it was deemed to no be malicious.
Definition: winvad.h:73
QWORD VadGva
The guest virtual address at which the corresponding Windows _MMVAD structure is located.
Definition: winvad.h:108
QWORD Left
Definition: winvad.h:88
QWORD SubsectionGva
Definition: winvad.h:139
The guest detour API.
WIN_VAD_PROT
VAD protection flags as used by the Windows kernel. These represent the values in the Protection port...
Definition: winvad.h:24
DWORD ExecCount
The number of execution violations triggered by pages inside this VAD.
Definition: winvad.h:147
QWORD Address
The base address of the page.
Definition: winvad.h:44
#define _Inout_
Definition: intro_sal.h:20
QWORD StartPage
Definition: winvad.h:102
QWORD Parent
Definition: winvad.h:98
DWORD DeleteInProgress
Set if the DeleteInProgress bit inside the VadFlags field is set.
Definition: winvad.h:164
INTSTATUS IntWinVadPatchVirtualProtect(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiProtectVirtualMemory guest API detour.It will be invoked before the detour is placed inside the guest and will patch the detour handler with the value of winKmFieldProcessSpare.
Definition: winvad.c:3621
DWORD Protection
Definition: winvad.h:124
void * ExecHook
Execution hook handle, if one exists.
Definition: winvad.h:59
unsigned long long QWORD
Definition: intro_types.h:53
WINUM_PATH * Path
Definition: winvad.h:144
INTSTATUS IntWinVadPatchDeleteVaRange(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiDeleteVirtualAddresses guest API detour.It will be invoked before the detour is placed inside the guest and will patch the detour handler with the value of winKmFieldProcessSpare.
Definition: winvad.c:3678
QWORD RangeEnd
Definition: winvad.h:70
INTSTATUS IntWinVadHandleInsertPrivate(void const *Detour)
The detour handler that will be invoked when the guest inserts a new VAD in the tree.This is the detour handler for the MiInsertPrivateVad guest API.
Definition: winvad.c:2897
INTSTATUS IntWinVadPatchFinishVadDeletion(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiFinishVadDeletion guest API detour.It will be invoked b...
Definition: winvad.c:3726
BOOLEAN IntWinVadIsInTree(const VAD *Vad)
Checks if a VAD is inserted in a guest VAD tree.
Definition: winvad.c:3437
DWORD VadProtection
Definition: winvad.h:116
struct _VAD * Vad
The VAD containing this page.
Definition: winvad.h:57
VAD_TYPE VadType
The type of the VAD.
Definition: winvad.h:118
INTSTATUS(* PFUNC_PreDetourCallback)(QWORD FunctionAddress, void *Handler, void *Descriptor)
The type of a callback invoked before setting a detour.
Definition: detours.h:232
RBNODE RbNode
The node inside the WIN_PROCESS_OBJECT.VadTree tree.
Definition: winvad.h:83
INTSTATUS IntWinPatchVadHandleCommit(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiCommitExistingVad guest API detour.It will be invoked b...
Definition: winvad.c:2960
DWORD Unused
Spare bits.
Definition: winvad.h:166
A representation of a memory page included in a VAD structure.
Definition: winvad.h:41
INTSTATUS IntWinVadWalkTree(PWIN_PROCESS_OBJECT Process, PFUNC_RbTreeWalkCallback Callback)
Walks the VAD tree of a process.
Definition: winvad.c:2025
DWORD StaticScan
Set if the VAD was statically detected by a scan, after it was created.
Definition: winvad.h:152
WIN_PROCESS_OBJECT * Process
The process to which this VAD belongs to.
Definition: winvad.h:127
uint32_t DWORD
Definition: intro_types.h:49
INTSTATUS IntWinVadFetchByRange(QWORD VadRoot, QWORD StartPage, QWORD EndPage, VAD *Vad)
Fetches and returns a VAD object containing the range represented by [StartPage, EndPage].
Definition: winvad.c:3877
INTSTATUS IntWinVadHandleInsertMap(void const *Detour)
The detour handler that will be invoked when a VAD is inserted in the guest VAD tree.This is the detour handler for the MiGetWsAndInsertVad guest API.
Definition: winvad.c:3142
INTSTATUS IntWinVadPatchInsertPrivate(QWORD FunctionAddress, API_HOOK_HANDLER *Handler, void *Descriptor)
This is the PFUNC_PreDetourCallback for the MiInsertPrivateVad guest API detour.It will be invoked be...
Definition: winvad.c:3512
FUNC_RbTreeWalkCallback * PFUNC_RbTreeWalkCallback
Definition: rbtree.h:79
INTSTATUS IntWinVadHandleFinishVadDeletion(void const *Detour)
The detour handler that will be invoked when a memory range contained by a VAD is deleted...
Definition: winvad.c:3268
void IntWinVadDestroyObject(VAD **Vad)
Frees a VAD and all the resources held by it.
Definition: winvad.c:285
#define _Function_class_(expr)
Definition: intro_sal.h:40
VAD * IntWinVadFindAndUpdateIfNecessary(WIN_PROCESS_OBJECT *Process, QWORD StartHint, QWORD LengthHint)
Searches for a VAD in the Introcore VAD tree. If no VAD is found, or if the found one does not fully ...
Definition: winvad.c:2122
VAD * IntWinVadFindByVa(WIN_PROCESS_OBJECT *Process, QWORD Va)
Finds a VAD that contains a given guest virtual address.
Definition: winvad.c:602
enum _VAD_TYPE VAD_TYPE
The types of a _MMVAD structure.
INTSTATUS(* PFUNC_WinVadTraversalCallback)(QWORD VadNodeGva, DWORD Level, void *Context)
Callback type used for in-guest VAD tree traversals.
Definition: winvad.h:182
QWORD PageCount
The number of 4K pages in the VAD.
Definition: winvad.h:110
INTSTATUS IntWinVadInOrderRecursiveTraversal(QWORD VadNodeGva, DWORD Level, PFUNC_WinVadTraversalCallback Callback, void *Context)
Traverses a guest VAD tree.
Definition: winvad.c:1920
DWORD Protection
Definition: winvad.h:54
BOOLEAN FUNC_RbTreeWalkCallback(RBNODE *Node, void *WalkContext)
Definition: rbtree.h:78
INTSTATUS IntWinVadShortDump(QWORD VadNodeGva, DWORD Level, void *Context)
Prints a _MMVAD_SHORT structure.
Definition: winvad.c:1659
DWORD HugeVad
Set if the memory range represented by this VAD has a size of at least 4G.
Definition: winvad.h:156
QWORD EndPage
Definition: winvad.h:106
DWORD NoChange
Set if the NoChange bit inside the VadFlags field is set.
Definition: winvad.h:160
void IntWinVadProcessInit(WIN_PROCESS_OBJECT *Process)
Initializes a WIN_PROCESS_OBJECT.VadTree.
Definition: winvad.c:462
DWORD PrivateFixup
Set if the PrivateFixup bit inside the VadFlags field is set.
Definition: winvad.h:162
A representation of a Windows VAD structure.
Definition: winvad.h:80
DWORD IsIgnored
Set if this VAD is not monitored regardless of the protection rights it has.
Definition: winvad.h:158
struct _VAD_PAGE VAD_PAGE
A representation of a memory page included in a VAD structure.
DWORD IsStack
Set if the memory range represented by this VAD is a stack.
Definition: winvad.h:154
VAD_PAGE ** VadPages
An array representing each page in the VAD. It has PageCount entries.
Definition: winvad.h:129
This structure describes a running process inside the guest.
Definition: winprocess.h:83
QWORD IntWinVadFindNodeInGuestSpace(QWORD VadRoot, QWORD StartPage, QWORD EndPage, DWORD Level, QWORD OldStartPage, BOOLEAN LastBranchRight)
Searches for a VAD node inside a guest VAD tree.
Definition: winvad.c:1825