-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
simrep.c
3177 lines (2276 loc) · 91.2 KB
/
simrep.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:
SimRep.c
Abstract:
The Simulate Reparse Sample demonstrates how to return STATUS_REPARSE
on precreates. This allows the filter to redirect opens down one path
to another path. The Precreate path is complicated by network query opens
which come down as Fast IO. Fast IO cannot be redirected with Status Reparse
because reparse only works on IRP based IO.
Simulating reparse points requires that the filter replace the name in the
file object. This will cause Driver Verifier to complain that the filter is
leaking pool and will prevent it from being unloaded. To solve this issue
SimRep attempts to use a Windows 7 Function called IoReplaceFileObjectName
which will allow IO Mgr to replace the name for us with the correct pool tag.
However, on downlevel OS Versions SimRep will go ahead and replace the name
itself.
It is important to note that SimRep only demonstrates how to return
STATUS_REPARSE, not how to deal with file names on NT. SimRep uses two strings
to act as a mapping. When the file open name starts with the "old name mapping"
string the filter replaces it with the "new name mapping" string. This does not
take short names into account.
SimRep can also be configured to redirect renames and creation of hardlinks.
This functionality is demonstrated in the code and can be turned on with a
registry key value indicated in the inf file. To correctly handle rename and
set link operations:
1. SimRep has to reparse opens with the SL_OPEN_TARGET_DIRECTORY flag set in
the pre-create, since this is the create that IoManager uses to open the
target of the rename.
2. SimRep implements a "pass-through" name provider. It needs to do this so
that the creates issued to resolve normalized name queries will be seen by
SimRep and it can redirect them correctly, so as to provide consistent names
to other filters.
3. SimRep has to monitor IRP_MJ_SET_INFORMATION for rename and set link
operations and re-issue them for the correct destination so that filters
below SimRep are made aware of this redirection.
Note that SimRep simply redirects creates (and optionally renames and set
hardlink) operations. It makes no attempt to virtualize the namespace for
filters above SimRep. So the layers above SimRep will be aware of the
redirection if they query the name of the file once the create, rename or set
hardlink operation is complete.
Environment:
Kernel mode
--*/
//
// Enabled warnings
//
#pragma warning(error:4100) // Enable-Unreferenced formal parameter
#pragma warning(error:4101) // Enable-Unreferenced local variable
#pragma warning(error:4061) // Enable-missing enumeration in switch statement
#pragma warning(error:4505) // Enable-identify dead functions
//
// Includes
//
//
// This sample contains OS version specific code. If compiled for VISTA it
// will not run properly on older versions of Windows.
//
#define SIMREP_VISTA (NTDDI_VERSION >= NTDDI_VISTA)
#include <fltKernel.h>
//
// Memory Pool Tags
//
#define SIMREP_STRING_TAG 'tSpR'
#define SIMREP_REG_TAG 'eRpR'
//
// Constants
//
#define REPLACE_ROUTINE_NAME_STRING L"IoReplaceFileObjectName"
#define REPLACE_QUERY_DIRECTORY_FILE_ROUTINE_NAME_STRING "FltQueryDirectoryFile"
//
// Context sample filter global data structures.
//
typedef struct _MAPPING_ENTRY {
//
// Path underwhich we want to reparse.
//
UNICODE_STRING OldName;
//
// Path to reparse to.
//
UNICODE_STRING NewName;
} MAPPING_ENTRY, *PMAPPING_ENTRY;
//
// Starting with windows 7, the IO Manager provides IoReplaceFileObjectName,
// but old versions of Windows will not have this function. Rather than just
// writing our own function, and forfeiting future windows functionality, we can
// use MmGetRoutineAddr, which will allow us to dynamically import IoReplaceFileObjectName
// if it exists. If not it allows us to implement the function ourselves.
//
typedef
NTSTATUS
(* PReplaceFileObjectName ) (
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(FileNameLength) PWSTR NewFileName,
_In_ USHORT FileNameLength
);
typedef
NTSTATUS
(FLTAPI *PFltQueryDirectoryFile)(
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(Length) PVOID FileInformationBuffer,
_In_ ULONG Length,
_In_ FILE_INFORMATION_CLASS FileInformationClass,
_In_ BOOLEAN ReturnSingleEntry,
_In_opt_ PUNICODE_STRING FileName,
_In_ BOOLEAN RestartScan,
_Out_opt_ PULONG LengthReturned
);
typedef struct _SIMREP_GLOBAL_DATA {
//
// Handle to minifilter returned from FltRegisterFilter()
//
PFLT_FILTER Filter;
//
// Structure to hold mapping information.
//
MAPPING_ENTRY Mapping;
//
// Pointer to the function we will use to
// replace file names.
//
PReplaceFileObjectName ReplaceFileNameFunction;
//
// Pointer to the function we will use to
// query directory file.
//
PFltQueryDirectoryFile QueryDirectoryFileFunction;
//
// Flag to control if the filter remaps renames
//
BOOLEAN RemapRenamesAndLinks;
#if DBG
//
// Field to control nature of debug output
//
ULONG DebugLevel;
#endif
} SIMREP_GLOBAL_DATA, *PSIMREP_GLOBAL_DATA;
//
// Debug helper functions
//
#if DBG
#define DEBUG_TRACE_ERROR 0x00000001 // Errors - whenever we return a failure code
#define DEBUG_TRACE_LOAD_UNLOAD 0x00000002 // Loading/unloading of the filter
#define DEBUG_TRACE_INSTANCES 0x00000004 // Attach / detach of instances
#define DEBUG_TRACE_REPARSE_OPERATIONS 0x00000008 // Operations that are performed to determine if we should return STATUS_REPARSE
#define DEBUG_TRACE_REPARSED_OPERATIONS 0x00000010 // Operations that return STATUS_REPARSE
#define DEBUG_TRACE_REPARSED_REISSUE 0X00000020 // Operations that need to be reissued with an IRP.
#define DEBUG_TRACE_NAME_OPERATIONS 0x00000040 // Operations involving name provider callbacks
#define DEBUG_TRACE_RENAME_REDIRECTION_OPERATIONS 0x00000080 // Operations involving rename or hardlink redirection
#define DEBUG_TRACE_ALL_IO 0x00000100 // All IO operations tracked by this filter
#define DEBUG_TRACE_ALL 0xFFFFFFFF // All flags
#define DebugTrace(Level, Data) \
if ((Level) & Globals.DebugLevel) { \
DbgPrint Data; \
}
#else
#define DebugTrace(Level, Data) {NOTHING;}
#endif
//
// Function that handle driver load/unload and instance setup/cleanup
//
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
SimRepGetIoOpenDriverRegistryKey (
VOID
);
NTSTATUS
SimRepOpenServiceParametersKey (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING ServiceRegistryPath,
_Out_ PHANDLE ServiceParametersKey
);
NTSTATUS
SimRepSetConfiguration(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
VOID SimRepFreeGlobals(
);
NTSTATUS
SimRepUnload (
FLT_FILTER_UNLOAD_FLAGS Flags
);
NTSTATUS
SimRepInstanceSetup (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
);
NTSTATUS
SimRepInstanceQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
);
//
// Functions that track operations on the volume
//
FLT_PREOP_CALLBACK_STATUS
SimRepPreCreate (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
FLT_PREOP_CALLBACK_STATUS
SimRepPreNetworkQueryOpen (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
//
// Functions to support rename and hard link creation remapping
//
FLT_PREOP_CALLBACK_STATUS
SimRepPreSetInformation (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
//
// Functions that provide string allocation support
//
_When_(return==0, _Post_satisfies_(String->Buffer != NULL))
NTSTATUS
SimRepAllocateUnicodeString (
_Inout_ PUNICODE_STRING String
);
VOID
SimRepFreeUnicodeString (
_Inout_ PUNICODE_STRING String
);
NTSTATUS
SimRepReplaceFileObjectName (
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(FileNameLength) PWSTR NewFileName,
_In_ USHORT FileNameLength
);
BOOLEAN
SimRepCompareMapping(
_In_ PFLT_FILE_NAME_INFORMATION NameInfo,
_In_ PUNICODE_STRING MappingPath,
_In_ BOOLEAN IgnoreCase,
_Out_opt_ PBOOLEAN ExactMatch
);
NTSTATUS
SimRepMungeName(
_In_ PFLT_FILE_NAME_INFORMATION NameInfo,
_In_ PUNICODE_STRING SubPath,
_In_ PUNICODE_STRING NewSubPath,
_In_ BOOLEAN IgnoreCase,
_In_ BOOLEAN ExactMatch,
_Out_ PUNICODE_STRING MungedPath
);
//
// Functions that implement a pass through name provider
//
NTSTATUS
SimRepGenerateFileName (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_When_(FileObject->FsContext != NULL, _In_opt_)
_When_(FileObject->FsContext == NULL, _In_)
PFLT_CALLBACK_DATA Cbd,
_In_ FLT_FILE_NAME_OPTIONS NameOptions,
_Out_ PBOOLEAN CacheFileNameInformation,
_Inout_ PFLT_NAME_CONTROL FileName
);
NTSTATUS
SimRepNormalizeNameComponent (
_In_ PFLT_INSTANCE Instance,
_In_ PCUNICODE_STRING ParentDirectory,
_In_ USHORT DeviceNameLength,
_In_ PCUNICODE_STRING Component,
_Out_writes_bytes_(ExpandComponentNameLength) PFILE_NAMES_INFORMATION ExpandComponentName,
_In_ ULONG ExpandComponentNameLength,
_In_ FLT_NORMALIZE_NAME_FLAGS Flags,
_Inout_ PVOID *NormalizationContext
);
#if SIMREP_VISTA
NTSTATUS
SimRepNormalizeNameComponentEx (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_In_ PCUNICODE_STRING ParentDirectory,
_In_ USHORT DeviceNameLength,
_In_ PCUNICODE_STRING Component,
_Out_writes_bytes_(ExpandComponentNameLength) PFILE_NAMES_INFORMATION ExpandComponentName,
_In_ ULONG ExpandComponentNameLength,
_In_ FLT_NORMALIZE_NAME_FLAGS Flags,
_Inout_ PVOID *NormalizationContext
);
#endif
NTSTATUS
SimRepQueryDirectoryFile (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_writes_bytes_(Length) PVOID FileInformationBuffer,
_In_ ULONG Length,
_In_ FILE_INFORMATION_CLASS FileInformationClass,
_In_ BOOLEAN ReturnSingleEntry,
_In_opt_ PUNICODE_STRING FileName,
_In_ BOOLEAN RestartScan,
_Out_opt_ PULONG LengthReturned
);
//
// Filter callback routines
//
FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreCreate,
NULL },
{ IRP_MJ_NETWORK_QUERY_OPEN,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreNetworkQueryOpen,
NULL },
{ IRP_MJ_OPERATION_END }
};
//
// Filter registration data structure
//
FLT_REGISTRATION FilterRegistration = {
sizeof( FLT_REGISTRATION ), // Size
FLT_REGISTRATION_VERSION, // Version
0, // Flags
NULL, // Context
Callbacks, // Operation callbacks
SimRepUnload, // Filters unload routine
SimRepInstanceSetup, // InstanceSetup routine
SimRepInstanceQueryTeardown, // InstanceQueryTeardown routine
NULL, // InstanceTeardownStart routine
NULL, // InstanceTeardownComplete routine
NULL, // Filename generation support callback
NULL, // Filename normalization support callback
NULL, // Normalize name component cleanup callback
#if SIMREP_VISTA
NULL, // Transaction notification callback
NULL // Filename normalization support callback
#endif // SIMREP_VISTA
};
//
// Filter callback routines with rename handling
//
FLT_OPERATION_REGISTRATION CallbacksWithRename[] = {
{ IRP_MJ_CREATE,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreCreate,
NULL },
{ IRP_MJ_NETWORK_QUERY_OPEN,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreNetworkQueryOpen,
NULL },
{ IRP_MJ_SET_INFORMATION,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreSetInformation,
NULL },
{ IRP_MJ_OPERATION_END }
};
//
// Filter registration data structure with renames
// Filter registers as a name provider and for SetInformation
//
FLT_REGISTRATION FilterRegistrationWithRename = {
sizeof( FLT_REGISTRATION ), // Size
FLT_REGISTRATION_VERSION, // Version
0, // Flags
NULL, // Context
CallbacksWithRename, // Operation callbacks
SimRepUnload, // Filters unload routine
SimRepInstanceSetup, // InstanceSetup routine
SimRepInstanceQueryTeardown, // InstanceQueryTeardown routine
NULL, // InstanceTeardownStart routine
NULL, // InstanceTeardownComplete routine
SimRepGenerateFileName, // Filename generation support callback
SimRepNormalizeNameComponent, // Filename normalization support callback
NULL, // Normalize name component cleanup callback
#if SIMREP_VISTA
NULL, // Transaction notification callback
SimRepNormalizeNameComponentEx // Filename normalization support callback
#endif // SIMREP_VISTA
};
//
// Global variables
//
SIMREP_GLOBAL_DATA Globals;
//
// Assign text sections for each routine.
//
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(INIT, SimRepGetIoOpenDriverRegistryKey)
#pragma alloc_text(INIT, SimRepOpenServiceParametersKey)
#pragma alloc_text(INIT, SimRepSetConfiguration)
#pragma alloc_text(PAGE, SimRepUnload)
#pragma alloc_text(PAGE, SimRepInstanceSetup)
#pragma alloc_text(PAGE, SimRepInstanceQueryTeardown)
#pragma alloc_text(PAGE, SimRepAllocateUnicodeString)
#pragma alloc_text(PAGE, SimRepFreeUnicodeString)
#pragma alloc_text(PAGE, SimRepReplaceFileObjectName)
#pragma alloc_text(PAGE, SimRepCompareMapping)
#pragma alloc_text(PAGE, SimRepMungeName)
#pragma alloc_text(PAGE, SimRepPreCreate)
#pragma alloc_text(PAGE, SimRepPreNetworkQueryOpen)
#pragma alloc_text(PAGE, SimRepPreSetInformation)
#pragma alloc_text(PAGE, SimRepFreeGlobals)
#pragma alloc_text(PAGE, SimRepGenerateFileName)
#pragma alloc_text(PAGE, SimRepNormalizeNameComponent)
#if SIMREP_VISTA
#pragma alloc_text(PAGE, SimRepNormalizeNameComponentEx)
#endif
#pragma alloc_text(PAGE, SimRepQueryDirectoryFile)
#endif
//
// Filter driver initialization and unload routines
//
#pragma warning(push)
#pragma warning(disable:4152) // nonstandard extension, function/data pointer conversion in expression
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
This is the initialization routine for this filter driver. It registers
itself 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.
--*/
{
NTSTATUS status;
UNICODE_STRING replaceRoutineName;
PFLT_REGISTRATION Registration;
//
// Default to NonPagedPoolNx for non paged pool allocations where supported.
//
ExInitializeDriverRuntime( DrvRtPoolNxOptIn );
//
// Set default global configuration
//
#if DBG
Globals.DebugLevel = DEBUG_TRACE_ALL;
#endif
Globals.RemapRenamesAndLinks = FALSE;
RtlInitUnicodeString( &Globals.Mapping.NewName, NULL );
RtlInitUnicodeString( &Globals.Mapping.OldName, NULL );
//
// Import function to replace file names.
//
RtlInitUnicodeString( &replaceRoutineName, REPLACE_ROUTINE_NAME_STRING );
Globals.ReplaceFileNameFunction = MmGetSystemRoutineAddress( &replaceRoutineName );
if (Globals.ReplaceFileNameFunction == NULL) {
Globals.ReplaceFileNameFunction = SimRepReplaceFileObjectName;
}
//
// If available (Windows Vista or later), use the FltQueryDirectoryFile API.
//
Globals.QueryDirectoryFileFunction = FltGetRoutineAddress( REPLACE_QUERY_DIRECTORY_FILE_ROUTINE_NAME_STRING );
//
// Set the filter configuration based on registry keys
//
status = SimRepSetConfiguration( DriverObject, RegistryPath );
DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
("[SimRep]: Driver being loaded\n") );
if (!NT_SUCCESS( status )) {
goto DriverEntryCleanup;
}
//
// Register with the filter manager. If the filter is not
// configured to remap renames and hardlink creation do not
// register name provider or SetInformation callbacks.
//
Registration = (Globals.RemapRenamesAndLinks == FALSE) ?
&FilterRegistration : &FilterRegistrationWithRename;
status = FltRegisterFilter( DriverObject,
Registration,
&Globals.Filter );
if (!NT_SUCCESS( status )) {
goto DriverEntryCleanup;
}
//
// Start filtering I/O
//
status = FltStartFiltering( Globals.Filter );
if (!NT_SUCCESS( status )) {
FltUnregisterFilter( Globals.Filter );
}
DriverEntryCleanup:
DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
("[SimRep]: Driver loaded complete (Status = 0x%08X)\n",
status) );
if (!NT_SUCCESS( status )) {
SimRepFreeGlobals();
}
return status;
}
#pragma warning(pop)
PFN_IoOpenDriverRegistryKey
SimRepGetIoOpenDriverRegistryKey (
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
SimRepOpenServiceParametersKey (
_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 = SimRepGetIoOpenDriverRegistryKey();
if (pIoOpenDriverRegistryKey != NULL) {
//
// Open the parameters key using the API
//
status = pIoOpenDriverRegistryKey( DriverObject,
DriverRegKeyParameters,
KEY_READ,
0,
&ParametersKey );
if (!NT_SUCCESS( status )) {
goto SimRepOpenServiceParametersKeyCleanup;
}
} 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 SimRepOpenServiceParametersKeyCleanup;
}
//
// 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 SimRepOpenServiceParametersKeyCleanup;
}
}
//
// Return value to caller
//
*ServiceParametersKey = ParametersKey;
SimRepOpenServiceParametersKeyCleanup:
if (ServiceRegKey != NULL) {
ZwClose( ServiceRegKey );
}
return status;
}
NTSTATUS
SimRepSetConfiguration(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Descrition:
This routine sets the filter configuration based on registry values.
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:
Returns the status of this operation.
--*/
{
NTSTATUS status;
HANDLE driverRegKey = NULL;
UNICODE_STRING valueName;
UCHAR buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
PKEY_VALUE_PARTIAL_INFORMATION value = (PKEY_VALUE_PARTIAL_INFORMATION)buffer;
ULONG valueLength = sizeof(buffer);
ULONG resultLength;
PKEY_VALUE_PARTIAL_INFORMATION mappingValue = NULL;
ULONG mappingValueLength = 0;
WCHAR oldMappingTail;
WCHAR newMappingTail;
PAGED_CODE();
//
// Open service parameters key to query values from
//
status = SimRepOpenServiceParametersKey( DriverObject,
RegistryPath,
&driverRegKey );
if (!NT_SUCCESS( status )) {
driverRegKey = NULL;
goto SimRepSetConfigurationCleanup;
}
#if DBG
//
// Query the debug level
//
RtlInitUnicodeString( &valueName, L"DebugLevel" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
value,
valueLength,
&resultLength );
if (NT_SUCCESS( status )) {
Globals.DebugLevel = *(PULONG)value->Data;
}
#endif
//
// Query the remap rename flag
//
RtlInitUnicodeString( &valueName, L"RemapRenamesAndLinks" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
value,
valueLength,
&resultLength );
if (NT_SUCCESS( status )) {
Globals.RemapRenamesAndLinks = *(PULONG)value->Data > 0 ? TRUE : FALSE;
}
//
// Query the length of the old mapping.
//
RtlInitUnicodeString( &valueName, L"OldMapping" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
NULL,
0,
&mappingValueLength );
if (status!=STATUS_BUFFER_TOO_SMALL && status!=STATUS_BUFFER_OVERFLOW) {
status = STATUS_INVALID_PARAMETER;
goto SimRepSetConfigurationCleanup;
}
//
// Extract the old mapping string.
//
mappingValue = ExAllocatePoolZero( PagedPool,
mappingValueLength,
SIMREP_REG_TAG );
if (mappingValue == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto SimRepSetConfigurationCleanup;
}
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
mappingValue,
mappingValueLength,
&resultLength );
if (!NT_SUCCESS( status )) {
goto SimRepSetConfigurationCleanup;
}
if (mappingValue->Type != REG_SZ) {
status = STATUS_INVALID_PARAMETER;
goto SimRepSetConfigurationCleanup;
}
Globals.Mapping.OldName.MaximumLength = (USHORT)mappingValue->DataLength;
status = SimRepAllocateUnicodeString( &Globals.Mapping.OldName );
if (!NT_SUCCESS( status )) {
goto SimRepSetConfigurationCleanup;
}
//
// The length which we receive from ZwQueryValueKey contains size for
// the NULL termination as well. Since we are dealing with unicode
// string we'll chop off the null termination in the length.
//
Globals.Mapping.OldName.Length = (USHORT)mappingValue->DataLength - sizeof( UNICODE_NULL );
RtlCopyMemory(Globals.Mapping.OldName.Buffer,
mappingValue->Data,
Globals.Mapping.OldName.Length);
//
// Query the length of the new mapping.
//
RtlInitUnicodeString( &valueName, L"NewMapping" );