Bitdefender Hypervisor Memory Introspection
hook_dtr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "hook_dtr.h"
6 #include "callbacks.h"
7 #include "guests.h"
8 
9 
12  _In_ DWORD Flags,
14  _Out_opt_ void **Hook
15  )
38 {
39  if (Callback == NULL)
40  {
42  }
43 
44  HOOK_DTR *pHook = HpAllocWithTag(sizeof(*pHook), IC_TAG_DTRH);
45  if (NULL == pHook)
46  {
48  }
49 
50  pHook->Flags = Flags;
51  pHook->Callback = Callback;
52 
53  if (0 == gGuest.DtrHooks->HooksCount++)
54  {
56  if (!INT_SUCCESS(status))
57  {
59  return status;
60  }
61  }
62 
64 
65  if (NULL != Hook)
66  {
67  *Hook = pHook;
68  }
69 
70  return INT_STATUS_SUCCESS;
71 }
72 
73 
74 static INTSTATUS
76  _In_ HOOK_DTR *Hook
77  )
88 {
89  if (!Hook->Disabled)
90  {
92  }
93 
95 
96  if (0 >= --gGuest.DtrHooks->HooksCount)
97  {
99  }
100 
101  return INT_STATUS_SUCCESS;
102 }
103 
104 
105 INTSTATUS
107  _In_ HOOK_DTR *Hook
108  )
121 {
122  if (NULL == Hook)
123  {
125  }
126 
127  Hook->Disabled = TRUE;
128 
129  // If we're not handling an DTR violation right now than we can safely delete the DTR hook.
131  {
132  RemoveEntryList(&Hook->Link);
133 
134  INTSTATUS status = IntHookDtrDeleteHook(Hook);
135  if (!INT_SUCCESS(status))
136  {
137  ERROR("[ERROR] IntHookDtrDeleteHook failed: 0x%08x\n", status);
138  }
139  }
140 
141  return INT_STATUS_SUCCESS;
142 }
143 
144 
145 static void
147  void
148  )
152 {
154  {
155  INTSTATUS status = IntHookDtrRemoveHook(pHook);
156  if (!INT_SUCCESS(status))
157  {
158  ERROR("[ERROR] IntHookDtrRemoveHook failed: 0x%08x\n", status);
159  }
160  }
161 }
162 
163 
164 INTSTATUS
166  void
167  )
176 {
177  INTSTATUS status = INT_STATUS_SUCCESS;
178 
179  if (NULL == gGuest.DtrHooks)
180  {
182  }
183 
185  {
186  if (pHook->Disabled)
187  {
188  RemoveEntryList(&pHook->Link);
189 
190  status = IntHookDtrDeleteHook(pHook);
191  if (!INT_SUCCESS(status))
192  {
193  ERROR("[ERROR] IntHookDtrDeleteHook failed: 0x%08x\n", status);
194  }
195  }
196  }
197 
198  return status;
199 }
200 
201 
202 INTSTATUS
204  void
205  )
212 {
214  if (NULL == gGuest.DtrHooks)
215  {
217  }
218 
220 
221  return INT_STATUS_SUCCESS;
222 }
223 
224 
225 INTSTATUS
227  void
228  )
235 {
236  if (NULL == gGuest.DtrHooks)
237  {
239  }
240 
242 
244 
245  return INT_STATUS_SUCCESS;
246 }
INTSTATUS IntHookDtrSetHook(DWORD Flags, PFUNC_DtrReadWriteHookCallback Callback, void **Hook)
Places a descriptor table register hook.
Definition: hook_dtr.c:11
#define _In_
Definition: intro_sal.h:21
INT64 HooksCount
The total number of DTR hooks.
Definition: hook_dtr.h:35
#define INT_STATUS_SUCCESS
Definition: introstatus.h:54
#define IC_TAG_DTRS
IDTR & GDTR hook state.
Definition: memtags.h:73
LIST_ENTRY Link
List entry element.
Definition: hook_dtr.h:44
DWORD Flags
Hook flags, a combination of IG_DESC_ACCESS.
Definition: hook_dtr.h:45
#define INT_SUCCESS(Status)
Definition: introstatus.h:42
#define ERROR(fmt,...)
Definition: glue.h:62
PFUNC_DtrReadWriteHookCallback Callback
The callback.
Definition: hook_dtr.h:47
#define HpAllocWithTag(Len, Tag)
Definition: glue.h:516
int INTSTATUS
The status data type.
Definition: introstatus.h:24
static void IntHookDtrRemoveAllHooks(void)
Remove all descriptor register hooks.
Definition: hook_dtr.c:146
INTSTATUS IntHookDtrInit(void)
Initialize the descriptor registers hook state.
Definition: hook_dtr.c:203
#define _Out_opt_
Definition: intro_sal.h:30
#define INT_STATUS_NOT_INITIALIZED
Definition: introstatus.h:266
static BOOLEAN RemoveEntryList(LIST_ENTRY *Entry)
Definition: introlists.h:87
CPU_STATE State
The state of this VCPU. Describes what action is the VCPU currently doing.
Definition: guests.h:173
Handling a LIDT or LGDT.
Definition: guests.h:26
#define TRUE
Definition: intro_types.h:30
#define HpFreeAndNullWithTag(Add, Tag)
Definition: glue.h:517
INTSTATUS IntHookDtrUninit(void)
Uninit the descriptor registers hooks state.
Definition: hook_dtr.c:226
INTSTATUS IntHookDtrRemoveHook(HOOK_DTR *Hook)
Remove a descriptor register hook.
Definition: hook_dtr.c:106
static void InsertTailList(LIST_ENTRY *ListHead, LIST_ENTRY *Entry)
Definition: introlists.h:135
DTR_HOOK_STATE * DtrHooks
DTR hook state.
Definition: guests.h:390
static void InitializeListHead(LIST_ENTRY *ListHead)
Definition: introlists.h:69
INTSTATUS IntHookDtrCommit(void)
Commit the descriptor registers hooks.
Definition: hook_dtr.c:165
static INTSTATUS IntDisableDtrNotifications(void)
Definition: callbacks.h:142
uint32_t DWORD
Definition: intro_types.h:49
INTSTATUS(* PFUNC_DtrReadWriteHookCallback)(DTR *OldDtr, DTR *NewDtr, DWORD Flags, INTRO_ACTION *Action)
Called when a descriptor table register is accessed.
Definition: hook_dtr.h:21
GUEST_STATE gGuest
The current guest state.
Definition: guests.c:50
static INTSTATUS IntHookDtrDeleteHook(HOOK_DTR *Hook)
Permanently delete a descriptor register hook.
Definition: hook_dtr.c:75
#define INT_STATUS_NOT_INITIALIZED_HINT
Definition: introstatus.h:320
#define INT_STATUS_INVALID_PARAMETER_1
Definition: introstatus.h:62
VCPU_STATE * gVcpu
The state of the current VCPU.
Definition: guests.c:59
static INTSTATUS IntEnableDtrNotifications(void)
Definition: callbacks.h:125
#define list_for_each(_head, _struct_type, _var)
Definition: introlists.h:41
LIST_HEAD DtrHooksList
The list of DTR hooks.
Definition: hook_dtr.h:34
#define IC_TAG_DTRH
IDTR & GDTR hook.
Definition: memtags.h:72
#define INT_STATUS_INSUFFICIENT_RESOURCES
Definition: introstatus.h:281
#define INT_STATUS_INVALID_PARAMETER_3
Definition: introstatus.h:68