A Developer's Guide to IRP_MJ_CLEANUP vs. IRP_MJ_CLOSE

Start Building Your Security PoC Today

Get the full-featured 30-day trial SDK for free. Includes pre-built C# /C++/Python/Rust/Java templates to test file, process, and registry security instantly.

If you are developing a file system filter driver using the EaseFilter SDK, you have likely encountered the major function codes IRP_MJ_CLEANUP and IRP_MJ_CLOSE. For developers transitioning into Windows driver development, distinguishing between these two IRPs (I/O Request Packets) is a notorious rite of passage.

At first glance, they seem to do the exact same thing: handle the closing of a file. However, in the Windows I/O manager, they represent two very different stages of a file object's lifecycle. Misunderstanding this distinction can lead to memory leaks, deadlocks, or blocked file deletion operations.

In this post, we will break down the differences between IRP_MJ_CLEANUP and IRP_MJ_CLOSE, when they are triggered, and how to handle them effectively using the EaseFilter SDK.


1. The Core Difference: Handles vs. References

To understand the difference between cleanup and close, you first need to understand how the Windows Object Manager tracks objects. Windows uses two counters for file objects:

  • Handle Count: The number of open user-mode or kernel-mode handles to the file object.
  • Reference Count: The number of internal system pointers to the file object (this includes handles, but also internal references from the Cache Manager or Memory Manager).
The Golden Rule:
IRP_MJ_CLEANUP is sent when the handle count drops to zero.
IRP_MJ_CLOSE is sent when the reference count drops to zero.

2. Understanding IRP_MJ_CLEANUP

IRP_MJ_CLEANUP indicates that the application that opened the file has closed its last handle (e.g., the user-mode application called CloseHandle()).

However, just because the application is done with the file does not mean the system is done with it. The Memory Manager might still have the file mapped in memory, or the Cache Manager might still be flushing delayed write data to the disk. Therefore, the file object itself still exists and is perfectly valid.

What you should do during IRP_MJ_CLEANUP:

  • Flush Buffers: If your filter driver caches data, this is the time to flush it to the underlying file system.
  • Release Byte-Range Locks: Any locks held by the process that opened the handle should be released here.
  • Handle "Delete On Close": If the file was opened with the FILE_FLAG_DELETE_ON_CLOSE flag, the actual deletion logic often begins here.
  • Update Metadata: This is the appropriate time to update file sizes, last modified times, or other metadata, as the user application is officially finished writing.

3. Understanding IRP_MJ_CLOSE

IRP_MJ_CLOSE is the final nail in the coffin. It is sent when the reference count drops to zero. This means not only have all handles been closed, but the Cache Manager, Memory Manager, and all other system components have released their internal pointers to the file object.

At this point, the I/O Manager is preparing to completely destroy and free the file object from memory.

What you should do during IRP_MJ_CLOSE:

  • Free Context Structures: If your filter driver allocated any per-file-object context data (like stream handle contexts), this is where you must free that memory to prevent leaks.
  • Clean Up Driver-Specific Tracking: Remove the file object from any internal tracking lists or trees your driver maintains.
  • Do NOT attempt file I/O: Because the file object is being torn down, you should generally avoid initiating new I/O operations against it during a close routine.

4. Comparison Summary

Feature IRP_MJ_CLEANUP IRP_MJ_CLOSE
Trigger Handle count reaches 0. Reference count reaches 0.
Meaning The application is done with the file. The system is done with the file object.
File Object Status Still valid; Cache/Memory managers may still be using it. Invalidating; about to be destroyed and freed.
Primary Developer Task Flush data, release locks, handle deletion. Free driver-allocated context memory (prevent memory leaks).
Frequency Always precedes IRP_MJ_CLOSE. Always follows IRP_MJ_CLEANUP (though sometimes much later).

5. How EaseFilter SDK Simplifies the Process

Developing kernel-level drivers from scratch requires meticulously managing these IRP lifecycles, and a single mistake can cause a Blue Screen of Death (BSOD).

The EaseFilter SDK significantly lowers this barrier to entry. Instead of wrestling with raw IRP structures and reference counts, the SDK provides higher-level callback events.

  • Pre/Post Cleanup Events: The SDK exposes intuitive events (like OnPostCleanup) allowing you to inject your custom business logic exactly when the application finishes with the file, without worrying about the underlying IRP mechanics.
  • Automatic Context Management: EaseFilter automatically handles much of the boilerplate associated with tracking file contexts, making it much harder to introduce memory leaks during the IRP_MJ_CLOSE phase.
  • Clear Event Separation: The SDK clearly separates Close and Cleanup callbacks in its API, allowing you to gracefully handle file encryption, auditing, or access control at the correct stage of the object's lifecycle.

6. Conclusion

Understanding the difference between IRP_MJ_CLEANUP and IRP_MJ_CLOSE is essential for building stable, memory-safe Windows file system filters. Remember: Cleanup is for the application, Close is for the system. By leveraging the EaseFilter SDK, you can abstract away much of the underlying complexity and focus entirely on your software's core features—whether that is continuous data protection, seamless encryption, or file auditing.