xref: /freebsd/tools/regression/security/cap_test/cap_test_relative.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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  * $FreeBSD$
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/capability.h>
35 #include <sys/errno.h>
36 
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "cap_test.h"
44 
45 /*
46  * Test openat(2) in a variety of sitations to ensure that it obeys Capsicum
47  * "strict relative" rules:
48  *
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
53  *    '..' components.
54  */
55 int
56 test_relative(void)
57 {
58 	int success = PASSED;
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;
61 	cap_rights_t rights;
62 
63 	REQUIRE(etc = open("/etc/", O_RDONLY));
64 	CHECK_SYSCALL_FAILS(EINVAL, cap_getrights, etc, &rights);
65 
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);
70 
71 	/*
72 	 * openat(2) with regular file descriptors in non-capability mode
73 	 * should Just Work (tm).
74 	 */
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);
79 
80 	/*
81 	 * Lookups relative to capabilities should be strictly relative.
82 	 *
83 	 * When not in capability mode, we don't actually require CAP_LOOKUP.
84 	 */
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);
88 
89 	CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY);
90 	CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY);
91 
92 	/*
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)?
96 	 */
97 	CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY);
98 
99 	/*
100 	 * A file opened relative to a capability should itself be a capability.
101 	 */
102 	CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights);
103 
104 	REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
105 	CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
106 	CHECK_RIGHTS(rights, baserights);
107 
108 	/*
109 	 * Enter capability mode; now ALL lookups are strictly relative.
110 	 */
111 	REQUIRE(cap_enter());
112 
113 	/*
114 	 * Relative lookups on regular files or capabilities with CAP_LOOKUP
115 	 * ought to succeed.
116 	 */
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);
121 
122 	/*
123 	 * Lookup relative to capabilities without CAP_LOOKUP should fail.
124 	 */
125 	CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY);
126 
127 	/*
128 	 * Absolute lookups should fail.
129 	 */
130 	CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY);
131 	CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY);
132 
133 	/*
134 	 * Lookups containing '..' should fail in capability mode.
135 	 */
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);
139 
140 	REQUIRE(fd = openat(etc, "passwd", O_RDONLY));
141 	CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
142 
143 	/*
144 	 * A file opened relative to a capability should itself be a capability.
145 	 */
146 	REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY));
147 	CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights);
148 	CHECK_RIGHTS(rights, baserights);
149 
150 	return success;
151 }
152