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 defined(_WIN32) && !defined(__CYGWIN__)
38 skipping("archive_write_disk security checks not supported on Windows");
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_FAILED, archive_write_header(a, ae));
84 archive_entry_free(ae);
85 assert(0 == archive_write_finish_entry(a));
87 /* Write an absolute symlink to /tmp. */
88 assert((ae = archive_entry_new()) != NULL);
89 archive_entry_copy_pathname(ae, "/tmp/libarchive_test-test_write_disk_secure-absolute_symlink");
90 archive_entry_set_mode(ae, S_IFLNK | 0777);
91 archive_entry_set_symlink(ae, "/tmp");
92 archive_write_disk_set_options(a, 0);
93 assert(0 == archive_write_header(a, ae));
94 assert(0 == archive_write_finish_entry(a));
96 /* With security checks enabled, this should fail. */
97 assert(archive_entry_clear(ae) != NULL);
98 archive_entry_copy_pathname(ae, "/tmp/libarchive_test-test_write_disk_secure-absolute_symlink/libarchive_test-test_write_disk_secure-absolute_symlink_path.tmp");
99 archive_entry_set_mode(ae, S_IFREG | 0777);
100 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
101 failure("Extracting a file through an absolute symlink should fail here.");
102 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
103 archive_entry_free(ae);
104 assertFileNotExists("/tmp/libarchive_test-test_write_disk_secure-absolute_symlink/libarchive_test-test_write_disk_secure-absolute_symlink_path.tmp");
105 assert(0 == unlink("/tmp/libarchive_test-test_write_disk_secure-absolute_symlink"));
106 unlink("/tmp/libarchive_test-test_write_disk_secure-absolute_symlink_path.tmp");
108 /* Create another link. */
109 assert((ae = archive_entry_new()) != NULL);
110 archive_entry_copy_pathname(ae, "link_to_dir2");
111 archive_entry_set_mode(ae, S_IFLNK | 0777);
112 archive_entry_set_symlink(ae, "dir");
113 archive_write_disk_set_options(a, 0);
114 assert(0 == archive_write_header(a, ae));
115 assert(0 == archive_write_finish_entry(a));
118 * With symlink check and unlink option, it should remove
119 * the link and create the dir.
121 assert(archive_entry_clear(ae) != NULL);
122 archive_entry_copy_pathname(ae, "link_to_dir2/filec");
123 archive_entry_set_mode(ae, S_IFREG | 0777);
124 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_UNLINK);
125 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
126 archive_entry_free(ae);
127 assert(0 == archive_write_finish_entry(a));
129 /* Create a nested symlink. */
130 assert((ae = archive_entry_new()) != NULL);
131 archive_entry_copy_pathname(ae, "dir/nested_link_to_dir");
132 archive_entry_set_mode(ae, S_IFLNK | 0777);
133 archive_entry_set_symlink(ae, "../dir");
134 archive_write_disk_set_options(a, 0);
135 assert(0 == archive_write_header(a, ae));
136 assert(0 == archive_write_finish_entry(a));
138 /* But with security checks enabled, this should fail. */
139 assert(archive_entry_clear(ae) != NULL);
140 archive_entry_copy_pathname(ae, "dir/nested_link_to_dir/filed");
141 archive_entry_set_mode(ae, S_IFREG | 0777);
142 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
143 failure("Extracting a file through a symlink should fail here.");
144 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
145 archive_entry_free(ae);
146 assert(0 == archive_write_finish_entry(a));
149 * Without security checks, extracting a dir over a link to a
150 * dir should follow the link.
152 /* Create a symlink to a dir. */
153 assert((ae = archive_entry_new()) != NULL);
154 archive_entry_copy_pathname(ae, "link_to_dir3");
155 archive_entry_set_mode(ae, S_IFLNK | 0777);
156 archive_entry_set_symlink(ae, "dir");
157 archive_write_disk_set_options(a, 0);
158 assert(0 == archive_write_header(a, ae));
159 assert(0 == archive_write_finish_entry(a));
160 /* Extract a dir whose name matches the symlink. */
161 assert(archive_entry_clear(ae) != NULL);
162 archive_entry_copy_pathname(ae, "link_to_dir3");
163 archive_entry_set_mode(ae, S_IFDIR | 0777);
164 assert(0 == archive_write_header(a, ae));
165 assert(0 == archive_write_finish_entry(a));
166 /* Verify link was followed. */
167 assertEqualInt(0, lstat("link_to_dir3", &st));
168 assert(S_ISLNK(st.st_mode));
169 archive_entry_free(ae);
172 * As above, but a broken link, so the link should get replaced.
174 /* Create a symlink to a dir. */
175 assert((ae = archive_entry_new()) != NULL);
176 archive_entry_copy_pathname(ae, "link_to_dir4");
177 archive_entry_set_mode(ae, S_IFLNK | 0777);
178 archive_entry_set_symlink(ae, "nonexistent_dir");
179 archive_write_disk_set_options(a, 0);
180 assert(0 == archive_write_header(a, ae));
181 assert(0 == archive_write_finish_entry(a));
182 /* Extract a dir whose name matches the symlink. */
183 assert(archive_entry_clear(ae) != NULL);
184 archive_entry_copy_pathname(ae, "link_to_dir4");
185 archive_entry_set_mode(ae, S_IFDIR | 0777);
186 assert(0 == archive_write_header(a, ae));
187 assert(0 == archive_write_finish_entry(a));
188 /* Verify link was replaced. */
189 assertEqualInt(0, lstat("link_to_dir4", &st));
190 assert(S_ISDIR(st.st_mode));
191 archive_entry_free(ae);
194 * As above, but a link to a non-dir, so the link should get replaced.
196 /* Create a regular file and a symlink to it */
197 assert((ae = archive_entry_new()) != NULL);
198 archive_entry_copy_pathname(ae, "non_dir");
199 archive_entry_set_mode(ae, S_IFREG | 0777);
200 archive_write_disk_set_options(a, 0);
201 assert(0 == archive_write_header(a, ae));
202 assert(0 == archive_write_finish_entry(a));
203 /* Create symlink to the file. */
204 archive_entry_copy_pathname(ae, "link_to_dir5");
205 archive_entry_set_mode(ae, S_IFLNK | 0777);
206 archive_entry_set_symlink(ae, "non_dir");
207 archive_write_disk_set_options(a, 0);
208 assert(0 == archive_write_header(a, ae));
209 assert(0 == archive_write_finish_entry(a));
210 /* Extract a dir whose name matches the symlink. */
211 assert(archive_entry_clear(ae) != NULL);
212 archive_entry_copy_pathname(ae, "link_to_dir5");
213 archive_entry_set_mode(ae, S_IFDIR | 0777);
214 assert(0 == archive_write_header(a, ae));
215 assert(0 == archive_write_finish_entry(a));
216 /* Verify link was replaced. */
217 assertEqualInt(0, lstat("link_to_dir5", &st));
218 assert(S_ISDIR(st.st_mode));
219 archive_entry_free(ae);
222 * Without security checks, we should be able to
223 * extract an absolute path.
225 assert((ae = archive_entry_new()) != NULL);
226 archive_entry_copy_pathname(ae, "/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
227 archive_entry_set_mode(ae, S_IFREG | 0777);
228 assert(0 == archive_write_header(a, ae));
229 assert(0 == archive_write_finish_entry(a));
230 assertFileExists("/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
231 assert(0 == unlink("/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp"));
233 /* But with security checks enabled, this should fail. */
234 assert(archive_entry_clear(ae) != NULL);
235 archive_entry_copy_pathname(ae, "/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
236 archive_entry_set_mode(ae, S_IFREG | 0777);
237 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS);
238 failure("Extracting an absolute path should fail here.");
239 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
240 archive_entry_free(ae);
241 assert(0 == archive_write_finish_entry(a));
242 assertFileNotExists("/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
244 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
246 /* Test the entries on disk. */
247 assert(0 == lstat("dir", &st));
248 failure("dir: st.st_mode=%o", st.st_mode);
249 assert((st.st_mode & 0777) == 0755);
251 assert(0 == lstat("link_to_dir", &st));
252 failure("link_to_dir: st.st_mode=%o", st.st_mode);
253 assert(S_ISLNK(st.st_mode));
255 /* Systems that lack lchmod() can't set symlink perms, so skip this. */
256 failure("link_to_dir: st.st_mode=%o", st.st_mode);
257 assert((st.st_mode & 07777) == 0755);
260 assert(0 == lstat("dir/filea", &st));
261 failure("dir/filea: st.st_mode=%o", st.st_mode);
262 assert((st.st_mode & 07777) == 0755);
264 failure("dir/fileb: This file should not have been created");
265 assert(0 != lstat("dir/fileb", &st));
267 assert(0 == lstat("link_to_dir2", &st));
268 failure("link_to_dir2 should have been re-created as a true dir");
269 assert(S_ISDIR(st.st_mode));
270 failure("link_to_dir2: Implicit dir creation should obey umask, but st.st_mode=%o", st.st_mode);
271 assert((st.st_mode & 0777) == 0755);
273 assert(0 == lstat("link_to_dir2/filec", &st));
274 assert(S_ISREG(st.st_mode));
275 failure("link_to_dir2/filec: st.st_mode=%o", st.st_mode);
276 assert((st.st_mode & 07777) == 0755);
278 failure("dir/filed: This file should not have been created");
279 assert(0 != lstat("dir/filed", &st));