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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
utility.c
Abstract:
Utility module implementation.
1) Generic table routines
2) Query file information routines
Environment:
Kernel mode
--*/
#include "avscan.h"
//
// Generic table routines.
//
RTL_GENERIC_COMPARE_RESULTS
NTAPI
AvCompareEntry (
_In_ PRTL_GENERIC_TABLE Table,
_In_ PVOID Lhs,
_In_ PVOID Rhs
)
/*++
Routine Description:
This routine is the callback for the generic table routines.
Arguments:
Table - Table for which this is invoked.
FirstStruct - An element in the table to compare.
SecondStruct - Another element in the table to compare.
Return Value:
RTL_GENERIC_COMPARE_RESULTS.
--*/
{
PAV_GENERIC_TABLE_ENTRY lhs = (PAV_GENERIC_TABLE_ENTRY)Lhs;
PAV_GENERIC_TABLE_ENTRY rhs = (PAV_GENERIC_TABLE_ENTRY)Rhs;
UNREFERENCED_PARAMETER (Table);
//
// Compare the 128 bit fileId in 64bit pieces for efficiency.
// Compare the lower 64 bits Value first since that is used
// in both 128 bit and 64 bit fileIds and doing so eliminates
// and unnecessary comparison of the UpperZeros field in the
// most common case. Note this comparison is not equivalent
// to a memcmp on the 128 bit values but that doesn't matter
// here since we just need the tree to be self-consistent.
//
if (lhs->FileId.FileId64.Value < rhs->FileId.FileId64.Value) {
return GenericLessThan;
} else if (lhs->FileId.FileId64.Value > rhs->FileId.FileId64.Value) {
return GenericGreaterThan;
} else if (lhs->FileId.FileId64.UpperZeroes < rhs->FileId.FileId64.UpperZeroes) {
return GenericLessThan;
} else if (lhs->FileId.FileId64.UpperZeroes > rhs->FileId.FileId64.UpperZeroes) {
return GenericGreaterThan;
}
return GenericEqual;
}
PVOID
NTAPI
AvAllocateGenericTableEntry (
_In_ PRTL_GENERIC_TABLE Table,
_In_ CLONG ByteSize
)
/*++
Routine Description:
This routine is the callback for allocation for entries in the generic table.
Arguments:
Table - Table for which this is invoked.
ByteSize - Amount of memory to allocate.
Return Value:
Pointer to allocated memory if successful, else NULL.
--*/
{
UNREFERENCED_PARAMETER (Table);
return ExAllocatePoolZero( PagedPool, ByteSize, AV_TABLE_ENTRY_TAG );
}
VOID
NTAPI
AvFreeGenericTableEntry (
_In_ PRTL_GENERIC_TABLE Table,
_In_ __drv_freesMem(Mem) _Post_invalid_ PVOID Entry
)
/*++
Routine Description:
This routine is the callback for releasing memory for entries in the generic
table.
Arguments:
Table - Table for which this is invoked.
Entry - Entry to free.
Return Value:
None.
--*/
{
UNREFERENCED_PARAMETER (Table);
ExFreePoolWithTag( Entry, AV_TABLE_ENTRY_TAG );
}
//
// Query File Information Routines
//
NTSTATUS
AvGetFileId (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_ PAV_FILE_REFERENCE FileId
)
/*++
Routine Description:
This routine obtains the File ID and saves it in the stream context.
Arguments:
Instance - Opaque filter pointer for the caller. This parameter is required and cannot be NULL.
FileObject - File object pointer for the file. This parameter is required and cannot be NULL.
pFileId - Pointer to file id. This is the output
Return Value:
Returns statuses forwarded from FltQueryInformationFile.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
FLT_FILESYSTEM_TYPE type;
//
// Querying for FileInternalInformation gives you the file ID.
//
status = FltGetFileSystemType( Instance, &type );
if (NT_SUCCESS( status )) {
if (type == FLT_FSTYPE_REFS) {
FILE_ID_INFORMATION fileIdInformation;
status = FltQueryInformationFile( Instance,
FileObject,
&fileIdInformation,
sizeof(FILE_ID_INFORMATION),
FileIdInformation,
NULL );
if (NT_SUCCESS( status )) {
RtlCopyMemory(&(FileId->FileId128), &(fileIdInformation.FileId), sizeof(FileId->FileId128) );
}
} else {
FILE_INTERNAL_INFORMATION fileInternalInformation;
status = FltQueryInformationFile( Instance,
FileObject,
&fileInternalInformation,
sizeof(FILE_INTERNAL_INFORMATION),
FileInternalInformation,
NULL );
if (NT_SUCCESS( status )) {
FileId->FileId64.Value = fileInternalInformation.IndexNumber.QuadPart;
FileId->FileId64.UpperZeroes = 0ll;
}
}
}
return status;
}
NTSTATUS
AvGetFileSize (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_ PLONGLONG Size
)
/*++
Routine Description:
This routine obtains the size.
Arguments:
Instance - Opaque filter pointer for the caller. This parameter is required and cannot be NULL.
FileObject - File object pointer for the file. This parameter is required and cannot be NULL.
Size - Pointer to a LONGLONG indicating the file size. This is the output.
Return Value:
Returns statuses forwarded from FltQueryInformationFile.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
FILE_STANDARD_INFORMATION standardInfo;
//
// Querying for FileStandardInformation gives you the offset of EOF.
//
status = FltQueryInformationFile( Instance,
FileObject,
&standardInfo,
sizeof(FILE_STANDARD_INFORMATION),
FileStandardInformation,
NULL );
if (NT_SUCCESS( status )) {
*Size = standardInfo.EndOfFile.QuadPart;
}
return status;
}
NTSTATUS
AvGetFileEncrypted (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_ PBOOLEAN Encrypted
)
/*++
Routine Description:
This routine obtains the File ID and saves it in the stream context.
Arguments:
Instance - Opaque filter pointer for the caller. This parameter is required and cannot be NULL.
FileObject - File object pointer for the file. This parameter is required and cannot be NULL.
Encrypted - Pointer to a boolean indicating if this file is encrypted or not. This is the output.
Return Value:
Returns statuses forwarded from FltQueryInformationFile.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
FILE_BASIC_INFORMATION basicInfo;
//
// Querying for basic information to get encryption.
//
status = FltQueryInformationFile( Instance,
FileObject,
&basicInfo,
sizeof(FILE_BASIC_INFORMATION),
FileBasicInformation,
NULL );
if (NT_SUCCESS( status )) {
*Encrypted = BooleanFlagOn( basicInfo.FileAttributes, FILE_ATTRIBUTE_ENCRYPTED );
}
return status;
}
LONG
AvExceptionFilter (
_In_ PEXCEPTION_POINTERS ExceptionPointer,
_In_ BOOLEAN AccessingUserBuffer
)
/*++
Routine Description:
Exception filter to catch errors touching user buffers.
Arguments:
ExceptionPointer - The exception record.
AccessingUserBuffer - If TRUE, overrides FsRtlIsNtStatusExpected to allow
the caller to munge the error to a desired status.
Return Value:
EXCEPTION_EXECUTE_HANDLER - If the exception handler should be run.
EXCEPTION_CONTINUE_SEARCH - If a higher exception handler should take care of
this exception.
--*/
{
NTSTATUS Status;
Status = ExceptionPointer->ExceptionRecord->ExceptionCode;
//
// Certain exceptions shouldn't be dismissed within the filter
// unless we're touching user memory.
//
if (!FsRtlIsNtstatusExpected( Status ) &&
!AccessingUserBuffer) {
return EXCEPTION_CONTINUE_SEARCH;
}
return EXCEPTION_EXECUTE_HANDLER;
}
|