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/capsicum.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_SUCCEEDS(cap_getrights, etc, &rights);
65 CHECK_RIGHTS(rights, CAP_ALL);
67 MAKE_CAPABILITY(etc_cap, etc, CAP_READ);
68 MAKE_CAPABILITY(etc_cap_ro, etc, CAP_READ | CAP_LOOKUP);
69 MAKE_CAPABILITY(etc_cap_base, etc, baserights);
70 MAKE_CAPABILITY(etc_cap_all, etc, CAP_MASK_VALID);
73 * openat(2) with regular file descriptors in non-capability mode
74 * should Just Work (tm).
76 CHECK_SYSCALL_SUCCEEDS(openat, etc, "/etc/passwd", O_RDONLY);
77 CHECK_SYSCALL_SUCCEEDS(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
78 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
79 CHECK_SYSCALL_SUCCEEDS(openat, etc, "../etc/passwd", O_RDONLY);
82 * Lookups relative to capabilities should be strictly relative.
84 * When not in capability mode, we don't actually require CAP_LOOKUP.
86 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
87 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
88 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
90 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
91 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
94 * This requires discussion: do we treat a capability with
95 * CAP_MASK_VALID *exactly* like a non-capability file descriptor
96 * (currently, the implementation says yes)?
98 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY);
101 * A file opened relative to a capability should itself be a capability.
103 CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights);
105 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
106 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
107 CHECK_RIGHTS(rights, baserights);
110 * Enter capability mode; now ALL lookups are strictly relative.
112 REQUIRE(cap_enter());
115 * Relative lookups on regular files or capabilities with CAP_LOOKUP
118 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
119 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
120 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
121 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
124 * Lookup relative to capabilities without CAP_LOOKUP should fail.
126 CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY);
129 * Absolute lookups should fail.
131 CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
132 CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY);
135 * Lookups containing '..' should fail in capability mode.
137 CHECK_NOTCAPABLE(openat, etc, "../etc/passwd", O_RDONLY);
138 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
139 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
141 REQUIRE(fd = openat(etc, "passwd", O_RDONLY));
142 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
145 * A file opened relative to a capability should itself be a capability.
147 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
148 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
149 CHECK_RIGHTS(rights, baserights);