Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include "fs.h"
20 #include "telescope.h"
22 #ifdef __OpenBSD__
24 # include <errno.h>
25 # include <limits.h>
26 # include <stdlib.h>
27 # include <string.h>
28 # include <unistd.h>
30 void
31 sandbox_net_process(void)
32 {
33 if (pledge("stdio inet dns", NULL) == -1)
34 err(1, "pledge");
35 }
37 void
38 sandbox_ui_process(void)
39 {
40 if (pledge("stdio tty recvfd", NULL) == -1)
41 err(1, "pledge");
42 }
44 void
45 sandbox_fs_process(void)
46 {
47 char path[PATH_MAX];
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)
67 err(1, "pledge");
68 }
70 #elif HAVE_LINUX_LANDLOCK_H
72 #include <linux/landlock.h>
73 #include <linux/prctl.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 *
91 * Linux is such a mess sometimes. /rant
92 */
94 #ifndef landlock_create_ruleset
95 static inline int
96 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
97 __u32 flags)
98 {
99 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
101 #endif
103 #ifndef landlock_add_rule
104 static inline int
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);
110 #endif
112 #ifndef landlock_restrict_self
113 static inline int
114 landlock_restrict_self(int ruleset_fd, __u32 flags)
116 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
118 #endif
120 static int
121 open_landlock(void)
123 int fd;
124 struct landlock_ruleset_attr attr = {
125 .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE |
126 LANDLOCK_ACCESS_FS_READ_DIR |
127 LANDLOCK_ACCESS_FS_WRITE_FILE |
128 LANDLOCK_ACCESS_FS_MAKE_DIR |
129 LANDLOCK_ACCESS_FS_MAKE_REG,
130 };
132 fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
133 if (fd == -1) {
134 switch (errno) {
135 case ENOSYS:
136 case EOPNOTSUPP:
137 return -1;
138 default:
139 err(1, "can't create landlock ruleset");
142 return fd;
145 static int
146 landlock_unveil(int landlock_fd, const char *path, int perms)
148 struct landlock_path_beneath_attr pb;
149 int err, saved_errno;
151 pb.allowed_access = perms;
153 if ((pb.parent_fd = open(path, O_PATH)) == -1)
154 return -1;
156 err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
157 &pb, 0);
158 saved_errno = errno;
159 close(pb.parent_fd);
160 errno = saved_errno;
161 return err ? -1 : 0;
164 static int
165 landlock_apply(int fd)
167 int r, saved_errno;
169 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
170 err(1, "%s: prctl(PR_SET_NO_NEW_PRIVS)", __func__);
172 r = landlock_restrict_self(fd, 0);
173 saved_errno = errno;
174 close(fd);
175 errno = saved_errno;
176 return r ? -1 : 0;
179 static int
180 landlock_no_fs(void)
182 int fd;
184 /*
185 * XXX: landlock disabled at runtime, pretend everything's
186 * good.
187 */
188 if ((fd = open_landlock()) == -1)
189 return 0;
191 return landlock_apply(fd);
194 void
195 sandbox_net_process(void)
197 /*
198 * We don't know what paths are required for the TLS stack.
199 * Yes, it sucks.
200 */
201 return;
204 void
205 sandbox_ui_process(void)
207 if (landlock_no_fs() == -1)
208 err(1, "landlock");
211 void
212 sandbox_fs_process(void)
214 int fd, rwc;
215 char path[PATH_MAX];
217 /*
218 * XXX: at build-time we found landlock.h but we've just
219 * realized it's not available on this kernel, so do nothing.
220 */
221 if ((fd = open_landlock()) == -1)
222 return;
224 rwc = LANDLOCK_ACCESS_FS_READ_FILE |
225 LANDLOCK_ACCESS_FS_READ_DIR |
226 LANDLOCK_ACCESS_FS_WRITE_FILE |
227 LANDLOCK_ACCESS_FS_MAKE_DIR |
228 LANDLOCK_ACCESS_FS_MAKE_REG;
230 if (landlock_unveil(fd, "/tmp", rwc) == -1)
231 err(1, "landlock_unveil(/tmp)");
233 strlcpy(path, getenv("HOME"), sizeof(path));
234 strlcat(path, "/Downloads", sizeof(path));
235 if (landlock_unveil(fd, path, rwc) == -1 && errno != ENOENT)
236 err(1, "landlock_unveil(%s)", path);
238 if (landlock_unveil(fd, config_path_base, rwc) == -1)
239 err(1, "landlock_unveil(%s)", config_path_base);
241 if (landlock_unveil(fd, data_path_base, rwc) == -1)
242 err(1, "landlock_unveil(%s)", data_path_base);
244 if (landlock_unveil(fd, cache_path_base, rwc) == -1)
245 err(1, "landlock_unveil(%s)", cache_path_base);
248 #else
250 #warning "No sandbox for this OS"
252 void
253 sandbox_net_process(void)
255 return;
258 void
259 sandbox_ui_process(void)
261 return;
264 void
265 sandbox_fs_process(void)
267 return;
270 #endif