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$");
28 #if ARCHIVE_VERSION_STAMP >= 1009000
32 static long _default_gid = -1;
33 static long _invalid_gid = -1;
34 static long _alt_gid = -1;
37 * To fully test SGID restores, we need three distinct GIDs to work
39 * * the GID that files are created with by default (for the
40 * current user in the current directory)
41 * * An "alt gid" that this user can create files with
42 * * An "invalid gid" that this user is not permitted to create
44 * The second fails if this user doesn't belong to at least two groups;
45 * the third fails if the current user is root.
50 static int _searched = 0;
57 /* If we've already looked this up, we're done. */
62 /* Create a file on disk in the current default dir. */
63 fd = open("test_gid", O_CREAT, 0664);
64 failure("Couldn't create a file for gid testing.");
67 /* See what GID it ended up with. This is our "valid" GID. */
68 assert(fstat(fd, &st) == 0);
69 _default_gid = st.st_gid;
71 /* Find a GID for which fchown() fails. This is our "invalid" GID. */
73 /* This loop stops when we wrap the gid or examine 10,000 gids. */
74 for (gid = 1, n = 1; gid == n && n < 10000 ; n++, gid++) {
75 if (fchown(fd, uid, gid) != 0) {
82 * Find a GID for which fchown() succeeds, but which isn't the
83 * default. This is the "alternate" gid.
86 for (gid = 0, n = 0; gid == n && n < 10000 ; n++, gid++) {
87 /* _alt_gid must be different than _default_gid */
88 if (gid == (gid_t)_default_gid)
90 if (fchown(fd, uid, gid) == 0) {
109 return (_invalid_gid);
116 return (_default_gid);
121 * Exercise permission and ownership restores.
122 * In particular, try to exercise a bunch of border cases related
123 * to files/dirs that already exist, SUID/SGID bits, etc.
126 DEFINE_TEST(test_write_disk_perms)
128 #if ARCHIVE_VERSION_STAMP < 1009000
129 skipping("archive_write_disk interface");
132 struct archive_entry *ae;
136 * Set ownership of the current directory to the group of this
137 * process. Otherwise, the SGID tests below fail if the
138 * /tmp directory is owned by a group to which we don't belong
139 * and we're on a system where group ownership is inherited.
140 * (Because we're not allowed to SGID files with defaultgid().)
142 assertEqualInt(0, chown(".", getuid(), getgid()));
144 /* Create an archive_write_disk object. */
145 assert((a = archive_write_disk_new()) != NULL);
147 /* Write a regular file to it. */
148 assert((ae = archive_entry_new()) != NULL);
149 archive_entry_copy_pathname(ae, "file_0755");
150 archive_entry_set_mode(ae, S_IFREG | 0777);
151 assert(0 == archive_write_header(a, ae));
152 assert(0 == archive_write_finish_entry(a));
153 archive_entry_free(ae);
155 /* Write a regular file, then write over it. */
156 /* For files, the perms should get updated. */
157 assert((ae = archive_entry_new()) != NULL);
158 archive_entry_copy_pathname(ae, "file_overwrite_0144");
159 archive_entry_set_mode(ae, S_IFREG | 0777);
160 assert(0 == archive_write_header(a, ae));
161 archive_entry_free(ae);
162 assert(0 == archive_write_finish_entry(a));
163 /* Check that file was created with different perms. */
164 assert(0 == stat("file_overwrite_0144", &st));
165 failure("file_overwrite_0144: st.st_mode=%o", st.st_mode);
166 assert((st.st_mode & 07777) != 0144);
167 /* Overwrite, this should change the perms. */
168 assert((ae = archive_entry_new()) != NULL);
169 archive_entry_copy_pathname(ae, "file_overwrite_0144");
170 archive_entry_set_mode(ae, S_IFREG | 0144);
171 assert(0 == archive_write_header(a, ae));
172 archive_entry_free(ae);
173 assert(0 == archive_write_finish_entry(a));
175 /* Write a regular dir. */
176 assert((ae = archive_entry_new()) != NULL);
177 archive_entry_copy_pathname(ae, "dir_0514");
178 archive_entry_set_mode(ae, S_IFDIR | 0514);
179 assert(0 == archive_write_header(a, ae));
180 archive_entry_free(ae);
181 assert(0 == archive_write_finish_entry(a));
183 /* Overwrite an existing dir. */
184 /* For dir, the first perms should get left. */
185 assert(mkdir("dir_overwrite_0744", 0744) == 0);
186 /* Check original perms. */
187 assert(0 == stat("dir_overwrite_0744", &st));
188 failure("dir_overwrite_0744: st.st_mode=%o", st.st_mode);
189 assert((st.st_mode & 07777) == 0744);
190 /* Overwrite shouldn't edit perms. */
191 assert((ae = archive_entry_new()) != NULL);
192 archive_entry_copy_pathname(ae, "dir_overwrite_0744");
193 archive_entry_set_mode(ae, S_IFDIR | 0777);
194 assert(0 == archive_write_header(a, ae));
195 archive_entry_free(ae);
196 assert(0 == archive_write_finish_entry(a));
197 /* Make sure they're unchanged. */
198 assert(0 == stat("dir_overwrite_0744", &st));
199 failure("dir_overwrite_0744: st.st_mode=%o", st.st_mode);
200 assert((st.st_mode & 07777) == 0744);
202 /* Write a regular file with SUID bit, but don't use _EXTRACT_PERM. */
203 assert((ae = archive_entry_new()) != NULL);
204 archive_entry_copy_pathname(ae, "file_no_suid");
205 archive_entry_set_mode(ae, S_IFREG | S_ISUID | 0777);
206 archive_write_disk_set_options(a, 0);
207 assert(0 == archive_write_header(a, ae));
208 assert(0 == archive_write_finish_entry(a));
210 /* Write a regular file with ARCHIVE_EXTRACT_PERM. */
211 assert(archive_entry_clear(ae) != NULL);
212 archive_entry_copy_pathname(ae, "file_0777");
213 archive_entry_set_mode(ae, S_IFREG | 0777);
214 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_PERM);
215 assert(0 == archive_write_header(a, ae));
216 assert(0 == archive_write_finish_entry(a));
218 /* Write a regular file with ARCHIVE_EXTRACT_PERM & SUID bit */
219 assert(archive_entry_clear(ae) != NULL);
220 archive_entry_copy_pathname(ae, "file_4742");
221 archive_entry_set_mode(ae, S_IFREG | S_ISUID | 0742);
222 archive_entry_set_uid(ae, getuid());
223 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_PERM);
224 assert(0 == archive_write_header(a, ae));
225 assert(0 == archive_write_finish_entry(a));
228 * Write a regular file with ARCHIVE_EXTRACT_PERM & SUID bit,
229 * but wrong uid. POSIX says you shouldn't restore SUID bit
230 * unless the UID could be restored.
232 assert(archive_entry_clear(ae) != NULL);
233 archive_entry_copy_pathname(ae, "file_bad_suid");
234 archive_entry_set_mode(ae, S_IFREG | S_ISUID | 0742);
235 archive_entry_set_uid(ae, getuid() + 1);
236 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_PERM);
237 assertA(0 == archive_write_header(a, ae));
239 * Because we didn't ask for owner, the failure to
240 * restore SUID shouldn't return a failure.
241 * We check below to make sure SUID really wasn't set.
242 * See more detailed comments below.
244 failure("Opportunistic SUID failure shouldn't return error.");
245 assertEqualInt(0, archive_write_finish_entry(a));
247 assert(archive_entry_clear(ae) != NULL);
248 archive_entry_copy_pathname(ae, "file_bad_suid2");
249 archive_entry_set_mode(ae, S_IFREG | S_ISUID | 0742);
250 archive_entry_set_uid(ae, getuid() + 1);
251 archive_write_disk_set_options(a,
252 ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_OWNER);
253 assertA(0 == archive_write_header(a, ae));
254 /* Owner change should fail here. */
255 failure("Non-opportunistic SUID failure should return error.");
256 assertEqualInt(ARCHIVE_WARN, archive_write_finish_entry(a));
258 /* Write a regular file with ARCHIVE_EXTRACT_PERM & SGID bit */
259 assert(archive_entry_clear(ae) != NULL);
260 archive_entry_copy_pathname(ae, "file_perm_sgid");
261 archive_entry_set_mode(ae, S_IFREG | S_ISGID | 0742);
262 archive_entry_set_gid(ae, defaultgid());
263 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_PERM);
264 assert(0 == archive_write_header(a, ae));
265 failure("Setting SGID bit should succeed here.");
266 assertEqualIntA(a, 0, archive_write_finish_entry(a));
268 if (altgid() == -1) {
270 * Current user must belong to at least two groups or
271 * else we can't test setting the GID to another group.
273 printf("Current user can't test gid restore: must belong to more than one group.\n");
276 * Write a regular file with ARCHIVE_EXTRACT_PERM & SGID bit
277 * but without ARCHIVE_EXTRACT_OWNER.
280 * This is a weird case: The user has asked for permissions to
281 * be restored but not asked for ownership to be restored. As
282 * a result, the default file creation will create a file with
283 * the wrong group. There are several possible behaviors for
284 * libarchive in this scenario:
285 * = Set the SGID bit. It is wrong and a security hole to
286 * set SGID with the wrong group. Even POSIX thinks so.
287 * = Implicitly set the group. I don't like this.
288 * = drop the SGID bit and warn (the old libarchive behavior)
289 * = drop the SGID bit and don't warn (the current libarchive
291 * The current behavior sees SGID/SUID restore when you
292 * don't ask for owner restore as an "opportunistic"
293 * action. That is, libarchive should do it if it can,
294 * but if it can't, it's not an error.
296 assert(archive_entry_clear(ae) != NULL);
297 archive_entry_copy_pathname(ae, "file_alt_sgid");
298 archive_entry_set_mode(ae, S_IFREG | S_ISGID | 0742);
299 archive_entry_set_uid(ae, getuid());
300 archive_entry_set_gid(ae, altgid());
301 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_PERM);
302 assert(0 == archive_write_header(a, ae));
303 failure("Setting SGID bit should fail because of group mismatch but the failure should be silent because we didn't ask for the group to be set.");
304 assertEqualIntA(a, 0, archive_write_finish_entry(a));
307 * As above, but add _EXTRACT_OWNER to verify that it
310 assert(archive_entry_clear(ae) != NULL);
311 archive_entry_copy_pathname(ae, "file_alt_sgid_owner");
312 archive_entry_set_mode(ae, S_IFREG | S_ISGID | 0742);
313 archive_entry_set_uid(ae, getuid());
314 archive_entry_set_gid(ae, altgid());
315 archive_write_disk_set_options(a,
316 ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_OWNER);
317 assert(0 == archive_write_header(a, ae));
318 failure("Setting SGID bit should succeed here.");
319 assertEqualIntA(a, ARCHIVE_OK, archive_write_finish_entry(a));
323 * Write a regular file with ARCHIVE_EXTRACT_PERM & SGID bit,
324 * but wrong GID. POSIX says you shouldn't restore SGID bit
325 * unless the GID could be restored.
327 if (invalidgid() == -1) {
328 /* This test always fails for root. */
329 printf("Running as root: Can't test SGID failures.\n");
331 assert(archive_entry_clear(ae) != NULL);
332 archive_entry_copy_pathname(ae, "file_bad_sgid");
333 archive_entry_set_mode(ae, S_IFREG | S_ISGID | 0742);
334 archive_entry_set_gid(ae, invalidgid());
335 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_PERM);
336 assertA(0 == archive_write_header(a, ae));
337 failure("This SGID restore should fail without an error.");
338 assertEqualIntA(a, 0, archive_write_finish_entry(a));
340 assert(archive_entry_clear(ae) != NULL);
341 archive_entry_copy_pathname(ae, "file_bad_sgid2");
342 archive_entry_set_mode(ae, S_IFREG | S_ISGID | 0742);
343 archive_entry_set_gid(ae, invalidgid());
344 archive_write_disk_set_options(a,
345 ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_OWNER);
346 assertA(0 == archive_write_header(a, ae));
347 failure("This SGID restore should fail with an error.");
348 assertEqualIntA(a, ARCHIVE_WARN, archive_write_finish_entry(a));
351 /* Set ownership should fail if we're not root. */
353 printf("Running as root: Can't test setuid failures.\n");
355 assert(archive_entry_clear(ae) != NULL);
356 archive_entry_copy_pathname(ae, "file_bad_owner");
357 archive_entry_set_mode(ae, S_IFREG | 0744);
358 archive_entry_set_uid(ae, getuid() + 1);
359 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_OWNER);
360 assertA(0 == archive_write_header(a, ae));
361 assertEqualIntA(a,ARCHIVE_WARN,archive_write_finish_entry(a));
364 #if ARCHIVE_API_VERSION > 1
365 assert(0 == archive_write_finish(a));
367 archive_write_finish(a);
369 archive_entry_free(ae);
371 /* Test the entries on disk. */
372 assert(0 == stat("file_0755", &st));
373 failure("file_0755: st.st_mode=%o", st.st_mode);
374 assert((st.st_mode & 07777) == 0755);
376 assert(0 == stat("file_overwrite_0144", &st));
377 failure("file_overwrite_0144: st.st_mode=%o", st.st_mode);
378 assert((st.st_mode & 07777) == 0144);
380 assert(0 == stat("dir_0514", &st));
381 failure("dir_0514: st.st_mode=%o", st.st_mode);
382 assert((st.st_mode & 07777) == 0514);
384 assert(0 == stat("dir_overwrite_0744", &st));
385 failure("dir_overwrite_0744: st.st_mode=%o", st.st_mode);
386 assert((st.st_mode & 07777) == 0744);
388 assert(0 == stat("file_no_suid", &st));
389 failure("file_0755: st.st_mode=%o", st.st_mode);
390 assert((st.st_mode & 07777) == 0755);
392 assert(0 == stat("file_0777", &st));
393 failure("file_0777: st.st_mode=%o", st.st_mode);
394 assert((st.st_mode & 07777) == 0777);
396 /* SUID bit should get set here. */
397 assert(0 == stat("file_4742", &st));
398 failure("file_4742: st.st_mode=%o", st.st_mode);
399 assert((st.st_mode & 07777) == (S_ISUID | 0742));
401 /* SUID bit should NOT have been set here. */
402 assert(0 == stat("file_bad_suid", &st));
403 failure("file_bad_suid: st.st_mode=%o", st.st_mode);
404 assert((st.st_mode & 07777) == (0742));
406 /* SUID bit should NOT have been set here. */
407 assert(0 == stat("file_bad_suid2", &st));
408 failure("file_bad_suid2: st.st_mode=%o", st.st_mode);
409 assert((st.st_mode & 07777) == (0742));
411 /* SGID should be set here. */
412 assert(0 == stat("file_perm_sgid", &st));
413 failure("file_perm_sgid: st.st_mode=%o", st.st_mode);
414 assert((st.st_mode & 07777) == (S_ISGID | 0742));
416 if (altgid() != -1) {
417 /* SGID should not be set here. */
418 assert(0 == stat("file_alt_sgid", &st));
419 failure("file_alt_sgid: st.st_mode=%o", st.st_mode);
420 assert((st.st_mode & 07777) == (0742));
422 /* SGID should be set here. */
423 assert(0 == stat("file_alt_sgid_owner", &st));
424 failure("file_alt_sgid: st.st_mode=%o", st.st_mode);
425 assert((st.st_mode & 07777) == (S_ISGID | 0742));
428 if (invalidgid() != -1) {
429 /* SGID should NOT be set here. */
430 assert(0 == stat("file_bad_sgid", &st));
431 failure("file_bad_sgid: st.st_mode=%o", st.st_mode);
432 assert((st.st_mode & 07777) == (0742));
433 /* SGID should NOT be set here. */
434 assert(0 == stat("file_bad_sgid2", &st));
435 failure("file_bad_sgid2: st.st_mode=%o", st.st_mode);
436 assert((st.st_mode & 07777) == (0742));
440 assert(0 == stat("file_bad_owner", &st));
441 failure("file_bad_owner: st.st_mode=%o", st.st_mode);
442 assert((st.st_mode & 07777) == (0744));
443 failure("file_bad_owner: st.st_uid=%d getuid()=%d",
444 st.st_uid, getuid());
445 /* The entry had getuid()+1, but because we're
446 * not root, we should not have been able to set that. */
447 assert(st.st_uid == getuid());