Hello friends,
I’m in a big trouble. I wrote a encryption/decryption disk filter driver.
Which is a modification of a disk performance monitor filter driver. This driver
works fine with the debugging version. I am able to write the file, create any
folder, even I am able to format the drive in FAT and NTFS also. But When I try
to use free build It started giving me problems, like it corrupt the FAT when
i creat any folder or file on the drive.
I guess this problem is becaused of the updation of the original buffer
with the encrypted data while writing which I decrypt when the request is completed.
am I thinking in the right direction ? If yes then how can I solve the problem ?
following is my code , please give me some suggesstions to improve it…
Thanks in advance
Ashish
NTSTATUS DiskWrite ( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp )
{
PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
PDEVICE_EXTENSION physicalDisk = deviceExtension->PhysicalDevice->DeviceExtension;
PIO_STACK_LOCATION currentIrpStack = IoGetCurrentIrpStackLocation(Irp);
PIO_STACK_LOCATION nextIrpStack = IoGetNextIrpStackLocation(Irp);
PMDL StoreUserMdl;
PCHAR Buffer;
PCHAR TempBuffer;
BOOLEAN BufferFlag = FALSE;
#if DBG
InputPrint ( ( 1,“\n WRITE \n D - %d P - %d S - %ld”,deviceExtension -> DiskNumber \
,deviceExtension -> LastPartitionNumber \
,(ULONG) ( currentIrpStack->Parameters.Write.ByteOffset.LowPart>>9 ) ) );
#endif
StoreUserMdl = Irp -> MdlAddress;
while ( StoreUserMdl != NULL )
{
if ( !( ( StoreUserMdl )-> MdlFlags & ( MDL_MAPPED_TO_SYSTEM_VA | MDL_SOURCE_IS_NONPAGED_POOL ) ) )
{
#if DBG
InputPrint ( ( 1,"\n Buffer " ) );
#endif
Buffer = MmMapLockedPages ( StoreUserMdl, KernelMode );
BufferFlag = TRUE;
}
else
{
Buffer = ( PCHAR ) StoreUserMdl -> MappedSystemVa + StoreUserMdl -> ByteOffset;
BufferFlag = FALSE;
}
TempBuffer = ExAllocatePool ( NonPagedPool, StoreUserMdl -> ByteCount );
if ( TempBuffer == NULL )
{
#if DBG
InputPrint ( ( 1,"\n Unable to allocate the memory " ) );
#endif
return STATUS_UNSUCCESSFUL;
}
RtlCopyBytes ( TempBuffer, Buffer, StoreUserMdl -> ByteCount );
if ( EncryptBlock ( TempBuffer, StoreUserMdl -> ByteCount ) == FALSE )
{
ExFreePool ( TempBuffer );
return STATUS_UNSUCCESSFUL;
}
RtlCopyBytes ( Buffer, TempBuffer, StoreUserMdl -> ByteCount );
ExFreePool ( TempBuffer );
if ( BufferFlag == TRUE )
{
MmUnmapLockedPages ( Buffer, StoreUserMdl );
}
StoreUserMdl = StoreUserMdl -> Next;
}// end of while
#if DBG
InputPrint ( ( 1, “\n Write Sector %d Encrypted\n”, (ULONG) ( currentIrpStack->Parameters.Write.ByteOffset.LowPart>>9) ) );
#endif
/* Copy current stack to next stack.*/
* nextIrpStack = * currentIrpStack;
/* Set completion routine callback.*/
IoSetCompletionRoutine ( Irp, DiskIoCompletion,DeviceObject,TRUE,TRUE,TRUE );
/* Call to the disk driver.*/
return IoCallDriver ( deviceExtension -> TargetDeviceObject, Irp );
} // end DiskReadWrite()
NTSTATUS DiskRead ( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp )
{
PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
PDEVICE_EXTENSION physicalDisk = deviceExtension->PhysicalDevice->DeviceExtension;
PIO_STACK_LOCATION currentIrpStack = IoGetCurrentIrpStackLocation ( Irp );
PIO_STACK_LOCATION nextIrpStack = IoGetNextIrpStackLocation ( Irp );
/* Copy current stack to next stack.*/
* nextIrpStack = * currentIrpStack;
/* Set completion routine callback.*/
IoSetCompletionRoutine ( Irp, DiskIoCompletion,DeviceObject,TRUE,TRUE,TRUE );
/* Return the results of the call to the disk driver.*/
return IoCallDriver ( deviceExtension->TargetDeviceObject, Irp );
} // end DiskRead()
NTSTATUS DiskIoCompletion ( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context )
{
PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
PDEVICE_EXTENSION physicalDisk = deviceExtension->PhysicalDevice->DeviceExtension;
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation ( Irp );
PDISK_PERFORMANCE partitionCounters = &deviceExtension->DiskCounters;
PDISK_PERFORMANCE diskCounters = &physicalDisk->DiskCounters;
PMDL StoreUserMdl;
PCHAR Buffer;
PCHAR TempBuffer;
BOOLEAN BufferFlag = FALSE;
UNREFERENCED_PARAMETER ( Context );
if ( irpStack->MajorFunction == IRP_MJ_READ )
{
StoreUserMdl = Irp -> MdlAddress;
#if DBG
InputPrint ( ( 1,“\n\n READ \n D - %d P - %d S - %ld”,deviceExtension -> DiskNumber \
,deviceExtension -> LastPartitionNumber \
,(ULONG) ( irpStack->Parameters.Read.ByteOffset.LowPart>>9 ) ) );
#endif
while ( StoreUserMdl != NULL )
{
if ( ( ( StoreUserMdl )->MdlFlags & ( MDL_PAGES_LOCKED ) ) )
{
#if DBG
InputPrint ( ( 1,"\n Buffer " ) );
#endif
Buffer = MmMapLockedPages ( StoreUserMdl, KernelMode );
BufferFlag = TRUE;
}
else
{
Buffer = ( PCHAR ) StoreUserMdl -> MappedSystemVa + StoreUserMdl -> ByteOffset;
BufferFlag = FALSE;
}
TempBuffer = ExAllocatePool ( NonPagedPool, StoreUserMdl -> ByteCount );
if ( TempBuffer == NULL )
{
#if DBG
InputPrint ( ( 1,"\n Unable to allocate the memory " ) );
#endif
return STATUS_UNSUCCESSFUL;
}
RtlCopyBytes ( TempBuffer, Buffer , StoreUserMdl -> ByteCount );
if ( DecryptBlock ( TempBuffer, StoreUserMdl -> ByteCount ) == FALSE )
{
ExFreePool ( TempBuffer );
return STATUS_UNSUCCESSFUL;
}
RtlCopyBytes ( Buffer, TempBuffer , StoreUserMdl -> ByteCount );
ExFreePool ( TempBuffer );
if ( BufferFlag == TRUE )
{
MmUnmapLockedPages ( Buffer, StoreUserMdl );
}
#if DBG
InputPrint ( ( 1, “\n Read Sector %d Decrypted\n”, (ULONG) ( irpStack->Parameters.Read.ByteOffset.LowPart>>9 ) ) );
#endif
StoreUserMdl = StoreUserMdl -> Next;
}// end of while
}
else // ------ Write Request ----- //
{
StoreUserMdl = Irp -> MdlAddress;
while ( StoreUserMdl != NULL )
{
if ( ( ( StoreUserMdl ) -> MdlFlags & ( MDL_MAPPED_TO_SYSTEM_VA | MDL_SOURCE_IS_NONPAGED_POOL ) ) )
{
#if DBG
InputPrint ( ( 1,"\n Buffer " ) );
#endif
Buffer = MmMapLockedPages ( StoreUserMdl, KernelMode );
BufferFlag = TRUE;
}
else
{
Buffer = (PCHAR) StoreUserMdl -> MappedSystemVa + StoreUserMdl -> ByteOffset;
BufferFlag = FALSE;
}
TempBuffer = ExAllocatePool ( NonPagedPool, StoreUserMdl -> ByteCount );
if ( TempBuffer == NULL )
{
#if DBG
InputPrint ( ( 1,"\n Unable to allocate the memory " ) );
#endif
return STATUS_UNSUCCESSFUL;
}
RtlCopyBytes ( TempBuffer, Buffer , StoreUserMdl -> ByteCount );
if ( DecryptBlock ( TempBuffer, StoreUserMdl -> ByteCount ) == FALSE )
{
ExFreePool ( TempBuffer );
return STATUS_UNSUCCESSFUL;
}
RtlCopyBytes ( Buffer, TempBuffer , StoreUserMdl -> ByteCount );
ExFreePool ( TempBuffer );
if ( BufferFlag == TRUE )
{
MmUnmapLockedPages ( Buffer, StoreUserMdl );
}
#if DBG
InputPrint ( ( 1, “\n Write Complete Sector %d Decrypted\n”, (ULONG) ( irpStack->Parameters.Read.ByteOffset.LowPart>>9 ) ) );
#endif
StoreUserMdl = StoreUserMdl -> Next;
}// end of while
}// End of else Write
if ( Irp->PendingReturned )
{
IoMarkIrpPending ( Irp );
}
return STATUS_SUCCESS;
} // DiskIoCompletion