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)
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)
65 #elif HAVE_LINUX_LANDLOCK_H
67 #include <linux/landlock.h>
69 #include <sys/prctl.h>
71 #include <sys/syscall.h>
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.
86 #ifndef landlock_create_ruleset
88 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
91 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
95 #ifndef landlock_add_rule
97 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
98 const void *attr, __u32 flags)
100 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
104 #ifndef landlock_restrict_self
106 landlock_restrict_self(int ruleset_fd, __u32 flags)
108 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
113 * Maybe we should ship with a full copy of the linux headers because
117 #ifndef LANDLOCK_ACCESS_FS_REFER
118 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
121 #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
122 #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
125 static int landlock_state;
126 static int landlock_fd;
129 * Initialize landlock, which is stupidly complicated.
134 struct landlock_ruleset_attr rattr = {
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.)
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,
159 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
162 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
166 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
168 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
171 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
179 if (landlock_restrict_self(landlock_fd, 0)) {
192 landlock_unveil(const char *path, const char *permissions)
194 struct landlock_path_beneath_attr lpba;
197 if (landlock_state == 0) {
198 if ((landlock_fd = landlock_init()) == -1) {
200 /* this kernel doesn't have landlock built in */
201 if (errno == ENOSYS || errno == EOPNOTSUPP)
207 /* no landlock available */
208 if (landlock_state == -1)
211 if (path == NULL && permissions == NULL)
212 return landlock_lock();
214 if (path == NULL || permissions == NULL || landlock_state != 1) {
219 if (!strcmp(permissions, "r")) {
220 fd = open(path, O_PATH | O_CLOEXEC);
223 lpba = (struct landlock_path_beneath_attr){
225 LANDLOCK_ACCESS_FS_READ_FILE |
226 LANDLOCK_ACCESS_FS_READ_DIR,
229 if (landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
243 sandbox_net_process(void)
246 * We don't know what paths are required for the TLS stack and
247 * DNS, so allow accessing read-only the whole system.
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)");
258 sandbox_ui_process(void)
261 * Needs to be able to read files *and* execute programs,
262 * can't be sandboxed.
270 sandbox_net_process(void)
276 sandbox_ui_process(void)