1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
util.c
Abstract:
Utility routines for the sample driver.
Environment:
Kernel mode only
--*/
#include "regfltr.h"
FAST_MUTEX g_CallbackCtxListLock;
LIST_ENTRY g_CallbackCtxListHead;
USHORT g_NumCallbackCtxListEntries;
ULONG
ExceptionFilter (
_In_ PEXCEPTION_POINTERS ExceptionPointers
)
/*++
Routine Description:
ExceptionFilter breaks into the debugger if an exception happens
inside the callback.
Arguments:
ExceptionPointers - unused
Return Value:
Always returns EXCEPTION_CONTINUE_SEARCH
--*/
{
ErrorPrint("Exception %lx, ExceptionPointers = %p",
ExceptionPointers->ExceptionRecord->ExceptionCode,
ExceptionPointers);
DbgBreakPoint();
return EXCEPTION_EXECUTE_HANDLER;
}
PVOID
CreateCallbackContext(
_In_ CALLBACK_MODE CallbackMode,
_In_ PCWSTR AltitudeString
)
/*++
Routine Description:
Utility method to create a callback context. Callback context
should be freed using DeleteCallbackContext.
Arguments:
CallbackMode - the callback mode value
AltitudeString - a string with the altitude the callback will be
registered at
Return Value:
Pointer to the allocated and initialized callback context
--*/
{
PCALLBACK_CONTEXT CallbackCtx = NULL;
NTSTATUS Status;
BOOLEAN Success = FALSE;
CallbackCtx = (PCALLBACK_CONTEXT) ExAllocatePoolZero (
PagedPool,
sizeof(CALLBACK_CONTEXT),
REGFLTR_CONTEXT_POOL_TAG);
if (CallbackCtx == NULL) {
ErrorPrint("CreateCallbackContext failed due to insufficient resources.");
goto Exit;
}
CallbackCtx->CallbackMode = CallbackMode;
CallbackCtx->ProcessId = PsGetCurrentProcessId();
Status = RtlStringCbPrintfW(CallbackCtx->AltitudeBuffer,
MAX_ALTITUDE_BUFFER_LENGTH * sizeof(WCHAR),
L"%s",
AltitudeString);
if (!NT_SUCCESS(Status)) {
ErrorPrint("RtlStringCbPrintfW in CreateCallbackContext failed. Status 0x%x", Status);
goto Exit;
}
RtlInitUnicodeString (&CallbackCtx->Altitude, CallbackCtx->AltitudeBuffer);
Success = TRUE;
Exit:
if (Success == FALSE) {
if (CallbackCtx != NULL) {
ExFreePoolWithTag(CallbackCtx, REGFLTR_CONTEXT_POOL_TAG);
CallbackCtx = NULL;
}
}
return CallbackCtx;
}
BOOLEAN
InsertCallbackContext(
_In_ PCALLBACK_CONTEXT CallbackCtx
)
/*++
Routine Description:
Utility method to insert the callback context into a list.
Arguments:
CallbackCtx - the callback context to insert
Return Value:
TRUE if successful, FALSE otherwise
--*/
{
BOOLEAN Success = FALSE;
ExAcquireFastMutex(&g_CallbackCtxListLock);
if (g_NumCallbackCtxListEntries < MAX_CALLBACK_CTX_ENTRIES) {
g_NumCallbackCtxListEntries++;
InsertHeadList(&g_CallbackCtxListHead, &CallbackCtx->CallbackCtxList);
Success = TRUE;
} else {
ErrorPrint("Insert Callback Ctx failed: Max CallbackCtx entries reached.");
}
ExReleaseFastMutex(&g_CallbackCtxListLock);
return Success;
}
PCALLBACK_CONTEXT
FindCallbackContext(
_In_ LARGE_INTEGER Cookie
)
/*++
Routine Description:
Utility method to find a callback context using the cookie value.
Arguments:
Cookie - the cookie value associated with the callback context. The
cookie is returned when CmRegisterCallbackEx is called.
Return Value:
Pointer to the found callback context
--*/
{
PCALLBACK_CONTEXT CallbackCtx = NULL;
PLIST_ENTRY Entry;
ExAcquireFastMutex(&g_CallbackCtxListLock);
Entry = g_CallbackCtxListHead.Flink;
while (Entry != &g_CallbackCtxListHead) {
CallbackCtx = CONTAINING_RECORD(Entry,
CALLBACK_CONTEXT,
CallbackCtxList);
if (CallbackCtx->Cookie.QuadPart == Cookie.QuadPart) {
break;
}
Entry = Entry->Flink;
}
ExReleaseFastMutex(&g_CallbackCtxListLock);
if (CallbackCtx == NULL) {
ErrorPrint("FindCallbackContext failed: No context with specified cookied was found.");
}
return CallbackCtx;
}
PCALLBACK_CONTEXT
FindAndRemoveCallbackContext(
_In_ LARGE_INTEGER Cookie
)
/*++
Routine Description:
Utility method to find a callback context using the cookie value and then
remove it.
Arguments:
Cookie - the cookie value associated with the callback context. The
cookie is returned when CmRegisterCallbackEx is called.
Return Value:
Pointer to the found callback context
--*/
{
PCALLBACK_CONTEXT CallbackCtx = NULL;
PLIST_ENTRY Entry;
ExAcquireFastMutex(&g_CallbackCtxListLock);
Entry = g_CallbackCtxListHead.Flink;
while (Entry != &g_CallbackCtxListHead) {
CallbackCtx = CONTAINING_RECORD(Entry,
CALLBACK_CONTEXT,
CallbackCtxList);
if (CallbackCtx->Cookie.QuadPart == Cookie.QuadPart) {
RemoveEntryList(&CallbackCtx->CallbackCtxList);
g_NumCallbackCtxListEntries--;
break;
}
}
ExReleaseFastMutex(&g_CallbackCtxListLock);
if (CallbackCtx == NULL) {
ErrorPrint("FindAndRemoveCallbackContext failed: No context with specified cookied was found.");
}
return CallbackCtx;
}
VOID
DeleteCallbackContext(
_In_ PCALLBACK_CONTEXT CallbackCtx
)
/*++
Routine Description:
Utility method to delete a callback context.
Arguments:
CallbackCtx - the callback context to insert
Return Value:
None
--*/
{
if (CallbackCtx != NULL) {
ExFreePoolWithTag(CallbackCtx, REGFLTR_CONTEXT_POOL_TAG);
}
}
|