Skip to content

Commit 0f4cbb8

Browse files
authored
Merge pull request #325 from tyler92/add-in-place-fuzzer
Add fuzzer for mz_zip_add_mem_to_archive_file_in_place function
2 parents 35528ad + efbf393 commit 0f4cbb8

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ if(BUILD_FUZZERS)
278278
set(SMALL_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/small_fuzzer.c")
279279
set(LARGE_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/large_fuzzer.c")
280280
set(ZIP_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/zip_fuzzer.c")
281+
set(ADD_IN_PLACE_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/add_in_place_fuzzer.c")
281282

282283
add_executable(checksum_fuzzer ${CHECKSUM_FUZZER_SRC_LIST})
283284
target_link_libraries(checksum_fuzzer miniz)
@@ -302,6 +303,9 @@ if(BUILD_FUZZERS)
302303

303304
add_executable(zip_fuzzer ${ZIP_FUZZER_SRC_LIST})
304305
target_link_libraries(zip_fuzzer miniz)
306+
307+
add_executable(add_in_place_fuzzer ${ADD_IN_PLACE_FUZZER_SRC_LIST})
308+
target_link_libraries(add_in_place_fuzzer miniz)
305309
endif()
306310

307311
if(BUILD_TESTS)

tests/add_in_place_fuzzer.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdio.h>
2+
#include <limits.h>
3+
#include "miniz.h"
4+
5+
static const mz_uint files_count = 5;
6+
static const mz_uint max_file_size = 1024 * 1024;
7+
static const char *zip_file_name = "/tmp/miniz-fuzzer-test.zip";
8+
9+
/* Read 32-bit integer from the fuzzer input with range [0, max] */
10+
static mz_uint read_uint32(const mz_uint8 **data, size_t *size, mz_uint max)
11+
{
12+
mz_uint value = 0;
13+
14+
if (*size >= sizeof(mz_uint))
15+
{
16+
memcpy(&value, *data, sizeof(mz_uint));
17+
*data += sizeof(mz_uint);
18+
*size -= sizeof(mz_uint);
19+
value = MZ_MIN(max == UINT_MAX ? value : value % (max + 1), *size);
20+
}
21+
22+
return value;
23+
}
24+
25+
/* Read random-length null terminated string from the fuzzer input */
26+
static mz_bool read_string(const mz_uint8 **data, size_t *size, char *destination, mz_uint max_len)
27+
{
28+
mz_uint filename_len = read_uint32(data, size, max_len - 1);
29+
memcpy(destination, *data, filename_len);
30+
destination[filename_len] = 0;
31+
*data += filename_len;
32+
*size -= filename_len;
33+
return filename_len > 0;
34+
}
35+
36+
/* Get random-length buffer from the fuzzer input */
37+
static mz_bool read_buffer(const mz_uint8 **data, size_t *size, const mz_uint8 **destination, mz_uint *len)
38+
{
39+
*len = read_uint32(data, size, max_file_size);
40+
*destination = *data;
41+
*data += *len;
42+
*size -= *len;
43+
return *len > 0;
44+
}
45+
46+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
47+
{
48+
mz_uint i;
49+
char archive_file_name[FILENAME_MAX];
50+
const mz_uint8 *file_data;
51+
mz_uint file_length, flags;
52+
size_t extracted_size;
53+
mz_uint8 *extracted_data;
54+
const char *comment = mz_version();
55+
56+
/* Remove the temporary file for better reproducibility */
57+
remove(zip_file_name);
58+
59+
for (i = 0; i < files_count; ++i)
60+
{
61+
/* Fill archive file name */
62+
if (!read_string(&data, &size, archive_file_name, sizeof(archive_file_name)))
63+
{
64+
break;
65+
}
66+
67+
/* Prepare file's content */
68+
if (!read_buffer(&data, &size, &file_data, &file_length))
69+
{
70+
break;
71+
}
72+
73+
/* Prepare flags for adding file */
74+
flags = read_uint32(&data, &size, UINT_MAX);
75+
76+
mz_zip_add_mem_to_archive_file_in_place(zip_file_name, archive_file_name, file_data, file_length, comment,
77+
(mz_uint16)strlen(comment), flags);
78+
79+
/* Prepare flags for extracting file */
80+
flags = read_uint32(&data, &size, UINT_MAX);
81+
extracted_data = mz_zip_extract_archive_file_to_heap(zip_file_name, archive_file_name, &extracted_size, flags);
82+
free(extracted_data);
83+
}
84+
85+
remove(zip_file_name);
86+
return 0;
87+
}

0 commit comments

Comments
 (0)