Line data Source code
1 : /* fuzz_quic_wire is a simple and stateless fuzz target for fd_quic.
2 :
3 : The attack surface consists of fd_quic's packet handlers.
4 : The input vectors are the raw contents of UDP datagrams (in encrypted
5 : form) A custom mutator is used to temporarily remove the decryption
6 : before calling the generic libFuzzer mutator. If we tried mutating
7 : the encrypted inputs directly, everything would just be an encryption
8 : failure.
9 :
10 : The goal of fuzz_quic_wire is to cover the early upstream stages of
11 : the QUIC packet processing pipeline. This includes packet header
12 : parsing, connection creation, retry handling, etc. */
13 :
14 : #include "../../../util/sanitize/fd_fuzz.h"
15 : #include "fd_quic_test_helpers.h"
16 : #include "../crypto/fd_quic_crypto_suites.h"
17 : #include "../templ/fd_quic_parse_util.h"
18 : #include "../../tls/test_tls_helper.h"
19 : #include "../../../util/net/fd_ip4.h"
20 : #include "../../../util/net/fd_udp.h"
21 : #include "../fd_quic_proto.h"
22 : #include "../fd_quic_proto.c"
23 : #include "../fd_quic_private.h"
24 :
25 : #include <assert.h>
26 : #include <stdlib.h> /* putenv, atexit */
27 :
28 : static FD_TL ulong g_clock;
29 :
30 : static ulong
31 6699 : test_clock( void * context FD_FN_UNUSED ) {
32 6699 : return g_clock;
33 6699 : }
34 :
35 : int
36 : LLVMFuzzerInitialize( int * pargc,
37 15 : char *** pargv ) {
38 15 : putenv( "FD_LOG_BACKTRACE=0" );
39 15 : fd_boot( pargc, pargv );
40 15 : atexit( fd_halt );
41 15 : fd_log_level_logfile_set(0);
42 15 : fd_log_level_stderr_set(0);
43 15 : # ifndef FD_DEBUG_MODE
44 15 : fd_log_level_core_set(3); /* crash on warning log */
45 15 : # endif
46 15 : return 0;
47 15 : }
48 :
49 : static int
50 : _aio_send( void * ctx,
51 : fd_aio_pkt_info_t const * batch,
52 : ulong batch_cnt,
53 : ulong * opt_batch_idx,
54 150 : int flush ) {
55 150 : (void)flush;
56 150 : (void)batch;
57 150 : (void)batch_cnt;
58 150 : (void)opt_batch_idx;
59 150 : (void)ctx;
60 150 : return 0;
61 150 : }
62 :
63 : static void
64 : send_udp_packet( fd_quic_t * quic,
65 : uchar const * data,
66 2088 : ulong size ) {
67 :
68 2088 : uchar buf[16384];
69 :
70 2088 : ulong headers_sz = sizeof(fd_ip4_hdr_t) + sizeof(fd_udp_hdr_t);
71 :
72 2088 : uchar * cur = buf;
73 2088 : uchar * end = buf + sizeof(buf);
74 :
75 2088 : fd_ip4_hdr_t ip4 = {
76 2088 : .verihl = FD_IP4_VERIHL(4,5),
77 2088 : .protocol = FD_IP4_HDR_PROTOCOL_UDP,
78 2088 : .net_tot_len = (ushort)( sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)+size ),
79 2088 : };
80 2088 : fd_udp_hdr_t udp = {
81 2088 : .net_sport = 8000,
82 2088 : .net_dport = 8001,
83 2088 : .net_len = (ushort)( sizeof(fd_udp_hdr_t)+size ),
84 2088 : .check = 0
85 2088 : };
86 :
87 : /* Guaranteed to not overflow */
88 2088 : fd_quic_encode_ip4( cur, (ulong)( end-cur ), &ip4 ); cur += sizeof(fd_ip4_hdr_t);
89 2088 : fd_quic_encode_udp( cur, (ulong)( end-cur ), &udp ); cur += sizeof(fd_udp_hdr_t);
90 :
91 2088 : if( cur + size > end ) return;
92 2088 : fd_memcpy( cur, data, size );
93 :
94 : /* Main fuzz entrypoint */
95 :
96 2088 : fd_quic_process_packet( quic, buf, headers_sz + size );
97 2088 : }
98 :
99 : int
100 : LLVMFuzzerTestOneInput( uchar const * data,
101 2088 : ulong size ) {
102 :
103 2088 : fd_rng_t _rng[1]; fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, 0U, 0UL ) );
104 :
105 : /* Memory region to hold the QUIC instance */
106 2088 : static uchar quic_mem[ 1<<23 ] __attribute__((aligned(FD_QUIC_ALIGN)));
107 :
108 : /* Create ultra low limits for QUIC instance for maximum performance */
109 2088 : fd_quic_limits_t const quic_limits = {
110 2088 : .conn_cnt = 2,
111 2088 : .handshake_cnt = 2,
112 2088 : .conn_id_cnt = 4,
113 2088 : .inflight_pkt_cnt = 8UL,
114 2088 : .stream_pool_cnt = 8UL
115 2088 : };
116 :
117 : /* Enable features depending on the last few bits. The last bits are
118 : pseudorandom (either ignored or belong to the MAC tag) */
119 2088 : uint last_byte = 0U;
120 2088 : if( size > 0 ) last_byte = data[ size-1 ];
121 2088 : int enable_retry = !!(last_byte & 1);
122 2088 : int role = (last_byte & 2) ? FD_QUIC_ROLE_SERVER : FD_QUIC_ROLE_CLIENT;
123 2088 : int established = !!(last_byte & 4);
124 :
125 2088 : assert( fd_quic_footprint( &quic_limits ) <= sizeof(quic_mem) );
126 0 : void * shquic = fd_quic_new( quic_mem, &quic_limits );
127 2088 : fd_quic_t * quic = fd_quic_join( shquic );
128 :
129 2088 : fd_quic_config_anonymous( quic, role );
130 :
131 2088 : fd_tls_test_sign_ctx_t test_signer[1];
132 2088 : fd_tls_test_sign_ctx( test_signer, rng );
133 2088 : fd_quic_config_test_signer( quic, test_signer );
134 :
135 2088 : quic->cb.now = test_clock;
136 2088 : quic->config.retry = enable_retry;
137 :
138 2088 : fd_aio_t aio_[1];
139 2088 : fd_aio_t * aio = fd_aio_join( fd_aio_new( aio_, NULL, _aio_send ) );
140 2088 : assert( aio );
141 :
142 0 : fd_quic_set_aio_net_tx( quic, aio );
143 2088 : assert( fd_quic_init( quic ) );
144 0 : assert( quic->config.idle_timeout > 0 );
145 :
146 0 : fd_quic_state_t * state = fd_quic_get_state( quic );
147 :
148 : /* Create dummy connection */
149 2088 : ulong our_conn_id = ULONG_MAX;
150 2088 : fd_quic_conn_id_t peer_conn_id = { .sz=8 };
151 2088 : uint dst_ip_addr = 0U;
152 2088 : ushort dst_udp_port = (ushort)0;
153 :
154 2088 : fd_quic_conn_t * conn =
155 2088 : fd_quic_conn_create( quic,
156 2088 : our_conn_id, &peer_conn_id,
157 2088 : dst_ip_addr, (ushort)dst_udp_port,
158 2088 : 0U, 0U,
159 2088 : 1 /* we are the server */ );
160 2088 : assert( conn );
161 0 : assert( conn->svc_type == FD_QUIC_SVC_WAIT );
162 :
163 0 : conn->tx_max_data = 512UL;
164 2088 : conn->tx_initial_max_stream_data_uni = 64UL;
165 2088 : conn->srx->rx_max_data = 512UL;
166 2088 : conn->srx->rx_sup_stream_id = 32UL;
167 2088 : conn->tx_max_datagram_sz = FD_QUIC_MTU;
168 2088 : conn->tx_sup_stream_id = 32UL;
169 :
170 2088 : if( established ) {
171 861 : conn->state = FD_QUIC_CONN_STATE_ACTIVE;
172 861 : conn->keys_avail = 0xff;
173 861 : }
174 :
175 2088 : g_clock = 1000UL;
176 :
177 : /* Calls fuzz entrypoint */
178 2088 : send_udp_packet( quic, data, size );
179 :
180 : /* svc_quota is the max number of service calls that we expect to
181 : schedule in response to a single packet. */
182 2088 : long svc_quota = fd_long_max( (long)size, 1000L );
183 :
184 2169 : while( state->svc_queue[ FD_QUIC_SVC_INSTANT ].tail!=UINT_MAX ) {
185 81 : fd_quic_service( quic );
186 81 : assert( --svc_quota > 0 );
187 81 : }
188 2088 : assert( conn->svc_type != FD_QUIC_SVC_INSTANT );
189 :
190 : /* Generate ACKs */
191 2088 : while( state->svc_queue[ FD_QUIC_SVC_ACK_TX ].head != UINT_MAX ) {
192 0 : fd_quic_conn_t * conn = fd_quic_conn_at_idx( state, state->svc_queue[ FD_QUIC_SVC_ACK_TX ].head );
193 0 : g_clock = conn->svc_time;
194 0 : fd_quic_service( quic );
195 0 : assert( --svc_quota > 0 );
196 0 : }
197 2088 : assert( conn->svc_type != FD_QUIC_SVC_INSTANT &&
198 2088 : conn->svc_type != FD_QUIC_SVC_ACK_TX );
199 :
200 : /* Simulate conn timeout */
201 6618 : while( state->svc_queue[ FD_QUIC_SVC_WAIT ].head != UINT_MAX ) {
202 4530 : ulong idle_timeout_ts = conn->last_activity + quic->config.idle_timeout + 1UL;
203 4530 : fd_quic_conn_t * conn = fd_quic_conn_at_idx( state, state->svc_queue[ FD_QUIC_SVC_WAIT ].head );
204 :
205 : /* Idle timeouts should not be scheduled significantly late */
206 4530 : assert( conn->svc_time < idle_timeout_ts + (ulong)2e9 );
207 :
208 0 : g_clock = conn->svc_time;
209 4530 : fd_quic_service( quic );
210 4530 : assert( --svc_quota > 0 );
211 4530 : }
212 2088 : assert( conn->svc_type == UINT_MAX );
213 0 : assert( conn->state == FD_QUIC_CONN_STATE_DEAD || conn->state == FD_QUIC_CONN_STATE_INVALID );
214 :
215 0 : fd_quic_delete( fd_quic_leave( fd_quic_fini( quic ) ) );
216 2088 : fd_aio_delete( fd_aio_leave( aio ) );
217 2088 : fd_rng_delete( fd_rng_leave( rng ) );
218 2088 : return 0;
219 2088 : }
220 :
221 : #if !FD_QUIC_DISABLE_CRYPTO
222 :
223 : static fd_quic_crypto_keys_t const keys[1] = {{
224 : .pkt_key = {0},
225 : .iv = {0},
226 : .hp_key = {0},
227 : }};
228 :
229 : /* guess_packet_size attempts to discover the end of a QUIC packet.
230 : Returns the total length (including GCM tag) on success, sets *pn_off
231 : to the packet number offset and *pn to the packet number. Returns
232 : 0UL on failure. */
233 :
234 : static ulong
235 : guess_packet_size( uchar const * data,
236 : ulong size,
237 0 : ulong * pn_off ) {
238 :
239 0 : uchar const * cur_ptr = data;
240 0 : ulong cur_sz = size;
241 :
242 0 : ulong pkt_num_pnoff = 0UL;
243 0 : ulong total_len = size;
244 :
245 0 : if( FD_UNLIKELY( size < 1 ) ) return FD_QUIC_PARSE_FAIL;
246 0 : uchar hdr_form = fd_quic_h0_hdr_form( *cur_ptr );
247 :
248 0 : ulong rc;
249 0 : if( hdr_form == 1 ) { /* long header */
250 :
251 0 : uchar long_packet_type = fd_quic_h0_long_packet_type( *cur_ptr );
252 0 : cur_ptr += 1; cur_sz -= 1UL;
253 0 : fd_quic_long_hdr_t long_hdr[1];
254 0 : rc = fd_quic_decode_long_hdr( long_hdr, cur_ptr, cur_sz );
255 0 : if( rc == FD_QUIC_PARSE_FAIL ) return 0UL;
256 0 : cur_ptr += rc; cur_sz -= rc;
257 :
258 0 : switch( long_packet_type ) {
259 0 : case FD_QUIC_PKT_TYPE_INITIAL: {
260 0 : fd_quic_initial_t initial[1];
261 0 : rc = fd_quic_decode_initial( initial, cur_ptr, cur_sz );
262 0 : if( rc == FD_QUIC_PARSE_FAIL ) return 0UL;
263 0 : cur_ptr += rc; cur_sz -= rc;
264 :
265 0 : pkt_num_pnoff = initial->pkt_num_pnoff;
266 0 : total_len = pkt_num_pnoff + initial->len;
267 0 : break;
268 0 : }
269 0 : case FD_QUIC_PKT_TYPE_HANDSHAKE: {
270 0 : fd_quic_handshake_t handshake[1];
271 0 : rc = fd_quic_decode_handshake( handshake, cur_ptr, cur_sz );
272 0 : if( rc == FD_QUIC_PARSE_FAIL ) return 0UL;
273 0 : cur_ptr += rc; cur_sz -= rc;
274 :
275 0 : pkt_num_pnoff = handshake->pkt_num_pnoff;
276 0 : total_len = pkt_num_pnoff + handshake->len;
277 0 : break;
278 0 : }
279 0 : case FD_QUIC_PKT_TYPE_RETRY:
280 : /* Do we need to decrypt Retry packets? I'm not sure */
281 : /* TODO correctly derive size of packet in case there is another
282 : packet following the retry packet */
283 0 : return 0UL;
284 0 : case FD_QUIC_PKT_TYPE_ZERO_RTT:
285 : /* No support for 0-RTT yet */
286 0 : return 0UL;
287 0 : default:
288 0 : __builtin_unreachable();
289 0 : }
290 :
291 0 : } else { /* short header */
292 :
293 0 : fd_quic_one_rtt_t one_rtt[1];
294 0 : one_rtt->dst_conn_id_len = 8;
295 0 : rc = fd_quic_decode_one_rtt( one_rtt, cur_ptr, cur_sz );
296 0 : if( rc == FD_QUIC_PARSE_FAIL ) return 0UL;
297 0 : cur_ptr += rc; cur_sz -= rc;
298 :
299 0 : pkt_num_pnoff = one_rtt->pkt_num_pnoff;
300 :
301 0 : }
302 :
303 0 : *pn_off = pkt_num_pnoff;
304 0 : return total_len;
305 0 : }
306 :
307 : /* decrypt_packet attempts to decrypt the first QUIC packet in the given
308 : buffer. data points to the first byte of the QUIC packet. size is
309 : the number of bytes until the end of the UDP datagram. Returns the
310 : number of bytes that belonged to the first packet (<= size) on
311 : success. Returns 0 on failure and leaves the packet (partially)
312 : encrypted. */
313 :
314 : static ulong
315 : decrypt_packet( uchar * const data,
316 0 : ulong const size ) {
317 :
318 0 : ulong pkt_num_pnoff = 0UL;
319 0 : ulong total_len = guess_packet_size( data, size, &pkt_num_pnoff );
320 0 : if( !total_len ) return 0UL;
321 :
322 : /* Decrypt the packet */
323 :
324 0 : int decrypt_res = fd_quic_crypto_decrypt_hdr( data, size, pkt_num_pnoff, keys );
325 0 : if( decrypt_res != FD_QUIC_SUCCESS ) return 0UL;
326 :
327 0 : uint pkt_number_sz = fd_quic_h0_pkt_num_len( data[0] ) + 1u;
328 0 : ulong pkt_number = fd_quic_pktnum_decode( data+pkt_num_pnoff, pkt_number_sz );
329 :
330 0 : decrypt_res =
331 0 : fd_quic_crypto_decrypt( data, size,
332 0 : pkt_num_pnoff, pkt_number,
333 0 : keys );
334 0 : if( decrypt_res != FD_QUIC_SUCCESS ) return 0UL;
335 :
336 0 : return fd_ulong_min( total_len + FD_QUIC_CRYPTO_TAG_SZ, size );
337 0 : }
338 :
339 : /* decrypt_payload attempts to remove packet protection of a UDP
340 : datagram payload in-place. Note that a UDP datagram can contain
341 : multiple QUIC packets. */
342 :
343 : static int
344 : decrypt_payload( uchar * data,
345 0 : ulong size ) {
346 :
347 0 : if( size < 16 ) return 0;
348 :
349 : /* Heuristic: If the last 16 bytes of the packet (the AES-GCM tag) are
350 : zero consider it an unencrypted packet */
351 :
352 0 : uint mask=0U;
353 0 : for( ulong j=0UL; j<16UL; j++ ) mask |= data[size-16+j];
354 0 : if( !mask ) return 1;
355 :
356 0 : uchar * cur_ptr = data;
357 0 : ulong cur_sz = size;
358 :
359 0 : do {
360 :
361 0 : ulong sz = decrypt_packet( cur_ptr, cur_sz );
362 0 : if( !sz ) return 0;
363 0 : assert( sz <= cur_sz ); /* prevent out of bounds */
364 :
365 0 : cur_ptr += sz; cur_sz -= sz;
366 :
367 0 : } while( cur_sz );
368 :
369 0 : return 1;
370 0 : }
371 :
372 : static ulong
373 : encrypt_packet( uchar * const data,
374 0 : ulong const size ) {
375 :
376 0 : uchar out[ FD_QUIC_MTU ];
377 :
378 0 : ulong pkt_num_pnoff = 0UL;
379 0 : ulong total_len = guess_packet_size( data, size, &pkt_num_pnoff );
380 0 : if( ( total_len < FD_QUIC_CRYPTO_TAG_SZ ) |
381 0 : ( total_len > size ) |
382 0 : ( total_len > sizeof(out) ) )
383 0 : return size;
384 :
385 0 : uchar first = data[0];
386 0 : ulong pkt_number_sz = ( first & 0x03u ) + 1;
387 :
388 0 : ulong out_sz = total_len;
389 0 : uchar const * hdr = data;
390 0 : ulong hdr_sz = pkt_num_pnoff + pkt_number_sz;
391 :
392 0 : ulong pkt_number = 0UL;
393 0 : for( ulong j = 0UL; j < pkt_number_sz; ++j ) {
394 0 : pkt_number = ( pkt_number << 8UL ) + (ulong)( hdr[pkt_num_pnoff + j] );
395 0 : }
396 :
397 0 : if( ( out_sz < hdr_sz ) |
398 0 : ( out_sz - hdr_sz < FD_QUIC_CRYPTO_TAG_SZ ) )
399 0 : return size;
400 :
401 0 : uchar const * pay = hdr + hdr_sz;
402 0 : ulong pay_sz = out_sz - hdr_sz - FD_QUIC_CRYPTO_TAG_SZ;
403 :
404 0 : int encrypt_res =
405 0 : fd_quic_crypto_encrypt( out, &out_sz,
406 0 : hdr, hdr_sz,
407 0 : pay, pay_sz,
408 0 : keys, keys,
409 0 : pkt_number );
410 0 : if( encrypt_res != FD_QUIC_SUCCESS )
411 0 : return size;
412 0 : assert( out_sz == total_len );
413 :
414 0 : fd_memcpy( data, out, out_sz );
415 0 : return out_sz;
416 0 : }
417 :
418 : static void
419 : encrypt_payload( uchar * data,
420 0 : ulong size ) {
421 :
422 0 : uchar * cur_ptr = data;
423 0 : ulong cur_sz = size;
424 :
425 0 : while( cur_sz ) {
426 0 : ulong sz = encrypt_packet( cur_ptr, cur_sz );
427 0 : assert( sz ); /* prevent infinite loop */
428 0 : assert( sz <= cur_sz ); /* prevent out of bounds */
429 :
430 0 : cur_ptr += sz; cur_sz -= sz;
431 0 : }
432 0 : }
433 :
434 : /* LLVMFuzzerCustomMutator has the following behavior:
435 :
436 : - If the input is not encrypted, mutates the raw input, and produces
437 : an encrypted output
438 : - If the input is encrypted, mutates the decrypted input, and
439 : produces another encrypted output
440 : - If the input appears to be encrypted but fails to decrypt, mutates
441 : the raw encrypted input, and produces another output that will fail
442 : to decrypt. */
443 :
444 : ulong
445 : LLVMFuzzerCustomMutator( uchar * data,
446 : ulong data_sz,
447 : ulong max_sz,
448 0 : uint seed ) {
449 0 : int ok = decrypt_payload( data, data_sz );
450 0 : data_sz = LLVMFuzzerMutate( data, data_sz, max_sz );
451 0 : if( ok ) encrypt_payload( data, data_sz );
452 0 : (void)seed;
453 0 : return data_sz;
454 0 : }
455 :
456 : /* Find a strategy for custom crossover of decrypted packets */
457 :
458 : #endif /* !FD_QUIC_DISABLE_CRYPTO */
|