There is one handle. Every new clipboard response throws it away and allocates a new one. Nothing frees the old block. That is the whole bug.

I sent 400 unsolicited FormatDataResponse messages to an unmodified RustDesk 1.5.3 client on Windows 11. Its private bytes went from 196 MiB to 594 MiB and stayed there after the flood stopped. 400 messages, 1 MiB each, close to 400 MiB of memory that will not come back until the process exits. No paste, no click, no user interaction. Just a connected peer talking.

If you only came for the bug, you can stop reading here. The rest is the story around it: a Rust project with a C basement, an hour I lost to an exploit so broken that it protected its target, a mail server that rejected my security report as a security issue, and a fix that reached the repository without anyone telling me.

RustDesk and I also seem to be developing a tradition. Something is missing, and I write a post with "missing" in the title. Last time it was an if on Android. This time it is a GlobalFree on Windows.

A Rust project with a C basement

RustDesk is a remote desktop tool written in Rust, and most of the time that shows. Copying files through the clipboard on Windows, however, is an old and very Win32 problem, and RustDesk solves it the way many projects do: with C. The code lives in libs/clipboard/src/windows/wf_cliprdr.c. The header says where it comes from, FreeRDP's Windows clipboard redirection, with copyright lines from 2012 to 2015. At the checkout I tested it is 4,370 lines long, and its very first line is a comment:

// to-do: TOO MANY compilation warnings. Fix them. 

I have rarely felt so welcome in a file.

Rust keeps its promises. It just never promised anything about the C file next door. The borrow checker stops at the FFI boundary. Behind it, a handle is a number, ownership is a convention, and "freed" means that somebody remembered.

What that handle is for

The clipboard context keeps a single HGLOBAL called clipboard->hmem. It holds the data for a pending paste.

The intended lifetime is short. cliprdr_send_data_request() (lines 2039 to 2067) resets formatDataRespEvent, sends a FormatDataRequest to the peer, and blocks in wait_response_event() until the answer arrives. The response callback fills hmem, calls SetEvent, the waiter wakes up, and SetClipboardData takes ownership of the handle. Allocate, signal, hand off, set to NULL.

That only works if responses are paired with requests. Remember that sentence. Everything below is a consequence of it.

The three places that matter

The callback that handles a response is wf_cliprdr_server_format_data_response(). Three spots in it carry the whole bug:

// libs/clipboard/src/windows/wf_cliprdr.c:3628-3629 clipboard->hmem = NULL; clipboard->hmem_data_len = 0; // :3646 hMem = GlobalAlloc(GMEM_MOVEABLE, formatDataResponse->dataLen); // :3670-3671 clipboard->hmem_data_len = formatDataResponse->dataLen; clipboard->hmem = hMem; 

Line 3628 runs near the top of the function, before the msgFlags check and before any free. It does not call GlobalFree. After those two assignments there is no pointer left to the previous allocation. GMEM_MOVEABLE memory is not garbage collected, and Windows does not get sentimental about it either: the block stays committed until somebody calls GlobalFree or the process exits.

The early exits are slightly worse. If msgFlags is not CB_RESPONSE_OK, the callback returns E_FAIL at line 3635 after already having NULLed the handle. Even a rejected response drops the previous block.

Why nothing else frees it

My first reaction was that I had to be wrong. A handle that is set to NULL without a free usually means somebody else owns it, so I went looking for that somebody.

There are four places that free hmem: lines 947 to 950, 2168 to 2172, 2205 to 2217 and 2309 to 2321. All four sit in paste or clipboard-update paths. They need a WM_RENDERFORMAT, a WM_CLIPBOARDUPDATE, or a failed data object. None of them runs for a response that nobody asked for.

cliprdr_send_data_request() is the only code that resets formatDataRespEvent and waits on it. With no preceding FormatDataRequest there is no waiter, so the SetEvent at line 3677 signals nobody. On the Rust side, server_format_data_response() in libs/clipboard/src/platform/windows.rs:890-905 passes format_data.len() as dataLen, the callback returns, and the Vec<u8> is dropped. Rust cleans up its copy like a good citizen. The C copy is nobody's job.

Why the client accepts messages nobody asked for

So the leak needs a response without a request. The next question was whether a peer can actually deliver one, or whether something in front of the C code filters it out. Something does stand in front of it:

// src/client/io_loop.rs:2823-2825 let is_stopping_allowed = clip.is_beginning_message(); let file_transfer_enabled = self.handler.is_file_clipboard_required(); let stop = is_stopping_allowed && !file_transfer_enabled; // libs/clipboard/src/lib.rs:163-168 pub fn is_beginning_message(&self) -> bool { matches!( self, ClipboardFile::MonitorReady | ClipboardFile::FormatList { .. } ) } 

Only MonitorReady and FormatList are beginning messages. FormatDataResponse is not, so stop is false on every response. Turning off file clipboard does not help either. The gate is about starting a clipboard conversation, not about validating a response. It checks who may say hello. It does not check who may answer a question that was never asked.

From there the path is direct: ContextSend::make_sure_enabled(), then server_clip_file(), then server_format_data_response(). src/clipboard_file.rs:199-202 decodes the message into ClipboardFile::FormatDataResponse with no pairing check. Nothing along that path asks whether a FormatDataRequest is outstanding.

The wire message

This is all a peer has to send:

Message.cliprdr = 20 Cliprdr.format_data_response = 5 int32 msg_flags = 2 -> 1 (CB_RESPONSE_OK, wf_cliprdr.c:262) bytes format_data = 3 -> 1 MiB of attacker bytes 

dataLen is a UINT32 taken from the peer. The only bound is the frame limit 0x3FFF_FFFF in libs/hbb_common/src/bytes_codec.rs:8, so one response can carry close to 1 GiB. The leak grows linearly with the number of responses times that size.

An exploit so broken it protected the target

One mistake cost me a full run, and it is my favourite part of this story. msg_flags is an int32, so it has to be wire type 0, a varint. I encoded it as wire type 2 on my first attempt. The protobuf parser ignored the field, msgFlags read as 0, and the C callback exited with E_FAIL before it allocated anything.

Memory stayed flat. I spent an hour assuming my injection had failed and looking for the reason in all the wrong places. It had not failed. The injection worked perfectly. My message was just invalid in the one way that made the bug invisible. I had written a denial of service against my own proof of concept.

The lesson is boring and I will forget it again: when the exploit does nothing, check the exploit before you doubt the bug.

Three levels of proof

I did this at three levels, because a code reading is not a vulnerability report.

1. The verbatim callback on Linux. wf_cliprdr_server_format_data_response() copied from lines 3605 to 3685, with the Win32 calls replaced by counting shims. 1000 responses of 64 KiB: 1000 live handles, 0 frees, 65,536,000 bytes retained. Add GlobalFree(clipboard->hmem) before line 3628: 1 live handle, 999 frees, 65,536 bytes. That pins the mechanism, but a shim is whatever I say it is.

2. Real kernel32!GlobalAlloc on Windows 11. A C# port using P/Invoke for GlobalAlloc, GlobalLock, GlobalUnlock, GlobalFree and SetEvent. 512 responses of 1 MiB: 512 live handles, 0 frees, 540,295,168 bytes of growth. The control build: 1 live handle, 511 frees, 1,060,864 bytes. Now it is the real allocator, but still my program calling it.

3. The unmodified application. A self-hosted hbbs/hbbr pair, a controlled peer on Linux, and stock RustDesk 1.5.3 portable as the victim. 400 responses of 1 MiB into a live session, 235 samples of PrivateMemorySize64:

first 205,946,880 (196.4 MiB) last 622,985,216 (594.1 MiB) growth 417,038,336 (397.7 MiB) 

The last 20 samples sit between 591.3 and 594.1 MiB. No reclamation, no GC, no drop after the flood ends. 400 MiB went in, 398 MiB stayed.

Build live handles frees growth
Stock, 512 x 1 MiB 512 0 540,295,168 bytes
Control, 512 x 1 MiB 1 511 1,060,864 bytes

I injected without patching the client. On the controlled peer, ipc::Data::RawMessage at src/server/connection.rs:914-916 goes to conn.stream.send_raw(), and libs/hbb_common/src/tcp.rs:193-200 encrypts those plaintext bytes with the session key before they reach the wire. That is what a connected peer does. No man in the middle, no stolen keys, no rebuilt client.

Sunday, half past four

By the time the numbers were in a CSV file it was half past four on a Sunday morning, which is when all well-rested decisions are made. I wrote the report, attached a ZIP with the write-up, the three reproductions and the evidence, and sent it to info@rustdesk.com, the address in the project's security policy.

It bounced. The mail gateway rejected the message with 552 5.7.0 and the reason "potential security issue".

I would like to point out that this was technically correct. I have never been classified so accurately by a mail server. The likely culprit was the archive: it contained script files, and gateways do not like those inside a ZIP.

Eighteen minutes later I sent the report again, this time with a link instead of an attachment. Six minutes after that I had an answer: thanks, we will look into it. On a Sunday, at five in the morning my time, which says something about time zones and something good about the RustDesk team. Two days later I asked whether they had been able to reproduce it, and the answer was that they were working on it.

The fix I asked for

My report proposed one small change:

if (clipboard->hmem) { GlobalFree(clipboard->hmem); clipboard->hmem = NULL; clipboard->hmem_data_len = 0; } 

Put that before line 3628. It closes the leak, and the control build above shows exactly that behaviour.

I also suggested two more changes.

  1. Ignore FormatDataResponse when no FormatDataRequest is outstanding. The leak is the symptom. The broken assumption is that responses are paired with requests, and the transport never enforces it.
  2. Cap format_data.len() before it reaches GlobalAlloc. dataLen is attacker-controlled and only bounded by the frame limit.

The first change closes the trigger. The GlobalFree change closes the leak even if an unsolicited response gets through anyway.

The fix that shipped

On November 19 a pull request titled "Fix/windows clipboard response leak" was merged into RustDesk's master branch: #17113, commit dcb6291. Nobody mailed me. I found it on November 25 in the commit history of the file, which is fitting for a bug about responses that arrive while nobody is waiting for them.

The maintainers did not take my one-liner. They took the first of my two extra suggestions and built it properly. The callback now accepts a response only if a request is pending and the connection matches:

AcquireSRWLockExclusive(&clipboard->format_data_lock); if (!clipboard->format_data_pending || clipboard->format_data_conn_id != formatDataResponse->connID) { ReleaseSRWLockExclusive(&clipboard->format_data_lock); return ERROR_INVALID_DATA; } clipboard->format_data_pending = FALSE; 

cliprdr_send_data_request() sets format_data_pending under the same lock before it sends. It refuses to send while a request is pending or hmem is still held, and it frees hmem if the wait fails. That is the explicit pending state the protocol was missing.

With it, the two assignments that used to drop the handle can only run for a response somebody asked for, at a moment when there is no old handle left to drop. As far as I can read the new code, my GlobalFree line is no longer needed to stop this leak. I would still add it, for the same reason people wear a belt with suspenders, but that is taste and not a finding.

The commit message reports a test with 64 unsolicited responses of 1 MiB each, all rejected, with no measured growth in private memory. The change does not cap format_data.len(). With the pairing check that matters much less, because a peer can no longer decide how many responses get stored.

The pull request also says how the maintainers see the issue. A stock client never sends an unsolicited response, so this needs a modified client on the other end, and the change is closer to hardening than to a fix. They are right about the first half. A stock RustDesk does not do this. But "the other side runs a modified client" is also a fairly complete description of an attacker, and the peer is the one party in this protocol that I do not get to choose. I rated it Medium in the report and I still would. We agree on the patch, which is the part that counts.

I have not re-run my reproduction against a build with this commit yet. Every number in this post is from 1.5.3.

Not the earlier clipboard bug

An earlier Windows clipboard advisory this year was about a peer-supplied length copied into a fixed-size buffer. Different failure class. Here the allocation succeeds and the copy succeeds. The only thing that goes wrong is that nobody keeps the handle. It is a lifetime bug, not a bounds bug.

What I took away

  1. x = NULL is only a free if somebody else still holds the pointer. Grep for the free, not for the assignment.
  2. A request/response protocol needs an explicit pending state. This code had an event and a formatDataRespReceived flag, but nothing consulted them before storing a response. The fix added exactly that state.
  3. An early return can leak too. Line 3628 ran before the msgFlags check, so a rejected response dropped the previous block as well.
  4. Simulating the transport is fine, but say so, then run it end to end anyway. The Linux harness and the C# port pinned the mechanism. The live session is what turned it into a report.
  5. When the exploit does nothing, check the exploit before you doubt the bug. A wrong wire type cost me an hour.
  6. Read the commit log now and then. The answer to your report may already be in it.

Disclosure status

Reported privately on November 15, 2026 to info@rustdesk.com, the address in the project's security policy. The fix was merged into master on November 19 as #17113. As of November 25 the latest release is still 1.5.3 from November 9, and it does not contain the fix. Until a release with it is out, a connected peer can still do this to a 1.5.3 client on Windows. Do not run this reproduction against systems you do not own.

Tested on RustDesk 1.5.3 (checkout 3ccb4c4b5), Windows 11 Enterprise LTSC Evaluation build 26100.1742, Ubuntu 26.04.1 LTS, self-hosted rustdesk-server-s6. CWE-401, Medium.