Bitdefender Hypervisor Memory Introspection
guests.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _GUESTS_H_
6 #define _GUESTS_H_
7 
8 #include "winguest.h"
9 #include "lixguest.h"
10 #include "bddisasm.h"
11 #include "bdshemu.h"
12 #include "vecommon.h"
13 #include "udlist.h"
14 
18 typedef enum
19 {
20  CPU_STATE_NONE = 0x00,
27  CPU_STATE_TIMER = 0x07,
31 } CPU_STATE;
32 
38 typedef struct _PATCH_BUFFER
39 {
43  BYTE Data[ND_MAX_REGISTER_SIZE];
45 
49 typedef struct _PTEMU_BUFFER
50 {
57 
58 
64 typedef struct _PTWRITE_CACHE
65 {
70 
71 
72 #define SHEMU_SHELLCODE_SIZE 0x2000
73 #define SHEMU_STACK_SIZE 0x2000
74 #define SHEMU_MAX_INSTRUCTIONS 256
75 
76 //
77 // Per-CPU state structure.
78 //
79 
83 typedef struct _VCPU_STATE
84 {
88  INSTRUX Instruction;
104 
109 
113 
115 
124 
126  struct
127  {
136  } Exception;
137 
149  union
150  {
160  };
161 
162  // TBD - Add any other VCPU-specific field here.
163 
167 
171 
174 
179 
197 
201 
205 typedef struct _MM
206 {
211  QWORD LastGpa;
218 } MM, *PMM;
219 
223 typedef struct _INTRO_PROT_OPTIONS
224 {
233 
253 
254 // Forward these declarations, makes it easy to use it directly when needed
260 typedef struct _EXCEPTIONS EXCEPTIONS;
261 
265 typedef struct _GUEST_STATE
266 {
269 
271 
273 
278 
281 
294  BOOLEAN KernelBetaDetections;
311  BOOLEAN EnterHibernate;
317  BOOLEAN DisableOnReturn;
330  BOOLEAN PtFilterEnabled;
350 
355 
362 
364 
368  PVCPU_STATE VcpuArray;
369 
370  MM Mm;
371 
378 
380 
382 
387 
389 
398 
399  void *GpaCache;
401 
402  SHEMU_CONTEXT Shemucontext;
403 
405  BYTE ShemuShellcode[SHEMU_SHELLCODE_SIZE];
407  BYTE ShemuStack[SHEMU_STACK_SIZE];
410 
413  union
414  {
417  };
419 
420 
421 extern GUEST_STATE gGuest;
422 extern WINDOWS_GUEST *gWinGuest;
423 extern LINUX_GUEST *gLixGuest;
424 
425 extern VCPU_STATE *gVcpu;
426 
430 typedef enum
431 {
432  POST_COMMIT_MEM = 0x00000001,
433  POST_COMMIT_MSR = 0x00000002,
434  POST_COMMIT_CR = 0x00000004,
435  POST_COMMIT_XCR = 0x00000008,
436  POST_COMMIT_DTR = 0x00000010,
437  POST_INJECT_PF = 0x00000100,
438  POST_RETRY_PERFAGENT = 0x00000200,
440 
441 //
442 // GUESTS related API
443 //
444 INTSTATUS
446  _Out_ PGUEST_INFO GuestInfo
447  );
448 
449 INTSTATUS
451  _In_ DWORD Options
452  );
453 
454 void
456  _In_ QWORD NewOptions
457  );
458 
459 void
461  _In_ QWORD NewOptions
462  );
463 
464 INTSTATUS
466  _In_ QWORD Options
467  );
468 
469 void
471  void
472  );
473 
474 void
476  void
477  );
478 
479 INTSTATUS
481  _In_ QWORD Flags
482  );
483 
484 INTSTATUS
486  _Out_ QWORD *MaxGpa
487  );
488 
489 void
491  _In_ INTRO_ERROR_STATE State,
493  );
494 
497  void
498  );
499 
502  void
503  );
504 
505 BOOLEAN
507  void
508  );
509 
510 
523  _In_ QWORD Address,
524  _Out_ QWORD *IdtBase,
525  _Out_ QWORD *IdtLimit
526  )
527 {
528  for (DWORD cpuIndex = 0; cpuIndex < gGuest.CpuCount; cpuIndex++)
529  {
530  if (gGuest.VcpuArray[cpuIndex].IdtBase <= Address &&
531  Address <= gGuest.VcpuArray[cpuIndex].IdtBase + gGuest.VcpuArray[cpuIndex].IdtLimit)
532  {
533  *IdtBase = gGuest.VcpuArray[cpuIndex].IdtBase;
534  *IdtLimit = gGuest.VcpuArray[cpuIndex].IdtLimit;
535 
536  return INT_STATUS_SUCCESS;
537  }
538  }
539 
540  return INT_STATUS_NOT_FOUND;
541 }
542 
543 #endif // _GUESTS_H_
CPU_STATE
The various states in which a VCPU can be.
Definition: guests.h:18
#define _In_opt_
Definition: intro_sal.h:16
QWORD GdtBase
Original GDT base.
Definition: guests.h:112
#define _Out_
Definition: intro_sal.h:22
_Bool BOOLEAN
Definition: intro_types.h:58
DWORD EptpIndex
The index of the current loaded EPT.
Definition: guests.h:196
BOOLEAN SupportDTR
Set to True if support for DTR access exits was detected.
Definition: guests.h:354
struct _PTEMU_BUFFER * PPTEMU_BUFFER
void IntGuestSetIntroErrorState(INTRO_ERROR_STATE State, INTRO_ERROR_CONTEXT *Context)
Updates the value of the gErrorState and the value of the gErrorStateContext.
Definition: guests.c:88
Commit all the MSR hooks.
Definition: guests.h:433
Handling MSR violation.
Definition: guests.h:23
uint8_t BYTE
Definition: intro_types.h:47
BYTE Vector
The injected exception number.
Definition: guests.h:133
BOOLEAN Terminating
Definition: guests.h:310
BOOLEAN PtContext
Set to True if we are in the context of a PT filter VMCALL.
Definition: guests.h:178
WINDOWS_GUEST * gWinGuest
Global variable holding the state of a Windows guest.
Definition: winguest.c:35
IG_ARCH_REGS Regs
The current state of the guest registers.
Definition: guests.h:95
DWORD Index
The VCPU number.
Definition: guests.h:172
SHEMU_CONTEXT Shemucontext
Shellcode emulator context.
Definition: guests.h:402
BOOLEAN Initialized
True if the VCPU is initialized and used by the guest, False if it is not.
Definition: guests.h:199
#define _In_
Definition: intro_sal.h:21
INTSTATUS IntGuestDisableIntro(QWORD Flags)
Disables and unloads the introspection engine.
Definition: guests.c:1201
Handling an event injection.
Definition: guests.h:30
QWORD SystemCr3
The Cr3 used to map the kernel.
Definition: guests.h:207
#define INT_STATUS_SUCCESS
Definition: introstatus.h:54
Up & running.
Definition: guests.h:21
void * IdtIntegrityObject
The integrity region used to protect the IDT.
Definition: guests.h:159
uint16_t WORD
Definition: intro_types.h:48
QWORD Efer
Definition: guests.h:210
DWORD KernelSize
The size of the kernel.
Definition: guests.h:280
CR_HOOK_STATE * CrHooks
CR hook state.
Definition: guests.h:385
struct _GUEST_STATE GUEST_STATE
Describes a guest.
WORD IdtLimit
The current IDT limit.
Definition: guests.h:111
BOOLEAN SysprocBetaDetections
Definition: guests.h:300
BOOLEAN RepOptDisabled
The state of the rep optimization feature.
Definition: guests.h:198
BOOLEAN Initialized
True if this structure was initialized and can be used.
Definition: guests.h:285
QWORD Feedback
Options that will be forced to feedback only mode.
Definition: guests.h:251
BOOLEAN ShutDown
True if the system process protection is in beta (log-only) mode.
Definition: guests.h:309
PRE_RET_OPTIONS
Flags that control the behavior of IntGuestPreReturnCallback.
Definition: guests.h:430
struct _PTWRITE_CACHE PTWRITE_CACHE
Will contain the last successfully written page-table entry. This will be used by newly placed hooks ...
BOOLEAN ProtectionActivated
Definition: guests.h:293
#define SHEMU_SHELLCODE_SIZE
The shell code buffer size. It should be at least 2 pages in size.
Definition: guests.h:72
void * IdtHookObject
The EPT hook object used to protect the IDT.
Definition: guests.h:155
QWORD ExitAccess
The access type for which the EPT violation was generated.
Definition: guests.h:100
struct _VCPU_STATE VCPU_STATE
Structure encapsulating VCPU-specific information.
BOOLEAN SafeToApplyOptions
True if the current options can be changed dynamically.
Definition: guests.h:290
void IntGuestPrepareUninit(void)
Prepares introcore to be unloaded.
Definition: guests.c:982
Handling a CR load.
Definition: guests.h:25
QWORD Gla
The guest linear address for which the buffer is filled.
Definition: guests.h:40
int INTSTATUS
The status data type.
Definition: introstatus.h:24
BOOLEAN Partial
True if the write is partial and not the entire page table entry is modified.
Definition: guests.h:55
DWORD OSVersion
Os version.
Definition: guests.h:277
#define INT_STATUS_NOT_FOUND
Definition: introstatus.h:284
BOOLEAN BootstrapAgentAllocated
True if the slack space for the bootstrap agent has been allocated.
Definition: guests.h:328
BOOLEAN SupportVMFUNC
Set to True if support for VMFUNC was detected.
Definition: guests.h:352
QWORD IntroActiveEventId
The event ID on which introcore became active.
Definition: guests.h:377
BOOLEAN Valid
Definition: guests.h:68
WINDOWS_GUEST _WindowsGuest
Linux specific information. Valid when OSType is introGuestWindows.
Definition: guests.h:416
PVCPU_STATE VcpuArray
Array of the VCPUs assigned to this guest. The index in this array matches the VCPU number...
Definition: guests.h:368
BOOLEAN Valid
True if Data is valid, False if it is not.
Definition: guests.h:42
INTRO_GUEST_TYPE OSType
The type of the guest.
Definition: guests.h:274
BOOLEAN VeAgentWaiting
True if the #VE agent was not yet injected, but it should be.
Definition: guests.h:348
INSTRUX Instruction
The current instruction, pointed by the guest RIP.
Definition: guests.h:88
Commit all the memory hooks.
Definition: guests.h:432
BOOLEAN Emulated
True if the access was already emulated; False if it was not emulated.
Definition: guests.h:54
QWORD Cr4
Cr4 value used when deducing the paging mode.
Definition: guests.h:208
struct _GUEST_STATE * PGUEST_STATE
PVECPU VeInfoPage
Pointer to the VEINFO page used for this VCPU.
Definition: guests.h:190
struct _MM * PMM
Describes a kernel driver.
Definition: drivers.h:30
QWORD Beta
Options that were forced to beta (log-only) mode.
Definition: guests.h:244
MSR_HOOK_STATE * MsrHooks
MSR hook state.
Definition: guests.h:383
struct _INTRO_PROT_OPTIONS INTRO_PROT_OPTIONS
Describes options for this guest.
BOOLEAN PtFilterWaiting
True if the in-guest PT filter was not yet injected, but it should be.
Definition: guests.h:341
DWORD AccessSize
The size of the memory access. Valid only for EPT exits.
Definition: guests.h:103
BOOLEAN IntGuestShouldNotifyErrorState(void)
Checks if an event should be sent to the integrator.
Definition: guests.c:133
BOOLEAN KptiActive
True if KPTI is enabled on this guest, False if it is not.
Definition: guests.h:287
Commit all the CR hooks.
Definition: guests.h:434
struct _PATCH_BUFFER * PPATCH_BUFFER
This file is common between the VE driver and the introspection engine.
BOOLEAN SupportSPP
Set to True if support for SPP was detected.
Definition: guests.h:353
INTSTATUS IntGuestGetLastGpa(QWORD *MaxGpa)
Get the upper limit of the guest physical memory range.
Definition: guests.c:1715
No state.
Definition: guests.h:20
The context of an error state.
Definition: intro_types.h:2252
EXCEPTIONS * Exceptions
The exceptions that are currently loaded.
Definition: guests.h:388
INTSTATUS IntGuestPreReturnCallback(DWORD Options)
Handles all the operations that must be done before returning from a VMEXIT event handler...
Definition: guests.c:1278
struct _VCPU_STATE * PVCPU_STATE
BOOLEAN PaeEnabled
True if Physical Address Extension is enabled.
Definition: guests.h:291
QWORD New
The new, to be written, value of the page table entry.
Definition: guests.h:52
XCR_HOOK_STATE * XcrHooks
XCR hook state.
Definition: guests.h:384
CPU_STATE State
The state of this VCPU. Describes what action is the VCPU currently doing.
Definition: guests.h:173
Will contain the last successfully written page-table entry. This will be used by newly placed hooks ...
Definition: guests.h:64
Reinject the #VE or PT filtering agent, based on the active options.
Definition: guests.h:438
BOOLEAN Guest64
True if this is a 64-bit guest, False if it is a 32-bit guest.
Definition: guests.h:286
Describes a Linux guest.
Definition: lixguest.h:473
unsigned long long QWORD
Definition: intro_types.h:53
QWORD Current
The currently used options.
Definition: guests.h:232
Handling a LIDT or LGDT.
Definition: guests.h:26
QWORD Old
The old, original, value of the written page table entry.
Definition: guests.h:51
struct _PTWRITE_CACHE * PPTWRITE_CACHE
PTWRITE_CACHE PtWriteCache
The last written PT entry.
Definition: guests.h:170
Handling a breakpoint (int3).
Definition: guests.h:29
Contains information about the buffer used to emulate page table writes.
Definition: guests.h:49
QWORD IdtBase
Original IDT base.
Definition: guests.h:110
struct _PATCH_BUFFER PATCH_BUFFER
Contains information about the patch buffer.
Structure encapsulating VCPU-specific information.
Definition: guests.h:83
void * GpaCache
The currently used GPA cache.
Definition: guests.h:399
BOOLEAN GuestInitialized
True if the OS-specific portion has been initialized.
Definition: guests.h:289
QWORD Gpa
The accessed guest physical address. Valid only for EPT exits.
Definition: guests.h:101
INFO_UD_PENDING * CurrentUD
The currently pending #UD injection on this CPU.
Definition: guests.h:123
Contains information about the patch buffer.
Definition: guests.h:38
Describes the internal exceptions data.
Definition: exceptions.h:87
QWORD ExitGpa
The accessed guest physical address, for which the EPT violation was generated.
Definition: guests.h:98
QWORD KernelVa
The guest virtual address at which the kernel image.
Definition: guests.h:279
struct _PTEMU_BUFFER PTEMU_BUFFER
Contains information about the buffer used to emulate page table writes.
BYTE WordSize
Guest word size. Will be 4 for 32-bit guests and 8 for 64-bit guests.
Definition: guests.h:363
DWORD RepOptsDisableCount
The number of times the rep optimizations have been disabled.
Definition: guests.h:379
QWORD Cr2
The Cr2. Valid only if Vector is 14 (Page Fault)
Definition: guests.h:135
Handling XSETBV.
Definition: guests.h:28
INTRO_ERROR_STATE IntGuestGetIntroErrorState(void)
Gets the last reported error-state.
Definition: guests.c:105
BOOLEAN UninitPrepared
Definition: guests.h:316
QWORD Xcr0
The value of XCR0. Updated by IntHandleXcrWrite.
Definition: guests.h:114
DWORD SelfMapIndex
The self map index.
Definition: guests.h:216
INTSTATUS IntGuestGetIdtFromGla(QWORD Address, QWORD *IdtBase, QWORD *IdtLimit)
Checks if an address is inside one of the guest&#39;s IDTs.
Definition: guests.h:522
QWORD ForceOff
Options that are forcibly disabled.
Definition: guests.h:238
DTR_HOOK_STATE * DtrHooks
DTR hook state.
Definition: guests.h:386
DWORD CpuCount
The number of logical CPUs.
Definition: guests.h:275
INTRO_ERROR_CONTEXT * IntGuestGetIntroErrorStateContext(void)
Gets the last reported error-context appropriate to the error-state.
Definition: guests.c:119
QWORD Value
Definition: guests.h:67
INTRO_GUEST_TYPE
The type of the introspected operating system.
Definition: intro_types.h:1877
void * InstructionCache
The currently used instructions cache.
Definition: guests.h:400
#define __forceinline
Definition: introtypes.h:61
uint32_t DWORD
Definition: intro_types.h:49
void IntGuestUninit(void)
Completely unloads the introspection engine.
Definition: guests.c:1034
struct _MM MM
Memory information structure.
Memory information structure.
Definition: guests.h:205
DWORD ProtectedEptIndex
The EPTP index of the trusted EPT.
Definition: guests.h:397
void IntGuestUpdateShemuOptions(QWORD NewOptions)
Update shemu options.
Definition: guests.c:1395
BOOLEAN VeInitialized
Set to True if #VE initialization was done.
Definition: guests.h:349
#define SHEMU_STACK_SIZE
The size of the stack buffer used by shemu.
Definition: guests.h:73
PAGING_MODE
Paging modes.
Definition: introcore.h:66
Holds information about a Windows guest.
Definition: winguest.h:796
QWORD Ia32Efer
The value of the guest IA32 EFER MSR.
Definition: guests.h:96
QWORD Cr0
Cr0 value used when deducing the paging mode.
Definition: guests.h:209
MM Mm
Guest memory information, such as paging mode, system Cr3 value, etc.
Definition: guests.h:370
PATCH_BUFFER PatchBuffer
The patch buffer used to emulate reads.
Definition: guests.h:168
GUEST_STATE gGuest
The current guest state.
Definition: guests.c:48
BOOLEAN VeContext
Set to True if we are in the context of the #VE agent.
Definition: guests.h:184
QWORD ExitGla
The accessed guest linear address, for which the EPT violation was generated.
Definition: guests.h:99
QWORD PteAddress
Definition: guests.h:66
INTRO_ERROR_STATE
Error states.
Definition: intro_types.h:2270
DWORD ErrorCode
The error code, for exceptions that have an error code.
Definition: guests.h:134
Guest information.
Definition: intro_types.h:2214
QWORD TimerCalls
The number of times the timer callback has been invoked.
Definition: guests.h:270
Handling a timer event.
Definition: guests.h:27
INTRO_PROT_OPTIONS ShemuOptions
Flags which describe the way shemu will give detections.
Definition: guests.h:268
Commit all the XCR hooks.
Definition: guests.h:435
LINUX_GUEST _LinuxGuest
Linux specific information. Valid when OSType is introGuestLinux.
Definition: guests.h:415
BOOLEAN KptiInstalled
True if KPTI was detected as installed (not necessarily active).
Definition: guests.h:288
QWORD Original
The original options as received from GLUE_IFACE.NewGuestNotification. This is updated when GLUE_IFAC...
Definition: guests.h:227
BOOLEAN SupportVE
Set to True if support for #VE was detected.
Definition: guests.h:351
BOOLEAN LA57
True if 5-level paging is being used.
Definition: guests.h:292
PAGING_MODE Mode
The paging mode used by the guest.
Definition: guests.h:217
DWORD Size
The valid size of the Data buffer.
Definition: guests.h:41
QWORD TscSpeed
Number of ticks/second of this given guest. Should be the same as the global (physical) one...
Definition: guests.h:272
KERNEL_DRIVER * KernelDriver
Points to the driver object that describes the kernel image.
Definition: guests.h:381
BOOLEAN Valid
True if the fields are valid; False if they are not.
Definition: guests.h:132
VCPU_STATE * gVcpu
The state of the current VCPU.
Definition: guests.c:57
void IntGuestUpdateCoreOptions(QWORD NewOptions)
Updates Introcore options.
Definition: guests.c:1424
DWORD ActiveCpuCount
The number of CPUs actually used by the guest.
Definition: guests.h:276
Holds register state.
Definition: glueiface.h:30
Describes options for this guest.
Definition: guests.h:223
BOOLEAN BugCheckInProgress
Definition: guests.h:329
BOOLEAN PtFilterFlagRemoved
Set to True if the INTRO_OPT_IN_GUEST_PT_FILTER was given, but it was removed.
Definition: guests.h:361
BOOLEAN Valid
True if the information in this structure is valid; False it it is not.
Definition: guests.h:53
QWORD LixProcessGva
The guest virtual address of the running task on the current vCPU (valid only for Linux / thread safe...
Definition: guests.h:166
Describes a guest.
Definition: guests.h:265
INTSTATUS IntGuestGetInfo(PGUEST_INFO GuestInfo)
Get basic information about the guest.
Definition: guests.c:348
PTEMU_BUFFER PtEmuBuffer
The page table write emulator buffer.
Definition: guests.h:169
QWORD PcrGla
The guest linear address of the _KPCR structure loaded by this CPU.
Definition: guests.h:108
DWORD UntrustedEptIndex
The EPTP index of the untrusted EPT.
Definition: guests.h:393
BYTE Data[ND_MAX_REGISTER_SIZE]
The actual contents of the buffer.
Definition: guests.h:43
INTRO_PROT_OPTIONS CoreOptions
The activation and protection options for this guest.
Definition: guests.h:267
QWORD Gla
The accessed guest virtual address. Valid only for EPT exits.
Definition: guests.h:102
QWORD EventId
EventId for which VCPU_STATE.Regs is valid.
Definition: guests.h:97
LINUX_GUEST * gLixGuest
Global variable holding the state of a Linux guest.
Definition: lixguest.c:29
Inject pending page faults.
Definition: guests.h:437
Commit all the DTR hooks.
Definition: guests.h:436
Handling a VMCALL.
Definition: guests.h:24
INTSTATUS IntGuestInit(QWORD Options)
Initialize the given guest state.
Definition: guests.c:753
Handling EPT violation.
Definition: guests.h:22