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
/* 
 * FFS File System Driver for Windows
 *
 * devctl.c
 *
 * 2004.5.6 ~
 *
 * Lee Jae-Hong, http://www.pyrasis.com
 *
 * See License.txt
 *
 */

#include "ntifs.h"
#include "ffsdrv.h"

/* Globals */

extern PFFS_GLOBAL FFSGlobal;


/* Definitions */

#ifdef _PREFAST_
IO_COMPLETION_ROUTINE FFSDeviceControlCompletion;
#endif // _PREFAST_

NTSTATUS NTAPI
FFSDeviceControlCompletion(
	IN PDEVICE_OBJECT   DeviceObject,
	IN PIRP             Irp,
	IN PVOID            Context);


#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FFSDeviceControl)
#pragma alloc_text(PAGE, FFSDeviceControlNormal)
#if FFS_UNLOAD
#pragma alloc_text(PAGE, FFSPrepareToUnload)
#endif
#endif


NTSTATUS NTAPI
FFSDeviceControlCompletion(
	IN PDEVICE_OBJECT   DeviceObject,
	IN PIRP             Irp,
	IN PVOID            Context)
{
	if (Irp->PendingReturned)
	{
		IoMarkIrpPending(Irp);
	}

	return STATUS_SUCCESS;
}


__drv_mustHoldCriticalRegion
NTSTATUS
FFSDeviceControlNormal(
	IN PFFS_IRP_CONTEXT IrpContext)
{
	PDEVICE_OBJECT  DeviceObject;
	BOOLEAN         CompleteRequest = TRUE;
	NTSTATUS        Status = STATUS_UNSUCCESSFUL;

	PFFS_VCB        Vcb;

	PIRP            Irp;
	PIO_STACK_LOCATION IrpSp;
	PIO_STACK_LOCATION NextIrpSp;

	PDEVICE_OBJECT  TargetDeviceObject;

    PAGED_CODE();

	_SEH2_TRY
	{
		ASSERT(IrpContext != NULL);

		ASSERT((IrpContext->Identifier.Type == FFSICX) &&
				(IrpContext->Identifier.Size == sizeof(FFS_IRP_CONTEXT)));

		CompleteRequest = TRUE;<--- CompleteRequest is assigned

		DeviceObject = IrpContext->DeviceObject;

		if (DeviceObject == FFSGlobal->DeviceObject)
		{
			Status = STATUS_INVALID_DEVICE_REQUEST;<--- Status is assigned

			_SEH2_LEAVE;
		}

		Irp = IrpContext->Irp;
		IrpSp = IoGetCurrentIrpStackLocation(Irp);

		Vcb = (PFFS_VCB)IrpSp->FileObject->FsContext;

		if (!((Vcb) && (Vcb->Identifier.Type == FFSVCB) &&
					(Vcb->Identifier.Size == sizeof(FFS_VCB))
		    )
		  )
		{
			Status = STATUS_INVALID_PARAMETER;<--- Status is assigned
			_SEH2_LEAVE;
		}

		TargetDeviceObject = Vcb->TargetDeviceObject;

		//
		// Pass on the IOCTL to the driver below
		//

		CompleteRequest = FALSE;<--- CompleteRequest is overwritten

		NextIrpSp = IoGetNextIrpStackLocation(Irp);
		*NextIrpSp = *IrpSp;

		IoSetCompletionRoutine(
				Irp,
				FFSDeviceControlCompletion,
				NULL,
				FALSE,
				TRUE,
				TRUE);

		Status = IoCallDriver(TargetDeviceObject, Irp);<--- Status is overwritten<--- Status is overwritten
	}

	_SEH2_FINALLY
	{
		if (!IrpContext->ExceptionInProgress)
		{
			if (IrpContext)
			{
				if (!CompleteRequest)
				{
					IrpContext->Irp = NULL;
				}

				FFSCompleteIrpContext(IrpContext, Status);
			}
		}
	} _SEH2_END;

	return Status;
}


#if FFS_UNLOAD

__drv_mustHoldCriticalRegion
NTSTATUS
FFSPrepareToUnload(
	IN PFFS_IRP_CONTEXT IrpContext)
{
	PDEVICE_OBJECT  DeviceObject;
	NTSTATUS        Status = STATUS_UNSUCCESSFUL;
	BOOLEAN         GlobalDataResourceAcquired = FALSE;

    PAGED_CODE();

	_SEH2_TRY
	{
		ASSERT(IrpContext != NULL);

		ASSERT((IrpContext->Identifier.Type == FFSICX) &&
				(IrpContext->Identifier.Size == sizeof(FFS_IRP_CONTEXT)));

		DeviceObject = IrpContext->DeviceObject;

		if (DeviceObject != FFSGlobal->DeviceObject)
		{
			Status = STATUS_INVALID_DEVICE_REQUEST;<--- Status is assigned
			_SEH2_LEAVE;
		}

		ExAcquireResourceExclusiveLite(
				&FFSGlobal->Resource,
				TRUE);

		GlobalDataResourceAcquired = TRUE;

		if (FlagOn(FFSGlobal->Flags, FFS_UNLOAD_PENDING))
		{
			FFSPrint((DBG_ERROR, "FFSPrepareUnload:  Already ready to unload.\n"));

			Status = STATUS_ACCESS_DENIED;<--- Status is assigned

			_SEH2_LEAVE;
		}

		{
			PFFS_VCB                Vcb;
			PLIST_ENTRY             ListEntry;

			ListEntry = FFSGlobal->VcbList.Flink;

			while (ListEntry != &(FFSGlobal->VcbList))
			{
				Vcb = CONTAINING_RECORD(ListEntry, FFS_VCB, Next);
				ListEntry = ListEntry->Flink;

				if (Vcb && (!Vcb->ReferenceCount) &&
						IsFlagOn(Vcb->Flags, VCB_DISMOUNT_PENDING))
				{
					FFSRemoveVcb(Vcb);
					FFSClearVpbFlag(Vcb->Vpb, VPB_MOUNTED);

					FFSFreeVcb(Vcb);
				}
			}
		}

		if (!IsListEmpty(&(FFSGlobal->VcbList)))
		{

			FFSPrint((DBG_ERROR, "FFSPrepareUnload:  Mounted volumes exists.\n"));

			Status = STATUS_ACCESS_DENIED;<--- Status is assigned

			_SEH2_LEAVE;
		}

		IoUnregisterFileSystem(FFSGlobal->DeviceObject);

#ifdef _MSC_VER
#pragma prefast( suppress: 28175, "allowed to unload" )
#endif
		FFSGlobal->DriverObject->DriverUnload = DriverUnload;

		SetFlag(FFSGlobal->Flags, FFS_UNLOAD_PENDING);

		FFSPrint((DBG_INFO, "FFSPrepareToUnload: Driver is ready to unload.\n"));

		Status = STATUS_SUCCESS;<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten
	}
	_SEH2_FINALLY
	{
		if (GlobalDataResourceAcquired)
		{
			ExReleaseResourceForThreadLite(
					&FFSGlobal->Resource,
					ExGetCurrentResourceThread());
		}

		if (!IrpContext->ExceptionInProgress)
		{
			FFSCompleteIrpContext(IrpContext, Status);
		}
	} _SEH2_END;

	return Status;
}

#endif


__drv_mustHoldCriticalRegion
NTSTATUS
FFSDeviceControl(
	IN PFFS_IRP_CONTEXT IrpContext)
{
	PIRP                Irp;
	PIO_STACK_LOCATION  IoStackLocation;
	ULONG               IoControlCode;
	NTSTATUS            Status;

    PAGED_CODE();

	ASSERT(IrpContext);

	ASSERT((IrpContext->Identifier.Type == FFSICX) &&
			(IrpContext->Identifier.Size == sizeof(FFS_IRP_CONTEXT)));

	Irp = IrpContext->Irp;

	IoStackLocation = IoGetCurrentIrpStackLocation(Irp);

	IoControlCode =
		IoStackLocation->Parameters.DeviceIoControl.IoControlCode;

	switch (IoControlCode)
	{
#if FFS_UNLOAD
		case IOCTL_PREPARE_TO_UNLOAD:
			Status = FFSPrepareToUnload(IrpContext);
			break;
#endif

		case IOCTL_SELECT_BSD_PARTITION:
			Status = FFSSelectBSDPartition(IrpContext);
			break;

		default:
			Status = FFSDeviceControlNormal(IrpContext);
	}

	return Status;
}