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
|
/*++
Copyright (c) 1999 - 2002 Microsoft Corporation
Module Name:
context.c
Abstract:
This is the stream nd stream handle context module of the kernel mode
context sample filter driver
Environment:
Kernel mode
--*/
#include "pch.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, CtxFindOrCreateFileContext)
#pragma alloc_text(PAGE, CtxCreateFileContext)
#pragma alloc_text(PAGE, CtxFindOrCreateStreamContext)
#pragma alloc_text(PAGE, CtxCreateStreamContext)
#pragma alloc_text(PAGE, CtxUpdateNameInStreamContext)
#pragma alloc_text(PAGE, CtxCreateOrReplaceStreamHandleContext)
#pragma alloc_text(PAGE, CtxCreateStreamHandleContext)
#pragma alloc_text(PAGE, CtxUpdateNameInStreamHandleContext)
#endif
NTSTATUS
CtxFindOrCreateFileContext (
_In_ PFLT_CALLBACK_DATA Cbd,
_In_ BOOLEAN CreateIfNotFound,
_When_( CreateIfNotFound != FALSE, _In_ ) _When_( CreateIfNotFound == FALSE, _In_opt_ ) PUNICODE_STRING FileName,
_Outptr_ PCTX_FILE_CONTEXT *FileContext,
_Out_opt_ PBOOLEAN ContextCreated
)
/*++
Routine Description:
This routine finds the file context for the target file.
Optionally, if the context does not exist this routing creates
a new one and attaches the context to the file.
Arguments:
Cbd - Supplies a pointer to the callbackData which
declares the requested operation.
CreateIfNotFound - Supplies if the file context must be created if missing
FileName - Supplies the file name
FileContext - Returns the file context
ContextCreated - Returns if a new context was created
Return Value:
Status
--*/
{
NTSTATUS status;
PCTX_FILE_CONTEXT fileContext;
PCTX_FILE_CONTEXT oldFileContext;
PAGED_CODE();
*FileContext = NULL;
if (ContextCreated != NULL) *ContextCreated = FALSE;
//
// First try to get the file context.
//
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Trying to get file context (FileObject = %p, Instance = %p)\n",
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = FltGetFileContext( Cbd->Iopb->TargetInstance,
Cbd->Iopb->TargetFileObject,
&fileContext );
//
// If the call failed because the context does not exist
// and the user wants to creat a new one, the create a
// new context
//
if (!NT_SUCCESS( status ) &&
(status == STATUS_NOT_FOUND) &&
CreateIfNotFound) {
//
// Create a file context
//
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Creating file context (FileObject = %p, Instance = %p)\n",
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = CtxCreateFileContext( FileName, &fileContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_ERROR | DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Failed to create file context with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
return status;
}
//
// Set the new context we just allocated on the file object
//
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Setting file context %p (FileObject = %p, Instance = %p)\n",
fileContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = FltSetFileContext( Cbd->Iopb->TargetInstance,
Cbd->Iopb->TargetFileObject,
FLT_SET_CONTEXT_KEEP_IF_EXISTS,
fileContext,
&oldFileContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Failed to set file context with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
//
// We release the context here because FltSetFileContext failed
//
// If FltSetFileContext succeeded then the context will be returned
// to the caller. The caller will use the context and then release it
// when he is done with the context.
//
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Releasing file context %p (FileObject = %p, Instance = %p)\n",
fileContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
FltReleaseContext( fileContext );
if (status != STATUS_FLT_CONTEXT_ALREADY_DEFINED) {
//
// FltSetFileContext failed for a reason other than the context already
// existing on the file. So the object now does not have any context set
// on it. So we return failure to the caller.
//
DebugTrace( DEBUG_TRACE_ERROR | DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Failed to set file context with status 0x%x != STATUS_FLT_CONTEXT_ALREADY_DEFINED. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
return status;
}
//
// Race condition. Someone has set a context after we queried it.
// Use the already set context instead
//
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: File context already defined. Retaining old file context %p (FileObject = %p, Instance = %p)\n",
oldFileContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
//
// Return the existing context. Note that the new context that we allocated has already been
// realeased above.
//
fileContext = oldFileContext;
status = STATUS_SUCCESS;
} else {
if (ContextCreated != NULL) *ContextCreated = TRUE;
}
}
*FileContext = fileContext;
return status;
}
NTSTATUS
CtxCreateFileContext (
_In_ PUNICODE_STRING FileName,
_Outptr_ PCTX_FILE_CONTEXT *FileContext
)
/*++
Routine Description:
This routine creates a new file context
Arguments:
FileName - Supplies the file name
FileContext - Returns the file context
Return Value:
Status
--*/
{
NTSTATUS status;
PCTX_FILE_CONTEXT fileContext;
PAGED_CODE();
//
// Allocate a file context
//
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
("[Ctx]: Allocating file context \n") );
status = FltAllocateContext( Globals.Filter,
FLT_FILE_CONTEXT,
CTX_FILE_CONTEXT_SIZE,
PagedPool,
&fileContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
("[Ctx]: Failed to allocate file context with status 0x%x \n",
status) );
return status;
}
//
// Initialize the newly created context
//
//
// Allocate and copy off the file name
//
fileContext->FileName.MaximumLength = FileName->Length;
status = CtxAllocateUnicodeString( &fileContext->FileName );
if (NT_SUCCESS( status )) {
RtlCopyUnicodeString( &fileContext->FileName, FileName );
}
*FileContext = fileContext;
return STATUS_SUCCESS;
}
NTSTATUS
CtxFindOrCreateStreamContext (
_In_ PFLT_CALLBACK_DATA Cbd,
_In_ BOOLEAN CreateIfNotFound,
_Outptr_ PCTX_STREAM_CONTEXT *StreamContext,
_Out_opt_ PBOOLEAN ContextCreated
)
/*++
Routine Description:
This routine finds the stream context for the target stream.
Optionally, if the context does not exist this routing creates
a new one and attaches the context to the stream.
Arguments:
Cbd - Supplies a pointer to the callbackData which
declares the requested operation.
CreateIfNotFound - Supplies if the stream must be created if missing
StreamContext - Returns the stream context
ContextCreated - Returns if a new context was created
Return Value:
Status
--*/
{
NTSTATUS status;
PCTX_STREAM_CONTEXT streamContext;
PCTX_STREAM_CONTEXT oldStreamContext;
PAGED_CODE();
*StreamContext = NULL;
if (ContextCreated != NULL) *ContextCreated = FALSE;
//
// First try to get the stream context.
//
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Trying to get stream context (FileObject = %p, Instance = %p)\n",
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = FltGetStreamContext( Cbd->Iopb->TargetInstance,
Cbd->Iopb->TargetFileObject,
&streamContext );
//
// If the call failed because the context does not exist
// and the user wants to creat a new one, the create a
// new context
//
if (!NT_SUCCESS( status ) &&
(status == STATUS_NOT_FOUND) &&
CreateIfNotFound) {
//
// Create a stream context
//
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Creating stream context (FileObject = %p, Instance = %p)\n",
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = CtxCreateStreamContext( &streamContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_ERROR | DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Failed to create stream context with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
return status;
}
//
// Set the new context we just allocated on the file object
//
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Setting stream context %p (FileObject = %p, Instance = %p)\n",
streamContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = FltSetStreamContext( Cbd->Iopb->TargetInstance,
Cbd->Iopb->TargetFileObject,
FLT_SET_CONTEXT_KEEP_IF_EXISTS,
streamContext,
&oldStreamContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Failed to set stream context with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
//
// We release the context here because FltSetStreamContext failed
//
// If FltSetStreamContext succeeded then the context will be returned
// to the caller. The caller will use the context and then release it
// when he is done with the context.
//
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Releasing stream context %p (FileObject = %p, Instance = %p)\n",
streamContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
FltReleaseContext( streamContext );
if (status != STATUS_FLT_CONTEXT_ALREADY_DEFINED) {
//
// FltSetStreamContext failed for a reason other than the context already
// existing on the stream. So the object now does not have any context set
// on it. So we return failure to the caller.
//
DebugTrace( DEBUG_TRACE_ERROR | DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Failed to set stream context with status 0x%x != STATUS_FLT_CONTEXT_ALREADY_DEFINED. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
return status;
}
//
// Race condition. Someone has set a context after we queried it.
// Use the already set context instead
//
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Stream context already defined. Retaining old stream context %p (FileObject = %p, Instance = %p)\n",
oldStreamContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
//
// Return the existing context. Note that the new context that we allocated has already been
// realeased above.
//
streamContext = oldStreamContext;
status = STATUS_SUCCESS;
} else {
if (ContextCreated != NULL) *ContextCreated = TRUE;
}
}
*StreamContext = streamContext;
return status;
}
NTSTATUS
CtxCreateStreamContext (
_Outptr_ PCTX_STREAM_CONTEXT *StreamContext
)
/*++
Routine Description:
This routine creates a new stream context
Arguments:
StreamContext - Returns the stream context
Return Value:
Status
--*/
{
NTSTATUS status;
PCTX_STREAM_CONTEXT streamContext;
PAGED_CODE();
//
// Allocate a stream context
//
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
("[Ctx]: Allocating stream context \n") );
status = FltAllocateContext( Globals.Filter,
FLT_STREAM_CONTEXT,
CTX_STREAM_CONTEXT_SIZE,
PagedPool,
&streamContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
("[Ctx]: Failed to allocate stream context with status 0x%x \n",
status) );
return status;
}
//
// Initialize the newly created context
//
RtlZeroMemory( streamContext, CTX_STREAM_CONTEXT_SIZE );
streamContext->Resource = CtxAllocateResource();
if(streamContext->Resource == NULL) {
FltReleaseContext( streamContext );
return STATUS_INSUFFICIENT_RESOURCES;
}
ExInitializeResourceLite( streamContext->Resource );
*StreamContext = streamContext;
return STATUS_SUCCESS;
}
NTSTATUS
CtxUpdateNameInStreamContext (
_In_ PUNICODE_STRING DirectoryName,
_Inout_ PCTX_STREAM_CONTEXT StreamContext
)
/*++
Routine Description:
This routine updates the name of the target in the supplied stream context
Arguments:
DirectoryName - Supplies the directory name
StreamContext - Returns the updated name in the stream context
Return Value:
Status
Note:
The caller must synchronize access to the context. This routine does no
synchronization
--*/
{
NTSTATUS status;
PAGED_CODE();
//
// Free any existing name
//
if (StreamContext->FileName.Buffer != NULL) {
CtxFreeUnicodeString(&StreamContext->FileName);
}
//
// Allocate and copy off the directory name
//
StreamContext->FileName.MaximumLength = DirectoryName->Length;
status = CtxAllocateUnicodeString(&StreamContext->FileName);
if (NT_SUCCESS(status)) {
RtlCopyUnicodeString(&StreamContext->FileName, DirectoryName);
}
return status;
}
NTSTATUS
CtxCreateOrReplaceStreamHandleContext (
_In_ PFLT_CALLBACK_DATA Cbd,
_In_ BOOLEAN ReplaceIfExists,
_Outptr_ PCTX_STREAMHANDLE_CONTEXT *StreamHandleContext,
_Out_opt_ PBOOLEAN ContextReplaced
)
/*++
Routine Description:
This routine creates a stream handle context for the target stream
handle. Optionally, if the context already exists, this routine
replaces it with the new context and releases the old context
Arguments:
Cbd - Supplies a pointer to the callbackData which
declares the requested operation.
ReplaceIfExists - Supplies if the stream handle context must be
replaced if already present
StreamContext - Returns the stream context
ContextReplaced - Returns if an existing context was replaced
Return Value:
Status
--*/
{
NTSTATUS status;
PCTX_STREAMHANDLE_CONTEXT streamHandleContext;
PCTX_STREAMHANDLE_CONTEXT oldStreamHandleContext;
PAGED_CODE();
*StreamHandleContext = NULL;
if (ContextReplaced != NULL) *ContextReplaced = FALSE;
//
// Create a stream context
//
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Creating stream handle context (FileObject = %p, Instance = %p)\n",
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
status = CtxCreateStreamHandleContext( &streamHandleContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_ERROR | DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Failed to create stream context with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
return status;
}
//
// Set the new context we just allocated on the file object
//
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Setting stream context %p (FileObject = %p, Instance = %p, ReplaceIfExists = %x)\n",
streamHandleContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance,
ReplaceIfExists) );
status = FltSetStreamHandleContext( Cbd->Iopb->TargetInstance,
Cbd->Iopb->TargetFileObject,
ReplaceIfExists ? FLT_SET_CONTEXT_REPLACE_IF_EXISTS : FLT_SET_CONTEXT_KEEP_IF_EXISTS,
streamHandleContext,
&oldStreamHandleContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Failed to set stream handle context with status 0x%x. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
//
// We release the context here because FltSetStreamContext failed
//
// If FltSetStreamContext succeeded then the context will be returned
// to the caller. The caller will use the context and then release it
// when he is done with the context.
//
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Releasing stream handle context %p (FileObject = %p, Instance = %p)\n",
streamHandleContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
FltReleaseContext( streamHandleContext );
if (status != STATUS_FLT_CONTEXT_ALREADY_DEFINED) {
//
// FltSetStreamContext failed for a reason other than the context already
// existing on the stream. So the object now does not have any context set
// on it. So we return failure to the caller.
//
DebugTrace( DEBUG_TRACE_ERROR | DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Failed to set stream context with status 0x%x != STATUS_FLT_CONTEXT_ALREADY_DEFINED. (FileObject = %p, Instance = %p)\n",
status,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
return status;
}
//
// We will reach here only if we have failed with STATUS_FLT_CONTEXT_ALREADY_DEFINED
// and we can fail with that code only if the context already exists and we have used
// the FLT_SET_CONTEXT_KEEP_IF_EXISTS flag
FLT_ASSERT( ReplaceIfExists == FALSE );
//
// Race condition. Someone has set a context after we queried it.
// Use the already set context instead
//
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Stream context already defined. Retaining old stream context %p (FileObject = %p, Instance = %p)\n",
oldStreamHandleContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
//
// Return the existing context. Note that the new context that we allocated has already been
// realeased above.
//
streamHandleContext = oldStreamHandleContext;
status = STATUS_SUCCESS;
} else {
//
// FltSetStreamContext has suceeded. The new context will be returned
// to the caller. The caller will use the context and then release it
// when he is done with the context.
//
// However, if we have replaced an existing context then we need to
// release the old context so as to decrement the ref count on it.
//
// Note that the memory allocated to the objects within the context
// will be freed in the context cleanup and must not be done here.
//
if ( ReplaceIfExists &&
oldStreamHandleContext != NULL) {
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Releasing old stream handle context %p (FileObject = %p, Instance = %p)\n",
oldStreamHandleContext,
Cbd->Iopb->TargetFileObject,
Cbd->Iopb->TargetInstance) );
FltReleaseContext( oldStreamHandleContext );
if (ContextReplaced != NULL) *ContextReplaced = TRUE;
}
}
*StreamHandleContext = streamHandleContext;
return status;
}
NTSTATUS
CtxCreateStreamHandleContext (
_Outptr_ PCTX_STREAMHANDLE_CONTEXT *StreamHandleContext
)
/*++
Routine Description:
This routine creates a new stream context
Arguments:
StreamContext - Returns the stream context
Return Value:
Status
--*/
{
NTSTATUS status;
PCTX_STREAMHANDLE_CONTEXT streamHandleContext;
PAGED_CODE();
//
// Allocate a stream context
//
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
("[Ctx]: Allocating stream handle context \n") );
status = FltAllocateContext( Globals.Filter,
FLT_STREAMHANDLE_CONTEXT,
CTX_STREAMHANDLE_CONTEXT_SIZE,
PagedPool,
&streamHandleContext );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
("[Ctx]: Failed to allocate stream handle context with status 0x%x \n",
status) );
return status;
}
//
// Initialize the newly created context
//
RtlZeroMemory( streamHandleContext, CTX_STREAMHANDLE_CONTEXT_SIZE );
streamHandleContext->Resource = CtxAllocateResource();
if(streamHandleContext->Resource == NULL) {
FltReleaseContext( streamHandleContext );
return STATUS_INSUFFICIENT_RESOURCES;
}
ExInitializeResourceLite( streamHandleContext->Resource );
*StreamHandleContext = streamHandleContext;
return STATUS_SUCCESS;
}
NTSTATUS
CtxUpdateNameInStreamHandleContext (
_In_ PUNICODE_STRING DirectoryName,
_Inout_ PCTX_STREAMHANDLE_CONTEXT StreamHandleContext
)
/*++
Routine Description:
This routine updates the name of the target in the supplied stream handle context
Arguments:
DirectoryName - Supplies the directory name
StreamHandleContext - Returns the updated name in the stream context
Return Value:
Status
Note:
The caller must synchronize access to the context. This routine does no
synchronization
--*/
{
NTSTATUS status;
PAGED_CODE();
//
// Free any existing name
//
if (StreamHandleContext->FileName.Buffer != NULL) {
CtxFreeUnicodeString(&StreamHandleContext->FileName);
}
//
// Allocate and copy off the directory name
//
StreamHandleContext->FileName.MaximumLength = DirectoryName->Length;
status = CtxAllocateUnicodeString(&StreamHandleContext->FileName);
if (NT_SUCCESS(status)) {
RtlCopyUnicodeString(&StreamHandleContext->FileName, DirectoryName);
}
return status;
}
|