2 * Copyright (c) 2021, 2023 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #if defined(__OpenBSD__)
26 sandbox_main_process(void)
28 if (pledge("stdio rpath wpath cpath inet dns sendfd", NULL) == -1)
33 sandbox_server_process(void)
35 if (pledge("stdio rpath inet unix dns recvfd", NULL) == -1)
40 sandbox_crypto_process(void)
42 if (pledge("stdio recvfd", NULL) == -1)
47 sandbox_logger_process(void)
49 if (pledge("stdio recvfd", NULL) == -1)
55 #include <linux/landlock.h>
57 #include <sys/prctl.h>
58 #include <sys/syscall.h>
59 #include <sys/types.h>
64 * What's the deal with landlock? While distro with linux >= 5.13
65 * have the struct declarations, libc wrappers are missing. The
66 * sample landlock code provided by the authors includes these "shims"
67 * in their example for the landlock API until libc provides them.
70 #ifndef landlock_create_ruleset
72 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
75 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
79 #ifndef landlock_add_rule
81 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
82 const void *attr, __u32 flags)
84 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
88 #ifndef landlock_restrict_self
90 landlock_restrict_self(int ruleset_fd, __u32 flags)
92 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
97 * Maybe we should ship with a full copy of the linux headers because
101 #ifndef LANDLOCK_ACCESS_FS_REFER
102 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
105 #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
106 #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
109 static int landlock_state;
110 static int landlock_fd;
113 * Initialize landlock, which is stupidly complicated.
118 struct landlock_ruleset_attr rattr = {
120 * List all capabilities currently defined by landlock.
121 * Failure in doing so will implicitly allow those actions
122 * (i.e. omitting READ_FILE will allow to read _any_ file.)
125 LANDLOCK_ACCESS_FS_EXECUTE |
126 LANDLOCK_ACCESS_FS_READ_FILE |
127 LANDLOCK_ACCESS_FS_READ_DIR |
128 LANDLOCK_ACCESS_FS_WRITE_FILE |
129 LANDLOCK_ACCESS_FS_REMOVE_DIR |
130 LANDLOCK_ACCESS_FS_REMOVE_FILE |
131 LANDLOCK_ACCESS_FS_MAKE_CHAR |
132 LANDLOCK_ACCESS_FS_MAKE_DIR |
133 LANDLOCK_ACCESS_FS_MAKE_REG |
134 LANDLOCK_ACCESS_FS_MAKE_SOCK |
135 LANDLOCK_ACCESS_FS_MAKE_FIFO |
136 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
137 LANDLOCK_ACCESS_FS_MAKE_SYM |
138 LANDLOCK_ACCESS_FS_REFER |
139 LANDLOCK_ACCESS_FS_TRUNCATE,
141 int fd, abi, saved_errno;
143 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
146 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
150 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
152 rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
155 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
163 if (landlock_restrict_self(landlock_fd, 0)) {
177 landlock_unveil(const char *path, const char *permissions)
179 struct landlock_path_beneath_attr lpba;
182 if (landlock_state == 0) {
183 if ((landlock_fd = landlock_init()) == -1) {
185 /* this kernel doesn't have landlock built in */
186 if (errno == ENOSYS || errno == EOPNOTSUPP)
192 /* no landlock available */
193 if (landlock_state == -1)
196 if (path == NULL && permissions == NULL)
197 return landlock_lock();
199 if (path == NULL || permissions == NULL || landlock_state != 1) {
204 if (!strcmp(permissions, "r")) {
205 fd = open(path, O_PATH | O_CLOEXEC);
208 lpba = (struct landlock_path_beneath_attr){
210 LANDLOCK_ACCESS_FS_READ_FILE |
211 LANDLOCK_ACCESS_FS_READ_DIR,
214 if (landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
228 sandbox_main_process(void)
234 sandbox_server_process(void)
236 if (landlock_unveil("/", "r") == -1)
237 fatal("landlock_unveil(/)");
239 if (landlock_unveil(NULL, NULL) == -1)
240 fatal("landlock_unveil(NULL, NULL)");
244 sandbox_crypto_process(void)
246 /* contrary to unveil(2), this removes all fs access. */
247 if (landlock_unveil(NULL, NULL) == -1)
248 fatal("landlock_unveil(NULL, NULL)");
252 sandbox_logger_process(void)
254 /* contrary to unveil(2), this removes all fs access. */
255 if (landlock_unveil(NULL, NULL) == -1)
256 fatal("landlock_unveil(NULL, NULL)");
262 sandbox_main_process(void)
268 sandbox_server_process(void)
274 sandbox_crypto_process(void)
280 sandbox_logger_process(void)