1 /*-
2 * Copyright (c) 2009-2011 Robert N. M. Watson
3 * Copyright (c) 2011 Jonathan Anderson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/capsicum.h>
31 #include <sys/errno.h>
32
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include "cap_test.h"
40
41 /*
42 * Test openat(2) in a variety of sitations to ensure that it obeys Capsicum
43 * "strict relative" rules:
44 *
45 * 1. Use strict relative lookups in capability mode or when operating
46 * relative to a capability.
47 * 2. When performing strict relative lookups, absolute paths (including
48 * symlinks to absolute paths) are not allowed, nor are paths containing
49 * '..' components.
50 */
51 int
test_relative(void)52 test_relative(void)
53 {
54 int success = PASSED;
55 int fd, etc, etc_cap, etc_cap_ro, etc_cap_base, etc_cap_all;
56 cap_rights_t baserights = CAP_READ | CAP_WRITE | CAP_SEEK | CAP_LOOKUP;
57 cap_rights_t rights;
58
59 REQUIRE(etc = open("/etc/", O_RDONLY));
60 CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc, &rights);
61 CHECK_RIGHTS(rights, CAP_ALL);
62
63 MAKE_CAPABILITY(etc_cap, etc, CAP_READ);
64 MAKE_CAPABILITY(etc_cap_ro, etc, CAP_READ | CAP_LOOKUP);
65 MAKE_CAPABILITY(etc_cap_base, etc, baserights);
66 MAKE_CAPABILITY(etc_cap_all, etc, CAP_MASK_VALID);
67
68 /*
69 * openat(2) with regular file descriptors in non-capability mode
70 * should Just Work (tm).
71 */
72 CHECK_SYSCALL_SUCCEEDS(openat, etc, "/etc/passwd", O_RDONLY);
73 CHECK_SYSCALL_SUCCEEDS(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
74 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
75 CHECK_SYSCALL_SUCCEEDS(openat, etc, "../etc/passwd", O_RDONLY);
76
77 /*
78 * Lookups relative to capabilities should be strictly relative.
79 *
80 * When not in capability mode, we don't actually require CAP_LOOKUP.
81 */
82 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
83 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
84 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
85
86 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
87 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
88
89 /*
90 * This requires discussion: do we treat a capability with
91 * CAP_MASK_VALID *exactly* like a non-capability file descriptor
92 * (currently, the implementation says yes)?
93 */
94 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY);
95
96 /*
97 * A file opened relative to a capability should itself be a capability.
98 */
99 CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights);
100
101 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
102 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
103 CHECK_RIGHTS(rights, baserights);
104
105 /*
106 * Enter capability mode; now ALL lookups are strictly relative.
107 */
108 REQUIRE(cap_enter());
109
110 /*
111 * Relative lookups on regular files or capabilities with CAP_LOOKUP
112 * ought to succeed.
113 */
114 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
115 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
116 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
117 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
118
119 /*
120 * Lookup relative to capabilities without CAP_LOOKUP should fail.
121 */
122 CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY);
123
124 /*
125 * Absolute lookups should fail.
126 */
127 CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
128 CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY);
129
130 /*
131 * Lookups containing '..' should fail in capability mode.
132 */
133 CHECK_NOTCAPABLE(openat, etc, "../etc/passwd", O_RDONLY);
134 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
135 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
136
137 REQUIRE(fd = openat(etc, "passwd", O_RDONLY));
138 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
139
140 /*
141 * A file opened relative to a capability should itself be a capability.
142 */
143 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
144 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
145 CHECK_RIGHTS(rights, baserights);
146
147 return success;
148 }
149