summaryrefslogtreecommitdiff
path: root/filesys/miniFilter/NameChanger/nccontext.c
blob: 43592ecb84419bfddcb760183f5f7caaacc05433 (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
/*++

Copyright (c) 1999 - 2002  Microsoft Corporation

Module Name:

    nccontext.c

Abstract:

    Contains routines to manage the lifetime of instance contexts and
    stream handle contexts.  This file performs context-wide setup
    and teardown, with callouts for specific functions to perform their
    own setup and teardown.

Environment:

    Kernel mode

--*/

#include "nc.h"

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NcInstanceContextClose)
#pragma alloc_text(PAGE, NcStreamHandleContextAllocAndAttach)
#pragma alloc_text(PAGE, NcStreamHandleContextClose)
#endif

VOID
NcInstanceContextClose (
    _In_ PFLT_CONTEXT Context,
    _In_ FLT_CONTEXT_TYPE ContextType
    )
/*++

Routine Description:

    This routine is used to tear down and remove an instance context.

Arguments:

    Context - The context to destroy.
    
    ContextType - The type of the context.  Since this function is specific
        to instance contexts, we expect this to always be the instance
        context type.

Return Value:

    None.

--*/
{
    PNC_INSTANCE_CONTEXT InstanceContext = (PNC_INSTANCE_CONTEXT) Context;
    PAGED_CODE();

    UNREFERENCED_PARAMETER( ContextType );
    FLT_ASSERT( ContextType == FLT_INSTANCE_CONTEXT );

    NcTeardownMapping( &InstanceContext->Mapping );
}

VOID
NcStreamHandleContextClose (
    _In_ PFLT_CONTEXT Context,
    _In_ FLT_CONTEXT_TYPE ContextType
    )
/*++

Routine Description:

    This routine is used to tear down and remove a stream handle context.

Arguments:

    Context - The context to destroy.
    
    ContextType - The type of the context.  Since this function is specific
        to stream handle contexts, we expect this to always be the stream
        handle context type.

Return Value:

    None.

--*/
{
    PNC_STREAM_HANDLE_CONTEXT StreamContext = (PNC_STREAM_HANDLE_CONTEXT) Context;

    PAGED_CODE();

    UNREFERENCED_PARAMETER( ContextType );
    FLT_ASSERT( ContextType == FLT_STREAMHANDLE_CONTEXT );

    //
    //  Clean up notification context 
    //

    NcStreamHandleContextEnumClose( &StreamContext->DirectoryQueryContext );

    //
    //  Clean up enumeration context
    //

    NcStreamHandleContextNotClose( &StreamContext->DirectoryNotificationContext );

    //
    //  Clean up find by SID context
    //

    NcStreamHandleContextFindBySidClose( &StreamContext->FindBySidContext );

    //
    //  Clean up shared context information
    //

    if (StreamContext->Lock != NULL) {

        NcFreeEResource( StreamContext->Lock );
        StreamContext->Lock = NULL;
    }
}

NTSTATUS
NcStreamHandleContextAllocAndAttach (
    _In_ PFLT_FILTER Filter,
    _In_ PFLT_INSTANCE Instance,
    _In_ PFILE_OBJECT FileObject,
    _Out_ PNC_STREAM_HANDLE_CONTEXT * Context
    )
/*++

Routine Description:

    Allocates and initializes an empty stream handle context.

Arguments:

    Filter - The filter we are going to attach with.
    
    Instance - The instance we are going to attach with.

    FileObject - File object we are going to attach to.

    Context - Pointer to a user allocated PNC_STREAM_HANDLE_CONTEXT.

Return Value:

    On success, returns STATUS_SUCCESS and a pointer to the context in *Context.
    Otherwise returns an error and NULL in *Context.

    The user must call FltReleaseContext to balance ref count.

--*/
{
    PNC_STREAM_HANDLE_CONTEXT OurContext = NULL;
    PNC_STREAM_HANDLE_CONTEXT TheirContext = NULL;

    NTSTATUS Status;

    PAGED_CODE();

    //
    //  Default to no return context.
    //

    *Context = NULL;

    //
    //  Check if a context already exists.  If so, return it.
    //

    Status = FltGetStreamHandleContext( Instance,
                                        FileObject,
                                        &TheirContext );

    if (NT_SUCCESS( Status )) {

        *Context = TheirContext;
        return Status;
    }

    //
    //  Allocate a new context.
    //

    Status = FltAllocateContext( Filter,
                                 FLT_STREAMHANDLE_CONTEXT,
                                 sizeof(NC_STREAM_HANDLE_CONTEXT),
                                 PagedPool, 
                                 &OurContext );

    if (!NT_SUCCESS( Status )) {

        goto NcStreamHandleContextAllocAndAttachClose;
    }

    //
    //  Zero out the buffer so we can unwind on failure.
    //

    RtlZeroMemory( OurContext, sizeof(NC_STREAM_HANDLE_CONTEXT));

    //
    //  Initialize shared context information.
    //

    Status = NcAllocateEResource( &OurContext->Lock );

    if (!NT_SUCCESS( Status )) {

        goto NcStreamHandleContextAllocAndAttachClose;
    }

    //
    //  Initialize the Directory Notification Structure.
    //
 
    Status = NcStreamHandleContextNotCreate( &OurContext->DirectoryNotificationContext );

    if (!NT_SUCCESS( Status )) {

        goto NcStreamHandleContextAllocAndAttachClose;
    }

    //
    //  Initialize the Directory Enumeration Structure
    //
    
    Status = NcStreamHandleContextDirEnumCreate( &OurContext->DirectoryQueryContext );

    if (!NT_SUCCESS( Status )) {

        goto NcStreamHandleContextAllocAndAttachClose;
    }

    //
    //  Initialize the Find by SID Structure
    //
    
    Status = NcStreamHandleContextFindBySidCreate( &OurContext->FindBySidContext );

    if (!NT_SUCCESS( Status )) {

        goto NcStreamHandleContextAllocAndAttachClose;
    }

    //
    //  Now we have a new context, but we could be racing with another thread
    //  attaching a context to this stream.  We will now try to attach, but
    //  keep the existing context if it is there. If there is an existing
    //  context, then we return the pre-existing context and free ours.
    //
    
    Status = FltSetStreamHandleContext( Instance,
                                        FileObject,
                                        FLT_SET_CONTEXT_KEEP_IF_EXISTS,
                                        OurContext,
                                        &TheirContext );

    if (Status == STATUS_FLT_CONTEXT_ALREADY_DEFINED) {
            
        Status = STATUS_SUCCESS;

    } else if (!NT_SUCCESS( Status )) {

        goto NcStreamHandleContextAllocAndAttachClose;
    }

    //
    //  We need to keep 1 ref count on the context we are sticking with.
    //  However, if there are two contexts we need to free one of them.
    //  We therefore NULL out the context that is being returned so as
    //  to only clean up the other (if it exists.)
    //
    
    if (TheirContext != NULL) {

        *Context = TheirContext;
        TheirContext = NULL;

    } else {

        *Context = OurContext;
        OurContext = NULL;
    }
    
NcStreamHandleContextAllocAndAttachClose:

    if (OurContext != NULL) {

        FltReleaseContext( OurContext );
    }

    if (TheirContext != NULL) {

        FltReleaseContext( TheirContext );
    }

    return Status;
}