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 #if notyet
43 char path[PATH_MAX];
45 if (unveil("/tmp", "rwc") == -1)
46 err(1, "unveil(/tmp)");
48 strlcpy(path, getenv("HOME"), sizeof(path));
49 strlcat(path, "/Downloads", sizeof(path));
50 if (unveil(path, "rwc") == -1 && errno != ENOENT)
51 err(1, "unveil(%s)", path);
53 if (unveil(config_path_base, "rwc") == -1)
54 err(1, "unveil(%s)", config_path_base);
56 if (unveil(data_path_base, "rwc") == -1)
57 err(1, "unveil(%s)", data_path_base);
59 if (unveil(cache_path_base, "rwc") == -1)
60 err(1, "unveil(%s)", cache_path_base);
62 if (unveil("/bin/sh", "rx") == -1)
63 err(1, "unveil(xdg-open)");
64 #endif
66 if (pledge("stdio rpath wpath cpath unix sendfd tty proc exec",
67 NULL) == -1)
68 err(1, "pledge");
69 }
71 #elif HAVE_LINUX_LANDLOCK_H
73 #include <linux/landlock.h>
75 #include <sys/prctl.h>
76 #include <sys/stat.h>
77 #include <sys/syscall.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <unistd.h>
85 /*
86 * What's the deal with landlock? While distro with linux >= 5.13
87 * have the struct declarations, libc wrappers are missing. The
88 * sample landlock code provided by the authors includes these "shims"
89 * in their example for the landlock API until libc provides them.
90 */
92 #ifndef landlock_create_ruleset
93 static inline int
94 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
95 __u32 flags)
96 {
97 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
98 }
99 #endif
101 #ifndef landlock_add_rule
102 static inline int
103 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
104 const void *attr, __u32 flags)
106 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
108 #endif
110 #ifndef landlock_restrict_self
111 static inline int
112 landlock_restrict_self(int ruleset_fd, __u32 flags)
114 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
116 #endif
118 /*
119 * Maybe we should ship with a full copy of the linux headers because
120 * you never know...
121 */
123 #ifndef LANDLOCK_ACCESS_FS_REFER
124 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
125 #endif
127 #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
128 #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
129 #endif
131 static int landlock_state;
132 static int landlock_fd;
134 /*
135 * Initialize landlock, which is stupidly complicated.
136 */
137 static int
138 landlock_init(void)
140 struct landlock_ruleset_attr rattr = {
141 /*
142 * List all capabilities currently defined by landlock.
143 * Failure in doing so will implicitly allow those actions
144 * (i.e. omitting READ_FILE will allow to read _any_ file.)
145 */
146 .handled_access_fs =
147 LANDLOCK_ACCESS_FS_EXECUTE |
148 LANDLOCK_ACCESS_FS_READ_FILE |
149 LANDLOCK_ACCESS_FS_READ_DIR |
150 LANDLOCK_ACCESS_FS_WRITE_FILE |
151 LANDLOCK_ACCESS_FS_REMOVE_DIR |
152 LANDLOCK_ACCESS_FS_REMOVE_FILE |
153 LANDLOCK_ACCESS_FS_MAKE_CHAR |
154 LANDLOCK_ACCESS_FS_MAKE_DIR |
155 LANDLOCK_ACCESS_FS_MAKE_REG |
156 LANDLOCK_ACCESS_FS_MAKE_SOCK |
157 LANDLOCK_ACCESS_FS_MAKE_FIFO |
158 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
159 LANDLOCK_ACCESS_FS_MAKE_SYM |
160 LANDLOCK_ACCESS_FS_REFER |
161 LANDLOCK_ACCESS_FS_TRUNCATE,
162 };
163 int abi;
165 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
166 return -1;
168 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
169 if (abi == -1)
170 return -1;
171 if (abi < 2)
172 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
173 if (abi < 3)
174 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
176 landlock_state = 1;
177 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
180 static int
181 landlock_lock(void)
183 int saved_errno;
185 if (landlock_restrict_self(landlock_fd, 0)) {
186 saved_errno = errno;
187 close(landlock_fd);
188 errno = saved_errno;
189 landlock_state = -1;
190 return -1;
193 landlock_state = 2;
194 close(landlock_fd);
195 return 0;
197 static int
198 landlock_unveil(const char *path, const char *permissions)
200 struct landlock_path_beneath_attr lpba;
201 int fd, saved_errno;
203 if (landlock_state == 0) {
204 if ((landlock_fd = landlock_init()) == -1) {
205 landlock_state = -1;
206 /* this kernel doesn't have landlock built in */
207 if (errno == ENOSYS || errno == EOPNOTSUPP)
208 return 0;
209 return -1;
213 /* no landlock available */
214 if (landlock_state == -1)
215 return 0;
217 if (path == NULL && permissions == NULL)
218 return landlock_lock();
220 if (path == NULL || permissions == NULL || landlock_state != 1) {
221 errno = EINVAL;
222 return -1;
225 if (!strcmp(permissions, "r")) {
226 fd = open(path, O_PATH | O_CLOEXEC);
227 if (fd == -1)
228 return -1;
229 lpba = (struct landlock_path_beneath_attr){
230 .allowed_access =
231 LANDLOCK_ACCESS_FS_READ_FILE |
232 LANDLOCK_ACCESS_FS_READ_DIR,
233 .parent_fd = fd,
234 };
235 if (landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
236 &lpba, 0) == -1) {
237 saved_errno = errno;
238 close(fd);
239 errno = saved_errno;
240 return -1;
242 close(fd);
245 return 0;
248 void
249 sandbox_net_process(void)
251 /*
252 * We don't know what paths are required for the TLS stack and
253 * DNS, so allow accessing read-only the whole system.
254 * Yes, it sucks.
255 */
257 if (landlock_unveil("/", "r") == -1)
258 err(1, "landlock_unveil(/, r)");
259 if (landlock_unveil(NULL, NULL) == -1)
260 err(1, "landlock_unveil(NULL, NULL)");
263 void
264 sandbox_ui_process(void)
266 /*
267 * Needs to be able to read files *and* execute programs,
268 * can't be sandboxed.
269 */
270 return;
273 #else
275 void
276 sandbox_net_process(void)
278 return;
281 void
282 sandbox_ui_process(void)
284 return;
287 #endif