summaryrefslogtreecommitdiff
path: root/filesys/miniFilter/NameChanger/ncoffsets.c
blob: f958b21ba7b9b02633acb417a9e9168e884af5e2 (plain)
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
#include "nc.h"

/*

Munging entries from the directory query responses requires name changer
to interpret the data returned from the query. There are currently 9 
information classes, each with a different structure for holding the file data.

Of the 9 queries, we are interested in the following:

FileBothDirectoryInformation
FileDirectoryInformation
FileFullDirectoryInformation
FileFullDirectoryInformation
FileIdFullDirectoryInformation
FileNamesInformation
FileObjectIdInformation

Each of structures related to these queries have the following properties:
1) Holds the offset to the next entry
2) Holds the file name length (in bytes)
3) The first WCHAR of the file name is the last entry of the structure,
with the remainder of the name following the structure.

The purpose of the DIRECTORY_CONTROL_OFFSETS structure is to allow
a generic method of interpreting the query data by holding the offset
information for each of the above variables. Combined with the directory
walking functions (below) we now have an easy uniform means of parsing
the directory queries.

*/

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NcCalculateDirectoryNotificationOffsets)
#pragma alloc_text(PAGE, NcDetermineStructureOffsets)
#pragma alloc_text(PAGE, NcGetEntrySize)
#pragma alloc_text(PAGE, NcGetFileName)
#pragma alloc_text(PAGE, NcGetFileNameLength)
#pragma alloc_text(PAGE, NcGetNextEntry)
#pragma alloc_text(PAGE, NcGetNextEntryOffset)
#pragma alloc_text(PAGE, NcGetShortName)
#pragma alloc_text(PAGE, NcGetShortNameLength)
#pragma alloc_text(PAGE, NcSetFileName)
#pragma alloc_text(PAGE, NcSetNextEntryOffset)
#pragma alloc_text(PAGE, NcSetShortName)
#endif

//---------------------------------------------------------------------------
// FUNCTIONS FOR WALKING DIRECTORY STRUCTURE
//---------------------------------------------------------------------------
BOOLEAN
NcDetermineStructureOffsets (
    _Out_ PDIRECTORY_CONTROL_OFFSETS Offsets,
    _In_ FILE_INFORMATION_CLASS Information
    )
/*++

Routine Description:

    Determines the structure offsets for the FILE_INFORMATION_CLASS provided.

Arguments:

    Offsets - Output DIRECTORY_CONTROL_OFFSETS pointer.

    Information - What structure to use.

    DirectoryBuffer - A sample buffer for us to test with.

Returns:

    return TRUE if we have a structure to handle this request,
    otherwise return FALSE. Offsets are returned through the Offsets
    pointer.

--*/
{
    BOOLEAN ReturnValue = TRUE;
    PAGED_CODE();

    //
    //  Not all directory information classes have short names. 
    //  So we'll zero out short name data for classes that don't expose it .
    //

    Offsets->ShortNamePresent = FALSE;
    Offsets->ShortNameLengthDist = 0;
    Offsets->ShortNameDist = 0;
    
    switch( Information ) {

        case FileBothDirectoryInformation:

            Offsets->NextEntryOffsetDist =
                FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION, NextEntryOffset );

            Offsets->FileNameLengthDist =
                FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION, FileNameLength );

            Offsets->FileNameDist =
                FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION, FileName );

            Offsets->ShortNamePresent = TRUE;

            Offsets->ShortNameLengthDist =
                FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION, ShortNameLength );

            Offsets->ShortNameDist = 
                FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION, ShortName );

            break;

        case FileDirectoryInformation:

            Offsets->NextEntryOffsetDist =
                FIELD_OFFSET( FILE_DIRECTORY_INFORMATION, NextEntryOffset );

            Offsets->FileNameLengthDist =
                FIELD_OFFSET( FILE_DIRECTORY_INFORMATION, FileNameLength );

            Offsets->FileNameDist =
                FIELD_OFFSET( FILE_DIRECTORY_INFORMATION, FileName );

            break;

        case FileFullDirectoryInformation:

            Offsets->NextEntryOffsetDist =
                FIELD_OFFSET( FILE_FULL_DIR_INFORMATION, NextEntryOffset );

            Offsets->FileNameLengthDist =
                FIELD_OFFSET( FILE_FULL_DIR_INFORMATION, FileNameLength );

            Offsets->FileNameDist =
                FIELD_OFFSET( FILE_FULL_DIR_INFORMATION, FileName );

            break;

        case FileIdBothDirectoryInformation:

            Offsets->NextEntryOffsetDist =
                FIELD_OFFSET( FILE_ID_BOTH_DIR_INFORMATION, NextEntryOffset );

            Offsets->FileNameLengthDist =
                FIELD_OFFSET( FILE_ID_BOTH_DIR_INFORMATION, FileNameLength );

            Offsets->FileNameDist =
                FIELD_OFFSET( FILE_ID_BOTH_DIR_INFORMATION, FileName );

            Offsets->ShortNamePresent = TRUE;

            Offsets->ShortNameLengthDist =
                FIELD_OFFSET( FILE_ID_BOTH_DIR_INFORMATION, ShortNameLength );

            Offsets->ShortNameDist = 
                FIELD_OFFSET( FILE_ID_BOTH_DIR_INFORMATION, ShortName );

            break;

        case FileIdFullDirectoryInformation:

            Offsets->NextEntryOffsetDist =
                FIELD_OFFSET( FILE_ID_FULL_DIR_INFORMATION, NextEntryOffset );

            Offsets->FileNameLengthDist =
                FIELD_OFFSET( FILE_ID_FULL_DIR_INFORMATION, FileNameLength );

            Offsets->FileNameDist =
                FIELD_OFFSET( FILE_ID_FULL_DIR_INFORMATION, FileName );

            break;

        case FileNamesInformation:

            Offsets->NextEntryOffsetDist =
                FIELD_OFFSET( FILE_NAMES_INFORMATION, NextEntryOffset );

            Offsets->FileNameLengthDist =
                FIELD_OFFSET( FILE_NAMES_INFORMATION, FileNameLength );

            Offsets->FileNameDist =
                FIELD_OFFSET( FILE_NAMES_INFORMATION, FileName );

            break;

        default:

            //
            //  There are queries that we don't filter,
            //  so just return FALSE to state failure.
            //

            ReturnValue = FALSE;
            break;
    }

    return ReturnValue;
}

VOID
NcCalculateDirectoryNotificationOffsets (
    PDIRECTORY_CONTROL_OFFSETS Offsets 
    )
/*++

Routine Description:

    Directory Notifications use a structure with similar properties 
    to a directory query. CalculateDirectoryNotificationOffsets allows
    us to reuse the ncoffsets library with directory notification structures.

Arguments

    Offsets - Output DIRECTORY_CONTROL_OFFSETS pointer.

--*/
{

    PAGED_CODE();

    //
    //  Place offsets for next entry and file name.
    //
    
    Offsets->NextEntryOffsetDist = FIELD_OFFSET( FILE_NOTIFY_INFORMATION, NextEntryOffset );
    Offsets->FileNameLengthDist = FIELD_OFFSET( FILE_NOTIFY_INFORMATION, FileNameLength );
    Offsets->FileNameDist = FIELD_OFFSET( FILE_NOTIFY_INFORMATION, FileName );

    //
    //  Directory notification does not have short names.
    //

    Offsets->ShortNamePresent = FALSE;
    Offsets->ShortNameLengthDist = 0;
    Offsets->ShortNameDist = 0;
}


ULONG
NcGetNextEntryOffset (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns the offset in bytes to the next entry.

Arguments:

    Buffer - Pointer to the start of the current entry.

    Offsets - Offsets structure for this information class.

Return Value:

    The number of bytes from Buffer to the next entry.

--*/
{
    PULONG Offset = Add2Ptr( Buffer, Offsets->NextEntryOffsetDist );
    PAGED_CODE();
    return *Offset;
}


PVOID
NcGetNextEntry (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns a pointer to the next entry in the buffer.

Arguments:

    Buffer - Pointer to the start of the current entry.

    Offsets - Offsets structure for this information class.

Return Value:

    Returns a pointer to the beginning of the next entry.

--*/
{
    ULONG Offset = NcGetNextEntryOffset( Buffer, Offsets );
    PVOID NextEntry = Add2Ptr( Buffer, Offset );
    PAGED_CODE();
    return NextEntry;
}


ULONG
NcGetFileNameLength (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns the length of the file name in bytes.

Arguments:

    Buffer - Pointer to the start of the entry.

    Offsets - Offsets structure for the information class.

Return Value:

    Returns the length of the file name component of the buffer.

--*/
{
    ULONG Result = *((PULONG)(Add2Ptr( Buffer, Offsets->FileNameLengthDist )));
    PAGED_CODE();
    return Result;
}


ULONG
NcGetEntrySize (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns the size of this entry in bytes.

Arguments:

    Buffer - Pointer to the start of the entry.

    Offsets - Offsets structure for the information class.

Return Value:

    Returns the size of this entry in bytes.

--*/
{
    ULONG EntrySize = NcGetNextEntryOffset(Buffer, Offsets);
    PAGED_CODE();

    if (EntrySize == 0) {

        //
        //  We are at last entry, so we need to calculate this ourselves.
        //

        EntrySize = NcGetFileNameLength( Buffer, Offsets );
        EntrySize += Offsets->FileNameDist;
    }

    return EntrySize;
}


PWSTR
NcGetFileName (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns a pointer to the file name string.

Arguments:

    Buffer - Pointer to the start of the entry.

    Offsets - Offsets structure for the information class.

Return Value:

    A pointer to the start of the file name for this entry.

--*/
{
    PAGED_CODE();
    return Add2Ptr(Buffer, Offsets->FileNameDist);
}


PWSTR
NcGetShortName (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns a pointer to the short name for this entry.

Arguments:

    Buffer - Pointer to the start of this entry.

    Offsets - Offsets structure for the information class.

Return Value:

    If there is a short name in this information class, returns a pointer to the
    start of the short name. Otherwise, returns NULL.
--*/
{
    PAGED_CODE();

    if (Offsets->ShortNamePresent) {

        return Add2Ptr(Buffer, Offsets->ShortNameDist);

    } else {

        return NULL;
    }
}


CCHAR
NcGetShortNameLength (
    _In_ CONST PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Returns the length of the shortname.

Arguments

    Buffer - Pointer to the start of this entry.

    Offsets - Offsets structure for the information class.

Return Value 

    Returns the length of the short name.
    Returns -1 on failure.
    
--*/
{
    CCHAR *Ptr;
    PAGED_CODE();

    if (Offsets->ShortNamePresent) {

        Ptr = Add2Ptr(Buffer, Offsets->ShortNameLengthDist);
        return *Ptr;
    } else {

        return -1;
    }
}


VOID
NcSetNextEntryOffset (
    _Inout_ PVOID Buffer,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets,
    _In_ BOOLEAN ForceLast
    )
/*++

Routine Description:

    Sets the Next Entry Offset to Value.

Arguments:

    Buffer - Pointer to the start of this entry.

    Offsets - Offsets structure for the information class.

    ForceLast - If true, the size of the next entry is set to zero.
                Otherwise, the size will be set to the size of the 
                information class + the file name's length.

--*/
{
    //
    //  Get pointer to NextEntryOffset value.
    //
    
    PULONG NextEntry = Add2Ptr( Buffer, Offsets->NextEntryOffsetDist );
    PAGED_CODE();
 
    //
    //  Get length of name.
    //
    
    if (ForceLast) {

        //
        //  This is the last entry, so we need to make sure 0 is in the 
        //  next entry offset.
        //

        *NextEntry = 0;

    } else {

        //
        //  Next entry offset is the distance to the name
        //  plus the length of the name.
        //
        
        ULONG NameLength = NcGetFileNameLength( Buffer, Offsets );
        *NextEntry = Offsets->FileNameDist + NameLength;
    }
}


VOID 
NcSetFileName (
    _In_ PVOID Entry,
    _In_ PWSTR NewName,
    _In_ ULONG Length,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets,
    _In_ BOOLEAN ForceLast
    )
/*++

Routine Description:

    Sets a new file name into the entry.

Arguments:

    Entry - A pointer to the start of the entry.

    NewName - A pointer to the new file name.

    Length - The length of the new name (in bytes).

    Offsets - Offsets structure for the information class.

    ForceLast - If true, the entry's size will be set to zero
                so that it looks like a valid last entry.

Return Value

    None.

--*/
{
    PWSTR NamePtr;
    PULONG NameLength;

    PAGED_CODE();

    //
    //  Get a pointer to the name in the buffer.
    //

    NamePtr = NcGetFileName( Entry, Offsets);
    NameLength = Add2Ptr( Entry, Offsets->FileNameLengthDist );

    //
    //  Copy the new name into buffer.
    //
    
    RtlCopyMemory( NamePtr, NewName, Length );
    *NameLength = Length;

    //
    //  Now we have to update the size of this entry.
    //

    NcSetNextEntryOffset( Entry, Offsets, ForceLast );
}


VOID 
NcSetShortName (
    _In_ PVOID Entry,
    _In_ PWSTR NewShortName,
    _In_ USHORT Length,
    _In_ CONST PDIRECTORY_CONTROL_OFFSETS Offsets
    )
/*++

Routine Description:

    Sets a new short name into an entry if the information class
    supports short names.

Arguments:

    Entry - Pointer to the start of an entry.

    NewShortName - Pointer to the new shortname.

    Length - The length of the short name in bytes.

    Offsets - Offsets structure for the information class.

Return Value:

    None.

--*/
{
    PWSTR NamePtr;
    PCCHAR NameLength;

    PAGED_CODE();

    if( Offsets->ShortNamePresent ) {

        NamePtr = NcGetShortName( Entry, Offsets );
        NameLength = Add2Ptr( Entry, Offsets->ShortNameLengthDist );

        FLT_ASSERT( Length <= (12 * sizeof(WCHAR)) );

        RtlCopyMemory(NamePtr, NewShortName, Length );
        *NameLength = (UCHAR) Length;
    }
}