cwindowsfilter-driver

How to block file and folder creation in Legacy filter driver for windows


I am trying to create a legacy filter driver for windows to block the "creation of file and folder" in external storage devices.

I tried with the following code

if( lDeviceType==cwUSBDRIVE || lDeviceType==cwEXTERNALHDD)
{
  if(irpSp->MajorFunction==IRP_MJ_CREATE)
  {
   if((irpSp->Parameters.Create.Options)&FILE_DIRECTORY_FILE)
    {
            Irp->IoStatus.Status = STATUS_ACCESS_DENIED;//Deny Access
        Irp->IoStatus.Information = 0;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        return STATUS_ACCESS_DENIED;
    }
   else if((irpSp->Parameters.Create.Options)&FILE_NON_DIRECTORY_FILE)
   {

        Irp->IoStatus.Status = STATUS_ACCESS_DENIED;//Deny Access
        Irp->IoStatus.Information = 0;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
         return STATUS_ACCESS_DENIED;

   }
  }
}

while working with the code it block's the file/folder creation. But it also block's file copying from device and file opening operations.

I need to block the creation of file\folder and allow the copying from the device and opening the files.


Solution

  • Thanks a lot, @RbMm. This issue was solved using CREATE DISPOSITION. Below I am posting the code that I used to solve this issue. As a reference to others,

                  if((irpSp->Parameters.Create.Options)&FILE_DIRECTORY_FILE)
                       {
                           if((irpSp->Parameters.Create.Options >> 24) == FILE_CREATE)
                           {
                               Irp->IoStatus.Status = STATUS_ACCESS_DENIED;//Deny Access
                               Irp->IoStatus.Information = 0;
                               IoCompleteRequest(Irp, IO_NO_INCREMENT);
                               return STATUS_ACCESS_DENIED;
                           }
                       }
                       else if((irpSp->Parameters.Create.Options)&FILE_NON_DIRECTORY_FILE)
                       {
                           if((irpSp->Parameters.Create.Options >> 24) == FILE_CREATE)
                           {
                               Irp->IoStatus.Status = STATUS_ACCESS_DENIED;//Deny Access
                               Irp->IoStatus.Information = 0;
                               IoCompleteRequest(Irp, IO_NO_INCREMENT);
                               return STATUS_ACCESS_DENIED;
                           }
                }