test_fuzz.c revision 302001
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 302001 2016-06-17 22:40:10Z 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				rawimage = (char *)realloc(rawimage, oldsize + size);
114				memcpy(rawimage + oldsize, tmp, size);
115				oldsize += size;
116				size = oldsize;
117				free(tmp);
118				if (!assert(rawimage != NULL))
119					continue;
120			}
121		}
122		if (size == 0)
123			continue;
124		image = malloc(size);
125		assert(image != NULL);
126		if (image == NULL) {
127			free(rawimage);
128			return;
129		}
130		srand((unsigned)time(NULL));
131
132		for (i = 0; i < 1000; ++i) {
133			FILE *f;
134			int j, numbytes, trycnt;
135
136			/* Fuzz < 1% of the bytes in the archive. */
137			memcpy(image, rawimage, size);
138			q = (int)size / 100;
139			if (q < 4)
140				q = 4;
141			numbytes = (int)(rand() % q);
142			for (j = 0; j < numbytes; ++j)
143				image[rand() % size] = (char)rand();
144
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");
150				if (f != NULL)
151					break;
152#if defined(_WIN32) && !defined(__CYGWIN__)
153				/*
154				 * Sometimes previous close operation does not completely
155				 * end at this time. So we should take a wait while
156				 * the operation running.
157				 */
158				Sleep(100);
159#endif
160			}
161			assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
162			fclose(f);
163
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));
170
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))
175						continue;
176				}
177				archive_read_close(a);
178			}
179			archive_read_free(a);
180
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));
187
188			if (0 == archive_read_open_memory(a, image, size)) {
189				while(0 == archive_read_next_header(a, &ae)) {
190				}
191				archive_read_close(a);
192			}
193			archive_read_free(a);
194}
195		free(image);
196		free(rawimage);
197	}
198}
199
200DEFINE_TEST(test_fuzz_ar)
201{
202	static const char *fileset1[] = {
203		"test_read_format_ar.ar",
204		NULL
205	};
206	static const struct files filesets[] = {
207		{0, fileset1},
208		{1, NULL}
209	};
210	test_fuzz(filesets);
211}
212
213DEFINE_TEST(test_fuzz_cab)
214{
215	static const char *fileset1[] = {
216		"test_fuzz.cab",
217		NULL
218	};
219	static const struct files filesets[] = {
220		{0, fileset1},
221		{1, NULL}
222	};
223	test_fuzz(filesets);
224}
225
226DEFINE_TEST(test_fuzz_cpio)
227{
228	static const char *fileset1[] = {
229		"test_read_format_cpio_bin_be.cpio",
230		NULL
231	};
232	static const char *fileset2[] = {
233		"test_read_format_cpio_bin_le.cpio",
234		NULL
235	};
236	static const char *fileset3[] = {
237		/* Test RPM unwrapper */
238		"test_read_format_cpio_svr4_gzip_rpm.rpm",
239		NULL
240	};
241	static const struct files filesets[] = {
242		{0, fileset1},
243		{0, fileset2},
244		{0, fileset3},
245		{1, NULL}
246	};
247	test_fuzz(filesets);
248}
249
250DEFINE_TEST(test_fuzz_iso9660)
251{
252	static const char *fileset1[] = {
253		"test_fuzz_1.iso.Z",
254		NULL
255	};
256	static const struct files filesets[] = {
257		{0, fileset1}, /* Exercise compress decompressor. */
258		{1, fileset1},
259		{1, NULL}
260	};
261	test_fuzz(filesets);
262}
263
264DEFINE_TEST(test_fuzz_lzh)
265{
266	static const char *fileset1[] = {
267		"test_fuzz.lzh",
268		NULL
269	};
270	static const struct files filesets[] = {
271		{0, fileset1},
272		{1, NULL}
273	};
274	test_fuzz(filesets);
275}
276
277DEFINE_TEST(test_fuzz_mtree)
278{
279	static const char *fileset1[] = {
280		"test_read_format_mtree.mtree",
281		NULL
282	};
283	static const struct files filesets[] = {
284		{0, fileset1},
285		{1, NULL}
286	};
287	test_fuzz(filesets);
288}
289
290DEFINE_TEST(test_fuzz_rar)
291{
292	static const char *fileset1[] = {
293		/* Uncompressed RAR test */
294		"test_read_format_rar.rar",
295		NULL
296	};
297	static const char *fileset2[] = {
298		/* RAR file with binary data */
299		"test_read_format_rar_binary_data.rar",
300		NULL
301	};
302	static const char *fileset3[] = {
303		/* Best Compressed RAR test */
304		"test_read_format_rar_compress_best.rar",
305		NULL
306	};
307	static const char *fileset4[] = {
308		/* Normal Compressed RAR test */
309		"test_read_format_rar_compress_normal.rar",
310		NULL
311	};
312	static const char *fileset5[] = {
313		/* Normal Compressed Multi LZSS blocks RAR test */
314		"test_read_format_rar_multi_lzss_blocks.rar",
315		NULL
316	};
317	static const char *fileset6[] = {
318		/* RAR with no EOF header */
319		"test_read_format_rar_noeof.rar",
320		NULL
321	};
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",
325		NULL
326	};
327	static const char *fileset8[] = {
328		/* RAR with subblocks */
329		"test_read_format_rar_subblock.rar",
330		NULL
331	};
332	static const char *fileset9[] = {
333		/* RAR with Unicode filenames */
334		"test_read_format_rar_unicode.rar",
335		NULL
336	};
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",
342		NULL
343	};
344	static const struct files filesets[] = {
345		{0, fileset1},
346		{0, fileset2},
347		{0, fileset3},
348		{0, fileset4},
349		{0, fileset5},
350		{0, fileset6},
351		{0, fileset7},
352		{0, fileset8},
353		{0, fileset9},
354		{0, fileset10},
355		{1, NULL}
356	};
357	test_fuzz(filesets);
358}
359
360DEFINE_TEST(test_fuzz_tar)
361{
362	static const char *fileset1[] = {
363		"test_compat_bzip2_1.tbz",
364		NULL
365	};
366	static const char *fileset2[] = {
367		"test_compat_gtar_1.tar",
368		NULL
369	};
370	static const char *fileset3[] = {
371		"test_compat_gzip_1.tgz",
372		NULL
373	};
374	static const char *fileset4[] = {
375		"test_compat_gzip_2.tgz",
376		NULL
377	};
378	static const char *fileset5[] = {
379		"test_compat_tar_hardlink_1.tar",
380		NULL
381	};
382	static const char *fileset6[] = {
383		"test_compat_xz_1.txz",
384		NULL
385	};
386	static const char *fileset7[] = {
387		"test_read_format_gtar_sparse_1_17_posix10_modified.tar",
388		NULL
389	};
390	static const char *fileset8[] = {
391		"test_read_format_tar_empty_filename.tar",
392		NULL
393	};
394	static const char *fileset9[] = {
395		"test_compat_lzop_1.tar.lzo",
396		NULL
397	};
398	static const struct files filesets[] = {
399		{0, fileset1}, /* Exercise bzip2 decompressor. */
400		{1, fileset1},
401		{0, fileset2},
402		{0, fileset3}, /* Exercise gzip decompressor. */
403		{0, fileset4}, /* Exercise gzip decompressor. */
404		{0, fileset5},
405		{0, fileset6}, /* Exercise xz decompressor. */
406		{0, fileset7},
407		{0, fileset8},
408		{0, fileset9}, /* Exercise lzo decompressor. */
409		{1, NULL}
410	};
411	test_fuzz(filesets);
412}
413
414DEFINE_TEST(test_fuzz_zip)
415{
416	static const char *fileset1[] = {
417		"test_compat_zip_1.zip",
418		NULL
419	};
420	static const char *fileset2[] = {
421		"test_compat_zip_2.zip",
422		NULL
423	};
424	static const char *fileset3[] = {
425		"test_compat_zip_3.zip",
426		NULL
427	};
428	static const char *fileset4[] = {
429		"test_compat_zip_4.zip",
430		NULL
431	};
432	static const char *fileset5[] = {
433		"test_compat_zip_5.zip",
434		NULL
435	};
436	static const char *fileset6[] = {
437		"test_compat_zip_6.zip",
438		NULL
439	};
440	static const char *fileset7[] = {
441		"test_read_format_zip.zip",
442		NULL
443	};
444	static const char *fileset8[] = {
445		"test_read_format_zip_comment_stored_1.zip",
446		NULL
447	};
448	static const char *fileset9[] = {
449		"test_read_format_zip_comment_stored_2.zip",
450		NULL
451	};
452	static const char *fileset10[] = {
453		"test_read_format_zip_encryption_data.zip",
454		NULL
455	};
456	static const char *fileset11[] = {
457		"test_read_format_zip_encryption_header.zip",
458		NULL
459	};
460	static const char *fileset12[] = {
461		"test_read_format_zip_encryption_partially.zip",
462		NULL
463	};
464	static const char *fileset13[] = {
465		"test_read_format_zip_filename_cp866.zip",
466		NULL
467	};
468	static const char *fileset14[] = {
469		"test_read_format_zip_filename_cp932.zip",
470		NULL
471	};
472	static const char *fileset15[] = {
473		"test_read_format_zip_filename_koi8r.zip",
474		NULL
475	};
476	static const char *fileset16[] = {
477		"test_read_format_zip_filename_utf8_jp.zip",
478		NULL
479	};
480	static const char *fileset17[] = {
481		"test_read_format_zip_filename_utf8_ru.zip",
482		NULL
483	};
484	static const char *fileset18[] = {
485		"test_read_format_zip_filename_utf8_ru2.zip",
486		NULL
487	};
488	static const char *fileset19[] = {
489		"test_read_format_zip_length_at_end.zip",
490		NULL
491	};
492	static const char *fileset20[] = {
493		"test_read_format_zip_mac_metadata.zip",
494		NULL
495	};
496	static const char *fileset21[] = {
497		"test_read_format_zip_malformed1.zip",
498		NULL
499	};
500	static const char *fileset22[] = {
501		"test_read_format_zip_msdos.zip",
502		NULL
503	};
504	static const char *fileset23[] = {
505		"test_read_format_zip_nested.zip",
506		NULL
507	};
508	static const char *fileset24[] = {
509		"test_read_format_zip_nofiletype.zip",
510		NULL
511	};
512	static const char *fileset25[] = {
513		"test_read_format_zip_padded1.zip",
514		NULL
515	};
516	static const char *fileset26[] = {
517		"test_read_format_zip_padded2.zip",
518		NULL
519	};
520	static const char *fileset27[] = {
521		"test_read_format_zip_padded3.zip",
522		NULL
523	};
524	static const char *fileset28[] = {
525		"test_read_format_zip_symlink.zip",
526		NULL
527	};
528	static const char *fileset29[] = {
529		"test_read_format_zip_traditional_encryption_data.zip",
530		NULL
531	};
532	static const char *fileset30[] = {
533		"test_read_format_zip_ux.zip",
534		NULL
535	};
536	static const char *fileset31[] = {
537		"test_read_format_zip_winzip_aes128.zip",
538		NULL
539	};
540	static const char *fileset32[] = {
541		"test_read_format_zip_winzip_aes256.zip",
542		NULL
543	};
544	static const char *fileset33[] = {
545		"test_read_format_zip_winzip_aes256_large.zip",
546		NULL
547	};
548	static const char *fileset34[] = {
549		"test_read_format_zip_winzip_aes256_stored.zip",
550		NULL
551	};
552	static const char *fileset35[] = {
553		"test_read_format_zip_zip64a.zip",
554		NULL
555	};
556	static const char *fileset36[] = {
557		"test_read_format_zip_zip64b.zip",
558		NULL
559	};
560
561	static const struct files filesets[] = {
562		{0, fileset1},
563		{0, fileset2},
564		{0, fileset3},
565		{0, fileset4},
566		{0, fileset5},
567		{0, fileset6},
568		{0, fileset7},
569		{0, fileset8},
570		{0, fileset9},
571		{0, fileset10},
572		{0, fileset11},
573		{0, fileset12},
574		{0, fileset13},
575		{0, fileset14},
576		{0, fileset15},
577		{0, fileset16},
578		{0, fileset17},
579		{0, fileset18},
580		{0, fileset19},
581		{0, fileset20},
582		{0, fileset21},
583		{0, fileset22},
584		{0, fileset23},
585		{0, fileset24},
586		{0, fileset25},
587		{0, fileset26},
588		{0, fileset27},
589		{0, fileset28},
590		{0, fileset29},
591		{0, fileset30},
592		{0, fileset31},
593		{0, fileset32},
594		{0, fileset33},
595		{0, fileset34},
596		{0, fileset35},
597		{0, fileset36},
598		{1, NULL}
599	};
600	test_fuzz(filesets);
601}
602
603