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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* 
 * FFS File System Driver for Windows
 *
 * volinfo.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 ALLOC_PRAGMA
#pragma alloc_text(PAGE, FFSQueryVolumeInformation)
#pragma alloc_text(PAGE, FFSSetVolumeInformation)
#endif


__drv_mustHoldCriticalRegion
NTSTATUS
FFSQueryVolumeInformation(
	IN PFFS_IRP_CONTEXT IrpContext)
{
	PDEVICE_OBJECT          DeviceObject;
	NTSTATUS                Status = STATUS_UNSUCCESSFUL;
	PFFS_VCB                Vcb = 0;
	PIRP                    Irp;
	PIO_STACK_LOCATION      IoStackLocation;
	FS_INFORMATION_CLASS    FsInformationClass;
	ULONG                   Length;
	PVOID                   Buffer;
	BOOLEAN                 VcbResourceAcquired = FALSE;

    PAGED_CODE();

	_SEH2_TRY
	{
		ASSERT(IrpContext != NULL);

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

		DeviceObject = IrpContext->DeviceObject;

		//
		// This request is not allowed on the main device object
		//
		if (DeviceObject == FFSGlobal->DeviceObject)
		{
			Status = STATUS_INVALID_DEVICE_REQUEST;
			_SEH2_LEAVE;
		}

		Vcb = (PFFS_VCB)DeviceObject->DeviceExtension;

		ASSERT(Vcb != NULL);

		ASSERT((Vcb->Identifier.Type == FFSVCB) &&
				(Vcb->Identifier.Size == sizeof(FFS_VCB)));

		ASSERT(IsMounted(Vcb));

		if (!ExAcquireResourceSharedLite(
					&Vcb->MainResource,
					IrpContext->IsSynchronous))
		{
			Status = STATUS_PENDING;
			_SEH2_LEAVE;
		}

		VcbResourceAcquired = TRUE;

		Irp = IrpContext->Irp;

		IoStackLocation = IoGetCurrentIrpStackLocation(Irp);

		FsInformationClass =
			IoStackLocation->Parameters.QueryVolume.FsInformationClass;

		Length = IoStackLocation->Parameters.QueryVolume.Length;

		Buffer = Irp->AssociatedIrp.SystemBuffer;

		RtlZeroMemory(Buffer, Length);

		switch (FsInformationClass)
		{
			case FileFsVolumeInformation:
				{
					PFILE_FS_VOLUME_INFORMATION FsVolInfo;
					ULONG                       VolumeLabelLength;
					ULONG                       RequiredLength;

					if (Length < sizeof(FILE_FS_VOLUME_INFORMATION))
					{
						Status = STATUS_INFO_LENGTH_MISMATCH;<--- Status is assigned
						_SEH2_LEAVE;
					}

					FsVolInfo = (PFILE_FS_VOLUME_INFORMATION)Buffer;

					FsVolInfo->VolumeCreationTime.QuadPart = 0;

					FsVolInfo->VolumeSerialNumber = Vcb->Vpb->SerialNumber;

					VolumeLabelLength = Vcb->Vpb->VolumeLabelLength;

					FsVolInfo->VolumeLabelLength = VolumeLabelLength;

					// I don't know what this means
					FsVolInfo->SupportsObjects = FALSE;

					RequiredLength = sizeof(FILE_FS_VOLUME_INFORMATION)
						+ VolumeLabelLength - sizeof(WCHAR);

					if (Length < RequiredLength)
					{
						Irp->IoStatus.Information =
							sizeof(FILE_FS_VOLUME_INFORMATION);
						Status = STATUS_BUFFER_OVERFLOW;<--- Status is assigned
						_SEH2_LEAVE;
					}

					RtlCopyMemory(FsVolInfo->VolumeLabel, Vcb->Vpb->VolumeLabel, Vcb->Vpb->VolumeLabelLength);

					Irp->IoStatus.Information = RequiredLength;
					Status = STATUS_SUCCESS;<--- Status is overwritten<--- Status is overwritten<--- $symbol is assigned
					_SEH2_LEAVE;
				}

			case FileFsSizeInformation:
				{
					PFILE_FS_SIZE_INFORMATION FsSizeInfo;

					if (Length < sizeof(FILE_FS_SIZE_INFORMATION))
					{
						Status = STATUS_INFO_LENGTH_MISMATCH;<--- Status is assigned
						_SEH2_LEAVE;
					}

					FsSizeInfo = (PFILE_FS_SIZE_INFORMATION)Buffer;

					{
						if (FS_VERSION == 1)
						{
							FsSizeInfo->TotalAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_old_size / 8);

							FsSizeInfo->AvailableAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_old_cstotal.cs_nbfree / 8);
						}
						else
						{
							FsSizeInfo->TotalAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_size / 8);

							FsSizeInfo->AvailableAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_cstotal.cs_nbfree / 8);
						}
					}

					FsSizeInfo->SectorsPerAllocationUnit =
						Vcb->BlockSize / Vcb->DiskGeometry.BytesPerSector;

					FsSizeInfo->BytesPerSector =
						Vcb->DiskGeometry.BytesPerSector;

					Irp->IoStatus.Information = sizeof(FILE_FS_SIZE_INFORMATION);
					Status = STATUS_SUCCESS;<--- $symbol is overwritten<--- Status is overwritten<--- $symbol is assigned
					_SEH2_LEAVE;
				}

			case FileFsDeviceInformation:
				{
					PFILE_FS_DEVICE_INFORMATION FsDevInfo;

					if (Length < sizeof(FILE_FS_DEVICE_INFORMATION))
					{
						Status = STATUS_INFO_LENGTH_MISMATCH;<--- Status is assigned
						_SEH2_LEAVE;
					}

					FsDevInfo = (PFILE_FS_DEVICE_INFORMATION)Buffer;

					FsDevInfo->DeviceType =
						Vcb->TargetDeviceObject->DeviceType;

					FsDevInfo->Characteristics =
						Vcb->TargetDeviceObject->Characteristics;

					if (FlagOn(Vcb->Flags, VCB_READ_ONLY))
					{
						SetFlag(FsDevInfo->Characteristics,
								FILE_READ_ONLY_DEVICE);
					}

					Irp->IoStatus.Information = sizeof(FILE_FS_DEVICE_INFORMATION);
					Status = STATUS_SUCCESS;<--- $symbol is overwritten<--- Status is overwritten<--- $symbol is assigned
					_SEH2_LEAVE;
				}

			case FileFsAttributeInformation:
				{
					PFILE_FS_ATTRIBUTE_INFORMATION  FsAttrInfo;
					ULONG                           RequiredLength;

					if (Length < sizeof(FILE_FS_ATTRIBUTE_INFORMATION))
					{
						Status = STATUS_INFO_LENGTH_MISMATCH;<--- Status is assigned
						_SEH2_LEAVE;
					}

					FsAttrInfo =
						(PFILE_FS_ATTRIBUTE_INFORMATION)Buffer;

					FsAttrInfo->FileSystemAttributes =
						FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;

					FsAttrInfo->MaximumComponentNameLength = FFS_NAME_LEN;

					FsAttrInfo->FileSystemNameLength = 10;

					RequiredLength = sizeof(FILE_FS_ATTRIBUTE_INFORMATION) +
						10 - sizeof(WCHAR);

					if (Length < RequiredLength)
					{
						Irp->IoStatus.Information =
							sizeof(FILE_FS_ATTRIBUTE_INFORMATION);
						Status = STATUS_BUFFER_OVERFLOW;<--- Status is assigned
						_SEH2_LEAVE;
					}

					RtlCopyMemory(
							FsAttrInfo->FileSystemName,
							L"FFS\0", 8);

					Irp->IoStatus.Information = RequiredLength;
					Status = STATUS_SUCCESS;<--- $symbol is overwritten<--- Status is overwritten<--- Status is overwritten<--- Status is assigned
					_SEH2_LEAVE;
				}

#if (_WIN32_WINNT >= 0x0500)

			case FileFsFullSizeInformation:
				{
					PFILE_FS_FULL_SIZE_INFORMATION PFFFSI;

					if (Length < sizeof(FILE_FS_FULL_SIZE_INFORMATION))
					{
						Status = STATUS_INFO_LENGTH_MISMATCH;
						_SEH2_LEAVE;
					}

					PFFFSI = (PFILE_FS_FULL_SIZE_INFORMATION)Buffer;

					/*
					typedef struct _FILE_FS_FULL_SIZE_INFORMATION {
						LARGE_INTEGER   TotalAllocationUnits;
						LARGE_INTEGER   CallerAvailableAllocationUnits;
						LARGE_INTEGER   ActualAvailableAllocationUnits;
						ULONG           SectorsPerAllocationUnit;
						ULONG           BytesPerSector;
					} FILE_FS_FULL_SIZE_INFORMATION, *PFILE_FS_FULL_SIZE_INFORMATION;
					*/

					{
						if (FS_VERSION == 1)
						{
							PFFFSI->TotalAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_old_size / 8);

							PFFFSI->CallerAvailableAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_old_cstotal.cs_nbfree / 8);

							PFFFSI->ActualAvailableAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_old_cstotal.cs_nbfree / 8);
						}
						else
						{
							PFFFSI->TotalAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_size / 8);

							PFFFSI->CallerAvailableAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_cstotal.cs_nbfree / 8);

							PFFFSI->ActualAvailableAllocationUnits.QuadPart =
								(Vcb->ffs_super_block->fs_cstotal.cs_nbfree / 8);
						}
					}

					PFFFSI->SectorsPerAllocationUnit =
						Vcb->BlockSize / Vcb->DiskGeometry.BytesPerSector;

					PFFFSI->BytesPerSector = Vcb->DiskGeometry.BytesPerSector;

					Irp->IoStatus.Information = sizeof(FILE_FS_FULL_SIZE_INFORMATION);
					Status = STATUS_SUCCESS;
					_SEH2_LEAVE;
				}

#endif // (_WIN32_WINNT >= 0x0500)

			default:
				Status = STATUS_INVALID_INFO_CLASS;<--- Status is overwritten
		}
	}

	_SEH2_FINALLY
	{
		if (VcbResourceAcquired)
		{
			ExReleaseResourceForThreadLite(
					&Vcb->MainResource,
					ExGetCurrentResourceThread());
		}

		if (!IrpContext->ExceptionInProgress)
		{
			if (Status == STATUS_PENDING)
			{
				FFSQueueRequest(IrpContext);
			}
			else
			{
				FFSCompleteIrpContext(IrpContext, Status);
			}
		}
	} _SEH2_END;

	return Status;
}

#if !FFS_READ_ONLY

__drv_mustHoldCriticalRegion
NTSTATUS
FFSSetVolumeInformation(
	IN PFFS_IRP_CONTEXT IrpContext)
{
	PDEVICE_OBJECT          DeviceObject;
	NTSTATUS                Status = STATUS_UNSUCCESSFUL;
	PFFS_VCB                Vcb;
	PIRP                    Irp;
	PIO_STACK_LOCATION      IoStackLocation;
	FS_INFORMATION_CLASS    FsInformationClass;

    PAGED_CODE();

	_SEH2_TRY
	{
		ASSERT(IrpContext != NULL);

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

		DeviceObject = IrpContext->DeviceObject;

		//
		// This request is not allowed on the main device object
		//
		if (DeviceObject == FFSGlobal->DeviceObject)
		{
			Status = STATUS_INVALID_DEVICE_REQUEST;
			_SEH2_LEAVE;
		}

		Vcb = (PFFS_VCB)DeviceObject->DeviceExtension;

		ASSERT(Vcb != NULL);

		ASSERT((Vcb->Identifier.Type == FFSVCB) &&
				(Vcb->Identifier.Size == sizeof(FFS_VCB)));

		ASSERT(IsMounted(Vcb));

		if (IsFlagOn(Vcb->Flags, VCB_READ_ONLY))
		{
			Status = STATUS_MEDIA_WRITE_PROTECTED;
			_SEH2_LEAVE;
		}

		Irp = IrpContext->Irp;

		IoStackLocation = IoGetCurrentIrpStackLocation(Irp);

		//Notes: SetVolume is not defined in ntddk.h of win2k ddk,
		//       But it's same to QueryVolume ....
		FsInformationClass =
			IoStackLocation->Parameters./*SetVolume*/QueryVolume.FsInformationClass;

		switch (FsInformationClass)
		{
			case FileFsLabelInformation:
				{
					PFILE_FS_LABEL_INFORMATION      VolLabelInfo = NULL;
					ULONG                           VolLabelLen;
					UNICODE_STRING                  LabelName  ;

					OEM_STRING                      OemName;

					VolLabelInfo = (PFILE_FS_LABEL_INFORMATION)Irp->AssociatedIrp.SystemBuffer;

					VolLabelLen = VolLabelInfo->VolumeLabelLength;

					if(VolLabelLen > (16 * sizeof(WCHAR)))
					{
						Status = STATUS_INVALID_VOLUME_LABEL;
						_SEH2_LEAVE;
					}

					RtlCopyMemory(Vcb->Vpb->VolumeLabel,
							VolLabelInfo->VolumeLabel,
							VolLabelLen);

					RtlZeroMemory(Vcb->ffs_super_block->fs_volname, 16);

					LabelName.Buffer = VolLabelInfo->VolumeLabel;
					LabelName.MaximumLength = (USHORT)16 * sizeof(WCHAR);
					LabelName.Length = (USHORT)VolLabelLen;

					OemName.Buffer = SUPER_BLOCK->fs_volname;
					OemName.Length  = 0;
					OemName.MaximumLength = 16;

					FFSUnicodeToOEM(&OemName,
							&LabelName);

					Vcb->Vpb->VolumeLabelLength = 
						(USHORT)VolLabelLen;

					if (FFSSaveSuper(IrpContext, Vcb))
					{
						Status = STATUS_SUCCESS;
					}

					Irp->IoStatus.Information = 0;

				}
				break;

			default:
				Status = STATUS_INVALID_INFO_CLASS;
		}
	}

	_SEH2_FINALLY
	{

		if (!IrpContext->ExceptionInProgress)
		{
			if (Status == STATUS_PENDING)
			{
				FFSQueueRequest(IrpContext);
			}
			else
			{
				FFSCompleteIrpContext(IrpContext, Status);
			}
		}
	} _SEH2_END;

	return Status;

}

#endif // !FFS_READ_ONLY