FreeRDP
Loading...
Searching...
No Matches
TestFuzzRdpUdpServer.c
1
17
18/* Fuzzes client input to the server's shared UDP port, before and after acceptance:
19 * - Entire input treated as a SYN for rdpudp_get_syn_cookie_hash() and rdpudp_accept().
20 * - Valid v3 SYN, followed by a sequence of operations defined by a 16-bit BE value:
21 * - < 0x8000: Length of the following datagram passed to rdpudp_receive().
22 * - 0xFFFF: Mark client as cookie-verified.
23 * - 0xFFFE: Trigger server to send N chunks (N = next byte), allowing the fuzzer to ack,
24 * report, or lose them.
25 * - Otherwise: Advance clock by the lower 15 bits (ms) and run timers.
26 * Timers also run after every eighth datagram.
27 *
28 * The server connection has no socket; all outgoing data is discarded. */
29
30#include <stdint.h>
31#include <string.h>
32
33#include <winpr/wtypes.h>
34#include <winpr/crt.h>
35#include <winpr/stream.h>
36#include <winpr/wlog.h>
37
38#include "../rdpudp.h"
39
40#define SYN_LENGTH 1232
41
42typedef struct
43{
44 BOOL ready;
45 wLog* log;
46 UINT64 now; /* Time in nanoseconds, advanced by the fuzzer */
47} FuzzCtx;
48
49static FuzzCtx g_ctx;
50
51static UINT64 fuzz_clock(WINPR_ATTR_UNUSED void* custom)
52{
53 return g_ctx.now;
54}
55
56static BOOL on_receive(WINPR_ATTR_UNUSED void* custom, WINPR_ATTR_UNUSED const BYTE* data,
57 WINPR_ATTR_UNUSED size_t length)
58{
59 return TRUE;
60}
61
62static BOOL on_send(WINPR_ATTR_UNUSED void* custom, WINPR_ATTR_UNUSED const BYTE* data,
63 WINPR_ATTR_UNUSED size_t length)
64{
65 return TRUE;
66}
67
68static BOOL fuzz_init(void)
69{
70 if (g_ctx.ready)
71 return TRUE;
72
73 WLog_SetLogLevel(WLog_GetRoot(), WLOG_OFF);
74 g_ctx.log = WLog_Get("com.freerdp.core.test.fuzz.rdpudp");
75 g_ctx.ready = TRUE;
76 return TRUE;
77}
78
79/* [MS-RDPEUDP] 2.2.2.1, 2.2.2.5, and 2.2.2.9: Create a version 3 SYN packet, zero-padded to the
80 * MTU. */
81static void make_syn(BYTE syn[SYN_LENGTH])
82{
83 memset(syn, 0, SYN_LENGTH);
84 wStream sbuffer = WINPR_C_ARRAY_INIT;
85 wStream* s = Stream_StaticInit(&sbuffer, syn, SYN_LENGTH);
86 Stream_Write_UINT32_BE(s, 0xFFFFFFFF); /* Source Acknowledgement Number */
87 Stream_Write_UINT16_BE(s, 64); /* Set the receive window size */
88 Stream_Write_UINT16_BE(s,
89 0x0001 | 0x1000); /* Set RDPUDP_FLAG_SYN and RDPUDP_FLAG_SYNEX flags */
90 Stream_Write_UINT32_BE(s, 1000); /* Initial sequence number */
91 Stream_Write_UINT16_BE(s, SYN_LENGTH); /* Upstream MTU */
92 Stream_Write_UINT16_BE(s, SYN_LENGTH); /* Downstream MTU */
93 Stream_Write_UINT16_BE(s, 0x0001); /* Mark version information as valid */
94 Stream_Write_UINT16_BE(s, RDPUDP_PROTOCOL_VERSION_3);
95 /* The cookieHash is set to zero. */
96}
97
98static void fuzz_syn(const uint8_t* data, size_t size)
99{
100 BYTE hash[32] = WINPR_C_ARRAY_INIT;
101 (void)rdpudp_get_syn_cookie_hash(data, size, hash);
102
103 rdpUdp* udp = rdpudp_new(g_ctx.log, nullptr, on_receive, nullptr);
104 if (!udp)
105 return;
106 (void)rdpudp_accept(udp, on_send, nullptr, data, size);
107 rdpudp_free(udp);
108}
109
110static void fuzz_datagrams(const uint8_t* data, size_t size)
111{
112 rdpUdp* udp = rdpudp_new(g_ctx.log, nullptr, on_receive, nullptr);
113 if (!udp)
114 return;
115
116 /* Use a constant start time for reproducible crashes */
117 g_ctx.now = 1000000000000ULL;
118 rdpudp_set_clock(udp, fuzz_clock, nullptr);
119
120 BYTE syn[SYN_LENGTH];
121 make_syn(syn);
122 if (!rdpudp_accept(udp, on_send, nullptr, syn, sizeof(syn)))
123 goto out;
124
125 size_t offset = 0;
126 size_t count = 0;
127 while (size - offset >= 2)
128 {
129 const size_t length = ((size_t)data[offset] << 8) | data[offset + 1];
130 offset += 2;
131 if (length == 0xFFFF)
132 {
133 rdpudp_set_peer_verified(udp);
134 continue;
135 }
136 if (length == 0xFFFE)
137 {
138 if (offset >= size)
139 break;
140 const size_t chunks = data[offset++];
141 static const BYTE chunk[100] = WINPR_C_ARRAY_INIT;
142 for (size_t x = 0; x < chunks; x++)
143 {
144 if (!rdpudp_send(udp, chunk, sizeof(chunk)))
145 break;
146 }
147 continue;
148 }
149 if (length >= 0x8000)
150 {
151 g_ctx.now += (length & 0x7FFF) * 1000000ULL;
152 if (!rdpudp_check(udp))
153 break;
154 continue;
155 }
156 if (length > size - offset)
157 break;
158 /* rdpudp_receive() modifies the datagram buffer in place */
159 BYTE datagram[0x8000];
160 memcpy(datagram, &data[offset], length);
161 if (!rdpudp_receive(udp, datagram, length))
162 break;
163 offset += length;
164 if ((++count % 8) == 0)
165 {
166 if (!rdpudp_check(udp))
167 break;
168 }
169 }
170
171out:
172 rdpudp_free(udp);
173}
174
175int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);
176int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
177{
178 if (!fuzz_init())
179 return 0;
180 fuzz_syn(Data, Size);
181 fuzz_datagrams(Data, Size);
182 return 0;
183}