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