RE: Encrypt Write... Important...

I am also facing the same problem in my disk filter driver…
Since the driver traping each and every request and doing encryption or decryption
on the data… Some time the driver is not getting access to the data and it
gives me this error… How can we solve this problem ???

-----Original Message-----
From: Paul Delivett [SMTP:xxxxx@Carraig.co.uk]
Sent: Thursday, September 07, 2000 5:59 PM
To: File Systems Developers
Subject: [ntfsd] Encrypt Write

Hi again

Please can you tell me if my theory for performing encrypted writes is
correct as I am seeing some IRQL_NOT_LESS_OR_EQUAL errors.

In The IRP_MJ_WRITE

/* Get the Virtual Address of the user buffer */
LPBYTE UserBuffer = (LPBYTE)MmGetSystemAddressForMdl(Irp->MdlAddress);
/* Get the buffer Size */
DWORD BufferSize = Irp->MdlAddress->ByteCount;
/* Allocate my buffer */
LPBYTE MyBuf = (LPBYTE)ExAllocatePool(PagedPool, BufferSize);
/* Copy the data into the buffer */
RtlCopyMemory(MyBuf, UserBuffer, BufferSize);
/* Encrypt Data */
EncipherData(MyBuf, BufferSize);
/* Create a new MDL attached to this IRP */
Irp->MdlAddress = NULL;
IoAllocateMdl(MyBuf, BufferSize, FALSE, FALSE, Irp);
/* Build the MDL to describe the memory pages */
MmProbeAndLockPages(Irp->MdlAddress);
/* Setup the completion Routine so can delete Replacement MDL (MUST DO
ALWAYS) */
IoSetCompletionRoutine(Irp, SFilterWriteCompletion, EncWrite, TRUE, TRUE,
TRUE);
/* Now call the appropriate file system driver with the request. */
Status = IoCallDriver(PtrDeviceExtension->TargetDeviceObject, Irp);
return (Status);

TTFN
Paul Delivett


You are currently subscribed to ntfsd as: ashishm@i3-micro.com
To unsubscribe send a blank email to $subst(‘Email.Unsub’)

Hi

I am having some luck with the following code. I think it crashes because I
have not correctly unlocked and released all the memory in the completion
routine. Any help in that error would be gratefully received.

typedef struct _EncWriteData
{
PMDL OrgMDL;
LPBYTE SystemVirtual;
LPVOID UserVirtual;
} ENCWRITEDATA;

NTSTATUS Write(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
NTSTATUS Status = STATUS_SUCCESS;
/* Get a pointer to this driver’s device extension for the specified
device. */
PtrSFilterDeviceExtension PtrDeviceExtension =
(PtrSFilterDeviceExtension)(DeviceObject->DeviceExtension);
/* Get a pointer to the current stack location in the IRP */
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation( Irp );
/* Setup Next Driver */
IoCopyCurrentIrpStackLocationToNext(Irp);
/* Setup Default completion Routine */
IoSetCompletionRoutine(Irp, SFilterDefaultCompletion, NULL, FALSE, FALSE,
FALSE);
/* Check if File is marked as Enciphered */
BOOLEAN Tracing = EncryptionTraceCheck(Irp);
if ((Tracing) && (Irp->MdlAddress))
{
LPBYTE UserBuffer = (LPBYTE)MmGetSystemAddressForMdl(Irp->MdlAddress);
/* Create the Data Store */
ENCWRITEDATA *EncData = (ENCWRITEDATA *)BWGetMemory(sizeof(ENCWRITEDATA));
/* Get the Size of the Encryption buffer */
DWORD BufferSize = irpSp->Parameters.Write.Length;
/* Get the SeekPos */
DWORD SeekPos = (DWORD)irpSp->Parameters.Write.ByteOffset.QuadPart;
/* Create the Buffer */
EncData->SystemVirtual = (LPBYTE)ExAllocatePool(PagedPool, BufferSize);
/* Copy the Data from the other org buffer */
RtlMoveMemory(EncData->SystemVirtual, UserBuffer, BufferSize);
/* Encipher the buffer */
EncipherBuffer(EncData->SystemVirtual, BufferSize, SeekPos);
/* Preserve the org MDL */
EncData->OrgMDL = Irp->MdlAddress;
Irp->MdlAddress = NULL;
/* Allocate the MDL */
IoAllocateMdl(EncData->SystemVirtual, BufferSize, FALSE, TRUE, Irp);
/* Build the MDL */
MmProbeAndLockPages(Irp->MdlAddress, KernelMode, IoReadAccess);
/* Lock the User Address space */
EncData->UserVirtual = MmMapLockedPages(Irp->MdlAddress, UserMode);
/* Setup the completion Routine so can destroy Enc MDL */
IoSetCompletionRoutine(Irp, SFilterWriteCompletion, EncData, TRUE, TRUE,
TRUE);
}

/* Now call the appropriate file system driver with the request. */
Status = IoCallDriver(PtrDeviceExtension->TargetDeviceObject, Irp);

return (Status);
}

NTSTATUS WriteCompletion(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN
PVOID Context)
{
ENCWRITEDATA *EncWrite = (ENCWRITEDATA *)Context;
/* Unlock the User address space */
MmUnmapLockedPages(EncWrite->UserVirtual, Irp->MdlAddress);
/* Unlock the page */
MmUnlockPages(Irp->MdlAddress);
/* Free Enc MDL */
IoFreeMdl(Irp->MdlAddress);
/* Restore the org MDL */
Irp->MdlAddress = EncWrite->OrgMDL;
/* Free the memory */
ExFreePool(EncWrite->SystemVirtual);
/* Free the struct */
BWFreeMemory((LPBYTE)EncWrite);

/* Propogate the IRP pending flag. */
if (Irp->PendingReturned)
{
IoMarkIrpPending(Irp);
}

return (STATUS_SUCCESS);
}

TTFN
Paul Delivett