]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/test/test_write_disk_secure.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / test / test_write_disk_secure.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 #define UMASK 022
29
30 /*
31  * Exercise security checks that should prevent certain
32  * writes.
33  */
34
35 DEFINE_TEST(test_write_disk_secure)
36 {
37 #if ARCHIVE_VERSION_NUMBER < 1009000
38         skipping("archive_write_disk interface");
39 #else
40         struct archive *a;
41         struct archive_entry *ae;
42         struct stat st;
43
44         /* Start with a known umask. */
45         umask(UMASK);
46
47         /* Create an archive_write_disk object. */
48         assert((a = archive_write_disk_new()) != NULL);
49
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));
57
58 #if !defined(_WIN32) || defined(__CYGWIN__)
59         /* Write a symlink to the dir above. */
60         assert((ae = archive_entry_new()) != NULL);
61         archive_entry_copy_pathname(ae, "link_to_dir");
62         archive_entry_set_mode(ae, S_IFLNK | 0777);
63         archive_entry_set_symlink(ae, "dir");
64         archive_write_disk_set_options(a, 0);
65         assert(0 == archive_write_header(a, ae));
66         assert(0 == archive_write_finish_entry(a));
67
68         /*
69          * Without security checks, we should be able to
70          * extract a file through the link.
71          */
72         assert(archive_entry_clear(ae) != NULL);
73         archive_entry_copy_pathname(ae, "link_to_dir/filea");
74         archive_entry_set_mode(ae, S_IFREG | 0777);
75         assert(0 == archive_write_header(a, ae));
76         assert(0 == archive_write_finish_entry(a));
77
78         /* But with security checks enabled, this should fail. */
79         assert(archive_entry_clear(ae) != NULL);
80         archive_entry_copy_pathname(ae, "link_to_dir/fileb");
81         archive_entry_set_mode(ae, S_IFREG | 0777);
82         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
83         failure("Extracting a file through a symlink should fail here.");
84         assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
85         archive_entry_free(ae);
86         assert(0 == archive_write_finish_entry(a));
87
88         /* Create another link. */
89         assert((ae = archive_entry_new()) != NULL);
90         archive_entry_copy_pathname(ae, "link_to_dir2");
91         archive_entry_set_mode(ae, S_IFLNK | 0777);
92         archive_entry_set_symlink(ae, "dir");
93         archive_write_disk_set_options(a, 0);
94         assert(0 == archive_write_header(a, ae));
95         assert(0 == archive_write_finish_entry(a));
96
97         /*
98          * With symlink check and unlink option, it should remove
99          * the link and create the dir.
100          */
101         assert(archive_entry_clear(ae) != NULL);
102         archive_entry_copy_pathname(ae, "link_to_dir2/filec");
103         archive_entry_set_mode(ae, S_IFREG | 0777);
104         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_UNLINK);
105         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
106         archive_entry_free(ae);
107         assert(0 == archive_write_finish_entry(a));
108
109         /*
110          * Without security checks, extracting a dir over a link to a
111          * dir should follow the link.
112          */
113         /* Create a symlink to a dir. */
114         assert((ae = archive_entry_new()) != NULL);
115         archive_entry_copy_pathname(ae, "link_to_dir3");
116         archive_entry_set_mode(ae, S_IFLNK | 0777);
117         archive_entry_set_symlink(ae, "dir");
118         archive_write_disk_set_options(a, 0);
119         assert(0 == archive_write_header(a, ae));
120         assert(0 == archive_write_finish_entry(a));
121         /* Extract a dir whose name matches the symlink. */
122         assert(archive_entry_clear(ae) != NULL);
123         archive_entry_copy_pathname(ae, "link_to_dir3");
124         archive_entry_set_mode(ae, S_IFDIR | 0777);
125         assert(0 == archive_write_header(a, ae));
126         assert(0 == archive_write_finish_entry(a));
127         /* Verify link was followed. */
128         assertEqualInt(0, lstat("link_to_dir3", &st));
129         assert(S_ISLNK(st.st_mode));
130         archive_entry_free(ae);
131
132         /*
133          * As above, but a broken link, so the link should get replaced.
134          */
135         /* Create a symlink to a dir. */
136         assert((ae = archive_entry_new()) != NULL);
137         archive_entry_copy_pathname(ae, "link_to_dir4");
138         archive_entry_set_mode(ae, S_IFLNK | 0777);
139         archive_entry_set_symlink(ae, "nonexistent_dir");
140         archive_write_disk_set_options(a, 0);
141         assert(0 == archive_write_header(a, ae));
142         assert(0 == archive_write_finish_entry(a));
143         /* Extract a dir whose name matches the symlink. */
144         assert(archive_entry_clear(ae) != NULL);
145         archive_entry_copy_pathname(ae, "link_to_dir4");
146         archive_entry_set_mode(ae, S_IFDIR | 0777);
147         assert(0 == archive_write_header(a, ae));
148         assert(0 == archive_write_finish_entry(a));
149         /* Verify link was replaced. */
150         assertEqualInt(0, lstat("link_to_dir4", &st));
151         assert(S_ISDIR(st.st_mode));
152         archive_entry_free(ae);
153 #endif
154
155         /*
156          * As above, but a link to a non-dir, so the link should get replaced.
157          */
158         /* Create a regular file and a symlink to it */
159         assert((ae = archive_entry_new()) != NULL);
160         archive_entry_copy_pathname(ae, "non_dir");
161         archive_entry_set_mode(ae, S_IFREG | 0777);
162         archive_write_disk_set_options(a, 0);
163         assert(0 == archive_write_header(a, ae));
164         assert(0 == archive_write_finish_entry(a));
165         /* Create symlink to the file. */
166         archive_entry_copy_pathname(ae, "link_to_dir5");
167         archive_entry_set_mode(ae, S_IFLNK | 0777);
168         archive_entry_set_symlink(ae, "non_dir");
169         archive_write_disk_set_options(a, 0);
170         assert(0 == archive_write_header(a, ae));
171         assert(0 == archive_write_finish_entry(a));
172         /* Extract a dir whose name matches the symlink. */
173         assert(archive_entry_clear(ae) != NULL);
174         archive_entry_copy_pathname(ae, "link_to_dir5");
175         archive_entry_set_mode(ae, S_IFDIR | 0777);
176         assert(0 == archive_write_header(a, ae));
177         assert(0 == archive_write_finish_entry(a));
178         /* Verify link was replaced. */
179         assertEqualInt(0, lstat("link_to_dir5", &st));
180         assert(S_ISDIR(st.st_mode));
181         archive_entry_free(ae);
182
183
184 #if ARCHIVE_VERSION_NUMBER < 2000000
185         archive_write_finish(a);
186 #else
187         assert(0 == archive_write_finish(a));
188 #endif
189
190 #if !defined(_WIN32) || defined(__CYGWIN__)
191         /* Test the entries on disk. */
192         assert(0 == lstat("dir", &st));
193         failure("dir: st.st_mode=%o", st.st_mode);
194         assert((st.st_mode & 0777) == 0755);
195
196         assert(0 == lstat("link_to_dir", &st));
197         failure("link_to_dir: st.st_mode=%o", st.st_mode);
198         assert(S_ISLNK(st.st_mode));
199 #if HAVE_LCHMOD
200         /* Systems that lack lchmod() can't set symlink perms, so skip this. */
201         failure("link_to_dir: st.st_mode=%o", st.st_mode);
202         assert((st.st_mode & 07777) == 0755);
203 #endif
204
205         assert(0 == lstat("dir/filea", &st));
206         failure("dir/filea: st.st_mode=%o", st.st_mode);
207         assert((st.st_mode & 07777) == 0755);
208
209         failure("dir/fileb: This file should not have been created");
210         assert(0 != lstat("dir/fileb", &st));
211
212         assert(0 == lstat("link_to_dir2", &st));
213         failure("link_to_dir2 should have been re-created as a true dir");
214         assert(S_ISDIR(st.st_mode));
215         failure("link_to_dir2: Implicit dir creation should obey umask, but st.st_mode=%o", st.st_mode);
216         assert((st.st_mode & 0777) == 0755);
217
218         assert(0 == lstat("link_to_dir2/filec", &st));
219         assert(S_ISREG(st.st_mode));
220         failure("link_to_dir2/filec: st.st_mode=%o", st.st_mode);
221         assert((st.st_mode & 07777) == 0755);
222 #endif
223 #endif
224 }