forked from aleksk/LazyCopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LazyCopyDriver.c
389 lines (270 loc) · 10.5 KB
/
LazyCopyDriver.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
/*++
The MIT License (MIT)
Copyright (c) 2015 Aleksey Kabanov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Module Name:
LazyCopyDriver.c
Abstract:
This is the main module of the Lazy Copy minifilter driver.
Environment:
Kernel mode.
--*/
//------------------------------------------------------------------------
// Includes.
//------------------------------------------------------------------------
#include "LazyCopyDriver.h"
#include "Configuration.h"
#include "Communication.h"
#include "FileLocks.h"
#include "Utilities.h"
// DriverEvents.h was generated by the 'mc.exe -z LazyCopyEtw -n -km LazyCopyEtw.mc' command.
// The file contains a macro per event, and the required code to raise the event.
#include "LazyCopyEtw.h"
//------------------------------------------------------------------------
// Global variables.
//------------------------------------------------------------------------
// Defined in the "Globals.h".
DRIVER_GLOBAL_DATA Globals = { 0 };
// TODO: Before releasing the driver, make sure to contact Microsoft to register your reparse point GUID.
// {611F0D07-698B-49F4-9DDB-8446662D3325}
GUID LC_REPARSE_GUID = { 0x611F0D07, 0x698B, 0x49F4, 0x9D, 0xDB, 0x84, 0x46, 0x66, 0x2D, 0x33, 0x25 };
ULONG LC_RANDOM_SEED = 'SeeD';
//------------------------------------------------------------------------
// Local function prototypes.
//------------------------------------------------------------------------
static
_Check_return_
NTSTATUS
LcInitializeGlobals(
_In_ PDRIVER_OBJECT DriverObject
);
static
VOID
LcFreeDriverObjects();
//------------------------------------------------------------------------
// Text sections.
//------------------------------------------------------------------------
#ifdef ALLOC_PRAGMA
// Functions that handle driver load/unload and instance setup/cleanup.
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, DriverInstanceSetup)
#pragma alloc_text(PAGE, DriverInstanceQueryTeardown)
// Local functions.
#pragma alloc_text(PAGE, LcInitializeGlobals)
#pragma alloc_text(PAGE, LcFreeDriverObjects)
#endif // ALLOC_PRAGMA
//------------------------------------------------------------------------
// Functions that handle driver load/unload and instance setup/cleanup.
//------------------------------------------------------------------------
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Summary:
This is the initialization function for this minifilter driver.
It registers the current driver with the Filter Manager and initializes
all global data structures.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - Unicode string identifying where the parameters for this
driver are located in the registry.
Return value:
The return value is the status of the operation.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
UNREFERENCED_PARAMETER(RegistryPath);
FLT_ASSERT(DriverObject != NULL);
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL, "[LazyCopy] Initializing driver\n"));
// Register with the ETW.
EventRegisterLazyCopyDriver();
EventWriteDriver_Init_Start(NULL);
__try
{
// Initialize global values and configure driver.
NT_IF_FAIL_LEAVE(LcInitializeGlobals(DriverObject));
NT_IF_FAIL_LEAVE(LcInitializeConfiguration(RegistryPath));
NT_IF_FAIL_LEAVE(LcInitializeFileLocks());
// Register with the Filter Manager to tell it our callback functions.
NT_IF_FAIL_LEAVE(FltRegisterFilter(DriverObject, &FilterRegistration, &Globals.Filter));
// Open the 'server' communication port.
NT_IF_FAIL_LEAVE(LcCreateCommunicationPort());
// Start filtering I/O.
NT_IF_FAIL_LEAVE(FltStartFiltering(Globals.Filter));
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL, "[LazyCopy] Driver has been initialized\n"));
}
__finally
{
if (!NT_SUCCESS(status))
{
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[LazyCopy] Unable to initialize driver: %08X\n", status));
LcFreeDriverObjects();
}
}
EventWriteDriver_Init_Stop(NULL);
return status;
}
//------------------------------------------------------------------------
NTSTATUS
DriverInstanceSetup(
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
)
/*++
Summary:
This function is called whenever a new driver instance is created on a volume.
This gives us a chance to decide if we need to attach to this volume or not.
If this function is not defined in the registration structure, automatic
instances are always created.
In the current implementation we allow attachment to NTFS volumes only.
Arguments:
FltObjects - Pointer to the 'FLT_RELATED_OBJECTS' data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Flags describing the reason for this attach request.
VolumeDeviceType - Volume type.
VolumeFilesystemType - File system type.
Return value:
STATUS_SUCCESS - Attach.
STATUS_FLT_DO_NOT_ATTACH - Do not attach.
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Flags);
UNREFERENCED_PARAMETER(FltObjects);
FLT_ASSERT(FltObjects != NULL);
FLT_ASSERT(Globals.Filter == FltObjects->Filter);
// Allow attachments only on NTFS volumes.
if (VolumeDeviceType == FILE_DEVICE_DISK_FILE_SYSTEM && VolumeFilesystemType == FLT_FSTYPE_NTFS)
{
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL, "[LazyCopy] Attached to Volume = %p, Instance = %p\n", FltObjects->Volume, FltObjects->Instance));
return STATUS_SUCCESS;
}
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_WARNING_LEVEL, "[LazyCopy] Volume is not eligible for attachment: %p\n", FltObjects->Volume));
return STATUS_FLT_DO_NOT_ATTACH;
}
//------------------------------------------------------------------------
NTSTATUS
DriverInstanceQueryTeardown(
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
)
/*++
Summary:
This function is called when an instance is being manually deleted by a
call to 'FltDetachVolume' or 'FilterDetach' thereby giving us a
chance to fail that detach request.
If this function is not defined in the registration structure, explicit
detach requests via 'FltDetachVolume' or 'FilterDetach' will always fail.
Arguments:
FltObjects - Pointer to the 'FLT_RELATED_OBJECTS' data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Indicating where this detach request came from.
Return value:
Always returns STATUS_SUCCESS.
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER(FltObjects);
UNREFERENCED_PARAMETER(Flags);
return STATUS_SUCCESS;
}
//------------------------------------------------------------------------
NTSTATUS
DriverUnload(
_In_ FLT_FILTER_UNLOAD_FLAGS Flags
)
/*++
Summary:
This is the unload function for this minifilter driver.
It's called when the minifilter is about to be unloaded.
We can fail this unload request if this is not a mandatory unload
indicated by the 'Flags' parameter.
Arguments:
Flags - Indicating if this is a mandatory unload.
Return value:
Always returns STATUS_SUCCESS.
--*/
{
UNREFERENCED_PARAMETER(Flags);
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL, "[LazyCopy] Unloading driver\n"));
// Free global variables, close ports, unregister filter, and etc.
LcFreeDriverObjects();
// Unregister from the ETW.
EventUnregisterLazyCopyDriver();
LOG((DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL, "[LazyCopy] Driver has been unloaded\n"));
return STATUS_SUCCESS;
}
//------------------------------------------------------------------------
// Local functions.
//------------------------------------------------------------------------
static
_Check_return_
NTSTATUS
LcInitializeGlobals(
_In_ PDRIVER_OBJECT DriverObject
)
/*++
Summary:
This function initializes the 'Globals' structure that contains globally shared data.
Arguments:
DriverObject - Driver object to be stored in the 'Globals' structure.
Return value:
The return value is the status of the operation.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
PAGED_CODE();
IF_FALSE_RETURN_RESULT(DriverObject != NULL, STATUS_INVALID_PARAMETER_1);
Globals.DriverObject = DriverObject;
NT_IF_FAIL_RETURN(LcAllocateResource(&Globals.Lock));
return status;
}
//------------------------------------------------------------------------
static
VOID
LcFreeDriverObjects()
/*++
Summary:
This function unregisters all callbacks, closes communication port(s),
and frees all global resources allocated.
Arguments:
None.
ReturnvValue:
None.
--*/
{
PAGED_CODE();
// Close the 'server' port.
LcCloseCommunicationPort();
if (Globals.Filter != NULL)
{
FltUnregisterFilter(Globals.Filter);
Globals.Filter = NULL;
}
LcFreeConfiguration();
LcFreeFileLocks();
if (Globals.Lock != NULL)
{
LcFreeResource(Globals.Lock);
Globals.Lock = NULL;
}
}