Hello, everyone
I have a simple driver for virtual device, and it handles some ioctl, and each does very simple job. after I call DeviceIoControl from app, the driver pops up an access violation. even i make the ioctl handler do almost nothing. my code is below.
#define FILE_DEVICE_VS8 0x00008260
#define VS8_IOCTL_INDEX 0x860
#define IOCTL_VS8_INIT CTL_CODE(FILE_DEVICE_VS8, \
VS8_IOCTL_INDEX + 1, \
METHOD_BUFFERED, \
FILE_ANY_ACCESS)
NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath)
{
UNICODE_STRING DeviceNameUnicodeString;
UNICODE_STRING DeviceLinkUnicodeString;
NTSTATUS ntStatus;
PDEVICE_OBJECT DeviceObject = NULL;
KdPrint((“Vs8 Entering DriverEntry\n”));
RtlInitUnicodeString (&DeviceNameUnicodeString, L"\Device\Vs8");
ntStatus = IoCreateDevice (DriverObject,
0,
&DeviceNameUnicodeString,
FILE_DEVICE_VS8,
0,
TRUE,
&DeviceObject);
if (NT_SUCCESS(ntStatus))
{
DriverObject->MajorFunction[IRP_MJ_CREATE] =
DriverObject->MajorFunction[IRP_MJ_CLOSE] =
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = Vs8Dispatch;
DriverObject->DriverUnload = Vs8Unload;
RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\DosDevices\Vs8");
ntStatus = IoCreateSymbolicLink (&DeviceLinkUnicodeString,
&DeviceNameUnicodeString);
if (!NT_SUCCESS(ntStatus))
{
KdPrint((“Vs8 ERROR: IoCreateSymbolicLink failed”));
IoDeleteDevice (DeviceObject);
return ntStatus;
}
}
else
{
KdPrint((“Vs8 ERROR: IoCreateDevice failed\n”));
}
KdPrint((“Vs8 Leaving DriverEntry\n”));
return ntStatus;
}
NTSTATUS Vs8Dispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
PIO_STACK_LOCATION IrpStack;
ULONG dwInputBufferLength;
ULONG dwOutputBufferLength;
ULONG dwIoControlCode;
NTSTATUS ntStatus;
UCHAR *pbValue;
LARGE_INTEGER largeint;
int i;
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IrpStack = IoGetCurrentIrpStackLocation(Irp);
switch (IrpStack->MajorFunction)
{
case IRP_MJ_CREATE:
KdPrint((“IRP_MJ_CREATE\n”));
break;
case IRP_MJ_CLOSE:
KdPrint((“IRP_MJ_CLOSE\n”));
break;
case IRP_MJ_DEVICE_CONTROL:
{
pbValue = (PUCHAR)(Irp->AssociatedIrp.SystemBuffer);
dwInputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
dwOutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
dwIoControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
KdPrint((“IRP_MJ_DEVICE_CONTROL:%d\n”,dwIoControlCode));
switch (dwIoControlCode)
{
case IOCTL_VS8_INIT:
{ // i make here do almost nothing, but i call deviceiocontrol in app still gets access violation
char al;
al = 0;
break;
}
case …:
{
break;
}
default:
KdPrint((“ERROR: Unknown IRP_MJ_DEVICE_CONTROL\n”));
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
break;
}
break;
}
}
ntStatus = Irp->IoStatus.Status;
IoCompleteRequest (Irp, IO_NO_INCREMENT);
KdPrint((“Leaving Vs8Dispatch\n”));
return ntStatus;
}
in my app :
hDriver = CreateFile(“\\.\VS8”,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(!DeviceIoControl(hDriver,IOCTL_VS8_INIT,NULL,0,NULL,0,NULL,NULL))
{
…
}
i traced with softice, my ioctl handler goes no error, but access violation occurs after it and in kernel’s deviceiocontrol.
thank in advance
metawest