FreeRDP
Loading...
Searching...
No Matches
TestFuzzRdpUdpTunnel.c
1
17
18/* The first byte determines the input target:
19 * - Tunnel PDUs: TLS is bypassed to allow fuzzing. Flag 0x20 selects emt_process_pdus()
20 * instead of emt_process_server_pdus().
21 * - Listener: Datagrams from up to 3 clients, each prefixed with a client ID and a 16-bit BE
22 * length. A SYN can claim a flow.
23 * - Soft-Sync Response: Tunnel data provided before the response. Flag 0x10 selects the
24 * state (sent vs answered) and flag 0x20 indicates if the client named the tunnel. */
25
26/* Required by udpsocket.c before system headers */
27#if defined(__linux__) && !defined(_GNU_SOURCE)
28#define _GNU_SOURCE // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
29#endif
30#if defined(__APPLE__) && !defined(__APPLE_USE_RFC_3542)
31#define __APPLE_USE_RFC_3542 // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
32#endif
33
34#include <stdint.h>
35#include <string.h>
36
37#include <winpr/wtypes.h>
38#include <winpr/crt.h>
39#include <winpr/stream.h>
40#include <winpr/winsock.h>
41#include <winpr/wlog.h>
42
43/* Include source files directly to allow the fuzzer to target the tunnel's static functions. */
44// NOLINTNEXTLINE(bugprone-suspicious-include)
45#include "../rdpemt.c"
46#undef TAG
47// NOLINTNEXTLINE(bugprone-suspicious-include)
48#include "../udpsocket.c"
49#undef TAG
50// NOLINTNEXTLINE(bugprone-suspicious-include)
51#include "../udpflow.c"
52#undef TAG
53// NOLINTNEXTLINE(bugprone-suspicious-include)
54#include "../udplistener.c"
55#undef TAG
56// NOLINTNEXTLINE(bugprone-suspicious-include)
57#include "../udplistener_relay.c"
58#undef TAG
59// NOLINTNEXTLINE(bugprone-suspicious-include)
60#include "../server_tunnel.c"
61
62static wLog* g_log;
63static SSL_CTX* g_ctx;
64static rdpUdp* g_udp;
65
66static BOOL on_send(WINPR_ATTR_UNUSED void* custom, WINPR_ATTR_UNUSED const BYTE* data,
67 WINPR_ATTR_UNUSED size_t length)
68{
69 return TRUE;
70}
71
72static BOOL on_receive(WINPR_ATTR_UNUSED void* custom, WINPR_ATTR_UNUSED const BYTE* data,
73 WINPR_ATTR_UNUSED size_t length)
74{
75 return TRUE;
76}
77
78static void on_event(WINPR_ATTR_UNUSED void* custom, WINPR_ATTR_UNUSED rdpEmt* emt,
79 WINPR_ATTR_UNUSED RDPEMT_EVENT event, const BYTE* data, size_t length)
80{
81 /* Read every byte to simulate a real receiver and prevent optimization */
82 volatile BYTE sum = 0;
83 for (size_t x = 0; x < length; x++)
84 sum ^= data[x];
85}
86
87static BOOL on_read_pdu(WINPR_ATTR_UNUSED rdpPeerChannel* channel, wStream* s,
88 WINPR_ATTR_UNUSED UINT8 Cmd, WINPR_ATTR_UNUSED UINT8 Sp,
89 WINPR_ATTR_UNUSED UINT8 cbChId, WINPR_ATTR_UNUSED BOOL fromTunnel)
90{
91 return Stream_GetRemainingLength(s) < 1000;
92}
93
94/* [MS-RDPEUDP] 2.2.2.1, 2.2.2.5, and 2.2.2.9: Version 3 SYN packet, zero-padded to the MTU. */
95static void make_syn(BYTE syn[1232])
96{
97 memset(syn, 0, 1232);
98 wStream sbuffer = WINPR_C_ARRAY_INIT;
99 wStream* s = Stream_StaticInit(&sbuffer, syn, 1232);
100 Stream_Write_UINT32_BE(s, 0xFFFFFFFF); /* Source acknowledgment number (snSourceAck) */
101 Stream_Write_UINT16_BE(s, 64); /* Receive window size */
102 Stream_Write_UINT16_BE(s,
103 0x0001 | 0x1000); /* Set RDPUDP_FLAG_SYN and RDPUDP_FLAG_SYNEX flags */
104 Stream_Write_UINT32_BE(s, 1000); /* Initial sequence number (snInitialSequenceNumber) */
105 Stream_Write_UINT16_BE(s, 1232); /* Upstream Maximum Transmission Unit */
106 Stream_Write_UINT16_BE(s, 1232); /* Downstream MTU */
107 Stream_Write_UINT16_BE(s, 0x0001); /* Set the RDPUDP_VERSION_INFO_VALID flag */
108 Stream_Write_UINT16_BE(s, RDPUDP_PROTOCOL_VERSION_3);
109}
110
111static BOOL fuzz_init(void)
112{
113 if (g_log)
114 return TRUE;
115
116 WLog_SetLogLevel(WLog_GetRoot(), WLOG_OFF);
117 g_ctx = SSL_CTX_new(TLS_server_method());
118 g_udp = rdpudp_new(WLog_Get("com.freerdp.core.test.fuzz.rdpemt"), nullptr, on_receive, nullptr);
119 if (!g_ctx || !g_udp)
120 return FALSE;
121
122 BYTE syn[1232];
123 make_syn(syn);
124 if (!rdpudp_accept(g_udp, on_send, nullptr, syn, sizeof(syn)))
125 return FALSE;
126 g_log = WLog_Get("com.freerdp.core.test.fuzz.rdpemt");
127 return TRUE;
128}
129
130static void fuzz_tunnel(BYTE flags, const uint8_t* data, size_t size)
131{
132 rdpEmt emt = WINPR_C_ARRAY_INIT;
133 emt.log = g_log;
134 emt.server = TRUE;
135 emt.requestId = 0x01020304;
136 emt.callback = on_event;
137 emt.udp = g_udp;
138 emt.ready = (flags & 0x10) != 0;
139 emt.tlsStarted = TRUE;
140 emt.handshakeDone = TRUE;
141 emt.sendLimit = 1024ull * 1024ull;
142 emt.ssl = SSL_new(g_ctx);
143 emt.rbio = BIO_new(BIO_s_mem());
144 emt.wbio = BIO_new(BIO_s_mem());
145 emt.in = Stream_New(nullptr, size + 1);
146 emt.out = Stream_New(nullptr, 4096);
147 emt.sendQueue = Queue_New(FALSE, -1, -1);
148 if (!emt.ssl || !emt.rbio || !emt.wbio || !emt.in || !emt.out || !emt.sendQueue ||
149 !InitializeCriticalSectionAndSpinCount(&emt.lock, 4000))
150 return;
151 SSL_set_bio(emt.ssl, emt.rbio, emt.wbio);
152 SSL_set_accept_state(emt.ssl);
153
154 /* Route input to the PDU parser (generic or server-side) for untrusted client data. A zeroed
155 * cookie allows Tunnel Create Requests to be valid. */
156 Stream_Write(emt.in, data, size);
157 if (flags & 0x20)
158 (void)emt_process_pdus(&emt);
159 else
160 (void)emt_process_server_pdus(&emt);
161
162 wStream* s = nullptr;
163 while ((s = Queue_Dequeue(emt.sendQueue)))
164 Stream_Free(s, TRUE);
165 Queue_Free(emt.sendQueue);
166 SSL_free(emt.ssl);
167 Stream_Free(emt.in, TRUE);
168 Stream_Free(emt.out, TRUE);
169 Stream_Free(emt.batch, TRUE);
170 DeleteCriticalSection(&emt.lock);
171}
172
173static void fuzz_listener(const uint8_t* data, size_t size)
174{
175 static const BYTE hash[32] = WINPR_C_ARRAY_INIT;
176 static const BYTE other[32] = { 1 };
177
178 freerdp_udp_listener* listener = freerdp_udp_listener_new();
179 rdpUdpFlow* flow = freerdp_udp_flow_new(hash);
180 rdpUdpFlow* second = freerdp_udp_flow_new(other);
181 if (!listener || !flow || !second || !freerdp_udp_listener_add_flow(listener, flow) ||
182 !freerdp_udp_listener_add_flow(listener, second))
183 goto out;
184
185 const udpListenerSocket sock = { .sockfd = INVALID_SOCKET };
186 size_t offset = 0;
187 while (size - offset >= 3)
188 {
189 struct sockaddr_storage addr = WINPR_C_ARRAY_INIT;
190 struct sockaddr_in* in = (struct sockaddr_in*)&addr;
191 in->sin_family = AF_INET;
192 in->sin_port = (u_short)(data[offset] % 3);
193 const udpLocalAddress local = WINPR_C_ARRAY_INIT;
194 const size_t length = ((size_t)data[offset + 1] << 8) | data[offset + 2];
195 offset += 3;
196 if (length > size - offset)
197 break;
198 udplistener_dispatch(listener, &sock, &addr, sizeof(*in), &local, &data[offset], length);
199 offset += length;
200
201 BYTE datagram[UDPFLOW_MAX_DATAGRAM];
202 size_t got = 0;
203 while (udpflow_read(flow, datagram, &got) || udpflow_read(second, datagram, &got))
204 {
205 }
206 }
207
208out:
209 freerdp_udp_flow_free(flow);
210 freerdp_udp_flow_free(second);
211 /* Removing flows only releases the listener after freerdp_udp_listener_free() has marked it
212 * as freed. */
213 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): see above
214 freerdp_udp_listener_free(listener);
215}
216
217static void fuzz_soft_sync(BYTE flags, const uint8_t* data, size_t size)
218{
219 struct s_wts_tunnel tunnel = WINPR_C_ARRAY_INIT;
220 rdpPeerChannel channel = WINPR_C_ARRAY_INIT;
221 WTSVirtualChannelManager vcm = WINPR_C_ARRAY_INIT;
222 vcm.tunnel = &tunnel;
223 vcm.drdynvc_channel = &channel;
224 vcm.drdynvc_state = DRDYNVC_STATE_READY;
225 tunnel.readPdu = on_read_pdu;
226 tunnel.softSyncState = (flags & 0x10) ? SOFT_SYNC_STATE_SENT : SOFT_SYNC_STATE_ANSWERED;
227 tunnel.clientOnTunnel = (flags & 0x20) != 0;
228 tunnel.tunnelBacklog = Queue_New(FALSE, -1, -1);
229 /* Soft-Sync state changes are protected by the table lock, as on a real server */
230 vcm.dynamicVirtualChannels = HashTable_New(TRUE);
231 if (!tunnel.tunnelBacklog || !vcm.dynamicVirtualChannels)
232 {
233 HashTable_Free(vcm.dynamicVirtualChannels);
234 Queue_Free(tunnel.tunnelBacklog);
235 return;
236 }
237
238 /* First half is tunnel data, second half is the Soft-Sync Response */
239 const size_t half = size / 2;
240 wStream* early = Stream_New(nullptr, half + 1);
241 if (early)
242 {
243 Stream_Write(early, data, half);
244 Stream_SealLength(early);
245 Stream_ResetPosition(early);
246 (void)wts_tunnel_data_received(&vcm, TUNNELTYPE_UDPFECR, early);
247 }
248
249 wStream sbuffer = WINPR_C_ARRAY_INIT;
250 wStream* s = Stream_StaticConstInit(&sbuffer, &data[half], size - half);
251 if (s)
252 (void)wts_tunnel_read_soft_sync_response(&vcm, s);
253
254 wStream* held = nullptr;
255 while ((held = Queue_Dequeue(tunnel.tunnelBacklog)))
256 Stream_Release(held);
257 Queue_Free(tunnel.tunnelBacklog);
258 HashTable_Free(vcm.dynamicVirtualChannels);
259}
260
261int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);
262int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
263{
264 if ((Size < 1) || !fuzz_init())
265 return 0;
266
267 switch (Data[0] & 0x03)
268 {
269 case 0:
270 fuzz_tunnel(Data[0], &Data[1], Size - 1);
271 break;
272 case 1:
273 fuzz_listener(&Data[1], Size - 1);
274 break;
275 default:
276 fuzz_soft_sync(Data[0], &Data[1], Size - 1);
277 break;
278 }
279 return 0;
280}