]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - contrib/libarchive/libarchive/test/test_write_disk_secure746.c
Fix bspatch heap overflow vulnerability. [SA-16:29]
[FreeBSD/releng/10.3.git] / contrib / libarchive / libarchive / test / test_write_disk_secure746.c
1 /*-
2  * Copyright (c) 2003-2007,2016 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  * Github Issue #746 describes a problem in which hardlink targets are
32  * not adequately checked and can be used to modify entries outside of
33  * the sandbox.
34  */
35
36 /*
37  * Verify that ARCHIVE_EXTRACT_SECURE_NODOTDOT disallows '..' in hardlink
38  * targets.
39  */
40 DEFINE_TEST(test_write_disk_secure746a)
41 {
42 #if defined(_WIN32) && !defined(__CYGWIN__)
43         skipping("archive_write_disk security checks not supported on Windows");
44 #else
45         struct archive *a;
46         struct archive_entry *ae;
47
48         /* Start with a known umask. */
49         assertUmask(UMASK);
50
51         /* The target directory we're going to try to affect. */
52         assertMakeDir("target", 0700);
53         assertMakeFile("target/foo", 0700, "unmodified");
54
55         /* The sandbox dir we're going to work within. */
56         assertMakeDir("sandbox", 0700);
57         assertChdir("sandbox");
58
59         /* Create an archive_write_disk object. */
60         assert((a = archive_write_disk_new()) != NULL);
61         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_NODOTDOT);
62
63         /* Attempt to hardlink to the target directory. */
64         assert((ae = archive_entry_new()) != NULL);
65         archive_entry_copy_pathname(ae, "bar");
66         archive_entry_set_mode(ae, AE_IFREG | 0777);
67         archive_entry_set_size(ae, 8);
68         archive_entry_copy_hardlink(ae, "../target/foo");
69         assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
70         assertEqualInt(ARCHIVE_FATAL, archive_write_data(a, "modified", 8));
71         archive_entry_free(ae);
72
73         /* Verify that target file contents are unchanged. */
74         assertTextFileContents("unmodified", "../target/foo");
75 #endif
76 }
77
78 /*
79  * Verify that ARCHIVE_EXTRACT_SECURE_NOSYMLINK disallows symlinks in hardlink
80  * targets.
81  */
82 DEFINE_TEST(test_write_disk_secure746b)
83 {
84 #if defined(_WIN32) && !defined(__CYGWIN__)
85         skipping("archive_write_disk security checks not supported on Windows");
86 #else
87         struct archive *a;
88         struct archive_entry *ae;
89
90         /* Start with a known umask. */
91         assertUmask(UMASK);
92
93         /* The target directory we're going to try to affect. */
94         assertMakeDir("target", 0700);
95         assertMakeFile("target/foo", 0700, "unmodified");
96
97         /* The sandbox dir we're going to work within. */
98         assertMakeDir("sandbox", 0700);
99         assertChdir("sandbox");
100
101         /* Create an archive_write_disk object. */
102         assert((a = archive_write_disk_new()) != NULL);
103         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
104
105         /* Create a symlink to the target directory. */
106         assert((ae = archive_entry_new()) != NULL);
107         archive_entry_copy_pathname(ae, "symlink");
108         archive_entry_set_mode(ae, AE_IFLNK | 0777);
109         archive_entry_copy_symlink(ae, "../target");
110         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
111         archive_entry_free(ae);
112
113         /* Attempt to hardlink to the target directory via the symlink. */
114         assert((ae = archive_entry_new()) != NULL);
115         archive_entry_copy_pathname(ae, "bar");
116         archive_entry_set_mode(ae, AE_IFREG | 0777);
117         archive_entry_set_size(ae, 8);
118         archive_entry_copy_hardlink(ae, "symlink/foo");
119         assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
120         assertEqualIntA(a, ARCHIVE_FATAL, archive_write_data(a, "modified", 8));
121         archive_entry_free(ae);
122
123         /* Verify that target file contents are unchanged. */
124         assertTextFileContents("unmodified", "../target/foo");
125
126         assertEqualIntA(a, ARCHIVE_FATAL, archive_write_close(a));
127         archive_write_free(a);
128 #endif
129 }
130 /*-
131  * Copyright (c) 2003-2007,2016 Tim Kientzle
132  * All rights reserved.
133  *
134  * Redistribution and use in source and binary forms, with or without
135  * modification, are permitted provided that the following conditions
136  * are met:
137  * 1. Redistributions of source code must retain the above copyright
138  *    notice, this list of conditions and the following disclaimer.
139  * 2. Redistributions in binary form must reproduce the above copyright
140  *    notice, this list of conditions and the following disclaimer in the
141  *    documentation and/or other materials provided with the distribution.
142  *
143  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
144  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
145  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
146  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
147  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
148  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
149  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
150  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
151  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
152  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
153  */
154 #include "test.h"
155 __FBSDID("$FreeBSD$");
156
157 #define UMASK 022
158
159 /*
160  * Github Issue #746 describes a problem in which hardlink targets are
161  * not adequately checked and can be used to modify entries outside of
162  * the sandbox.
163  */
164
165 /*
166  * Verify that ARCHIVE_EXTRACT_SECURE_NODOTDOT disallows '..' in hardlink
167  * targets.
168  */
169 DEFINE_TEST(test_write_disk_secure746a)
170 {
171 #if defined(_WIN32) && !defined(__CYGWIN__)
172         skipping("archive_write_disk security checks not supported on Windows");
173 #else
174         struct archive *a;
175         struct archive_entry *ae;
176
177         /* Start with a known umask. */
178         assertUmask(UMASK);
179
180         /* The target directory we're going to try to affect. */
181         assertMakeDir("target", 0700);
182         assertMakeFile("target/foo", 0700, "unmodified");
183
184         /* The sandbox dir we're going to work within. */
185         assertMakeDir("sandbox", 0700);
186         assertChdir("sandbox");
187
188         /* Create an archive_write_disk object. */
189         assert((a = archive_write_disk_new()) != NULL);
190         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_NODOTDOT);
191
192         /* Attempt to hardlink to the target directory. */
193         assert((ae = archive_entry_new()) != NULL);
194         archive_entry_copy_pathname(ae, "bar");
195         archive_entry_set_mode(ae, AE_IFREG | 0777);
196         archive_entry_set_size(ae, 8);
197         archive_entry_copy_hardlink(ae, "../target/foo");
198         assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
199         assertEqualInt(ARCHIVE_FATAL, archive_write_data(a, "modified", 8));
200         archive_entry_free(ae);
201
202         /* Verify that target file contents are unchanged. */
203         assertTextFileContents("unmodified", "../target/foo");
204 #endif
205 }
206
207 /*
208  * Verify that ARCHIVE_EXTRACT_SECURE_NOSYMLINK disallows symlinks in hardlink
209  * targets.
210  */
211 DEFINE_TEST(test_write_disk_secure746b)
212 {
213 #if defined(_WIN32) && !defined(__CYGWIN__)
214         skipping("archive_write_disk security checks not supported on Windows");
215 #else
216         struct archive *a;
217         struct archive_entry *ae;
218
219         /* Start with a known umask. */
220         assertUmask(UMASK);
221
222         /* The target directory we're going to try to affect. */
223         assertMakeDir("target", 0700);
224         assertMakeFile("target/foo", 0700, "unmodified");
225
226         /* The sandbox dir we're going to work within. */
227         assertMakeDir("sandbox", 0700);
228         assertChdir("sandbox");
229
230         /* Create an archive_write_disk object. */
231         assert((a = archive_write_disk_new()) != NULL);
232         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
233
234         /* Create a symlink to the target directory. */
235         assert((ae = archive_entry_new()) != NULL);
236         archive_entry_copy_pathname(ae, "symlink");
237         archive_entry_set_mode(ae, AE_IFLNK | 0777);
238         archive_entry_copy_symlink(ae, "../target");
239         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
240         archive_entry_free(ae);
241
242         /* Attempt to hardlink to the target directory via the symlink. */
243         assert((ae = archive_entry_new()) != NULL);
244         archive_entry_copy_pathname(ae, "bar");
245         archive_entry_set_mode(ae, AE_IFREG | 0777);
246         archive_entry_set_size(ae, 8);
247         archive_entry_copy_hardlink(ae, "symlink/foo");
248         assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
249         assertEqualIntA(a, ARCHIVE_FATAL, archive_write_data(a, "modified", 8));
250         archive_entry_free(ae);
251
252         /* Verify that target file contents are unchanged. */
253         assertTextFileContents("unmodified", "../target/foo");
254
255         assertEqualIntA(a, ARCHIVE_FATAL, archive_write_close(a));
256         archive_write_free(a);
257 #endif
258 }