Line | Count | Source |
1 | | /* Copyright 2022 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include <stdio.h> |
14 | | #include <stddef.h> |
15 | | #include <stdint.h> |
16 | | #include <string.h> |
17 | | #include <assert.h> |
18 | | #include <stdlib.h> |
19 | | #include <inttypes.h> |
20 | | #include "zlib.h" |
21 | | |
22 | | static const uint8_t *data; |
23 | | static size_t dataLen; |
24 | | |
25 | | static void check_compress_level(uint8_t *compr, size_t comprLen, |
26 | | uint8_t *uncompr, size_t uncomprLen, |
27 | 3.84k | int level) { |
28 | 3.84k | if (compress2(compr, &comprLen, data, dataLen, level) == Z_MEM_ERROR) |
29 | 0 | return; |
30 | 3.84k | if (uncompress(uncompr, &uncomprLen, compr, comprLen) == Z_MEM_ERROR) |
31 | 0 | return; |
32 | | |
33 | | /* Make sure compress + uncompress gives back the input data. */ |
34 | 3.84k | assert(dataLen == uncomprLen); |
35 | 3.84k | assert(0 == memcmp(data, uncompr, dataLen)); |
36 | 3.84k | } |
37 | | |
38 | 7.68k | #define put_byte(s, i, c) {s[i] = (unsigned char)(c);} |
39 | | |
40 | 3.84k | static void write_zlib_header(uint8_t *s, unsigned compression_method, unsigned flags) { |
41 | 3.84k | unsigned int header = (Z_DEFLATED + ((flags)<<4)) << 8; |
42 | 3.84k | header |= (compression_method << 6); |
43 | | |
44 | 3.84k | header += 31 - (header % 31); |
45 | | |
46 | | /* s is guaranteed to be longer than 2 bytes. */ |
47 | 3.84k | put_byte(s, 0, (unsigned char)(header >> 8)); |
48 | 3.84k | put_byte(s, 1, (unsigned char)(header & 0xff)); |
49 | 3.84k | } |
50 | | |
51 | 3.84k | static void check_decompress(uint8_t *compr, size_t comprLen, unsigned compression_method, unsigned flags) { |
52 | | /* We need to write a valid zlib header of size two bytes. Copy the input data |
53 | | in a larger buffer. Do not modify the input data to avoid libFuzzer error: |
54 | | fuzz target overwrites its const input. */ |
55 | 3.84k | size_t copyLen = dataLen + 2; |
56 | 3.84k | uint8_t *copy = (uint8_t *)malloc(copyLen); |
57 | 3.84k | if (!copy) |
58 | 0 | return; |
59 | 3.84k | memcpy(copy + 2, data, dataLen); |
60 | 3.84k | write_zlib_header(copy, compression_method, flags); |
61 | | |
62 | 3.84k | uncompress(compr, &comprLen, copy, copyLen); |
63 | 3.84k | free(copy); |
64 | 3.84k | } |
65 | | |
66 | 3.84k | int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { |
67 | 3.84k | if (size < 10 || size > 1024 * 1024) |
68 | 6 | return 0; |
69 | | |
70 | 3.84k | const int level = d[0] % 10; |
71 | 3.84k | d++,size--; |
72 | | |
73 | | //https://web.archive.org/web/20200220015003/http://www.onicos.com/staff/iz/formats/gzip.html |
74 | 3.84k | unsigned compression_method = d[0] % 5; |
75 | 3.84k | if (compression_method == 4) //[4...7] are reserved |
76 | 310 | compression_method = 8; |
77 | 3.84k | d++,size--; |
78 | 3.84k | unsigned flags = d[0] & (2 << 4); |
79 | 3.84k | d++,size--; |
80 | | |
81 | 3.84k | size_t comprLen = compressBound(size); |
82 | 3.84k | size_t uncomprLen = size; |
83 | 3.84k | uint8_t *compr = NULL, *uncompr = NULL; |
84 | | |
85 | 3.84k | data = d; |
86 | 3.84k | dataLen = size; |
87 | 3.84k | compr = (uint8_t *)calloc(1, comprLen); |
88 | 3.84k | if (!compr) |
89 | 0 | goto err; |
90 | 3.84k | uncompr = (uint8_t *)calloc(1, uncomprLen); |
91 | 3.84k | if (!uncompr) |
92 | 0 | goto err; |
93 | | |
94 | 3.84k | check_compress_level(compr, comprLen, uncompr, uncomprLen, level); |
95 | 3.84k | check_decompress(compr, comprLen, compression_method, flags); |
96 | | |
97 | 3.84k | err: |
98 | 3.84k | free(compr); |
99 | 3.84k | free(uncompr); |
100 | | |
101 | | /* This function must return 0. */ |
102 | 3.84k | return 0; |
103 | 3.84k | } |