/src/lz4/ossfuzz/file_fuzzer.c
Line | Count | Source |
1 | | /** |
2 | | * This fuzz target exercises the lz4 file API (lib/lz4file.c) end to end. |
3 | | * |
4 | | * Phase 1 feeds arbitrary, possibly corrupt or truncated input to the read |
5 | | * side: LZ4F_readOpen() / LZ4F_read() / LZ4F_seek() / LZ4F_tell() driven by an |
6 | | * operation script taken from the fuzzed data. Errors are expected on |
7 | | * malformed input; crashes are not. |
8 | | * |
9 | | * Phase 2 round trips the fuzzed data through the write side and back: the |
10 | | * bytes written with LZ4F_writeOpen() / LZ4F_write() / LZ4F_writeClose() must |
11 | | * decode back through the read side to exactly the original data. |
12 | | */ |
13 | | |
14 | | #define _POSIX_C_SOURCE 200809L /* fmemopen, open_memstream */ |
15 | | |
16 | | #include <stddef.h> |
17 | | #include <stdint.h> |
18 | | #include <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <string.h> |
21 | | |
22 | | #include "fuzz_helpers.h" |
23 | | #include "fuzz_data_producer.h" |
24 | | #define LZ4F_STATIC_LINKING_ONLY |
25 | | #include "lz4frame.h" |
26 | | #include "lz4file.h" |
27 | | |
28 | 15.7k | #define FILE_FUZZ_MAX_OPS 16 |
29 | 39.0k | #define FILE_FUZZ_BUF_CAP (64 * 1024) |
30 | | |
31 | | typedef struct { |
32 | | unsigned kind; /* 0 : read, 1 : seek, 2 : tell */ |
33 | | size_t want; /* read size */ |
34 | | long long offset; /* seek offset */ |
35 | | int origin; /* seek origin */ |
36 | | } file_op_t; |
37 | | |
38 | | /* runs one scripted operation; returns 0 if the file state became unusable |
39 | | * (read error on malformed input), meaning the remaining ops are moot */ |
40 | | static int runFileOp(LZ4_readFile_t* rf, uint8_t* buf, const file_op_t* op) |
41 | 16.3k | { |
42 | 16.3k | if (op->kind == 0) { |
43 | 4.05k | size_t const got = LZ4F_read(rf, buf, op->want); |
44 | 4.05k | return !LZ4F_isError(got); |
45 | 4.05k | } |
46 | 12.3k | if (op->kind == 1) { |
47 | | /* errors are expected: seeking past the end or before the start */ |
48 | 9.18k | (void)LZ4F_seek(rf, op->offset, op->origin); |
49 | 9.18k | return 1; |
50 | 9.18k | } |
51 | 3.15k | (void)LZ4F_tell(rf); |
52 | 3.15k | return 1; |
53 | 12.3k | } |
54 | | |
55 | | /* drives the read side over @fp, which holds arbitrary attacker-controlled |
56 | | * bytes; every LZ4F_* error return is acceptable here, crashes are not */ |
57 | | static void readFileOps(FILE* fp, const file_op_t* ops, unsigned nbOps) |
58 | 15.6k | { |
59 | 15.6k | LZ4_readFile_t* rf = NULL; |
60 | 15.6k | uint8_t* buf; |
61 | 15.6k | unsigned op; |
62 | | |
63 | 15.6k | if (LZ4F_isError(LZ4F_readOpen(&rf, fp))) |
64 | 11.9k | return; |
65 | | |
66 | 3.71k | buf = (uint8_t*)malloc(FILE_FUZZ_BUF_CAP); |
67 | 3.71k | FUZZ_ASSERT(buf); |
68 | | |
69 | 17.8k | for (op = 0; op < nbOps; op++) { |
70 | 16.3k | if (!runFileOp(rf, buf, &ops[op])) |
71 | 2.29k | break; |
72 | 16.3k | } |
73 | | |
74 | 3.71k | free(buf); |
75 | 3.71k | FUZZ_ASSERT(!LZ4F_isError(LZ4F_readClose(rf))); |
76 | 3.71k | } |
77 | | |
78 | | /* runs the op script on a valid frame of known content @data : |
79 | | * seeks land within it, and every read must return the original bytes */ |
80 | | static void checkFileOps(LZ4_readFile_t* rf, const uint8_t* data, size_t size, |
81 | | const file_op_t* ops, unsigned nbOps) |
82 | 15.6k | { |
83 | 15.6k | uint8_t* const buf = (uint8_t*)malloc(FILE_FUZZ_BUF_CAP); |
84 | 15.6k | unsigned long long pos = LZ4F_tell(rf); |
85 | 15.6k | unsigned op; |
86 | | |
87 | 15.6k | FUZZ_ASSERT(buf); |
88 | 69.5k | for (op = 0; op < nbOps; op++) { |
89 | 53.8k | if (ops[op].kind == 0) { |
90 | 19.3k | size_t const expected = MIN(ops[op].want, (size_t)(size - pos)); |
91 | 19.3k | FUZZ_ASSERT(LZ4F_read(rf, buf, ops[op].want) == expected); |
92 | 19.3k | FUZZ_ASSERT(memcmp(buf, data + pos, expected) == 0); |
93 | 19.3k | pos += expected; |
94 | 34.4k | } else if (ops[op].kind == 1) { |
95 | 18.6k | long long const magnitude = (ops[op].offset < 0) ? -ops[op].offset : ops[op].offset; |
96 | 18.6k | unsigned long long const target = (unsigned long long)magnitude % (size + 1); |
97 | 18.6k | long long const offset = (ops[op].origin == SEEK_SET) ? (long long)target |
98 | 18.6k | : (long long)target - (long long)pos; |
99 | 18.6k | FUZZ_ASSERT(!LZ4F_isError(LZ4F_seek(rf, offset, ops[op].origin))); |
100 | 18.6k | pos = target; |
101 | 18.6k | } |
102 | 53.8k | FUZZ_ASSERT(LZ4F_tell(rf) == pos); |
103 | 53.8k | } |
104 | 15.6k | free(buf); |
105 | 15.6k | } |
106 | | |
107 | | /* writes @data through the lz4file write API and reads it back through the |
108 | | * read API; the round trip must reproduce the original bytes exactly */ |
109 | | static void writeReadRoundTrip(const LZ4F_preferences_t* prefs, |
110 | | const uint8_t* data, size_t size, |
111 | | const file_op_t* ops, unsigned nbOps) |
112 | 15.6k | { |
113 | 15.6k | char* compressed = NULL; |
114 | 15.6k | size_t compressedSize = 0; |
115 | 15.6k | FILE* const wfp = open_memstream(&compressed, &compressedSize); |
116 | 15.6k | FILE* rfp; |
117 | 15.6k | LZ4_writeFile_t* wf = NULL; |
118 | 15.6k | LZ4_readFile_t* rf; |
119 | 15.6k | uint8_t* back; |
120 | 15.6k | size_t first; |
121 | 15.6k | size_t total; |
122 | | |
123 | 15.6k | FUZZ_ASSERT(wfp); |
124 | | |
125 | 15.6k | if (!LZ4F_isError(LZ4F_writeOpen(&wf, wfp, prefs))) { |
126 | | /* split in two writes to exercise the internal buffering */ |
127 | 15.6k | first = size / 2; |
128 | 15.6k | if (!LZ4F_isError(LZ4F_write(wf, data, first))) |
129 | 15.6k | (void)LZ4F_write(wf, data + first, size - first); |
130 | 15.6k | FUZZ_ASSERT(!LZ4F_isError(LZ4F_writeClose(wf))); |
131 | 15.6k | } |
132 | | |
133 | 15.6k | fclose(wfp); /* flushes and finalizes compressed/compressedSize */ |
134 | 15.6k | FUZZ_ASSERT(compressed != NULL); |
135 | 15.6k | FUZZ_ASSERT(compressedSize > 0); |
136 | | |
137 | 15.6k | rfp = fmemopen(compressed, compressedSize, "rb"); |
138 | 15.6k | FUZZ_ASSERT(rfp); |
139 | | |
140 | 15.6k | FUZZ_ASSERT(!LZ4F_isError(LZ4F_readOpen(&rf, rfp))); |
141 | 15.6k | back = (uint8_t*)malloc(size); |
142 | 15.6k | FUZZ_ASSERT(back); |
143 | 15.6k | total = 0; |
144 | 31.3k | while (total < size) { |
145 | 15.6k | size_t const got = LZ4F_read(rf, back + total, size - total); |
146 | | /* the frame was just written by this process: it must decode */ |
147 | 15.6k | FUZZ_ASSERT(!LZ4F_isError(got)); |
148 | 15.6k | if (got == 0) |
149 | 0 | break; |
150 | 15.6k | total += got; |
151 | 15.6k | } |
152 | 15.6k | FUZZ_ASSERT_MSG(total == size, "lz4file round trip truncated the data"); |
153 | 15.6k | FUZZ_ASSERT_MSG(memcmp(back, data, size) == 0, |
154 | 15.6k | "lz4file round trip corrupted the data"); |
155 | 15.6k | checkFileOps(rf, data, size, ops, nbOps); |
156 | 15.6k | free(back); |
157 | 15.6k | FUZZ_ASSERT(!LZ4F_isError(LZ4F_readClose(rf))); |
158 | | |
159 | 15.6k | fclose(rfp); |
160 | 15.6k | free(compressed); |
161 | 15.6k | } |
162 | | |
163 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
164 | 15.7k | { |
165 | 15.7k | FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); |
166 | | |
167 | | /* consume the op script and preferences first; the front remainder of the |
168 | | * input is the file contents both phases operate on */ |
169 | 15.7k | LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); |
170 | 15.7k | file_op_t ops[FILE_FUZZ_MAX_OPS]; |
171 | 15.7k | unsigned const nbOps = FUZZ_dataProducer_range32(producer, 1, FILE_FUZZ_MAX_OPS); |
172 | 15.7k | unsigned op; |
173 | | |
174 | 15.7k | memset(ops, 0, sizeof(ops)); |
175 | 69.9k | for (op = 0; op < nbOps; op++) { |
176 | 54.2k | ops[op].kind = FUZZ_dataProducer_range32(producer, 0, 2); |
177 | 54.2k | if (ops[op].kind == 0) { |
178 | 19.6k | ops[op].want = FUZZ_dataProducer_range32(producer, 0, FILE_FUZZ_BUF_CAP); |
179 | 34.5k | } else if (ops[op].kind == 1) { |
180 | 18.6k | long long const forward = |
181 | 18.6k | (long long)FUZZ_dataProducer_range32(producer, 0, 1u << 20); |
182 | 18.6k | long long const backward = |
183 | 18.6k | (long long)FUZZ_dataProducer_range32(producer, 0, 1u << 20); |
184 | 18.6k | ops[op].offset = forward - backward; |
185 | 18.6k | ops[op].origin = (FUZZ_dataProducer_range32(producer, 0, 1) == 0) |
186 | 18.6k | ? SEEK_SET : SEEK_CUR; |
187 | 18.6k | } |
188 | 54.2k | } |
189 | | |
190 | 15.7k | size = FUZZ_dataProducer_remainingBytes(producer); |
191 | | |
192 | 15.7k | if (size > 0) { |
193 | 15.6k | FILE* const fp = fmemopen((void*)data, size, "rb"); |
194 | 15.6k | FUZZ_ASSERT(fp); |
195 | 15.6k | readFileOps(fp, ops, nbOps); |
196 | 15.6k | fclose(fp); |
197 | | |
198 | 15.6k | writeReadRoundTrip(&prefs, data, size, ops, nbOps); |
199 | 15.6k | } |
200 | | |
201 | 15.7k | FUZZ_dataProducer_free(producer); |
202 | 15.7k | return 0; |
203 | 15.7k | } |