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 #if defined(_WIN32) && !defined(__CYGWIN__)
37 skipping("archive_write_disk security checks not supported on Windows");
38 #elif 0
39 struct archive *a;
40 struct archive_entry *ae;
41 size_t buff_size = 8192;
42 char *buff = malloc(buff_size);
43 char *p = buff;
44 int n = 0;
45 int t;
46
47 assert(buff != NULL);
48
49 /* Start with a known umask. */
50 assertUmask(UMASK);
51
52 /* Create an archive_write_disk object. */
53 assert((a = archive_write_disk_new()) != NULL);
54 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
55
56 while (p + 500 < buff + buff_size) {
57 memset(p, 'x', 100);
58 p += 100;
59 p[0] = '\0';
60
61 buff[0] = ((n / 1000) % 10) + '0';
62 buff[1] = ((n / 100) % 10)+ '0';
63 buff[2] = ((n / 10) % 10)+ '0';
64 buff[3] = ((n / 1) % 10)+ '0';
65 buff[4] = '_';
66 ++n;
67
68 /* Create a symlink pointing to the testworkdir */
69 assert((ae = archive_entry_new()) != NULL);
70 archive_entry_copy_pathname(ae, buff);
71 archive_entry_set_mode(ae, S_IFREG | 0777);
72 archive_entry_copy_symlink(ae, testworkdir);
73 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
74 archive_entry_free(ae);
75
76 *p++ = '/';
77 snprintf(p, buff_size - (p - buff), "target%d", n);
78
79 /* Try to create a file through the symlink, should fail. */
80 assert((ae = archive_entry_new()) != NULL);
81 archive_entry_copy_pathname(ae, buff);
82 archive_entry_set_mode(ae, S_IFDIR | 0777);
83
84 t = archive_write_header(a, ae);
85 archive_entry_free(ae);
86 failure("Attempt to create target%d via %d-character symlink should have failed", n, (int)strlen(buff));
87 if(!assertEqualInt(ARCHIVE_FAILED, t)) {
88 break;
89 }
90 }
91 archive_free(a);
92 free(buff);
93 #endif
94 }
95