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 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/capsicum.h>
32 #include <sys/sysctl.h>
40 #include "freebsd_test_suite/macros.h"
42 static int dirfd = -1;
46 touchat(int _dirfd, const char *name)
50 ATF_REQUIRE((fd = openat(_dirfd, name, O_CREAT | O_TRUNC | O_WRONLY,
52 ATF_REQUIRE(close(fd) == 0);
56 prepare_dotdot_tests(void)
60 ATF_REQUIRE(getcwd(cwd, sizeof(cwd)) != NULL);
61 asprintf(&abspath, "%s/testdir/d1/f1", cwd);
63 ATF_REQUIRE(mkdir("testdir", 0777) == 0);
64 ATF_REQUIRE((dirfd = open("testdir", O_RDONLY)) >= 0);
66 ATF_REQUIRE(mkdirat(dirfd, "d1", 0777) == 0);
67 ATF_REQUIRE(mkdirat(dirfd, "d1/d2", 0777) == 0);
68 ATF_REQUIRE(mkdirat(dirfd, "d1/d2/d3", 0777) == 0);
69 touchat(dirfd, "d1/f1");
70 touchat(dirfd, "d1/d2/f2");
71 touchat(dirfd, "d1/d2/d3/f3");
72 ATF_REQUIRE(symlinkat("d1/d2/d3", dirfd, "l3") == 0);
73 ATF_REQUIRE(symlinkat("../testdir/d1", dirfd, "lup") == 0);
74 ATF_REQUIRE(symlinkat("../..", dirfd, "d1/d2/d3/ld1") == 0);
75 ATF_REQUIRE(symlinkat("../../f1", dirfd, "d1/d2/d3/lf1") == 0);
81 ATF_REQUIRE_FEATURE("security_capabilities");
82 ATF_REQUIRE_FEATURE("security_capability_mode");
83 ATF_REQUIRE_SYSCTL_BOOL("kern.trap_enotcap", false);
89 ATF_TC(openat__basic_positive);
90 ATF_TC_HEAD(openat__basic_positive, tc)
92 atf_tc_set_md_var(tc, "descr", "Basic positive openat testcases");
95 ATF_TC_BODY(openat__basic_positive, tc)
97 prepare_dotdot_tests();
99 ATF_REQUIRE(openat(dirfd, "d1/d2/d3/f3", O_RDONLY) >= 0);
100 ATF_REQUIRE(openat(dirfd, "d1/d2/d3/../../f1", O_RDONLY) >= 0);
101 ATF_REQUIRE(openat(dirfd, "l3/f3", O_RDONLY) >= 0);
102 ATF_REQUIRE(openat(dirfd, "l3/../../f1", O_RDONLY) >= 0);
103 ATF_REQUIRE(openat(dirfd, "../testdir/d1/f1", O_RDONLY) >= 0);
104 ATF_REQUIRE(openat(dirfd, "lup/f1", O_RDONLY) >= 0);
105 ATF_REQUIRE(openat(dirfd, "l3/ld1", O_RDONLY) >= 0);
106 ATF_REQUIRE(openat(dirfd, "l3/lf1", O_RDONLY) >= 0);
107 ATF_REQUIRE(open(abspath, O_RDONLY) >= 0);
108 ATF_REQUIRE(openat(dirfd, abspath, O_RDONLY) >= 0);
111 ATF_TC(lookup_cap_dotdot__basic);
112 ATF_TC_HEAD(lookup_cap_dotdot__basic, tc)
114 atf_tc_set_md_var(tc, "descr",
115 "Validate cap-mode (testdir)/d1/.. lookup");
118 ATF_TC_BODY(lookup_cap_dotdot__basic, tc)
123 prepare_dotdot_tests();
125 cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
126 ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
128 ATF_REQUIRE(cap_enter() >= 0);
130 ATF_REQUIRE_MSG(openat(dirfd, "d1/..", O_RDONLY) >= 0, "%s",
134 ATF_TC(lookup_cap_dotdot__advanced);
135 ATF_TC_HEAD(lookup_cap_dotdot__advanced, tc)
137 atf_tc_set_md_var(tc, "descr",
138 "Validate cap-mode (testdir)/d1/.. lookup");
141 ATF_TC_BODY(lookup_cap_dotdot__advanced, tc)
146 prepare_dotdot_tests();
148 cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
149 ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
151 ATF_REQUIRE(cap_enter() >= 0);
153 ATF_REQUIRE(openat(dirfd, "d1/d2/d3/../../f1", O_RDONLY) >= 0);
154 ATF_REQUIRE(openat(dirfd, "l3/../../f1", O_RDONLY) >= 0);
155 ATF_REQUIRE(openat(dirfd, "l3/ld1", O_RDONLY) >= 0);
156 ATF_REQUIRE(openat(dirfd, "l3/lf1", O_RDONLY) >= 0);
162 ATF_TC(openat__basic_negative);
163 ATF_TC_HEAD(openat__basic_negative, tc)
165 atf_tc_set_md_var(tc, "descr", "Basic negative openat testcases");
168 ATF_TC_BODY(openat__basic_negative, tc)
170 prepare_dotdot_tests();
172 ATF_REQUIRE_ERRNO(ENOENT,
173 openat(dirfd, "does-not-exist", O_RDONLY) < 0);
174 ATF_REQUIRE_ERRNO(ENOENT,
175 openat(dirfd, "l3/does-not-exist", O_RDONLY) < 0);
178 ATF_TC(capmode__negative);
179 ATF_TC_HEAD(capmode__negative, tc)
181 atf_tc_set_md_var(tc, "descr", "Negative Capability mode testcases");
184 ATF_TC_BODY(capmode__negative, tc)
189 prepare_dotdot_tests();
191 ATF_REQUIRE(cap_enter() == 0);
193 /* open() not permitted in capability mode */
194 ATF_REQUIRE_ERRNO(ECAPMODE, open("testdir", O_RDONLY) < 0);
196 /* AT_FDCWD not permitted in capability mode */
197 ATF_REQUIRE_ERRNO(ECAPMODE, openat(AT_FDCWD, "d1/f1", O_RDONLY) < 0);
199 /* Relative path above dirfd not capable */
200 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "..", O_RDONLY) < 0);
201 ATF_REQUIRE((subdirfd = openat(dirfd, "l3", O_RDONLY)) >= 0);
202 ATF_REQUIRE_ERRNO(ENOTCAPABLE,
203 openat(subdirfd, "../../f1", O_RDONLY) < 0);
205 /* Absolute paths not capable */
206 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, abspath, O_RDONLY) < 0);
208 /* Symlink above dirfd */
209 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "lup/f1", O_RDONLY) < 0);
212 ATF_TC(lookup_cap_dotdot__negative);
213 ATF_TC_HEAD(lookup_cap_dotdot__negative, tc)
215 atf_tc_set_md_var(tc, "descr",
216 "Validate cap-mode (testdir)/.. lookup fails");
219 ATF_TC_BODY(lookup_cap_dotdot__negative, tc)
224 prepare_dotdot_tests();
226 cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
227 ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
229 ATF_REQUIRE(cap_enter() >= 0);
231 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "..", O_RDONLY) < 0);
232 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "d1/../..", O_RDONLY) < 0);
233 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "../testdir/d1/f1", O_RDONLY) < 0);
236 ATF_TC(lookup_cap_dotdot__root);
237 ATF_TC_HEAD(lookup_cap_dotdot__root, tc)
239 atf_tc_set_md_var(tc, "descr", "Validate cap-mode /.. lookup fails");
242 ATF_TC_BODY(lookup_cap_dotdot__root, tc)
248 dfd = open("/", O_DIRECTORY);
249 ATF_REQUIRE(dfd >= 0);
251 dfd2 = openat(dfd, "..", O_DIRECTORY);
252 ATF_REQUIRE(dfd2 >= 0);
253 ATF_REQUIRE(close(dfd2) == 0);
255 ATF_REQUIRE(cap_enter() >= 0);
257 dfd2 = openat(dfd, "..", O_DIRECTORY);
258 ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dfd, "..", O_DIRECTORY));
264 ATF_TP_ADD_TC(tp, openat__basic_positive);
265 ATF_TP_ADD_TC(tp, openat__basic_negative);
267 ATF_TP_ADD_TC(tp, capmode__negative);
269 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__basic);
270 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__advanced);
271 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__negative);
272 ATF_TP_ADD_TC(tp, lookup_cap_dotdot__root);
274 return (atf_no_error());