Critical Use-After-Free Vulnerability in Windows Remote Desktop Gateway Enables Remote Code Execution

In January 2025, Microsoft disclosed a critical security vulnerability in its Remote Desktop Gateway (RD Gateway) service, identified as CVE-2025-21297. This flaw allows remote attackers to execute arbitrary code on affected systems without requiring authentication, posing a significant threat to organizations relying on RD Gateway for secure remote access.

Understanding the Vulnerability

The vulnerability originates from a use-after-free (UAF) condition within the RD Gateway service. Specifically, the issue resides in the `aaedge.dll` library, particularly within the `CTsgMsgServer::GetCTsgMsgServerInstance` function. Here, a global pointer (`m_pMsgSvrInstance`) is initialized without proper thread synchronization. This oversight allows multiple threads to concurrently overwrite the same global pointer, leading to corrupted reference counts and the dereferencing of a dangling pointer—a classic UAF scenario.

Technical Breakdown

The exploitation process involves:

1. Concurrent Connections: An attacker initiates multiple socket connections to the RD Gateway service.

2. Triggering Race Condition: These concurrent connections exploit a timing issue where memory allocation and pointer assignment occur out of sync.

3. Overwriting Global Pointer: One connection overwrites the global pointer before another finishes referencing it, leading to the use of freed memory.

This sequence can result in arbitrary code execution, granting attackers control over the affected system.

Affected Systems

The vulnerability impacts multiple versions of Windows Server that utilize RD Gateway, including:

– Windows Server 2016 (Core and Standard installations)

– Windows Server 2019 (Core and Standard installations)

– Windows Server 2022 (Core and Standard installations)

– Windows Server 2025 (Core and Standard installations)

Organizations employing RD Gateway for remote access are particularly at risk.

Mitigation and Recommendations

Microsoft addressed this vulnerability in the May 2025 Patch Tuesday updates by introducing mutex-based synchronization, ensuring that only one thread can initialize the global instance at any given time. The following security updates are available:

– Windows Server 2016: Update KB5050011

– Windows Server 2019: Update KB5050008 (Build 10.0.17763.6775)

– Windows Server 2022: Update KB5049983 (Build 10.0.20348.3091)

– Windows Server 2025: Update KB5050009 (Build 10.0.26100.2894)

Organizations are strongly urged to apply these patches immediately to mitigate the risk of exploitation. Until patches can be applied, it is advisable to monitor RD Gateway logs for unusual activity and implement network-level protections to limit incoming connections to trusted sources.

Conclusion

The CVE-2025-21297 vulnerability in Windows Remote Desktop Gateway represents a critical risk to enterprise environments that rely on RD Gateway for secure remote access. By understanding the technical details and promptly applying the necessary patches, organizations can protect their systems from potential exploitation.