Major refactoring: WIP

This commit is contained in:
Bill Zissimopoulos
2015-12-20 10:57:45 -08:00
parent f099fe4bff
commit 7197501c9a
47 changed files with 2622 additions and 1982 deletions

176
src0/sys/cleanup.c Normal file
View File

@ -0,0 +1,176 @@
/**
* @file sys/cleanup.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
static NTSTATUS FspFsctlCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvrtCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvolCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
FSP_IOCMPL_DISPATCH FspFsvolCleanupComplete;
FSP_DRIVER_DISPATCH FspCleanup;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFsctlCleanup)
#pragma alloc_text(PAGE, FspFsvrtCleanup)
#pragma alloc_text(PAGE, FspFsvolCleanup)
#pragma alloc_text(PAGE, FspFsvolCleanupComplete)
#pragma alloc_text(PAGE, FspCleanup)
#endif
static NTSTATUS FspFsctlCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
return Result;
}
static NTSTATUS FspFsvrtCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
return Result;
}
static NTSTATUS FspFsvolCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
/* is this a valid FileObject? */
if (!FspFileContextIsValid(IrpSp->FileObject->FsContext))
return STATUS_SUCCESS;
NTSTATUS Result;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PFILE_OBJECT FileObject = IrpSp->FileObject;
FSP_FILE_CONTEXT *FsContext = FileObject->FsContext;
UINT64 UserContext = FsContext->UserContext;
UINT64 UserContext2 = (UINT_PTR)FileObject->FsContext2;
BOOLEAN DeletePending;
LONG OpenCount;
/* lock the FsContext */
ExAcquireResourceExclusiveLite(FsContext->Header.Resource, TRUE);
/* propagate the FsContext DeleteOnClose to DeletePending */
if (FsContext->DeleteOnClose)
FsContext->DeletePending = TRUE;
DeletePending = FsContext->DeletePending;
/* all handles on this FileObject are gone; decrement its FsContext->OpenCount */
OpenCount = FspFileContextClose(FsContext);
/* unlock the FsContext */
ExReleaseResourceLite(FsContext->Header.Resource);
/* is the FsContext going away as well? */
if (0 == OpenCount)
{
/*
* The following must be done under the file system volume device Resource,
* because we are manipulating its GenericTable.
*/
ExAcquireResourceExclusiveLite(&FsvolDeviceExtension->Base.Resource, TRUE);
try
{
/* remove the FsContext from the file system volume device generic table */
FspFsvolDeviceDeleteContext(DeviceObject, FsContext->UserContext, 0);
}
finally
{
ExReleaseResourceLite(&FsvolDeviceExtension->Base.Resource);
}
}
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
/* IRP_MJ_CLEANUP cannot really fail :-\ */
return STATUS_SUCCESS;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
BOOLEAN FileNameRequired = 0 != FsvrtDeviceExtension->VolumeParams.FileNameRequired;
FSP_FSCTL_TRANSACT_REQ *Request;
/* create the user-mode file system request */
Result = FspIopCreateRequest(Irp, FileNameRequired ? &FsContext->FileName : 0, 0, &Request);
if (!NT_SUCCESS(Result))
goto leak_exit;
/*
* The new request is associated with our IRP and will be deleted during its completion.
*/
/* populate the Cleanup request */
Request->Kind = FspFsctlTransactCleanupKind;
Request->Req.Cleanup.UserContext = UserContext;
Request->Req.Cleanup.UserContext2 = UserContext2;
Request->Req.Cleanup.Delete = DeletePending && 0 == OpenCount;
Result = STATUS_PENDING;
goto exit;
leak_exit:;
#if DBG
DEBUGLOG("FileObject=%p[%p:\"%wZ\"], UserContext=%llx, UserContext2=%llx: "
"error: the user-mode file system handle will be leaked!",
IrpSp->FileObject, IrpSp->FileObject->RelatedFileObject, IrpSp->FileObject->FileName,
UserContext, UserContext2);
#endif
/* IRP_MJ_CLEANUP cannot really fail :-\ */
Result = STATUS_SUCCESS;
exit:;
}
finally
{
FspDeviceRelease(FsvrtDeviceObject);
}
return Result;
}
VOID FspFsvolCleanupComplete(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response)
{
FSP_ENTER_IOC(PAGED_CODE());
FSP_LEAVE_IOC("FileObject=%p", IrpSp->FileObject);
}
NTSTATUS FspCleanup(
PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
FSP_ENTER_MJ(PAGED_CODE());
ASSERT(IRP_MJ_CLEANUP == IrpSp->MajorFunction);
switch (FspDeviceExtension(DeviceObject)->Kind)
{
case FspFsvolDeviceExtensionKind:
FSP_RETURN(Result = FspFsvolCleanup(DeviceObject, Irp, IrpSp));
case FspFsvrtDeviceExtensionKind:
FSP_RETURN(Result = FspFsvrtCleanup(DeviceObject, Irp, IrpSp));
case FspFsctlDeviceExtensionKind:
FSP_RETURN(Result = FspFsctlCleanup(DeviceObject, Irp, IrpSp));
default:
FSP_RETURN(Result = STATUS_INVALID_DEVICE_REQUEST);
}
FSP_LEAVE_MJ("FileObject=%p", IrpSp->FileObject);
}

148
src0/sys/close.c Normal file
View File

@ -0,0 +1,148 @@
/**
* @file sys/close.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
static NTSTATUS FspFsctlClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvrtClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvolClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
FSP_IOCMPL_DISPATCH FspFsvolCloseComplete;
FSP_DRIVER_DISPATCH FspClose;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFsctlClose)
#pragma alloc_text(PAGE, FspFsvrtClose)
#pragma alloc_text(PAGE, FspFsvolClose)
#pragma alloc_text(PAGE, FspFsvolCloseComplete)
#pragma alloc_text(PAGE, FspClose)
#endif
static NTSTATUS FspFsctlClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
return Result;
}
static NTSTATUS FspFsvrtClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
return Result;
}
static NTSTATUS FspFsvolClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
/* is this a valid FileObject? */
if (!FspFileContextIsValid(IrpSp->FileObject->FsContext))
return STATUS_SUCCESS;
NTSTATUS Result;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PFILE_OBJECT FileObject = IrpSp->FileObject;
FSP_FILE_CONTEXT *FsContext = FileObject->FsContext;
UINT64 UserContext = FsContext->UserContext;
UINT64 UserContext2 = (UINT_PTR)FileObject->FsContext2;
/* dereference the FsContext (and delete if no more references) */
FspFileContextRelease(FsContext);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
/* IRP_MJ_CLOSE cannot really fail :-\ */
return STATUS_SUCCESS;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
BOOLEAN FileNameRequired = 0 != FsvrtDeviceExtension->VolumeParams.FileNameRequired;
FSP_FSCTL_TRANSACT_REQ *Request;
/* create the user-mode file system request */
Result = FspIopCreateRequest(0, FileNameRequired ? &FsContext->FileName : 0, 0, &Request);
if (!NT_SUCCESS(Result))
goto leak_exit;
/*
* The new request is associated with our IRP and will be deleted during its completion.
*/
/* populate the Close request */
Request->Kind = FspFsctlTransactCloseKind;
Request->Req.Close.UserContext = UserContext;
Request->Req.Close.UserContext2 = UserContext2;
/* post as a work request; this allows us to complete our own IRP and return immediately! */
if (!FspIopPostWorkRequest(DeviceObject, Request))
/* no need to delete the request here as FspIopPostWorkRequest() will do so in all cases */
goto leak_exit;
Result = STATUS_SUCCESS;
goto exit;
leak_exit:;
#if DBG
DEBUGLOG("FileObject=%p[%p:\"%wZ\"], UserContext=%llx, UserContext2=%llx: "
"error: the user-mode file system handle will be leaked!",
IrpSp->FileObject, IrpSp->FileObject->RelatedFileObject, IrpSp->FileObject->FileName,
UserContext, UserContext2);
#endif
/* IRP_MJ_CLOSE cannot really fail :-\ */
Result = STATUS_SUCCESS;
exit:;
}
finally
{
FspDeviceRelease(FsvrtDeviceObject);
}
return Result;
}
VOID FspFsvolCloseComplete(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response)
{
FSP_ENTER_IOC(PAGED_CODE());
FSP_LEAVE_IOC("FileObject=%p", IrpSp->FileObject);
}
NTSTATUS FspClose(
PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
FSP_ENTER_MJ(PAGED_CODE());
ASSERT(IRP_MJ_CLOSE == IrpSp->MajorFunction);
switch (FspDeviceExtension(DeviceObject)->Kind)
{
case FspFsvolDeviceExtensionKind:
FSP_RETURN(Result = FspFsvolClose(DeviceObject, Irp, IrpSp));
case FspFsvrtDeviceExtensionKind:
FSP_RETURN(Result = FspFsvrtClose(DeviceObject, Irp, IrpSp));
case FspFsctlDeviceExtensionKind:
FSP_RETURN(Result = FspFsctlClose(DeviceObject, Irp, IrpSp));
default:
FSP_RETURN(Result = STATUS_INVALID_DEVICE_REQUEST);
}
FSP_LEAVE_MJ("FileObject=%p", IrpSp->FileObject);
}

767
src0/sys/create.c Normal file
View File

@ -0,0 +1,767 @@
/**
* @file sys/create.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
static NTSTATUS FspFsctlCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvrtCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvolCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
FSP_IOPREP_DISPATCH FspFsvolCreatePrepare;
FSP_IOCMPL_DISPATCH FspFsvolCreateComplete;
static VOID FspFsvolCreateCleanupClose(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response);
FSP_IOP_REQUEST_FINI FspFsvolCreateRequestFini;
FSP_DRIVER_DISPATCH FspCreate;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFsctlCreate)
#pragma alloc_text(PAGE, FspFsvrtCreate)
#pragma alloc_text(PAGE, FspFsvolCreate)
#pragma alloc_text(PAGE, FspFsvolCreatePrepare)
#pragma alloc_text(PAGE, FspFsvolCreateComplete)
#pragma alloc_text(PAGE, FspFsvolCreateCleanupClose)
#pragma alloc_text(PAGE, FspFsvolCreateRequestFini)
#pragma alloc_text(PAGE, FspCreate)
#endif
enum
{
RequestFsContext = 0,
RequestAccessToken,
};
static NTSTATUS FspFsctlCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_SUCCESS;
Irp->IoStatus.Information = FILE_OPENED;
return Result;
}
static NTSTATUS FspFsvrtCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_SUCCESS;
Irp->IoStatus.Information = FILE_OPENED;
return Result;
}
static NTSTATUS FspFsvolCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
/* open the volume object? */
if (0 == IrpSp->FileObject->FileName.Length &&
(0 == IrpSp->FileObject->RelatedFileObject ||
0 == IrpSp->FileObject->RelatedFileObject->FsContext))
{
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
IrpSp->FileObject->Vpb = FsvrtDeviceObject->Vpb;
Irp->IoStatus.Information = FILE_OPENED;
return STATUS_SUCCESS;
}
NTSTATUS Result;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
return STATUS_CANCELLED;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
PFILE_OBJECT FileObject = IrpSp->FileObject;
PFILE_OBJECT RelatedFileObject = FileObject->RelatedFileObject;
UNICODE_STRING FileName = FileObject->FileName;
PACCESS_STATE AccessState = IrpSp->Parameters.Create.SecurityContext->AccessState;
ULONG CreateOptions = IrpSp->Parameters.Create.Options;
USHORT FileAttributes = IrpSp->Parameters.Create.FileAttributes;
PSECURITY_DESCRIPTOR SecurityDescriptor = AccessState->SecurityDescriptor;
ULONG SecurityDescriptorSize = 0;
LARGE_INTEGER AllocationSize = Irp->Overlay.AllocationSize;
ACCESS_MASK DesiredAccess = IrpSp->Parameters.Create.SecurityContext->DesiredAccess;
USHORT ShareAccess = IrpSp->Parameters.Create.ShareAccess;
PFILE_FULL_EA_INFORMATION EaBuffer = Irp->AssociatedIrp.SystemBuffer;
//ULONG EaLength = IrpSp->Parameters.Create.EaLength;
ULONG Flags = IrpSp->Flags;
KPROCESSOR_MODE RequestorMode =
FlagOn(Flags, SL_FORCE_ACCESS_CHECK) ? UserMode : Irp->RequestorMode;
BOOLEAN HasTraversePrivilege =
BooleanFlagOn(AccessState->Flags, TOKEN_HAS_TRAVERSE_PRIVILEGE);
BOOLEAN IsAbsoluteSecurityDescriptor = FALSE;
BOOLEAN IsSelfRelativeSecurityDescriptor = FALSE;
BOOLEAN HasTrailingBackslash = FALSE;
FSP_FILE_CONTEXT *FsContext = 0;
FSP_FSCTL_TRANSACT_REQ *Request;
/* cannot open a paging file */
if (FlagOn(Flags, SL_OPEN_PAGING_FILE))
{
Result = STATUS_ACCESS_DENIED;
goto exit;
}
/* cannot open files by fileid */
if (FlagOn(CreateOptions, FILE_OPEN_BY_FILE_ID))
{
Result = STATUS_NOT_IMPLEMENTED;
goto exit;
}
/* do we support EA? */
if (0 != EaBuffer && !FsvrtDeviceExtension->VolumeParams.EaSupported)
{
Result = STATUS_EAS_NOT_SUPPORTED;
goto exit;
}
/* check create options */
if (FlagOn(CreateOptions, FILE_NON_DIRECTORY_FILE) &&
FlagOn(CreateOptions, FILE_DIRECTORY_FILE))
{
Result = STATUS_INVALID_PARAMETER;
goto exit;
}
/* check security descriptor validity */
if (0 != SecurityDescriptor)
{
IsAbsoluteSecurityDescriptor = RtlValidSecurityDescriptor(SecurityDescriptor);
if (IsAbsoluteSecurityDescriptor)
{
Result = RtlAbsoluteToSelfRelativeSD(SecurityDescriptor, 0, &SecurityDescriptorSize);
if (STATUS_BUFFER_TOO_SMALL != Result)
{
Result = STATUS_INVALID_PARAMETER;
goto exit;
}
}
else
{
SecurityDescriptorSize = RtlLengthSecurityDescriptor(SecurityDescriptor);
IsSelfRelativeSecurityDescriptor = RtlValidRelativeSecurityDescriptor(
SecurityDescriptor, SecurityDescriptorSize, 0);
if (!IsSelfRelativeSecurityDescriptor)
{
Result = STATUS_INVALID_PARAMETER;
goto exit;
}
}
}
/* according to fastfat, filenames that begin with two backslashes are ok */
if (sizeof(WCHAR) * 2 <= FileName.Length &&
L'\\' == FileName.Buffer[1] && L'\\' == FileName.Buffer[0])
{
FileName.Length -= sizeof(WCHAR);
FileName.MaximumLength -= sizeof(WCHAR);
FileName.Buffer++;
if (sizeof(WCHAR) * 2 <= FileName.Length &&
L'\\' == FileName.Buffer[1] && L'\\' == FileName.Buffer[0])
{
Result = STATUS_OBJECT_NAME_INVALID;
goto exit;
}
}
/* check for trailing backslash */
if (sizeof(WCHAR) * 2/* not empty or root */ <= FileName.Length &&
L'\\' == FileName.Buffer[FileName.Length / 2 - 1])
{
FileName.Length -= sizeof(WCHAR);
HasTrailingBackslash = TRUE;
if (sizeof(WCHAR) * 2 <= FileName.Length && L'\\' == FileName.Buffer[FileName.Length / 2 - 1])
{
Result = STATUS_OBJECT_NAME_INVALID;
goto exit;
}
}
if (HasTrailingBackslash && !FlagOn(CreateOptions, FILE_DIRECTORY_FILE))
{
Result = STATUS_OBJECT_NAME_INVALID;
goto exit;
}
/* is this a relative or absolute open? */
if (0 != RelatedFileObject)
{
FSP_FILE_CONTEXT *RelatedFsContext = RelatedFileObject->FsContext;
/* is this a valid RelatedFileObject? */
if (!FspFileContextIsValid(RelatedFsContext))
{
Result = STATUS_OBJECT_PATH_NOT_FOUND;
goto exit;
}
/* must be a relative path */
if (sizeof(WCHAR) <= FileName.Length && L'\\' == FileName.Buffer[0])
{
Result = STATUS_OBJECT_NAME_INVALID;
goto exit;
}
/* cannot FILE_DELETE_ON_CLOSE on the root directory */
if (sizeof(WCHAR) == RelatedFsContext->FileName.Length &&
0 == FileName.Length &&
FlagOn(CreateOptions, FILE_DELETE_ON_CLOSE))
{
Result = STATUS_CANNOT_DELETE;
goto exit;
}
/*
* There is no need to lock our accesses of RelatedFileObject->FsContext->FileName,
* because RelatedFileObject->FsContext->Filename is read-only (after creation) and
* because RelatedFileObject->FsContext is guaranteed to exist while RelatedFileObject
* exists.
*/
BOOLEAN AppendBackslash =
sizeof(WCHAR) * 2/* not empty or root */ <= RelatedFsContext->FileName.Length &&
sizeof(WCHAR) <= FileName.Length && L':' != FileName.Buffer[0];
Result = FspFileContextCreate(DeviceObject,
RelatedFsContext->FileName.Length + AppendBackslash * sizeof(WCHAR) + FileName.Length,
&FsContext);
if (!NT_SUCCESS(Result))
goto exit;
Result = RtlAppendUnicodeStringToString(&FsContext->FileName, &RelatedFsContext->FileName);
ASSERT(NT_SUCCESS(Result));
if (AppendBackslash)
{
Result = RtlAppendUnicodeToString(&FsContext->FileName, L"\\");
ASSERT(NT_SUCCESS(Result));
}
}
else
{
/* must be an absolute path */
if (sizeof(WCHAR) <= FileName.Length && L'\\' != FileName.Buffer[0])
{
Result = STATUS_OBJECT_NAME_INVALID;
goto exit;
}
/* cannot FILE_DELETE_ON_CLOSE on the root directory */
if (sizeof(WCHAR) == FileName.Length &&
FlagOn(CreateOptions, FILE_DELETE_ON_CLOSE))
{
Result = STATUS_CANNOT_DELETE;
goto exit;
}
Result = FspFileContextCreate(DeviceObject,
FileName.Length,
&FsContext);
if (!NT_SUCCESS(Result))
goto exit;
}
Result = RtlAppendUnicodeStringToString(&FsContext->FileName, &FileName);
ASSERT(NT_SUCCESS(Result));
/* create the user-mode file system request */
Result = FspIopCreateRequestEx(Irp, &FsContext->FileName, SecurityDescriptorSize,
FspFsvolCreateRequestFini, &Request);
if (!NT_SUCCESS(Result))
{
FspFileContextRelease(FsContext);
goto exit;
}
/*
* The new request is associated with our IRP and will be deleted during its completion.
* Go ahead and associate our FsContext with the Request as well.
*/
FspIopRequestContext(Request, RequestFsContext) = FsContext;
/* populate the Create request */
Request->Kind = FspFsctlTransactCreateKind;
Request->Req.Create.CreateOptions = CreateOptions;
Request->Req.Create.FileAttributes = FileAttributes;
Request->Req.Create.SecurityDescriptor.Offset = 0 == SecurityDescriptorSize ? 0 :
FSP_FSCTL_DEFAULT_ALIGN_UP(Request->FileName.Size);
Request->Req.Create.SecurityDescriptor.Size = (UINT16)SecurityDescriptorSize;
Request->Req.Create.AllocationSize = AllocationSize.QuadPart;
Request->Req.Create.AccessToken = 0;
Request->Req.Create.DesiredAccess = DesiredAccess;
Request->Req.Create.ShareAccess = ShareAccess;
Request->Req.Create.Ea.Offset = 0;
Request->Req.Create.Ea.Size = 0;
Request->Req.Create.UserMode = UserMode == RequestorMode;
Request->Req.Create.HasTraversePrivilege = HasTraversePrivilege;
Request->Req.Create.OpenTargetDirectory = BooleanFlagOn(Flags, SL_OPEN_TARGET_DIRECTORY);
Request->Req.Create.CaseSensitive = BooleanFlagOn(Flags, SL_CASE_SENSITIVE);
/* copy the security descriptor into the request */
if (IsAbsoluteSecurityDescriptor)
{
Result = RtlAbsoluteToSelfRelativeSD(SecurityDescriptor,
Request->Buffer + Request->Req.Create.SecurityDescriptor.Offset, &SecurityDescriptorSize);
if (!NT_SUCCESS(Result))
{
if (STATUS_BAD_DESCRIPTOR_FORMAT == Result || STATUS_BUFFER_TOO_SMALL == Result)
Result = STATUS_INVALID_PARAMETER; /* should not happen */
goto exit;
}
}
else if (IsSelfRelativeSecurityDescriptor)
RtlCopyMemory(Request->Buffer + Request->Req.Create.SecurityDescriptor.Offset,
SecurityDescriptor, SecurityDescriptorSize);
Result = STATUS_PENDING;
exit:;
}
finally
{
FspDeviceRelease(FsvrtDeviceObject);
}
return Result;
}
NTSTATUS FspFsvolCreatePrepare(
PIRP Irp, FSP_FSCTL_TRANSACT_REQ *Request)
{
FSP_ENTER_IOP(PAGED_CODE());
PDEVICE_OBJECT DeviceObject = IrpSp->DeviceObject;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvolDeviceExtension->FsvrtDeviceObject);
/* is the user-mode file system doing access checks? */
if (!FsvrtDeviceExtension->VolumeParams.NoSystemAccessCheck)
{
ASSERT(0 == Request->Req.Create.AccessToken);
FSP_RETURN(Result = STATUS_SUCCESS);
}
/* get a user-mode handle to the access token */
PACCESS_STATE AccessState = IrpSp->Parameters.Create.SecurityContext->AccessState;
HANDLE UserModeAccessToken;
Result = ObOpenObjectByPointer(SeQuerySubjectContextToken(&AccessState->SubjectSecurityContext),
0, 0, TOKEN_QUERY, *SeTokenObjectType, UserMode, &UserModeAccessToken);
if (!NT_SUCCESS(Result))
FSP_RETURN();
/* send the user-mode handle to the user-mode file system */
FspIopRequestContext(Request, RequestAccessToken) = UserModeAccessToken;
Request->Req.Create.AccessToken = (UINT_PTR)UserModeAccessToken;
FSP_LEAVE_IOP();
}
VOID FspFsvolCreateComplete(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response)
{
FSP_ENTER_IOC(PAGED_CODE());
/*
* NOTE:
* In the following we may have to close the just opened file object. But we must
* never close a file we just created, because this will leave an orphan file on
* the disk.
*
* Files may have to be closed if a security access or share access check fails. In
* both those cases we were opening an existing file and so it is safe to close it.
*
* Because of how IRP_MJ_CREATE works in Windows, it is difficult to improve on this
* scheme without introducing a 2-phase Create protocol, which is undesirable as it
* means two trips into user-mode for a single Create request.
*/
PDEVICE_OBJECT DeviceObject = IrpSp->DeviceObject;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
FSP_FSCTL_TRANSACT_REQ *Request = FspIrpRequest(Irp);
PFILE_OBJECT FileObject = IrpSp->FileObject;
PACCESS_STATE AccessState = IrpSp->Parameters.Create.SecurityContext->AccessState;
ULONG CreateOptions = IrpSp->Parameters.Create.Options;
BOOLEAN FileCreated = FILE_CREATED == Response->IoStatus.Information;
UINT32 ResponseFileAttributes = Response->Rsp.Create.Opened.FileAttributes;
PSECURITY_DESCRIPTOR SecurityDescriptor;
ULONG SecurityDescriptorSize;
UNICODE_STRING ReparseFileName;
ACCESS_MASK DesiredAccess = IrpSp->Parameters.Create.SecurityContext->DesiredAccess;
PPRIVILEGE_SET Privileges = 0;
USHORT ShareAccess = IrpSp->Parameters.Create.ShareAccess;
ULONG Flags = IrpSp->Flags;
KPROCESSOR_MODE RequestorMode =
FlagOn(Flags, SL_FORCE_ACCESS_CHECK) ? UserMode : Irp->RequestorMode;
FSP_FILE_CONTEXT *FsContext = FspIopRequestContext(Request, RequestFsContext);
ACCESS_MASK GrantedAccess;
BOOLEAN Inserted = FALSE;
/* did the user-mode file system sent us a failure code? */
if (!NT_SUCCESS(Response->IoStatus.Status))
{
Irp->IoStatus.Information = Response->IoStatus.Information;
Result = Response->IoStatus.Status;
FSP_RETURN();
}
/* special case STATUS_REPARSE */
if (STATUS_REPARSE == Result)
{
ReparseFileName.Buffer =
(PVOID)(Response->Buffer + Response->Rsp.Create.Reparse.FileName.Offset);
ReparseFileName.Length = ReparseFileName.MaximumLength =
Response->Rsp.Create.Reparse.FileName.Size;
Result = STATUS_ACCESS_DENIED;
if (IO_REPARSE == Response->IoStatus.Information)
{
if (0 == ReparseFileName.Length ||
(PUINT8)ReparseFileName.Buffer + ReparseFileName.Length >
(PUINT8)Response + Response->Size)
FSP_RETURN();
if (ReparseFileName.Length > FileObject->FileName.MaximumLength)
{
PVOID Buffer = FspAllocExternal(ReparseFileName.Length);
if (0 == Buffer)
FSP_RETURN(Result = STATUS_INSUFFICIENT_RESOURCES);
FspFreeExternal(FileObject->FileName.Buffer);
FileObject->FileName.MaximumLength = ReparseFileName.Length;
FileObject->FileName.Buffer = Buffer;
}
FileObject->FileName.Length = 0;
RtlCopyUnicodeString(&FileObject->FileName, &ReparseFileName);
}
else
if (IO_REMOUNT == Response->IoStatus.Information)
{
if (0 != ReparseFileName.Length)
FSP_RETURN();
}
else
FSP_RETURN();
Irp->IoStatus.Information = Response->IoStatus.Information;
Result = Response->IoStatus.Status;
FSP_RETURN();
}
/* are we doing access checks? */
if (!FsvrtDeviceExtension->VolumeParams.NoSystemAccessCheck)
{
/* read-only attribute check */
if (!FileCreated && FlagOn(ResponseFileAttributes, FILE_ATTRIBUTE_READONLY))
{
/* from fastfat: allowed accesses when read-only */
ACCESS_MASK Allowed =
DELETE | READ_CONTROL | WRITE_OWNER | WRITE_DAC |
SYNCHRONIZE | ACCESS_SYSTEM_SECURITY |
FILE_READ_DATA | FILE_READ_EA | FILE_WRITE_EA |
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES |
FILE_EXECUTE | FILE_TRAVERSE | FILE_LIST_DIRECTORY |
FILE_ADD_SUBDIRECTORY | FILE_ADD_FILE | FILE_DELETE_CHILD;
if (FlagOn(DesiredAccess, ~Allowed))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN(Result = STATUS_ACCESS_DENIED);
}
else
if (!FlagOn(ResponseFileAttributes, FILE_ATTRIBUTE_DIRECTORY) &&
FlagOn(CreateOptions, FILE_DELETE_ON_CLOSE))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN(Result = STATUS_CANNOT_DELETE);
}
}
/* security descriptor check */
SecurityDescriptor =
(PVOID)(Response->Buffer + Response->Rsp.Create.Opened.SecurityDescriptor.Offset);
SecurityDescriptorSize = Response->Rsp.Create.Opened.SecurityDescriptor.Size;
if (0 != SecurityDescriptorSize)
{
if ((PUINT8)SecurityDescriptor + SecurityDescriptorSize >
(PUINT8)Response + Response->Size ||
!FspValidRelativeSecurityDescriptor(SecurityDescriptor, SecurityDescriptorSize, 0))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN(Result = STATUS_ACCESS_DENIED);
}
/* access check */
if (!SeAccessCheck(SecurityDescriptor,
&AccessState->SubjectSecurityContext,
FALSE,
DesiredAccess,
AccessState->PreviouslyGrantedAccess,
&Privileges,
IoGetFileObjectGenericMapping(),
RequestorMode,
&GrantedAccess,
&Result))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN();
}
if (0 != Privileges)
{
Result = SeAppendPrivileges(AccessState, Privileges);
SeFreePrivileges(Privileges);
if (!NT_SUCCESS(Result))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN();
}
}
SetFlag(AccessState->PreviouslyGrantedAccess, GrantedAccess);
ClearFlag(AccessState->RemainingDesiredAccess, GrantedAccess);
}
}
/* were we asked to open a directory or non-directory? */
if (FlagOn(CreateOptions, FILE_DIRECTORY_FILE) &&
!FileCreated && !FlagOn(ResponseFileAttributes, FILE_ATTRIBUTE_DIRECTORY))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN(Result = STATUS_NOT_A_DIRECTORY);
}
if (FlagOn(CreateOptions, FILE_NON_DIRECTORY_FILE) &&
!FileCreated && FlagOn(ResponseFileAttributes, FILE_ATTRIBUTE_DIRECTORY))
{
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN(Result = STATUS_FILE_IS_A_DIRECTORY);
}
/*
* The following must be done under the file system volume device Resource,
* because we are manipulating its GenericTable and accessing foreign FsContext's.
*/
ExAcquireResourceExclusiveLite(&FsvolDeviceExtension->Base.Resource, TRUE);
try
{
/* insert the newly created FsContext into our generic table */
FsContext = FspFsvolDeviceInsertContext(DeviceObject,
FsContext->UserContext, FsContext, &FsContext->ElementStorage, &Inserted);
ASSERT(0 != FsContext);
/* share access check */
if (Inserted)
{
/*
* This is a newly created FsContext. Set its share access and
* increment its open count. There is no need to acquire the
* FsContext's Resource (because it is newly created).
*/
IoSetShareAccess(AccessState->PreviouslyGrantedAccess,
ShareAccess, FileObject, &FsContext->ShareAccess);
FspFileContextOpen(FsContext);
if (FlagOn(CreateOptions, FILE_DELETE_ON_CLOSE))
FsContext->DeleteOnClose = TRUE;
Result = STATUS_SUCCESS;
}
else
{
/*
* This is an existing FsContext. We must acquire its Resource and
* check if there is a delete pending and the share access. Only if
* both tests succeed we increment the open count and report success.
*/
ExAcquireResourceExclusiveLite(FsContext->Header.Resource, TRUE);
if (FsContext->DeletePending)
Result = STATUS_DELETE_PENDING;
else
Result = IoCheckShareAccess(AccessState->PreviouslyGrantedAccess,
ShareAccess, FileObject, &FsContext->ShareAccess, TRUE);
if (NT_SUCCESS(Result))
{
FspFileContextRetain(FsContext);
FspFileContextOpen(FsContext);
if (FlagOn(CreateOptions, FILE_DELETE_ON_CLOSE))
FsContext->DeleteOnClose = TRUE;
}
ExReleaseResourceLite(FsContext->Header.Resource);
}
}
finally
{
ExReleaseResourceLite(&FsvolDeviceExtension->Base.Resource);
}
/* did we fail our share access checks? */
if (!NT_SUCCESS(Result))
{
ASSERT(!Inserted);
FspFsvolCreateCleanupClose(Irp, Response);
FSP_RETURN();
}
/*
* Looks like SUCCESS!
*/
/* did an FsContext with the same UserContext already exist? */
if (!Inserted)
/* delete the newly created FsContext as it is not being used */
FspFileContextRelease(FspIopRequestContext(Request, RequestFsContext));
/* disassociate our FsContext from the Request */
FspIopRequestContext(Request, RequestFsContext) = 0;
/* record the user-mode file system contexts */
FsContext->UserContext = Response->Rsp.Create.Opened.UserContext;
FileObject->FsContext = FsContext;
FileObject->FsContext2 = (PVOID)(UINT_PTR)Response->Rsp.Create.Opened.UserContext2;
/* finish seting up the FileObject */
FileObject->Vpb = FsvrtDeviceObject->Vpb;
/* SUCCESS! */
Irp->IoStatus.Information = Response->IoStatus.Information;
Result = Response->IoStatus.Status;
FSP_LEAVE_IOC(
"FileObject=%p[%p:\"%wZ\"]",
IrpSp->FileObject, IrpSp->FileObject->RelatedFileObject, IrpSp->FileObject->FileName);
}
static VOID FspFsvolCreateCleanupClose(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response)
{
PAGED_CODE();
/*
* This routine handles the case where we must close an open file,
* because of a failure during Create completion. We simply create
* a CreateClose request and we post it as a work item.
*
* Ideally there would be no failure modes for this routine. Reality is
* different.
*
* The more serious (but perhaps non-existent in practice) failure is a
* memory allocation failure. In this case we will leak the user-mode
* file system handle!
*
* This routine may also fail if we cannot post a work item, which means that
* the virtual volume device and the file system volume device are being
* deleted. Because it is assumed that only the user-mode file system would
* initiate a device deletion, this case is more benign (presumably the file
* system knows to close off all its handles when tearing down its devices).
*/
NTSTATUS Result;
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
PDEVICE_OBJECT DeviceObject = IrpSp->DeviceObject;
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvolDeviceExtension->FsvrtDeviceObject);
FSP_FSCTL_TRANSACT_REQ *OriginalRequest = FspIrpRequest(Irp);
FSP_FILE_CONTEXT *FsContext = FspIopRequestContext(OriginalRequest, RequestFsContext);
UINT64 UserContext = Response->Rsp.Create.Opened.UserContext;
UINT64 UserContext2 = Response->Rsp.Create.Opened.UserContext2;
BOOLEAN FileNameRequired = 0 != FsvrtDeviceExtension->VolumeParams.FileNameRequired;
FSP_FSCTL_TRANSACT_REQ *Request;
/* create the user-mode file system request */
Result = FspIopCreateRequest(0, FileNameRequired ? &FsContext->FileName : 0, 0, &Request);
if (!NT_SUCCESS(Result))
goto leak_exit;
/* populate the CreateCleanupClose request */
Request->Kind = FspFsctlTransactCreateCleanupCloseKind;
Request->Req.Cleanup.UserContext = UserContext;
Request->Req.Cleanup.UserContext2 = UserContext2;
Request->Req.Cleanup.Delete = FILE_CREATED == Response->IoStatus.Information;
/* post as a work request */
if (!FspIopPostWorkRequest(DeviceObject, Request))
/* no need to delete the request here as FspIopPostWorkRequest() will do so in all cases */
goto leak_exit;
goto exit;
leak_exit:;
#if DBG
DEBUGLOG("FileObject=%p[%p:\"%wZ\"], UserContext=%llx, UserContext2=%llx: "
"error: the user-mode file system handle will be leaked!",
IrpSp->FileObject, IrpSp->FileObject->RelatedFileObject, IrpSp->FileObject->FileName,
UserContext, UserContext2);
#endif
exit:;
}
VOID FspFsvolCreateRequestFini(PVOID Context[3])
{
PAGED_CODE();
if (0 != Context[RequestFsContext])
FspFileContextRelease(Context[RequestFsContext]);
if (0 != Context[RequestAccessToken])
{
#if DBG
NTSTATUS Result0;
Result0 = ObCloseHandle(Context[RequestAccessToken], KernelMode);
if (!NT_SUCCESS(Result0))
DEBUGLOG("ObCloseHandle() = %s", NtStatusSym(Result0));
#else
ObCloseHandle(Context[RequestAccessToken], KernelMode);
#endif
}
}
NTSTATUS FspCreate(
PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
FSP_ENTER_MJ(PAGED_CODE());
ASSERT(IRP_MJ_CREATE == IrpSp->MajorFunction);
switch (FspDeviceExtension(DeviceObject)->Kind)
{
case FspFsvolDeviceExtensionKind:
FSP_RETURN(Result = FspFsvolCreate(DeviceObject, Irp, IrpSp));
case FspFsvrtDeviceExtensionKind:
FSP_RETURN(Result = FspFsvrtCreate(DeviceObject, Irp, IrpSp));
case FspFsctlDeviceExtensionKind:
FSP_RETURN(Result = FspFsctlCreate(DeviceObject, Irp, IrpSp));
default:
FSP_RETURN(Result = STATUS_INVALID_DEVICE_REQUEST);
}
FSP_LEAVE_MJ(
"FileObject=%p[%p:\"%wZ\"], "
"Flags=%x, "
"DesiredAccess=%#lx, "
"ShareAccess=%#x, "
"Options=%#lx, "
"FileAttributes=%#x, "
"AllocationSize=%#lx:%#lx, "
"Ea=%p, EaLength=%ld",
IrpSp->FileObject, IrpSp->FileObject->RelatedFileObject, IrpSp->FileObject->FileName,
IrpSp->Flags,
IrpSp->Parameters.Create.SecurityContext->DesiredAccess,
IrpSp->Parameters.Create.ShareAccess,
IrpSp->Parameters.Create.Options,
IrpSp->Parameters.Create.FileAttributes,
Irp->Overlay.AllocationSize.HighPart, Irp->Overlay.AllocationSize.LowPart,
Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.Create.EaLength);
}

72
src0/sys/fileobj.c Normal file
View File

@ -0,0 +1,72 @@
/**
* @file sys/fileobj.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
NTSTATUS FspFileContextCreate(PDEVICE_OBJECT DeviceObject,
ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext);
VOID FspFileContextDelete(FSP_FILE_CONTEXT *Context);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFileContextCreate)
#pragma alloc_text(PAGE, FspFileContextDelete)
#endif
NTSTATUS FspFileContextCreate(PDEVICE_OBJECT DeviceObject,
ULONG ExtraSize, FSP_FILE_CONTEXT **PFsContext)
{
PAGED_CODE();
*PFsContext = 0;
FSP_FILE_CONTEXT_NONPAGED *NonPaged = FspAllocNonPaged(sizeof *NonPaged);
if (0 == NonPaged)
return STATUS_INSUFFICIENT_RESOURCES;
FSP_FILE_CONTEXT *FsContext = FspAlloc(sizeof *FsContext + ExtraSize);
if (0 == FsContext)
{
FspFree(NonPaged);
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlZeroMemory(NonPaged, sizeof *NonPaged);
ExInitializeResourceLite(&NonPaged->Resource);
ExInitializeResourceLite(&NonPaged->PagingIoResource);
ExInitializeFastMutex(&NonPaged->HeaderFastMutex);
RtlZeroMemory(FsContext, sizeof *FsContext + ExtraSize);
FsContext->Header.NodeTypeCode = FspFileContextFileKind;
FsContext->Header.NodeByteSize = sizeof *FsContext;
FsContext->Header.IsFastIoPossible = FastIoIsQuestionable;
FsContext->Header.Resource = &NonPaged->Resource;
FsContext->Header.PagingIoResource = &NonPaged->PagingIoResource;
FsRtlSetupAdvancedHeader(&FsContext->Header, &NonPaged->HeaderFastMutex);
FsContext->NonPaged = NonPaged;
FsContext->RefCount = 1;
FsContext->FsvolDeviceObject = DeviceObject;
FspDeviceRetain(FsContext->FsvolDeviceObject);
RtlInitEmptyUnicodeString(&FsContext->FileName, FsContext->FileNameBuf, (USHORT)ExtraSize);
*PFsContext = FsContext;
return STATUS_SUCCESS;
}
VOID FspFileContextDelete(FSP_FILE_CONTEXT *FsContext)
{
PAGED_CODE();
FsRtlTeardownPerStreamContexts(&FsContext->Header);
FspDeviceRelease(FsContext->FsvolDeviceObject);
ExDeleteResourceLite(&FsContext->NonPaged->PagingIoResource);
ExDeleteResourceLite(&FsContext->NonPaged->Resource);
FspFree(FsContext->NonPaged);
FspFree(FsContext);
}

625
src0/sys/fsctl.c Normal file
View File

@ -0,0 +1,625 @@
/**
* @file sys/fsctl.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
/*
* Overview
*
* The fsctl module provides the IOCTL interface to interact with the
* user-mode file system. The user-mode file system can use the IOCTL's
* to create new volumes, delete them (while they are live!) and transact
* with them.
*
*
* Volume Creation
*
* The creation of a new volume is performed using an FSP_FSCTL_CREATE
* IOCTL code. Creation is simple: a new device \Device\Volume{GUID} is
* created and its path is returned to the user-mode file system. The
* user-mode file system also passes a security descriptor to associate
* with the new virtual volume device so that only the creating user-mode
* file system can control the new volume.
*
*
* Volume Deletion
*
* Deletion of an existing volume is performed using FSP_FSCTL_DELETE and
* is quite a bit more involved. We must protect against the following two
* eventualities: (1) that the volume is currently in use and cannot simply
* go away, and (2) that a simultaneous mount operation is taking place
* while we are deleting the volume.
*
* To protect against the first eventuality we maintain a reference count
* on all our device extensions. Every time an MJ function is entered,
* the reference count is incremented (FspDeviceRetain). Every time
* an IRP is completed, the reference count is decremented (FspDeviceRelease).
* When the reference count reaches 0 the device is deleted using
* IoDeleteDevice. This ensures that a device will not go away while an
* IRP is being pending/processed.
*
* To protect against the second eventuality we use the lock (ERESOURCE)
* on the root Fsctl device to wrap volume deletion and attempts from the
* system to mount the same volume. We also mark the virtual volume device
* as Deleted in case we attempt to delete it (FspDeviceRelease) but we
* cannot because it is currently in use.
*
* A sticky point is our use of the Windows VPB. It is not well documented
* how one should handle this structure during forcible dismount. The fastfat
* and cdfs samples use a technique where they keep a spare VPB and they swap
* it with the volume one during forcible dismount. We do something similar.
* The issue is what to do with the old VPB, because we can delete a volume
* that is not currently being used. We check the VPB's ReferenceCount and
* we free the VPB in this case.
*
*
* Volume Transact
*
* The user-mode file system's primary interaction with the kernel-mode driver
* is by using the FSP_FSCTL_TRANSACT IOCTL code. Every virtual volume device
* maintains an FSP_IOQ (refer to ioq.c for more). When an FSP_FSCTL_TRANSACT
* arrives it first processes any responses (FSP_FSCTL_TRANSACT_RSP) that the
* user-mode file system has sent to handle requests sent to it using a prior
* FSP_FSCTL_TRANSACT. It then proceeds to handle any pending IRP requests by
* sending the corresponding requests (FSP_FSCTL_TRANSACT_REQ) to the user-
* mode file system.
*/
static NTSTATUS FspFsctlCreateVolume(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsctlMountVolume(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvrtDeleteVolume(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static WORKER_THREAD_ROUTINE FspFsvrtDeleteVolumeDelayed;
static NTSTATUS FspFsvrtTransact(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsctlFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvrtFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
static NTSTATUS FspFsvolFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
FSP_DRIVER_DISPATCH FspFileSystemControl;
FSP_IOCMPL_DISPATCH FspFileSystemControlComplete;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFsctlCreateVolume)
#pragma alloc_text(PAGE, FspFsctlMountVolume)
#pragma alloc_text(PAGE, FspFsvrtDeleteVolume)
#pragma alloc_text(PAGE, FspFsvrtDeleteVolumeDelayed)
#pragma alloc_text(PAGE, FspFsvrtTransact)
#pragma alloc_text(PAGE, FspFsctlFileSystemControl)
#pragma alloc_text(PAGE, FspFsvrtFileSystemControl)
#pragma alloc_text(PAGE, FspFsvolFileSystemControl)
#pragma alloc_text(PAGE, FspFileSystemControl)
#pragma alloc_text(PAGE, FspFileSystemControlComplete)
#endif
static NTSTATUS FspFsctlCreateVolume(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
/* check parameters */
ULONG InputBufferLength = IrpSp->Parameters.FileSystemControl.InputBufferLength;
ULONG OutputBufferLength = IrpSp->Parameters.FileSystemControl.OutputBufferLength;
PVOID SystemBuffer = Irp->AssociatedIrp.SystemBuffer;
PSECURITY_DESCRIPTOR SecurityDescriptor =
(PVOID)((PUINT8)SystemBuffer + FSP_FSCTL_VOLUME_PARAMS_SIZE);
DWORD SecurityDescriptorSize = InputBufferLength - FSP_FSCTL_VOLUME_PARAMS_SIZE;
if (FSP_FSCTL_VOLUME_PARAMS_SIZE >= InputBufferLength || 0 == SystemBuffer ||
!FspValidRelativeSecurityDescriptor(SecurityDescriptor, SecurityDescriptorSize,
DACL_SECURITY_INFORMATION))
return STATUS_INVALID_PARAMETER;
if (FSP_FSCTL_CREATE_BUFFER_SIZEMIN > OutputBufferLength)
return STATUS_BUFFER_TOO_SMALL;
NTSTATUS Result;
FSP_FSCTL_VOLUME_PARAMS VolumeParams = *(FSP_FSCTL_VOLUME_PARAMS *)SystemBuffer;
PVOID SecurityDescriptorBuf = 0;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension;
/* check the passed in VolumeParams */
if (FspFsctlIrpTimeoutMinimum > VolumeParams.IrpTimeout ||
VolumeParams.IrpTimeout > FspFsctlIrpTimeoutMaximum)
#if DBG
/* allow the debug timeout value on debug builds */
if (FspFsctlIrpTimeoutDebug != VolumeParams.IrpTimeout)
#endif
VolumeParams.IrpTimeout = FspFsctlIrpTimeoutDefault;
if (FspFsctlTransactTimeoutMinimum > VolumeParams.TransactTimeout ||
VolumeParams.TransactTimeout > FspFsctlTransactTimeoutMaximum)
VolumeParams.TransactTimeout = FspFsctlTransactTimeoutDefault;
/* create volume guid */
GUID Guid;
Result = FspCreateGuid(&Guid);
if (!NT_SUCCESS(Result))
return Result;
/* copy the security descriptor from the system buffer to a temporary one */
SecurityDescriptorBuf = FspAlloc(SecurityDescriptorSize);
if (0 == SecurityDescriptorBuf)
return STATUS_INSUFFICIENT_RESOURCES;
RtlCopyMemory(SecurityDescriptorBuf, SecurityDescriptor, SecurityDescriptorSize);
/* prepare the device name and SDDL */
PDEVICE_OBJECT FsvrtDeviceObject;
UNICODE_STRING DeviceSddl;
UNICODE_STRING DeviceName;
RtlInitUnicodeString(&DeviceSddl, L"" FSP_FSVRT_DEVICE_SDDL);
RtlInitEmptyUnicodeString(&DeviceName, SystemBuffer, FSP_FSCTL_CREATE_BUFFER_SIZEMIN);
Result = RtlUnicodeStringPrintf(&DeviceName,
L"\\Device\\Volume{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
Guid.Data1, Guid.Data2, Guid.Data3,
Guid.Data4[0], Guid.Data4[1], Guid.Data4[2], Guid.Data4[3],
Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]);
ASSERT(NT_SUCCESS(Result));
/* create the virtual volume device */
FSP_FSCTL_DEVICE_EXTENSION *FsctlDeviceExtension = FspFsctlDeviceExtension(DeviceObject);
ExAcquireResourceExclusiveLite(&FsctlDeviceExtension->Base.Resource, TRUE);
try
{
Result = FspDeviceCreateSecure(FspFsvrtDeviceExtensionKind, SecurityDescriptorSize,
&DeviceName, FILE_DEVICE_VIRTUAL_DISK,
&DeviceSddl, &FspFsvrtDeviceClassGuid,
&FsvrtDeviceObject);
if (NT_SUCCESS(Result))
{
#pragma prefast(suppress:28175, "We are a filesystem: ok to access SectorSize")
FsvrtDeviceObject->SectorSize = VolumeParams.SectorSize;
FsvrtDeviceExtension = FspFsvrtDeviceExtension(FsvrtDeviceObject);
FsvrtDeviceExtension->FsctlDeviceObject = DeviceObject;
FsvrtDeviceExtension->VolumeParams = VolumeParams;
RtlCopyMemory(FsvrtDeviceExtension->SecurityDescriptorBuf,
SecurityDescriptorBuf, SecurityDescriptorSize);
FspDeviceInitComplete(FsvrtDeviceObject);
Irp->IoStatus.Information = DeviceName.Length + sizeof(WCHAR);
FspFsctlDeviceVolumeCreated(DeviceObject);
}
}
finally
{
ExReleaseResourceLite(&FsctlDeviceExtension->Base.Resource);
}
/* free the temporary security descriptor */
if (0 != SecurityDescriptorBuf)
FspFree(SecurityDescriptorBuf);
return Result;
}
static NTSTATUS FspFsctlMountVolume(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result;
FSP_FSCTL_DEVICE_EXTENSION *FsctlDeviceExtension = FspFsctlDeviceExtension(DeviceObject);
ExAcquireResourceExclusiveLite(&FsctlDeviceExtension->Base.Resource, TRUE);
try
{
PDEVICE_OBJECT *DeviceObjects = 0;
ULONG DeviceObjectCount = 0;
PVPB Vpb = IrpSp->Parameters.MountVolume.Vpb;
PDEVICE_OBJECT FsvrtDeviceObject = IrpSp->Parameters.MountVolume.DeviceObject;
PDEVICE_OBJECT FsvolDeviceObject;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvrtDeviceObject);
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension;
/* check the passed in volume object; it must be one of our own and not marked Deleted */
Result = FspDeviceCopyList(&DeviceObjects, &DeviceObjectCount);
if (NT_SUCCESS(Result))
{
Result = STATUS_UNRECOGNIZED_VOLUME;
for (ULONG i = 0; DeviceObjectCount > i; i++)
if (DeviceObjects[i] == FsvrtDeviceObject)
{
if (FspDeviceRetain(FsvrtDeviceObject))
{
if (!FsvrtDeviceExtension->Deleted &&
FILE_DEVICE_VIRTUAL_DISK == FsvrtDeviceObject->DeviceType)
Result = STATUS_SUCCESS;
else
FspDeviceRelease(FsvrtDeviceObject);
}
break;
}
FspDeviceDeleteList(DeviceObjects, DeviceObjectCount);
}
if (!NT_SUCCESS(Result))
goto exit;
/* create the file system device object */
Result = FspDeviceCreate(FspFsvolDeviceExtensionKind, 0,
DeviceObject->DeviceType,
&FsvolDeviceObject);
if (NT_SUCCESS(Result))
{
/*
* Reference the virtual volume device so that it will not go away while the
* file system device object is alive!
*/
ObReferenceObject(FsvrtDeviceObject);
#pragma prefast(suppress:28175, "We are a filesystem: ok to access SectorSize")
FsvolDeviceObject->SectorSize = FsvrtDeviceExtension->VolumeParams.SectorSize;
FsvolDeviceExtension = FspFsvolDeviceExtension(FsvolDeviceObject);
FsvolDeviceExtension->FsvrtDeviceObject = FsvrtDeviceObject;
FsvrtDeviceExtension->FsvolDeviceObject = FsvolDeviceObject;
FspDeviceInitComplete(FsvolDeviceObject);
Vpb->DeviceObject = FsvolDeviceObject;
Vpb->SerialNumber = FsvrtDeviceExtension->VolumeParams.SerialNumber;
Irp->IoStatus.Information = 0;
}
FspDeviceRelease(FsvrtDeviceObject);
exit:;
}
finally
{
ExReleaseResourceLite(&FsctlDeviceExtension->Base.Resource);
}
return Result;
}
typedef struct
{
PDEVICE_OBJECT FsvolDeviceObject;
PVPB OldVpb;
FSP_WORK_ITEM_WITH_DELAY WorkItemWithDelay;
} FSP_FSVRT_DELETE_VOLUME_WORK_ITEM;
static NTSTATUS FspFsvrtDeleteVolume(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(DeviceObject);
FSP_FSCTL_DEVICE_EXTENSION *FsctlDeviceExtension =
FspFsctlDeviceExtension(FsvrtDeviceExtension->FsctlDeviceObject);
ExAcquireResourceExclusiveLite(&FsctlDeviceExtension->Base.Resource, TRUE);
try
{
PDEVICE_OBJECT FsctlDeviceObject = FsvrtDeviceExtension->FsctlDeviceObject;
PDEVICE_OBJECT FsvolDeviceObject = FsvrtDeviceExtension->FsvolDeviceObject;
PVPB OldVpb;
BOOLEAN DeleteVpb = FALSE;
BOOLEAN DeleteDelayed = FALSE;
LARGE_INTEGER DelayTimeout;
FSP_FSVRT_DELETE_VOLUME_WORK_ITEM *WorkItem = 0;
KIRQL Irql;
/* access check */
Result = FspSecuritySubjectContextAccessCheck(
FsvrtDeviceExtension->SecurityDescriptorBuf, FILE_WRITE_DATA, Irp->RequestorMode);
if (!NT_SUCCESS(Result))
goto exit;
/* pre-allocate a work item in case we need it for delayed delete */
WorkItem = FspAllocNonPaged(sizeof *WorkItem);
if (0 == WorkItem)
{
Result = STATUS_INSUFFICIENT_RESOURCES;
goto exit;
}
/* mark the virtual volume device as deleted */
FsvrtDeviceExtension->Deleted = TRUE;
/* stop the I/O queue */
FspIoqStop(&FsvrtDeviceExtension->Ioq);
/* swap the preallocated VPB */
#pragma prefast(push)
#pragma prefast(disable:28175, "We are a filesystem: ok to access Vpb")
IoAcquireVpbSpinLock(&Irql);
OldVpb = DeviceObject->Vpb;
if (0 != OldVpb && 0 != FsvrtDeviceExtension->SwapVpb)
{
DeviceObject->Vpb = FsvrtDeviceExtension->SwapVpb;
DeviceObject->Vpb->Size = sizeof *DeviceObject->Vpb;
DeviceObject->Vpb->Type = IO_TYPE_VPB;
DeviceObject->Vpb->Flags = FlagOn(OldVpb->Flags, VPB_REMOVE_PENDING);
DeviceObject->Vpb->RealDevice = OldVpb->RealDevice;
DeviceObject->Vpb->RealDevice->Vpb = DeviceObject->Vpb;
FsvrtDeviceExtension->SwapVpb = 0;
DeleteVpb = 0 == OldVpb->ReferenceCount;
DeleteDelayed = !DeleteVpb && 0 != FsvolDeviceObject;
if (DeleteDelayed)
/* keep VPB around for delayed delete */
OldVpb->ReferenceCount++;
}
IoReleaseVpbSpinLock(Irql);
if (DeleteDelayed)
/* keep fsvol around for delayed delete */
FspDeviceRetain(FsvolDeviceObject);
else if (DeleteVpb)
FspFreeExternal(OldVpb);
#pragma prefast(pop)
/* release the file system device and virtual volume objects */
FsvrtDeviceExtension->FsvolDeviceObject = 0;
if (0 != FsvolDeviceObject)
FspDeviceRelease(FsvolDeviceObject);
FspDeviceRelease(DeviceObject);
FspFsctlDeviceVolumeDeleted(FsctlDeviceObject);
/* are we doing delayed delete of VPB and fsvol? */
if (DeleteDelayed)
{
DelayTimeout.QuadPart = 300/*ms*/ * -10000;
WorkItem->FsvolDeviceObject = FsvolDeviceObject;
WorkItem->OldVpb = OldVpb;
FspInitializeWorkItemWithDelay(&WorkItem->WorkItemWithDelay,
FspFsvrtDeleteVolumeDelayed, WorkItem);
FspQueueWorkItemWithDelay(&WorkItem->WorkItemWithDelay, DelayTimeout);
WorkItem = 0;
}
Result = STATUS_SUCCESS;
exit:
if (0 != WorkItem)
FspFree(WorkItem);
}
finally
{
ExReleaseResourceLite(&FsctlDeviceExtension->Base.Resource);
}
return Result;
}
static VOID FspFsvrtDeleteVolumeDelayed(PVOID Context)
{
PAGED_CODE();
FSP_FSVRT_DELETE_VOLUME_WORK_ITEM *WorkItem = Context;
BOOLEAN DeleteVpb = FALSE;
LARGE_INTEGER DelayTimeout;
KIRQL Irql;
IoAcquireVpbSpinLock(&Irql);
ASSERT(0 != WorkItem->OldVpb->ReferenceCount);
DeleteVpb = 1 == WorkItem->OldVpb->ReferenceCount;
if (DeleteVpb)
WorkItem->OldVpb->ReferenceCount = 0;
IoReleaseVpbSpinLock(Irql);
if (DeleteVpb)
{
FspFreeExternal(WorkItem->OldVpb);
FspDeviceRelease(WorkItem->FsvolDeviceObject);
FspFree(WorkItem);
}
else
{
DelayTimeout.QuadPart = 300/*ms*/ * -10000;
FspQueueWorkItemWithDelay(&WorkItem->WorkItemWithDelay, DelayTimeout);
}
}
static NTSTATUS FspFsvrtTransact(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
/* check parameters */
ULONG InputBufferLength = IrpSp->Parameters.FileSystemControl.InputBufferLength;
ULONG OutputBufferLength = IrpSp->Parameters.FileSystemControl.OutputBufferLength;
PVOID SystemBuffer = Irp->AssociatedIrp.SystemBuffer;
if (0 == SystemBuffer ||
(0 != InputBufferLength &&
FSP_FSCTL_DEFAULT_ALIGN_UP(sizeof(FSP_FSCTL_TRANSACT_RSP)) > InputBufferLength))
return STATUS_INVALID_PARAMETER;
if (FSP_FSCTL_TRANSACT_REQ_BUFFER_SIZEMIN > OutputBufferLength)
return STATUS_BUFFER_TOO_SMALL;
NTSTATUS Result;
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension = FspFsvrtDeviceExtension(DeviceObject);
PUINT8 SystemBufferEnd;
FSP_FSCTL_TRANSACT_RSP *Response, *NextResponse;
FSP_FSCTL_TRANSACT_REQ *Request, *PendingIrpRequest;
PIRP ProcessIrp, PendingIrp;
LARGE_INTEGER Timeout;
/* access check */
Result = FspSecuritySubjectContextAccessCheck(
FsvrtDeviceExtension->SecurityDescriptorBuf, FILE_WRITE_DATA, Irp->RequestorMode);
if (!NT_SUCCESS(Result))
return Result;
/* process any user-mode file system responses */
Response = SystemBuffer;
SystemBufferEnd = (PUINT8)SystemBuffer + InputBufferLength;
for (;;)
{
NextResponse = FspFsctlTransactConsumeResponse(Response, SystemBufferEnd);
if (0 == NextResponse)
break;
ProcessIrp = FspIoqEndProcessingIrp(&FsvrtDeviceExtension->Ioq, (UINT_PTR)Response->Hint);
if (0 == ProcessIrp)
/* either IRP was canceled or a bogus Hint was provided */
continue;
FspIopDispatchComplete(ProcessIrp, Response);
Response = NextResponse;
}
/* wait for an IRP to arrive */
KeQuerySystemTime(&Timeout);
Timeout.QuadPart += FsvrtDeviceExtension->VolumeParams.TransactTimeout * 10000;
/* convert millis to nanos and add to absolute time */
while (0 == (PendingIrp = FspIoqNextPendingIrp(&FsvrtDeviceExtension->Ioq, &Timeout)))
{
if (FspIoqStopped(&FsvrtDeviceExtension->Ioq))
return STATUS_CANCELLED;
}
if (FspIoqTimeout == PendingIrp)
{
Irp->IoStatus.Information = 0;
return STATUS_SUCCESS;
}
/* send any pending IRP's to the user-mode file system */
Request = SystemBuffer;
SystemBufferEnd = (PUINT8)SystemBuffer + OutputBufferLength;
ASSERT(FspFsctlTransactCanProduceRequest(Request, SystemBufferEnd));
for (;;)
{
PendingIrpRequest = FspIrpRequest(PendingIrp);
Result = FspIopDispatchPrepare(PendingIrp, PendingIrpRequest);
if (!NT_SUCCESS(Result))
FspIopCompleteIrp(PendingIrp, Result);
else
{
RtlCopyMemory(Request, PendingIrpRequest, PendingIrpRequest->Size);
Request = FspFsctlTransactProduceRequest(Request, PendingIrpRequest->Size);
if (!FspIoqStartProcessingIrp(&FsvrtDeviceExtension->Ioq, PendingIrp))
{
/*
* This can only happen if the Ioq was stopped. Abandon everything
* and return STATUS_CANCELLED. Any IRP's in the Pending and Process
* queues of the Ioq will be cancelled during FspIoqStop(). We must
* also cancel the PendingIrp we have in our hands.
*/
ASSERT(FspIoqStopped(&FsvrtDeviceExtension->Ioq));
FspIopCompleteIrp(PendingIrp, STATUS_CANCELLED);
return STATUS_CANCELLED;
}
/* check that we have enough space before pulling the next pending IRP off the queue */
if (!FspFsctlTransactCanProduceRequest(Request, SystemBufferEnd))
break;
}
PendingIrp = FspIoqNextPendingIrp(&FsvrtDeviceExtension->Ioq, 0);
if (0 == PendingIrp)
break;
}
Irp->IoStatus.Information = (PUINT8)Request - (PUINT8)SystemBuffer;
return STATUS_SUCCESS;
}
static NTSTATUS FspFsctlFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_INVALID_DEVICE_REQUEST;
switch (IrpSp->MinorFunction)
{
case IRP_MN_USER_FS_REQUEST:
switch (IrpSp->Parameters.FileSystemControl.FsControlCode)
{
case FSP_FSCTL_CREATE:
Result = FspFsctlCreateVolume(DeviceObject, Irp, IrpSp);
break;
}
break;
case IRP_MN_MOUNT_VOLUME:
Result = FspFsctlMountVolume(DeviceObject, Irp, IrpSp);
break;
#if 0
case IRP_MN_VERIFY_VOLUME:
break;
#endif
}
return Result;
}
static NTSTATUS FspFsvrtFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_INVALID_DEVICE_REQUEST;
switch (IrpSp->MinorFunction)
{
case IRP_MN_USER_FS_REQUEST:
switch (IrpSp->Parameters.FileSystemControl.FsControlCode)
{
case FSP_FSCTL_DELETE:
Result = FspFsvrtDeleteVolume(DeviceObject, Irp, IrpSp);
break;
case FSP_FSCTL_TRANSACT:
Result = FspFsvrtTransact(DeviceObject, Irp, IrpSp);
break;
}
break;
}
return Result;
}
static NTSTATUS FspFsvolFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_INVALID_DEVICE_REQUEST;
switch (IrpSp->MinorFunction)
{
case IRP_MN_USER_FS_REQUEST:
break;
}
return Result;
}
NTSTATUS FspFileSystemControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
FSP_ENTER_MJ(PAGED_CODE());
ASSERT(IRP_MJ_FILE_SYSTEM_CONTROL == IrpSp->MajorFunction);
switch (FspDeviceExtension(DeviceObject)->Kind)
{
case FspFsvolDeviceExtensionKind:
FSP_RETURN(Result = FspFsvolFileSystemControl(DeviceObject, Irp, IrpSp));
case FspFsvrtDeviceExtensionKind:
FSP_RETURN(Result = FspFsvrtFileSystemControl(DeviceObject, Irp, IrpSp));
case FspFsctlDeviceExtensionKind:
FSP_RETURN(Result = FspFsctlFileSystemControl(DeviceObject, Irp, IrpSp));
default:
FSP_RETURN(Result = STATUS_INVALID_DEVICE_REQUEST);
}
FSP_LEAVE_MJ(
"FileObject=%p%s%s",
IrpSp->FileObject,
IRP_MN_USER_FS_REQUEST == IrpSp->MinorFunction ? ", " : "",
IRP_MN_USER_FS_REQUEST == IrpSp->MinorFunction ?
IoctlCodeSym(IrpSp->Parameters.FileSystemControl.FsControlCode) : "");
}
VOID FspFileSystemControlComplete(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response)
{
FSP_ENTER_IOC(PAGED_CODE());
FSP_LEAVE_IOC(
"FileObject=%p%s%s",
IrpSp->FileObject,
IRP_MN_USER_FS_REQUEST == IrpSp->MinorFunction ? ", " : "",
IRP_MN_USER_FS_REQUEST == IrpSp->MinorFunction ?
IoctlCodeSym(IrpSp->Parameters.FileSystemControl.FsControlCode) : "");
}

102
src0/sys/idevctl.c Normal file
View File

@ -0,0 +1,102 @@
/**
* @file sys/idevctl.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
static NTSTATUS FspFsvolInternalDeviceControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp);
FSP_IOCMPL_DISPATCH FspFsvolInternalDeviceControlComplete;
FSP_DRIVER_DISPATCH FspInternalDeviceControl;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFsvolInternalDeviceControl)
#pragma alloc_text(PAGE, FspFsvolInternalDeviceControlComplete)
#pragma alloc_text(PAGE, FspInternalDeviceControl)
#endif
static NTSTATUS FspFsvolInternalDeviceControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp)
{
PAGED_CODE();
NTSTATUS Result = STATUS_INVALID_DEVICE_REQUEST;
switch (IrpSp->Parameters.DeviceIoControl.IoControlCode)
{
case FSP_FSCTL_WORK:
{
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject);
PDEVICE_OBJECT FsvrtDeviceObject = FsvolDeviceExtension->FsvrtDeviceObject;
if (!FspDeviceRetain(FsvrtDeviceObject))
return STATUS_CANCELLED;
try
{
FSP_FSVRT_DEVICE_EXTENSION *FsvrtDeviceExtension =
FspFsvrtDeviceExtension(FsvrtDeviceObject);
FSP_FSCTL_TRANSACT_REQ *Request = IrpSp->Parameters.DeviceIoControl.Type3InputBuffer;
ASSERT(0 == Request->Hint);
/* associate the passed Request with our Irp; acquire ownership of the Request */
Request->Hint = (UINT_PTR)Irp;
FspIrpRequest(Irp) = Request;
/*
* Post the IRP to our Ioq; we do this here instead of at IRP_LEAVE_MJ time,
* so that we can disassociate the Request on failure and release ownership
* back to the caller.
*/
if (!FspIoqPostIrp(&FsvrtDeviceExtension->Ioq, Irp))
{
/* this can only happen if the Ioq was stopped */
ASSERT(FspIoqStopped(&FsvrtDeviceExtension->Ioq));
Request->Hint = 0;
FspIrpRequest(Irp) = 0;
Result = STATUS_CANCELLED;
goto exit;
}
Result = STATUS_PENDING;
exit:;
}
finally
{
FspDeviceRelease(FsvrtDeviceObject);
}
}
break;
}
return Result;
}
VOID FspFsvolInternalDeviceControlComplete(
PIRP Irp, const FSP_FSCTL_TRANSACT_RSP *Response)
{
FSP_ENTER_IOC(PAGED_CODE());
FSP_LEAVE_IOC("%s", IoctlCodeSym(IrpSp->Parameters.DeviceIoControl.IoControlCode));
}
NTSTATUS FspInternalDeviceControl(
PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
FSP_ENTER_MJ(PAGED_CODE());
ASSERT(IRP_MJ_INTERNAL_DEVICE_CONTROL == IrpSp->MajorFunction);
switch (FspDeviceExtension(DeviceObject)->Kind)
{
case FspFsvolDeviceExtensionKind:
FSP_RETURN(Result = FspFsvolInternalDeviceControl(DeviceObject, Irp, IrpSp));
default:
FSP_RETURN(Result = STATUS_INVALID_DEVICE_REQUEST);
}
FSP_LEAVE_MJ("%s", IoctlCodeSym(IrpSp->Parameters.DeviceIoControl.IoControlCode));
}

109
src0/sys/misc.c Normal file
View File

@ -0,0 +1,109 @@
/**
* @file sys/misc.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <sys/driver.h>
NTSTATUS FspCreateGuid(GUID *Guid);
BOOLEAN FspValidRelativeSecurityDescriptor(
PSECURITY_DESCRIPTOR SecurityDescriptor, ULONG SecurityDescriptorLength,
SECURITY_INFORMATION RequiredInformation);
NTSTATUS FspSecuritySubjectContextAccessCheck(
PSECURITY_DESCRIPTOR SecurityDescriptor, ACCESS_MASK DesiredAccess, KPROCESSOR_MODE AccessMode);
VOID FspInitializeWorkItemWithDelay(FSP_WORK_ITEM_WITH_DELAY *WorkItem,
PWORKER_THREAD_ROUTINE Routine, PVOID Context);
VOID FspQueueWorkItemWithDelay(FSP_WORK_ITEM_WITH_DELAY *WorkItem, LARGE_INTEGER Timeout);
static KDEFERRED_ROUTINE FspQueueWorkItemWithDelayDPC;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspCreateGuid)
#pragma alloc_text(PAGE, FspValidRelativeSecurityDescriptor)
#pragma alloc_text(PAGE, FspSecuritySubjectContextAccessCheck)
#pragma alloc_text(PAGE, FspInitializeWorkItemWithDelay)
#pragma alloc_text(PAGE, FspQueueWorkItemWithDelay)
#endif
NTSTATUS FspCreateGuid(GUID *Guid)
{
PAGED_CODE();
NTSTATUS Result;
int Retries = 3;
do
{
Result = ExUuidCreate(Guid);
} while (!NT_SUCCESS(Result) && 0 < --Retries);
return Result;
}
BOOLEAN FspValidRelativeSecurityDescriptor(
PSECURITY_DESCRIPTOR SecurityDescriptor, ULONG SecurityDescriptorLength,
SECURITY_INFORMATION RequiredInformation)
{
PAGED_CODE();
BOOLEAN Result;
try
{
Result = RtlValidRelativeSecurityDescriptor(SecurityDescriptor, SecurityDescriptorLength,
RequiredInformation);
}
except (EXCEPTION_EXECUTE_HANDLER)
{
Result = FALSE;
}
return Result;
}
NTSTATUS FspSecuritySubjectContextAccessCheck(
PSECURITY_DESCRIPTOR SecurityDescriptor, ACCESS_MASK DesiredAccess, KPROCESSOR_MODE AccessMode)
{
PAGED_CODE();
NTSTATUS Result = STATUS_ACCESS_DENIED;
SECURITY_SUBJECT_CONTEXT SecuritySubjectContext;
ACCESS_MASK GrantedAccess;
SeCaptureSubjectContext(&SecuritySubjectContext);
if (SeAccessCheck(SecurityDescriptor,
&SecuritySubjectContext, FALSE,
DesiredAccess, 0, 0, IoGetFileObjectGenericMapping(), AccessMode,
&GrantedAccess, &Result))
Result = STATUS_SUCCESS;
SeReleaseSubjectContext(&SecuritySubjectContext);
return Result;
}
VOID FspInitializeWorkItemWithDelay(FSP_WORK_ITEM_WITH_DELAY *WorkItem,
PWORKER_THREAD_ROUTINE Routine, PVOID Context)
{
PAGED_CODE();
KeInitializeTimer(&WorkItem->Timer);
KeInitializeDpc(&WorkItem->Dpc, FspQueueWorkItemWithDelayDPC, WorkItem);
ExInitializeWorkItem(&WorkItem->WorkQueueItem, Routine, Context);
}
VOID FspQueueWorkItemWithDelay(FSP_WORK_ITEM_WITH_DELAY *WorkItem, LARGE_INTEGER Timeout)
{
PAGED_CODE();
KeSetTimer(&WorkItem->Timer, Timeout, &WorkItem->Dpc);
}
static VOID FspQueueWorkItemWithDelayDPC(PKDPC Dpc,
PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
{
// !PAGED_CODE();
FSP_WORK_ITEM_WITH_DELAY *WorkItem = DeferredContext;
ExQueueWorkItem(&WorkItem->WorkQueueItem, DelayedWorkQueue);
}