Debian Codegraph @

crt/aws-c-http/tests/fuzz/fuzz_h2_decoder_correct.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/testing/aws_test_harness.h>
6
7 #include <aws/common/allocator.h>
8 #include <aws/common/byte_buf.h>
9 #include <aws/common/logging.h>
10
11 #include <aws/http/private/h2_decoder.h>
12 #include <aws/http/private/h2_frames.h>
13 #include <aws/http/private/hpack.h>
14
15 #include <aws/io/stream.h>
16
17 #include <inttypes.h>
18
19 static const uint32_t MAX_PAYLOAD_SIZE = 16384;
20
21 enum header_style {
22 HEADER_STYLE_REQUEST,
23 HEADER_STYLE_RESPONSE,
24 HEADER_STYLE_TRAILER,
25 };
26
27 static struct aws_http_headers *s_generate_headers(
28 struct aws_allocator *allocator,
29 struct aws_byte_cursor *input,
30 enum header_style header_style) {
31
32 struct aws_http_headers *headers = aws_http_headers_new(allocator);
33
34 /* There are pretty strict requirements about pseudo-headers, no randomness for now */
35 if (header_style == HEADER_STYLE_REQUEST) {
36 struct aws_http_header method = {.name = aws_http_header_method, .value = aws_http_method_get};
37 aws_http_headers_add_header(headers, &method);
38
39 struct aws_http_header scheme = {.name = aws_http_header_scheme, .value = aws_http_scheme_https};
40 aws_http_headers_add_header(headers, &scheme);
41
42 struct aws_http_header path = {.name = aws_http_header_path, .value = aws_byte_cursor_from_c_str("/")};
43 aws_http_headers_add_header(headers, &path);
44
45 struct aws_http_header authority = {
46 .name = aws_http_header_authority,
47 .value = aws_byte_cursor_from_c_str("example.com"),
48 };
49 aws_http_headers_add_header(headers, &authority);
50
51 } else if (header_style == HEADER_STYLE_RESPONSE) {
52 struct aws_http_header status = {.name = aws_http_header_status, .value = aws_byte_cursor_from_c_str("200")};
53 aws_http_headers_add_header(headers, &status);
54 }
55
56 struct aws_byte_buf buf;
57 aws_byte_buf_init(&buf, allocator, 1024);
58
59 while (input->len) {
60 buf.len = 0;
61
62 struct aws_http_header header;
63 AWS_ZERO_STRUCT(header);
64
65 uint8_t type = 0;
66 aws_byte_cursor_read_u8(input, &type);
67 switch (type % 3) {
68 case 0:
69 header.compression = AWS_HTTP_HEADER_COMPRESSION_USE_CACHE;
70 break;
71 case 1:
72 header.compression = AWS_HTTP_HEADER_COMPRESSION_NO_CACHE;
73 break;
74 case 2:
75 header.compression = AWS_HTTP_HEADER_COMPRESSION_NO_FORWARD_CACHE;
76 break;
77 }
78
79 /* Start name with "x-" so we don't violate some rule for an official header.
80 * Then add some more valid characters. */
81 struct aws_byte_cursor header_name_prefix = aws_byte_cursor_from_c_str("x-");
82 aws_byte_buf_append(&buf, &header_name_prefix);
83
84 uint8_t name_suffix_len = 0;
85 aws_byte_cursor_read_u8(input, &name_suffix_len);
86 for (size_t i = 0; i < name_suffix_len; ++i) {
87 uint8_t c = 0;
88 aws_byte_cursor_read_u8(input, &c);
89 c = 'a' + (c % 26); /* a-z */
90 aws_byte_buf_write_u8(&buf, c);
91 }
92
93 header.name = aws_byte_cursor_from_buf(&buf);
94
95 /* Fill header.value with valid characters */
96 uint8_t value_len = 0;
97 aws_byte_cursor_read_u8(input, &value_len);
98 for (size_t i = 0; i < value_len; ++i) {
99 uint8_t c = 0;
100 aws_byte_cursor_read_u8(input, &c);
101 c = 'a' + (c % 26); /* a-z */
102 aws_byte_buf_write_u8(&buf, c);
103 }
104
105 header.value = aws_byte_cursor_from_buf(&buf);
106 aws_byte_cursor_advance(&header.value, header.name.len);
107
108 aws_http_headers_add_header(headers, &header);
109 }
110
111 aws_byte_buf_clean_up(&buf);
112 return headers;
113 }
114
115 static uint32_t s_generate_stream_id(struct aws_byte_cursor *input) {
116 uint32_t stream_id = 0;
117 aws_byte_cursor_read_be32(input, &stream_id);
118 return aws_min_u32(AWS_H2_STREAM_ID_MAX, aws_max_u32(1, stream_id));
119 }
120
121 /* Server-initiated stream-IDs must be even */
122 static uint32_t s_generate_even_stream_id(struct aws_byte_cursor *input) {
123 uint32_t stream_id = 0;
124 aws_byte_cursor_read_be32(input, &stream_id);
125 stream_id = aws_min_u32(AWS_H2_STREAM_ID_MAX, aws_max_u32(2, stream_id));
126
127 if (stream_id % 2 != 0) {
128 stream_id -= 1;
129 }
130
131 return stream_id;
132 }
133
134 /* Client-initiated stream-IDs must be odd */
135 static uint32_t s_generate_odd_stream_id(struct aws_byte_cursor *input) {
136 uint32_t stream_id = 0;
137 aws_byte_cursor_read_be32(input, &stream_id);
138 stream_id = aws_min_u32(AWS_H2_STREAM_ID_MAX, aws_max_u32(1, stream_id));
139
140 if (stream_id % 2 == 0) {
141 stream_id += 1;
142 }
143
144 return stream_id;
145 }
146
147 static struct aws_h2_frame_priority_settings s_generate_priority(struct aws_byte_cursor *input) {
148 struct aws_h2_frame_priority_settings priority;
149 priority.stream_dependency = s_generate_stream_id(input);
150
151 uint8_t exclusive = 0;
152 aws_byte_cursor_read_u8(input, &exclusive);
153 priority.stream_dependency_exclusive = (bool)exclusive;
154
155 aws_byte_cursor_read_u8(input, &priority.weight);
156
157 return priority;
158 }
159
160 AWS_EXTERN_C_BEGIN
161
162 /**
163 * This test generates valid frames from the random input.
164 * It feeds these frames through the encoder and ensures that they're output without error.
165 * Then it feeds the encoder's output to the decoder and ensures that it does not report an error.
166 * It does not currently investigate the outputs to see if they line up with they inputs,
167 * it just checks for errors from the encoder & decoder.
168 */
169 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
170 /* Setup allocator and parameters */
171 struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0);
172 struct aws_byte_cursor input = aws_byte_cursor_from_array(data, size);
173
174 /* Enable logging */
175 struct aws_logger logger;
176 struct aws_logger_standard_options log_options = {
177 .level = AWS_LL_TRACE,
178 .file = stdout,
179 };
180 aws_logger_init_standard(&logger, allocator, &log_options);
181 aws_logger_set(&logger);
182
183 /* Init HTTP (s2n init is weird, so don't do this under the tracer) */
184 aws_http_library_init(aws_default_allocator());
185
186 /* Create the encoder */
187 struct aws_h2_frame_encoder encoder;
188 aws_h2_frame_encoder_init(&encoder, allocator, NULL /*logging_id*/);
189
190 /* Create the decoder */
191 uint8_t decoder_is_server = 0;
192 aws_byte_cursor_read_u8(&input, &decoder_is_server);
193 const struct aws_h2_decoder_vtable decoder_vtable = {0};
194 struct aws_h2_decoder_params decoder_params = {
195 .alloc = allocator,
196 .vtable = &decoder_vtable,
197 .skip_connection_preface = true,
198 .is_server = decoder_is_server,
199 };
200 struct aws_h2_decoder *decoder = aws_h2_decoder_new(&decoder_params);
201
202 /* Init the buffer */
203 struct aws_byte_buf frame_data;
204 aws_byte_buf_init(&frame_data, allocator, AWS_H2_FRAME_PREFIX_SIZE + MAX_PAYLOAD_SIZE);
205
206 /*
207 * Generate the frame to decode
208 */
209
210 uint8_t frame_type = 0;
211 aws_byte_cursor_read_u8(&input, &frame_type);
212 frame_type = frame_type % (AWS_H2_FRAME_TYPE_COUNT);
213 if (decoder_is_server && frame_type == AWS_H2_FRAME_T_PUSH_PROMISE) {
214 /* Client can't send push-promise to server */
215 frame_type = AWS_H2_FRAME_T_HEADERS;
216 }
217
218 /* figure out if we should use huffman encoding */
219 uint8_t huffman_choice = 0;
220 aws_byte_cursor_read_u8(&input, &huffman_choice);
221 aws_hpack_encoder_set_huffman_mode(&encoder.hpack, huffman_choice % 3);
222
223 switch (frame_type) {
224 case AWS_H2_FRAME_T_DATA: {
225 uint32_t stream_id = s_generate_stream_id(&input);
226
227 uint8_t flags = 0;
228 aws_byte_cursor_read_u8(&input, &flags);
229 bool body_ends_stream = flags & AWS_H2_FRAME_F_END_STREAM;
230
231 uint8_t pad_length = 0;
232 aws_byte_cursor_read_u8(&input, &pad_length);
233
234 /* Allow body to exceed available space. Data encoder should just write what it can fit */
235 struct aws_input_stream *body = aws_input_stream_new_from_cursor(allocator, &input);
236 if (input.len == 0) {
237 /* In case of empty body, make sure the end stream flag to be set, other wise, no frames should be
238 * generated to decode. */
239 body_ends_stream = true;
240 }
241
242 bool body_complete;
243 bool body_stalled;
244 bool body_failed;
245 int32_t stream_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
246 uint32_t connection_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
247 AWS_FATAL_ASSERT(
248 aws_h2_encode_data_frame(
249 &encoder,
250 stream_id,
251 body,
252 (bool)body_ends_stream,
253 pad_length,
254 &stream_window_size_peer,
255 &connection_window_size_peer,
256 &frame_data,
257 &body_complete,
258 &body_stalled,
259 &body_failed) == AWS_OP_SUCCESS);
260
261 struct aws_stream_status body_status;
262 aws_input_stream_get_status(body, &body_status);
263 AWS_FATAL_ASSERT(body_complete == body_status.is_end_of_stream);
264 aws_input_stream_release(body);
265 break;
266 }
267 case AWS_H2_FRAME_T_HEADERS: {
268 /* If decoder is server, headers can only arrive on client-initiated streams
269 * If decoder is client, header might arrive on server-initiated or client-initiated streams */
270 uint32_t stream_id = decoder_is_server ? s_generate_odd_stream_id(&input) : s_generate_stream_id(&input);
271
272 uint8_t flags = 0;
273 aws_byte_cursor_read_u8(&input, &flags);
274 bool end_stream = flags & AWS_H2_FRAME_F_END_STREAM;
275 bool use_priority = flags & AWS_H2_FRAME_F_PRIORITY;
276
277 uint8_t pad_length = 0;
278 aws_byte_cursor_read_u8(&input, &pad_length);
279
280 struct aws_h2_frame_priority_settings priority = s_generate_priority(&input);
281 struct aws_h2_frame_priority_settings *priority_ptr = use_priority ? &priority : NULL;
282
283 /* Server can only receive request-style HEADERS, client can only receive response-style HEADERS.
284 * But either side can receive trailer-style HEADERS */
285 uint8_t is_normal_header = 0;
286 aws_byte_cursor_read_u8(&input, &is_normal_header);
287 enum header_style header_style;
288 if (is_normal_header) {
289 if (decoder_is_server) {
290 header_style = HEADER_STYLE_REQUEST;
291 } else {
292 header_style = HEADER_STYLE_RESPONSE;
293 }
294 } else {
295 header_style = HEADER_STYLE_TRAILER;
296 end_stream = true; /* Trailer must END_STREAM */
297 }
298
299 /* generate headers last since it uses up the rest of input */
300 struct aws_http_headers *headers = s_generate_headers(allocator, &input, header_style);
301
302 struct aws_h2_frame *frame =
303 aws_h2_frame_new_headers(allocator, stream_id, headers, end_stream, pad_length, priority_ptr);
304 AWS_FATAL_ASSERT(frame);
305
306 bool frame_complete;
307 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
308 AWS_FATAL_ASSERT(frame_complete == true);
309
310 aws_h2_frame_destroy(frame);
311 aws_http_headers_release(headers);
312 break;
313 }
314 case AWS_H2_FRAME_T_PRIORITY: {
315 uint32_t stream_id = s_generate_stream_id(&input);
316 struct aws_h2_frame_priority_settings priority = s_generate_priority(&input);
317
318 struct aws_h2_frame *frame = aws_h2_frame_new_priority(allocator, stream_id, &priority);
319 AWS_FATAL_ASSERT(frame);
320
321 bool frame_complete;
322 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
323 AWS_FATAL_ASSERT(frame_complete == true);
324
325 aws_h2_frame_destroy(frame);
326 break;
327 }
328 case AWS_H2_FRAME_T_RST_STREAM: {
329 uint32_t stream_id = s_generate_stream_id(&input);
330
331 uint32_t error_code = 0;
332 aws_byte_cursor_read_be32(&input, &error_code);
333
334 struct aws_h2_frame *frame = aws_h2_frame_new_rst_stream(allocator, stream_id, error_code);
335 AWS_FATAL_ASSERT(frame);
336
337 bool frame_complete;
338 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
339 AWS_FATAL_ASSERT(frame_complete == true);
340
341 aws_h2_frame_destroy(frame);
342 break;
343 }
344 case AWS_H2_FRAME_T_SETTINGS: {
345 uint8_t flags = 0;
346 aws_byte_cursor_read_u8(&input, &flags);
347
348 bool ack = flags & AWS_H2_FRAME_F_ACK;
349
350 size_t settings_count = 0;
351 struct aws_http2_setting *settings_array = NULL;
352
353 if (!ack) {
354 settings_count = aws_min_size(input.len / 6, MAX_PAYLOAD_SIZE);
355 if (settings_count > 0) {
356 settings_array = aws_mem_calloc(allocator, settings_count, sizeof(struct aws_http2_setting));
357 for (size_t i = 0; i < settings_count; ++i) {
358 uint16_t id = 0;
359 uint32_t value = 0;
360 aws_byte_cursor_read_be16(&input, &id);
361 aws_byte_cursor_read_be32(&input, &value);
362 if (id >= AWS_HTTP2_SETTINGS_BEGIN_RANGE && id < AWS_HTTP2_SETTINGS_END_RANGE) {
363 value = aws_max_u32(value, aws_h2_settings_bounds[id][0]);
364 value = aws_min_u32(value, aws_h2_settings_bounds[id][1]);
365 }
366 settings_array[i].id = id;
367 settings_array[i].value = value;
368 }
369 }
370 }
371
372 struct aws_h2_frame *frame = aws_h2_frame_new_settings(allocator, settings_array, settings_count, ack);
373 AWS_FATAL_ASSERT(frame);
374
375 bool frame_complete;
376 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
377 AWS_FATAL_ASSERT(frame_complete == true);
378
379 aws_h2_frame_destroy(frame);
380 aws_mem_release(allocator, settings_array);
381 break;
382 }
383 case AWS_H2_FRAME_T_PUSH_PROMISE: {
384 uint32_t stream_id = s_generate_odd_stream_id(&input);
385 uint32_t promised_stream_id = s_generate_even_stream_id(&input);
386
387 uint8_t pad_length = 0;
388 aws_byte_cursor_read_u8(&input, &pad_length);
389
390 /* generate headers last since it uses up the rest of input */
391 struct aws_http_headers *headers = s_generate_headers(allocator, &input, HEADER_STYLE_REQUEST);
392
393 struct aws_h2_frame *frame =
394 aws_h2_frame_new_push_promise(allocator, stream_id, promised_stream_id, headers, pad_length);
395 AWS_FATAL_ASSERT(frame);
396
397 bool frame_complete;
398 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
399 AWS_FATAL_ASSERT(frame_complete == true);
400
401 aws_h2_frame_destroy(frame);
402 aws_http_headers_release(headers);
403 break;
404 }
405 case AWS_H2_FRAME_T_PING: {
406 uint8_t flags;
407 aws_byte_cursor_read_u8(&input, &flags);
408 bool ack = flags & AWS_H2_FRAME_F_ACK;
409
410 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0};
411 size_t copy_len = aws_min_size(input.len, AWS_HTTP2_PING_DATA_SIZE);
412 if (copy_len > 0) {
413 struct aws_byte_cursor copy = aws_byte_cursor_advance(&input, copy_len);
414 memcpy(opaque_data, copy.ptr, copy.len);
415 }
416
417 struct aws_h2_frame *frame = aws_h2_frame_new_ping(allocator, ack, opaque_data);
418 AWS_FATAL_ASSERT(frame);
419
420 bool frame_complete;
421 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
422 AWS_FATAL_ASSERT(frame_complete == true);
423
424 aws_h2_frame_destroy(frame);
425 break;
426 }
427 case AWS_H2_FRAME_T_GOAWAY: {
428 uint32_t last_stream_id = s_generate_stream_id(&input);
429
430 uint32_t error_code = 0;
431 aws_byte_cursor_read_be32(&input, &error_code);
432
433 /* Pass debug_data that might be too large (it will get truncated if necessary) */
434 struct aws_byte_cursor debug_data = aws_byte_cursor_advance(&input, input.len);
435
436 struct aws_h2_frame *frame = aws_h2_frame_new_goaway(allocator, last_stream_id, error_code, debug_data);
437 AWS_FATAL_ASSERT(frame);
438
439 bool frame_complete;
440 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
441 AWS_FATAL_ASSERT(frame_complete == true);
442
443 aws_h2_frame_destroy(frame);
444 break;
445 }
446 case AWS_H2_FRAME_T_WINDOW_UPDATE: {
447 /* WINDOW_UPDATE's stream-id can be zero or non-zero */
448 uint32_t stream_id = 0;
449 aws_byte_cursor_read_be32(&input, &stream_id);
450 stream_id = aws_min_u32(stream_id, AWS_H2_STREAM_ID_MAX);
451
452 uint32_t window_size_increment = 0;
453 aws_byte_cursor_read_be32(&input, &window_size_increment);
454 window_size_increment = aws_min_u32(window_size_increment, AWS_H2_WINDOW_UPDATE_MAX);
455
456 struct aws_h2_frame *frame = aws_h2_frame_new_window_update(allocator, stream_id, window_size_increment);
457 AWS_FATAL_ASSERT(frame);
458
459 bool frame_complete;
460 AWS_FATAL_ASSERT(aws_h2_encode_frame(&encoder, frame, &frame_data, &frame_complete) == AWS_OP_SUCCESS);
461 AWS_FATAL_ASSERT(frame_complete == true);
462
463 aws_h2_frame_destroy(frame);
464 break;
465 }
466 case AWS_H2_FRAME_T_CONTINUATION:
467 /* We don't directly create CONTINUATION frames (they occur when HEADERS or PUSH_PROMISE gets too big) */
468 frame_type = AWS_H2_FRAME_T_UNKNOWN;
469 /* fallthrough */
470 case AWS_H2_FRAME_T_UNKNOWN: {
471 /* #YOLO roll our own frame */
472 uint32_t payload_length = aws_min_u32(input.len, MAX_PAYLOAD_SIZE - AWS_H2_FRAME_PREFIX_SIZE);
473
474 /* Write payload length */
475 aws_byte_buf_write_be24(&frame_data, payload_length);
476
477 /* Write type */
478 aws_byte_buf_write_u8(&frame_data, frame_type);
479
480 /* Write flags */
481 uint8_t flags = 0;
482 aws_byte_cursor_read_u8(&input, &flags);
483 aws_byte_buf_write_u8(&frame_data, flags);
484
485 /* Write stream-id */
486 uint32_t stream_id = 0;
487 aws_byte_cursor_read_be32(&input, &stream_id);
488 aws_byte_buf_write_be32(&frame_data, stream_id);
489
490 /* Write payload */
491 aws_byte_buf_write_from_whole_cursor(&frame_data, aws_byte_cursor_advance(&input, payload_length));
492 break;
493 }
494 default: {
495 AWS_FATAL_ASSERT(false);
496 }
497 }
498
499 /* Decode whatever we got */
500 AWS_FATAL_ASSERT(frame_data.len > 0);
501 struct aws_byte_cursor to_decode = aws_byte_cursor_from_buf(&frame_data);
502 struct aws_h2err err = aws_h2_decode(decoder, &to_decode);
503 AWS_FATAL_ASSERT(aws_h2err_success(err));
504 AWS_FATAL_ASSERT(to_decode.len == 0);
505
506 /* Clean up */
507 aws_byte_buf_clean_up(&frame_data);
508 aws_h2_decoder_destroy(decoder);
509 aws_h2_frame_encoder_clean_up(&encoder);
510 aws_logger_set(NULL);
511 aws_logger_clean_up(&logger);
512
513 atexit(aws_http_library_clean_up);
514
515 /* Check for leaks */
516 AWS_FATAL_ASSERT(aws_mem_tracer_count(allocator) == 0);
517 allocator = aws_mem_tracer_destroy(allocator);
518
519 return 0;
520 }
521
522 AWS_EXTERN_C_END