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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
|
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
context.c
Abstract:
Filter Context-related module implementation.
Environment:
Kernel mode
--*/
#include "avscan.h"
//
// Local function prototypes.
//
VOID
AvStreamContextCleanup (
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
);
VOID
AvTransactionContextCleanup (
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
);
VOID
AvSectionContextCleanup(
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
);
VOID
AvInstanceContextCleanup(
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, AvCreateStreamContext)
#pragma alloc_text(PAGE, AvCreateStreamHandleContext)
#pragma alloc_text(PAGE, AvFindOrCreateTransactionContext)
#pragma alloc_text(PAGE, AvCreateSectionContext)
#pragma alloc_text(PAGE, AvStreamContextCleanup)
#pragma alloc_text(PAGE, AvTransactionContextCleanup)
#pragma alloc_text(PAGE, AvSectionContextCleanup)
#pragma alloc_text(PAGE, AvInstanceContextCleanup)
#pragma alloc_text(PAGE, AvAllocateScanContext)
#pragma alloc_text(PAGE, AvReferenceScanContext)
#pragma alloc_text(PAGE, AvReleaseScanContext)
#endif
//
// Context registration structure
//
const FLT_CONTEXT_REGISTRATION ContextRegistration[] = {
{ FLT_STREAM_CONTEXT,
0,
AvStreamContextCleanup,
AV_STREAM_CONTEXT_SIZE,
AV_STREAM_CONTEXT_TAG },
{ FLT_STREAMHANDLE_CONTEXT,
0,
NULL,
AV_STREAMHANDLE_CONTEXT_SIZE,
AV_STREAMHANDLE_CONTEXT_TAG },
{ FLT_TRANSACTION_CONTEXT,
0,
AvTransactionContextCleanup,
AV_TRANSACTION_CONTEXT_SIZE,
AV_TRANSACTION_CONTEXT_TAG },
{ FLT_SECTION_CONTEXT,
0,
AvSectionContextCleanup,
AV_SECTION_CONTEXT_SIZE,
AV_SECTION_CONTEXT_TAG },
{ FLT_INSTANCE_CONTEXT,
0,
AvInstanceContextCleanup,
AV_INSTANCE_CONTEXT_SIZE,
AV_INSTANCE_CONTEXT_TAG },
{ FLT_CONTEXT_END }
};
VOID
AvStreamContextCleanup (
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
)
/*++
Routine Description:
This function is called by the filter manager before freeing any of the minifilter
driver's contexts of that type.
In this routine, the driver has to perform any needed cleanup, such as freeing
additional memory that the minifilter driver allocated inside the context structure
Arguments:
Context - Pointer to the minifilter driver's portion of the context.
ContextType - Supposed to be FLT_STREAM_CONTEXT.
Return Value:
None
--*/
{
PAV_STREAM_CONTEXT streamContext = (PAV_STREAM_CONTEXT) Context;
UNREFERENCED_PARAMETER( ContextType );
PAGED_CODE();
FLT_ASSERTMSG( "[AV]: Stream context is not supposed to be in the transaction context list at cleanup.!\n",
NULL == streamContext->TxContext );
AvFreeKevent( streamContext->ScanSynchronizationEvent );
}
VOID
AvTransactionContextCleanup (
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
)
/*++
Routine Description:
This function is called by the filter manager before freeing any of the minifilter
driver's contexts of that type.
In this routine, the driver has to perform any needed cleanup, such as freeing
additional memory that the minifilter driver allocated inside the context structure
We delete the stream context list in transaction context here.
Arguments:
Context - Pointer to the minifilter driver's portion of the context.
ContextType - Supposed to be FLT_TRANSACTION_CONTEXT.
Return Value:
None
--*/
{
PAV_TRANSACTION_CONTEXT transactionContext = (PAV_TRANSACTION_CONTEXT) Context;
UNREFERENCED_PARAMETER( ContextType );
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_DEBUG,
("[Av]: AvTransactionContextCleanup context cleanup entered.\n") );
ExDeleteResourceLite( transactionContext->Resource );
AvFreeResource( transactionContext->Resource );
transactionContext->Resource = NULL;
ObDereferenceObject( transactionContext->Transaction );
transactionContext->Transaction = NULL;
}
VOID
AvSectionContextCleanup(
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
)
/*++
Routine Description:
This function is called by the filter manager before freeing any of the minifilter
driver's contexts of that type.
In this routine, the driver has to perform any needed cleanup, such as freeing
additional memory that the minifilter driver allocated inside the context structure
Arguments:
Context - Pointer to the minifilter driver's portion of the context.
ContextType - Supposed to be FLT_SECTION_CONTEXT (win8 or later).
Return Value:
None
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER( Context );
UNREFERENCED_PARAMETER( ContextType );
FLT_ASSERTMSG( "[AV] AvSectionContextCleanup: Section handle should be NULL at cleanup.\n",
((PAV_SECTION_CONTEXT) Context)->SectionHandle == NULL );
FLT_ASSERTMSG( "[AV] AvSectionContextCleanup: Section object should be NULL at cleanup.\n",
((PAV_SECTION_CONTEXT) Context)->SectionObject == NULL );
}
VOID
AvInstanceContextCleanup(
_In_ PFLT_CONTEXT Context,
_In_ FLT_CONTEXT_TYPE ContextType
)
/*++
Routine Description:
This function is called by the filter manager before freeing any of the minifilter
driver's contexts of that type.
In this routine, the driver has to perform any needed cleanup, such as freeing
additional memory that the minifilter driver allocated inside the context structure.
We delete the cache table if the file system supports one.
Arguments:
Context - Pointer to the minifilter driver's portion of the context.
ContextType - Supposed to be FLT_INSTANCE_CONTEXT (win8 or later).
Return Value:
None
--*/
{
PAV_INSTANCE_CONTEXT instanceContext = (PAV_INSTANCE_CONTEXT) Context;
UNREFERENCED_PARAMETER( Context );
UNREFERENCED_PARAMETER( ContextType );
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_ROUTINES,
( "[Av]: AvInstanceContextCleanup context cleanup entered\n") );
if (FS_SUPPORTS_FILE_STATE_CACHE( instanceContext->VolumeFSType )) {
FLT_ASSERTMSG( "[AV] AvInstanceContextCleanup: The generic table should be empty at cleanup.\n",
RtlIsGenericTableEmpty( &instanceContext->FileStateCacheTable ) );
ExDeleteResourceLite( &instanceContext->Resource );
}
}
NTSTATUS
AvCreateStreamHandleContext (
_In_ PFLT_FILTER Filter,
_Outptr_ PAV_STREAMHANDLE_CONTEXT *StreamHandleContext
)
/*++
Routine Description:
This routine creates a new streamhandle context
Arguments:
StreamHandleContext - Returns the streamhandle context
Return Value:
Status
--*/
{
NTSTATUS status;
PAV_STREAMHANDLE_CONTEXT streamHandleContext;
PAGED_CODE();
//
// Allocate a streamhandle context
//
status = FltAllocateContext( Filter,
FLT_STREAMHANDLE_CONTEXT,
AV_STREAMHANDLE_CONTEXT_SIZE,
PagedPool,
&streamHandleContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[Av]: Failed to allocate stream handle context with status 0x%x \n",
status) );
return status;
}
//
// Initialize the newly created context
//
RtlZeroMemory(streamHandleContext, AV_STREAMHANDLE_CONTEXT_SIZE);
*StreamHandleContext = streamHandleContext;
return STATUS_SUCCESS;
}
NTSTATUS
AvCreateStreamContext (
_In_ PFLT_FILTER Filter,
_Outptr_ PAV_STREAM_CONTEXT *StreamContext
)
/*++
Routine Description:
This routine creates a new stream context
Arguments:
StreamContext - Returns the stream context
Return Value:
Status
--*/
{
NTSTATUS status;
PKEVENT event = NULL;
PAV_STREAM_CONTEXT streamContext;
PAGED_CODE();
//
// Allocate the kernel event object
//
event = AvAllocateKevent();
if (NULL == event) {
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Allocate a stream context
//
status = FltAllocateContext( Filter,
FLT_STREAM_CONTEXT,
AV_STREAM_CONTEXT_SIZE,
PagedPool,
&streamContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[Av]: Failed to allocate stream context with status 0x%x \n",
status) );
AvFreeKevent( event );
return status;
}
//
// Initialize the newly created context
//
RtlZeroMemory(streamContext, AV_STREAM_CONTEXT_SIZE);
streamContext->ScanSynchronizationEvent = event;
KeInitializeEvent( streamContext->ScanSynchronizationEvent, SynchronizationEvent, TRUE );
SET_FILE_MODIFIED( streamContext );
SET_FILE_TX_MODIFIED( streamContext );
*StreamContext = streamContext;
return STATUS_SUCCESS;
}
NTSTATUS
AvFindOrCreateTransactionContext(
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Outptr_ PAV_TRANSACTION_CONTEXT *TransactionContext
)
/*++
Routine Description
This routine finds the transaction context, if not found, it will
try to create a new one. The caller is responsible for calling
FltReleaseContext to decrement its reference count.
Arguments
FltObjects - Contains parameters required to enlist in a transaction.
TransactionContext - Returns the transaction context
Return value
Returns STATUS_SUCCESS if we were able to successfully find/create
a transaction context. Returns an appropriate error code on a failure.
--*/
{
NTSTATUS status;
PAV_TRANSACTION_CONTEXT transactionContext = NULL;
PAV_TRANSACTION_CONTEXT oldTransactionContext = NULL;
PERESOURCE pResource = NULL;
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_DEBUG,
("[Av]: AvFindOrCreateTransactionContext entered. \n") );
status = FltGetTransactionContext( FltObjects->Instance,
FltObjects->Transaction,
&transactionContext );
if (NT_SUCCESS( status )) {
*TransactionContext = transactionContext;
return STATUS_SUCCESS;
}
if (status != STATUS_NOT_FOUND) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV]: Failed to get transaction context with status 0x%x \n",
status) );
return status;
}
//
// Allocate the resource
//
pResource = AvAllocateResource();
if ( NULL == pResource ) {
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Allocate a transaction context.
//
status = FltAllocateContext( Globals.Filter,
FLT_TRANSACTION_CONTEXT,
AV_TRANSACTION_CONTEXT_SIZE,
PagedPool,
&transactionContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV]: Failed to allocate transaction context with status 0x%x \n",
status) );
AvFreeResource( pResource );
return status;
}
FLT_ASSERTMSG( "[AV]: Transaction object pointer is not supposed to be NULL !\n", FltObjects->Transaction != NULL);
//
// Initialization of transaction context.
// The reason we allocate eResource seperately is because
// eResource has to be allocated in the non-paged pool.
//
RtlZeroMemory(transactionContext, AV_TRANSACTION_CONTEXT_SIZE);
transactionContext->Resource = pResource;
ObReferenceObject( FltObjects->Transaction );
transactionContext->Transaction = FltObjects->Transaction;
InitializeListHead( &transactionContext->ScListHead );
ExInitializeResourceLite( transactionContext->Resource );
status = FltSetTransactionContext( FltObjects->Instance,
FltObjects->Transaction,
FLT_SET_CONTEXT_KEEP_IF_EXISTS,
transactionContext,
&oldTransactionContext );
if (NT_SUCCESS( status )) {
*TransactionContext = transactionContext;
return STATUS_SUCCESS;
}
FltReleaseContext( transactionContext );
if (status != STATUS_FLT_CONTEXT_ALREADY_DEFINED) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[Av]: Failed to set transaction context with status 0x%x \n",
status) );
return status;
}
if (NULL == oldTransactionContext) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[Av]: Failed to set transaction context oldTransactionContext is NULL \n") );
return status;
}
*TransactionContext = oldTransactionContext;
return STATUS_SUCCESS;
}
NTSTATUS
AvCreateSectionContext (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Outptr_ PAV_SECTION_CONTEXT *SectionContext
)
/*++
Routine Description:
This routine creates a new section context.
Arguments:
Instance - Opaque instance 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.
SectionContext - Returns the section context
Return Value:
Status
--*/
{
NTSTATUS status;
LONGLONG fileSize;
PAV_SECTION_CONTEXT sectionContext = NULL;
PAGED_CODE();
status = FltAllocateContext( Globals.Filter,
FLT_SECTION_CONTEXT,
AV_SECTION_CONTEXT_SIZE,
PagedPool,
§ionContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[Av]: Failed to allocate section context.\n, 0x%08x\n",
status) );
return status;
}
RtlZeroMemory(sectionContext, AV_SECTION_CONTEXT_SIZE);
status = AvGetFileSize( Instance,
FileObject,
&fileSize );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[Av]: Failed to get file size with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
FileObject,
Instance ));
} else {
sectionContext->FileSize = fileSize;
}
*SectionContext = sectionContext;
return STATUS_SUCCESS;
}
NTSTATUS
AvEnumerateInstances(
_Outptr_result_buffer_(*NumberInstances) PFLT_INSTANCE **InstanceArray,
_Out_ PULONG NumberInstances
)
/*++
Routine Description:
This routine returns all the instances available of this filter
The caller is responsible for calling AvFreeInstances(...) to release the instance references.
Arguments:
InstanceArray - This function will allocate the memory of the arrary containing the instances.
NumberInstances - The number of instances in InstanceArray.
Return Value:
Status
--*/
{
PFLT_INSTANCE *instArray = NULL;
NTSTATUS status = STATUS_SUCCESS;
ULONG i = 0;
ULONG instCnt = 0;
ULONG newCount = 0;
//
// Get a count of how many instances there are
//
status = FltEnumerateInstances( NULL,
Globals.Filter,
NULL,
0,
&instCnt );
if (!NT_SUCCESS(status) &&
(status != STATUS_BUFFER_TOO_SMALL)) {
goto Cleanup;
}
//
// Get handles for all instances. This will loop in case too many
// filters load between the time we got the count and the time
// we actually get the list.
//
for (;;) {
//
// Free old memory if we have some
//
if (instArray != NULL) {
ExFreePoolWithTag( instArray, AV_INSTANCES_ARRAY_TAG);
instArray = NULL;
}
//
// Allocate memory for list, add a couple of entries in case
// a filter loads while we are doing this
//
instCnt += 2;
instArray = ExAllocatePoolZero( PagedPool,
(instCnt * sizeof(PFLT_INSTANCE)),
AV_INSTANCES_ARRAY_TAG );
if (instArray == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto Cleanup;
}
//
// This time get list of filters (and a new count)
//
status = FltEnumerateInstances( NULL,
Globals.Filter,
instArray,
instCnt,
&newCount );
//
// exit loop if we succeeded
//
if (NT_SUCCESS(status)) {
instCnt = newCount;
break;
}
//
// If it was an unexpected error, quit processing, else allocate
// more memory and try again
//
if (status != STATUS_BUFFER_TOO_SMALL) {
goto Cleanup;
}
//
// The buffer was too small, try again
//
FLT_ASSERT(newCount > instCnt);
instCnt = newCount;
}
*InstanceArray = instArray;
*NumberInstances = instCnt;
Cleanup:
if ( !NT_SUCCESS(status) ) {
if (instArray) {
//
// Release all the objects in the array
//
for (i = 0; i < instCnt; i++) {
FltObjectDereference( instArray[i] );
instArray[i] = NULL;
}
ExFreePoolWithTag( instArray, AV_INSTANCES_ARRAY_TAG );
instArray = NULL;
}
}
return status;
}
VOID
AvFreeInstances (
_In_reads_(InstanceCount) PFLT_INSTANCE *InstanceArray,
_In_ ULONG InstanceCount
)
/*++
Routine Description:
This routine frees the reference count and memory of instance array obtained from AvEnumerateInstances(...).
Arguments:
InstanceArray - The instance arrary to be freed.
NumberInstances - The number of instances in InstanceArray.
Return Value:
None.
--*/
{
ULONG i = 0;
//
// Release all the objects in the array
//
for (i = 0; i < InstanceCount; i++) {
FltObjectDereference( InstanceArray[i] );
InstanceArray[i] = NULL;
}
ExFreePoolWithTag( InstanceArray, AV_INSTANCES_ARRAY_TAG );
}
NTSTATUS
AvAllocateScanContext(
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Outptr_ PAV_SCAN_CONTEXT *ScanContext
)
/*++
Routine Description:
The routine allocates the scan context
Arguments:
Instance - Opaque instance 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.
ScanContext - The output scan context.
Return Value:
STATUS_INSUFFICIENT_RESOURCES if allocation failed.
STATUS_SUCCESS if successfully allocated.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
PAV_SCAN_CONTEXT scanCtx = NULL;
PAGED_CODE();
ASSERT(Instance != NULL);
ASSERT(FileObject != NULL);
scanCtx = ExAllocatePoolZero( NonPagedPoolNx,
sizeof(AV_SCAN_CONTEXT),
AV_SCAN_CTX_TAG );
if (NULL == scanCtx) {
return STATUS_INSUFFICIENT_RESOURCES;
}
scanCtx->RefCount = 1;
scanCtx->IoWaitOnScanCompleteNotificationAborted = FALSE;
//
// Keeps a reference in scan contex.
// We also handle the case that the instance is being torn down.
//
status = FltObjectReference( Instance );
if (!NT_SUCCESS(status)) {
ExFreePoolWithTag( scanCtx, AV_SCAN_CTX_TAG );
return status;
}
scanCtx->FilterInstance = Instance;
//
// Keeps a reference in scan context
//
ObReferenceObject( FileObject );
scanCtx->FileObject = FileObject;
*ScanContext = scanCtx;
return STATUS_SUCCESS;
}
NTSTATUS
AvReferenceScanContext(
_In_ PAV_SCAN_CONTEXT ScanContext
)
/*++
Routine Description:
The routine increments the reference count of scan context to prevent it from deletion.
Arguments:
ScanContext - The scan context to be added reference.
Return Value:
STATUS_INVALID_PARAMETER if ScanContext is NULL.
STATUS_SUCCESS if successfully incremented.
--*/
{
PAGED_CODE();
if (ScanContext == NULL) {
return STATUS_INVALID_PARAMETER;
}
ASSERT(ScanContext->RefCount != 0);
ASSERT(ScanContext->FilterInstance != NULL);
ASSERT(ScanContext->FileObject != NULL);
InterlockedIncrement(&ScanContext->RefCount);
return STATUS_SUCCESS;
}
NTSTATUS
AvReleaseScanContext(
_In_ PAV_SCAN_CONTEXT ScanContext
)
/*++
Routine Description:
The routine decrements the reference count of scan context.
Release it if reference count goes to zero.
Arguments:
ScanContext - The scan context to be released.
Return Value:
STATUS_INVALID_PARAMETER if ScanContext is NULL.
STATUS_SUCCESS if successfully decremented.
--*/
{
ULONG newRefCount = 0;
PAGED_CODE();
if (ScanContext == NULL) {
return STATUS_INVALID_PARAMETER;
}
ASSERT(ScanContext->FilterInstance != NULL);
ASSERT(ScanContext->FileObject != NULL);
//
// Assume the usage of AvReferenceScanContext and AvReleaseScanContext are not raced,
// This simple version would suffice.
//
newRefCount = InterlockedDecrement(&ScanContext->RefCount);
if (newRefCount == 0) {
//
// Before freeing scan context, we need to release the file object and instance.
//
FltObjectDereference( ScanContext->FilterInstance );
ObDereferenceObject( ScanContext->FileObject );
ExFreePoolWithTag( ScanContext, AV_SCAN_CTX_TAG );
}
return STATUS_SUCCESS;
}
|