/
avscan.c
3310 lines (2391 loc) · 89.6 KB
/
avscan.c
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
avscan.c
Abstract:
This is the main module of the avscan mini-filter driver.
This filter demonstrates how to implement a transaction-aware
anti-virus filter.
Av prefix denotes "Anti-virus" module.
Environment:
Kernel mode
--*/
#include <initguid.h>
#include "avscan.h"
/*************************************************************************
Local Function Prototypes
*************************************************************************/
DRIVER_INITIALIZE DriverEntry;
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
typedef
NTSTATUS
(*PFN_IoOpenDriverRegistryKey) (
PDRIVER_OBJECT DriverObject,
DRIVER_REGKEY_TYPE RegKeyType,
ACCESS_MASK DesiredAccess,
ULONG Flags,
PHANDLE DriverRegKey
);
PFN_IoOpenDriverRegistryKey
AvGetIoOpenDriverRegistryKey (
VOID
);
NTSTATUS
AvOpenServiceParametersKey (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING ServiceRegistryPath,
_Out_ PHANDLE ServiceParametersKey
);
NTSTATUS
AvSetConfiguration (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
NTSTATUS
AvInstanceSetup (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
);
VOID
AvInstanceTeardownStart (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Unreferenced_parameter_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
);
VOID
AvInstanceTeardownComplete (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
);
NTSTATUS
AvUnload (
_Unreferenced_parameter_ FLT_FILTER_UNLOAD_FLAGS Flags
);
NTSTATUS
AvInstanceQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
);
FLT_PREOP_CALLBACK_STATUS
AvPreOperationCallback (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
FLT_PREOP_CALLBACK_STATUS
AvPreCreate (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
FLT_POSTOP_CALLBACK_STATUS
AvPostCreate (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_opt_ PVOID CompletionContext,
_In_ FLT_POST_OPERATION_FLAGS Flags
);
FLT_PREOP_CALLBACK_STATUS
AvPreCleanup (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
FLT_PREOP_CALLBACK_STATUS
AvPreFsControl (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
NTSTATUS
AvKtmNotificationCallback (
_Unreferenced_parameter_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ PFLT_CONTEXT TransactionContext,
_In_ ULONG TransactionNotification
);
NTSTATUS
AvScanAbortCallbackAsync (
_Unreferenced_parameter_ PFLT_INSTANCE Instance,
_In_ PFLT_CONTEXT Context,
_Unreferenced_parameter_ PFLT_CALLBACK_DATA Data
);
//
// Local routines
//
BOOLEAN
AvOperationsModifyingFile (
_In_ PFLT_CALLBACK_DATA Data
);
NTSTATUS
AvQueryTransactionOutcome(
_In_ PKTRANSACTION Transaction,
_Out_ PULONG TxOutcome
);
NTSTATUS
AvProcessPreviousTransaction (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Inout_ PAV_STREAM_CONTEXT StreamContext
);
NTSTATUS
AvProcessTransactionOutcome (
_Inout_ PAV_TRANSACTION_CONTEXT TransactionContext,
_In_ ULONG TransactionOutcome
);
NTSTATUS
AvLoadFileStateFromCache (
_In_ PFLT_INSTANCE Instance,
_In_ PAV_FILE_REFERENCE FileId,
_Out_ LONG volatile* State,
_Out_ PLONGLONG VolumeRevision,
_Out_ PLONGLONG CacheRevision,
_Out_ PLONGLONG FileRevision
);
NTSTATUS
AvSyncCache (
_In_ PFLT_INSTANCE Instance,
_In_ PAV_STREAM_CONTEXT StreamContext
);
BOOLEAN
AvIsPrefetchEcpPresent (
_In_ PFLT_FILTER Filter,
_In_ PFLT_CALLBACK_DATA Data
);
BOOLEAN
AvIsStreamAlternate (
_Inout_ PFLT_CALLBACK_DATA Data
);
NTSTATUS
AvScan (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ AV_SCAN_MODE ScanMode,
_In_ UCHAR IOMajorFunctionAtScan,
_In_ BOOLEAN IsInTxWriter,
_Inout_ PAV_STREAM_CONTEXT StreamContext
);
VOID
AvDoCancelScanAndRelease (
_In_ PAV_SCAN_CONTEXT ScanContext,
_In_ PAV_SECTION_CONTEXT SectionContext
);
NTSTATUS
AvSendUnloadingToUser (
VOID
);
//
// Assign text sections for each routine.
//
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(INIT, AvGetIoOpenDriverRegistryKey)
#pragma alloc_text(INIT, AvOpenServiceParametersKey)
#pragma alloc_text(INIT, AvSetConfiguration)
#pragma alloc_text(PAGE, AvUnload)
#pragma alloc_text(PAGE, AvInstanceQueryTeardown)
#pragma alloc_text(PAGE, AvInstanceSetup)
#pragma alloc_text(PAGE, AvInstanceTeardownStart)
#pragma alloc_text(PAGE, AvInstanceTeardownComplete)
#pragma alloc_text(PAGE, AvPreCreate)
#pragma alloc_text(PAGE, AvPostCreate)
#pragma alloc_text(PAGE, AvPreFsControl)
#pragma alloc_text(PAGE, AvPreCleanup)
#pragma alloc_text(PAGE, AvKtmNotificationCallback)
#pragma alloc_text(PAGE, AvScanAbortCallbackAsync)
#pragma alloc_text(PAGE, AvOperationsModifyingFile)
#pragma alloc_text(PAGE, AvQueryTransactionOutcome)
#pragma alloc_text(PAGE, AvProcessPreviousTransaction)
#pragma alloc_text(PAGE, AvProcessTransactionOutcome)
#pragma alloc_text(PAGE, AvLoadFileStateFromCache)
#pragma alloc_text(PAGE, AvSyncCache)
#pragma alloc_text(PAGE, AvIsPrefetchEcpPresent)
#pragma alloc_text(PAGE, AvIsStreamAlternate)
#pragma alloc_text(PAGE, AvScan)
#pragma alloc_text(PAGE, AvDoCancelScanAndRelease)
#pragma alloc_text(PAGE, AvSendAbortToUser)
#pragma alloc_text(PAGE, AvSendUnloadingToUser)
#endif
//
// operation registration
//
CONST FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE,
0,
AvPreCreate,
AvPostCreate },
{ IRP_MJ_CLEANUP,
0,
AvPreCleanup,
NULL },
{ IRP_MJ_WRITE,
0,
AvPreOperationCallback,
NULL },
{ IRP_MJ_SET_INFORMATION,
0,
AvPreOperationCallback,
NULL },
{ IRP_MJ_FILE_SYSTEM_CONTROL,
0,
AvPreFsControl,
NULL },
{ IRP_MJ_OPERATION_END }
};
//
// Context registraction construct defined in context.c
//
extern const FLT_CONTEXT_REGISTRATION ContextRegistration[];
//
// This defines what we want to filter with FltMgr
//
CONST FLT_REGISTRATION FilterRegistration = {
sizeof( FLT_REGISTRATION ), // Size
FLT_REGISTRATION_VERSION, // Version
0, // Flags
ContextRegistration, // Context
Callbacks, // Operation callbacks
AvUnload, // MiniFilterUnload
AvInstanceSetup, // InstanceSetup
AvInstanceQueryTeardown, // InstanceQueryTeardown
AvInstanceTeardownStart, // InstanceTeardownStart
AvInstanceTeardownComplete, // InstanceTeardownComplete
NULL, // GenerateFileName
NULL, // NormalizeNameComponentCallback
NULL, // NormalizeContextCleanupCallback
AvKtmNotificationCallback, // TransactionNotificationCallback
NULL, // NormalizeNameComponentExCallback
AvScanAbortCallbackAsync // SectionNotificationCallback
};
NTSTATUS
AvInstanceSetup (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
)
/*++
Routine Description:
This routine is called whenever a new instance is created on a volume. This
gives us a chance to decide if we need to attach to this volume or not.
If this routine is not defined in the registration structure, automatic
instances are alwasys created.
Arguments:
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Flags describing the reason for this attach request.
Return Value:
STATUS_SUCCESS - attach
STATUS_FLT_DO_NOT_ATTACH - do not attach
--*/
{
NTSTATUS status;
PAV_INSTANCE_CONTEXT instanceContext = NULL;
BOOLEAN isOnCsv = FALSE;
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_ROUTINES,
("[AV] AvInstanceSetup: Entered\n") );
//
// Don't attach to network volumes.
//
if (VolumeDeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM) {
return STATUS_FLT_DO_NOT_ATTACH;
}
//
// Determine if the filter is attaching to the hidden NTFS volume
// that corresponds to a CSV volume. If so do not attach. Note
// that it would be feasible for the filter to attach to this
// volume as part of a distrubuted filter implementation but that
// is beyond the scope of this sample.
//
if (VolumeFilesystemType == FLT_FSTYPE_NTFS) {
isOnCsv = AvIsVolumeOnCsvDisk( FltObjects->Volume );
if (isOnCsv) {
return STATUS_FLT_DO_NOT_ATTACH;
}
}
status = FltAllocateContext( Globals.Filter,
FLT_INSTANCE_CONTEXT,
AV_INSTANCE_CONTEXT_SIZE,
NonPagedPoolNx,
&instanceContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] AvInstanceSetup: allocate instance context failed. status = 0x%x\n", status) );
return STATUS_FLT_DO_NOT_ATTACH;
}
//
// Setup instance context
//
RtlZeroMemory(instanceContext, AV_INSTANCE_CONTEXT_SIZE);
instanceContext->Volume = FltObjects->Volume;
instanceContext->Instance = FltObjects->Instance;
instanceContext->VolumeFSType = VolumeFilesystemType;
instanceContext->IsOnCsvMDS = isOnCsv;
//
// There will be a file state cache table for each NTFS volume instance.
// As for other file systems, file id is not unique, and thus we do
// not have cache for other kinds of file systems. Since the cache
// table is not mandatory to implement an anti-virus filter, we
// only have the volatile cache for NTFS, CSVFS and REFS.
//
// It is worth mentioning that the table is potentially very large.
// We use an AVL tree to improve insertion and query times. We do not
// set an upper bound for the size of the tree which is not optimal.
// Consider limiting the size of the tree for a production filter.
//
if (FS_SUPPORTS_FILE_STATE_CACHE( VolumeFilesystemType )) {
//
// Initialize file state cache in the instance context.
//
ExInitializeResourceLite( &instanceContext->Resource );
RtlInitializeGenericTable( &instanceContext->FileStateCacheTable,
AvCompareEntry,
AvAllocateGenericTableEntry,
AvFreeGenericTableEntry,
NULL );
}
status = FltSetInstanceContext( FltObjects->Instance,
FLT_SET_CONTEXT_KEEP_IF_EXISTS,
instanceContext,
NULL );
//
// In all cases, we need to release the instance context at this time.
// If we hit an error, it will get freed now.
//
FltReleaseContext( instanceContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] AvInstanceSetup: set instance context failed. status = 0x%x\n", status) );
return STATUS_FLT_DO_NOT_ATTACH;
}
//
// Register this instance as a datascan filter. If this call
// fails the underlying filesystem does not support using
// the filter manager datascan API. Currently only the
// the namedpipe and mailslot file systems are unsupported.
//
status = FltRegisterForDataScan( FltObjects->Instance );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] AvInstanceSetup: FltRegisterForDataScan failed. status = 0x%x\n", status) );
return STATUS_FLT_DO_NOT_ATTACH;
}
return STATUS_SUCCESS;
}
NTSTATUS
AvInstanceQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
)
/*++
Routine Description:
This is called when an instance is being manually deleted by a
call to FltDetachVolume or FilterDetach thereby giving us a
chance to fail that detach request.
If this routine is not defined in the registration structure, explicit
detach requests via FltDetachVolume or FilterDetach will always be
failed.
Arguments:
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Indicating where this detach request came from.
Return Value:
Returns the status of this operation.
--*/
{
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_ROUTINES,
("[AV] AvInstanceQueryTeardown: Entered\n") );
return STATUS_SUCCESS;
}
VOID
AvInstanceTeardownStart (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Unreferenced_parameter_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
)
/*++
Routine Description:
This routine is called at the start of instance teardown.
If we have cache table, we have to clean up the table at this point.
Arguments:
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Reason why this instance is been deleted.
Return Value:
None.
--*/
{
NTSTATUS status;
PLIST_ENTRY scan;
PLIST_ENTRY next;
PAV_SCAN_CONTEXT scanCtx = NULL;
PAV_INSTANCE_CONTEXT instanceContext = NULL;
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_DEBUG,
("[AV] AvInstanceTeardownStart: Entered\n") );
status = FltGetInstanceContext( FltObjects->Instance,
&instanceContext );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] AvInstanceTeardownStart: FltGetInstanceContext failed. status = 0x%x\n", status) );
return;
}
//
// Search the scan context from the global list.
//
AvAcquireResourceExclusive( &Globals.ScanCtxListLock );
LIST_FOR_EACH_SAFE( scan, next, &Globals.ScanCtxListHead ) {
scanCtx = CONTAINING_RECORD( scan, AV_SCAN_CONTEXT, List );
if (scanCtx->FilterInstance != FltObjects->Instance) {
continue;
}
//
// Notify the user scan thread to abort the scan.
//
status = AvSendAbortToUser(scanCtx->ScanThreadId,
scanCtx->ScanId);
//
// If we fail to send message to the user, then we
// do the cancel and cleanup by ourself; otherwise,
// the listening thread will call back to cleanup and
// I/O request thred will tear down the scan context.
//
if (!NT_SUCCESS( status ) || status == STATUS_TIMEOUT) {
AvFinalizeScanAndSection(scanCtx);
}
}
AvReleaseResource( &Globals.ScanCtxListLock );
//
// Clean up the cache table if the volume supports one.
//
if (FS_SUPPORTS_FILE_STATE_CACHE( instanceContext->VolumeFSType )) {
PAV_GENERIC_TABLE_ENTRY entry = NULL;
AvAcquireResourceExclusive( &instanceContext->Resource );
while (!RtlIsGenericTableEmpty( &instanceContext->FileStateCacheTable ) ) {
entry = RtlGetElementGenericTable(&instanceContext->FileStateCacheTable, 0);
AV_DBG_PRINT( AVDBG_TRACE_ROUTINES,
("[AV] AvInstanceTeardownStart: %I64x,%I64x requesting deletion, state:%d\n",
entry->FileId.FileId64.UpperZeroes,
entry->FileId.FileId64.Value,
entry->InfectedState) );
RtlDeleteElementGenericTable(&instanceContext->FileStateCacheTable, entry);
}
AvReleaseResource( &instanceContext->Resource );
}
FltReleaseContext( instanceContext );
FltDeleteInstanceContext( FltObjects->Instance, NULL );
}
VOID
AvInstanceTeardownComplete (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
)
/*++
Routine Description:
This routine is called at the end of instance teardown.
Arguments:
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Reason why this instance is been deleted.
Return Value:
None.
--*/
{
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
AV_DBG_PRINT( AVDBG_TRACE_ROUTINES,
("[AV] AvInstanceTeardownComplete: Entered\n") );
}
/*************************************************************************
MiniFilter initialization and unload routines.
*************************************************************************/
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
This is the initialization routine for this miniFilter driver. This
registers with FltMgr and initializes all global data structures.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - Unicode string identifying where the parameters for this
driver are located in the registry.
Return Value:
Returns the final status of this operation.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
PSECURITY_DESCRIPTOR sd = NULL;
AV_DBG_PRINT( AVDBG_TRACE_ROUTINES,
("[AV] DriverEntry: Entered\n") );
//
// Set default global configuration
//
RtlZeroMemory( &Globals, sizeof(Globals) );
InitializeListHead( &Globals.ScanCtxListHead );
ExInitializeResourceLite( &Globals.ScanCtxListLock );
Globals.ScanIdCounter = 0;
Globals.LocalScanTimeout = 30000;
Globals.NetworkScanTimeout = 60000;
#if DBG
Globals.DebugLevel = 0xffffffff; // AVDBG_TRACE_ERROR | AVDBG_TRACE_DEBUG;
#endif
try {
//
// Set the filter configuration based on registry keys
//
status = AvSetConfiguration( DriverObject, RegistryPath );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV]: DriverEntry: SetConfiguration FAILED. status = 0x%x\n", status) );
leave;
}
//
// Register with FltMgr to tell it our callback routines
//
status = FltRegisterFilter( DriverObject,
&FilterRegistration,
&Globals.Filter );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] DriverEntry: FltRegisterFilter FAILED. status = 0x%x\n", status) );
leave;
}
//
// Builds a default security descriptor for use with FltCreateCommunicationPort.
//
status = FltBuildDefaultSecurityDescriptor( &sd,
FLT_PORT_ALL_ACCESS );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] DriverEntry: FltBuildDefaultSecurityDescriptor FAILED. status = 0x%x\n", status) );
leave;
}
//
// Prepare ports between kernel and user.
//
status = AvPrepareServerPort( sd, AvConnectForScan );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] DriverEntry: AvPrepareServerPort Scan Port FAILED. status = 0x%x\n", status) );
leave;
}
status = AvPrepareServerPort( sd, AvConnectForAbort );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] DriverEntry: AvPrepareServerPort Abort Port FAILED. status = 0x%x\n", status) );
leave;
}
status = AvPrepareServerPort( sd, AvConnectForQuery );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] DriverEntry: AvPrepareServerPort Query Port FAILED. status = 0x%x\n", status) );
leave;
}
//
// Start filtering i/o
//
status = FltStartFiltering( Globals.Filter );
if (!NT_SUCCESS( status )) {
AV_DBG_PRINT( AVDBG_TRACE_ERROR,
("[AV] DriverEntry: FltStartFiltering FAILED. status = 0x%x\n", status) );
leave;
}
} finally {
if ( sd != NULL ) {
FltFreeSecurityDescriptor( sd );
}
if (!NT_SUCCESS( status ) ) {
if (NULL != Globals.ScanServerPort) {
FltCloseCommunicationPort( Globals.ScanServerPort );
}
if (NULL != Globals.AbortServerPort) {
FltCloseCommunicationPort( Globals.AbortServerPort );
}
if (NULL != Globals.QueryServerPort) {
FltCloseCommunicationPort( Globals.QueryServerPort );
}
if (NULL != Globals.Filter) {
FltUnregisterFilter( Globals.Filter );
Globals.Filter = NULL;
}
ExDeleteResourceLite( &Globals.ScanCtxListLock );
}
}
return status;
}
NTSTATUS
AvUnload (
_Unreferenced_parameter_ FLT_FILTER_UNLOAD_FLAGS Flags
)
/*++
Routine Description:
This is the unload routine for this miniFilter driver. This is called
when the minifilter is about to be unloaded. We can fail this unload
request if this is not a mandatory unloaded indicated by the Flags
parameter.
Arguments:
Flags - Indicating if this is a mandatory unload.
Return Value:
Returns the final status of this operation.
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER( Flags );
AV_DBG_PRINT( AVDBG_TRACE_DEBUG,
("[AV] AvUnload: Entered\n") );
//
// Traverse the scan context list, and cancel the scan if it exists.
//
AvAcquireResourceExclusive( &Globals.ScanCtxListLock );
Globals.Unloading = TRUE;
AvReleaseResource( &Globals.ScanCtxListLock );
//
// This function will wait for the user to abort the outstanding scan and
// close the section
//
AvSendUnloadingToUser();
FltCloseCommunicationPort( Globals.ScanServerPort );
Globals.ScanServerPort = NULL;
FltCloseCommunicationPort( Globals.AbortServerPort );
Globals.AbortServerPort = NULL;
FltCloseCommunicationPort( Globals.QueryServerPort );
Globals.QueryServerPort = NULL;
FltUnregisterFilter( Globals.Filter ); // This will typically trigger instance tear down.
Globals.Filter = NULL;
ExDeleteResourceLite( &Globals.ScanCtxListLock );
return STATUS_SUCCESS;
}
/*************************************************************************
Local utility routines.
*************************************************************************/
BOOLEAN
AvOperationsModifyingFile (
_In_ PFLT_CALLBACK_DATA Data
)
/*++
Routine Description:
This identifies those operations we need to set the file to be modified.
Arguments:
Data - Pointer to the filter callbackData that is passed to us.
Return Value:
TRUE - If we want the file associated with the request to be modified.
FALSE - If we don't
--*/
{
PFLT_IO_PARAMETER_BLOCK iopb = Data->Iopb;
PAGED_CODE();
switch(iopb->MajorFunction) {
case IRP_MJ_WRITE:
return TRUE;
case IRP_MJ_FILE_SYSTEM_CONTROL:
switch ( iopb->Parameters.FileSystemControl.Common.FsControlCode ) {
case FSCTL_OFFLOAD_WRITE:
case FSCTL_WRITE_RAW_ENCRYPTED:
case FSCTL_SET_ZERO_DATA:
return TRUE;
default: break;
}
break;
case IRP_MJ_SET_INFORMATION:
switch ( iopb->Parameters.SetFileInformation.FileInformationClass ) {
case FileEndOfFileInformation:
case FileValidDataLengthInformation:
return TRUE;
default: break;
}
break;
default:
break;
}
return FALSE;
}
NTSTATUS
AvQueryTransactionOutcome(
_In_ PKTRANSACTION Transaction,
_Out_ PULONG TxOutcome
)
/*++
Routine Description:
This is a helper function that qeury the KTM that how trasnaction was ended.
Arguments:
Transaction - Pointer to transaction object.
TxOutcome - Output. Specifies the type of transaction outcome.
Return Value:
The status of the operation
--*/
{
HANDLE transactionHandle;
NTSTATUS status;
TRANSACTION_BASIC_INFORMATION txBasicInfo = {0};
PAGED_CODE();
status = ObOpenObjectByPointer( Transaction,
OBJ_KERNEL_HANDLE,
NULL,
GENERIC_READ,
*TmTransactionObjectType,
KernelMode,
&transactionHandle );
if (!NT_SUCCESS(status)) {