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
|
/*++
Copyright (c) 1999 - 2002 Microsoft Corporation
Module Name:
ncmapping.c
Abstract:
This module contains helper routines for manipulating mapping
objects.
Environment:
Kernel mode
--*/
#include "nc.h"
BOOLEAN
NcIsMappingPathZeroed (
_In_ PNC_MAPPING_PATH Path
);
NTSTATUS
NcBuildMappingPath (
_In_ PUNICODE_STRING VolumeName,
_In_ PUNICODE_STRING ParentPath,
_In_ PUNICODE_STRING FinalComponent,
_Inout_ PNC_MAPPING_PATH Path
);
NTSTATUS
NcBuildMappingPathFromFile (
_In_ PFILE_OBJECT Parent,
_In_ PUNICODE_STRING FinalComponent,
_In_ PFLT_INSTANCE Instance,
_In_ BOOLEAN Normalized,
_Inout_ PNC_MAPPING_PATH Path
);
//
// Functions to manage mapping entry
//
BOOLEAN
NcIsMappingEntryZeroed (
PNC_MAPPING_ENTRY Entry
);
VOID
NcInitMappingEntry (
_Inout_ PNC_MAPPING_ENTRY Entry
);
VOID
NcTeardownMappingEntry (
_Inout_ PNC_MAPPING_ENTRY Path
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NcBuildMappingPath)
#pragma alloc_text(PAGE, NcBuildMappingPathFromFile)
#pragma alloc_text(PAGE, NcBuildMappingPathFromVolume)
#pragma alloc_text(PAGE, NcInitMappingPath)
#pragma alloc_text(PAGE, NcIsMappingPathZeroed)
#pragma alloc_text(PAGE, NcTeardownMappingPath)
#pragma alloc_text(PAGE, NcInitMappingEntry)
#pragma alloc_text(PAGE, NcIsMappingEntryZeroed)
#pragma alloc_text(PAGE, NcTeardownMappingEntry)
#pragma alloc_text(PAGE, NcInitMapping)
#pragma alloc_text(PAGE, NcIsMappingZeroed)
#pragma alloc_text(PAGE, NcTeardownMapping)
#pragma alloc_text(PAGE, NcBuildMapping)
#endif
//
// Functions to manage the mapping path.
//
BOOLEAN
NcIsMappingPathZeroed(
_In_ PNC_MAPPING_PATH Path
)
{
PAGED_CODE();
if (Path->FullPath.Buffer != NULL ||
Path->FullPath.Length != 0 ||
Path->FullPath.MaximumLength != 0) {
return FALSE;
}
if (Path->VolumePath.Buffer != NULL ||
Path->VolumePath.Length != 0 ||
Path->VolumePath.MaximumLength != 0) {
return FALSE;
}
if (Path->ParentPath.Buffer != NULL ||
Path->ParentPath.Length != 0 ||
Path->ParentPath.MaximumLength != 0) {
return FALSE;
}
if (Path->FinalComponentName.Buffer != NULL ||
Path->FinalComponentName.Length != 0 ||
Path->FinalComponentName.MaximumLength != 0) {
return FALSE;
}
if (Path->VolumelessName.Buffer != NULL ||
Path->VolumelessName.Length != 0 ||
Path->VolumelessName.MaximumLength != 0) {
return FALSE;
}
return TRUE;
}
VOID
NcInitMappingPath (
_Out_ PNC_MAPPING_PATH Path
)
/*++
Routine Description:
Routine to initialize a mapping path.
Arguments:
Path - Pointer to a user allocated NC_MAPPING_PATH.
Return Value:
None.
--*/
{
PAGED_CODE();
RtlZeroMemory( Path, sizeof( NC_MAPPING_PATH ) );
}
VOID
NcTeardownMappingPath (
_Inout_ PNC_MAPPING_PATH Path
)
/*++
Routine Description:
Frees the allocations in a NC_MAPPING_PATH.
Arguments:
Path - The mapping which you want to clean up.
Return Value:
None.
--*/
{
PAGED_CODE();
if (Path->FullPath.Buffer != NULL) {
ExFreePoolWithTag( Path->FullPath.Buffer, NC_MAPPING_TAG );
}
//
// All of the strings point at the same buffer allocation.
// So once we free the buffer in the FullPath, we just zero out
// all of the other strings.
//
NcInitMappingPath( Path );
}
NTSTATUS
NcBuildMappingPath (
_In_ PUNICODE_STRING VolumeName,
_In_ PUNICODE_STRING ParentPath,
_In_ PUNICODE_STRING FinalComponent,
_Inout_ PNC_MAPPING_PATH Path
)
/*++
Routine Description:
Builds a NC_MAPPING_PATH from a collection of Unicode strings.
Arguments:
VolumeName - Name of the volume.
ParentPath - string for the parent of mapping.
FinalComponent - The final component of the mapping path.
Path - Pointer to a used allocated and zeroed NC_MAPPING_PATH.
Return Value:
Returns STATUS_SUCCESS on success. Otherwise returns an error code.
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
USHORT NameLength;
PWCHAR NameBuffer = NULL;
UNICODE_STRING NameString;
USHORT SeparatorLength;
WCHAR ParentEnd;
USHORT Index;
PAGED_CODE();
FLT_ASSERT( NcIsMappingPathZeroed( Path ) );
//
// We cannot remap to/from the root of the volume.
//
FLT_ASSERT( FinalComponent->Length > 0 );
//
// If the parent path does not end with a '\' then we insert one
// between the parent and the final component.
//
ParentEnd = ParentPath->Buffer[(ParentPath->Length / sizeof(WCHAR)) - 1];
if (ParentEnd != NC_SEPARATOR) {
SeparatorLength = sizeof(WCHAR);
} else {
SeparatorLength = 0;
}
//
// Allocate Buffer for Name.
//
NameLength = VolumeName->Length + ParentPath->Length + SeparatorLength + FinalComponent->Length;
NameBuffer = ExAllocatePoolZero( PagedPool,
NameLength,
NC_MAPPING_TAG );
if (NameBuffer == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
goto NcBuildMappingPathCleanup;
}
//
// Create String
//
NameString.Length = 0;
NameString.MaximumLength = NameLength;
NameString.Buffer = NameBuffer;
//
// Copy Volume Name
//
RtlCopyUnicodeString( &NameString, VolumeName );
//
// Copy Parent Path
//
RtlAppendUnicodeStringToString( &NameString,ParentPath );
//
// Add separator
//
if (SeparatorLength != 0) {
NameString.Buffer[NameString.Length/SeparatorLength] = NC_SEPARATOR;
NameString.Length = NameString.Length + SeparatorLength;
FLT_ASSERT( NameString.Length <= NameString.MaximumLength );
}
//
// Copy Final Component
//
RtlAppendUnicodeStringToString( &NameString, FinalComponent );
//
// Setup Unicode Strings
//
Path->FullPath.Buffer = NameString.Buffer;
Path->FullPath.Length = NameString.Length;
Path->FullPath.MaximumLength = NameString.MaximumLength;
Path->VolumePath.Buffer = NameString.Buffer;
Path->VolumePath.Length = VolumeName->Length;
Path->VolumePath.MaximumLength = VolumeName->Length;
Path->ParentPath.Buffer = NameString.Buffer;
Path->ParentPath.Length = SeparatorLength == sizeof(WCHAR) ?
VolumeName->Length+ParentPath->Length :
VolumeName->Length+ParentPath->Length-sizeof(WCHAR);
Path->ParentPath.MaximumLength = SeparatorLength == sizeof(WCHAR) ?
VolumeName->Length+ParentPath->Length :
VolumeName->Length+ParentPath->Length-sizeof(WCHAR);
Path->FinalComponentName.Buffer = (PWSTR)Add2Ptr( NameString.Buffer,
VolumeName->Length+ParentPath->Length+SeparatorLength );
Path->FinalComponentName.Length = FinalComponent->Length;
Path->FinalComponentName.MaximumLength = FinalComponent->Length;
Path->VolumelessName.Buffer = (PWSTR)Add2Ptr( NameString.Buffer, VolumeName->Length );
Path->VolumelessName.Length = NameString.Length - VolumeName->Length;
Path->VolumelessName.MaximumLength = Path->VolumelessName.Length;
Path->NumberComponentsInVolumePath = 0;
for (Index = 0; Index < Path->VolumePath.Length/sizeof(WCHAR); Index++) {
if (Path->VolumePath.Buffer[Index] == L'\\') {
Path->NumberComponentsInVolumePath++;
}
}
Path->NumberComponentsInFullPath = 0;
for (Index = 0; Index < Path->FullPath.Length/sizeof(WCHAR); Index++) {
if (Path->FullPath.Buffer[Index] == L'\\') {
Path->NumberComponentsInFullPath++;
}
}
FLT_ASSERT( Path->NumberComponentsInFullPath > Path->NumberComponentsInVolumePath );
//
// We reached the end without incident.
//
Status = STATUS_SUCCESS;
NcBuildMappingPathCleanup:
if (!NT_SUCCESS( Status )) {
if (NameBuffer != NULL) {
ExFreePoolWithTag( NameBuffer, NC_MAPPING_TAG );
}
NcInitMappingPath( Path );
}
return Status;
}
NTSTATUS
NcBuildMappingPathFromVolume (
_In_ PFLT_VOLUME CONST Volume,
_In_ PUNICODE_STRING ParentPath,
_In_ PUNICODE_STRING FinalComponentName,
_Inout_ PNC_MAPPING_PATH Entry
)
/*++
Routine Description:
Constructs a path from a Volume and a Caller Specified Path.
Arguments:
Volume - Pointer to the volume which the mapping path will be used on.
ParentPath - Path to the parent of the mapping.
FinalComponentName - The final component name.
Entry - Pointer to a user allocated NC_MAPPING_PATH.
Return Value:
Returns STATUS_SUCCESS on success. Otherwise returns an error code.
--*/
{
NTSTATUS Status;
ULONG VolumeNameLength;
PVOID VolumeNameBuffer = NULL;
UNICODE_STRING VolumeNameString;
PAGED_CODE();
FLT_ASSERT( NcIsMappingPathZeroed( Entry ) );
//
// Query the volume name length.
//
Status = FltGetVolumeName( Volume,
NULL,
&VolumeNameLength);
if (!NT_SUCCESS( Status ) && Status != STATUS_BUFFER_TOO_SMALL) {
goto NcBuildPathCleanup;
}
//
// Allocate a buffer for the name.
//
Status = STATUS_SUCCESS;
VolumeNameBuffer = ExAllocatePoolZero( PagedPool,
VolumeNameLength,
NC_MAPPING_TAG );
if (VolumeNameBuffer == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
goto NcBuildPathCleanup;
}
RtlInitEmptyUnicodeString( &VolumeNameString, VolumeNameBuffer, (USHORT) VolumeNameLength );
//
// Query the volume name
//
Status = FltGetVolumeName( Volume,
&VolumeNameString,
NULL );
if (!NT_SUCCESS( Status )) {
goto NcBuildPathCleanup;
}
//
// Generate Mapping Path
//
Status = NcBuildMappingPath( &VolumeNameString,
ParentPath,
FinalComponentName,
Entry );
if (!NT_SUCCESS( Status )) {
goto NcBuildPathCleanup;
}
NcBuildPathCleanup:
if (!NT_SUCCESS( Status )) {
NcTeardownMappingPath( Entry );
}
if (VolumeNameBuffer != NULL) {
ExFreePoolWithTag( VolumeNameBuffer, NC_MAPPING_TAG );
}
return Status;
}
NTSTATUS
NcBuildMappingPathFromFile (
_In_ PFILE_OBJECT Parent,
_In_ PUNICODE_STRING FinalComponent,
_In_ PFLT_INSTANCE Instance,
_In_ BOOLEAN Normalized,
_Inout_ PNC_MAPPING_PATH Path
)
/*++
Routine Description:
Builds a path from the parent of the mapping and string for the final component.
Arguments:
Parent - The file object for the parent of the mapping.
FinalComponent - A string which is to be used as the final component of the name.
Instance - The instance for which we are building the mapping.
NameFlags - Parameters which we will use when checking the name from the file object.
Path - Pointer to a user allocated NC_MAPPING_PATH which will be populated.
Return Value
On success, returns STATUS_SUCCESS, otherwise returns an error code.
--*/
{
NTSTATUS Status;
UNICODE_STRING ParentPath;
PFLT_FILE_NAME_INFORMATION ParentNameInfo = NULL;
FLT_FILE_NAME_OPTIONS NameFlags;
PAGED_CODE();
FLT_ASSERT( NcIsMappingPathZeroed( Path ) );
if (Normalized) {
NameFlags = FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT;
} else {
NameFlags = FLT_FILE_NAME_OPENED | FLT_FILE_NAME_QUERY_DEFAULT;
}
//
// Get File parent's name info.
//
Status = NcGetFileNameInformation( NULL,
Parent,
Instance,
NameFlags,
&ParentNameInfo );
if (!NT_SUCCESS( Status )) {
goto NcBuildMappingPathFromFileCleanup;
}
Status = FltParseFileNameInformation( ParentNameInfo );
if (!NT_SUCCESS( Status )) {
goto NcBuildMappingPathFromFileCleanup;
}
FLT_ASSERT( ParentNameInfo->Format == FLT_FILE_NAME_NORMALIZED ||
ParentNameInfo->Format == FLT_FILE_NAME_OPENED );
//
// Format Parent Name
//
ParentPath.Buffer = ParentNameInfo->ParentDir.Buffer;
ParentPath.Length = ParentNameInfo->ParentDir.Length+ParentNameInfo->FinalComponent.Length;
ParentPath.MaximumLength = ParentPath.Length;
//
// Generate name
//
Status = NcBuildMappingPath( &ParentNameInfo->Volume,
&ParentPath,
FinalComponent,
Path );
NcBuildMappingPathFromFileCleanup:
if (!NT_SUCCESS( Status )) {
NcTeardownMappingPath( Path );
}
if (ParentNameInfo != NULL) {
FltReleaseFileNameInformation( ParentNameInfo );
}
return Status;
}
//
// Functions to manage mapping entry
//
BOOLEAN
NcIsMappingEntryZeroed (
PNC_MAPPING_ENTRY Entry
)
{
PAGED_CODE();
if (!NcIsMappingPathZeroed( &Entry->LongNamePath ))
return FALSE;
if (!NcIsMappingPathZeroed( &Entry->ShortNamePath ))
return FALSE;
return TRUE;
}
VOID
NcInitMappingEntry (
_Inout_ PNC_MAPPING_ENTRY Entry
)
{
PAGED_CODE();
NcInitMappingPath( &Entry->LongNamePath );
NcInitMappingPath( &Entry->ShortNamePath );
}
VOID
NcTeardownMappingEntry (
_Inout_ PNC_MAPPING_ENTRY Path
)
{
PWCHAR StrBuff = Path->LongNamePath.FullPath.Buffer;
PAGED_CODE();
NcTeardownMappingPath( &Path->LongNamePath );
if (StrBuff != Path->ShortNamePath.FullPath.Buffer) {
//
// There are situations where the two paths point to the
// same buffer. We do this check to make sure that
// we don't double free it.
//
NcTeardownMappingPath( &Path->ShortNamePath );
} else {
NcInitMappingPath( &Path->ShortNamePath );
}
}
//
// Functions to manage mapping
//
BOOLEAN
NcIsMappingZeroed (
PNC_MAPPING Mapping
)
{
PAGED_CODE();
if (!NcIsMappingEntryZeroed( &Mapping->RealMapping )) {
return FALSE;
}
if (!NcIsMappingEntryZeroed( &Mapping->UserMapping )) {
return FALSE;
}
return TRUE;
}
VOID
NcInitMapping (
PNC_MAPPING Mapping
)
{
PAGED_CODE();
NcInitMappingEntry( &Mapping->RealMapping );
NcInitMappingEntry( &Mapping->UserMapping );
}
VOID
NcTeardownMapping (
_Inout_ PNC_MAPPING Mapping
)
{
PAGED_CODE();
NcTeardownMappingEntry( &Mapping->RealMapping );
NcTeardownMappingEntry( &Mapping->UserMapping );
}
NTSTATUS
NcBuildMapping (
_In_ PFILE_OBJECT UserParent,
_In_ PFILE_OBJECT RealParent,
_In_ PUNICODE_STRING UserFinalComponentShortName,
_In_ PUNICODE_STRING UserFinalComponentLongName,
_In_ PUNICODE_STRING RealFinalComponentName,
_In_ PFLT_INSTANCE Instance,
_Out_ PNC_MAPPING Mapping
)
/*++
Routine Description:
Constructs the mapping for instance setup.
Arguments:
UserParent - File object of the parent of the user mapping.
RealParent - File object of the parent of the real mapping.
UserFinalComponentShortName - Short name for the user mapping's final component.
UserFinalComponentLongName - Long name for the user mapping's final component.
RealFinalComponentName - Final component name for the real mapping (either long or short).
Instance - Instance for which we are constructing the mapping.
Mapping - Pointer to a user allocated NC_MAPPING which will be populated.
--*/
{
NTSTATUS Status;
PAGED_CODE();
FLT_ASSERT( NcIsMappingZeroed( Mapping ) );
//
// Build the Real Path.
//
Status = NcBuildMappingPathFromFile( RealParent,
RealFinalComponentName,
Instance,
TRUE,
&Mapping->RealMapping.LongNamePath );
if (!NT_SUCCESS( Status )) {
goto NcBuildMappingCleanup;
}
// TODO: Do we really know that the real path is 8dot3 compliant?
//
// Reuse the real path for long and short name.
//
Mapping->RealMapping.ShortNamePath =
Mapping->RealMapping.LongNamePath;
//
// Build the user short path.
//
Status = NcBuildMappingPathFromFile( UserParent,
UserFinalComponentShortName,
Instance,
FALSE,
&Mapping->UserMapping.ShortNamePath );
if (!NT_SUCCESS( Status )) {
goto NcBuildMappingCleanup;
}
//
// Build the user long path.
//
Status = NcBuildMappingPathFromFile( UserParent,
UserFinalComponentLongName,
Instance,
TRUE,
&Mapping->UserMapping.LongNamePath );
if (!NT_SUCCESS( Status )) {
goto NcBuildMappingCleanup;
}
NcBuildMappingCleanup:
return Status;
}
|