1a0108be6SJonathan Anderson /*-
2a0108be6SJonathan Anderson * Copyright (c) 2009-2011 Robert N. M. Watson
3a0108be6SJonathan Anderson * Copyright (c) 2011 Jonathan Anderson
4a0108be6SJonathan Anderson * All rights reserved.
5a0108be6SJonathan Anderson *
6a0108be6SJonathan Anderson * Redistribution and use in source and binary forms, with or without
7a0108be6SJonathan Anderson * modification, are permitted provided that the following conditions
8a0108be6SJonathan Anderson * are met:
9a0108be6SJonathan Anderson * 1. Redistributions of source code must retain the above copyright
10a0108be6SJonathan Anderson * notice, this list of conditions and the following disclaimer.
11a0108be6SJonathan Anderson * 2. Redistributions in binary form must reproduce the above copyright
12a0108be6SJonathan Anderson * notice, this list of conditions and the following disclaimer in the
13a0108be6SJonathan Anderson * documentation and/or other materials provided with the distribution.
14a0108be6SJonathan Anderson *
15a0108be6SJonathan Anderson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16a0108be6SJonathan Anderson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a0108be6SJonathan Anderson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a0108be6SJonathan Anderson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19a0108be6SJonathan Anderson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a0108be6SJonathan Anderson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21a0108be6SJonathan Anderson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a0108be6SJonathan Anderson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23a0108be6SJonathan Anderson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24a0108be6SJonathan Anderson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25a0108be6SJonathan Anderson * SUCH DAMAGE.
26a0108be6SJonathan Anderson */
27a0108be6SJonathan Anderson
28a0108be6SJonathan Anderson #include <sys/param.h>
29*b881b8beSRobert Watson #include <sys/capsicum.h>
30a0108be6SJonathan Anderson #include <sys/errno.h>
31a0108be6SJonathan Anderson
32a0108be6SJonathan Anderson #include <err.h>
33a0108be6SJonathan Anderson #include <fcntl.h>
34a0108be6SJonathan Anderson #include <stdlib.h>
35a0108be6SJonathan Anderson #include <string.h>
36a0108be6SJonathan Anderson #include <unistd.h>
37a0108be6SJonathan Anderson
38a0108be6SJonathan Anderson #include "cap_test.h"
39a0108be6SJonathan Anderson
40a0108be6SJonathan Anderson /*
41a0108be6SJonathan Anderson * Test openat(2) in a variety of sitations to ensure that it obeys Capsicum
42a0108be6SJonathan Anderson * "strict relative" rules:
43a0108be6SJonathan Anderson *
44a0108be6SJonathan Anderson * 1. Use strict relative lookups in capability mode or when operating
45a0108be6SJonathan Anderson * relative to a capability.
46a0108be6SJonathan Anderson * 2. When performing strict relative lookups, absolute paths (including
47a0108be6SJonathan Anderson * symlinks to absolute paths) are not allowed, nor are paths containing
48a0108be6SJonathan Anderson * '..' components.
49a0108be6SJonathan Anderson */
50a0108be6SJonathan Anderson int
test_relative(void)51a0108be6SJonathan Anderson test_relative(void)
52a0108be6SJonathan Anderson {
53a0108be6SJonathan Anderson int success = PASSED;
54a0108be6SJonathan Anderson int fd, etc, etc_cap, etc_cap_ro, etc_cap_base, etc_cap_all;
55a0108be6SJonathan Anderson cap_rights_t baserights = CAP_READ | CAP_WRITE | CAP_SEEK | CAP_LOOKUP;
56a0108be6SJonathan Anderson cap_rights_t rights;
57a0108be6SJonathan Anderson
58a0108be6SJonathan Anderson REQUIRE(etc = open("/etc/", O_RDONLY));
59f2908898SPawel Jakub Dawidek CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc, &rights);
60f2908898SPawel Jakub Dawidek CHECK_RIGHTS(rights, CAP_ALL);
61a0108be6SJonathan Anderson
62a0108be6SJonathan Anderson MAKE_CAPABILITY(etc_cap, etc, CAP_READ);
63a0108be6SJonathan Anderson MAKE_CAPABILITY(etc_cap_ro, etc, CAP_READ | CAP_LOOKUP);
64a0108be6SJonathan Anderson MAKE_CAPABILITY(etc_cap_base, etc, baserights);
65a0108be6SJonathan Anderson MAKE_CAPABILITY(etc_cap_all, etc, CAP_MASK_VALID);
66a0108be6SJonathan Anderson
67a0108be6SJonathan Anderson /*
68a0108be6SJonathan Anderson * openat(2) with regular file descriptors in non-capability mode
69a0108be6SJonathan Anderson * should Just Work (tm).
70a0108be6SJonathan Anderson */
71a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc, "/etc/passwd", O_RDONLY);
72a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
73a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
74a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc, "../etc/passwd", O_RDONLY);
75a0108be6SJonathan Anderson
76a0108be6SJonathan Anderson /*
77a0108be6SJonathan Anderson * Lookups relative to capabilities should be strictly relative.
78a0108be6SJonathan Anderson *
79a0108be6SJonathan Anderson * When not in capability mode, we don't actually require CAP_LOOKUP.
80a0108be6SJonathan Anderson */
81a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
82a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
83a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
84a0108be6SJonathan Anderson
85a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
86a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
87a0108be6SJonathan Anderson
88a0108be6SJonathan Anderson /*
89a0108be6SJonathan Anderson * This requires discussion: do we treat a capability with
90a0108be6SJonathan Anderson * CAP_MASK_VALID *exactly* like a non-capability file descriptor
91a0108be6SJonathan Anderson * (currently, the implementation says yes)?
92a0108be6SJonathan Anderson */
93a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY);
94a0108be6SJonathan Anderson
95a0108be6SJonathan Anderson /*
96a0108be6SJonathan Anderson * A file opened relative to a capability should itself be a capability.
97a0108be6SJonathan Anderson */
98a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights);
99a0108be6SJonathan Anderson
100a0108be6SJonathan Anderson REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
101a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
102a0108be6SJonathan Anderson CHECK_RIGHTS(rights, baserights);
103a0108be6SJonathan Anderson
104a0108be6SJonathan Anderson /*
105a0108be6SJonathan Anderson * Enter capability mode; now ALL lookups are strictly relative.
106a0108be6SJonathan Anderson */
107a0108be6SJonathan Anderson REQUIRE(cap_enter());
108a0108be6SJonathan Anderson
109a0108be6SJonathan Anderson /*
110a0108be6SJonathan Anderson * Relative lookups on regular files or capabilities with CAP_LOOKUP
111a0108be6SJonathan Anderson * ought to succeed.
112a0108be6SJonathan Anderson */
113a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY);
114a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY);
115a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY);
116a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY);
117a0108be6SJonathan Anderson
118a0108be6SJonathan Anderson /*
119a0108be6SJonathan Anderson * Lookup relative to capabilities without CAP_LOOKUP should fail.
120a0108be6SJonathan Anderson */
121a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY);
122a0108be6SJonathan Anderson
123a0108be6SJonathan Anderson /*
124a0108be6SJonathan Anderson * Absolute lookups should fail.
125a0108be6SJonathan Anderson */
126a0108be6SJonathan Anderson CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
127a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY);
128a0108be6SJonathan Anderson
129a0108be6SJonathan Anderson /*
130a0108be6SJonathan Anderson * Lookups containing '..' should fail in capability mode.
131a0108be6SJonathan Anderson */
132a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc, "../etc/passwd", O_RDONLY);
133a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
134a0108be6SJonathan Anderson CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
135a0108be6SJonathan Anderson
136a0108be6SJonathan Anderson REQUIRE(fd = openat(etc, "passwd", O_RDONLY));
137a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
138a0108be6SJonathan Anderson
139a0108be6SJonathan Anderson /*
140a0108be6SJonathan Anderson * A file opened relative to a capability should itself be a capability.
141a0108be6SJonathan Anderson */
142a0108be6SJonathan Anderson REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
143a0108be6SJonathan Anderson CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
144a0108be6SJonathan Anderson CHECK_RIGHTS(rights, baserights);
145a0108be6SJonathan Anderson
146a0108be6SJonathan Anderson return success;
147a0108be6SJonathan Anderson }
148