forked from winfsp/winfsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mup.c
433 lines (378 loc) · 15.9 KB
/
mup.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
/**
* @file sys/mup.c
*
* @copyright 2015-2020 Bill Zissimopoulos
*/
/*
* This file is part of WinFsp.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*
* Licensees holding a valid commercial license may use this software
* in accordance with the commercial license agreement provided in
* conjunction with the software. The terms and conditions of any such
* commercial license agreement shall govern, supersede, and render
* ineffective any application of the GPLv3 license to this software,
* notwithstanding of any reference thereto in the software or
* associated repository.
*/
#include <sys/driver.h>
/*
* FSP_MUP_PREFIX_CLASS
*
* Define the following macro to claim "class" prefixes during prefix
* resolution. A "class" prefix is of the form \ClassName. The alternative
* is a "full" prefix, which is of the form \ClassName\InstanceName.
*
* Claiming a class prefix has advantages and disadvantages. The main
* advantage is that by claiming a \ClassName prefix, paths such as
* \ClassName\IPC$ will be handled by WinFsp, thus speeding up prefix
* resolution for all \ClassName prefixed names. The disadvantage is
* it is no longer possible for WinFsp and another redirector to handle
* instances ("shares") under the same \ClassName prefix.
*/
#define FSP_MUP_PREFIX_CLASS
static NTSTATUS FspMupGetClassName(
PUNICODE_STRING Prefix, PUNICODE_STRING ClassName);
NTSTATUS FspMupRegister(
PDEVICE_OBJECT FsmupDeviceObject, PDEVICE_OBJECT FsvolDeviceObject);
VOID FspMupUnregister(
PDEVICE_OBJECT FsmupDeviceObject, PDEVICE_OBJECT FsvolDeviceObject);
NTSTATUS FspMupHandleIrp(
PDEVICE_OBJECT FsmupDeviceObject, PIRP Irp);
static NTSTATUS FspMupRedirQueryPathEx(
PDEVICE_OBJECT FsmupDeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspMupGetClassName)
#pragma alloc_text(PAGE, FspMupRegister)
#pragma alloc_text(PAGE, FspMupUnregister)
#pragma alloc_text(PAGE, FspMupHandleIrp)
#pragma alloc_text(PAGE, FspMupRedirQueryPathEx)
#endif
typedef struct _FSP_MUP_CLASS
{
LONG RefCount;
UNICODE_STRING Name;
UNICODE_PREFIX_TABLE_ENTRY Entry;
WCHAR Buffer[];
} FSP_MUP_CLASS;
static NTSTATUS FspMupGetClassName(
PUNICODE_STRING VolumePrefix, PUNICODE_STRING ClassName)
{
PAGED_CODE();
RtlZeroMemory(ClassName, sizeof *ClassName);
if (L'\\' == VolumePrefix->Buffer[0])
for (PWSTR P = VolumePrefix->Buffer + 1,
EndP = VolumePrefix->Buffer + VolumePrefix->Length / sizeof(WCHAR);
EndP > P; P++)
{
if (L'\\' == *P)
{
ClassName->Buffer = VolumePrefix->Buffer;
ClassName->Length = (USHORT)((P - ClassName->Buffer) * sizeof(WCHAR));
ClassName->MaximumLength = ClassName->Length;
return STATUS_SUCCESS;
}
}
return STATUS_INVALID_PARAMETER;
}
NTSTATUS FspMupRegister(
PDEVICE_OBJECT FsmupDeviceObject, PDEVICE_OBJECT FsvolDeviceObject)
{
PAGED_CODE();
NTSTATUS Result;
BOOLEAN Success;
FSP_FSMUP_DEVICE_EXTENSION *FsmupDeviceExtension = FspFsmupDeviceExtension(FsmupDeviceObject);
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(FsvolDeviceObject);
PUNICODE_PREFIX_TABLE_ENTRY ClassEntry;
UNICODE_STRING ClassName;
FSP_MUP_CLASS *Class = 0;
Result = FspMupGetClassName(&FsvolDeviceExtension->VolumePrefix, &ClassName);
ASSERT(NT_SUCCESS(Result));
Class = FspAlloc(sizeof *Class + ClassName.Length);
if (0 == Class)
{
Result = STATUS_INSUFFICIENT_RESOURCES;
goto exit;
}
RtlZeroMemory(Class, sizeof *Class);
Class->RefCount = 1;
Class->Name.Length = ClassName.Length;
Class->Name.MaximumLength = ClassName.MaximumLength;
Class->Name.Buffer = Class->Buffer;
RtlCopyMemory(Class->Buffer, ClassName.Buffer, ClassName.Length);
ExAcquireResourceExclusiveLite(&FsmupDeviceExtension->PrefixTableResource, TRUE);
Success = RtlInsertUnicodePrefix(&FsmupDeviceExtension->PrefixTable,
&FsvolDeviceExtension->VolumePrefix, &FsvolDeviceExtension->VolumePrefixEntry);
if (Success)
{
FspDeviceReference(FsvolDeviceObject);
ClassEntry = RtlFindUnicodePrefix(&FsmupDeviceExtension->ClassTable,
&Class->Name, 0);
if (0 == ClassEntry)
{
Success = RtlInsertUnicodePrefix(&FsmupDeviceExtension->ClassTable,
&Class->Name, &Class->Entry);
ASSERT(Success);
Class = 0;
}
else
CONTAINING_RECORD(ClassEntry, FSP_MUP_CLASS, Entry)->RefCount++;
Result = STATUS_SUCCESS;
}
else
Result = STATUS_OBJECT_NAME_COLLISION;
ExReleaseResourceLite(&FsmupDeviceExtension->PrefixTableResource);
exit:
if (0 != Class)
FspFree(Class);
return Result;
}
VOID FspMupUnregister(
PDEVICE_OBJECT FsmupDeviceObject, PDEVICE_OBJECT FsvolDeviceObject)
{
PAGED_CODE();
NTSTATUS Result;
FSP_FSMUP_DEVICE_EXTENSION *FsmupDeviceExtension = FspFsmupDeviceExtension(FsmupDeviceObject);
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(FsvolDeviceObject);
PUNICODE_PREFIX_TABLE_ENTRY PrefixEntry;
PUNICODE_PREFIX_TABLE_ENTRY ClassEntry;
UNICODE_STRING ClassName;
FSP_MUP_CLASS *Class;
Result = FspMupGetClassName(&FsvolDeviceExtension->VolumePrefix, &ClassName);
ASSERT(NT_SUCCESS(Result));
ExAcquireResourceExclusiveLite(&FsmupDeviceExtension->PrefixTableResource, TRUE);
PrefixEntry = RtlFindUnicodePrefix(&FsmupDeviceExtension->PrefixTable,
&FsvolDeviceExtension->VolumePrefix, 0);
if (0 != PrefixEntry)
{
RtlRemoveUnicodePrefix(&FsmupDeviceExtension->PrefixTable,
&FsvolDeviceExtension->VolumePrefixEntry);
FspDeviceDereference(FsvolDeviceObject);
ClassEntry = RtlFindUnicodePrefix(&FsmupDeviceExtension->ClassTable,
&ClassName, 0);
if (0 != ClassEntry)
{
Class = CONTAINING_RECORD(ClassEntry, FSP_MUP_CLASS, Entry);
if (0 == --Class->RefCount)
{
RtlRemoveUnicodePrefix(&FsmupDeviceExtension->ClassTable,
ClassEntry);
FspFree(Class);
}
}
}
ExReleaseResourceLite(&FsmupDeviceExtension->PrefixTableResource);
}
NTSTATUS FspMupHandleIrp(
PDEVICE_OBJECT FsmupDeviceObject, PIRP Irp)
{
PAGED_CODE();
FSP_FSMUP_DEVICE_EXTENSION *FsmupDeviceExtension = FspFsmupDeviceExtension(FsmupDeviceObject);
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
PFILE_OBJECT FileObject = IrpSp->FileObject;
PDEVICE_OBJECT FsvolDeviceObject = 0;
PUNICODE_PREFIX_TABLE_ENTRY PrefixEntry;
BOOLEAN DeviceDeref = FALSE;
NTSTATUS Result;
FsRtlEnterFileSystem();
switch (IrpSp->MajorFunction)
{
case IRP_MJ_CREATE:
/*
* A CREATE request with an empty file name indicates that the fsmup device
* is being opened. Check for this case and handle it.
*/
if (0 == FileObject->FileName.Length)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = FILE_OPENED;
IoCompleteRequest(Irp, FSP_IO_INCREMENT);
Result = Irp->IoStatus.Status;
goto exit;
}
/*
* Every other CREATE request must be forwarded to the appropriate fsvol device.
*/
if (0 != FileObject->RelatedFileObject)
FileObject = FileObject->RelatedFileObject;
ExAcquireResourceExclusiveLite(&FsmupDeviceExtension->PrefixTableResource, TRUE);
PrefixEntry = RtlFindUnicodePrefix(&FsmupDeviceExtension->PrefixTable,
&FileObject->FileName, 0);
if (0 != PrefixEntry)
{
FsvolDeviceObject = CONTAINING_RECORD(PrefixEntry,
FSP_FSVOL_DEVICE_EXTENSION, VolumePrefixEntry)->FsvolDeviceObject;
FspDeviceReference(FsvolDeviceObject);
DeviceDeref = TRUE;
}
ExReleaseResourceLite(&FsmupDeviceExtension->PrefixTableResource);
break;
case IRP_MJ_DEVICE_CONTROL:
/*
* A DEVICE_CONTROL request with IOCTL_REDIR_QUERY_PATH_EX must be handled
* by the fsmup device. Check for this case and handle it.
*/
if (IOCTL_REDIR_QUERY_PATH_EX == IrpSp->Parameters.DeviceIoControl.IoControlCode)
{
Irp->IoStatus.Status = FspMupRedirQueryPathEx(FsmupDeviceObject, Irp, IrpSp);
IoCompleteRequest(Irp, FSP_IO_INCREMENT);
Result = Irp->IoStatus.Status;
goto exit;
}
/*
* Every other DEVICE_CONTROL request must be forwarded to the appropriate fsvol device.
*/
/* fall through! */
default:
/*
* Every other request must be forwarded to the appropriate fsvol device. If there is no
* fsvol device, then we must return the appropriate status code (see below).
*
* Please note that since we allow the fsmup device to be opened, we must also handle
* CLEANUP and CLOSE requests for it.
*/
if (0 != FileObject)
{
if (FspFileNodeIsValid(FileObject->FsContext))
FsvolDeviceObject = ((FSP_FILE_NODE *)FileObject->FsContext)->FsvolDeviceObject;
else if (0 != FileObject->FsContext2 &&
#pragma prefast(disable:28175, "We are a filesystem: ok to access DeviceObject->Type")
3 == ((PDEVICE_OBJECT)FileObject->FsContext2)->Type &&
0 != ((PDEVICE_OBJECT)FileObject->FsContext2)->DeviceExtension &&
FspFsvolDeviceExtensionKind == FspDeviceExtension((PDEVICE_OBJECT)FileObject->FsContext2)->Kind)
FsvolDeviceObject = (PDEVICE_OBJECT)FileObject->FsContext2;
}
break;
}
if (0 == FsvolDeviceObject)
{
/*
* We were not able to find an fsvol device to forward this IRP to. We will complete
* the IRP with an appropriate status code.
*/
switch (IrpSp->MajorFunction)
{
case IRP_MJ_CREATE:
/*
* For CREATE requests we return STATUS_BAD_NETWORK_PATH. Here is why.
*
* When a file \ClassName\InstanceName\Path is opened by an application, this request
* first goes to MUP. The MUP gives DFS a first chance to handle the request and if
* that fails the MUP proceeds with prefix resolution. The DFS attempts to open the
* file \ClassName\IPC$, this results in a prefix resolution for \ClassName\IPC$
* through a recursive MUP call! If this resolution fails the DFS returns to the MUP,
* which now attempts prefix resolution for \ClassName\InstanceName\Path.
*
* Under the new fsmup design we respond to IOCTL_REDIR_QUERY_PATH_EX by handling all
* paths with a \ClassName prefix (that we know). This way we ensure that we will get
* all opens for paths with a \ClassName prefix and avoid delays for requests of
* \ClassName\IPC$, which if left unhandled will be forwarded to all network
* redirectors.
*
* In order to successfully short-circuit requests for \ClassName\IPC$ we must also
* return STATUS_BAD_NETWORK_PATH in CREATE. This makes DFS think that prefix
* resolution failed and does not complain if it cannot open \ClassName\IPC$. Other
* error codes cause DFS to completely fail the open issued by the application.
*/
Irp->IoStatus.Status = STATUS_BAD_NETWORK_PATH;
break;
case IRP_MJ_CLEANUP:
case IRP_MJ_CLOSE:
/*
* CLEANUP and CLOSE requests ignore their status code (except for STATUS_PENDING).
* So return STATUS_SUCCESS. This works regardless of whether this is a legitimate
* fsmup request or an erroneous CLOSE request that we should not have seen.
*/
Irp->IoStatus.Status = STATUS_SUCCESS;
break;
case IRP_MJ_QUERY_INFORMATION:
case IRP_MJ_SET_INFORMATION:
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
break;
default:
Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, FSP_IO_INCREMENT);
Result = Irp->IoStatus.Status;
goto exit;
}
ASSERT(FspFsvolDeviceExtensionKind == FspDeviceExtension(FsvolDeviceObject)->Kind);
/*
* Forward the IRP to the appropriate fsvol device. The fsvol device will take care
* to complete the IRP, etc.
*/
IoSkipCurrentIrpStackLocation(Irp);
Result = IoCallDriver(FsvolDeviceObject, Irp);
if (DeviceDeref)
FspDeviceDereference(FsvolDeviceObject);
exit:
FsRtlExitFileSystem();
return Result;
}
static NTSTATUS FspMupRedirQueryPathEx(
PDEVICE_OBJECT FsmupDeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
ASSERT(IRP_MJ_DEVICE_CONTROL == IrpSp->MajorFunction);
ASSERT(IOCTL_REDIR_QUERY_PATH_EX == IrpSp->Parameters.DeviceIoControl.IoControlCode);
Irp->IoStatus.Information = 0;
if (KernelMode != Irp->RequestorMode)
return STATUS_INVALID_DEVICE_REQUEST;
/* check parameters */
ULONG InputBufferLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
ULONG OutputBufferLength = IrpSp->Parameters.DeviceIoControl.OutputBufferLength;
QUERY_PATH_REQUEST_EX *QueryPathRequest = IrpSp->Parameters.DeviceIoControl.Type3InputBuffer;
QUERY_PATH_RESPONSE *QueryPathResponse = Irp->UserBuffer;
if (sizeof(QUERY_PATH_REQUEST_EX) > InputBufferLength ||
0 == QueryPathRequest || 0 == QueryPathResponse)
return STATUS_INVALID_PARAMETER;
if (sizeof(QUERY_PATH_RESPONSE) > OutputBufferLength)
return STATUS_BUFFER_TOO_SMALL;
NTSTATUS Result;
FSP_FSMUP_DEVICE_EXTENSION *FsmupDeviceExtension = FspFsmupDeviceExtension(FsmupDeviceObject);
PUNICODE_PREFIX_TABLE_ENTRY Entry;
#if defined(FSP_MUP_PREFIX_CLASS)
UNICODE_STRING ClassName;
Result = FspMupGetClassName(&QueryPathRequest->PathName, &ClassName);
if (!NT_SUCCESS(Result))
return STATUS_BAD_NETWORK_PATH;
Result = STATUS_BAD_NETWORK_PATH;
ExAcquireResourceExclusiveLite(&FsmupDeviceExtension->PrefixTableResource, TRUE);
Entry = RtlFindUnicodePrefix(&FsmupDeviceExtension->ClassTable, &ClassName, 0);
if (0 != Entry)
{
QueryPathResponse->LengthAccepted = ClassName.Length;
Result = STATUS_SUCCESS;
}
ExReleaseResourceLite(&FsmupDeviceExtension->PrefixTableResource);
#else
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension;
Result = STATUS_BAD_NETWORK_PATH;
ExAcquireResourceExclusiveLite(&FsmupDeviceExtension->PrefixTableResource, TRUE);
Entry = RtlFindUnicodePrefix(&FsmupDeviceExtension->PrefixTable,
&QueryPathRequest->PathName, 0);
if (0 != Entry)
{
FsvolDeviceExtension = CONTAINING_RECORD(Entry, FSP_FSVOL_DEVICE_EXTENSION, VolumePrefixEntry);
if (!FspIoqStopped(FsvolDeviceExtension->Ioq))
{
if (0 < FsvolDeviceExtension->VolumePrefix.Length &&
FspFsvolDeviceVolumePrefixInString(
FsvolDeviceExtension->FsvolDeviceObject, &QueryPathRequest->PathName) &&
(QueryPathRequest->PathName.Length == FsvolDeviceExtension->VolumePrefix.Length ||
'\\' == QueryPathRequest->PathName.Buffer[FsvolDeviceExtension->VolumePrefix.Length / sizeof(WCHAR)]))
{
QueryPathResponse->LengthAccepted = FsvolDeviceExtension->VolumePrefix.Length;
Result = STATUS_SUCCESS;
}
}
}
ExReleaseResourceLite(&FsmupDeviceExtension->PrefixTableResource);
#endif
return Result;
}