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