]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/test_write_disk_perms.c
This commit was generated by cvs2svn to compensate for changes in r173143,
[FreeBSD/FreeBSD.git] / lib / libarchive / test / test_write_disk_perms.c
1 /*-
2  * Copyright (c) 2003-2007 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 __FBSDID("$FreeBSD$");
27
28 #if ARCHIVE_VERSION_STAMP >= 1009000
29
30 #define UMASK 022
31
32 static long _default_gid = -1;
33 static long _invalid_gid = -1;
34 static long _alt_gid = -1;
35
36 /*
37  * To fully test SGID restores, we need three distinct GIDs to work
38  * with:
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
43  *      files with.
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.
46  */
47 static void
48 searchgid(void)
49 {
50         static int   _searched = 0;
51         uid_t uid = getuid();
52         gid_t gid = 0;
53         unsigned int n;
54         struct stat st;
55         int fd;
56
57         /* If we've already looked this up, we're done. */
58         if (_searched)
59                 return;
60         _searched = 1;
61
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.");
65         assert(fd > 0);
66
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;
70
71         /* Find a GID for which fchown() fails.  This is our "invalid" GID. */
72         _invalid_gid = -1;
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) {
76                         _invalid_gid = gid;
77                         break;
78                 }
79         }
80
81         /*
82          * Find a GID for which fchown() succeeds, but which isn't the
83          * default.  This is the "alternate" gid.
84          */
85         _alt_gid = -1;
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)
89                         continue;
90                 if (fchown(fd, uid, gid) == 0) {
91                         _alt_gid = gid;
92                         break;
93                 }
94         }
95         close(fd);
96 }
97
98 static int
99 altgid(void)
100 {
101         searchgid();
102         return (_alt_gid);
103 }
104
105 static int
106 invalidgid(void)
107 {
108         searchgid();
109         return (_invalid_gid);
110 }
111
112 static int
113 defaultgid(void)
114 {
115         searchgid();
116         return (_default_gid);
117 }
118 #endif
119
120 /*
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.
124  */
125
126 DEFINE_TEST(test_write_disk_perms)
127 {
128 #if ARCHIVE_VERSION_STAMP < 1009000
129         skipping("archive_write_disk interface");
130 #else
131         struct archive *a;
132         struct archive_entry *ae;
133         struct stat st;
134
135         /*
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().)
141          */
142         assertEqualInt(0, chown(".", getuid(), getgid()));
143
144         /* Create an archive_write_disk object. */
145         assert((a = archive_write_disk_new()) != NULL);
146
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);
154
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));
174
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));
182
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);
201
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));
209
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));
217
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));
226
227         /*
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.
231          */
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));
238         /*
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.
243          */
244         failure("Opportunistic SUID failure shouldn't return error.");
245         assertEqualInt(0, archive_write_finish_entry(a));
246
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));
257
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));
267
268         if (altgid() == -1) {
269                 /*
270                  * Current user must belong to at least two groups or
271                  * else we can't test setting the GID to another group.
272                  */
273                 printf("Current user can't test gid restore: must belong to more than one group.\n");
274         } else {
275                 /*
276                  * Write a regular file with ARCHIVE_EXTRACT_PERM & SGID bit
277                  * but without ARCHIVE_EXTRACT_OWNER.
278                  */
279                 /*
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
290                  *    behavior).
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.
295                  */
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));
305
306                 /*
307                  * As above, but add _EXTRACT_OWNER to verify that it
308                  * does succeed.
309                  */
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));
320         }
321
322         /*
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.
326          */
327         if (invalidgid() == -1) {
328                 /* This test always fails for root. */
329                 printf("Running as root: Can't test SGID failures.\n");
330         } else {
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));
339
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));
349         }
350
351         /* Set ownership should fail if we're not root. */
352         if (getuid() == 0) {
353                 printf("Running as root: Can't test setuid failures.\n");
354         } else {
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));
362         }
363
364 #if ARCHIVE_API_VERSION > 1
365         assert(0 == archive_write_finish(a));
366 #else
367         archive_write_finish(a);
368 #endif
369         archive_entry_free(ae);
370
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);
375
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);
379
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);
383
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);
387
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);
391
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);
395
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));
400
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));
405
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));
410
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));
415
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));
421
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));
426         }
427
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));
437         }
438
439         if (getuid() != 0) {
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());
448         }
449 #endif
450 }