2 * Copyright (c) 2003-2007 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 * Exercise security checks that should prevent certain
35 DEFINE_TEST(test_write_disk_secure)
37 #if ARCHIVE_VERSION_STAMP < 1009000
38 skipping("archive_write_disk interface");
41 struct archive_entry *ae;
44 /* Start with a known umask. */
47 /* Create an archive_write_disk object. */
48 assert((a = archive_write_disk_new()) != NULL);
50 /* Write a regular dir to it. */
51 assert((ae = archive_entry_new()) != NULL);
52 archive_entry_copy_pathname(ae, "dir");
53 archive_entry_set_mode(ae, S_IFDIR | 0777);
54 assert(0 == archive_write_header(a, ae));
55 archive_entry_free(ae);
56 assert(0 == archive_write_finish_entry(a));
58 /* Write a symlink to the dir above. */
59 assert((ae = archive_entry_new()) != NULL);
60 archive_entry_copy_pathname(ae, "link_to_dir");
61 archive_entry_set_mode(ae, S_IFLNK | 0777);
62 archive_entry_set_symlink(ae, "dir");
63 archive_write_disk_set_options(a, 0);
64 assert(0 == archive_write_header(a, ae));
65 assert(0 == archive_write_finish_entry(a));
68 * Without security checks, we should be able to
69 * extract a file through the link.
71 assert(archive_entry_clear(ae) != NULL);
72 archive_entry_copy_pathname(ae, "link_to_dir/filea");
73 archive_entry_set_mode(ae, S_IFREG | 0777);
74 assert(0 == archive_write_header(a, ae));
75 assert(0 == archive_write_finish_entry(a));
77 /* But with security checks enabled, this should fail. */
78 assert(archive_entry_clear(ae) != NULL);
79 archive_entry_copy_pathname(ae, "link_to_dir/fileb");
80 archive_entry_set_mode(ae, S_IFREG | 0777);
81 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
82 failure("Extracting a file through a symlink should fail here.");
83 assertEqualInt(ARCHIVE_WARN, archive_write_header(a, ae));
84 archive_entry_free(ae);
85 assert(0 == archive_write_finish_entry(a));
87 /* Create another link. */
88 assert((ae = archive_entry_new()) != NULL);
89 archive_entry_copy_pathname(ae, "link_to_dir2");
90 archive_entry_set_mode(ae, S_IFLNK | 0777);
91 archive_entry_set_symlink(ae, "dir");
92 archive_write_disk_set_options(a, 0);
93 assert(0 == archive_write_header(a, ae));
94 assert(0 == archive_write_finish_entry(a));
97 * With symlink check and unlink option, it should remove
98 * the link and create the dir.
100 assert(archive_entry_clear(ae) != NULL);
101 archive_entry_copy_pathname(ae, "link_to_dir2/filec");
102 archive_entry_set_mode(ae, S_IFREG | 0777);
103 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_UNLINK);
104 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
105 archive_entry_free(ae);
106 assert(0 == archive_write_finish_entry(a));
109 #if ARCHIVE_API_VERSION > 1
110 assert(0 == archive_write_finish(a));
112 archive_write_finish(a);
115 /* Test the entries on disk. */
116 assert(0 == lstat("dir", &st));
117 failure("dir: st.st_mode=%o", st.st_mode);
118 assert((st.st_mode & 07777) == 0755);
120 assert(0 == lstat("link_to_dir", &st));
121 failure("link_to_dir: st.st_mode=%o", st.st_mode);
122 assert(S_ISLNK(st.st_mode));
124 /* Systems that lack lchmod() can't set symlink perms, so skip this. */
125 failure("link_to_dir: st.st_mode=%o", st.st_mode);
126 assert((st.st_mode & 07777) == 0755);
129 assert(0 == lstat("dir/filea", &st));
130 failure("dir/filea: st.st_mode=%o", st.st_mode);
131 assert((st.st_mode & 07777) == 0755);
133 failure("dir/fileb: This file should not have been created");
134 assert(0 != lstat("dir/fileb", &st));
136 assert(0 == lstat("link_to_dir2", &st));
137 failure("link_to_dir2 should have been re-created as a true dir");
138 assert(S_ISDIR(st.st_mode));
139 failure("link_to_dir2: Implicit dir creation should obey umask, but st.st_mode=%o", st.st_mode);
140 assert((st.st_mode & 07777) == 0755);
142 assert(0 == lstat("link_to_dir2/filec", &st));
143 assert(S_ISREG(st.st_mode));
144 failure("link_to_dir2/filec: st.st_mode=%o", st.st_mode);
145 assert((st.st_mode & 07777) == 0755);