Blame


1 677d90f7 2021-03-12 op /*
2 677d90f7 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 677d90f7 2021-03-12 op *
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.
7 677d90f7 2021-03-12 op *
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.
15 677d90f7 2021-03-12 op */
16 677d90f7 2021-03-12 op
17 a9d11f81 2021-10-08 op #include "compat.h"
18 a9d11f81 2021-10-08 op
19 f63b8f73 2022-04-24 op #include <limits.h>
20 f63b8f73 2022-04-24 op
21 4b877649 2021-10-07 op #include "fs.h"
22 754622a2 2021-03-15 op #include "telescope.h"
23 677d90f7 2021-03-12 op
24 677d90f7 2021-03-12 op #ifdef __OpenBSD__
25 677d90f7 2021-03-12 op
26 fced318d 2021-10-08 op # include <errno.h>
27 35e1f40a 2021-03-14 op # include <stdlib.h>
28 35e1f40a 2021-03-14 op # include <string.h>
29 2b1f38ab 2021-03-13 op # include <unistd.h>
30 2b1f38ab 2021-03-13 op
31 677d90f7 2021-03-12 op void
32 17c10c65 2021-07-12 op sandbox_net_process(void)
33 677d90f7 2021-03-12 op {
34 677d90f7 2021-03-12 op if (pledge("stdio inet dns", NULL) == -1)
35 677d90f7 2021-03-12 op err(1, "pledge");
36 677d90f7 2021-03-12 op }
37 677d90f7 2021-03-12 op
38 b1d4d01b 2021-03-14 op void
39 b1d4d01b 2021-03-14 op sandbox_ui_process(void)
40 b1d4d01b 2021-03-14 op {
41 35e1f40a 2021-03-14 op char path[PATH_MAX];
42 35e1f40a 2021-03-14 op
43 de2a69bb 2021-05-17 op if (unveil("/tmp", "rwc") == -1)
44 e9cb759e 2021-10-08 op err(1, "unveil(/tmp)");
45 35e1f40a 2021-03-14 op
46 35e1f40a 2021-03-14 op strlcpy(path, getenv("HOME"), sizeof(path));
47 35e1f40a 2021-03-14 op strlcat(path, "/Downloads", sizeof(path));
48 4380c692 2021-10-08 op if (unveil(path, "rwc") == -1 && errno != ENOENT)
49 fd0beb53 2021-10-07 op err(1, "unveil(%s)", path);
50 35e1f40a 2021-03-14 op
51 4b877649 2021-10-07 op if (unveil(config_path_base, "rwc") == -1)
52 fd0beb53 2021-10-07 op err(1, "unveil(%s)", config_path_base);
53 35e1f40a 2021-03-14 op
54 4b877649 2021-10-07 op if (unveil(data_path_base, "rwc") == -1)
55 fd0beb53 2021-10-07 op err(1, "unveil(%s)", data_path_base);
56 4b877649 2021-10-07 op
57 4b877649 2021-10-07 op if (unveil(cache_path_base, "rwc") == -1)
58 fd0beb53 2021-10-07 op err(1, "unveil(%s)", cache_path_base);
59 4b877649 2021-10-07 op
60 8f90b214 2022-08-16 op if (unveil("/bin/sh", "rx") == -1)
61 8f90b214 2022-08-16 op err(1, "unveil(xdg-open)");
62 8f90b214 2022-08-16 op
63 8f90b214 2022-08-16 op if (pledge("stdio rpath wpath cpath unix tty proc exec",
64 8f90b214 2022-08-16 op NULL) == -1)
65 35e1f40a 2021-03-14 op err(1, "pledge");
66 35e1f40a 2021-03-14 op }
67 35e1f40a 2021-03-14 op
68 a9d11f81 2021-10-08 op #elif HAVE_LINUX_LANDLOCK_H
69 a9d11f81 2021-10-08 op
70 a9d11f81 2021-10-08 op #include <linux/landlock.h>
71 a9d11f81 2021-10-08 op
72 a9d11f81 2021-10-08 op #include <sys/prctl.h>
73 a9d11f81 2021-10-08 op #include <sys/stat.h>
74 a9d11f81 2021-10-08 op #include <sys/syscall.h>
75 a9d11f81 2021-10-08 op
76 fced318d 2021-10-08 op #include <errno.h>
77 a9d11f81 2021-10-08 op #include <fcntl.h>
78 a9d11f81 2021-10-08 op #include <stdlib.h>
79 a9d11f81 2021-10-08 op #include <string.h>
80 a9d11f81 2021-10-08 op #include <unistd.h>
81 a9d11f81 2021-10-08 op
82 a9d11f81 2021-10-08 op /*
83 a9d11f81 2021-10-08 op * What's the deal with landlock? While distro with linux >= 5.13
84 a9d11f81 2021-10-08 op * have the struct declarations, libc wrappers are missing. The
85 a9d11f81 2021-10-08 op * sample landlock code provided by the authors includes these "shims"
86 a9d11f81 2021-10-08 op * in their example for the landlock API until libc provides them.
87 a9d11f81 2021-10-08 op *
88 a9d11f81 2021-10-08 op * Linux is such a mess sometimes. /rant
89 a9d11f81 2021-10-08 op */
90 a9d11f81 2021-10-08 op
91 a9d11f81 2021-10-08 op #ifndef landlock_create_ruleset
92 a9d11f81 2021-10-08 op static inline int
93 a9d11f81 2021-10-08 op landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
94 a9d11f81 2021-10-08 op __u32 flags)
95 a9d11f81 2021-10-08 op {
96 a9d11f81 2021-10-08 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
97 a9d11f81 2021-10-08 op }
98 a9d11f81 2021-10-08 op #endif
99 a9d11f81 2021-10-08 op
100 a9d11f81 2021-10-08 op #ifndef landlock_add_rule
101 a9d11f81 2021-10-08 op static inline int
102 a9d11f81 2021-10-08 op landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
103 a9d11f81 2021-10-08 op const void *attr, __u32 flags)
104 a9d11f81 2021-10-08 op {
105 a9d11f81 2021-10-08 op return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
106 a9d11f81 2021-10-08 op }
107 a9d11f81 2021-10-08 op #endif
108 a9d11f81 2021-10-08 op
109 a9d11f81 2021-10-08 op #ifndef landlock_restrict_self
110 a9d11f81 2021-10-08 op static inline int
111 a9d11f81 2021-10-08 op landlock_restrict_self(int ruleset_fd, __u32 flags)
112 a9d11f81 2021-10-08 op {
113 a9d11f81 2021-10-08 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
114 a9d11f81 2021-10-08 op }
115 a9d11f81 2021-10-08 op #endif
116 a9d11f81 2021-10-08 op
117 a9d11f81 2021-10-08 op static int
118 a9d11f81 2021-10-08 op open_landlock(void)
119 a9d11f81 2021-10-08 op {
120 4ab3b651 2021-11-29 op int fd;
121 a9d11f81 2021-10-08 op struct landlock_ruleset_attr attr = {
122 35ae81fd 2022-02-10 op .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
123 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_READ_FILE |
124 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_READ_DIR |
125 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_WRITE_FILE |
126 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_REMOVE_DIR |
127 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_REMOVE_FILE |
128 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_CHAR |
129 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_DIR |
130 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_REG |
131 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_SOCK |
132 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_FIFO |
133 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_BLOCK |
134 35ae81fd 2022-02-10 op LANDLOCK_ACCESS_FS_MAKE_SYM,
135 a9d11f81 2021-10-08 op };
136 a9d11f81 2021-10-08 op
137 4ab3b651 2021-11-29 op fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
138 4ab3b651 2021-11-29 op if (fd == -1) {
139 4ab3b651 2021-11-29 op switch (errno) {
140 4ab3b651 2021-11-29 op case ENOSYS:
141 4ab3b651 2021-11-29 op case EOPNOTSUPP:
142 4ab3b651 2021-11-29 op return -1;
143 4ab3b651 2021-11-29 op default:
144 4ab3b651 2021-11-29 op err(1, "can't create landlock ruleset");
145 4ab3b651 2021-11-29 op }
146 4ab3b651 2021-11-29 op }
147 4ab3b651 2021-11-29 op return fd;
148 a9d11f81 2021-10-08 op }
149 a9d11f81 2021-10-08 op
150 a9d11f81 2021-10-08 op static int
151 a9d11f81 2021-10-08 op landlock_unveil(int landlock_fd, const char *path, int perms)
152 a9d11f81 2021-10-08 op {
153 a9d11f81 2021-10-08 op struct landlock_path_beneath_attr pb;
154 a9d11f81 2021-10-08 op int err, saved_errno;
155 a9d11f81 2021-10-08 op
156 a9d11f81 2021-10-08 op pb.allowed_access = perms;
157 a9d11f81 2021-10-08 op
158 a9d11f81 2021-10-08 op if ((pb.parent_fd = open(path, O_PATH)) == -1)
159 a9d11f81 2021-10-08 op return -1;
160 a9d11f81 2021-10-08 op
161 a9d11f81 2021-10-08 op err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
162 a9d11f81 2021-10-08 op &pb, 0);
163 a9d11f81 2021-10-08 op saved_errno = errno;
164 a9d11f81 2021-10-08 op close(pb.parent_fd);
165 a9d11f81 2021-10-08 op errno = saved_errno;
166 a9d11f81 2021-10-08 op return err ? -1 : 0;
167 a9d11f81 2021-10-08 op }
168 a9d11f81 2021-10-08 op
169 a9d11f81 2021-10-08 op static int
170 a9d11f81 2021-10-08 op landlock_apply(int fd)
171 a9d11f81 2021-10-08 op {
172 a9d11f81 2021-10-08 op int r, saved_errno;
173 a9d11f81 2021-10-08 op
174 a9d11f81 2021-10-08 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
175 a9d11f81 2021-10-08 op err(1, "%s: prctl(PR_SET_NO_NEW_PRIVS)", __func__);
176 a9d11f81 2021-10-08 op
177 a9d11f81 2021-10-08 op r = landlock_restrict_self(fd, 0);
178 a9d11f81 2021-10-08 op saved_errno = errno;
179 a9d11f81 2021-10-08 op close(fd);
180 a9d11f81 2021-10-08 op errno = saved_errno;
181 a9d11f81 2021-10-08 op return r ? -1 : 0;
182 a9d11f81 2021-10-08 op }
183 a9d11f81 2021-10-08 op
184 a9d11f81 2021-10-08 op static int
185 a9d11f81 2021-10-08 op landlock_no_fs(void)
186 a9d11f81 2021-10-08 op {
187 a9d11f81 2021-10-08 op int fd;
188 a9d11f81 2021-10-08 op
189 4ab3b651 2021-11-29 op /*
190 4ab3b651 2021-11-29 op * XXX: landlock disabled at runtime, pretend everything's
191 4ab3b651 2021-11-29 op * good.
192 4ab3b651 2021-11-29 op */
193 a9d11f81 2021-10-08 op if ((fd = open_landlock()) == -1)
194 4ab3b651 2021-11-29 op return 0;
195 a9d11f81 2021-10-08 op
196 a9d11f81 2021-10-08 op return landlock_apply(fd);
197 a9d11f81 2021-10-08 op }
198 a9d11f81 2021-10-08 op
199 a9d11f81 2021-10-08 op void
200 a9d11f81 2021-10-08 op sandbox_net_process(void)
201 a9d11f81 2021-10-08 op {
202 ed1d237e 2021-11-27 op /*
203 ed1d237e 2021-11-27 op * We don't know what paths are required for the TLS stack.
204 ed1d237e 2021-11-27 op * Yes, it sucks.
205 ed1d237e 2021-11-27 op */
206 a9d11f81 2021-10-08 op return;
207 a9d11f81 2021-10-08 op }
208 a9d11f81 2021-10-08 op
209 a9d11f81 2021-10-08 op void
210 a9d11f81 2021-10-08 op sandbox_ui_process(void)
211 a9d11f81 2021-10-08 op {
212 4ab3b651 2021-11-29 op /*
213 f63b8f73 2022-04-24 op * Needs to be able to read files *and* execute programs,
214 f63b8f73 2022-04-24 op * can't be sandboxed.
215 4ab3b651 2021-11-29 op */
216 f63b8f73 2022-04-24 op return;
217 a9d11f81 2021-10-08 op }
218 a9d11f81 2021-10-08 op
219 68a9b7d2 2021-03-13 op #else
220 68a9b7d2 2021-03-13 op
221 68a9b7d2 2021-03-13 op #warning "No sandbox for this OS"
222 68a9b7d2 2021-03-13 op
223 68a9b7d2 2021-03-13 op void
224 17c10c65 2021-07-12 op sandbox_net_process(void)
225 68a9b7d2 2021-03-13 op {
226 68a9b7d2 2021-03-13 op return;
227 68a9b7d2 2021-03-13 op }
228 68a9b7d2 2021-03-13 op
229 35e1f40a 2021-03-14 op void
230 35e1f40a 2021-03-14 op sandbox_ui_process(void)
231 35e1f40a 2021-03-14 op {
232 35e1f40a 2021-03-14 op return;
233 35e1f40a 2021-03-14 op }
234 35e1f40a 2021-03-14 op
235 677d90f7 2021-03-12 op #endif