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