/
scanner.c
1843 lines (1286 loc) · 45.4 KB
/
scanner.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) 1999-2002 Microsoft Corporation
Module Name:
scanner.c
Abstract:
This is the main module of the scanner filter.
This filter scans the data in a file before allowing an open to proceed. This is similar
to what virus checkers do.
Environment:
Kernel mode
--*/
#include <fltKernel.h>
#include <dontuse.h>
#include <suppress.h>
#include "scanuk.h"
#include "scanner.h"
#pragma prefast(disable:__WARNING_ENCODE_MEMBER_FUNCTION_POINTER, "Not valid for kernel mode drivers")
#define SCANNER_REG_TAG 'Rncs'
#define SCANNER_STRING_TAG 'Sncs'
//
// Structure that contains all the global data structures
// used throughout the scanner.
//
SCANNER_DATA ScannerData;
//
// This is a static list of file name extensions files we are interested in scanning
//
PUNICODE_STRING ScannedExtensions;
ULONG ScannedExtensionCount;
//
// The default extension to scan if not configured in the registry
//
UNICODE_STRING ScannedExtensionDefault = RTL_CONSTANT_STRING( L"doc" );
//
// Function prototypes
//
typedef
NTSTATUS
(*PFN_IoOpenDriverRegistryKey) (
PDRIVER_OBJECT DriverObject,
DRIVER_REGKEY_TYPE RegKeyType,
ACCESS_MASK DesiredAccess,
ULONG Flags,
PHANDLE DriverRegKey
);
PFN_IoOpenDriverRegistryKey
ScannerGetIoOpenDriverRegistryKey (
VOID
);
NTSTATUS
ScannerOpenServiceParametersKey (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING ServiceRegistryPath,
_Out_ PHANDLE ServiceParametersKey
);
NTSTATUS
ScannerInitializeScannedExtensions(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
VOID
ScannerFreeExtensions(
);
NTSTATUS
ScannerAllocateUnicodeString (
_Inout_ PUNICODE_STRING String
);
VOID
ScannerFreeUnicodeString (
_Inout_ PUNICODE_STRING String
);
NTSTATUS
ScannerPortConnect (
_In_ PFLT_PORT ClientPort,
_In_opt_ PVOID ServerPortCookie,
_In_reads_bytes_opt_(SizeOfContext) PVOID ConnectionContext,
_In_ ULONG SizeOfContext,
_Outptr_result_maybenull_ PVOID *ConnectionCookie
);
VOID
ScannerPortDisconnect (
_In_opt_ PVOID ConnectionCookie
);
NTSTATUS
ScannerpScanFileInUserMode (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_ PBOOLEAN SafeToOpen
);
BOOLEAN
ScannerpCheckExtension (
_In_ PUNICODE_STRING Extension
);
//
// Assign text sections for each routine.
//
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(INIT, ScannerGetIoOpenDriverRegistryKey)
#pragma alloc_text(INIT, ScannerOpenServiceParametersKey)
#pragma alloc_text(INIT, ScannerInitializeScannedExtensions)
#pragma alloc_text(PAGE, ScannerInstanceSetup)
#pragma alloc_text(PAGE, ScannerPreCreate)
#pragma alloc_text(PAGE, ScannerPortConnect)
#pragma alloc_text(PAGE, ScannerPortDisconnect)
#pragma alloc_text(PAGE, ScannerFreeExtensions)
#pragma alloc_text(PAGE, ScannerAllocateUnicodeString)
#pragma alloc_text(PAGE, ScannerFreeUnicodeString)
#endif
//
// Constant FLT_REGISTRATION structure for our filter. This
// initializes the callback routines our filter wants to register
// for. This is only used to register with the filter manager
//
const FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE,
0,
ScannerPreCreate,
ScannerPostCreate},
{ IRP_MJ_CLEANUP,
0,
ScannerPreCleanup,
NULL},
{ IRP_MJ_WRITE,
0,
ScannerPreWrite,
NULL},
#if (WINVER>=0x0602)
{ IRP_MJ_FILE_SYSTEM_CONTROL,
0,
ScannerPreFileSystemControl,
NULL
},
#endif
{ IRP_MJ_OPERATION_END}
};
const FLT_CONTEXT_REGISTRATION ContextRegistration[] = {
{ FLT_STREAMHANDLE_CONTEXT,
0,
NULL,
sizeof(SCANNER_STREAM_HANDLE_CONTEXT),
'chBS' },
{ FLT_CONTEXT_END }
};
const FLT_REGISTRATION FilterRegistration = {
sizeof( FLT_REGISTRATION ), // Size
FLT_REGISTRATION_VERSION, // Version
0, // Flags
ContextRegistration, // Context Registration.
Callbacks, // Operation callbacks
ScannerUnload, // FilterUnload
ScannerInstanceSetup, // InstanceSetup
ScannerQueryTeardown, // InstanceQueryTeardown
NULL, // InstanceTeardownStart
NULL, // InstanceTeardownComplete
NULL, // GenerateFileName
NULL, // GenerateDestinationFileName
NULL // NormalizeNameComponent
};
////////////////////////////////////////////////////////////////////////////
//
// Filter initialization and unload routines.
//
////////////////////////////////////////////////////////////////////////////
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
This is the initialization routine for the Filter driver. This
registers the Filter with the filter manager and initializes all
its 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 STATUS_SUCCESS.
--*/
{
OBJECT_ATTRIBUTES oa;
UNICODE_STRING uniString;
PSECURITY_DESCRIPTOR sd;
NTSTATUS status;
//
// Default to NonPagedPoolNx for non paged pool allocations where supported.
//
ExInitializeDriverRuntime( DrvRtPoolNxOptIn );
//
// Register with filter manager.
//
status = FltRegisterFilter( DriverObject,
&FilterRegistration,
&ScannerData.Filter );
if (!NT_SUCCESS( status )) {
return status;
}
//
// Obtain the extensions to scan from the registry
//
status = ScannerInitializeScannedExtensions( DriverObject, RegistryPath );
if (!NT_SUCCESS( status )) {
status = STATUS_SUCCESS;
ScannedExtensions = &ScannedExtensionDefault;
ScannedExtensionCount = 1;
}
//
// Create a communication port.
//
RtlInitUnicodeString( &uniString, ScannerPortName );
//
// We secure the port so only ADMINs & SYSTEM can acecss it.
//
status = FltBuildDefaultSecurityDescriptor( &sd, FLT_PORT_ALL_ACCESS );
if (NT_SUCCESS( status )) {
InitializeObjectAttributes( &oa,
&uniString,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
sd );
status = FltCreateCommunicationPort( ScannerData.Filter,
&ScannerData.ServerPort,
&oa,
NULL,
ScannerPortConnect,
ScannerPortDisconnect,
NULL,
1 );
//
// Free the security descriptor in all cases. It is not needed once
// the call to FltCreateCommunicationPort() is made.
//
FltFreeSecurityDescriptor( sd );
if (NT_SUCCESS( status )) {
//
// Start filtering I/O.
//
status = FltStartFiltering( ScannerData.Filter );
if (NT_SUCCESS( status )) {
return STATUS_SUCCESS;
}
FltCloseCommunicationPort( ScannerData.ServerPort );
}
}
ScannerFreeExtensions();
FltUnregisterFilter( ScannerData.Filter );
return status;
}
PFN_IoOpenDriverRegistryKey
ScannerGetIoOpenDriverRegistryKey (
VOID
)
{
static PFN_IoOpenDriverRegistryKey pIoOpenDriverRegistryKey = NULL;
UNICODE_STRING FunctionName = {0};
if (pIoOpenDriverRegistryKey == NULL) {
RtlInitUnicodeString(&FunctionName, L"IoOpenDriverRegistryKey");
pIoOpenDriverRegistryKey = (PFN_IoOpenDriverRegistryKey)MmGetSystemRoutineAddress(&FunctionName);
}
return pIoOpenDriverRegistryKey;
}
NTSTATUS
ScannerOpenServiceParametersKey (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING ServiceRegistryPath,
_Out_ PHANDLE ServiceParametersKey
)
/*++
Routine Description:
This routine opens the service parameters key, using the isolation-compliant
APIs when possible.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - The path key passed to the driver during DriverEntry.
ServiceParametersKey - Returns a handle to the service parameters subkey.
Return Value:
STATUS_SUCCESS if the function completes successfully. Otherwise a valid
NTSTATUS code is returned.
--*/
{
NTSTATUS status;
PFN_IoOpenDriverRegistryKey pIoOpenDriverRegistryKey;
UNICODE_STRING Subkey;
HANDLE ParametersKey = NULL;
HANDLE ServiceRegKey = NULL;
OBJECT_ATTRIBUTES Attributes;
//
// Open the parameters key to read values from the INF, using the API to
// open the key if possible
//
pIoOpenDriverRegistryKey = ScannerGetIoOpenDriverRegistryKey();
if (pIoOpenDriverRegistryKey != NULL) {
//
// Open the parameters key using the API
//
status = pIoOpenDriverRegistryKey( DriverObject,
DriverRegKeyParameters,
KEY_READ,
0,
&ParametersKey );
if (!NT_SUCCESS( status )) {
goto ScannerOpenServiceParametersKeyCleanup;
}
} else {
//
// Open specified service root key
//
InitializeObjectAttributes( &Attributes,
ServiceRegistryPath,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
NULL );
status = ZwOpenKey( &ServiceRegKey,
KEY_READ,
&Attributes );
if (!NT_SUCCESS( status )) {
goto ScannerOpenServiceParametersKeyCleanup;
}
//
// Open the parameters key relative to service key path
//
RtlInitUnicodeString( &Subkey, L"Parameters" );
InitializeObjectAttributes( &Attributes,
&Subkey,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
ServiceRegKey,
NULL );
status = ZwOpenKey( &ParametersKey,
KEY_READ,
&Attributes );
if (!NT_SUCCESS( status )) {
goto ScannerOpenServiceParametersKeyCleanup;
}
}
//
// Return value to caller
//
*ServiceParametersKey = ParametersKey;
ScannerOpenServiceParametersKeyCleanup:
if (ServiceRegKey != NULL) {
ZwClose( ServiceRegKey );
}
return status;
}
NTSTATUS
ScannerInitializeScannedExtensions(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Descrition:
This routine sets the the extensions for files to be scanned based
on the registry.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - The path key passed to the driver during DriverEntry.
Return Value:
STATUS_SUCCESS if the function completes successfully. Otherwise a valid
NTSTATUS code is returned.
--*/
{
NTSTATUS status;
HANDLE driverRegKey = NULL;
UNICODE_STRING valueName;
PKEY_VALUE_PARTIAL_INFORMATION valueBuffer = NULL;
ULONG valueLength = 0;
PWCHAR ch;
SIZE_T length;
ULONG count;
PUNICODE_STRING ext;
PAGED_CODE();
ScannedExtensions = NULL;
ScannedExtensionCount = 0;
//
// Open service parameters key to query values from.
//
status = ScannerOpenServiceParametersKey( DriverObject,
RegistryPath,
&driverRegKey );
if (!NT_SUCCESS( status )) {
driverRegKey = NULL;
goto ScannerInitializeScannedExtensionsCleanup;
}
//
// Query the length of the reg value
//
RtlInitUnicodeString( &valueName, L"Extensions" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
NULL,
0,
&valueLength );
if (status!=STATUS_BUFFER_TOO_SMALL && status!=STATUS_BUFFER_OVERFLOW) {
status = STATUS_INVALID_PARAMETER;
goto ScannerInitializeScannedExtensionsCleanup;
}
//
// Extract the path.
//
valueBuffer = ExAllocatePoolZero( NonPagedPool,
valueLength,
SCANNER_REG_TAG );
if (valueBuffer == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto ScannerInitializeScannedExtensionsCleanup;
}
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
valueBuffer,
valueLength,
&valueLength );
if (!NT_SUCCESS( status )) {
goto ScannerInitializeScannedExtensionsCleanup;
}
ch = (PWCHAR)(valueBuffer->Data);
count = 0;
//
// Count how many strings are in the multi string
//
while (*ch != '\0') {
ch = ch + wcslen( ch ) + 1;
count++;
}
ScannedExtensions = ExAllocatePoolZero( PagedPool,
count * sizeof(UNICODE_STRING),
SCANNER_STRING_TAG );
if (ScannedExtensions == NULL) {
goto ScannerInitializeScannedExtensionsCleanup;
}
ch = (PWCHAR)((PKEY_VALUE_PARTIAL_INFORMATION)valueBuffer->Data);
ext = ScannedExtensions;
while (ScannedExtensionCount < count) {
length = wcslen( ch ) * sizeof(WCHAR);
ext->MaximumLength = (USHORT) length;
status = ScannerAllocateUnicodeString( ext );
if (!NT_SUCCESS( status )) {
goto ScannerInitializeScannedExtensionsCleanup;
}
ext->Length = (USHORT)length;
RtlCopyMemory( ext->Buffer, ch, length );
ch = ch + length/sizeof(WCHAR) + 1;
ScannedExtensionCount++;
ext++;
}
ScannerInitializeScannedExtensionsCleanup:
//
// Note that this function leaks the global buffers.
// On failure DriverEntry will clean up the globals
// so we don't have to do that here.
//
if (valueBuffer != NULL) {
ExFreePoolWithTag( valueBuffer, SCANNER_REG_TAG );
valueBuffer = NULL;
}
if (driverRegKey != NULL) {
ZwClose( driverRegKey );
}
if (!NT_SUCCESS( status )) {
ScannerFreeExtensions();
}
return status;
}
VOID
ScannerFreeExtensions(
)
/*++
Routine Descrition:
This routine cleans up the global buffers on both
teardown and initialization failure.
Arguments:
Return Value:
None.
--*/
{
PAGED_CODE();
//
// Free the strings in the scanned extension array
//
while (ScannedExtensionCount > 0) {
ScannedExtensionCount--;
if (ScannedExtensions != &ScannedExtensionDefault) {
ScannerFreeUnicodeString( ScannedExtensions + ScannedExtensionCount );
}
}
if (ScannedExtensions != &ScannedExtensionDefault && ScannedExtensions != NULL) {
ExFreePoolWithTag( ScannedExtensions, SCANNER_STRING_TAG );
}
ScannedExtensions = NULL;
}
NTSTATUS
ScannerAllocateUnicodeString (
_Inout_ PUNICODE_STRING String
)
/*++
Routine Description:
This routine allocates a unicode string
Arguments:
String - supplies the size of the string to be allocated in the MaximumLength field
return the unicode string
Return Value:
STATUS_SUCCESS - success
STATUS_INSUFFICIENT_RESOURCES - failure
--*/
{
PAGED_CODE();
String->Buffer = ExAllocatePoolZero( NonPagedPool,
String->MaximumLength,
SCANNER_STRING_TAG );
if (String->Buffer == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
}
String->Length = 0;
return STATUS_SUCCESS;
}
VOID
ScannerFreeUnicodeString (
_Inout_ PUNICODE_STRING String
)
/*++
Routine Description:
This routine frees a unicode string
Arguments:
String - supplies the string to be freed
Return Value:
None
--*/
{
PAGED_CODE();
if (String->Buffer) {
ExFreePoolWithTag( String->Buffer,
SCANNER_STRING_TAG );
String->Buffer = NULL;
}
String->Length = String->MaximumLength = 0;
String->Buffer = NULL;
}
NTSTATUS
ScannerPortConnect (
_In_ PFLT_PORT ClientPort,
_In_opt_ PVOID ServerPortCookie,
_In_reads_bytes_opt_(SizeOfContext) PVOID ConnectionContext,
_In_ ULONG SizeOfContext,
_Outptr_result_maybenull_ PVOID *ConnectionCookie
)
/*++
Routine Description
This is called when user-mode connects to the server port - to establish a
connection
Arguments
ClientPort - This is the client connection port that will be used to
send messages from the filter
ServerPortCookie - The context associated with this port when the
minifilter created this port.
ConnectionContext - Context from entity connecting to this port (most likely
your user mode service)
SizeofContext - Size of ConnectionContext in bytes
ConnectionCookie - Context to be passed to the port disconnect routine.
Return Value
STATUS_SUCCESS - to accept the connection
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER( ServerPortCookie );
UNREFERENCED_PARAMETER( ConnectionContext );
UNREFERENCED_PARAMETER( SizeOfContext);
UNREFERENCED_PARAMETER( ConnectionCookie = NULL );
FLT_ASSERT( ScannerData.ClientPort == NULL );
FLT_ASSERT( ScannerData.UserProcess == NULL );
//
// Set the user process and port. In a production filter it may
// be necessary to synchronize access to such fields with port
// lifetime. For instance, while filter manager will synchronize
// FltCloseClientPort with FltSendMessage's reading of the port
// handle, synchronizing access to the UserProcess would be up to
// the filter.
//
ScannerData.UserProcess = PsGetCurrentProcess();
ScannerData.ClientPort = ClientPort;
DbgPrint( "!!! scanner.sys --- connected, port=0x%p\n", ClientPort );
return STATUS_SUCCESS;
}
VOID
ScannerPortDisconnect(
_In_opt_ PVOID ConnectionCookie
)
/*++
Routine Description
This is called when the connection is torn-down. We use it to close our
handle to the connection
Arguments
ConnectionCookie - Context from the port connect routine
Return value
None
--*/
{
UNREFERENCED_PARAMETER( ConnectionCookie );
PAGED_CODE();
DbgPrint( "!!! scanner.sys --- disconnected, port=0x%p\n", ScannerData.ClientPort );
//
// Close our handle to the connection: note, since we limited max connections to 1,
// another connect will not be allowed until we return from the disconnect routine.
//
FltCloseClientPort( ScannerData.Filter, &ScannerData.ClientPort );
//
// Reset the user-process field.
//
ScannerData.UserProcess = NULL;
}
NTSTATUS
ScannerUnload (
_In_ FLT_FILTER_UNLOAD_FLAGS Flags
)
/*++
Routine Description:
This is the unload routine for the Filter driver. This unregisters the
Filter with the filter manager and frees any allocated global data
structures.
Arguments:
None.
Return Value:
Returns the final status of the deallocation routines.
--*/
{
UNREFERENCED_PARAMETER( Flags );
ScannerFreeExtensions();
//
// Close the server port.
//
FltCloseCommunicationPort( ScannerData.ServerPort );
//
// Unregister the filter
//
FltUnregisterFilter( ScannerData.Filter );
return STATUS_SUCCESS;
}
NTSTATUS
ScannerInstanceSetup (
_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 by the filter manager when a new instance is created.
We specified in the registry that we only want for manual attachments,
so that is all we should receive here.
Arguments:
FltObjects - Describes the instance and volume which we are being asked to
setup.
Flags - Flags describing the type of attachment this is.
VolumeDeviceType - The DEVICE_TYPE for the volume to which this instance
will attach.
VolumeFileSystemType - The file system formatted on this volume.
Return Value:
STATUS_SUCCESS - we wish to attach to the volume
STATUS_FLT_DO_NOT_ATTACH - no, thank you
--*/
{
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( Flags );
UNREFERENCED_PARAMETER( VolumeFilesystemType );
PAGED_CODE();
FLT_ASSERT( FltObjects->Filter == ScannerData.Filter );
//
// Don't attach to network volumes.
//
if (VolumeDeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM) {
return STATUS_FLT_DO_NOT_ATTACH;
}
return STATUS_SUCCESS;
}
NTSTATUS
ScannerQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
)
/*++
Routine Description:
This is the instance detach routine for the filter. This
routine is called by filter manager when a user initiates a manual instance
detach. This is a 'query' routine: if the filter does not want to support
manual detach, it can return a failure status
Arguments:
FltObjects - Describes the instance and volume for which we are receiving
this query teardown request.
Flags - Unused
Return Value:
STATUS_SUCCESS - we allow instance detach to happen
--*/