2 * Copyright (c) 2009-2011 Robert N. M. Watson
3 * Copyright (c) 2011 Jonathan Anderson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/capability.h>
35 #include <sys/errno.h>
46 * Test openat(2) in a variety of sitations to ensure that it obeys Capsicum
47 * "strict relative" rules:
49 * 1. Use strict relative lookups in capability mode or when operating
50 * relative to a capability.
51 * 2. When performing strict relative lookups, absolute paths (including
52 * symlinks to absolute paths) are not allowed, nor are paths containing
59 int fd, etc, etc_cap, etc_cap_ro, etc_cap_base, etc_cap_all;
60 cap_rights_t baserights = CAP_READ | CAP_WRITE | CAP_SEEK | CAP_LOOKUP;
63 REQUIRE(etc = open("/etc/", O_RDONLY));
64 CHECK_SYSCALL_FAILS(EINVAL, cap_getrights, etc, &rights);
66 MAKE_CAPABILITY(etc_cap, etc, CAP_READ);
67 MAKE_CAPABILITY(etc_cap_ro, etc, CAP_READ | CAP_LOOKUP);
68 MAKE_CAPABILITY(etc_cap_base, etc, baserights);
69 MAKE_CAPABILITY(etc_cap_all, etc, CAP_MASK_VALID);
72 * openat(2) with regular file descriptors in non-capability mode
73 * should Just Work (tm).
75 CHECK_SYSCALL_SUCCEEDS(openat, etc, "/etc/passwd", O_RDONLY);
76 CHECK_SYSCALL_SUCCEEDS(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
77 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
78 CHECK_SYSCALL_SUCCEEDS(openat, etc, "../etc/passwd", O_RDONLY);
81 * Lookups relative to capabilities should be strictly relative.
83 * When not in capability mode, we don't actually require CAP_LOOKUP.
85 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
86 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
87 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
89 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
90 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
93 * This requires discussion: do we treat a capability with
94 * CAP_MASK_VALID *exactly* like a non-capability file descriptor
95 * (currently, the implementation says yes)?
97 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY);
100 * A file opened relative to a capability should itself be a capability.
102 CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights);
104 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
105 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
106 CHECK_RIGHTS(rights, baserights);
109 * Enter capability mode; now ALL lookups are strictly relative.
111 REQUIRE(cap_enter());
114 * Relative lookups on regular files or capabilities with CAP_LOOKUP
117 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
118 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
119 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
120 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
123 * Lookup relative to capabilities without CAP_LOOKUP should fail.
125 CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY);
128 * Absolute lookups should fail.
130 CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
131 CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY);
134 * Lookups containing '..' should fail in capability mode.
136 CHECK_NOTCAPABLE(openat, etc, "../etc/passwd", O_RDONLY);
137 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
138 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
140 REQUIRE(fd = openat(etc, "passwd", O_RDONLY));
141 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
144 * A file opened relative to a capability should itself be a capability.
146 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
147 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
148 CHECK_RIGHTS(rights, baserights);