aboutsummaryrefslogtreecommitdiffstats
path: root/libarchive/test/test_fuzz.c
blob: 42351450b26530ed2f43310b679de83480204415 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*-
 * Copyright (c) 2003-2007 Tim Kientzle
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "test.h"
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_fuzz.c 201247 2009-12-30 05:59:21Z kientzle $");

/*
 * This was inspired by an ISO fuzz tester written by Michal Zalewski
 * and posted to the "vulnwatch" mailing list on March 17, 2005:
 *    http://seclists.org/vulnwatch/2005/q1/0088.html
 *
 * This test simply reads each archive image into memory, pokes
 * random values into it and runs it through libarchive.  It tries
 * to damage about 1% of each file and repeats the exercise 100 times
 * with each file.
 *
 * Unlike most other tests, this test does not verify libarchive's
 * responses other than to ensure that libarchive doesn't crash.
 *
 * Due to the deliberately random nature of this test, it may be hard
 * to reproduce failures.  Because this test deliberately attempts to
 * induce crashes, there's little that can be done in the way of
 * post-failure diagnostics.
 */

/* Because this works for any archive, we can just re-use the archives
 * developed for other tests. */
struct files {
	int uncompress; /* If 1, decompress the file before fuzzing. */
	const char **names;
};

static void
test_fuzz(const struct files *filesets)
{
	const void *blk;
	size_t blk_size;
	int64_t blk_offset;
	int n;

	for (n = 0; filesets[n].names != NULL; ++n) {
		const size_t buffsize = 30000000;
		struct archive_entry *ae;
		struct archive *a;
		char *rawimage = NULL, *image = NULL, *tmp = NULL;
		size_t size = 0, oldsize = 0;
		int i, q;

		extract_reference_files(filesets[n].names);
		if (filesets[n].uncompress) {
			int r;
			/* Use format_raw to decompress the data. */
			assert((a = archive_read_new()) != NULL);
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_filter_all(a));
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_format_raw(a));
			r = archive_read_open_filenames(a, filesets[n].names, 16384);
			if (r != ARCHIVE_OK) {
				archive_read_free(a);
				if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
					skipping("Cannot uncompress fileset");
				} else {
					skipping("Cannot uncompress %s", filesets[n].names[0]);
				}
				continue;
			}
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_next_header(a, &ae));
			rawimage = malloc(buffsize);
			size = archive_read_data(a, rawimage, buffsize);
			assertEqualIntA(a, ARCHIVE_EOF,
			    archive_read_next_header(a, &ae));
			assertEqualInt(ARCHIVE_OK,
			    archive_read_free(a));
			assert(size > 0);
			if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
				failure("Internal buffer is not big enough for "
					"uncompressed test files");
			} else {
				failure("Internal buffer is not big enough for "
					"uncompressed test file: %s", filesets[n].names[0]);
			}
			if (!assert(size < buffsize)) {
				free(rawimage);
				rawimage = NULL;
				continue;
			}
		} else {
			for (i = 0; filesets[n].names[i] != NULL; ++i)
			{
				char *newraw;
				tmp = slurpfile(&size, filesets[n].names[i]);
				newraw = realloc(rawimage, oldsize + size);
				if (!assert(newraw != NULL))
				{
					free(rawimage);
					rawimage = NULL;
					free(tmp);
					continue;
				}
				rawimage = newraw;
				memcpy(rawimage + oldsize, tmp, size);
				oldsize += size;
				size = oldsize;
				free(tmp);
			}
		}
		if (size == 0) {
			free(rawimage);
			rawimage = NULL;
			continue;
		}
		image = malloc(size);
		assert(image != NULL);
		if (image == NULL) {
			free(rawimage);
			rawimage = NULL;
			return;
		}

		assert(rawimage != NULL);

		srand((unsigned)time(NULL));

		for (i = 0; i < 1000; ++i) {
			FILE *f;
			int j, numbytes, trycnt;

			/* Fuzz < 1% of the bytes in the archive. */
			memcpy(image, rawimage, size);
			q = (int)size / 100;
			if (q < 4)
				q = 4;
			numbytes = (int)(rand() % q);
			for (j = 0; j < numbytes; ++j)
				image[rand() % size] = (char)rand();

			/* Save the messed-up image to a file.
			 * If we crash, that file will be useful. */
			for (trycnt = 0; trycnt < 3; trycnt++) {
				f = fopen("after.test.failure.send.this.file."
				    "to.libarchive.maintainers.with.system.details", "wb");
				if (f != NULL)
					break;
#if defined(_WIN32) && !defined(__CYGWIN__)
				/*
				 * Sometimes previous close operation does not completely
				 * end at this time. So we should take a wait while
				 * the operation running.
				 */
				Sleep(100);
#endif
			}
			assert(f != NULL);
			assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
			fclose(f);

			// Try to read all headers and bodies.
			assert((a = archive_read_new()) != NULL);
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_filter_all(a));
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_format_all(a));

			if (0 == archive_read_open_memory(a, image, size)) {
				while(0 == archive_read_next_header(a, &ae)) {
					while (0 == archive_read_data_block(a,
						&blk, &blk_size, &blk_offset))
						continue;
				}
				archive_read_close(a);
			}
			archive_read_free(a);

			// Just list headers, skip bodies.
			assert((a = archive_read_new()) != NULL);
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_filter_all(a));
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_format_all(a));

			if (0 == archive_read_open_memory(a, image, size)) {
				while(0 == archive_read_next_header(a, &ae)) {
				}
				archive_read_close(a);
			}
			archive_read_free(a);
		}
		free(image);
		free(rawimage);
	}
}

DEFINE_TEST(test_fuzz_ar)
{
	static const char *fileset1[] = {
		"test_read_format_ar.ar",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_cab)
{
	static const char *fileset1[] = {
		"test_fuzz.cab",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_cpio)
{
	static const char *fileset1[] = {
		"test_read_format_cpio_bin_be.cpio",
		NULL
	};
	static const char *fileset2[] = {
		"test_read_format_cpio_bin_le.cpio",
		NULL
	};
	static const char *fileset3[] = {
		/* Test RPM unwrapper */
		"test_read_format_cpio_svr4_gzip_rpm.rpm",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1},
		{0, fileset2},
		{0, fileset3},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_iso9660)
{
	static const char *fileset1[] = {
		"test_fuzz_1.iso.Z",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1}, /* Exercise compress decompressor. */
		{1, fileset1},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_lzh)
{
	static const char *fileset1[] = {
		"test_fuzz.lzh",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_mtree)
{
	static const char *fileset1[] = {
		"test_read_format_mtree.mtree",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_rar)
{
	static const char *fileset1[] = {
		/* Uncompressed RAR test */
		"test_read_format_rar.rar",
		NULL
	};
	static const char *fileset2[] = {
		/* RAR file with binary data */
		"test_read_format_rar_binary_data.rar",
		NULL
	};
	static const char *fileset3[] = {
		/* Best Compressed RAR test */
		"test_read_format_rar_compress_best.rar",
		NULL
	};
	static const char *fileset4[] = {
		/* Normal Compressed RAR test */
		"test_read_format_rar_compress_normal.rar",
		NULL
	};
	static const char *fileset5[] = {
		/* Normal Compressed Multi LZSS blocks RAR test */
		"test_read_format_rar_multi_lzss_blocks.rar",
		NULL
	};
	static const char *fileset6[] = {
		/* RAR with no EOF header */
		"test_read_format_rar_noeof.rar",
		NULL
	};
	static const char *fileset7[] = {
		/* Best Compressed RAR file with both PPMd and LZSS blocks */
		"test_read_format_rar_ppmd_lzss_conversion.rar",
		NULL
	};
	static const char *fileset8[] = {
		/* RAR with subblocks */
		"test_read_format_rar_subblock.rar",
		NULL
	};
	static const char *fileset9[] = {
		/* RAR with Unicode filenames */
		"test_read_format_rar_unicode.rar",
		NULL
	};
	static const char *fileset10[] = {
		"test_read_format_rar_multivolume.part0001.rar",
		"test_read_format_rar_multivolume.part0002.rar",
		"test_read_format_rar_multivolume.part0003.rar",
		"test_read_format_rar_multivolume.part0004.rar",
		NULL
	};
	static const struct files filesets[] = {
		{0, fileset1},
		{0, fileset2},
		{0, fileset3},
		{0, fileset4},
		{0, fileset5},
		{0, fileset6},
		{0, fileset7},
		{0, fileset8},
		{0, fileset9},
		{0, fileset10},
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_tar)
{
	static const char *fileset1[] = {
		"test_compat_bzip2_1.tbz",
		NULL
	};
	static const char *fileset2[] = {
		"test_compat_gtar_1.tar",
		NULL
	};
	static const char *fileset3[] = {
		"test_compat_gzip_1.tgz",
		NULL
	};
	static const char *fileset4[] = {
		"test_compat_gzip_2.tgz",
		NULL
	};
	static const char *fileset5[] = {
		"test_compat_tar_hardlink_1.tar",
		NULL
	};
	static const char *fileset6[] = {
		"test_compat_xz_1.txz",
		NULL
	};
	static const char *fileset7[] = {
		"test_read_format_gtar_sparse_1_17_posix10_modified.tar",
		NULL
	};
	static const char *fileset8[] = {
		"test_read_format_tar_empty_filename.tar",
		NULL
	};
#if HAVE_LIBLZO2 && HAVE_LZO_LZO1X_H && HAVE_LZO_LZOCONF_H
	static const char *fileset9[] = {
		"test_compat_lzop_1.tar.lzo",
		NULL
	};
#endif
#if HAVE_ZSTD_H && HAVE_LIBZSTD
	static const char *fileset10[] = {
		"test_compat_zstd_1.tar.zst",
		NULL
	};
#endif
	static const struct files filesets[] = {
		{0, fileset1}, /* Exercise bzip2 decompressor. */
		{1, fileset1},
		{0, fileset2},
		{0, fileset3}, /* Exercise gzip decompressor. */
		{0, fileset4}, /* Exercise gzip decompressor. */
		{0, fileset5},
		{0, fileset6}, /* Exercise xz decompressor. */
		{0, fileset7},
		{0, fileset8},
#if HAVE_LIBLZO2 && HAVE_LZO_LZO1X_H && HAVE_LZO_LZOCONF_H
		{0, fileset9}, /* Exercise lzo decompressor. */
#endif
#if HAVE_ZSTD_H && HAVE_LIBZSTD
		{0, fileset10}, /* Excercise zstd decompressor. */
#endif
		{1, NULL}
	};
	test_fuzz(filesets);
}

DEFINE_TEST(test_fuzz_zip)
{
	static const char *fileset1[] = {
		"test_compat_zip_1.zip",
		NULL
	};
	static const char *fileset2[] = {
		"test_compat_zip_2.zip",
		NULL
	};
	static const char *fileset3[] = {
		"test_compat_zip_3.zip",
		NULL
	};
	static const char *fileset4[] = {
		"test_compat_zip_4.zip",
		NULL
	};
	static const char *fileset5[] = {
		"test_compat_zip_5.zip",
		NULL
	};
	static const char *fileset6[] = {
		"test_compat_zip_6.zip",
		NULL
	};
	static const char *fileset7[] = {
		"test_read_format_zip.zip",
		NULL
	};
	static const char *fileset8[] = {
		"test_read_format_zip_comment_stored_1.zip",
		NULL
	};
	static const char *fileset9[] = {
		"test_read_format_zip_comment_stored_2.zip",
		NULL
	};
	static const char *fileset10[] = {
		"test_read_format_zip_encryption_data.zip",
		NULL
	};
	static const char *fileset11[] = {
		"test_read_format_zip_encryption_header.zip",
		NULL
	};
	static const char *fileset12[] = {
		"test_read_format_zip_encryption_partially.zip",
		NULL
	};
	static const char *fileset13[] = {
		"test_read_format_zip_filename_cp866.zip",
		NULL
	};
	static const char *fileset14[] = {
		"test_read_format_zip_filename_cp932.zip",
		NULL
	};
	static const char *fileset15[] = {
		"test_read_format_zip_filename_koi8r.zip",
		NULL
	};
	static const char *fileset16[] = {
		"test_read_format_zip_filename_utf8_jp.zip",
		NULL
	};
	static const char *fileset17[] = {
		"test_read_format_zip_filename_utf8_ru.zip",
		NULL
	};
	static const char *fileset18[] = {
		"test_read_format_zip_filename_utf8_ru2.zip",
		NULL
	};
	static const char *fileset19[] = {
		"test_read_format_zip_length_at_end.zip",
		NULL
	};
	static const char *fileset20[] = {
		"test_read_format_zip_mac_metadata.zip",
		NULL
	};
	static const char *fileset21[] = {
		"test_read_format_zip_malformed1.zip",
		NULL
	};
	static const char *fileset22[] = {
		"test_read_format_zip_msdos.zip",
		NULL
	};
	static const char *fileset23[] = {
		"test_read_format_zip_nested.zip",
		NULL
	};
	static const char *fileset24[] = {
		"test_read_format_zip_nofiletype.zip",
		NULL
	};
	static const char *fileset25[] = {
		"test_read_format_zip_padded1.zip",
		NULL
	};
	static const char *fileset26[] = {
		"test_read_format_zip_padded2.zip",
		NULL
	};
	static const char *fileset27[] = {
		"test_read_format_zip_padded3.zip",
		NULL
	};
	static const char *fileset28[] = {
		"test_read_format_zip_symlink.zip",
		NULL
	};
	static const char *fileset29[] = {
		"test_read_format_zip_traditional_encryption_data.zip",
		NULL
	};
	static const char *fileset30[] = {
		"test_read_format_zip_ux.zip",
		NULL
	};
	static const char *fileset31[] = {
		"test_read_format_zip_winzip_aes128.zip",
		NULL
	};
	static const char *fileset32[] = {
		"test_read_format_zip_winzip_aes256.zip",
		NULL
	};
	static const char *fileset33[] = {
		"test_read_format_zip_winzip_aes256_large.zip",
		NULL
	};
	static const char *fileset34[] = {
		"test_read_format_zip_winzip_aes256_stored.zip",
		NULL
	};
	static const char *fileset35[] = {
		"test_read_format_zip_zip64a.zip",
		NULL
	};
	static const char *fileset36[] = {
		"test_read_format_zip_zip64b.zip",
		NULL
	};

	static const struct files filesets[] = {
		{0, fileset1},
		{0, fileset2},
		{0, fileset3},
		{0, fileset4},
		{0, fileset5},
		{0, fileset6},
		{0, fileset7},
		{0, fileset8},
		{0, fileset9},
		{0, fileset10},
		{0, fileset11},
		{0, fileset12},
		{0, fileset13},
		{0, fileset14},
		{0, fileset15},
		{0, fileset16},
		{0, fileset17},
		{0, fileset18},
		{0, fileset19},
		{0, fileset20},
		{0, fileset21},
		{0, fileset22},
		{0, fileset23},
		{0, fileset24},
		{0, fileset25},
		{0, fileset26},
		{0, fileset27},
		{0, fileset28},
		{0, fileset29},
		{0, fileset30},
		{0, fileset31},
		{0, fileset32},
		{0, fileset33},
		{0, fileset34},
		{0, fileset35},
		{0, fileset36},
		{1, NULL}
	};
	test_fuzz(filesets);
}