Racing round and round: The little bug that could
A logic error in the Microsoft Kernel Streaming Service driver's cleanup routine caused a use-after-free when a foreign process closed the last handle to a shared object, allowing an attacker to trigger memory corruption. The vulnerability could be reliably triggered and was exploited to achieve a kernel read/write primitive on a fully patched Windows 11 system.
securityintelligence.com
Published on securityintelligence.com
By Valentina Palmiotti
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
CVE-2024-30089 is a kernel vulnerability in the Microsoft Kernel Streaming Service (mskssrv.sys), the driver behind the MS KS Server (MSKSSRV) subsystem. The author used it to exploit a fully updated Windows 11 machine with all Virtualization Based Security and hardware security mitigations enabled, and it scored a first win at Pwn2Own. The bug is a logic error that occurs in a specific state of an inter-process communication system and results in a use-after-free (CWE-416). It can be triggered reliably.
MSKSSRV interacts with a userland process through FSStreamReg and FSContextReg objects, both derived from the base FSRegObject class. Opening a file handle to the MSKSSRV device with CreateFile creates a FILE_OBJECT for that handle. Using the handle, a process can initialize a new stream or context object by sending the device an IOCTL through DeviceIoControl. The initializing process designates which remote process may register the object by passing the process ID in the lpInBuffer argument. The remote process, using a new file handle to the MSKSSRV device, then registers the object via device IOCTL. The pointer to the FSRegObject is stored in Irp->CurrentStackLocation->FileObject->FsContext2.
The same pointer is stored twice in FsContext2: once in the FILE_OBJECT used by the initializing process and once in the FILE_OBJECT used by the registering process. References to the FSRegObject can therefore be shared across processes. With an FSStreamReg object, for example, multiple processes can access the stream frame buffer. An FSRegObject reference count is initialised to 1, incremented again after initialisation completes, and incremented once more when the object is registered, for a total of three references per object.

Diagram 1: Inter-process Communication in MS KS Server


Diagram 2: Initializing and Registering FSContextReg Objects

Setup
The hunt was done by reading the driver code and mentally emulating the program logic for the possible states of the IPC system; previous versions from Winbindex were analysed later to find how the bug was introduced. The target was a fully updated Windows 11 machine with Virtualization Based Security and hardware security mitigations enabled. The report does not describe a separate lab setup, accounts or tooling beyond this.
Finding it
An unlocked reference count
The starting inspiration was a blog post by k0shl about a vulnerability in which an object's reference count is initialised and incremented without proper locking, creating a use-after-free window. That bug was in userland, not the kernel, but the coding style of the vulnerable library reminded the author of the MSKSSRV driver.
Looking at MSKSSRV, FSContextReg does not implement a locking vtable function, and the base class (FSRegObject) implementation is simply a nop instruction. No locking mechanism is implemented for FSContextReg objects. FSStreamReg, by contrast, implements a locking function that uses a mutex. The locking mechanism is used when accessing the objects for cleanup, in FSRendezvousServer::Close:

Code Block 1: FSRendezvousServer::Close, locking and unlocking path for FSRegObjects

Two object types derive from FSRegObject, but only one implements proper locking. Attempts to trigger undefined behaviour through references to the unprotected FSContextReg object did not succeed, because of the locking protections on the global FSRendezvousServer object that holds the pointers to the lists of FSRegObjects. The reference counting system still looked suspicious.
Dispatch routines and process context
FSRendezvousClose, which calls FSRendezvousServer::Close, is called within the driver's dispatch cleanup and close function routines:

Code Block 2: DispatchCleanup and DispatchClose routines for MS KS Server Driver

A dispatch routine handles one or more types of IRPs, which are packaged I/O requests. When all handle references to a file have been closed, the corresponding file system driver receives an IRP_MJ_CLEANUP and an IRP_MJ_CLOSE request, handled by the driver's DispatchCleanup and DispatchClose routines. In MSKSSRV, FSRendezvousClose is called within both, if the pointer stored in Irp->CurrentStackLocation->FileObject->FsContext2 is not NULL.
FSRendezvousServer::Close contains various checks on the caller's process ID. Process context matters because all kernel mode code operates within a single kernel address space, separate from user-mode address spaces. Each process has its own user-mode memory context, and the process context in which a kernel thread executes determines which process' user-mode address space is accessed if the thread accesses user addresses. The following code checks whether the calling process is the initializing or registering process:

Code Block 3: FSRendezvousServer::Close, Process checks on FSRegObjects

Process-specific information is stored in the FSRegObject in Irp->CurrentStackLocation->FileObject->FsContext2 at initialisation or registration time. The driver determines which process-specific resources (EPROCESS objects, event objects and other things) it needs to release by checking the caller's process ID. If the process is the initializing or registering process, additional cleanup is done for those resources.
This stood out because dispatch routines generally execute in an arbitrary process context, with some exceptions: the system picks a thread to do the dispatch work, and which thread it picks is arbitrary. DispatchCleanup is called in the process context of the process that closed the final handle, so the process ID checks make sense there. DispatchClose is called from an arbitrary process context. If FSRendezvousServer::Close runs as a result of an IRP_MJ_CLOSE request, it would be from an arbitrary process context, defeating the purpose of the process ID checks.
Shared handles
Handles can be shared with other processes, by child process inheritance or with the DuplicateHandle API function. Through the shared file handle, the other process can interact with the same FILE_OBJECT.

Diagram 3: Sharing MS KS Server Device Handle

The process context during DispatchCleanup can therefore be neither the initialising nor the registering process. If either handle is duplicated and shared to another process, and that process is the last to close the handle, DispatchCleanup is called in the context of that foreign process.

Diagram 4: Foreign Process Closing Last Handle to a FILE_OBJECT

Counting dereferences
FSRendezvousServer::Close can be called twice when a file handle is closed: once for the IRP_MJ_CLEANUP request and once for the IRP_MJ_CLOSE request (Code Block 2). Within the function there are two possible calls to FSRegObject::Release (Code Block 4), which dereferences the object and frees its memory if the reference count drops to 0. FSRegObject::Release could therefore be called up to four times for a handle.
There are also two file handles whose FILE_OBJECT points to the same FSRegObject through the FSContext2 pointer, giving a possibility of calling FSRegObject::Release up to eight times on the same object, four for each handle. With an original reference count of only three, too many dereferences could trigger a use after free. The program structure would probably prevent hitting the theoretical maximum, but maybe not quite enough.

Code Block 4: FSRegObject::Release can be called twice from FSRendezvousServer::Close

More dereferences than references is not the only way a use after free can occur, but in this case, if an FSRegObject has been freed, its reference count has dropped to zero. The last time a valid FSRegObject is accessed during the IRP_MJ_CLEANUP/CLOSE requests is in a call to FSRegObject::Release. If a use after free is possible, a call to FSRegObject::Release will always occur after the object has already been freed, and during the call the object is dereferenced once more. Counting dereferences is therefore a good heuristic for finding use after frees in this case.
The remaining work was to trace the possible states of the program, noting when the object is freed and accessed, by mentally emulating the program logic during IRP_MJ_CLEANUP/CLOSE requests, each beginning with the corresponding dispatch functions (Code Block 2), for each possible case.
The states are based on which process closes the final reference to a handle. Each entry represents the number of dereferences of the FSContextReg object that occur if the corresponding process closes the final handle. There is no functional difference between HANDLE #1 (initializing handle) and HANDLE #2 (registering handle), because the FileObject->FSContext2 field points to the same memory in both FILE_OBJECTs represented by the corresponding handles.

FSContextReg dereferences for each of the possible MSKSSRV IPC states

The last state results in four dereferences: two by the foreign process and two by the initializing (and also registering) process, while only three references exist initially, so a use after free is possible. The same exercise with FSStreamReg objects showed that, due to a memory leak bug in the code, it is not possible to ever free an FSStreamReg object after it has been registered.
Exploitation
Step 1: Understand the cleanup path
If the process is the initializing or registering process, the appropriate cleanup happens and the pointer stored in Irp->CurrentStackLocation->FileObject->FsContext2 is set to NULL. The code block below shows where this occurs when the caller is the initializing process:

Code Block 5: FSRegObject::CloseInitProcess sets FSContext pointer to NULL

This means FSRendezvousClose will not be called again during completion of the IRP_MJ_CLOSE request in DispatchClose (SrvDispatchClose, Code Block 2).
Step 2: Close through a foreign process
If the calling process is a foreign process, no cleanup occurs and FSRegObject::Release is called once near the end of the function. Since FileObject->FsContext2 is not NULL, FSRendezvousServer::Close is called again during the subsequent IRP_MJ_CLOSE request and another call to FSRegObject::Release occurs.
Step 3: Close the second handle from the initialising process
If the second handle is closed by a process that both initialised and registered the FSContextReg object, the object cleans up all its stored process resources, making it empty. This causes FSRegObject::Release to be called twice within FSRendezvousServer::Close (Code Block 4). The extra dereference accounts for the extra initializing reference once the object is empty. The total is four dereferences on a single FSContextReg object, indicating a use after free.

Diagram 5: CVE-2024-30089 depicted

Step 4: Respect the list validation
A foreign process cannot be the last one to close both handles, even though this would seemingly also lead to four dereferences. Before the object is destructed and freed, it is unlinked from a list stored in the global FSRendezvousServer object. At the beginning of FSRendezvousServer::Close, the pointer in FSContext2 is checked to be a valid member of the list. The object is freed in the second call to FSRegObject::Release at the end of the function. During the fourth call to FSRendezvousServer::Close, the object has already been unlinked from the list, making it an invalid object, so it cannot be used. To trigger a use after free, it must occur after the object has been retrieved and validated in FSRendezvousServer::Close. The code snippet below shows the use-after-free primitive that the vulnerability can provide:

Code Block 6: UAF primitive path

Attack complexity
The security update guide for this vulnerability gives a CVSS score indicating that exploitation is more likely and an attack complexity of Low. Microsoft does not provide detailed explanations for its scoring, but the vulnerability likely received this score because it stems from a logic error, making it reliably triggerable. By following the steps in Diagram 5, an attacker can consistently trigger the use-after-free scenario in Code Block 6. That does not mean exploiting it in practice is straightforward; the exploitation steps are covered in the next part of the series.
Root cause
The ultimate cause is that Irp->CurrentStackLocation->FileObject->FsContext2 is not set to NULL when the calling process is a foreign process. The pointer therefore survives the first close, FSRendezvousServer::Close runs again on the IRP_MJ_CLOSE request, and the object is dereferenced more times than it has references.
To find how the bug was introduced, the author analysed previous versions of the driver from Winbindex and looked for differences in logic in FSRendezvousServer::Close. An early version of mskssrv.sys contained the exact line that was later missing:

Code Block 7: Early version of FSRendezvousServer::Close, FsContext2 is set to NULL

In that early code, FileObject->FsContext2 is explicitly set to NULL regardless of the result of the preceding process ID checks. This prevents FSRendezvousServer::Close from being called again in the subsequent IRP_MJ_CLOSE request, so the extra dereference cannot occur. A later version of the function, where the bug was first introduced, looks like this:

Code Block 8: FsContext set to NULL within a feature flag check

The later code checks the feature flag Feature_Servicing_TeamsUsingMediaFoundationCrashes. Feature flags are a Windows component that toggles functionality and experiments, and there is not much public information about them. If the feature is enabled, FileObject->FsContext2 is not set to NULL, introducing the vulnerability. The feature was observed to be enabled by default on Windows 10 installations. In Windows 11, as shown in Code Block 1, the feature flag conditional is not present and the pointer is not set to NULL, making it vulnerable as well.
Because of the feature's name, the author looked into Microsoft Teams, the video conferencing software, and confirmed that the application can use MSKSSRV functionality to share media streams across processes. It is possible that stream handle sharing was causing Teams to crash. How Teams shares MSKSSRV device handles across processes, and why proper pointer cleanup could cause the application to crash, is left as a topic for further research.
Impact
The vulnerability is a use-after-free in kernel mode, found while hunting a 0-day kernel vulnerability that can be exploited for privilege escalation. The author used it in an exploit attempt against a fully updated Windows 11 machine with Virtualization Based Security and hardware security mitigations enabled. The bug can be triggered reliably because it is a logic error, and the security update guide rates exploitation as more likely with attack complexity Low.
After the patch, the author suspected an Administrator to Kernel bug was still present, because the memory corruption problems were not addressed at all. This was confirmed by slightly modifying the original exploit: an administrator user can start the FrameServer service, open a handle to the service and create the exploit process using the handle. This yielded a full kernel R/W primitive on a fully patched system. Microsoft does not consider Administrator to Kernel to be a security boundary, but similar bugs have been used by threat actors to gain a kernel R/W primitive for EDR blinding and rootkit operations.
Fix
The logic error was patched in the June 2024 Security updates, but not addressed directly. Instead, an access token check was added before the vulnerable code paths. The initialize context IOCTL, handled by FSRendezvousServer::InitializeContext, now looks like this:

Code Block 9: IOCTL function begins with a feature flag check and checks if calling process is a frame server

The function begins by checking whether a feature is enabled, likely the feature flag corresponding to the patch. If the feature is enabled, KsIsCurrentProcessFrameServer must return TRUE, otherwise the NTSTATUS value STATUS_ACCESS_DENIED is returned. KsIsCurrentProcessFrameServer looks like this:

Code Block 10: KsIsCurrentProcessFrameServer performing a SID check on the calling thread's access token

This function checks the calling thread's token against two specific security identifiers (SIDs). The SIDs correspond to a token in group NT SERVICE\FrameServer. If either SID is enabled in the calling thread's access token, the vulnerable function code can execute.
A complete patch would require some base code restructuring and could reveal more about how the IPC system is intended to function. The author's proposed fix, setting the pointer to NULL for foreign processes, seemed to trigger crashes in Microsoft Teams, though at that point the author had not examined how a real application uses the MSKSSRV driver in practice.
Variants to test
- Other derived classes with a
noplock vtable entry. Look for classes derived from a base object where one subclass implements a locking function and another leaves the basenop. A positive result is a code path that accesses the unlocked object's reference count or list membership concurrently with cleanup. - Dispatch routines that check the caller's process ID. Enumerate dispatch routines that branch on the calling process ID and are reachable from both
IRP_MJ_CLEANUPandIRP_MJ_CLOSE. A positive result is a path where theIRP_MJ_CLOSEcontext is arbitrary, so the check can be bypassed by closing a duplicated handle from a foreign process. - Pointer fields not cleared on the foreign-process path. Search for cleanup functions that set a context pointer to
NULLonly inside a process-ID branch. A positive result is a second dispatch invocation that re-enters the cleanup function with a stale pointer. - Reference count versus dereference count. For each object with a known initial reference count, count the maximum number of release calls reachable from a single handle close and from two handles sharing the same context pointer. A positive result is a state where the dereference count exceeds the reference count before the object is unlinked from its validating list.
- Feature-flag-gated cleanup. Diff driver versions and look for cleanup lines moved inside a feature flag check. A positive result is a version where the flag being enabled removes a
NULLassignment or other safety step. - Access-token gating as a patch. When a patch adds a SID or service check instead of fixing the memory corruption, test whether a lower-privileged principal can obtain the required token (for example by starting the relevant service). A positive result is reaching the vulnerable code path from a context Microsoft does not treat as a security boundary.
What happened, in plain words
A security researcher named Valentina Palmiotti found a bug in a piece of Windows software called the Microsoft Kernel Streaming Service, whose file name is mskssrv.sys. This is a driver: a special program that runs deep inside Windows, closer to the hardware than normal apps, and that normal apps talk to when they need something done. The bug is tracked as CVE-2024-30089. A CVE is a public catalogue number that the security world gives to a known vulnerability so everyone can refer to the same thing.
The bug is a use-after-free. Imagine you borrow a library book, return it, and the librarian puts it back on the shelf and gives that slot to a new book. If you still have a note saying "my book is at shelf position 12" and you go back and read position 12, you are now reading a different book that you never borrowed. In software, a use-after-free means a program keeps a note (a pointer) to a chunk of memory, the memory gets handed back to the system (freed), and then the program uses the note anyway. Whoever controls the new contents of that memory can influence what the program does next. In the kernel, that can mean taking control of the whole machine.
The researcher used this bug in an exploit attempt against a fully updated Windows 11 machine with all the modern protections turned on, and it scored her first win at Pwn2Own, a well-known yearly contest where researchers demonstrate real attacks on fully patched devices.
The bug is not a crash-prone memory scribble. It is a logic error: the code follows a perfectly valid-looking path but makes a wrong decision in one specific situation. That situation involves two programs sharing something through the driver. Because the mistake is logical, it can be triggered again and again, reliably.
This article walks through how she found it, why it exists, and how Microsoft chose to patch it. It is written for someone who has never done security research before, so every term is explained as it appears.
Background: what the driver actually does
The driver is called MSKSSRV, short for MS KS Server, where KS stands for Kernel Streaming. Its job is to let programs share media streams, such as video frames, with each other. Think of it as a post office inside Windows: one program drops off a package (a stream of video frames), and another program picks it up.
The driver talks to normal programs (called userland programs, because they run in user mode, the safer part of the system) through two kinds of internal objects:
FSStreamReg, which represents a stream.FSContextReg, which represents a context.
Both are built on a common base called FSRegObject. An object here just means a structured bundle of data that the driver keeps in memory to represent something. The base class is like a template that the two specific types inherit from, the same way a "vehicle" template might be inherited by "car" and "truck".
When a program wants to talk to the driver, it opens a file handle to the driver's device using a Windows function called CreateFile. A handle is a small number the operating system gives you as a receipt: "you now have access to this thing, quote this number when you want to use it". Opening the handle creates a FILE_OBJECT, which is the kernel's internal record for that open file.
With that handle, the program can send the driver a command called an IOCTL (Input/Output Control). An IOCTL is a message with a number identifying the command and a buffer of data. Programs send IOCTLs using the DeviceIoControl function. Think of an IOCTL as filling in a form and handing it to a clerk: the form number says what you want, and the fields say the details.
Here is how the sharing works, step by step:
- Program A opens a handle to the driver and sends an IOCTL to initialise a new stream or context object. In the
lpInBufferargument (the buffer of data sent with the IOCTL), Program A says which other program is allowed to register the object, by giving its process ID. A process ID (PID) is the number Windows assigns to each running program so it can be told apart from the others. - Program B opens its own handle to the driver and sends an IOCTL to register the object that Program A created.
- The driver stores a pointer to the shared
FSRegObjectin a field calledIrp->CurrentStackLocation->FileObject->FsContext2. An IRP is an I/O Request Packet: the kernel's internal envelope for a request.FsContext2is a slot inside theFILE_OBJECTwhere the driver can stash a pointer for its own use.
The same pointer ends up stored twice: once in the FILE_OBJECT belonging to Program A's handle, and once in the FILE_OBJECT belonging to Program B's handle. That is how the two programs end up looking at the same object.
For an FSStreamReg object, this is exactly the point: multiple programs can read the same video frame buffer.
To know when to free the object, the driver uses a reference count. This is a counter that says "how many things are still using this object?". When the count reaches zero, the object is freed. The count starts at 1 when the object is created, is incremented again once initialisation finishes, and is incremented once more when the object is registered. That gives three references per object.

Diagram 1: Inter-process Communication in MS KS Server


Diagram 2: Initializing and Registering FSContextReg Objects

Setup
The research was done by reading the driver's code and mentally tracing the program's logic for each possible state of the sharing system. Later, to find how the bug was introduced, she compared versions downloaded from a site called Winbindex that archives old Windows binaries. She also mentally traced the program's logic for each possible state of the sharing system, like walking through a flowchart in her head.
The target machine was a fully updated Windows 11 system with Virtualization Based Security (VBS, a Windows feature that uses the CPU's virtualisation hardware to isolate sensitive parts of the system) and hardware security mitigations enabled. The report does not describe any other lab setup, accounts or tools.
Finding it
An unlocked reference count
The starting inspiration was a blog post by another researcher, k0shl, about a bug where an object's reference count was initialised and incremented without proper locking. Locking is how programs stop two threads (two streams of execution inside the same program) from stepping on each other. Imagine two people editing the same shared document at once: without a rule that says "only one person edits at a time", they can overwrite each other's changes. A mutex is one such rule, a lock that only one thread can hold at a time.
k0shl's bug was in userland, not the kernel, but the coding style reminded the researcher of the MSKSSRV driver she had audited before.
Looking at MSKSSRV, FSContextReg does not implement a locking function at all. The base class FSRegObject provides a placeholder that is simply a nop instruction, which means "do nothing". So no locking happens for FSContextReg objects. FSStreamReg, by contrast, does implement a locking function that uses a mutex. The lock is taken when the objects are accessed for cleanup, inside a function called FSRendezvousServer::Close:

Code Block 1: FSRendezvousServer::Close, locking and unlocking path for FSRegObjects

Two object types derive from FSRegObject, but only one implements proper locking. She tried to trigger undefined behaviour through references to the unprotected FSContextReg object, but could not, because the global FSRendezvousServer object (which holds the lists of all FSRegObjects) is itself protected by a lock. The reference counting system still looked suspicious.
Dispatch routines and process context
A dispatch routine is a function the driver registers so the kernel knows who to call when a particular kind of request arrives. It handles one or more types of IRPs, the packaged I/O requests mentioned earlier.
When all handle references to a file have been closed, the file system driver receives two requests in order: IRP_MJ_CLEANUP and IRP_MJ_CLOSE. These are handled by the driver's DispatchCleanup and DispatchClose routines. In MSKSSRV, a function called FSRendezvousClose (which calls FSRendezvousServer::Close) is invoked from both, as long as the pointer in Irp->CurrentStackLocation->FileObject->FsContext2 is not NULL. NULL means "points to nothing", the empty value.

Code Block 2: DispatchCleanup and DispatchClose routines for MS KS Server Driver

FSRendezvousServer::Close contains several checks on the caller's process ID. Why does the caller's identity matter? Because all kernel code runs in one shared kernel address space, separate from the user-mode address spaces of individual programs. Each program has its own user-mode memory. When a kernel thread accesses a user address, the kernel uses the process context (which program the thread is currently running on behalf of) to decide which program's memory to look at. So "which process is calling?" is a real question with real consequences.
The following code checks whether the calling process is the one that initialised the object or the one that registered it:

Code Block 3: FSRendezvousServer::Close, Process checks on FSRegObjects

Process-specific information is stored in the FSRegObject at initialisation or registration time. The driver uses the caller's process ID to decide which process-specific resources (EPROCESS objects, which are the kernel's records for processes, event objects, and other things) it needs to release. If the caller is the initialising or registering process, extra cleanup is done for those resources.
This stood out because dispatch routines generally run in an arbitrary process context, with some exceptions. The system picks a thread to do the dispatch work, and which thread it picks is arbitrary. DispatchCleanup is called in the context of the process that closed the final handle, so the process ID checks make sense there. But DispatchClose is called from an arbitrary process context. If FSRendezvousServer::Close runs because of an IRP_MJ_CLOSE request, it would be from an arbitrary process context, which defeats the purpose of the process ID checks.
Shared handles
Handles can be shared with other processes. Windows supports this in two ways: child process inheritance (when a program starts a child program, the child can inherit the parent's handles) and the DuplicateHandle API function. An API (Application Programming Interface) is a named function that one program can call to ask the operating system or another program to do something. DuplicateHandle makes a copy of a handle that another process can use.
Through the shared file handle, the other process can interact with the same FILE_OBJECT.

Diagram 3: Sharing MS KS Server Device Handle

The process context during DispatchCleanup can therefore be neither the initialising nor the registering process. If either handle is duplicated and shared to another process, and that other process is the last to close the handle, DispatchCleanup is called in the context of that foreign process (a process that is neither the initialiser nor the registrant).

Diagram 4: Foreign Process Closing Last Handle to a FILE_OBJECT

Counting dereferences
FSRendezvousServer::Close can be called twice when a file handle is closed: once for the IRP_MJ_CLEANUP request and once for the IRP_MJ_CLOSE request (Code Block 2). Inside that function there are two possible calls to FSRegObject::Release (Code Block 4). Release is the function that dereferences the object: it decrements the reference count, and if the count reaches zero, it frees the object's memory. So FSRegObject::Release could be called up to four times for a single handle.
There are also two file handles whose FILE_OBJECT points to the same FSRegObject through the FSContext2 pointer. That gives a possibility of calling FSRegObject::Release up to eight times on the same object, four for each handle. With an original reference count of only three, too many dereferences could trigger a use-after-free. The program structure would probably prevent hitting the theoretical maximum, but maybe not quite enough.

Code Block 4: FSRegObject::Release can be called twice from FSRendezvousServer::Close

More dereferences than references is not the only way a use-after-free can occur, but in this case, if an FSRegObject has been freed, its reference count has dropped to zero. The last time a valid FSRegObject is accessed during the IRP_MJ_CLEANUP/CLOSE requests is in a call to FSRegObject::Release. So if a use-after-free is possible, a call to FSRegObject::Release will always occur after the object has already been freed, and during that call the object is dereferenced once more. Counting dereferences is therefore a good rule of thumb for finding use-after-frees in this case.
The remaining work was to trace the possible states of the program, noting when the object is freed and accessed, by mentally emulating the program logic during IRP_MJ_CLEANUP/CLOSE requests, each beginning with the corresponding dispatch functions (Code Block 2), for each possible case.
The states are based on which process closes the final reference to a handle. Each entry represents the number of dereferences of the FSContextReg object that occur if the corresponding process closes the final handle. There is no functional difference between HANDLE #1 (the initialising handle) and HANDLE #2 (the registering handle), because the FileObject->FSContext2 field points to the same memory in both FILE_OBJECTs represented by the corresponding handles.

FSContextReg dereferences for each of the possible MSKSSRV IPC states

The last state results in four dereferences: two by the foreign process and two by the initialising (and also registering) process, while only three references exist initially, so a use-after-free is possible. The same exercise with FSStreamReg objects showed that, due to a memory leak bug in the code, it is not possible to ever free an FSStreamReg object after it has been registered. A memory leak is when memory is allocated but never released, so it stays occupied forever.
Exploitation
Step 1: Understand the cleanup path
If the process is the initialising or registering process, the appropriate cleanup happens and the pointer stored in Irp->CurrentStackLocation->FileObject->FsContext2 is set to NULL. The code block below shows where this occurs when the caller is the initialising process:

Code Block 5: FSRegObject::CloseInitProcess sets FSContext pointer to NULL

This means FSRendezvousClose will not be called again during completion of the IRP_MJ_CLOSE request in DispatchClose (SrvDispatchClose, Code Block 2). The pointer has been cleared, so the driver knows there is nothing left to clean up.
Step 2: Close through a foreign process
If the calling process is a foreign process, no cleanup occurs and FSRegObject::Release is called once near the end of the function. Since FileObject->FsContext2 is not NULL, FSRendezvousServer::Close is called again during the subsequent IRP_MJ_CLOSE request, and another call to FSRegObject::Release occurs. The pointer was never cleared, so the driver thinks there is still work to do.
Step 3: Close the second handle from the initialising process
If the second handle is closed by a process that both initialised and registered the FSContextReg object, the object cleans up all its stored process resources, making it empty. This causes FSRegObject::Release to be called twice within FSRendezvousServer::Close (Code Block 4). The extra dereference accounts for the extra initialising reference once the object is empty. The total is four dereferences on a single FSContextReg object, which indicates a use-after-free.

Diagram 5: CVE-2024-30089 depicted

Step 4: Respect the list validation
A foreign process cannot be the last one to close both handles, even though this would seemingly also lead to four dereferences. Before the object is destroyed and freed, it is unlinked from a list stored in the global FSRendezvousServer object. At the beginning of FSRendezvousServer::Close, the pointer in FSContext2 is checked to be a valid member of that list. The object is freed in the second call to FSRegObject::Release at the end of the function. During the fourth call to FSRendezvousServer::Close, the object has already been unlinked from the list, making it an invalid object, so it cannot be used. To trigger a use-after-free, it must occur after the object has been retrieved and validated in FSRendezvousServer::Close. The code snippet below shows the use-after-free primitive that the vulnerability can provide. A primitive here means a small, reliable building block that an attacker can use as part of a larger exploit.

Code Block 6: UAF primitive path

Attack complexity
The security update guide for this vulnerability gives a CVSS score indicating that exploitation is more likely and an attack complexity of Low. CVSS (Common Vulnerability Scoring System) is a standard way of scoring how serious a vulnerability is. Microsoft does not provide detailed explanations for its scoring, but the vulnerability likely received this score because it stems from a logic error, making it reliably triggerable. By following the steps in Diagram 5, an attacker can consistently trigger the use-after-free scenario in Code Block 6. That does not mean exploiting it in practice is straightforward; the exploitation steps are covered in the next part of the series.
Root cause
The ultimate cause is that Irp->CurrentStackLocation->FileObject->FsContext2 is not set to NULL when the calling process is a foreign process. The pointer therefore survives the first close, FSRendezvousServer::Close runs again on the IRP_MJ_CLOSE request, and the object is dereferenced more times than it has references.
To find how the bug was introduced, the author analysed previous versions of the driver from Winbindex and looked for differences in logic in FSRendezvousServer::Close. An early version of mskssrv.sys contained the exact line that was later missing:

Code Block 7: Early version of FSRendezvousServer::Close, FsContext2 is set to NULL

In that early code, FileObject->FsContext2 is explicitly set to NULL regardless of the result of the preceding process ID checks. This prevents FSRendezvousServer::Close from being called again in the subsequent IRP_MJ_CLOSE request, so the extra dereference cannot occur. A later version of the function, where the bug was first introduced, looks like this:

Code Block 8: FsContext set to NULL within a feature flag check

The later code checks the feature flag Feature_Servicing_TeamsUsingMediaFoundationCrashes. A feature flag is a switch inside Windows that turns a piece of functionality or an experiment on or off. There is not much public information about them. If the feature is enabled, FileObject->FsContext2 is not set to NULL, introducing the vulnerability. The feature was observed to be enabled by default on Windows 10 installations. In Windows 11, as shown in Code Block 1, the feature flag conditional is not present and the pointer is not set to NULL, making it vulnerable as well.
Because of the feature's name, the author looked into Microsoft Teams, the video conferencing software, and confirmed that the application can use MSKSSRV functionality to share media streams across processes. It is possible that stream handle sharing was causing Teams to crash. How Teams shares MSKSSRV device handles across processes, and why proper pointer cleanup could cause the application to crash, is left as a topic for further research.
Impact
The vulnerability is a use-after-free in kernel mode, found while hunting a 0-day kernel vulnerability that can be exploited for privilege escalation: going from a lower level of permission (like a normal user) to a higher one (like the kernel). The author used it to exploit a fully updated Windows 11 machine with Virtualization Based Security and hardware security mitigations enabled. The bug can be triggered reliably because it is a logic error, and the security update guide rates exploitation as more likely with attack complexity Low.
After the patch, the author suspected an Administrator to Kernel bug was still present, because the memory corruption problems were not addressed at all. Administrator to Kernel means going from an administrator account to full kernel control. Microsoft does not consider that transition a security boundary, because an administrator already has a lot of power. This was confirmed by slightly modifying the original exploit: an administrator user can start the FrameServer service, open a handle to the service and create the exploit process using the handle. This yielded a full kernel R/W primitive on a fully patched system. Kernel R/W means the ability to read and write arbitrary kernel memory, which is effectively total control of the machine. Similar bugs have been used by threat actors to gain a kernel R/W primitive for EDR blinding (disabling endpoint detection and response security software) and rootkit operations (installing software that hides itself deep in the system).
Fix
The logic error was patched in the June 2024 Security updates, but not addressed directly. Instead, an access token check was added before the vulnerable code paths. An access token is the kernel's record of who a process is and what it is allowed to do, like an ID badge. The initialise context IOCTL, handled by FSRendezvousServer::InitializeContext, now looks like this:

Code Block 9: IOCTL function begins with a feature flag check and checks if calling process is a frame server

The function begins by checking whether a feature is enabled, likely the feature flag corresponding to the patch. If the feature is enabled, KsIsCurrentProcessFrameServer must return TRUE, otherwise the NTSTATUS value STATUS_ACCESS_DENIED is returned. NTSTATUS is the standard way Windows kernel functions report success or failure. KsIsCurrentProcessFrameServer looks like this:

Code Block 10: KsIsCurrentProcessFrameServer performing a SID check on the calling thread's access token

This function checks the calling thread's token against two specific security identifiers (SIDs). A SID is a unique number that identifies a user, group or service in Windows. The SIDs correspond to a token in the group NT SERVICE\FrameServer. If either SID is enabled in the calling thread's access token, the vulnerable function code can execute.
A complete patch would require some base code restructuring and could reveal more about how the IPC system is intended to function. The author's proposed fix, setting the pointer to NULL for foreign processes, seemed to trigger crashes in Microsoft Teams, though at that point the author had not examined how a real application uses the MSKSSRV driver in practice.
Variants to test
These are patterns other researchers can look for in similar drivers:
- Other derived classes with a
noplock vtable entry. Look for classes derived from a base object where one subclass implements a locking function and another leaves the basenop. A positive result is a code path that accesses the unlocked object's reference count or list membership concurrently with cleanup. - Dispatch routines that check the caller's process ID. Enumerate dispatch routines that branch on the calling process ID and are reachable from both
IRP_MJ_CLEANUPandIRP_MJ_CLOSE. A positive result is a path where theIRP_MJ_CLOSEcontext is arbitrary, so the check can be bypassed by closing a duplicated handle from a foreign process. - Pointer fields not cleared on the foreign-process path. Search for cleanup functions that set a context pointer to
NULLonly inside a process-ID branch. A positive result is a second dispatch invocation that re-enters the cleanup function with a stale pointer. - Reference count versus dereference count. For each object with a known initial reference count, count the maximum number of release calls reachable from a single handle close and from two handles sharing the same context pointer. A positive result is a state where the dereference count exceeds the reference count before the object is unlinked from its validating list.
- Feature-flag-gated cleanup. Diff driver versions and look for cleanup lines moved inside a feature flag check. A positive result is a version where the flag being enabled removes a
NULLassignment or other safety step. - Access-token gating as a patch. When a patch adds a SID or service check instead of fixing the memory corruption, test whether a lower-privileged principal can obtain the required token (for example by starting the relevant service). A positive result is reaching the vulnerable code path from a context Microsoft does not treat as a security boundary.
Glossary
- Access token: the kernel's record of who a process is and what it is allowed to do, like an ID badge.
- API (Application Programming Interface): a named function one program can call to ask the operating system or another program to do something.
- Arbitrary process context: the situation where the kernel picks any thread to do work, so the work runs on behalf of whichever process that thread belongs to, not necessarily the one that made the request.
- Attack complexity: a CVSS measure of how hard a vulnerability is to exploit in practice.
- CVE: a public catalogue number given to a known vulnerability so everyone can refer to the same thing.
- CVSS (Common Vulnerability Scoring System): a standard way of scoring how serious a vulnerability is.
- Dereference: to use a pointer to access the memory it points to; in this report, also the act of decrementing a reference count.
- Dispatch routine: a function a driver registers so the kernel knows who to call when a particular kind of request arrives.
- Driver: a special program that runs deep inside the operating system, closer to the hardware than normal apps, and that normal apps talk to when they need something done.
- EDR (Endpoint Detection and Response): security software that watches a machine for signs of attack; blinding it means disabling it.
- EPROCESS: the kernel's internal record for a process.
- Feature flag: a switch inside Windows that turns a piece of functionality or an experiment on or off.
- FILE_OBJECT: the kernel's internal record for an open file or device handle.
- Foreign process: a process that is neither the one that initialised an object nor the one that registered it.
- FsContext2: a slot inside a
FILE_OBJECTwhere the driver can stash a pointer for its own use. - Handle: a small number the operating system gives you as a receipt for access to something, quoted whenever you want to use it.
- IOCTL (Input/Output Control): a message with a command number and a data buffer that a program sends to a driver.
- IPC (Inter-Process Communication): any mechanism that lets two programs exchange data or share objects.
- IRP (I/O Request Packet): the kernel's internal envelope for an I/O request.
- Kernel: the core of the operating system, with full access to memory and hardware.
- Kernel R/W primitive: the ability to read and write arbitrary kernel memory, effectively total control of the machine.
- Locking: a rule that stops two threads from stepping on each other when they access shared data.
- Logic error: a mistake in the decisions a program makes, rather than a crash-prone memory scribble.
- Memory leak: memory that is allocated but never released, so it stays occupied forever.
- Mutex: a lock that only one thread can hold at a time.
- nop: an instruction that does nothing.
- NTSTATUS: the standard way Windows kernel functions report success or failure.
- NULL: the empty value, meaning "points to nothing".
- Object: a structured bundle of data a program keeps in memory to represent something.
- Pointer: a note in memory saying where another piece of memory lives.
- Primitive: a small, reliable building block an attacker can use as part of a larger exploit.
- Privilege escalation: going from a lower level of permission to a higher one.
- Process context: which program a kernel thread is currently running on behalf of.
- Process ID (PID): the number Windows assigns to each running program so it can be told apart from the others.
- Pwn2Own: a yearly contest where researchers demonstrate real attacks on fully patched devices.
- Reference count: a counter saying how many things are still using an object; when it reaches zero, the object is freed.
- Rootkit: software that hides itself deep in the system to avoid detection.
- SID (Security Identifier): a unique number that identifies a user, group or service in Windows.
- Thread: a stream of execution inside a program.
- Use-after-free: a bug where a program keeps a pointer to memory that has already been freed and then uses it anyway.
- Userland / user mode: the safer part of the system where normal programs run, separate from the kernel.
- VBS (Virtualization Based Security): a Windows feature that uses the CPU's virtualisation hardware to isolate sensitive parts of the system.
- Vulnerability: a weakness in software that an attacker can exploit.
- Winbindex: a website that archives old Windows binaries so researchers can compare versions.