Attaching to a volume

Hi,

I have a question about attaching to another driver; my goal is to attach to an existing volume, such as C:, and monitor all the IRP_MJ_READ and IRP_MJ_CREATE IRPs.

Probably I am not getting the correct handle of C:\ (I am using “\DosDevices\C:\”),
because my driver crashes when IoAttachDeviceToDeviceStack is called.

Below is a part of the code which is supposed to be doing the actual attachment, so that
you can see how I am trying to achieve the functionality.

  
//attaches to DeviceName, such as "\\DosDevices\\C:\\"  
PDEVICE_OBJECT AttachToDevice(IN PUNICODE_STRING DeviceName, IN  
PDEVICE_OBJECT SourceDeviceObject)  
{  
 //target = the driver below  
 //source = myself  
 //attachedTo = the resulting object  
  
NTSTATUS status;  
 PFILE_OBJECT TargetFileObject;  
 PDEVICE_OBJECT TargetDeviceObject;  
 PDEVICE_OBJECT AttachedToDeviceObject;  
  
DbgPrint("CALL AttachToDevice\n");  
 KdBreakPoint();  
  
status=IoGetDeviceObjectPointer(DeviceName,  
 FILE_READ_ATTRIBUTES, //FILE_ALL_ACCESS,  
 &TargetFileObject,  
 &TargetDeviceObject);  
  
if (!NT_SUCCESS(status))  
 {  
 //DbgPrint("FAIL AttachToDevice::IoGetDeviceObjectPointer  
%S\n", DeviceName);  
 //returns further  
 }  
 else //pointer obtained, attach to device  
 {  
  
AttachedToDeviceObject=IoAttachDeviceToDeviceStack(SourceDeviceObject,  
 TargetDeviceObject);  
  
ASSERTMSG("Could not attach to target",  
AttachedToDeviceObject != NULL);  
 if (AttachedToDeviceObject==NULL)  
 {  
 DbgPrint("FAIL AttachToDevice\n");  
  
}  
  
DbgPrint("SUCCESS AttachToDevice\n");  
 return AttachedToDeviceObject;  
 }  
  
return NULL;  
}  

Can someone point out the cause of the problem?

Hi!

What you need is a pointer to the filesystem device object: ZwCreateFile ->
ObReferenceObjectByHandle -> IoGetBaseFileSystemDeviceObject

Search the WDK-fsfilter-samples for the word “SpyIsAttachedToDeviceByName”
to see how to do that. Consider to make a Mini-Filter.

Good luck
Frank

wrote news:xxxxx@ntfsd…
> Hi,
>
> I have a question about attaching to another driver; my goal is to attach
> to an existing volume, such as C:, and monitor all the IRP_MJ_READ and
> IRP_MJ_CREATE IRPs.
>
> Probably I am not getting the correct handle of C:\ (I am using
> “\DosDevices\C:\”),
> because my driver crashes when IoAttachDeviceToDeviceStack is called.
>
> Below is a part of the code which is supposed to be doing the actual
> attachment, so that
> you can see how I am trying to achieve the functionality.
>
> <br>&gt;<br>&gt; //attaches to DeviceName, such as "\\DosDevices\\C:\\"<br>&gt; PDEVICE_OBJECT AttachToDevice(IN PUNICODE_STRING DeviceName, IN<br>&gt; PDEVICE_OBJECT SourceDeviceObject)<br>&gt; {<br>&gt; //target = the driver below<br>&gt; //source = myself<br>&gt; //attachedTo = the resulting object<br>&gt;<br>&gt; NTSTATUS status;<br>&gt; PFILE_OBJECT TargetFileObject;<br>&gt; PDEVICE_OBJECT TargetDeviceObject;<br>&gt; PDEVICE_OBJECT AttachedToDeviceObject;<br>&gt;<br>&gt; DbgPrint("CALL AttachToDevice\n");<br>&gt; KdBreakPoint();<br>&gt;<br>&gt; status=IoGetDeviceObjectPointer(DeviceName,<br>&gt; FILE_READ_ATTRIBUTES, //FILE_ALL_ACCESS,<br>&gt; &amp;TargetFileObject,<br>&gt; &amp;TargetDeviceObject);<br>&gt;<br>&gt; if (!NT_SUCCESS(status))<br>&gt; {<br>&gt; //DbgPrint("FAIL AttachToDevice::IoGetDeviceObjectPointer<br>&gt; %S\n", DeviceName);<br>&gt; //returns further<br>&gt; }<br>&gt; else //pointer obtained, attach to device<br>&gt; {<br>&gt;<br>&gt; AttachedToDeviceObject=IoAttachDeviceToDeviceStack(SourceDeviceObject,<br>&gt; TargetDeviceObject);<br>&gt;<br>&gt; ASSERTMSG("Could not attach to target",<br>&gt; AttachedToDeviceObject != NULL);<br>&gt; if (AttachedToDeviceObject==NULL)<br>&gt; {<br>&gt; DbgPrint("FAIL AttachToDevice\n");<br>&gt;<br>&gt; }<br>&gt;<br>&gt; DbgPrint("SUCCESS AttachToDevice\n");<br>&gt; return AttachedToDeviceObject;<br>&gt; }<br>&gt;<br>&gt; return NULL;<br>&gt; }<br>&gt;
>
>
> Can someone point out the cause of the problem?
>