etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_chroot.c
blobce7170875f24dc29f5a5ed8b35f9017be5b68b03
1 /* $NetBSD: t_chroot.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.1 2011/07/07 06:57:53 jruoho Exp $");
34 #include <sys/wait.h>
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>
45 ATF_TC(chroot_basic);
46 ATF_TC_HEAD(chroot_basic, tc)
48 atf_tc_set_md_var(tc, "descr", "A basic test of chroot(2)");
49 atf_tc_set_md_var(tc, "require.user", "root");
52 ATF_TC_BODY(chroot_basic, tc)
54 char buf[PATH_MAX];
55 int fd, sta;
56 pid_t pid;
58 (void)memset(buf, '\0', sizeof(buf));
59 (void)getcwd(buf, sizeof(buf));
60 (void)strlcat(buf, "/dir", sizeof(buf));
62 ATF_REQUIRE(mkdir(buf, 0500) == 0);
63 ATF_REQUIRE(chdir(buf) == 0);
65 pid = fork();
66 ATF_REQUIRE(pid >= 0);
68 if (pid == 0) {
70 if (chroot(buf) != 0)
71 _exit(EXIT_FAILURE);
73 errno = 0;
75 if (chroot("/root") != -1)
76 _exit(EXIT_FAILURE);
78 if (errno != ENOENT)
79 _exit(EXIT_FAILURE);
81 fd = open("file", O_RDONLY | O_CREAT, 0600);
83 if (fd < 0)
84 _exit(EXIT_FAILURE);
86 if (close(fd) != 0)
87 _exit(EXIT_FAILURE);
89 _exit(EXIT_SUCCESS);
92 (void)wait(&sta);
94 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
95 atf_tc_fail("chroot(2) failed");
97 (void)chdir("/");
98 (void)strlcat(buf, "/file", sizeof(buf));
100 fd = open(buf, O_RDONLY);
102 if (fd < 0)
103 atf_tc_fail("chroot(2) did not change the root directory");
105 ATF_REQUIRE(close(fd) == 0);
106 ATF_REQUIRE(unlink(buf) == 0);
109 ATF_TC(chroot_err);
110 ATF_TC_HEAD(chroot_err, tc)
112 atf_tc_set_md_var(tc, "descr", "Test error conditions of chroot(2)");
113 atf_tc_set_md_var(tc, "require.user", "root");
116 ATF_TC_BODY(chroot_err, tc)
118 char buf[PATH_MAX + 1];
120 (void)memset(buf, 'x', sizeof(buf));
122 errno = 0;
123 ATF_REQUIRE_ERRNO(ENAMETOOLONG, chroot(buf) == -1);
125 errno = 0;
126 ATF_REQUIRE_ERRNO(EFAULT, chroot((void *)-1) == -1);
128 errno = 0;
129 ATF_REQUIRE_ERRNO(ENOENT, chroot("/a/b/c/d/e/f/g/h/i/j") == -1);
132 ATF_TC(chroot_perm);
133 ATF_TC_HEAD(chroot_perm, tc)
135 atf_tc_set_md_var(tc, "descr", "Test permissions with chroot(2)");
136 atf_tc_set_md_var(tc, "require.user", "unprivileged");
139 ATF_TC_BODY(chroot_perm, tc)
141 static char buf[LINE_MAX];
142 pid_t pid;
143 int sta;
145 (void)memset(buf, '\0', sizeof(buf));
146 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
148 pid = fork();
149 ATF_REQUIRE(pid >= 0);
151 if (pid == 0) {
153 errno = 0;
155 if (chroot(buf) != -1)
156 _exit(EXIT_FAILURE);
158 if (errno != EPERM)
159 _exit(EXIT_FAILURE);
161 _exit(EXIT_SUCCESS);
164 (void)wait(&sta);
166 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
167 atf_tc_fail("chroot(2) succeeded as unprivileged user");
170 ATF_TC(fchroot_basic);
171 ATF_TC_HEAD(fchroot_basic, tc)
173 atf_tc_set_md_var(tc, "descr", "A basic test of fchroot(2)");
174 atf_tc_set_md_var(tc, "require.user", "root");
177 ATF_TC_BODY(fchroot_basic, tc)
179 char buf[PATH_MAX];
180 int fd, sta;
181 pid_t pid;
183 (void)memset(buf, '\0', sizeof(buf));
184 (void)getcwd(buf, sizeof(buf));
185 (void)strlcat(buf, "/dir", sizeof(buf));
187 ATF_REQUIRE(mkdir(buf, 0500) == 0);
188 ATF_REQUIRE(chdir(buf) == 0);
190 fd = open(buf, O_RDONLY);
191 ATF_REQUIRE(fd >= 0);
193 pid = fork();
194 ATF_REQUIRE(pid >= 0);
196 if (pid == 0) {
198 if (fchroot(fd) != 0)
199 _exit(EXIT_FAILURE);
201 if (close(fd) != 0)
202 _exit(EXIT_FAILURE);
204 fd = open("file", O_RDONLY | O_CREAT, 0600);
206 if (fd < 0)
207 _exit(EXIT_FAILURE);
209 if (close(fd) != 0)
210 _exit(EXIT_FAILURE);
212 _exit(EXIT_SUCCESS);
215 (void)wait(&sta);
217 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
218 atf_tc_fail("fchroot(2) failed");
220 (void)chdir("/");
221 (void)strlcat(buf, "/file", sizeof(buf));
223 fd = open(buf, O_RDONLY);
225 if (fd < 0)
226 atf_tc_fail("fchroot(2) did not change the root directory");
228 ATF_REQUIRE(close(fd) == 0);
229 ATF_REQUIRE(unlink(buf) == 0);
232 ATF_TC(fchroot_err);
233 ATF_TC_HEAD(fchroot_err, tc)
235 atf_tc_set_md_var(tc, "descr", "Test error conditions of fchroot(2)");
236 atf_tc_set_md_var(tc, "require.user", "root");
239 ATF_TC_BODY(fchroot_err, tc)
241 int fd;
243 fd = open("/etc/passwd", O_RDONLY);
244 ATF_REQUIRE(fd >= 0);
246 errno = 0;
247 ATF_REQUIRE_ERRNO(EBADF, fchroot(-1) == -1);
249 errno = 0;
250 ATF_REQUIRE_ERRNO(ENOTDIR, fchroot(fd) == -1);
252 ATF_REQUIRE(close(fd) == 0);
255 ATF_TC(fchroot_perm);
256 ATF_TC_HEAD(fchroot_perm, tc)
258 atf_tc_set_md_var(tc, "descr", "Test permissions with fchroot(2)");
259 atf_tc_set_md_var(tc, "require.user", "root");
262 ATF_TC_BODY(fchroot_perm, tc)
264 static char buf[LINE_MAX];
265 struct passwd *pw;
266 int fd, sta;
267 pid_t pid;
269 (void)memset(buf, '\0', sizeof(buf));
270 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
272 pw = getpwnam("nobody");
273 fd = open(buf, O_RDONLY);
275 ATF_REQUIRE(fd >= 0);
276 ATF_REQUIRE(pw != NULL);
278 pid = fork();
279 ATF_REQUIRE(pid >= 0);
281 if (pid == 0) {
283 (void)setuid(pw->pw_uid);
285 errno = 0;
287 if (fchroot(fd) != -1)
288 _exit(EXIT_FAILURE);
290 if (errno != EPERM)
291 _exit(EXIT_FAILURE);
293 _exit(EXIT_SUCCESS);
296 (void)wait(&sta);
298 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
299 atf_tc_fail("fchroot(2) succeeded as unprivileged user");
302 ATF_TP_ADD_TCS(tp)
305 ATF_TP_ADD_TC(tp, chroot_basic);
306 ATF_TP_ADD_TC(tp, chroot_err);
307 ATF_TP_ADD_TC(tp, chroot_perm);
308 ATF_TP_ADD_TC(tp, fchroot_basic);
309 ATF_TP_ADD_TC(tp, fchroot_err);
310 ATF_TP_ADD_TC(tp, fchroot_perm);
312 return atf_no_error();