xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_chroot.c (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1 /* $NetBSD: t_chroot.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_chroot.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
33 
34 #include <sys/wait.h>
35 
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <pwd.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #ifdef __FreeBSD__
46 #include <sys/stat.h>
47 #endif
48 
49 ATF_TC(chroot_basic);
50 ATF_TC_HEAD(chroot_basic, tc)
51 {
52 	atf_tc_set_md_var(tc, "descr", "A basic test of chroot(2)");
53 	atf_tc_set_md_var(tc, "require.user", "root");
54 }
55 
56 ATF_TC_BODY(chroot_basic, tc)
57 {
58 	char buf[PATH_MAX];
59 	int fd, sta;
60 	pid_t pid;
61 
62 	(void)memset(buf, '\0', sizeof(buf));
63 	(void)getcwd(buf, sizeof(buf));
64 	(void)strlcat(buf, "/dir", sizeof(buf));
65 
66 	ATF_REQUIRE(mkdir(buf, 0500) == 0);
67 	ATF_REQUIRE(chdir(buf) == 0);
68 
69 	pid = fork();
70 	ATF_REQUIRE(pid >= 0);
71 
72 	if (pid == 0) {
73 
74 		if (chroot(buf) != 0)
75 			_exit(EXIT_FAILURE);
76 
77 		errno = 0;
78 
79 		if (chroot("/root") != -1)
80 			_exit(EXIT_FAILURE);
81 
82 		if (errno != ENOENT)
83 			_exit(EXIT_FAILURE);
84 
85 		fd = open("file", O_RDONLY | O_CREAT, 0600);
86 
87 		if (fd < 0)
88 			_exit(EXIT_FAILURE);
89 
90 		if (close(fd) != 0)
91 			_exit(EXIT_FAILURE);
92 
93 		_exit(EXIT_SUCCESS);
94 	}
95 
96 	(void)wait(&sta);
97 
98 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
99 		atf_tc_fail("chroot(2) failed");
100 
101 	(void)chdir("/");
102 	(void)strlcat(buf, "/file", sizeof(buf));
103 
104 	fd = open(buf, O_RDONLY);
105 
106 	if (fd < 0)
107 		atf_tc_fail("chroot(2) did not change the root directory");
108 
109 	ATF_REQUIRE(close(fd) == 0);
110 	ATF_REQUIRE(unlink(buf) == 0);
111 }
112 
113 ATF_TC(chroot_err);
114 ATF_TC_HEAD(chroot_err, tc)
115 {
116 	atf_tc_set_md_var(tc, "descr", "Test error conditions of chroot(2)");
117 	atf_tc_set_md_var(tc, "require.user", "root");
118 }
119 
120 ATF_TC_BODY(chroot_err, tc)
121 {
122 	char buf[PATH_MAX + 1];
123 
124 	(void)memset(buf, 'x', sizeof(buf));
125 
126 	errno = 0;
127 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, chroot(buf) == -1);
128 
129 	errno = 0;
130 	ATF_REQUIRE_ERRNO(EFAULT, chroot((void *)-1) == -1);
131 
132 	errno = 0;
133 	ATF_REQUIRE_ERRNO(ENOENT, chroot("/a/b/c/d/e/f/g/h/i/j") == -1);
134 }
135 
136 ATF_TC(chroot_perm);
137 ATF_TC_HEAD(chroot_perm, tc)
138 {
139 	atf_tc_set_md_var(tc, "descr", "Test permissions with chroot(2)");
140 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
141 }
142 
143 ATF_TC_BODY(chroot_perm, tc)
144 {
145 	static char buf[LINE_MAX];
146 	pid_t pid;
147 	int sta;
148 
149 	(void)memset(buf, '\0', sizeof(buf));
150 	ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
151 
152 	pid = fork();
153 	ATF_REQUIRE(pid >= 0);
154 
155 	if (pid == 0) {
156 
157 		errno = 0;
158 
159 		if (chroot(buf) != -1)
160 			_exit(EXIT_FAILURE);
161 
162 		if (errno != EPERM)
163 			_exit(EXIT_FAILURE);
164 
165 		_exit(EXIT_SUCCESS);
166 	}
167 
168 	(void)wait(&sta);
169 
170 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
171 		atf_tc_fail("chroot(2) succeeded as unprivileged user");
172 }
173 
174 #ifdef __NetBSD__
175 ATF_TC(fchroot_basic);
176 ATF_TC_HEAD(fchroot_basic, tc)
177 {
178 	atf_tc_set_md_var(tc, "descr", "A basic test of fchroot(2)");
179 	atf_tc_set_md_var(tc, "require.user", "root");
180 }
181 
182 ATF_TC_BODY(fchroot_basic, tc)
183 {
184 	char buf[PATH_MAX];
185 	int fd, sta;
186 	pid_t pid;
187 
188 	(void)memset(buf, '\0', sizeof(buf));
189 	(void)getcwd(buf, sizeof(buf));
190 	(void)strlcat(buf, "/dir", sizeof(buf));
191 
192 	ATF_REQUIRE(mkdir(buf, 0500) == 0);
193 	ATF_REQUIRE(chdir(buf) == 0);
194 
195 	fd = open(buf, O_RDONLY);
196 	ATF_REQUIRE(fd >= 0);
197 
198 	pid = fork();
199 	ATF_REQUIRE(pid >= 0);
200 
201 	if (pid == 0) {
202 
203 		if (fchroot(fd) != 0)
204 			_exit(EXIT_FAILURE);
205 
206 		if (close(fd) != 0)
207 			_exit(EXIT_FAILURE);
208 
209 		fd = open("file", O_RDONLY | O_CREAT, 0600);
210 
211 		if (fd < 0)
212 			_exit(EXIT_FAILURE);
213 
214 		if (close(fd) != 0)
215 			_exit(EXIT_FAILURE);
216 
217 		_exit(EXIT_SUCCESS);
218 	}
219 
220 	(void)wait(&sta);
221 
222 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
223 		atf_tc_fail("fchroot(2) failed");
224 
225 	(void)chdir("/");
226 	(void)strlcat(buf, "/file", sizeof(buf));
227 
228 	fd = open(buf, O_RDONLY);
229 
230 	if (fd < 0)
231 		atf_tc_fail("fchroot(2) did not change the root directory");
232 
233 	ATF_REQUIRE(close(fd) == 0);
234 	ATF_REQUIRE(unlink(buf) == 0);
235 }
236 
237 ATF_TC(fchroot_err);
238 ATF_TC_HEAD(fchroot_err, tc)
239 {
240 	atf_tc_set_md_var(tc, "descr", "Test error conditions of fchroot(2)");
241 	atf_tc_set_md_var(tc, "require.user", "root");
242 }
243 
244 ATF_TC_BODY(fchroot_err, tc)
245 {
246 	int fd;
247 
248 	fd = open("/etc/passwd", O_RDONLY);
249 	ATF_REQUIRE(fd >= 0);
250 
251 	errno = 0;
252 	ATF_REQUIRE_ERRNO(EBADF, fchroot(-1) == -1);
253 
254 	errno = 0;
255 	ATF_REQUIRE_ERRNO(ENOTDIR, fchroot(fd) == -1);
256 
257 	ATF_REQUIRE(close(fd) == 0);
258 }
259 
260 ATF_TC(fchroot_perm);
261 ATF_TC_HEAD(fchroot_perm, tc)
262 {
263 	atf_tc_set_md_var(tc, "descr", "Test permissions with fchroot(2)");
264 	atf_tc_set_md_var(tc, "require.user", "root");
265 }
266 
267 ATF_TC_BODY(fchroot_perm, tc)
268 {
269 	static char buf[LINE_MAX];
270 	struct passwd *pw;
271 	int fd, sta;
272 	pid_t pid;
273 
274 	(void)memset(buf, '\0', sizeof(buf));
275 	ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
276 
277 	pw = getpwnam("nobody");
278 	fd = open(buf, O_RDONLY);
279 
280 	ATF_REQUIRE(fd >= 0);
281 	ATF_REQUIRE(pw != NULL);
282 
283 	pid = fork();
284 	ATF_REQUIRE(pid >= 0);
285 
286 	if (pid == 0) {
287 
288 		(void)setuid(pw->pw_uid);
289 
290 		errno = 0;
291 
292 		if (fchroot(fd) != -1)
293 			_exit(EXIT_FAILURE);
294 
295 		if (errno != EPERM)
296 			_exit(EXIT_FAILURE);
297 
298 		_exit(EXIT_SUCCESS);
299 	}
300 
301 	(void)wait(&sta);
302 
303 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
304 		atf_tc_fail("fchroot(2) succeeded as unprivileged user");
305 }
306 #endif
307 
308 ATF_TP_ADD_TCS(tp)
309 {
310 
311 	ATF_TP_ADD_TC(tp, chroot_basic);
312 	ATF_TP_ADD_TC(tp, chroot_err);
313 	ATF_TP_ADD_TC(tp, chroot_perm);
314 #ifdef __NetBSD__
315 	ATF_TP_ADD_TC(tp, fchroot_basic);
316 	ATF_TP_ADD_TC(tp, fchroot_err);
317 	ATF_TP_ADD_TC(tp, fchroot_perm);
318 #endif
319 
320 	return atf_no_error();
321 }
322