test_write_disk_secure744.c revision 305192
1/*-
2 * Copyright (c) 2003-2007,2016 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_write_disk_secure744.c 305192 2016-09-01 12:01:23Z mm $");
27
28#define UMASK 022
29
30/*
31 * Github Issue #744 describes a bug in the sandboxing code that
32 * causes very long pathnames to not get checked for symlinks.
33 */
34
35DEFINE_TEST(test_write_disk_secure744)
36{
37#if defined(_WIN32) && !defined(__CYGWIN__)
38	skipping("archive_write_disk security checks not supported on Windows");
39#else
40	struct archive *a;
41	struct archive_entry *ae;
42	size_t buff_size = 8192;
43	char *buff = malloc(buff_size);
44	char *p = buff;
45	int n = 0;
46	int t;
47
48	assert(buff != NULL);
49
50	/* Start with a known umask. */
51	assertUmask(UMASK);
52
53	/* Create an archive_write_disk object. */
54	assert((a = archive_write_disk_new()) != NULL);
55	archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
56
57	while (p + 500 < buff + buff_size) {
58		memset(p, 'x', 100);
59		p += 100;
60		p[0] = '\0';
61
62		buff[0] = ((n / 1000) % 10) + '0';
63		buff[1] = ((n / 100) % 10)+ '0';
64		buff[2] = ((n / 10) % 10)+ '0';
65		buff[3] = ((n / 1) % 10)+ '0';
66		buff[4] = '_';
67		++n;
68
69		/* Create a symlink pointing to the testworkdir */
70		assert((ae = archive_entry_new()) != NULL);
71		archive_entry_copy_pathname(ae, buff);
72		archive_entry_set_mode(ae, S_IFREG | 0777);
73		archive_entry_copy_symlink(ae, testworkdir);
74		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
75		archive_entry_free(ae);
76
77		*p++ = '/';
78		sprintf(p, "target%d", n);
79
80		/* Try to create a file through the symlink, should fail. */
81		assert((ae = archive_entry_new()) != NULL);
82		archive_entry_copy_pathname(ae, buff);
83		archive_entry_set_mode(ae, S_IFDIR | 0777);
84
85		t = archive_write_header(a, ae);
86		archive_entry_free(ae);
87		failure("Attempt to create target%d via %d-character symlink should have failed", n, (int)strlen(buff));
88		if(!assertEqualInt(ARCHIVE_FAILED, t)) {
89			break;
90		}
91	}
92	archive_free(a);
93	free(buff);
94#endif
95}
96