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 
205 
209 typedef struct _MM
210 {
215  QWORD LastGpa;
222 } MM, *PMM;
223 
227 typedef struct _INTRO_PROT_OPTIONS
228 {
237 
257 
258 // Forward these declarations, makes it easy to use it directly when needed
264 typedef struct _EXCEPTIONS EXCEPTIONS;
265 
269 typedef struct _GUEST_STATE
270 {
273 
275 
277 
282 
285 
298  BOOLEAN KernelBetaDetections;
315  BOOLEAN EnterHibernate;
321  BOOLEAN DisableOnReturn;
334  BOOLEAN PtFilterEnabled;
354 
359 
366 
368 
372  PVCPU_STATE VcpuArray;
373 
374  MM Mm;
375 
382 
384 
386 
391 
393 
402 
403  void *GpaCache;
405 
406  SHEMU_CONTEXT Shemucontext;
407 
409  BYTE ShemuShellcode[SHEMU_SHELLCODE_SIZE];
411  BYTE ShemuStack[SHEMU_STACK_SIZE];
414 
417  union
418  {
421  };
423 
424 
425 extern GUEST_STATE gGuest;
426 extern WINDOWS_GUEST *gWinGuest;
427 extern LINUX_GUEST *gLixGuest;
428 
429 extern VCPU_STATE *gVcpu;
430 
434 typedef enum
435 {
436  POST_COMMIT_MEM = 0x00000001,
437  POST_COMMIT_MSR = 0x00000002,
438  POST_COMMIT_CR = 0x00000004,
439  POST_COMMIT_XCR = 0x00000008,
440  POST_COMMIT_DTR = 0x00000010,
441  POST_INJECT_PF = 0x00000100,
442  POST_RETRY_PERFAGENT = 0x00000200,
444 
445 //
446 // GUESTS related API
447 //
448 INTSTATUS
450  _Out_ PGUEST_INFO GuestInfo
451  );
452 
453 INTSTATUS
455  _In_ DWORD Options
456  );
457 
458 void
460  _In_ QWORD NewOptions
461  );
462 
463 void
465  _In_ QWORD NewOptions
466  );
467 
468 INTSTATUS
470  _In_ QWORD Options
471  );
472 
473 void
475  void
476  );
477 
478 void
480  void
481  );
482 
483 INTSTATUS
485  _In_ QWORD Flags
486  );
487 
488 INTSTATUS
490  _Out_ QWORD *MaxGpa
491  );
492 
493 void
495  _In_ INTRO_ERROR_STATE State,
497  );
498 
501  void
502  );
503 
506  void
507  );
508 
509 BOOLEAN
511  void
512  );
513 
514 
527  _In_ QWORD Address,
528  _Out_ QWORD *IdtBase,
529  _Out_ QWORD *IdtLimit
530  )
531 {
532  for (DWORD cpuIndex = 0; cpuIndex < gGuest.CpuCount; cpuIndex++)
533  {
534  if (gGuest.VcpuArray[cpuIndex].IdtBase <= Address &&
535  Address <= gGuest.VcpuArray[cpuIndex].IdtBase + gGuest.VcpuArray[cpuIndex].IdtLimit)
536  {
537  *IdtBase = gGuest.VcpuArray[cpuIndex].IdtBase;
538  *IdtLimit = gGuest.VcpuArray[cpuIndex].IdtLimit;
539 
540  return INT_STATUS_SUCCESS;
541  }
542  }
543 
544  return INT_STATUS_NOT_FOUND;
545 }
546 
547 #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:358
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:90
Commit all the MSR hooks.
Definition: guests.h:437
Handling MSR violation.
Definition: guests.h:23
uint8_t BYTE
Definition: intro_types.h:47
BOOLEAN SingleStep
True if th VCPU is currently single-stepping the current instruction.
Definition: guests.h:200
BYTE Vector
The injected exception number.
Definition: guests.h:133
BOOLEAN Terminating
Definition: guests.h:314
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:37
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:406
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:1203
Handling an event injection.
Definition: guests.h:30
QWORD SystemCr3
The Cr3 used to map the kernel.
Definition: guests.h:211
#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:214
DWORD KernelSize
The size of the kernel.
Definition: guests.h:284
CR_HOOK_STATE * CrHooks
CR hook state.
Definition: guests.h:389
struct _GUEST_STATE GUEST_STATE
Describes a guest.
WORD IdtLimit
The current IDT limit.
Definition: guests.h:111
BOOLEAN SysprocBetaDetections
Definition: guests.h:304
QWORD AllowOnExecRip
The RIP which was allowed to execute on an exec violation.
Definition: guests.h:202
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:289
QWORD Feedback
Options that will be forced to feedback only mode.
Definition: guests.h:255
BOOLEAN ShutDown
True if the system process protection is in beta (log-only) mode.
Definition: guests.h:313
PRE_RET_OPTIONS
Flags that control the behavior of IntGuestPreReturnCallback.
Definition: guests.h:434
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:297
#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:294
void IntGuestPrepareUninit(void)
Prepares introcore to be unloaded.
Definition: guests.c:984
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:281
BOOLEAN AllowOnExec
True if we returned introGuestAllowed on an execution alert.
Definition: guests.h:201
#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:332
BOOLEAN SupportVMFUNC
Set to True if support for VMFUNC was detected.
Definition: guests.h:356
QWORD IntroActiveEventId
The event ID on which introcore became active.
Definition: guests.h:381
BOOLEAN Valid
Definition: guests.h:68
WINDOWS_GUEST _WindowsGuest
Linux specific information. Valid when OSType is introGuestWindows.
Definition: guests.h:420
PVCPU_STATE VcpuArray
Array of the VCPUs assigned to this guest. The index in this array matches the VCPU number...
Definition: guests.h:372
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:278
BOOLEAN VeAgentWaiting
True if the #VE agent was not yet injected, but it should be.
Definition: guests.h:352
INSTRUX Instruction
The current instruction, pointed by the guest RIP.
Definition: guests.h:88
Commit all the memory hooks.
Definition: guests.h:436
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:212
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:248
MSR_HOOK_STATE * MsrHooks
MSR hook state.
Definition: guests.h:387
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:345
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:135
BOOLEAN KptiActive
True if KPTI is enabled on this guest, False if it is not.
Definition: guests.h:291
Commit all the CR hooks.
Definition: guests.h:438
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:357
INTSTATUS IntGuestGetLastGpa(QWORD *MaxGpa)
Get the upper limit of the guest physical memory range.
Definition: guests.c:1735
No state.
Definition: guests.h:20
The context of an error state.
Definition: intro_types.h:2415
EXCEPTIONS * Exceptions
The exceptions that are currently loaded.
Definition: guests.h:392
QWORD AllowOnExecGpa
The GPA which was allowed to execute on an exec violation.
Definition: guests.h:203
INTSTATUS IntGuestPreReturnCallback(DWORD Options)
Handles all the operations that must be done before returning from a VMEXIT event handler...
Definition: guests.c:1280
struct _VCPU_STATE * PVCPU_STATE
BOOLEAN PaeEnabled
True if Physical Address Extension is enabled.
Definition: guests.h:295
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:388
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:442
BOOLEAN Guest64
True if this is a 64-bit guest, False if it is a 32-bit guest.
Definition: guests.h:290
Describes a Linux guest.
Definition: lixguest.h:476
unsigned long long QWORD
Definition: intro_types.h:53
QWORD Current
The currently used options.
Definition: guests.h:236
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:403
BOOLEAN GuestInitialized
True if the OS-specific portion has been initialized.
Definition: guests.h:293
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:86
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:283
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:367
DWORD RepOptsDisableCount
The number of times the rep optimizations have been disabled.
Definition: guests.h:383
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:107
BOOLEAN UninitPrepared
Definition: guests.h:320
QWORD Xcr0
The value of XCR0. Updated by IntHandleXcrWrite.
Definition: guests.h:114
DWORD SelfMapIndex
The self map index.
Definition: guests.h:220
INTSTATUS IntGuestGetIdtFromGla(QWORD Address, QWORD *IdtBase, QWORD *IdtLimit)
Checks if an address is inside one of the guest&#39;s IDTs.
Definition: guests.h:526
QWORD ForceOff
Options that are forcibly disabled.
Definition: guests.h:242
DTR_HOOK_STATE * DtrHooks
DTR hook state.
Definition: guests.h:390
DWORD CpuCount
The number of logical CPUs.
Definition: guests.h:279
INTRO_ERROR_CONTEXT * IntGuestGetIntroErrorStateContext(void)
Gets the last reported error-context appropriate to the error-state.
Definition: guests.c:121
QWORD Value
Definition: guests.h:67
INTRO_GUEST_TYPE
The type of the introspected operating system.
Definition: intro_types.h:2040
void * InstructionCache
The currently used instructions cache.
Definition: guests.h:404
#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:1036
struct _MM MM
Memory information structure.
Memory information structure.
Definition: guests.h:209
DWORD ProtectedEptIndex
The EPTP index of the trusted EPT.
Definition: guests.h:401
void IntGuestUpdateShemuOptions(QWORD NewOptions)
Update shemu options.
Definition: guests.c:1397
BOOLEAN VeInitialized
Set to True if #VE initialization was done.
Definition: guests.h:353
#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:810
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:213
MM Mm
Guest memory information, such as paging mode, system Cr3 value, etc.
Definition: guests.h:374
PATCH_BUFFER PatchBuffer
The patch buffer used to emulate reads.
Definition: guests.h:168
GUEST_STATE gGuest
The current guest state.
Definition: guests.c:50
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:2433
DWORD ErrorCode
The error code, for exceptions that have an error code.
Definition: guests.h:134
Guest information.
Definition: intro_types.h:2377
QWORD TimerCalls
The number of times the timer callback has been invoked.
Definition: guests.h:274
Handling a timer event.
Definition: guests.h:27
INTRO_PROT_OPTIONS ShemuOptions
Flags which describe the way shemu will give detections.
Definition: guests.h:272
Commit all the XCR hooks.
Definition: guests.h:439
LINUX_GUEST _LinuxGuest
Linux specific information. Valid when OSType is introGuestLinux.
Definition: guests.h:419
BOOLEAN KptiInstalled
True if KPTI was detected as installed (not necessarily active).
Definition: guests.h:292
QWORD Original
The original options as received from GLUE_IFACE.NewGuestNotification. This is updated when GLUE_IFAC...
Definition: guests.h:231
BOOLEAN SupportVE
Set to True if support for #VE was detected.
Definition: guests.h:355
BOOLEAN LA57
True if 5-level paging is being used.
Definition: guests.h:296
PAGING_MODE Mode
The paging mode used by the guest.
Definition: guests.h:221
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:276
KERNEL_DRIVER * KernelDriver
Points to the driver object that describes the kernel image.
Definition: guests.h:385
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:59
void IntGuestUpdateCoreOptions(QWORD NewOptions)
Updates Introcore options.
Definition: guests.c:1426
DWORD ActiveCpuCount
The number of CPUs actually used by the guest.
Definition: guests.h:280
Holds register state.
Definition: glueiface.h:30
Describes options for this guest.
Definition: guests.h:227
BOOLEAN BugCheckInProgress
Definition: guests.h:333
BOOLEAN PtFilterFlagRemoved
Set to True if the INTRO_OPT_IN_GUEST_PT_FILTER was given, but it was removed.
Definition: guests.h:365
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:269
INTSTATUS IntGuestGetInfo(PGUEST_INFO GuestInfo)
Get basic information about the guest.
Definition: guests.c:350
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:397
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:271
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:30
Inject pending page faults.
Definition: guests.h:441
Commit all the DTR hooks.
Definition: guests.h:440
Handling a VMCALL.
Definition: guests.h:24
INTSTATUS IntGuestInit(QWORD Options)
Initialize the given guest state.
Definition: guests.c:755
Handling EPT violation.
Definition: guests.h:22