2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
22 #include "telescope.h"
32 sandbox_net_process(void)
34 if (pledge("stdio inet dns", NULL) == -1)
39 sandbox_ui_process(void)
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 (pledge("stdio rpath wpath cpath unix tty", NULL) == -1)
64 #elif HAVE_LINUX_LANDLOCK_H
66 #include <linux/landlock.h>
67 #include <linux/prctl.h>
69 #include <sys/prctl.h>
71 #include <sys/syscall.h>
80 * What's the deal with landlock? While distro with linux >= 5.13
81 * have the struct declarations, libc wrappers are missing. The
82 * sample landlock code provided by the authors includes these "shims"
83 * in their example for the landlock API until libc provides them.
85 * Linux is such a mess sometimes. /rant
88 #ifndef landlock_create_ruleset
90 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
93 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
97 #ifndef landlock_add_rule
99 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
100 const void *attr, __u32 flags)
102 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
106 #ifndef landlock_restrict_self
108 landlock_restrict_self(int ruleset_fd, __u32 flags)
110 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
118 struct landlock_ruleset_attr attr = {
119 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
120 LANDLOCK_ACCESS_FS_READ_FILE |
121 LANDLOCK_ACCESS_FS_READ_DIR |
122 LANDLOCK_ACCESS_FS_WRITE_FILE |
123 LANDLOCK_ACCESS_FS_REMOVE_DIR |
124 LANDLOCK_ACCESS_FS_REMOVE_FILE |
125 LANDLOCK_ACCESS_FS_MAKE_CHAR |
126 LANDLOCK_ACCESS_FS_MAKE_DIR |
127 LANDLOCK_ACCESS_FS_MAKE_REG |
128 LANDLOCK_ACCESS_FS_MAKE_SOCK |
129 LANDLOCK_ACCESS_FS_MAKE_FIFO |
130 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
131 LANDLOCK_ACCESS_FS_MAKE_SYM,
134 fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
141 err(1, "can't create landlock ruleset");
148 landlock_unveil(int landlock_fd, const char *path, int perms)
150 struct landlock_path_beneath_attr pb;
151 int err, saved_errno;
153 pb.allowed_access = perms;
155 if ((pb.parent_fd = open(path, O_PATH)) == -1)
158 err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
167 landlock_apply(int fd)
171 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
172 err(1, "%s: prctl(PR_SET_NO_NEW_PRIVS)", __func__);
174 r = landlock_restrict_self(fd, 0);
187 * XXX: landlock disabled at runtime, pretend everything's
190 if ((fd = open_landlock()) == -1)
193 return landlock_apply(fd);
197 sandbox_net_process(void)
200 * We don't know what paths are required for the TLS stack.
207 sandbox_ui_process(void)
210 * Needs to be able to read files *and* execute programs,
211 * can't be sandboxed.
218 #warning "No sandbox for this OS"
221 sandbox_net_process(void)
227 sandbox_ui_process(void)