15#pragma alloc_text(PAGE, VfdOpenCheck)
16#pragma alloc_text(PAGE, VfdOpenImage)
17#pragma alloc_text(PAGE, VfdCloseImage)
18#pragma alloc_text(PAGE, VfdQueryImage)
32 if (DeviceExtension->FileHandle ||
33 DeviceExtension->FileBuffer) {
35 VFDTRACE(VFDWARN, (
"[VFD] image already opened.\n"));
53 VFDTRACE(VFDWARN, (
"[VFD] invalid MediaType - %u.\n",
63 (
"[VFD] File name required for VFD_DISKTYPE_FILE.\n"));
76 if (DeviceExtension->SecurityContext !=
NULL) {
80 DeviceExtension->SecurityContext =
94 DeviceExtension->SecurityContext);
114 VFDTRACE(0, (
"[VFD] VfdOpenImage - IN\n"));
119 if (ImageInfo->NameLength) {
121 if (ImageInfo->NameLength + 1 >
122 DeviceExtension->FileName.MaximumLength) {
126 if (DeviceExtension->FileName.Buffer) {
129 &DeviceExtension->FileName,
136 if (!DeviceExtension->FileName.Buffer) {
137 VFDTRACE(0, (
"[VFD] Can't allocate memory for image path\n"));
141 DeviceExtension->FileName.MaximumLength
142 = (
USHORT)(ImageInfo->NameLength + 1);
145 DeviceExtension->FileName.Buffer,
146 DeviceExtension->FileName.MaximumLength);
149 if (DeviceExtension->FileName.Buffer) {
151 DeviceExtension->FileName.Buffer,
153 ImageInfo->NameLength);
155 DeviceExtension->FileName.Buffer[ImageInfo->NameLength] =
'\0';
159 DeviceExtension->FileName.Length = ImageInfo->NameLength;
166 geometry = &
geom_tbl[ImageInfo->MediaType];
173 if (ImageInfo->ImageSize != 0 &&
176 VFDTRACE(0, (
"[VFD] Image is smaller than the media\n"));
199 &unicode_name, &DeviceExtension->FileName,
TRUE);
202 VFDTRACE(0, (
"[VFD] Failed to convert filename to UNICODE\n"));
207 (
"[VFD] Opening %s\n", DeviceExtension->FileName.Buffer));
239 VFDTRACE(0, (
"[VFD] ZwCreateFile - %s\n",
246 status = ZwQueryInformationFile(
255 (
"[VFD] ZwQueryInformationFile - FILE_STANDARD_INFORMATION\n"));
265 VFDTRACE(0, (
"[VFD] file is smaller than the media.\n"));
277 network_drive =
FALSE;
287 (
PVOID *)&file_object,
292 if (file_object && file_object->DeviceObject) {
293 VFDTRACE(VFDINFO, (
"[VFD] Device type is 0x%08x\n",
294 file_object->DeviceObject->DeviceType));
296 if (file_object->DeviceObject->DeviceType
298 network_drive =
TRUE;
307 VFDTRACE(VFDWARN, (
"[VFD Cannot decide the device type\n"));
312 VFDTRACE(0, (
"[VFD] ObReferenceObjectByHandle - %s\n",
316 if (!network_drive) {
327 status = ZwQueryInformationFile(
336 (
"[VFD] ZwQueryInformationFile - FILE_BASIC_INFORMATION\n"));
346 (
"[VFD] Image file is compressed and/or encrypted\n"));
357 status = ZwQueryInformationFile(
366 (
"[VFD] ZwQueryInformationFile - FILE_ALIGNMENT_INFORMATION\n"));
376 VFDTRACE(0, (
"[VFD] Opened an image file\n"));
387 if (!DeviceExtension->FileBuffer) {
388 VFDTRACE(0, (
"[VFD] Can't allocate memory for RAM disk\n"));
393 DeviceExtension->FileBuffer,
396 if (ImageInfo->ImageSize) {
397 DeviceExtension->ImageSize = ImageInfo->ImageSize;
405 VFDTRACE(0, (
"[VFD] Created an empty RAM disk\n"));
408 DeviceExtension->MediaChangeCount++;
410 DeviceExtension->MediaType = ImageInfo->MediaType;
411 DeviceExtension->MediaFlags = ImageInfo->MediaFlags;
412 DeviceExtension->FileType = ImageInfo->FileType;
413 DeviceExtension->Geometry = geometry;
414 DeviceExtension->Sectors =
sectors;
416 VFDTRACE(0, (
"[VFD] Media:%d Flag:0x%02x Size:%lu Capacity:%lu\n",
417 DeviceExtension->MediaType,
418 DeviceExtension->MediaFlags,
419 DeviceExtension->ImageSize,
420 DeviceExtension->Sectors));
422 DeviceExtension->DeviceObject->AlignmentRequirement
426 VFDTRACE(0, (
"[VFD] VfdOpenImage - %s\n", GetStatusName(
status)));
438 VFDTRACE(0, (
"[VFD] VfdCloseImage - IN\n"));
443 DeviceExtension->MediaFlags = 0;
444 DeviceExtension->FileType = 0;
445 DeviceExtension->ImageSize = 0;
446 DeviceExtension->FileName.Length = 0;
447 DeviceExtension->Sectors = 0;
449 if (DeviceExtension->FileHandle) {
450 ZwClose(DeviceExtension->FileHandle);
451 DeviceExtension->FileHandle =
NULL;
454 if (DeviceExtension->FileBuffer) {
456 DeviceExtension->FileBuffer =
NULL;
459 VFDTRACE(0, (
"[VFD] VfdCloseImage - OUT\n"));
486 ImageInfo->MediaType = DeviceExtension->MediaType;
493 if (DeviceExtension->FileBuffer) {
500 ImageInfo->MediaFlags = DeviceExtension->MediaFlags;
501 ImageInfo->FileType = DeviceExtension->FileType;
502 ImageInfo->ImageSize = DeviceExtension->ImageSize;
504 ImageInfo->NameLength = DeviceExtension->FileName.Length;
509 DeviceExtension->FileName.Length)
517 if (DeviceExtension->FileName.Length &&
518 DeviceExtension->FileName.Buffer) {
521 DeviceExtension->FileName.Buffer,
522 DeviceExtension->FileName.Length);
528 DeviceExtension->FileName.Length;
#define FILE_NON_DIRECTORY_FILE
#define NT_SUCCESS(StatCode)
#define FILE_ATTRIBUTE_NORMAL
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
#define ExAllocatePoolWithTag(hernya, size, tag)
#define PsGetCurrentThread()
@ FileAlignmentInformation
#define FILE_SYNCHRONOUS_IO_NONALERT
#define FILE_NO_INTERMEDIATE_BUFFERING
#define FILE_RANDOM_ACCESS
#define OBJ_CASE_INSENSITIVE
struct _SECURITY_QUALITY_OF_SERVICE SECURITY_QUALITY_OF_SERVICE
#define SeDeleteClientSecurity(C)
struct _SECURITY_CLIENT_CONTEXT * PSECURITY_CLIENT_CONTEXT
NTKERNELAPI NTSTATUS NTAPI SeCreateClientSecurity(IN PETHREAD Thread, IN PSECURITY_QUALITY_OF_SERVICE QualityOfService, IN BOOLEAN RemoteClient, OUT PSECURITY_CLIENT_CONTEXT ClientContext)
static HANDLE PIO_APC_ROUTINE void PIO_STATUS_BLOCK io_status
#define InitializeObjectAttributes(p, n, a, r, s)
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define FILE_ATTRIBUTE_COMPRESSED
#define FILE_WORD_ALIGNMENT
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define FILE_ATTRIBUTE_ENCRYPTED
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
#define FileStandardInformation
#define FILE_DEVICE_NETWORK_FILE_SYSTEM
static FILE * file_handle
#define STATUS_BUFFER_TOO_SMALL
#define STATUS_BUFFER_OVERFLOW
SECURITY_CONTEXT_TRACKING_MODE ContextTrackingMode
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
#define RtlCopyMemory(Destination, Source, Length)
#define RtlZeroMemory(Destination, Length)
#define STATUS_ACCESS_DENIED
#define STATUS_INVALID_PARAMETER
#define STATUS_DEVICE_BUSY
#define STATUS_INSUFFICIENT_RESOURCES
#define VFDTRACE(LEVEL, STRING)
const DISK_GEOMETRY geom_tbl[VFD_MEDIA_MAX]
NTSTATUS VfdQueryImage(IN PDEVICE_EXTENSION DeviceExtension, OUT PVFD_IMAGE_INFO ImageInfo, IN ULONG BufferLength, OUT PULONG ReturnLength)
VOID VfdCloseImage(IN PDEVICE_EXTENSION DeviceExtension)
NTSTATUS VfdOpenImage(IN PDEVICE_EXTENSION DeviceExtension, IN PVFD_IMAGE_INFO ImageInfo)
NTSTATUS VfdOpenCheck(PDEVICE_EXTENSION DeviceExtension, PVFD_IMAGE_INFO ImageInfo, ULONG InputLength)
struct _VFD_IMAGE_INFO VFD_IMAGE_INFO
#define VFD_SECTOR_TO_BYTE(s)
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
#define ObDereferenceObject
#define SECURITY_STATIC_TRACKING