2 * Copyright (c) 2003-2007,2016 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
31 * Github Issue #746 describes a problem in which hardlink targets are
32 * not adequately checked and can be used to modify entries outside of
37 * Verify that ARCHIVE_EXTRACT_SECURE_NODOTDOT disallows '..' in hardlink
40 DEFINE_TEST(test_write_disk_secure746a)
42 #if defined(_WIN32) && !defined(__CYGWIN__)
43 skipping("archive_write_disk security checks not supported on Windows");
46 struct archive_entry *ae;
48 /* Start with a known umask. */
51 /* The target directory we're going to try to affect. */
52 assertMakeDir("target", 0700);
53 assertMakeFile("target/foo", 0700, "unmodified");
55 /* The sandbox dir we're going to work within. */
56 assertMakeDir("sandbox", 0700);
57 assertChdir("sandbox");
59 /* Create an archive_write_disk object. */
60 assert((a = archive_write_disk_new()) != NULL);
61 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_NODOTDOT);
63 /* Attempt to hardlink to the target directory. */
64 assert((ae = archive_entry_new()) != NULL);
65 archive_entry_copy_pathname(ae, "bar");
66 archive_entry_set_mode(ae, AE_IFREG | 0777);
67 archive_entry_set_size(ae, 8);
68 archive_entry_copy_hardlink(ae, "../target/foo");
69 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
70 assertEqualInt(ARCHIVE_FATAL, archive_write_data(a, "modified", 8));
71 archive_entry_free(ae);
73 /* Verify that target file contents are unchanged. */
74 assertTextFileContents("unmodified", "../target/foo");
76 assertEqualIntA(a, ARCHIVE_FATAL, archive_write_close(a));
77 archive_write_free(a);
82 * Verify that ARCHIVE_EXTRACT_SECURE_NOSYMLINK disallows symlinks in hardlink
85 DEFINE_TEST(test_write_disk_secure746b)
87 #if defined(_WIN32) && !defined(__CYGWIN__)
88 skipping("archive_write_disk security checks not supported on Windows");
91 struct archive_entry *ae;
93 /* Start with a known umask. */
96 /* The target directory we're going to try to affect. */
97 assertMakeDir("target", 0700);
98 assertMakeFile("target/foo", 0700, "unmodified");
100 /* The sandbox dir we're going to work within. */
101 assertMakeDir("sandbox", 0700);
102 assertChdir("sandbox");
104 /* Create an archive_write_disk object. */
105 assert((a = archive_write_disk_new()) != NULL);
106 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
108 /* Create a symlink to the target directory. */
109 assert((ae = archive_entry_new()) != NULL);
110 archive_entry_copy_pathname(ae, "symlink");
111 archive_entry_set_mode(ae, AE_IFLNK | 0777);
112 archive_entry_copy_symlink(ae, "../target");
113 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
114 archive_entry_free(ae);
116 /* Attempt to hardlink to the target directory via the symlink. */
117 assert((ae = archive_entry_new()) != NULL);
118 archive_entry_copy_pathname(ae, "bar");
119 archive_entry_set_mode(ae, AE_IFREG | 0777);
120 archive_entry_set_size(ae, 8);
121 archive_entry_copy_hardlink(ae, "symlink/foo");
122 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
123 assertEqualIntA(a, ARCHIVE_FATAL, archive_write_data(a, "modified", 8));
124 archive_entry_free(ae);
126 /* Verify that target file contents are unchanged. */
127 assertTextFileContents("unmodified", "../target/foo");
129 assertEqualIntA(a, ARCHIVE_FATAL, archive_write_close(a));
130 archive_write_free(a);