| File System Filter Driver Tutorial |
|
|
|
| Software Development Articles |
| Friday, 06 November 2009 16:42 |
|
This tutorial will show you how to develop a simple file system filter driver. The demo driver will print the names of opening files to debug output. The article requires basic windows driver and C/C++ knowledge. However it may be interesting to the people without windows driver experience. Written by: What is a file system filter driver?A file system filter driver is called on every file system I/O operation (create, read, write, rename and etc) and thus it can modify a file system behavior. File system filter drivers are almost similar to legacy drivers but they require some special steps to do. Such drivers are used by anti-viruses, security, backup and snapshot software. Creating a Simple File System Filter DriverBefore startingTo build a driver you need WDK or IFS Kit. You can get them from the Microsoft’s website. Also you have to set an environment variable Be careful: Even a small error in driver may cause BSOD or system instability. Main.cDriver entryThis is an entry point of any driver. The first thing that we do is to store Set IRP dispatch tableThe next step is to populate the IRP dispatch table with function pointers to IRP handlers. In our filter driver there is a generic pass-through IRP handler (which sends request further). And we will need a handler for Set Fast-IO dispatch tableA file system filter driver must have the fast-io dispatch table. If you’ve forgot to set up the fast-io dispatch table it will lead system to crash. Fast-io is an alternative way to initiate I/O operation (and it’s faster than IRP). Fast-io operations are always synchronous. If fast-io handler returns FALSE then fast-io way is imposible and IRP will be created. Register a notification for file system changesWe should track file system being activated/deactivated to perform attaching/detaching of our file system filter driver. How to start tracking file system changes is shown below. Set driver unload routineThe last part of the driver initialization sets an unload routine. Setting the driver unload routine makes the driver unloadable and you can load/unload it multiple times without system restart. However this driver is made unloadable only for debugging purpose because file system filters can’t be unloaded safely. Never do this in production code. Driver unload implementationDriver unload routine is responsible for cleaning up and deallocation of resources. First of all unregister the notification for file system changes. Then loop through the devices we created, detach and delete them. Wait for 5 seconds to let all outstanding IRPs to be completed. As it was mentioned before this is a debug only solution. It works in the majority of cases but there is no guarantee for all. IrpDispatch.cDispatch pass-throughThis IRP handler does nothing except passing requests further to the next driver. We have the next driver object stored in our device extension. Dispatch createThis IRP handler is invoked on every create file operation. We will grab a filename from FastIo.cTo test the fast-io dispatch table validity for the next driver we will use the following helper macro (not all of the fast-io routines must be implemented by the underlying file system, so we have to be sure in that): Fast-IO pass-throughPassing through fast-io requests requires writing a lot of code (in contrast of passing through IRP requests) because each fast-io function has its own set of parameters. Typical pass-through function is shown below: Fast-IO detach deviceThis is a special fast-io request which we have to handle ourselves and not to call the next driver. We have to detach our filter device from the file system device stack and delete our device. That can be done easily by the following code: Notification.cA typical file system consists of a control device and volume devices. A volume device is attached to the storage device stack. Control device is registered as a file system.
Figure 1 - Devices of the typical file system We have a callback which is invoked for all active file systems and whenever a file system has either registered or unregistered itself as active one. This is a good place to attach/detach our filter device. When a file system activates itself we attach to its control device (only if we are not already attached), enumerate its volume devices and attach to them too. On file system deactivation we examine file system control device stack, find our device and detach it. Detaching from file system volume devices is performed in AttachDetach.cThis file contains helper routines for attaching, detaching and checking whether our filter is already attached. AttachingTo perform attaching we create a new device object with device extension (call DetachingDetaching is quite simple. Get “attached to” device object from the device extension and call Checking whether our device is attachedTo check whether we are attached to a device we have to iterate through the device stack (using Sources and makefileSources and makefile files are used by build utility to build the driver. It contains project settings and source file names. Sources file contents: The makefile is standard: MSVC makefile project build command line is: How to install a driverSC.EXE overviewWe will use sc.exe (sc – service control) to manage our driver. It is a command-line utility that can be used to query or modify the database of installed services. It is shipped with Windows XP and higher or you can find it in Windows SDK/DDK. InstallTo install the driver call: A new service entry will be created with the name FsFilter, service type will be filesystem and binary path c:\FsFilter.sys. StartTo start the driver call: This starts a service named StopTo stop the driver call: This stop the service named UninstallAnd to uninstall call: This instructs service manager to delete service entry with the name FsFilter. Resulting scriptAll those commands are put into a single batch file to make driver testing easier. There is a listing of Install.cmd command file: Running a SampleThis is the most interesting part. To demonstrate the file system filter work we will use Sysinternals DebugView for Windows to monitor debug output and OSR Device Tree to see devices and drivers. So, build the driver. Then copy build output file FsFilter.sys and the install script Install.cmd to the root of the disk C.
Figure 2 - The driver and the install script on the disk. Run Install.cmd. It will install and start the driver and then wait for user input.
Figure 3 - The driver is successfully installed and started. Now start DebugView utility.
Figure 4 - Monitoring debug output. We can see what files are being opened! Our filter works. Now run the device tree utility and locate our driver there.
Figure 5 - Our filter driver in the device tree. We can see numerous devices created by our driver. Now let’s open NTFS driver and look at thedevice tree:
Figure 6 - Our filter is attached to NTFS. We are attached. Let’s take a look at the other file systems.
Figure 7 - Our filter is attached to the other file systems too. And finally press any key to move our install script forward. It will stop and uninstall the driver. Figure 8 - The driver is stopped and uninstalled. Refresh the device tree list by pressing F5: Figure 9 - There is no our filter devices in the device tree. Our filter is gone. The system is running as before. ImprovementsThe sample driver lacks a commonly required functionality of attaching to the newly arrived volumes. It is done so to make the driver as easy to understand as possible. You can write ConclusionThis tutorial showed how to create a simple file system filter driver, how to install, start, stop and uninstall it from a command line. Also some file system filter driver aspects were discussed. We saw file system device stack with attached filters and learned how to monitor debug output from the driver. You may use the provided sources as a skeleton for your own file system filter driver and modify its behavior. Useful references
Download source files of the sample project. |













