2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
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.
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.
23 #include "telescope.h"
33 sandbox_net_process(void)
35 if (pledge("stdio inet dns recvfd", NULL) == -1)
40 sandbox_ui_process(void)
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)");
66 if (pledge("stdio rpath wpath cpath unix sendfd tty proc exec",
71 #elif HAVE_LINUX_LANDLOCK_H
73 #include <linux/landlock.h>
75 #include <sys/prctl.h>
77 #include <sys/syscall.h>
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.
92 #ifndef landlock_create_ruleset
94 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
97 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
101 #ifndef landlock_add_rule
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);
110 #ifndef landlock_restrict_self
112 landlock_restrict_self(int ruleset_fd, __u32 flags)
114 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
119 * Maybe we should ship with a full copy of the linux headers because
123 #ifndef LANDLOCK_ACCESS_FS_REFER
124 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
127 #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
128 #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
131 static int landlock_state;
132 static int landlock_fd;
135 * Initialize landlock, which is stupidly complicated.
140 struct landlock_ruleset_attr rattr = {
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.)
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,
165 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
168 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
172 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
174 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
177 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
185 if (landlock_restrict_self(landlock_fd, 0)) {
198 landlock_unveil(const char *path, const char *permissions)
200 struct landlock_path_beneath_attr lpba;
203 if (landlock_state == 0) {
204 if ((landlock_fd = landlock_init()) == -1) {
206 /* this kernel doesn't have landlock built in */
207 if (errno == ENOSYS || errno == EOPNOTSUPP)
213 /* no landlock available */
214 if (landlock_state == -1)
217 if (path == NULL && permissions == NULL)
218 return landlock_lock();
220 if (path == NULL || permissions == NULL || landlock_state != 1) {
225 if (!strcmp(permissions, "r")) {
226 fd = open(path, O_PATH | O_CLOEXEC);
229 lpba = (struct landlock_path_beneath_attr){
231 LANDLOCK_ACCESS_FS_READ_FILE |
232 LANDLOCK_ACCESS_FS_READ_DIR,
235 if (landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
249 sandbox_net_process(void)
252 * We don't know what paths are required for the TLS stack and
253 * DNS, so allow accessing read-only the whole system.
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)");
264 sandbox_ui_process(void)
267 * Needs to be able to read files *and* execute programs,
268 * can't be sandboxed.
276 sandbox_net_process(void)
282 sandbox_ui_process(void)