1 /* $NetBSD: t_chroot.c,v 1.2 2017/01/10 22:36:29 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_chroot.c,v 1.2 2017/01/10 22:36:29 christos Exp $");
47 ATF_TC_HEAD(chroot_basic, tc)
49 atf_tc_set_md_var(tc, "descr", "A basic test of chroot(2)");
50 atf_tc_set_md_var(tc, "require.user", "root");
53 ATF_TC_BODY(chroot_basic, tc)
59 (void)memset(buf, '\0', sizeof(buf));
60 (void)getcwd(buf, sizeof(buf));
61 (void)strlcat(buf, "/dir", sizeof(buf));
63 ATF_REQUIRE(mkdir(buf, 0500) == 0);
64 ATF_REQUIRE(chdir(buf) == 0);
67 ATF_REQUIRE(pid >= 0);
76 if (chroot("/root") != -1)
82 fd = open("file", O_RDONLY | O_CREAT, 0600);
95 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
96 atf_tc_fail("chroot(2) failed");
99 (void)strlcat(buf, "/file", sizeof(buf));
101 fd = open(buf, O_RDONLY);
104 atf_tc_fail("chroot(2) did not change the root directory");
106 ATF_REQUIRE(close(fd) == 0);
107 ATF_REQUIRE(unlink(buf) == 0);
111 ATF_TC_HEAD(chroot_err, tc)
113 atf_tc_set_md_var(tc, "descr", "Test error conditions of chroot(2)");
114 atf_tc_set_md_var(tc, "require.user", "root");
117 ATF_TC_BODY(chroot_err, tc)
119 char buf[PATH_MAX + 1];
121 (void)memset(buf, 'x', sizeof(buf));
124 ATF_REQUIRE_ERRNO(ENAMETOOLONG, chroot(buf) == -1);
127 ATF_REQUIRE_ERRNO(EFAULT, chroot((void *)-1) == -1);
130 ATF_REQUIRE_ERRNO(ENOENT, chroot("/a/b/c/d/e/f/g/h/i/j") == -1);
134 ATF_TC_HEAD(chroot_perm, tc)
136 atf_tc_set_md_var(tc, "descr", "Test permissions with chroot(2)");
137 atf_tc_set_md_var(tc, "require.user", "unprivileged");
140 ATF_TC_BODY(chroot_perm, tc)
142 static char buf[LINE_MAX];
146 (void)memset(buf, '\0', sizeof(buf));
147 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
150 ATF_REQUIRE(pid >= 0);
156 if (chroot(buf) != -1)
167 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
168 atf_tc_fail("chroot(2) succeeded as unprivileged user");
172 ATF_TC(fchroot_basic);
173 ATF_TC_HEAD(fchroot_basic, tc)
175 atf_tc_set_md_var(tc, "descr", "A basic test of fchroot(2)");
176 atf_tc_set_md_var(tc, "require.user", "root");
179 ATF_TC_BODY(fchroot_basic, tc)
185 (void)memset(buf, '\0', sizeof(buf));
186 (void)getcwd(buf, sizeof(buf));
187 (void)strlcat(buf, "/dir", sizeof(buf));
189 ATF_REQUIRE(mkdir(buf, 0500) == 0);
190 ATF_REQUIRE(chdir(buf) == 0);
192 fd = open(buf, O_RDONLY);
193 ATF_REQUIRE(fd >= 0);
196 ATF_REQUIRE(pid >= 0);
200 if (fchroot(fd) != 0)
206 fd = open("file", O_RDONLY | O_CREAT, 0600);
219 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
220 atf_tc_fail("fchroot(2) failed");
223 (void)strlcat(buf, "/file", sizeof(buf));
225 fd = open(buf, O_RDONLY);
228 atf_tc_fail("fchroot(2) did not change the root directory");
230 ATF_REQUIRE(close(fd) == 0);
231 ATF_REQUIRE(unlink(buf) == 0);
235 ATF_TC_HEAD(fchroot_err, tc)
237 atf_tc_set_md_var(tc, "descr", "Test error conditions of fchroot(2)");
238 atf_tc_set_md_var(tc, "require.user", "root");
241 ATF_TC_BODY(fchroot_err, tc)
245 fd = open("/etc/passwd", O_RDONLY);
246 ATF_REQUIRE(fd >= 0);
249 ATF_REQUIRE_ERRNO(EBADF, fchroot(-1) == -1);
252 ATF_REQUIRE_ERRNO(ENOTDIR, fchroot(fd) == -1);
254 ATF_REQUIRE(close(fd) == 0);
257 ATF_TC(fchroot_perm);
258 ATF_TC_HEAD(fchroot_perm, tc)
260 atf_tc_set_md_var(tc, "descr", "Test permissions with fchroot(2)");
261 atf_tc_set_md_var(tc, "require.user", "root");
264 ATF_TC_BODY(fchroot_perm, tc)
266 static char buf[LINE_MAX];
271 (void)memset(buf, '\0', sizeof(buf));
272 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
274 pw = getpwnam("nobody");
275 fd = open(buf, O_RDONLY);
277 ATF_REQUIRE(fd >= 0);
278 ATF_REQUIRE(pw != NULL);
281 ATF_REQUIRE(pid >= 0);
285 (void)setuid(pw->pw_uid);
289 if (fchroot(fd) != -1)
300 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
301 atf_tc_fail("fchroot(2) succeeded as unprivileged user");
308 ATF_TP_ADD_TC(tp, chroot_basic);
309 ATF_TP_ADD_TC(tp, chroot_err);
310 ATF_TP_ADD_TC(tp, chroot_perm);
312 ATF_TP_ADD_TC(tp, fchroot_basic);
313 ATF_TP_ADD_TC(tp, fchroot_err);
314 ATF_TP_ADD_TC(tp, fchroot_perm);
317 return atf_no_error();