-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmount.c
151 lines (123 loc) · 4.47 KB
/
mount.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
#include "mount.h"
#include <mountmgr.h>
#include <mountdev.h>
NTSTATUS handleMountIrp(PIRP irp) {
PIO_STACK_LOCATION pStackLocation;
ULONG ioCode;
NTSTATUS status;
pStackLocation = IoGetCurrentIrpStackLocation(irp);
ioCode = pStackLocation->Parameters.DeviceIoControl.IoControlCode;
switch (ioCode) {
case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
return handleQueryDeviceName(irp);
case IOCTL_MOUNTDEV_QUERY_UNIQUE_ID:
return handleQueryUniqueId(irp);
//case IOCTL_MOUNTDEV_QUERY_SUGGESTED_LINK_NAME:
// return handleQueryLinkName(irp);
}
TRACE("In handleMountIrp. Not implemented IO Code - %x", ioCode);
status = STATUS_NOT_IMPLEMENTED;
irp->IoStatus.Status = status;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS handleQueryDeviceName(PIRP irp) {
NTSTATUS status;
PIO_STACK_LOCATION pStackLocation;
ULONG outputBufferLength;
PMOUNTDEV_NAME systemBuffer;
UNICODE_STRING devName;
RtlInitUnicodeString(&devName, L"\\Device\\VirtVol");
pStackLocation = IoGetCurrentIrpStackLocation(irp);
outputBufferLength = pStackLocation->Parameters.DeviceIoControl.OutputBufferLength;
systemBuffer = irp->AssociatedIrp.SystemBuffer;
TRACE("In handleQueryDeviceName. Buffer length - %x", outputBufferLength);
if (outputBufferLength < sizeof(MOUNTDEV_NAME)) {
// buffer can't even contain length of name
status = STATUS_INVALID_PARAMETER;
}
else {
systemBuffer->NameLength = devName.Length;
if (outputBufferLength < sizeof(USHORT) + devName.Length) {
// buffer can't contain full data. only length
irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
status = STATUS_BUFFER_OVERFLOW;
}
else {
// buffer size is enough.
irp->IoStatus.Information = sizeof(USHORT) + devName.Length;
RtlCopyMemory(systemBuffer->Name, devName.Buffer, devName.Length);
status = STATUS_SUCCESS;
}
}
irp->IoStatus.Status = status;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS handleQueryUniqueId(PIRP irp) {
NTSTATUS status;
PIO_STACK_LOCATION pStackLocation;
ULONG outputBufferLength;
PMOUNTDEV_UNIQUE_ID systemBuffer;
ANSI_STRING uniqueId;
RtlInitAnsiString(&uniqueId, "AABBCCDD");
pStackLocation = IoGetCurrentIrpStackLocation(irp);
outputBufferLength = pStackLocation->Parameters.DeviceIoControl.OutputBufferLength;
systemBuffer = irp->AssociatedIrp.SystemBuffer;
TRACE("In handleQueryUniqueId. Buffer length - %x", outputBufferLength);
if (outputBufferLength < sizeof(MOUNTDEV_UNIQUE_ID)) {
// buffer can't even contain length of name
status = STATUS_INVALID_PARAMETER;
}
else {
systemBuffer->UniqueIdLength = uniqueId.Length;
if (outputBufferLength < sizeof(USHORT) + uniqueId.Length) {
// buffer can't contain full data. only length
irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID);
status = STATUS_BUFFER_OVERFLOW;
}
else {
// buffer size is enough.
RtlCopyMemory(systemBuffer->UniqueId, uniqueId.Buffer, uniqueId.Length);
irp->IoStatus.Information = sizeof(USHORT) + uniqueId.Length;
status = STATUS_SUCCESS;
}
}
irp->IoStatus.Status = status;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS handleQueryLinkName(PIRP irp) {
NTSTATUS status;
PIO_STACK_LOCATION pStackLocation;
ULONG outputBufferLength;
PMOUNTDEV_SUGGESTED_LINK_NAME systemBuffer;
UNICODE_STRING linkName;
RtlInitUnicodeString(&linkName, "\\DosDevices\\V:");
pStackLocation = IoGetCurrentIrpStackLocation(irp);
outputBufferLength = pStackLocation->Parameters.DeviceIoControl.OutputBufferLength;
systemBuffer = irp->AssociatedIrp.SystemBuffer;
TRACE("In handleQueryLinkName. Buffer length - %x", outputBufferLength);
if (outputBufferLength < sizeof(MOUNTDEV_SUGGESTED_LINK_NAME)) {
// buffer can't even contain length of name
status = STATUS_INVALID_PARAMETER;
}
else {
systemBuffer->UseOnlyIfThereAreNoOtherLinks = FALSE;
systemBuffer->NameLength = linkName.Length*sizeof(WCHAR);
if (outputBufferLength < sizeof(BOOLEAN) + sizeof(USHORT) + linkName.Length*sizeof(WCHAR)) {
// buffer can't contain full data. only length
irp->IoStatus.Information = sizeof(BOOLEAN) + sizeof(USHORT) + linkName.Length*sizeof(WCHAR);
status = STATUS_BUFFER_OVERFLOW;
}
else {
// buffer size is enough.
RtlCopyMemory(systemBuffer->Name, linkName.Buffer, linkName.Length*sizeof(WCHAR));
irp->IoStatus.Information = sizeof(BOOLEAN) + sizeof(USHORT) + linkName.Length*sizeof(WCHAR);
status = STATUS_SUCCESS;
}
}
irp->IoStatus.Status = status;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return status;
}