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
|
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
context.h
Abstract:
Header file which contains context-related data
structures, type definitions, constants,
global variables and function prototypes.
Environment:
Kernel mode
--*/
#ifndef __CONTEXT_H__
#define __CONTEXT_H__
//
// The file infected state.
//
typedef enum _AV_FILE_INFECTED_STATE {
AvFileUnknown,
AvFileInfected,
AvFileNotInfected, // clean.
AvFileModified,
AvFileScanning
} AV_FILE_INFECTED_STATE;
#define AV_STREAMHANDLE_CONTEXT_TAG 'hSvA'
#define AV_STREAM_CONTEXT_TAG 'cSvA'
#define AV_TRANSACTION_CONTEXT_TAG 'cTvA'
#define AV_SECTION_CONTEXT_TAG 'eSvA'
#define AV_INSTANCE_CONTEXT_TAG 'cIvA'
#define AV_INSTANCES_ARRAY_TAG 'aIvA'
#define AV_CONNECTION_CTX_TAG 'cCvA'
#define AV_SCAN_CTX_TAG 'cMvA'
//
// Defines the transaction context structure
//
#define AV_TXCTX_ENLISTED 0x01
#define AV_TXCTX_LISTDRAINED 0x02
typedef struct _AV_TRANSACTION_CONTEXT {
//
// Transaction object pointer
//
PKTRANSACTION Transaction;
//
// List head for stream context list.
//
LIST_ENTRY ScListHead;
//
// Lock used to protect this context.
//
PERESOURCE Resource;
//
// A flag that tracks:
// AV_TXCTX_ENLISTED: if it has been enlisted in transaction
// AV_TXCTX_LISTDRAINED: list is drained.
//
ULONG Flags;
} AV_TRANSACTION_CONTEXT, *PAV_TRANSACTION_CONTEXT;
#define AV_TRANSACTION_CONTEXT_SIZE sizeof( AV_TRANSACTION_CONTEXT )
#define IS_FILE_MODIFIED( _sCtx ) ( (_sCtx)->State == AvFileModified )
#define IS_FILE_INFECTED( _sCtx ) ( (_sCtx)->State == AvFileInfected )
#define IS_FILE_NOT_INFECTED( _sCtx ) ( (_sCtx)->State == AvFileNotInfected )
#define IS_FILE_TX_MODIFIED( _sCtx ) ( (_sCtx)->TxState == AvFileModified )
#define IS_FILE_TX_INFECTED( _sCtx ) ( (_sCtx)->TxState == AvFileInfected )
#define IS_FILE_TX_NOT_INFECTED( _sCtx ) ( (_sCtx)->TxState == AvFileNotInfected )
#define IS_FILE_NEED_SCAN( _sCtx ) ((((_sCtx)->TxContext == NULL) && IS_FILE_MODIFIED( _sCtx )) || \
(((_sCtx)->TxContext != NULL) && IS_FILE_TX_MODIFIED( _sCtx )))
#define SET_FILE_UNKNOWN( _sCtx ) InterlockedExchange(&(_sCtx)->State, AvFileUnknown)
#define SET_FILE_MODIFIED( _sCtx ) InterlockedExchange(&(_sCtx)->State, AvFileModified)
#define SET_FILE_INFECTED( _sCtx ) InterlockedExchange(&(_sCtx)->State, AvFileInfected)
#define SET_FILE_NOT_INFECTED( _sCtx ) InterlockedExchange(&(_sCtx)->State, AvFileNotInfected)
#define SET_FILE_SCANNING( _sCtx ) InterlockedExchange(&(_sCtx)->State, AvFileScanning)
#define SET_FILE_TX_UNKNOWN( _sCtx ) InterlockedExchange(&(_sCtx)->TxState, AvFileUnknown)
#define SET_FILE_TX_MODIFIED( _sCtx ) InterlockedExchange(&(_sCtx)->TxState, AvFileModified)
#define SET_FILE_TX_INFECTED( _sCtx ) InterlockedExchange(&(_sCtx)->TxState, AvFileInfected)
#define SET_FILE_TX_NOT_INFECTED( _sCtx ) InterlockedExchange(&(_sCtx)->TxState, AvFileNotInfected)
#define SET_FILE_TX_SCANNING( _sCtx ) InterlockedExchange(&(_sCtx)->TxState, AvFileScanning)
#define SET_FILE_UNKNOWN_EX( _flag, _sCtx ) {\
if (_flag) { \
SET_FILE_TX_UNKNOWN( _sCtx ); \
} else { \
SET_FILE_UNKNOWN( _sCtx ); \
} \
}
#define SET_FILE_MODIFIED_EX( _flag, _sCtx ) {\
if (_flag) { \
SET_FILE_TX_MODIFIED( _sCtx ); \
} else { \
SET_FILE_MODIFIED( _sCtx ); \
} \
}
#define SET_FILE_INFECTED_EX( _flag, _sCtx ) {\
if (_flag) { \
SET_FILE_TX_INFECTED( _sCtx ); \
} else { \
SET_FILE_INFECTED( _sCtx ); \
} \
}
#define SET_FILE_NOT_INFECTED_EX( _flag, _sCtx ) {\
if (_flag) { \
SET_FILE_TX_NOT_INFECTED( _sCtx ); \
} else { \
SET_FILE_NOT_INFECTED( _sCtx ); \
} \
}
#define SET_FILE_SCANNING_EX( _flag, _sCtx ) {\
if (_flag) { \
SET_FILE_TX_SCANNING( _sCtx ); \
} else { \
SET_FILE_SCANNING( _sCtx ); \
} \
}
//
// Stream/Stream Handle flags
//
#define AV_FLAG_PREFETCH 0x00000001
typedef struct _AV_STREAMHANDLE_CONTEXT {
//
// Handle flags
//
ULONG Flags;
} AV_STREAMHANDLE_CONTEXT, *PAV_STREAMHANDLE_CONTEXT;
#define AV_STREAMHANDLE_CONTEXT_SIZE sizeof( AV_STREAMHANDLE_CONTEXT )
typedef struct _AV_STREAM_CONTEXT {
//
// Stream flags
//
ULONG Flags;
//
// File ID, obtained from querying the file system for
// FileInternalInformation or FileIdInformation.
//
AV_FILE_REFERENCE FileId;
//
// A pointer to the transaction context, so we can jump to list in the transaction.
//
PAV_TRANSACTION_CONTEXT TxContext;
//
// This list entry is exactly the embedded entry to
// form a doubly linked list inside transaction context.
//
LIST_ENTRY ListInTransaction;
//
// We need to synchronize the creation of the section object.
// If this syncrhonization is not made, FltCreateSectionForDataScan
// would return STATUS_FLT_CONTEXT_ALREADY_DEFINED when two threads
// are about to create the section for the same file.
//
PKEVENT ScanSynchronizationEvent;
//
// Please see AV_FILE_INFECTED_STATE for the definition of file state
// Note that we have TxState to maintain the isolation of
// the transacted writer's view.
//
volatile LONG State;
volatile LONG TxState;
//
// Revision numbers for files on CSVFS
//
LONGLONG VolumeRevision;
LONGLONG CacheRevision;
LONGLONG FileRevision;
} AV_STREAM_CONTEXT, *PAV_STREAM_CONTEXT;
#define AV_STREAM_CONTEXT_SIZE sizeof( AV_STREAM_CONTEXT )
//
// Defines the section context structure
//
typedef struct _AV_SECTION_CONTEXT {
//
// The associated section handle.
//
HANDLE SectionHandle;
//
// The associated section object.
//
PVOID SectionObject;
//
// The cancel flag (if scan in the kernel mode).
//
BOOLEAN Aborted;
//
// The size of the file associated with the section object.
//
LONGLONG FileSize;
//
// This flag indicates if this section data scan can be cancelable.
// Right now, only at pre-cleanup is cancelable on conflicting Io.
//
BOOLEAN CancelableOnConflictingIo;
//
// In the context of a conflict notification callback, only section context is given.
// We need to remember associated scan context to have scan id, so that
// We know which scan to cancel.
//
PVOID ScanContext;
} AV_SECTION_CONTEXT, *PAV_SECTION_CONTEXT;
#define AV_SECTION_CONTEXT_SIZE sizeof( AV_SECTION_CONTEXT )
//
// Instance context
//
typedef struct _AV_INSTANCE_CONTEXT {
//
// The associated volume object pointer
//
PFLT_VOLUME Volume;
//
// The associated filter instance pointer
//
PFLT_INSTANCE Instance;
//
// The file system type of the volume
//
FLT_FILESYSTEM_TYPE VolumeFSType;
//
// If the file system is NTFS, then it will support a file state cache table
// that saves the state of the file.
//
RTL_GENERIC_TABLE FileStateCacheTable;
//
// The per-instance lock to protect the cache table above.
//
ERESOURCE Resource;
//
// When set this flag indicates that the filter is attached on the
// hidden NTFS volume corresponding to a CSVFS volume
//
BOOLEAN IsOnCsvMDS;
} AV_INSTANCE_CONTEXT, *PAV_INSTANCE_CONTEXT;
#define AV_INSTANCE_CONTEXT_SIZE sizeof( AV_INSTANCE_CONTEXT )
NTSTATUS
AvFindOrCreateTransactionContext(
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Outptr_ PAV_TRANSACTION_CONTEXT *TransactionContext
);
NTSTATUS
AvCreateSectionContext (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Outptr_ PAV_SECTION_CONTEXT *SectionContext
);
NTSTATUS
AvCreateStreamHandleContext (
_In_ PFLT_FILTER Filter,
_Outptr_ PAV_STREAMHANDLE_CONTEXT *StreamHandleContext
);
NTSTATUS
AvCreateStreamContext (
_In_ PFLT_FILTER Filter,
_Outptr_ PAV_STREAM_CONTEXT *StreamContext
);
NTSTATUS
AvEnumerateInstances(
_Outptr_result_buffer_(*NumberInstances) PFLT_INSTANCE **InstanceArray,
_Out_ PULONG NumberInstances
);
VOID
AvFreeInstances (
_In_reads_(InstanceCount) PFLT_INSTANCE *InstanceArray,
_In_ ULONG InstanceCount
);
#endif
|