Hi,
I have a problem of writing files in driver.
I use this to create file:
status = ZwCreateFile(
FileHandle,
SYNCHRONIZE | GENERIC_ALL,
&oa,
&IoStatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
0, // FILE_SHARE_READ, // | FILE_SHARE_WRITE,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, // | FILE_WRITE_THROUGH,
NULL,
0
);
After that, I call:
status = ZwWriteFile(
*FileHandle,
NULL,
NULL,
NULL,
&IoStatus,
Buffer,
(ULONG)NumberOfBytesToWrite,
ByteOffset,
NULL
);
to write data. But sometimes ZwWriteFile hangs after couple successful calls. However, I’m pretty sure that ZwWriteFile runs in a worker thread which is a part of Csq fashion. Is there any proble with such Cancel-safe-queue worker thread?
Then, with the previous failures, I change the code to:
status = ObReferenceObjectByHandle(
*FileHandle,
0,
*IoFileObjectType,
KernelMode,
(PVOID*)&FileObject,
NULL
);
if(status == STATUS_SUCCESS)
{
DeviceObject = IoGetRelatedDeviceObject(FileObject);
if(DeviceObject)
{
Irp = IoBuildSynchronousFsdRequest(
IRP_MJ_WRITE,
DeviceObject,
Buffer,
(ULONG)NumberOfBytesToWrite,
ByteOffset,
&Event,
&IoStatus
);
if(Irp)
{
status = IoCallDriver(DeviceObject, Irp);
However, this code also causes BSOD when IoCallDriver gets executes:
NTFS_FILE_SYSTEM (24)
If you see NtfsExceptionFilter on the stack then the 2nd and 3rd
parameters are the exception record and context record. Do a .cxr
on the 3rd parameter and then kb to obtain a more informative stack
trace.
Arguments:
Arg1: 00190292
Arg2: f7a7ab00
Arg3: f7a7a7fc
Arg4: 804ee55c
Debugging Details:
EXCEPTION_RECORD: f7a7ab00 – (.exr fffffffff7a7ab00)
ExceptionAddress: 804ee55c (nt!IoIsOperationSynchronous+0x0000000e)
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 00000000
Parameter[1]: 0000002c
Attempt to read from address 0000002c
CONTEXT: f7a7a7fc – (.cxr fffffffff7a7a7fc)
eax=fdc06008 ebx=fdc06008 ecx=00000000 edx=fdc06008 esi=f7a7abe0 edi=804ee54e
eip=804ee55c esp=f7a7abc8 ebp=f7a7abc8 iopl=0 nv up ei pl zr na pe nc
cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00010246
nt!IoIsOperationSynchronous+0xe:
804ee55c f6412c02 test byte ptr [ecx+2Ch],2 ds:0023:0000002c=??
Resetting default scope
PROCESS_NAME: System
ERROR_CODE: (NTSTATUS) 0xc0000005 - The instruction at “0x%08lx” referenced memory at “0x%08lx”. The memory could not be “%s”.
READ_ADDRESS: 0000002c
BUGCHECK_STR: 0x24
DEFAULT_BUCKET_ID: NULL_CLASS_PTR_DEREFERENCE
LAST_CONTROL_TRANSFER: from f7310c0c to 804ee55c
STACK_TEXT:
f7a7abc8 f7310c0c fdc06008 00000000 82f6c910 nt!IoIsOperationSynchronous+0xe
f7a7ac28 804eddf9 82faa020 fdc06008 fdc8ab78 Ntfs!NtfsFsdWrite+0x3e
Any idea about writing file in a driver?
BTW: ZwReadFile always works fine at all time. So weird…