Blame


1 677d90f7 2021-03-12 op /*
2 677d90f7 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 7be09703 2024-02-12 op * Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
4 677d90f7 2021-03-12 op *
5 677d90f7 2021-03-12 op * Permission to use, copy, modify, and distribute this software for any
6 677d90f7 2021-03-12 op * purpose with or without fee is hereby granted, provided that the above
7 677d90f7 2021-03-12 op * copyright notice and this permission notice appear in all copies.
8 677d90f7 2021-03-12 op *
9 677d90f7 2021-03-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 677d90f7 2021-03-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 677d90f7 2021-03-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 677d90f7 2021-03-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 677d90f7 2021-03-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 677d90f7 2021-03-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 677d90f7 2021-03-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 677d90f7 2021-03-12 op */
17 677d90f7 2021-03-12 op
18 a9d11f81 2021-10-08 op #include "compat.h"
19 a9d11f81 2021-10-08 op
20 f63b8f73 2022-04-24 op #include <limits.h>
21 f63b8f73 2022-04-24 op
22 4b877649 2021-10-07 op #include "fs.h"
23 754622a2 2021-03-15 op #include "telescope.h"
24 677d90f7 2021-03-12 op
25 677d90f7 2021-03-12 op #ifdef __OpenBSD__
26 677d90f7 2021-03-12 op
27 fced318d 2021-10-08 op # include <errno.h>
28 35e1f40a 2021-03-14 op # include <stdlib.h>
29 35e1f40a 2021-03-14 op # include <string.h>
30 2b1f38ab 2021-03-13 op # include <unistd.h>
31 2b1f38ab 2021-03-13 op
32 677d90f7 2021-03-12 op void
33 17c10c65 2021-07-12 op sandbox_net_process(void)
34 677d90f7 2021-03-12 op {
35 d35e18b3 2024-02-04 op if (pledge("stdio inet dns recvfd", NULL) == -1)
36 677d90f7 2021-03-12 op err(1, "pledge");
37 677d90f7 2021-03-12 op }
38 677d90f7 2021-03-12 op
39 b1d4d01b 2021-03-14 op void
40 b1d4d01b 2021-03-14 op sandbox_ui_process(void)
41 b1d4d01b 2021-03-14 op {
42 3c7ed8d8 2024-06-07 op #if notyet
43 35e1f40a 2021-03-14 op char path[PATH_MAX];
44 35e1f40a 2021-03-14 op
45 de2a69bb 2021-05-17 op if (unveil("/tmp", "rwc") == -1)
46 e9cb759e 2021-10-08 op err(1, "unveil(/tmp)");
47 35e1f40a 2021-03-14 op
48 35e1f40a 2021-03-14 op strlcpy(path, getenv("HOME"), sizeof(path));
49 35e1f40a 2021-03-14 op strlcat(path, "/Downloads", sizeof(path));
50 4380c692 2021-10-08 op if (unveil(path, "rwc") == -1 && errno != ENOENT)
51 fd0beb53 2021-10-07 op err(1, "unveil(%s)", path);
52 35e1f40a 2021-03-14 op
53 4b877649 2021-10-07 op if (unveil(config_path_base, "rwc") == -1)
54 fd0beb53 2021-10-07 op err(1, "unveil(%s)", config_path_base);
55 35e1f40a 2021-03-14 op
56 4b877649 2021-10-07 op if (unveil(data_path_base, "rwc") == -1)
57 fd0beb53 2021-10-07 op err(1, "unveil(%s)", data_path_base);
58 4b877649 2021-10-07 op
59 4b877649 2021-10-07 op if (unveil(cache_path_base, "rwc") == -1)
60 fd0beb53 2021-10-07 op err(1, "unveil(%s)", cache_path_base);
61 4b877649 2021-10-07 op
62 b9a64d45 2024-06-04 thomas.ad if (unveil("/bin/sh", "rx") == -1)
63 b9a64d45 2024-06-04 thomas.ad err(1, "unveil(xdg-open)");
64 3c7ed8d8 2024-06-07 op #endif
65 b9a64d45 2024-06-04 thomas.ad
66 ec5e90b3 2024-10-20 op if (pledge("stdio rpath wpath cpath unix sendfd tty proc exec",
67 b9a64d45 2024-06-04 thomas.ad NULL) == -1)
68 35e1f40a 2021-03-14 op err(1, "pledge");
69 35e1f40a 2021-03-14 op }
70 35e1f40a 2021-03-14 op
71 a9d11f81 2021-10-08 op #elif HAVE_LINUX_LANDLOCK_H
72 a9d11f81 2021-10-08 op
73 a9d11f81 2021-10-08 op #include <linux/landlock.h>
74 a9d11f81 2021-10-08 op
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>
78 a9d11f81 2021-10-08 op
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>
84 a9d11f81 2021-10-08 op
85 a9d11f81 2021-10-08 op /*
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.
90 a9d11f81 2021-10-08 op */
91 a9d11f81 2021-10-08 op
92 a9d11f81 2021-10-08 op #ifndef landlock_create_ruleset
93 a9d11f81 2021-10-08 op static inline int
94 a9d11f81 2021-10-08 op landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
95 a9d11f81 2021-10-08 op __u32 flags)
96 a9d11f81 2021-10-08 op {
97 a9d11f81 2021-10-08 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
98 a9d11f81 2021-10-08 op }
99 a9d11f81 2021-10-08 op #endif
100 a9d11f81 2021-10-08 op
101 a9d11f81 2021-10-08 op #ifndef landlock_add_rule
102 a9d11f81 2021-10-08 op static inline int
103 a9d11f81 2021-10-08 op landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
104 a9d11f81 2021-10-08 op const void *attr, __u32 flags)
105 a9d11f81 2021-10-08 op {
106 a9d11f81 2021-10-08 op return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
107 a9d11f81 2021-10-08 op }
108 a9d11f81 2021-10-08 op #endif
109 a9d11f81 2021-10-08 op
110 a9d11f81 2021-10-08 op #ifndef landlock_restrict_self
111 a9d11f81 2021-10-08 op static inline int
112 a9d11f81 2021-10-08 op landlock_restrict_self(int ruleset_fd, __u32 flags)
113 a9d11f81 2021-10-08 op {
114 a9d11f81 2021-10-08 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
115 a9d11f81 2021-10-08 op }
116 a9d11f81 2021-10-08 op #endif
117 a9d11f81 2021-10-08 op
118 7be09703 2024-02-12 op /*
119 7be09703 2024-02-12 op * Maybe we should ship with a full copy of the linux headers because
120 7be09703 2024-02-12 op * you never know...
121 7be09703 2024-02-12 op */
122 7be09703 2024-02-12 op
123 7be09703 2024-02-12 op #ifndef LANDLOCK_ACCESS_FS_REFER
124 7be09703 2024-02-12 op #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
125 7be09703 2024-02-12 op #endif
126 7be09703 2024-02-12 op
127 7be09703 2024-02-12 op #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
128 7be09703 2024-02-12 op #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
129 7be09703 2024-02-12 op #endif
130 7be09703 2024-02-12 op
131 7be09703 2024-02-12 op static int landlock_state;
132 7be09703 2024-02-12 op static int landlock_fd;
133 7be09703 2024-02-12 op
134 7be09703 2024-02-12 op /*
135 7be09703 2024-02-12 op * Initialize landlock, which is stupidly complicated.
136 7be09703 2024-02-12 op */
137 a9d11f81 2021-10-08 op static int
138 7be09703 2024-02-12 op landlock_init(void)
139 a9d11f81 2021-10-08 op {
140 7be09703 2024-02-12 op struct landlock_ruleset_attr rattr = {
141 7be09703 2024-02-12 op /*
142 7be09703 2024-02-12 op * List all capabilities currently defined by landlock.
143 7be09703 2024-02-12 op * Failure in doing so will implicitly allow those actions
144 7be09703 2024-02-12 op * (i.e. omitting READ_FILE will allow to read _any_ file.)
145 7be09703 2024-02-12 op */
146 7be09703 2024-02-12 op .handled_access_fs =
147 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_EXECUTE |
148 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_READ_FILE |
149 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_READ_DIR |
150 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_WRITE_FILE |
151 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_REMOVE_DIR |
152 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_REMOVE_FILE |
153 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_CHAR |
154 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_DIR |
155 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_REG |
156 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_SOCK |
157 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_FIFO |
158 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_BLOCK |
159 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_MAKE_SYM |
160 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_REFER |
161 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_TRUNCATE,
162 a9d11f81 2021-10-08 op };
163 7be09703 2024-02-12 op int abi;
164 a9d11f81 2021-10-08 op
165 7be09703 2024-02-12 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
166 7be09703 2024-02-12 op return -1;
167 7be09703 2024-02-12 op
168 7be09703 2024-02-12 op abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
169 7be09703 2024-02-12 op if (abi == -1)
170 7be09703 2024-02-12 op return -1;
171 7be09703 2024-02-12 op if (abi < 2)
172 7be09703 2024-02-12 op rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
173 7be09703 2024-02-12 op if (abi < 3)
174 7be09703 2024-02-12 op rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
175 7be09703 2024-02-12 op
176 7be09703 2024-02-12 op landlock_state = 1;
177 7be09703 2024-02-12 op return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
178 a9d11f81 2021-10-08 op }
179 a9d11f81 2021-10-08 op
180 a9d11f81 2021-10-08 op static int
181 7be09703 2024-02-12 op landlock_lock(void)
182 a9d11f81 2021-10-08 op {
183 7be09703 2024-02-12 op int saved_errno;
184 a9d11f81 2021-10-08 op
185 7be09703 2024-02-12 op if (landlock_restrict_self(landlock_fd, 0)) {
186 7be09703 2024-02-12 op saved_errno = errno;
187 7be09703 2024-02-12 op close(landlock_fd);
188 7be09703 2024-02-12 op errno = saved_errno;
189 7be09703 2024-02-12 op landlock_state = -1;
190 a9d11f81 2021-10-08 op return -1;
191 7be09703 2024-02-12 op }
192 a9d11f81 2021-10-08 op
193 7be09703 2024-02-12 op landlock_state = 2;
194 7be09703 2024-02-12 op close(landlock_fd);
195 7be09703 2024-02-12 op return 0;
196 a9d11f81 2021-10-08 op }
197 a9d11f81 2021-10-08 op static int
198 7be09703 2024-02-12 op landlock_unveil(const char *path, const char *permissions)
199 a9d11f81 2021-10-08 op {
200 7be09703 2024-02-12 op struct landlock_path_beneath_attr lpba;
201 7be09703 2024-02-12 op int fd, saved_errno;
202 a9d11f81 2021-10-08 op
203 7be09703 2024-02-12 op if (landlock_state == 0) {
204 7be09703 2024-02-12 op if ((landlock_fd = landlock_init()) == -1) {
205 7be09703 2024-02-12 op landlock_state = -1;
206 7be09703 2024-02-12 op /* this kernel doesn't have landlock built in */
207 7be09703 2024-02-12 op if (errno == ENOSYS || errno == EOPNOTSUPP)
208 7be09703 2024-02-12 op return 0;
209 7be09703 2024-02-12 op return -1;
210 7be09703 2024-02-12 op }
211 7be09703 2024-02-12 op }
212 a9d11f81 2021-10-08 op
213 7be09703 2024-02-12 op /* no landlock available */
214 7be09703 2024-02-12 op if (landlock_state == -1)
215 7be09703 2024-02-12 op return 0;
216 a9d11f81 2021-10-08 op
217 7be09703 2024-02-12 op if (path == NULL && permissions == NULL)
218 7be09703 2024-02-12 op return landlock_lock();
219 a9d11f81 2021-10-08 op
220 7be09703 2024-02-12 op if (path == NULL || permissions == NULL || landlock_state != 1) {
221 7be09703 2024-02-12 op errno = EINVAL;
222 7be09703 2024-02-12 op return -1;
223 7be09703 2024-02-12 op }
224 a9d11f81 2021-10-08 op
225 7be09703 2024-02-12 op if (!strcmp(permissions, "r")) {
226 7be09703 2024-02-12 op fd = open(path, O_PATH | O_CLOEXEC);
227 7be09703 2024-02-12 op if (fd == -1)
228 7be09703 2024-02-12 op return -1;
229 7be09703 2024-02-12 op lpba = (struct landlock_path_beneath_attr){
230 7be09703 2024-02-12 op .allowed_access =
231 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_READ_FILE |
232 7be09703 2024-02-12 op LANDLOCK_ACCESS_FS_READ_DIR,
233 7be09703 2024-02-12 op .parent_fd = fd,
234 7be09703 2024-02-12 op };
235 7be09703 2024-02-12 op if (landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
236 7be09703 2024-02-12 op &lpba, 0) == -1) {
237 7be09703 2024-02-12 op saved_errno = errno;
238 7be09703 2024-02-12 op close(fd);
239 7be09703 2024-02-12 op errno = saved_errno;
240 7be09703 2024-02-12 op return -1;
241 7be09703 2024-02-12 op }
242 7be09703 2024-02-12 op close(fd);
243 7be09703 2024-02-12 op }
244 7be09703 2024-02-12 op
245 7be09703 2024-02-12 op return 0;
246 a9d11f81 2021-10-08 op }
247 a9d11f81 2021-10-08 op
248 a9d11f81 2021-10-08 op void
249 a9d11f81 2021-10-08 op sandbox_net_process(void)
250 a9d11f81 2021-10-08 op {
251 ed1d237e 2021-11-27 op /*
252 7be09703 2024-02-12 op * We don't know what paths are required for the TLS stack and
253 7be09703 2024-02-12 op * DNS, so allow accessing read-only the whole system.
254 ed1d237e 2021-11-27 op * Yes, it sucks.
255 ed1d237e 2021-11-27 op */
256 7be09703 2024-02-12 op
257 7be09703 2024-02-12 op if (landlock_unveil("/", "r") == -1)
258 7be09703 2024-02-12 op err(1, "landlock_unveil(/, r)");
259 7be09703 2024-02-12 op if (landlock_unveil(NULL, NULL) == -1)
260 7be09703 2024-02-12 op err(1, "landlock_unveil(NULL, NULL)");
261 a9d11f81 2021-10-08 op }
262 a9d11f81 2021-10-08 op
263 a9d11f81 2021-10-08 op void
264 a9d11f81 2021-10-08 op sandbox_ui_process(void)
265 a9d11f81 2021-10-08 op {
266 4ab3b651 2021-11-29 op /*
267 f63b8f73 2022-04-24 op * Needs to be able to read files *and* execute programs,
268 f63b8f73 2022-04-24 op * can't be sandboxed.
269 4ab3b651 2021-11-29 op */
270 f63b8f73 2022-04-24 op return;
271 a9d11f81 2021-10-08 op }
272 a9d11f81 2021-10-08 op
273 68a9b7d2 2021-03-13 op #else
274 68a9b7d2 2021-03-13 op
275 68a9b7d2 2021-03-13 op void
276 17c10c65 2021-07-12 op sandbox_net_process(void)
277 68a9b7d2 2021-03-13 op {
278 68a9b7d2 2021-03-13 op return;
279 68a9b7d2 2021-03-13 op }
280 68a9b7d2 2021-03-13 op
281 35e1f40a 2021-03-14 op void
282 35e1f40a 2021-03-14 op sandbox_ui_process(void)
283 35e1f40a 2021-03-14 op {
284 35e1f40a 2021-03-14 op return;
285 35e1f40a 2021-03-14 op }
286 35e1f40a 2021-03-14 op
287 677d90f7 2021-03-12 op #endif