2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 __FBSDID("$FreeBSD$");
29 * This was inspired by an ISO fuzz tester written by Michal Zalewski
30 * and posted to the "vulnwatch" mailing list on March 17, 2005:
31 * http://seclists.org/vulnwatch/2005/q1/0088.html
33 * This test simply reads each archive image into memory, pokes
34 * random values into it and runs it through libarchive. It tries
35 * to damage about 1% of each file and repeats the exercise 100 times
38 * Unlike most other tests, this test does not verify libarchive's
39 * responses other than to ensure that libarchive doesn't crash.
41 * Due to the deliberately random nature of this test, it may be hard
42 * to reproduce failures. Because this test deliberately attempts to
43 * induce crashes, there's little that can be done in the way of
44 * post-failure diagnostics.
47 /* Because this works for any archive, we can just re-use the archives
48 * developed for other tests. */
50 int uncompress; /* If 1, decompress the file before fuzzing. */
55 test_fuzz(const struct files *filesets)
62 for (n = 0; filesets[n].names != NULL; ++n) {
63 const size_t buffsize = 30000000;
64 struct archive_entry *ae;
66 char *rawimage = NULL, *image = NULL, *tmp = NULL;
67 size_t size = 0, oldsize = 0;
70 extract_reference_files(filesets[n].names);
71 if (filesets[n].uncompress) {
73 /* Use format_raw to decompress the data. */
74 assert((a = archive_read_new()) != NULL);
75 assertEqualIntA(a, ARCHIVE_OK,
76 archive_read_support_filter_all(a));
77 assertEqualIntA(a, ARCHIVE_OK,
78 archive_read_support_format_raw(a));
79 r = archive_read_open_filenames(a, filesets[n].names, 16384);
80 if (r != ARCHIVE_OK) {
82 if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
83 skipping("Cannot uncompress fileset");
85 skipping("Cannot uncompress %s", filesets[n].names[0]);
89 assertEqualIntA(a, ARCHIVE_OK,
90 archive_read_next_header(a, &ae));
91 rawimage = malloc(buffsize);
92 size = archive_read_data(a, rawimage, buffsize);
93 assertEqualIntA(a, ARCHIVE_EOF,
94 archive_read_next_header(a, &ae));
95 assertEqualInt(ARCHIVE_OK,
96 archive_read_free(a));
98 if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
99 failure("Internal buffer is not big enough for "
100 "uncompressed test files");
102 failure("Internal buffer is not big enough for "
103 "uncompressed test file: %s", filesets[n].names[0]);
105 if (!assert(size < buffsize)) {
110 for (i = 0; filesets[n].names[i] != NULL; ++i)
112 tmp = slurpfile(&size, filesets[n].names[i]);
113 rawimage = (char *)realloc(rawimage, oldsize + size);
114 memcpy(rawimage + oldsize, tmp, size);
118 if (!assert(rawimage != NULL))
124 image = malloc(size);
125 assert(image != NULL);
130 srand((unsigned)time(NULL));
132 for (i = 0; i < 1000; ++i) {
134 int j, numbytes, trycnt;
136 /* Fuzz < 1% of the bytes in the archive. */
137 memcpy(image, rawimage, size);
141 numbytes = (int)(rand() % q);
142 for (j = 0; j < numbytes; ++j)
143 image[rand() % size] = (char)rand();
145 /* Save the messed-up image to a file.
146 * If we crash, that file will be useful. */
147 for (trycnt = 0; trycnt < 3; trycnt++) {
148 f = fopen("after.test.failure.send.this.file."
149 "to.libarchive.maintainers.with.system.details", "wb");
152 #if defined(_WIN32) && !defined(__CYGWIN__)
154 * Sometimes previous close operation does not completely
155 * end at this time. So we should take a wait while
156 * the operation running.
161 assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
164 // Try to read all headers and bodies.
165 assert((a = archive_read_new()) != NULL);
166 assertEqualIntA(a, ARCHIVE_OK,
167 archive_read_support_filter_all(a));
168 assertEqualIntA(a, ARCHIVE_OK,
169 archive_read_support_format_all(a));
171 if (0 == archive_read_open_memory(a, image, size)) {
172 while(0 == archive_read_next_header(a, &ae)) {
173 while (0 == archive_read_data_block(a,
174 &blk, &blk_size, &blk_offset))
177 archive_read_close(a);
179 archive_read_free(a);
181 // Just list headers, skip bodies.
182 assert((a = archive_read_new()) != NULL);
183 assertEqualIntA(a, ARCHIVE_OK,
184 archive_read_support_filter_all(a));
185 assertEqualIntA(a, ARCHIVE_OK,
186 archive_read_support_format_all(a));
188 if (0 == archive_read_open_memory(a, image, size)) {
189 while(0 == archive_read_next_header(a, &ae)) {
191 archive_read_close(a);
193 archive_read_free(a);
200 DEFINE_TEST(test_fuzz_ar)
202 static const char *fileset1[] = {
203 "test_read_format_ar.ar",
206 static const struct files filesets[] = {
213 DEFINE_TEST(test_fuzz_cab)
215 static const char *fileset1[] = {
219 static const struct files filesets[] = {
226 DEFINE_TEST(test_fuzz_cpio)
228 static const char *fileset1[] = {
229 "test_read_format_cpio_bin_be.cpio",
232 static const char *fileset2[] = {
233 "test_read_format_cpio_bin_le.cpio",
236 static const char *fileset3[] = {
237 /* Test RPM unwrapper */
238 "test_read_format_cpio_svr4_gzip_rpm.rpm",
241 static const struct files filesets[] = {
250 DEFINE_TEST(test_fuzz_iso9660)
252 static const char *fileset1[] = {
256 static const struct files filesets[] = {
257 {0, fileset1}, /* Exercise compress decompressor. */
264 DEFINE_TEST(test_fuzz_lzh)
266 static const char *fileset1[] = {
270 static const struct files filesets[] = {
277 DEFINE_TEST(test_fuzz_mtree)
279 static const char *fileset1[] = {
280 "test_read_format_mtree.mtree",
283 static const struct files filesets[] = {
290 DEFINE_TEST(test_fuzz_rar)
292 static const char *fileset1[] = {
293 /* Uncompressed RAR test */
294 "test_read_format_rar.rar",
297 static const char *fileset2[] = {
298 /* RAR file with binary data */
299 "test_read_format_rar_binary_data.rar",
302 static const char *fileset3[] = {
303 /* Best Compressed RAR test */
304 "test_read_format_rar_compress_best.rar",
307 static const char *fileset4[] = {
308 /* Normal Compressed RAR test */
309 "test_read_format_rar_compress_normal.rar",
312 static const char *fileset5[] = {
313 /* Normal Compressed Multi LZSS blocks RAR test */
314 "test_read_format_rar_multi_lzss_blocks.rar",
317 static const char *fileset6[] = {
318 /* RAR with no EOF header */
319 "test_read_format_rar_noeof.rar",
322 static const char *fileset7[] = {
323 /* Best Compressed RAR file with both PPMd and LZSS blocks */
324 "test_read_format_rar_ppmd_lzss_conversion.rar",
327 static const char *fileset8[] = {
328 /* RAR with subblocks */
329 "test_read_format_rar_subblock.rar",
332 static const char *fileset9[] = {
333 /* RAR with Unicode filenames */
334 "test_read_format_rar_unicode.rar",
337 static const char *fileset10[] = {
338 "test_read_format_rar_multivolume.part0001.rar",
339 "test_read_format_rar_multivolume.part0002.rar",
340 "test_read_format_rar_multivolume.part0003.rar",
341 "test_read_format_rar_multivolume.part0004.rar",
344 static const struct files filesets[] = {
360 DEFINE_TEST(test_fuzz_tar)
362 static const char *fileset1[] = {
363 "test_compat_bzip2_1.tbz",
366 static const char *fileset2[] = {
367 "test_compat_gtar_1.tar",
370 static const char *fileset3[] = {
371 "test_compat_gzip_1.tgz",
374 static const char *fileset4[] = {
375 "test_compat_gzip_2.tgz",
378 static const char *fileset5[] = {
379 "test_compat_tar_hardlink_1.tar",
382 static const char *fileset6[] = {
383 "test_compat_xz_1.txz",
386 static const char *fileset7[] = {
387 "test_read_format_gtar_sparse_1_17_posix10_modified.tar",
390 static const char *fileset8[] = {
391 "test_read_format_tar_empty_filename.tar",
394 static const char *fileset9[] = {
395 "test_compat_lzop_1.tar.lzo",
398 static const struct files filesets[] = {
399 {0, fileset1}, /* Exercise bzip2 decompressor. */
402 {0, fileset3}, /* Exercise gzip decompressor. */
403 {0, fileset4}, /* Exercise gzip decompressor. */
405 {0, fileset6}, /* Exercise xz decompressor. */
408 {0, fileset9}, /* Exercise lzo decompressor. */
414 DEFINE_TEST(test_fuzz_zip)
416 static const char *fileset1[] = {
417 "test_compat_zip_1.zip",
420 static const char *fileset2[] = {
421 "test_compat_zip_2.zip",
424 static const char *fileset3[] = {
425 "test_compat_zip_3.zip",
428 static const char *fileset4[] = {
429 "test_compat_zip_4.zip",
432 static const char *fileset5[] = {
433 "test_compat_zip_5.zip",
436 static const char *fileset6[] = {
437 "test_compat_zip_6.zip",
440 static const char *fileset7[] = {
441 "test_read_format_zip.zip",
444 static const char *fileset8[] = {
445 "test_read_format_zip_comment_stored_1.zip",
448 static const char *fileset9[] = {
449 "test_read_format_zip_comment_stored_2.zip",
452 static const char *fileset10[] = {
453 "test_read_format_zip_encryption_data.zip",
456 static const char *fileset11[] = {
457 "test_read_format_zip_encryption_header.zip",
460 static const char *fileset12[] = {
461 "test_read_format_zip_encryption_partially.zip",
464 static const char *fileset13[] = {
465 "test_read_format_zip_filename_cp866.zip",
468 static const char *fileset14[] = {
469 "test_read_format_zip_filename_cp932.zip",
472 static const char *fileset15[] = {
473 "test_read_format_zip_filename_koi8r.zip",
476 static const char *fileset16[] = {
477 "test_read_format_zip_filename_utf8_jp.zip",
480 static const char *fileset17[] = {
481 "test_read_format_zip_filename_utf8_ru.zip",
484 static const char *fileset18[] = {
485 "test_read_format_zip_filename_utf8_ru2.zip",
488 static const char *fileset19[] = {
489 "test_read_format_zip_length_at_end.zip",
492 static const char *fileset20[] = {
493 "test_read_format_zip_mac_metadata.zip",
496 static const char *fileset21[] = {
497 "test_read_format_zip_malformed1.zip",
500 static const char *fileset22[] = {
501 "test_read_format_zip_msdos.zip",
504 static const char *fileset23[] = {
505 "test_read_format_zip_nested.zip",
508 static const char *fileset24[] = {
509 "test_read_format_zip_nofiletype.zip",
512 static const char *fileset25[] = {
513 "test_read_format_zip_padded1.zip",
516 static const char *fileset26[] = {
517 "test_read_format_zip_padded2.zip",
520 static const char *fileset27[] = {
521 "test_read_format_zip_padded3.zip",
524 static const char *fileset28[] = {
525 "test_read_format_zip_symlink.zip",
528 static const char *fileset29[] = {
529 "test_read_format_zip_traditional_encryption_data.zip",
532 static const char *fileset30[] = {
533 "test_read_format_zip_ux.zip",
536 static const char *fileset31[] = {
537 "test_read_format_zip_winzip_aes128.zip",
540 static const char *fileset32[] = {
541 "test_read_format_zip_winzip_aes256.zip",
544 static const char *fileset33[] = {
545 "test_read_format_zip_winzip_aes256_large.zip",
548 static const char *fileset34[] = {
549 "test_read_format_zip_winzip_aes256_stored.zip",
552 static const char *fileset35[] = {
553 "test_read_format_zip_zip64a.zip",
556 static const char *fileset36[] = {
557 "test_read_format_zip_zip64b.zip",
561 static const struct files filesets[] = {