2 677d90f7 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 677d90f7 2021-03-12 op * Permission to use, copy, modify, and distribute this software for any
5 677d90f7 2021-03-12 op * purpose with or without fee is hereby granted, provided that the above
6 677d90f7 2021-03-12 op * copyright notice and this permission notice appear in all copies.
8 677d90f7 2021-03-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 677d90f7 2021-03-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 677d90f7 2021-03-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 677d90f7 2021-03-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 677d90f7 2021-03-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 677d90f7 2021-03-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 677d90f7 2021-03-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a9d11f81 2021-10-08 op #include "compat.h"
19 4b877649 2021-10-07 op #include "fs.h"
20 754622a2 2021-03-15 op #include "telescope.h"
22 677d90f7 2021-03-12 op #ifdef __OpenBSD__
24 fced318d 2021-10-08 op # include <errno.h>
25 35e1f40a 2021-03-14 op # include <limits.h>
26 35e1f40a 2021-03-14 op # include <stdlib.h>
27 35e1f40a 2021-03-14 op # include <string.h>
28 2b1f38ab 2021-03-13 op # include <unistd.h>
31 17c10c65 2021-07-12 op sandbox_net_process(void)
33 677d90f7 2021-03-12 op if (pledge("stdio inet dns", NULL) == -1)
34 677d90f7 2021-03-12 op err(1, "pledge");
38 b1d4d01b 2021-03-14 op sandbox_ui_process(void)
40 de2a69bb 2021-05-17 op if (pledge("stdio tty recvfd", NULL) == -1)
41 b1d4d01b 2021-03-14 op err(1, "pledge");
45 35e1f40a 2021-03-14 op sandbox_fs_process(void)
47 35e1f40a 2021-03-14 op char path[PATH_MAX];
49 de2a69bb 2021-05-17 op if (unveil("/tmp", "rwc") == -1)
50 e9cb759e 2021-10-08 op err(1, "unveil(/tmp)");
52 35e1f40a 2021-03-14 op strlcpy(path, getenv("HOME"), sizeof(path));
53 35e1f40a 2021-03-14 op strlcat(path, "/Downloads", sizeof(path));
54 4380c692 2021-10-08 op if (unveil(path, "rwc") == -1 && errno != ENOENT)
55 fd0beb53 2021-10-07 op err(1, "unveil(%s)", path);
57 4b877649 2021-10-07 op if (unveil(config_path_base, "rwc") == -1)
58 fd0beb53 2021-10-07 op err(1, "unveil(%s)", config_path_base);
60 4b877649 2021-10-07 op if (unveil(data_path_base, "rwc") == -1)
61 fd0beb53 2021-10-07 op err(1, "unveil(%s)", data_path_base);
63 4b877649 2021-10-07 op if (unveil(cache_path_base, "rwc") == -1)
64 fd0beb53 2021-10-07 op err(1, "unveil(%s)", cache_path_base);
66 de2a69bb 2021-05-17 op if (pledge("stdio rpath wpath cpath sendfd", NULL) == -1)
67 35e1f40a 2021-03-14 op err(1, "pledge");
70 a9d11f81 2021-10-08 op #elif HAVE_LINUX_LANDLOCK_H
72 a9d11f81 2021-10-08 op #include <linux/landlock.h>
73 a9d11f81 2021-10-08 op #include <linux/prctl.h>
75 a9d11f81 2021-10-08 op #include <sys/prctl.h>
76 a9d11f81 2021-10-08 op #include <sys/stat.h>
77 a9d11f81 2021-10-08 op #include <sys/syscall.h>
79 fced318d 2021-10-08 op #include <errno.h>
80 a9d11f81 2021-10-08 op #include <fcntl.h>
81 a9d11f81 2021-10-08 op #include <stdlib.h>
82 a9d11f81 2021-10-08 op #include <string.h>
83 a9d11f81 2021-10-08 op #include <unistd.h>
86 a9d11f81 2021-10-08 op * What's the deal with landlock? While distro with linux >= 5.13
87 a9d11f81 2021-10-08 op * have the struct declarations, libc wrappers are missing. The
88 a9d11f81 2021-10-08 op * sample landlock code provided by the authors includes these "shims"
89 a9d11f81 2021-10-08 op * in their example for the landlock API until libc provides them.
91 a9d11f81 2021-10-08 op * Linux is such a mess sometimes. /rant
94 a9d11f81 2021-10-08 op #ifndef landlock_create_ruleset
95 a9d11f81 2021-10-08 op static inline int
96 a9d11f81 2021-10-08 op landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
99 a9d11f81 2021-10-08 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
103 a9d11f81 2021-10-08 op #ifndef landlock_add_rule
104 a9d11f81 2021-10-08 op static inline int
105 a9d11f81 2021-10-08 op landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
106 a9d11f81 2021-10-08 op const void *attr, __u32 flags)
108 a9d11f81 2021-10-08 op return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
112 a9d11f81 2021-10-08 op #ifndef landlock_restrict_self
113 a9d11f81 2021-10-08 op static inline int
114 a9d11f81 2021-10-08 op landlock_restrict_self(int ruleset_fd, __u32 flags)
116 a9d11f81 2021-10-08 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
121 a9d11f81 2021-10-08 op open_landlock(void)
124 a9d11f81 2021-10-08 op struct landlock_ruleset_attr attr = {
125 a9d11f81 2021-10-08 op .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE |
126 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_READ_DIR |
127 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_WRITE_FILE |
128 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_MAKE_DIR |
129 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_MAKE_REG,
132 4ab3b651 2021-11-29 op fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
133 4ab3b651 2021-11-29 op if (fd == -1) {
134 4ab3b651 2021-11-29 op switch (errno) {
136 4ab3b651 2021-11-29 op case EOPNOTSUPP:
139 4ab3b651 2021-11-29 op err(1, "can't create landlock ruleset");
146 a9d11f81 2021-10-08 op landlock_unveil(int landlock_fd, const char *path, int perms)
148 a9d11f81 2021-10-08 op struct landlock_path_beneath_attr pb;
149 a9d11f81 2021-10-08 op int err, saved_errno;
151 a9d11f81 2021-10-08 op pb.allowed_access = perms;
153 a9d11f81 2021-10-08 op if ((pb.parent_fd = open(path, O_PATH)) == -1)
156 a9d11f81 2021-10-08 op err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
158 a9d11f81 2021-10-08 op saved_errno = errno;
159 a9d11f81 2021-10-08 op close(pb.parent_fd);
160 a9d11f81 2021-10-08 op errno = saved_errno;
161 a9d11f81 2021-10-08 op return err ? -1 : 0;
165 a9d11f81 2021-10-08 op landlock_apply(int fd)
167 a9d11f81 2021-10-08 op int r, saved_errno;
169 a9d11f81 2021-10-08 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
170 a9d11f81 2021-10-08 op err(1, "%s: prctl(PR_SET_NO_NEW_PRIVS)", __func__);
172 a9d11f81 2021-10-08 op r = landlock_restrict_self(fd, 0);
173 a9d11f81 2021-10-08 op saved_errno = errno;
175 a9d11f81 2021-10-08 op errno = saved_errno;
176 a9d11f81 2021-10-08 op return r ? -1 : 0;
180 a9d11f81 2021-10-08 op landlock_no_fs(void)
185 4ab3b651 2021-11-29 op * XXX: landlock disabled at runtime, pretend everything's
188 a9d11f81 2021-10-08 op if ((fd = open_landlock()) == -1)
191 a9d11f81 2021-10-08 op return landlock_apply(fd);
195 a9d11f81 2021-10-08 op sandbox_net_process(void)
198 ed1d237e 2021-11-27 op * We don't know what paths are required for the TLS stack.
199 ed1d237e 2021-11-27 op * Yes, it sucks.
205 a9d11f81 2021-10-08 op sandbox_ui_process(void)
207 a9d11f81 2021-10-08 op if (landlock_no_fs() == -1)
208 a9d11f81 2021-10-08 op err(1, "landlock");
212 a9d11f81 2021-10-08 op sandbox_fs_process(void)
215 7e76e0ef 2021-11-02 op char path[PATH_MAX];
218 4ab3b651 2021-11-29 op * XXX: at build-time we found landlock.h but we've just
219 4ab3b651 2021-11-29 op * realized it's not available on this kernel, so do nothing.
221 a9d11f81 2021-10-08 op if ((fd = open_landlock()) == -1)
224 a9d11f81 2021-10-08 op rwc = LANDLOCK_ACCESS_FS_READ_FILE |
225 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_READ_DIR |
226 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_WRITE_FILE |
227 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_MAKE_DIR |
228 a9d11f81 2021-10-08 op LANDLOCK_ACCESS_FS_MAKE_REG;
230 a9d11f81 2021-10-08 op if (landlock_unveil(fd, "/tmp", rwc) == -1)
231 a9d11f81 2021-10-08 op err(1, "landlock_unveil(/tmp)");
233 a9d11f81 2021-10-08 op strlcpy(path, getenv("HOME"), sizeof(path));
234 a9d11f81 2021-10-08 op strlcat(path, "/Downloads", sizeof(path));
235 4380c692 2021-10-08 op if (landlock_unveil(fd, path, rwc) == -1 && errno != ENOENT)
236 a9d11f81 2021-10-08 op err(1, "landlock_unveil(%s)", path);
238 a9d11f81 2021-10-08 op if (landlock_unveil(fd, config_path_base, rwc) == -1)
239 a9d11f81 2021-10-08 op err(1, "landlock_unveil(%s)", config_path_base);
241 a9d11f81 2021-10-08 op if (landlock_unveil(fd, data_path_base, rwc) == -1)
242 a9d11f81 2021-10-08 op err(1, "landlock_unveil(%s)", data_path_base);
244 a9d11f81 2021-10-08 op if (landlock_unveil(fd, cache_path_base, rwc) == -1)
245 a9d11f81 2021-10-08 op err(1, "landlock_unveil(%s)", cache_path_base);
250 68a9b7d2 2021-03-13 op #warning "No sandbox for this OS"
253 17c10c65 2021-07-12 op sandbox_net_process(void)
259 35e1f40a 2021-03-14 op sandbox_ui_process(void)
265 35e1f40a 2021-03-14 op sandbox_fs_process(void)