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)) {
111 for (i = 0; filesets[n].names[i] != NULL; ++i)
113 tmp = slurpfile(&size, filesets[n].names[i]);
114 char *newraw = realloc(rawimage, oldsize + size);
115 if (!assert(newraw != NULL))
123 memcpy(rawimage + oldsize, tmp, size);
134 image = malloc(size);
135 assert(image != NULL);
142 assert(rawimage != NULL);
144 srand((unsigned)time(NULL));
146 for (i = 0; i < 1000; ++i) {
148 int j, numbytes, trycnt;
150 /* Fuzz < 1% of the bytes in the archive. */
151 memcpy(image, rawimage, size);
155 numbytes = (int)(rand() % q);
156 for (j = 0; j < numbytes; ++j)
157 image[rand() % size] = (char)rand();
159 /* Save the messed-up image to a file.
160 * If we crash, that file will be useful. */
161 for (trycnt = 0; trycnt < 3; trycnt++) {
162 f = fopen("after.test.failure.send.this.file."
163 "to.libarchive.maintainers.with.system.details", "wb");
166 #if defined(_WIN32) && !defined(__CYGWIN__)
168 * Sometimes previous close operation does not completely
169 * end at this time. So we should take a wait while
170 * the operation running.
176 assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
179 // Try to read all headers and bodies.
180 assert((a = archive_read_new()) != NULL);
181 assertEqualIntA(a, ARCHIVE_OK,
182 archive_read_support_filter_all(a));
183 assertEqualIntA(a, ARCHIVE_OK,
184 archive_read_support_format_all(a));
186 if (0 == archive_read_open_memory(a, image, size)) {
187 while(0 == archive_read_next_header(a, &ae)) {
188 while (0 == archive_read_data_block(a,
189 &blk, &blk_size, &blk_offset))
192 archive_read_close(a);
194 archive_read_free(a);
196 // Just list headers, skip bodies.
197 assert((a = archive_read_new()) != NULL);
198 assertEqualIntA(a, ARCHIVE_OK,
199 archive_read_support_filter_all(a));
200 assertEqualIntA(a, ARCHIVE_OK,
201 archive_read_support_format_all(a));
203 if (0 == archive_read_open_memory(a, image, size)) {
204 while(0 == archive_read_next_header(a, &ae)) {
206 archive_read_close(a);
208 archive_read_free(a);
215 DEFINE_TEST(test_fuzz_ar)
217 static const char *fileset1[] = {
218 "test_read_format_ar.ar",
221 static const struct files filesets[] = {
228 DEFINE_TEST(test_fuzz_cab)
230 static const char *fileset1[] = {
234 static const struct files filesets[] = {
241 DEFINE_TEST(test_fuzz_cpio)
243 static const char *fileset1[] = {
244 "test_read_format_cpio_bin_be.cpio",
247 static const char *fileset2[] = {
248 "test_read_format_cpio_bin_le.cpio",
251 static const char *fileset3[] = {
252 /* Test RPM unwrapper */
253 "test_read_format_cpio_svr4_gzip_rpm.rpm",
256 static const struct files filesets[] = {
265 DEFINE_TEST(test_fuzz_iso9660)
267 static const char *fileset1[] = {
271 static const struct files filesets[] = {
272 {0, fileset1}, /* Exercise compress decompressor. */
279 DEFINE_TEST(test_fuzz_lzh)
281 static const char *fileset1[] = {
285 static const struct files filesets[] = {
292 DEFINE_TEST(test_fuzz_mtree)
294 static const char *fileset1[] = {
295 "test_read_format_mtree.mtree",
298 static const struct files filesets[] = {
305 DEFINE_TEST(test_fuzz_rar)
307 static const char *fileset1[] = {
308 /* Uncompressed RAR test */
309 "test_read_format_rar.rar",
312 static const char *fileset2[] = {
313 /* RAR file with binary data */
314 "test_read_format_rar_binary_data.rar",
317 static const char *fileset3[] = {
318 /* Best Compressed RAR test */
319 "test_read_format_rar_compress_best.rar",
322 static const char *fileset4[] = {
323 /* Normal Compressed RAR test */
324 "test_read_format_rar_compress_normal.rar",
327 static const char *fileset5[] = {
328 /* Normal Compressed Multi LZSS blocks RAR test */
329 "test_read_format_rar_multi_lzss_blocks.rar",
332 static const char *fileset6[] = {
333 /* RAR with no EOF header */
334 "test_read_format_rar_noeof.rar",
337 static const char *fileset7[] = {
338 /* Best Compressed RAR file with both PPMd and LZSS blocks */
339 "test_read_format_rar_ppmd_lzss_conversion.rar",
342 static const char *fileset8[] = {
343 /* RAR with subblocks */
344 "test_read_format_rar_subblock.rar",
347 static const char *fileset9[] = {
348 /* RAR with Unicode filenames */
349 "test_read_format_rar_unicode.rar",
352 static const char *fileset10[] = {
353 "test_read_format_rar_multivolume.part0001.rar",
354 "test_read_format_rar_multivolume.part0002.rar",
355 "test_read_format_rar_multivolume.part0003.rar",
356 "test_read_format_rar_multivolume.part0004.rar",
359 static const struct files filesets[] = {
375 DEFINE_TEST(test_fuzz_tar)
377 static const char *fileset1[] = {
378 "test_compat_bzip2_1.tbz",
381 static const char *fileset2[] = {
382 "test_compat_gtar_1.tar",
385 static const char *fileset3[] = {
386 "test_compat_gzip_1.tgz",
389 static const char *fileset4[] = {
390 "test_compat_gzip_2.tgz",
393 static const char *fileset5[] = {
394 "test_compat_tar_hardlink_1.tar",
397 static const char *fileset6[] = {
398 "test_compat_xz_1.txz",
401 static const char *fileset7[] = {
402 "test_read_format_gtar_sparse_1_17_posix10_modified.tar",
405 static const char *fileset8[] = {
406 "test_read_format_tar_empty_filename.tar",
409 static const char *fileset9[] = {
410 "test_compat_lzop_1.tar.lzo",
413 static const struct files filesets[] = {
414 {0, fileset1}, /* Exercise bzip2 decompressor. */
417 {0, fileset3}, /* Exercise gzip decompressor. */
418 {0, fileset4}, /* Exercise gzip decompressor. */
420 {0, fileset6}, /* Exercise xz decompressor. */
423 {0, fileset9}, /* Exercise lzo decompressor. */
429 DEFINE_TEST(test_fuzz_zip)
431 static const char *fileset1[] = {
432 "test_compat_zip_1.zip",
435 static const char *fileset2[] = {
436 "test_compat_zip_2.zip",
439 static const char *fileset3[] = {
440 "test_compat_zip_3.zip",
443 static const char *fileset4[] = {
444 "test_compat_zip_4.zip",
447 static const char *fileset5[] = {
448 "test_compat_zip_5.zip",
451 static const char *fileset6[] = {
452 "test_compat_zip_6.zip",
455 static const char *fileset7[] = {
456 "test_read_format_zip.zip",
459 static const char *fileset8[] = {
460 "test_read_format_zip_comment_stored_1.zip",
463 static const char *fileset9[] = {
464 "test_read_format_zip_comment_stored_2.zip",
467 static const char *fileset10[] = {
468 "test_read_format_zip_encryption_data.zip",
471 static const char *fileset11[] = {
472 "test_read_format_zip_encryption_header.zip",
475 static const char *fileset12[] = {
476 "test_read_format_zip_encryption_partially.zip",
479 static const char *fileset13[] = {
480 "test_read_format_zip_filename_cp866.zip",
483 static const char *fileset14[] = {
484 "test_read_format_zip_filename_cp932.zip",
487 static const char *fileset15[] = {
488 "test_read_format_zip_filename_koi8r.zip",
491 static const char *fileset16[] = {
492 "test_read_format_zip_filename_utf8_jp.zip",
495 static const char *fileset17[] = {
496 "test_read_format_zip_filename_utf8_ru.zip",
499 static const char *fileset18[] = {
500 "test_read_format_zip_filename_utf8_ru2.zip",
503 static const char *fileset19[] = {
504 "test_read_format_zip_length_at_end.zip",
507 static const char *fileset20[] = {
508 "test_read_format_zip_mac_metadata.zip",
511 static const char *fileset21[] = {
512 "test_read_format_zip_malformed1.zip",
515 static const char *fileset22[] = {
516 "test_read_format_zip_msdos.zip",
519 static const char *fileset23[] = {
520 "test_read_format_zip_nested.zip",
523 static const char *fileset24[] = {
524 "test_read_format_zip_nofiletype.zip",
527 static const char *fileset25[] = {
528 "test_read_format_zip_padded1.zip",
531 static const char *fileset26[] = {
532 "test_read_format_zip_padded2.zip",
535 static const char *fileset27[] = {
536 "test_read_format_zip_padded3.zip",
539 static const char *fileset28[] = {
540 "test_read_format_zip_symlink.zip",
543 static const char *fileset29[] = {
544 "test_read_format_zip_traditional_encryption_data.zip",
547 static const char *fileset30[] = {
548 "test_read_format_zip_ux.zip",
551 static const char *fileset31[] = {
552 "test_read_format_zip_winzip_aes128.zip",
555 static const char *fileset32[] = {
556 "test_read_format_zip_winzip_aes256.zip",
559 static const char *fileset33[] = {
560 "test_read_format_zip_winzip_aes256_large.zip",
563 static const char *fileset34[] = {
564 "test_read_format_zip_winzip_aes256_stored.zip",
567 static const char *fileset35[] = {
568 "test_read_format_zip_zip64a.zip",
571 static const char *fileset36[] = {
572 "test_read_format_zip_zip64b.zip",
576 static const struct files filesets[] = {