test_fuzz.c revision 305192
1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25#include "test.h"
26__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/test/test_fuzz.c 305192 2016-09-01 12:01:23Z mm $");
27
28/*
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
32 *
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
36 * with each file.
37 *
38 * Unlike most other tests, this test does not verify libarchive's
39 * responses other than to ensure that libarchive doesn't crash.
40 *
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.
45 */
46
47/* Because this works for any archive, we can just re-use the archives
48 * developed for other tests. */
49struct files {
50	int uncompress; /* If 1, decompress the file before fuzzing. */
51	const char **names;
52};
53
54static void
55test_fuzz(const struct files *filesets)
56{
57	const void *blk;
58	size_t blk_size;
59	int64_t blk_offset;
60	int n;
61
62	for (n = 0; filesets[n].names != NULL; ++n) {
63		const size_t buffsize = 30000000;
64		struct archive_entry *ae;
65		struct archive *a;
66		char *rawimage = NULL, *image = NULL, *tmp = NULL;
67		size_t size = 0, oldsize = 0;
68		int i, q;
69
70		extract_reference_files(filesets[n].names);
71		if (filesets[n].uncompress) {
72			int r;
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) {
81				archive_read_free(a);
82				if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
83					skipping("Cannot uncompress fileset");
84				} else {
85					skipping("Cannot uncompress %s", filesets[n].names[0]);
86				}
87				continue;
88			}
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));
97			assert(size > 0);
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");
101			} else {
102				failure("Internal buffer is not big enough for "
103					"uncompressed test file: %s", filesets[n].names[0]);
104			}
105			if (!assert(size < buffsize)) {
106				free(rawimage);
107				continue;
108			}
109		} else {
110			for (i = 0; filesets[n].names[i] != NULL; ++i)
111			{
112				tmp = slurpfile(&size, filesets[n].names[i]);
113				char *newraw = (char *)realloc(rawimage, oldsize + size);
114				if (!assert(newraw != NULL))
115				{
116					free(rawimage);
117					continue;
118				}
119				rawimage = newraw;
120				memcpy(rawimage + oldsize, tmp, size);
121				oldsize += size;
122				size = oldsize;
123				free(tmp);
124			}
125		}
126		if (size == 0)
127			continue;
128		image = malloc(size);
129		assert(image != NULL);
130		if (image == NULL) {
131			free(rawimage);
132			return;
133		}
134		srand((unsigned)time(NULL));
135
136		for (i = 0; i < 1000; ++i) {
137			FILE *f;
138			int j, numbytes, trycnt;
139
140			/* Fuzz < 1% of the bytes in the archive. */
141			memcpy(image, rawimage, size);
142			q = (int)size / 100;
143			if (q < 4)
144				q = 4;
145			numbytes = (int)(rand() % q);
146			for (j = 0; j < numbytes; ++j)
147				image[rand() % size] = (char)rand();
148
149			/* Save the messed-up image to a file.
150			 * If we crash, that file will be useful. */
151			for (trycnt = 0; trycnt < 3; trycnt++) {
152				f = fopen("after.test.failure.send.this.file."
153				    "to.libarchive.maintainers.with.system.details", "wb");
154				if (f != NULL)
155					break;
156#if defined(_WIN32) && !defined(__CYGWIN__)
157				/*
158				 * Sometimes previous close operation does not completely
159				 * end at this time. So we should take a wait while
160				 * the operation running.
161				 */
162				Sleep(100);
163#endif
164			}
165			assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
166			fclose(f);
167
168			// Try to read all headers and bodies.
169			assert((a = archive_read_new()) != NULL);
170			assertEqualIntA(a, ARCHIVE_OK,
171			    archive_read_support_filter_all(a));
172			assertEqualIntA(a, ARCHIVE_OK,
173			    archive_read_support_format_all(a));
174
175			if (0 == archive_read_open_memory(a, image, size)) {
176				while(0 == archive_read_next_header(a, &ae)) {
177					while (0 == archive_read_data_block(a,
178						&blk, &blk_size, &blk_offset))
179						continue;
180				}
181				archive_read_close(a);
182			}
183			archive_read_free(a);
184
185			// Just list headers, skip bodies.
186			assert((a = archive_read_new()) != NULL);
187			assertEqualIntA(a, ARCHIVE_OK,
188			    archive_read_support_filter_all(a));
189			assertEqualIntA(a, ARCHIVE_OK,
190			    archive_read_support_format_all(a));
191
192			if (0 == archive_read_open_memory(a, image, size)) {
193				while(0 == archive_read_next_header(a, &ae)) {
194				}
195				archive_read_close(a);
196			}
197			archive_read_free(a);
198}
199		free(image);
200		free(rawimage);
201	}
202}
203
204DEFINE_TEST(test_fuzz_ar)
205{
206	static const char *fileset1[] = {
207		"test_read_format_ar.ar",
208		NULL
209	};
210	static const struct files filesets[] = {
211		{0, fileset1},
212		{1, NULL}
213	};
214	test_fuzz(filesets);
215}
216
217DEFINE_TEST(test_fuzz_cab)
218{
219	static const char *fileset1[] = {
220		"test_fuzz.cab",
221		NULL
222	};
223	static const struct files filesets[] = {
224		{0, fileset1},
225		{1, NULL}
226	};
227	test_fuzz(filesets);
228}
229
230DEFINE_TEST(test_fuzz_cpio)
231{
232	static const char *fileset1[] = {
233		"test_read_format_cpio_bin_be.cpio",
234		NULL
235	};
236	static const char *fileset2[] = {
237		"test_read_format_cpio_bin_le.cpio",
238		NULL
239	};
240	static const char *fileset3[] = {
241		/* Test RPM unwrapper */
242		"test_read_format_cpio_svr4_gzip_rpm.rpm",
243		NULL
244	};
245	static const struct files filesets[] = {
246		{0, fileset1},
247		{0, fileset2},
248		{0, fileset3},
249		{1, NULL}
250	};
251	test_fuzz(filesets);
252}
253
254DEFINE_TEST(test_fuzz_iso9660)
255{
256	static const char *fileset1[] = {
257		"test_fuzz_1.iso.Z",
258		NULL
259	};
260	static const struct files filesets[] = {
261		{0, fileset1}, /* Exercise compress decompressor. */
262		{1, fileset1},
263		{1, NULL}
264	};
265	test_fuzz(filesets);
266}
267
268DEFINE_TEST(test_fuzz_lzh)
269{
270	static const char *fileset1[] = {
271		"test_fuzz.lzh",
272		NULL
273	};
274	static const struct files filesets[] = {
275		{0, fileset1},
276		{1, NULL}
277	};
278	test_fuzz(filesets);
279}
280
281DEFINE_TEST(test_fuzz_mtree)
282{
283	static const char *fileset1[] = {
284		"test_read_format_mtree.mtree",
285		NULL
286	};
287	static const struct files filesets[] = {
288		{0, fileset1},
289		{1, NULL}
290	};
291	test_fuzz(filesets);
292}
293
294DEFINE_TEST(test_fuzz_rar)
295{
296	static const char *fileset1[] = {
297		/* Uncompressed RAR test */
298		"test_read_format_rar.rar",
299		NULL
300	};
301	static const char *fileset2[] = {
302		/* RAR file with binary data */
303		"test_read_format_rar_binary_data.rar",
304		NULL
305	};
306	static const char *fileset3[] = {
307		/* Best Compressed RAR test */
308		"test_read_format_rar_compress_best.rar",
309		NULL
310	};
311	static const char *fileset4[] = {
312		/* Normal Compressed RAR test */
313		"test_read_format_rar_compress_normal.rar",
314		NULL
315	};
316	static const char *fileset5[] = {
317		/* Normal Compressed Multi LZSS blocks RAR test */
318		"test_read_format_rar_multi_lzss_blocks.rar",
319		NULL
320	};
321	static const char *fileset6[] = {
322		/* RAR with no EOF header */
323		"test_read_format_rar_noeof.rar",
324		NULL
325	};
326	static const char *fileset7[] = {
327		/* Best Compressed RAR file with both PPMd and LZSS blocks */
328		"test_read_format_rar_ppmd_lzss_conversion.rar",
329		NULL
330	};
331	static const char *fileset8[] = {
332		/* RAR with subblocks */
333		"test_read_format_rar_subblock.rar",
334		NULL
335	};
336	static const char *fileset9[] = {
337		/* RAR with Unicode filenames */
338		"test_read_format_rar_unicode.rar",
339		NULL
340	};
341	static const char *fileset10[] = {
342		"test_read_format_rar_multivolume.part0001.rar",
343		"test_read_format_rar_multivolume.part0002.rar",
344		"test_read_format_rar_multivolume.part0003.rar",
345		"test_read_format_rar_multivolume.part0004.rar",
346		NULL
347	};
348	static const struct files filesets[] = {
349		{0, fileset1},
350		{0, fileset2},
351		{0, fileset3},
352		{0, fileset4},
353		{0, fileset5},
354		{0, fileset6},
355		{0, fileset7},
356		{0, fileset8},
357		{0, fileset9},
358		{0, fileset10},
359		{1, NULL}
360	};
361	test_fuzz(filesets);
362}
363
364DEFINE_TEST(test_fuzz_tar)
365{
366	static const char *fileset1[] = {
367		"test_compat_bzip2_1.tbz",
368		NULL
369	};
370	static const char *fileset2[] = {
371		"test_compat_gtar_1.tar",
372		NULL
373	};
374	static const char *fileset3[] = {
375		"test_compat_gzip_1.tgz",
376		NULL
377	};
378	static const char *fileset4[] = {
379		"test_compat_gzip_2.tgz",
380		NULL
381	};
382	static const char *fileset5[] = {
383		"test_compat_tar_hardlink_1.tar",
384		NULL
385	};
386	static const char *fileset6[] = {
387		"test_compat_xz_1.txz",
388		NULL
389	};
390	static const char *fileset7[] = {
391		"test_read_format_gtar_sparse_1_17_posix10_modified.tar",
392		NULL
393	};
394	static const char *fileset8[] = {
395		"test_read_format_tar_empty_filename.tar",
396		NULL
397	};
398	static const char *fileset9[] = {
399		"test_compat_lzop_1.tar.lzo",
400		NULL
401	};
402	static const struct files filesets[] = {
403		{0, fileset1}, /* Exercise bzip2 decompressor. */
404		{1, fileset1},
405		{0, fileset2},
406		{0, fileset3}, /* Exercise gzip decompressor. */
407		{0, fileset4}, /* Exercise gzip decompressor. */
408		{0, fileset5},
409		{0, fileset6}, /* Exercise xz decompressor. */
410		{0, fileset7},
411		{0, fileset8},
412		{0, fileset9}, /* Exercise lzo decompressor. */
413		{1, NULL}
414	};
415	test_fuzz(filesets);
416}
417
418DEFINE_TEST(test_fuzz_zip)
419{
420	static const char *fileset1[] = {
421		"test_compat_zip_1.zip",
422		NULL
423	};
424	static const char *fileset2[] = {
425		"test_compat_zip_2.zip",
426		NULL
427	};
428	static const char *fileset3[] = {
429		"test_compat_zip_3.zip",
430		NULL
431	};
432	static const char *fileset4[] = {
433		"test_compat_zip_4.zip",
434		NULL
435	};
436	static const char *fileset5[] = {
437		"test_compat_zip_5.zip",
438		NULL
439	};
440	static const char *fileset6[] = {
441		"test_compat_zip_6.zip",
442		NULL
443	};
444	static const char *fileset7[] = {
445		"test_read_format_zip.zip",
446		NULL
447	};
448	static const char *fileset8[] = {
449		"test_read_format_zip_comment_stored_1.zip",
450		NULL
451	};
452	static const char *fileset9[] = {
453		"test_read_format_zip_comment_stored_2.zip",
454		NULL
455	};
456	static const char *fileset10[] = {
457		"test_read_format_zip_encryption_data.zip",
458		NULL
459	};
460	static const char *fileset11[] = {
461		"test_read_format_zip_encryption_header.zip",
462		NULL
463	};
464	static const char *fileset12[] = {
465		"test_read_format_zip_encryption_partially.zip",
466		NULL
467	};
468	static const char *fileset13[] = {
469		"test_read_format_zip_filename_cp866.zip",
470		NULL
471	};
472	static const char *fileset14[] = {
473		"test_read_format_zip_filename_cp932.zip",
474		NULL
475	};
476	static const char *fileset15[] = {
477		"test_read_format_zip_filename_koi8r.zip",
478		NULL
479	};
480	static const char *fileset16[] = {
481		"test_read_format_zip_filename_utf8_jp.zip",
482		NULL
483	};
484	static const char *fileset17[] = {
485		"test_read_format_zip_filename_utf8_ru.zip",
486		NULL
487	};
488	static const char *fileset18[] = {
489		"test_read_format_zip_filename_utf8_ru2.zip",
490		NULL
491	};
492	static const char *fileset19[] = {
493		"test_read_format_zip_length_at_end.zip",
494		NULL
495	};
496	static const char *fileset20[] = {
497		"test_read_format_zip_mac_metadata.zip",
498		NULL
499	};
500	static const char *fileset21[] = {
501		"test_read_format_zip_malformed1.zip",
502		NULL
503	};
504	static const char *fileset22[] = {
505		"test_read_format_zip_msdos.zip",
506		NULL
507	};
508	static const char *fileset23[] = {
509		"test_read_format_zip_nested.zip",
510		NULL
511	};
512	static const char *fileset24[] = {
513		"test_read_format_zip_nofiletype.zip",
514		NULL
515	};
516	static const char *fileset25[] = {
517		"test_read_format_zip_padded1.zip",
518		NULL
519	};
520	static const char *fileset26[] = {
521		"test_read_format_zip_padded2.zip",
522		NULL
523	};
524	static const char *fileset27[] = {
525		"test_read_format_zip_padded3.zip",
526		NULL
527	};
528	static const char *fileset28[] = {
529		"test_read_format_zip_symlink.zip",
530		NULL
531	};
532	static const char *fileset29[] = {
533		"test_read_format_zip_traditional_encryption_data.zip",
534		NULL
535	};
536	static const char *fileset30[] = {
537		"test_read_format_zip_ux.zip",
538		NULL
539	};
540	static const char *fileset31[] = {
541		"test_read_format_zip_winzip_aes128.zip",
542		NULL
543	};
544	static const char *fileset32[] = {
545		"test_read_format_zip_winzip_aes256.zip",
546		NULL
547	};
548	static const char *fileset33[] = {
549		"test_read_format_zip_winzip_aes256_large.zip",
550		NULL
551	};
552	static const char *fileset34[] = {
553		"test_read_format_zip_winzip_aes256_stored.zip",
554		NULL
555	};
556	static const char *fileset35[] = {
557		"test_read_format_zip_zip64a.zip",
558		NULL
559	};
560	static const char *fileset36[] = {
561		"test_read_format_zip_zip64b.zip",
562		NULL
563	};
564
565	static const struct files filesets[] = {
566		{0, fileset1},
567		{0, fileset2},
568		{0, fileset3},
569		{0, fileset4},
570		{0, fileset5},
571		{0, fileset6},
572		{0, fileset7},
573		{0, fileset8},
574		{0, fileset9},
575		{0, fileset10},
576		{0, fileset11},
577		{0, fileset12},
578		{0, fileset13},
579		{0, fileset14},
580		{0, fileset15},
581		{0, fileset16},
582		{0, fileset17},
583		{0, fileset18},
584		{0, fileset19},
585		{0, fileset20},
586		{0, fileset21},
587		{0, fileset22},
588		{0, fileset23},
589		{0, fileset24},
590		{0, fileset25},
591		{0, fileset26},
592		{0, fileset27},
593		{0, fileset28},
594		{0, fileset29},
595		{0, fileset30},
596		{0, fileset31},
597		{0, fileset32},
598		{0, fileset33},
599		{0, fileset34},
600		{0, fileset35},
601		{0, fileset36},
602		{1, NULL}
603	};
604	test_fuzz(filesets);
605}
606
607