2 * Copyright (c) 2016 Ed Maste <emaste@FreeBSD.org>
3 * Copyright (c) 2016 Conrad Meyer <cem@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/capsicum.h>
30 #include <sys/sysctl.h>
38 #include "freebsd_test_suite/macros.h"
40 static int dirfd = -1;
44 touchat(int _dirfd, const char *name)
48 ATF_REQUIRE((fd = openat(_dirfd, name, O_CREAT | O_TRUNC | O_WRONLY,
50 ATF_REQUIRE(close(fd) == 0);
54 prepare_dotdot_tests(void)
58 ATF_REQUIRE(getcwd(cwd, sizeof(cwd)) != NULL);
59 asprintf(&abspath, "%s/testdir/d1/f1", cwd);
61 ATF_REQUIRE(mkdir("testdir", 0777) == 0);
62 ATF_REQUIRE((dirfd = open("testdir", O_RDONLY)) >= 0);
64 ATF_REQUIRE(mkdirat(dirfd, "d1", 0777) == 0);
65 ATF_REQUIRE(mkdirat(dirfd, "d1/d2", 0777) == 0);
66 ATF_REQUIRE(mkdirat(dirfd, "d1/d2/d3", 0777) == 0);
67 touchat(dirfd, "d1/f1");
68 touchat(dirfd, "d1/d2/f2");
69 touchat(dirfd, "d1/d2/d3/f3");
70 ATF_REQUIRE(symlinkat("d1/d2/d3", dirfd, "l3") == 0);
71 ATF_REQUIRE(symlinkat("../testdir/d1", dirfd, "lup") == 0);
72 ATF_REQUIRE(symlinkat("../..", dirfd, "d1/d2/d3/ld1") == 0);
73 ATF_REQUIRE(symlinkat("../../f1", dirfd, "d1/d2/d3/lf1") == 0);
79 ATF_REQUIRE_FEATURE("security_capabilities");
80 ATF_REQUIRE_FEATURE("security_capability_mode");
81 ATF_REQUIRE_SYSCTL_BOOL("kern.trap_enotcap", false);
87 ATF_TC(openat__basic_positive);
88 ATF_TC_HEAD(openat__basic_positive, tc)
90 atf_tc_set_md_var(tc, "descr", "Basic positive openat testcases");
93 ATF_TC_BODY(openat__basic_positive, tc)
95 prepare_dotdot_tests();
97 ATF_REQUIRE(openat(dirfd, "d1/d2/d3/f3", O_RDONLY) >= 0);
98 ATF_REQUIRE(openat(dirfd, "d1/d2/d3/../../f1", O_RDONLY) >= 0);
99 ATF_REQUIRE(openat(dirfd, "l3/f3", O_RDONLY) >= 0);
100 ATF_REQUIRE(openat(dirfd, "l3/../../f1", O_RDONLY) >= 0);
101 ATF_REQUIRE(openat(dirfd, "../testdir/d1/f1", O_RDONLY) >= 0);
102 ATF_REQUIRE(openat(dirfd, "lup/f1", O_RDONLY) >= 0);
103 ATF_REQUIRE(openat(dirfd, "l3/ld1", O_RDONLY) >= 0);
104 ATF_REQUIRE(openat(dirfd, "l3/lf1", O_RDONLY) >= 0);
105 ATF_REQUIRE(open(abspath, O_RDONLY) >= 0);
106 ATF_REQUIRE(openat(dirfd, abspath, O_RDONLY) >= 0);
109 ATF_TC(lookup_cap_dotdot__basic);
110 ATF_TC_HEAD(lookup_cap_dotdot__basic, tc)
112 atf_tc_set_md_var(tc, "descr",
113 "Validate cap-mode (testdir)/d1/.. lookup");
116 ATF_TC_BODY(lookup_cap_dotdot__basic, tc)
121 prepare_dotdot_tests();
123 cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
124 ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
126 ATF_REQUIRE(cap_enter() >= 0);
128 ATF_REQUIRE_MSG(openat(dirfd, "d1/..", O_RDONLY) >= 0, "%s",
132 ATF_TC(lookup_cap_dotdot__advanced);
133 ATF_TC_HEAD(lookup_cap_dotdot__advanced, tc)
135 atf_tc_set_md_var(tc, "descr",
136 "Validate cap-mode (testdir)/d1/.. lookup");
139 ATF_TC_BODY(lookup_cap_dotdot__advanced, tc)
144 prepare_dotdot_tests();
146 cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
147 ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
149 ATF_REQUIRE(cap_enter() >= 0);
151 ATF_REQUIRE(openat(dirfd, "d1/d2/d3/../../f1", O_RDONLY) >= 0);
152 ATF_REQUIRE(openat(dirfd, "l3/../../f1", O_RDONLY) >= 0);
153 ATF_REQUIRE(openat(dirfd, "l3/ld1", O_RDONLY) >= 0);
154 ATF_REQUIRE(openat(dirfd, "l3/lf1", O_RDONLY) >= 0);
160 ATF_TC(openat__basic_negative);
161 ATF_TC_HEAD(openat__basic_negative, tc)
163 atf_tc_set_md_var(tc, "descr", "Basic negative openat testcases");
166 ATF_TC_BODY(openat__basic_negative, tc)
168 prepare_dotdot_tests();
170 ATF_REQUIRE_ERRNO(ENOENT,
171 openat(dirfd, "does-not-exist", O_RDONLY) < 0);
172 ATF_REQUIRE_ERRNO(ENOENT,
173 openat(dirfd, "l3/does-not-exist", O_RDONLY) < 0);
176 ATF_TC(capmode__negative);
177 ATF_TC_HEAD(capmode__negative, tc)
179 atf_tc_set_md_var(tc, "descr", "Negative Capability mode testcases");
182 ATF_TC_BODY(capmode__negative, tc)
187 prepare_dotdot_tests();
189 ATF_REQUIRE(cap_enter() == 0);
191 /* open() not permitted in capability mode */
192 ATF_REQUIRE_ERRNO(ECAPMODE, open("testdir", O_RDONLY) < 0);
194 /* AT_FDCWD not permitted in capability mode */
195 ATF_REQUIRE_ERRNO(ECAPMODE, openat(AT_FDCWD, "d1/f1", O_RDONLY) < 0);
197 /* Relative path above dirfd not capable */
198 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "..", O_RDONLY) < 0);
199 ATF_REQUIRE((subdirfd = openat(dirfd, "l3", O_RDONLY)) >= 0);
200 ATF_REQUIRE_ERRNO(ENOTCAPABLE,
201 openat(subdirfd, "../../f1", O_RDONLY) < 0);
203 /* Absolute paths not capable */
204 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, abspath, O_RDONLY) < 0);
206 /* Symlink above dirfd */
207 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "lup/f1", O_RDONLY) < 0);
210 ATF_TC(lookup_cap_dotdot__negative);
211 ATF_TC_HEAD(lookup_cap_dotdot__negative, tc)
213 atf_tc_set_md_var(tc, "descr",
214 "Validate cap-mode (testdir)/.. lookup fails");
217 ATF_TC_BODY(lookup_cap_dotdot__negative, tc)
222 prepare_dotdot_tests();
224 cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
225 ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
227 ATF_REQUIRE(cap_enter() >= 0);
229 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "..", O_RDONLY) < 0);
230 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "d1/../..", O_RDONLY) < 0);
231 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "../testdir/d1/f1", O_RDONLY) < 0);
234 ATF_TC(lookup_cap_dotdot__root);
235 ATF_TC_HEAD(lookup_cap_dotdot__root, tc)
237 atf_tc_set_md_var(tc, "descr", "Validate cap-mode /.. lookup fails");
240 ATF_TC_BODY(lookup_cap_dotdot__root, tc)
246 dfd = open("/", O_DIRECTORY);
247 ATF_REQUIRE(dfd >= 0);
249 dfd2 = openat(dfd, "..", O_DIRECTORY);
250 ATF_REQUIRE(dfd2 >= 0);
251 ATF_REQUIRE(close(dfd2) == 0);
253 ATF_REQUIRE(cap_enter() >= 0);
255 dfd2 = openat(dfd, "..", O_DIRECTORY);
256 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dfd, "..", O_DIRECTORY));
262 ATF_TP_ADD_TC(tp, openat__basic_positive);
263 ATF_TP_ADD_TC(tp, openat__basic_negative);
265 ATF_TP_ADD_TC(tp, capmode__negative);
267 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__basic);
268 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__advanced);
269 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__negative);
270 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__root);
272 return (atf_no_error());