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 <limits.h>
21 #include "fs.h"
22 #include "telescope.h"
24 #ifdef __OpenBSD__
26 # include <errno.h>
27 # include <stdlib.h>
28 # include <string.h>
29 # include <unistd.h>
31 void
32 sandbox_net_process(void)
33 {
34 if (pledge("stdio inet dns", NULL) == -1)
35 err(1, "pledge");
36 }
38 void
39 sandbox_ui_process(void)
40 {
41 char path[PATH_MAX];
43 if (unveil("/tmp", "rwc") == -1)
44 err(1, "unveil(/tmp)");
46 strlcpy(path, getenv("HOME"), sizeof(path));
47 strlcat(path, "/Downloads", sizeof(path));
48 if (unveil(path, "rwc") == -1 && errno != ENOENT)
49 err(1, "unveil(%s)", path);
51 if (unveil(config_path_base, "rwc") == -1)
52 err(1, "unveil(%s)", config_path_base);
54 if (unveil(data_path_base, "rwc") == -1)
55 err(1, "unveil(%s)", data_path_base);
57 if (unveil(cache_path_base, "rwc") == -1)
58 err(1, "unveil(%s)", cache_path_base);
60 if (unveil("/bin/sh", "rx") == -1)
61 err(1, "unveil(xdg-open)");
63 if (pledge("stdio rpath wpath cpath unix tty proc exec",
64 NULL) == -1)
65 err(1, "pledge");
66 }
68 #elif HAVE_LINUX_LANDLOCK_H
70 #include <linux/landlock.h>
72 #include <sys/prctl.h>
73 #include <sys/stat.h>
74 #include <sys/syscall.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
82 /*
83 * What's the deal with landlock? While distro with linux >= 5.13
84 * have the struct declarations, libc wrappers are missing. The
85 * sample landlock code provided by the authors includes these "shims"
86 * in their example for the landlock API until libc provides them.
87 *
88 * Linux is such a mess sometimes. /rant
89 */
91 #ifndef landlock_create_ruleset
92 static inline int
93 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
94 __u32 flags)
95 {
96 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
97 }
98 #endif
100 #ifndef landlock_add_rule
101 static inline int
102 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
103 const void *attr, __u32 flags)
105 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
107 #endif
109 #ifndef landlock_restrict_self
110 static inline int
111 landlock_restrict_self(int ruleset_fd, __u32 flags)
113 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
115 #endif
117 static int
118 open_landlock(void)
120 int fd;
121 struct landlock_ruleset_attr attr = {
122 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
123 LANDLOCK_ACCESS_FS_READ_FILE |
124 LANDLOCK_ACCESS_FS_READ_DIR |
125 LANDLOCK_ACCESS_FS_WRITE_FILE |
126 LANDLOCK_ACCESS_FS_REMOVE_DIR |
127 LANDLOCK_ACCESS_FS_REMOVE_FILE |
128 LANDLOCK_ACCESS_FS_MAKE_CHAR |
129 LANDLOCK_ACCESS_FS_MAKE_DIR |
130 LANDLOCK_ACCESS_FS_MAKE_REG |
131 LANDLOCK_ACCESS_FS_MAKE_SOCK |
132 LANDLOCK_ACCESS_FS_MAKE_FIFO |
133 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
134 LANDLOCK_ACCESS_FS_MAKE_SYM,
135 };
137 fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
138 if (fd == -1) {
139 switch (errno) {
140 case ENOSYS:
141 case EOPNOTSUPP:
142 return -1;
143 default:
144 err(1, "can't create landlock ruleset");
147 return fd;
150 static int
151 landlock_unveil(int landlock_fd, const char *path, int perms)
153 struct landlock_path_beneath_attr pb;
154 int err, saved_errno;
156 pb.allowed_access = perms;
158 if ((pb.parent_fd = open(path, O_PATH)) == -1)
159 return -1;
161 err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
162 &pb, 0);
163 saved_errno = errno;
164 close(pb.parent_fd);
165 errno = saved_errno;
166 return err ? -1 : 0;
169 static int
170 landlock_apply(int fd)
172 int r, saved_errno;
174 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
175 err(1, "%s: prctl(PR_SET_NO_NEW_PRIVS)", __func__);
177 r = landlock_restrict_self(fd, 0);
178 saved_errno = errno;
179 close(fd);
180 errno = saved_errno;
181 return r ? -1 : 0;
184 static int
185 landlock_no_fs(void)
187 int fd;
189 /*
190 * XXX: landlock disabled at runtime, pretend everything's
191 * good.
192 */
193 if ((fd = open_landlock()) == -1)
194 return 0;
196 return landlock_apply(fd);
199 void
200 sandbox_net_process(void)
202 /*
203 * We don't know what paths are required for the TLS stack.
204 * Yes, it sucks.
205 */
206 return;
209 void
210 sandbox_ui_process(void)
212 /*
213 * Needs to be able to read files *and* execute programs,
214 * can't be sandboxed.
215 */
216 return;
219 #else
221 #warning "No sandbox for this OS"
223 void
224 sandbox_net_process(void)
226 return;
229 void
230 sandbox_ui_process(void)
232 return;
235 #endif