2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "telescope.h"
31 sandbox_net_process(void)
33 if (pledge("stdio inet dns", NULL) == -1)
38 sandbox_ui_process(void)
40 if (pledge("stdio tty unix recvfd", NULL) == -1)
45 sandbox_fs_process(void)
49 if (unveil("/tmp", "rwc") == -1)
50 err(1, "unveil(/tmp)");
52 strlcpy(path, getenv("HOME"), sizeof(path));
53 strlcat(path, "/Downloads", sizeof(path));
54 if (unveil(path, "rwc") == -1 && errno != ENOENT)
55 err(1, "unveil(%s)", path);
57 if (unveil(config_path_base, "rwc") == -1)
58 err(1, "unveil(%s)", config_path_base);
60 if (unveil(data_path_base, "rwc") == -1)
61 err(1, "unveil(%s)", data_path_base);
63 if (unveil(cache_path_base, "rwc") == -1)
64 err(1, "unveil(%s)", cache_path_base);
66 if (pledge("stdio rpath wpath cpath sendfd", NULL) == -1)
70 #elif HAVE_LINUX_LANDLOCK_H
72 #include <linux/landlock.h>
73 #include <linux/prctl.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.
91 * Linux is such a mess sometimes. /rant
94 #ifndef landlock_create_ruleset
96 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
99 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
103 #ifndef landlock_add_rule
105 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
106 const void *attr, __u32 flags)
108 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
112 #ifndef landlock_restrict_self
114 landlock_restrict_self(int ruleset_fd, __u32 flags)
116 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
124 struct landlock_ruleset_attr attr = {
125 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
126 LANDLOCK_ACCESS_FS_READ_FILE |
127 LANDLOCK_ACCESS_FS_READ_DIR |
128 LANDLOCK_ACCESS_FS_WRITE_FILE |
129 LANDLOCK_ACCESS_FS_REMOVE_DIR |
130 LANDLOCK_ACCESS_FS_REMOVE_FILE |
131 LANDLOCK_ACCESS_FS_MAKE_CHAR |
132 LANDLOCK_ACCESS_FS_MAKE_DIR |
133 LANDLOCK_ACCESS_FS_MAKE_REG |
134 LANDLOCK_ACCESS_FS_MAKE_SOCK |
135 LANDLOCK_ACCESS_FS_MAKE_FIFO |
136 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
137 LANDLOCK_ACCESS_FS_MAKE_SYM,
140 fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
147 err(1, "can't create landlock ruleset");
154 landlock_unveil(int landlock_fd, const char *path, int perms)
156 struct landlock_path_beneath_attr pb;
157 int err, saved_errno;
159 pb.allowed_access = perms;
161 if ((pb.parent_fd = open(path, O_PATH)) == -1)
164 err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
173 landlock_apply(int fd)
177 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
178 err(1, "%s: prctl(PR_SET_NO_NEW_PRIVS)", __func__);
180 r = landlock_restrict_self(fd, 0);
193 * XXX: landlock disabled at runtime, pretend everything's
196 if ((fd = open_landlock()) == -1)
199 return landlock_apply(fd);
203 sandbox_net_process(void)
206 * We don't know what paths are required for the TLS stack.
213 sandbox_ui_process(void)
215 if (landlock_no_fs() == -1)
220 sandbox_fs_process(void)
226 * XXX: at build-time we found landlock.h but we've just
227 * realized it's not available on this kernel, so do nothing.
229 if ((fd = open_landlock()) == -1)
232 rwc = LANDLOCK_ACCESS_FS_READ_FILE |
233 LANDLOCK_ACCESS_FS_READ_DIR |
234 LANDLOCK_ACCESS_FS_WRITE_FILE |
235 LANDLOCK_ACCESS_FS_MAKE_DIR |
236 LANDLOCK_ACCESS_FS_MAKE_REG;
238 if (landlock_unveil(fd, "/tmp", rwc) == -1)
239 err(1, "landlock_unveil(/tmp)");
241 strlcpy(path, getenv("HOME"), sizeof(path));
242 strlcat(path, "/Downloads", sizeof(path));
243 if (landlock_unveil(fd, path, rwc) == -1 && errno != ENOENT)
244 err(1, "landlock_unveil(%s)", path);
246 if (landlock_unveil(fd, config_path_base, rwc) == -1)
247 err(1, "landlock_unveil(%s)", config_path_base);
249 if (landlock_unveil(fd, data_path_base, rwc) == -1)
250 err(1, "landlock_unveil(%s)", data_path_base);
252 if (landlock_unveil(fd, cache_path_base, rwc) == -1)
253 err(1, "landlock_unveil(%s)", cache_path_base);
258 #warning "No sandbox for this OS"
261 sandbox_net_process(void)
267 sandbox_ui_process(void)
273 sandbox_fs_process(void)