]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - contrib/libarchive/libarchive/test/test_write_disk_secure745.c
Fix bspatch heap overflow vulnerability. [SA-16:29]
[FreeBSD/releng/10.3.git] / contrib / libarchive / libarchive / test / test_write_disk_secure745.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 #745 describes a bug in the sandboxing code that
32  * allows one to use a symlink to edit the permissions on a file or
33  * directory outside of the sandbox.
34  */
35
36 DEFINE_TEST(test_write_disk_secure745)
37 {
38 #if defined(_WIN32) && !defined(__CYGWIN__)
39         skipping("archive_write_disk security checks not supported on Windows");
40 #else
41         struct archive *a;
42         struct archive_entry *ae;
43
44         /* Start with a known umask. */
45         assertUmask(UMASK);
46
47         /* Create an archive_write_disk object. */
48         assert((a = archive_write_disk_new()) != NULL);
49         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
50
51         /* The target dir:  The one we're going to try to change permission on */
52         assertMakeDir("target", 0700);
53
54         /* The sandbox dir we're going to run inside of. */
55         assertMakeDir("sandbox", 0700);
56         assertChdir("sandbox");
57
58         /* Create a symlink pointing to the target directory */
59         assert((ae = archive_entry_new()) != NULL);
60         archive_entry_copy_pathname(ae, "sym");
61         archive_entry_set_mode(ae, AE_IFLNK | 0777);
62         archive_entry_copy_symlink(ae, "../target");
63         assert(0 == archive_write_header(a, ae));
64         archive_entry_free(ae);
65
66         /* Try to alter the target dir through the symlink; this should fail. */
67         assert((ae = archive_entry_new()) != NULL);
68         archive_entry_copy_pathname(ae, "sym");
69         archive_entry_set_mode(ae, S_IFDIR | 0777);
70         assert(0 == archive_write_header(a, ae));
71         archive_entry_free(ae);
72
73         /* Permission of target dir should not have changed. */
74         assertFileMode("../target", 0700);
75
76         assert(0 == archive_write_close(a));
77         archive_write_free(a);
78 #endif
79 }
80 /*-
81  * Copyright (c) 2003-2007,2016 Tim Kientzle
82  * All rights reserved.
83  *
84  * Redistribution and use in source and binary forms, with or without
85  * modification, are permitted provided that the following conditions
86  * are met:
87  * 1. Redistributions of source code must retain the above copyright
88  *    notice, this list of conditions and the following disclaimer.
89  * 2. Redistributions in binary form must reproduce the above copyright
90  *    notice, this list of conditions and the following disclaimer in the
91  *    documentation and/or other materials provided with the distribution.
92  *
93  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
94  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
95  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
96  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
97  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
99  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
102  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103  */
104 #include "test.h"
105 __FBSDID("$FreeBSD$");
106
107 #define UMASK 022
108
109 /*
110  * Github Issue #745 describes a bug in the sandboxing code that
111  * allows one to use a symlink to edit the permissions on a file or
112  * directory outside of the sandbox.
113  */
114
115 DEFINE_TEST(test_write_disk_secure745)
116 {
117 #if defined(_WIN32) && !defined(__CYGWIN__)
118         skipping("archive_write_disk security checks not supported on Windows");
119 #else
120         struct archive *a;
121         struct archive_entry *ae;
122
123         /* Start with a known umask. */
124         assertUmask(UMASK);
125
126         /* Create an archive_write_disk object. */
127         assert((a = archive_write_disk_new()) != NULL);
128         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
129
130         /* The target dir:  The one we're going to try to change permission on */
131         assertMakeDir("target", 0700);
132
133         /* The sandbox dir we're going to run inside of. */
134         assertMakeDir("sandbox", 0700);
135         assertChdir("sandbox");
136
137         /* Create a symlink pointing to the target directory */
138         assert((ae = archive_entry_new()) != NULL);
139         archive_entry_copy_pathname(ae, "sym");
140         archive_entry_set_mode(ae, AE_IFLNK | 0777);
141         archive_entry_copy_symlink(ae, "../target");
142         assert(0 == archive_write_header(a, ae));
143         archive_entry_free(ae);
144
145         /* Try to alter the target dir through the symlink; this should fail. */
146         assert((ae = archive_entry_new()) != NULL);
147         archive_entry_copy_pathname(ae, "sym");
148         archive_entry_set_mode(ae, S_IFDIR | 0777);
149         assert(0 == archive_write_header(a, ae));
150         archive_entry_free(ae);
151
152         /* Permission of target dir should not have changed. */
153         assertFileMode("../target", 0700);
154
155         assert(0 == archive_write_close(a));
156         archive_write_free(a);
157 #endif
158 }