Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include "compat.h"
20 #include <limits.h>
22 #include "fs.h"
23 #include "telescope.h"
25 #ifdef __OpenBSD__
27 # include <errno.h>
28 # include <stdlib.h>
29 # include <string.h>
30 # include <unistd.h>
32 void
33 sandbox_net_process(void)
34 {
35 if (pledge("stdio inet dns recvfd", NULL) == -1)
36 err(1, "pledge");
37 }
39 void
40 sandbox_ui_process(void)
41 {
42 char path[PATH_MAX];
44 if (unveil("/tmp", "rwc") == -1)
45 err(1, "unveil(/tmp)");
47 strlcpy(path, getenv("HOME"), sizeof(path));
48 strlcat(path, "/Downloads", sizeof(path));
49 if (unveil(path, "rwc") == -1 && errno != ENOENT)
50 err(1, "unveil(%s)", path);
52 if (unveil(config_path_base, "rwc") == -1)
53 err(1, "unveil(%s)", config_path_base);
55 if (unveil(data_path_base, "rwc") == -1)
56 err(1, "unveil(%s)", data_path_base);
58 if (unveil(cache_path_base, "rwc") == -1)
59 err(1, "unveil(%s)", cache_path_base);
61 if (pledge("stdio rpath wpath cpath unix sendfd tty", NULL) == -1)
62 err(1, "pledge");
63 }
65 #elif HAVE_LINUX_LANDLOCK_H
67 #include <linux/landlock.h>
69 #include <sys/prctl.h>
70 #include <sys/stat.h>
71 #include <sys/syscall.h>
73 #include <errno.h>
74 #include <fcntl.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
79 /*
80 * What's the deal with landlock? While distro with linux >= 5.13
81 * have the struct declarations, libc wrappers are missing. The
82 * sample landlock code provided by the authors includes these "shims"
83 * in their example for the landlock API until libc provides them.
84 */
86 #ifndef landlock_create_ruleset
87 static inline int
88 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
89 __u32 flags)
90 {
91 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
92 }
93 #endif
95 #ifndef landlock_add_rule
96 static inline int
97 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
98 const void *attr, __u32 flags)
99 {
100 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
102 #endif
104 #ifndef landlock_restrict_self
105 static inline int
106 landlock_restrict_self(int ruleset_fd, __u32 flags)
108 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
110 #endif
112 /*
113 * Maybe we should ship with a full copy of the linux headers because
114 * you never know...
115 */
117 #ifndef LANDLOCK_ACCESS_FS_REFER
118 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
119 #endif
121 #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
122 #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
123 #endif
125 static int landlock_state;
126 static int landlock_fd;
128 /*
129 * Initialize landlock, which is stupidly complicated.
130 */
131 static int
132 landlock_init(void)
134 struct landlock_ruleset_attr rattr = {
135 /*
136 * List all capabilities currently defined by landlock.
137 * Failure in doing so will implicitly allow those actions
138 * (i.e. omitting READ_FILE will allow to read _any_ file.)
139 */
140 .handled_access_fs =
141 LANDLOCK_ACCESS_FS_EXECUTE |
142 LANDLOCK_ACCESS_FS_READ_FILE |
143 LANDLOCK_ACCESS_FS_READ_DIR |
144 LANDLOCK_ACCESS_FS_WRITE_FILE |
145 LANDLOCK_ACCESS_FS_REMOVE_DIR |
146 LANDLOCK_ACCESS_FS_REMOVE_FILE |
147 LANDLOCK_ACCESS_FS_MAKE_CHAR |
148 LANDLOCK_ACCESS_FS_MAKE_DIR |
149 LANDLOCK_ACCESS_FS_MAKE_REG |
150 LANDLOCK_ACCESS_FS_MAKE_SOCK |
151 LANDLOCK_ACCESS_FS_MAKE_FIFO |
152 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
153 LANDLOCK_ACCESS_FS_MAKE_SYM |
154 LANDLOCK_ACCESS_FS_REFER |
155 LANDLOCK_ACCESS_FS_TRUNCATE,
156 };
157 int abi;
159 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
160 return -1;
162 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
163 if (abi == -1)
164 return -1;
165 if (abi < 2)
166 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
167 if (abi < 3)
168 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
170 landlock_state = 1;
171 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
174 static int
175 landlock_lock(void)
177 int saved_errno;
179 if (landlock_restrict_self(landlock_fd, 0)) {
180 saved_errno = errno;
181 close(landlock_fd);
182 errno = saved_errno;
183 landlock_state = -1;
184 return -1;
187 landlock_state = 2;
188 close(landlock_fd);
189 return 0;
191 static int
192 landlock_unveil(const char *path, const char *permissions)
194 struct landlock_path_beneath_attr lpba;
195 int fd, saved_errno;
197 if (landlock_state == 0) {
198 if ((landlock_fd = landlock_init()) == -1) {
199 landlock_state = -1;
200 /* this kernel doesn't have landlock built in */
201 if (errno == ENOSYS || errno == EOPNOTSUPP)
202 return 0;
203 return -1;
207 /* no landlock available */
208 if (landlock_state == -1)
209 return 0;
211 if (path == NULL && permissions == NULL)
212 return landlock_lock();
214 if (path == NULL || permissions == NULL || landlock_state != 1) {
215 errno = EINVAL;
216 return -1;
219 if (!strcmp(permissions, "r")) {
220 fd = open(path, O_PATH | O_CLOEXEC);
221 if (fd == -1)
222 return -1;
223 lpba = (struct landlock_path_beneath_attr){
224 .allowed_access =
225 LANDLOCK_ACCESS_FS_READ_FILE |
226 LANDLOCK_ACCESS_FS_READ_DIR,
227 .parent_fd = fd,
228 };
229 if (landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
230 &lpba, 0) == -1) {
231 saved_errno = errno;
232 close(fd);
233 errno = saved_errno;
234 return -1;
236 close(fd);
239 return 0;
242 void
243 sandbox_net_process(void)
245 /*
246 * We don't know what paths are required for the TLS stack and
247 * DNS, so allow accessing read-only the whole system.
248 * Yes, it sucks.
249 */
251 if (landlock_unveil("/", "r") == -1)
252 err(1, "landlock_unveil(/, r)");
253 if (landlock_unveil(NULL, NULL) == -1)
254 err(1, "landlock_unveil(NULL, NULL)");
257 void
258 sandbox_ui_process(void)
260 /*
261 * Needs to be able to read files *and* execute programs,
262 * can't be sandboxed.
263 */
264 return;
267 #else
269 void
270 sandbox_net_process(void)
272 return;
275 void
276 sandbox_ui_process(void)
278 return;
281 #endif