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.
18 * This an implementation of an OpenBSD' unveil(2) compatible API on
19 * top of Linux' landlock.
22 #include <linux/landlock.h>
23 #include <linux/prctl.h>
25 #include <sys/prctl.h>
27 #include <sys/syscall.h>
36 #include "got_compat.h"
39 * What's the deal with landlock? While distro with linux >= 5.13
40 * have the struct declarations, libc wrappers are missing. The
41 * sample landlock code provided by the authors includes these "shims"
42 * in their example for the landlock API until libc provides them.
45 #ifndef landlock_create_ruleset
47 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
50 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
54 #ifndef landlock_add_rule
56 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
57 const void *attr, __u32 flags)
59 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
63 #ifndef landlock_restrict_self
65 landlock_restrict_self(int ruleset_fd, __u32 flags)
67 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
71 static int landlock_fd = -1;
76 struct landlock_ruleset_attr rattr = {
77 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
78 LANDLOCK_ACCESS_FS_WRITE_FILE |
79 LANDLOCK_ACCESS_FS_READ_FILE |
80 LANDLOCK_ACCESS_FS_READ_DIR |
81 LANDLOCK_ACCESS_FS_REMOVE_DIR |
82 LANDLOCK_ACCESS_FS_REMOVE_FILE |
83 LANDLOCK_ACCESS_FS_MAKE_CHAR |
84 LANDLOCK_ACCESS_FS_MAKE_DIR |
85 LANDLOCK_ACCESS_FS_MAKE_REG |
86 LANDLOCK_ACCESS_FS_MAKE_SOCK |
87 LANDLOCK_ACCESS_FS_MAKE_FIFO |
88 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
89 LANDLOCK_ACCESS_FS_MAKE_SYM,
92 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
98 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
101 if (landlock_restrict_self(landlock_fd, 0))
110 parse_permissions(const char *permission)
114 for (; *permission; ++permission) {
115 switch (*permission) {
117 perm |= LANDLOCK_ACCESS_FS_READ_FILE;
118 perm |= LANDLOCK_ACCESS_FS_READ_DIR;
121 perm |= LANDLOCK_ACCESS_FS_WRITE_FILE;
124 perm |= LANDLOCK_ACCESS_FS_EXECUTE;
127 perm |= LANDLOCK_ACCESS_FS_REMOVE_DIR;
128 perm |= LANDLOCK_ACCESS_FS_REMOVE_FILE;
129 perm |= LANDLOCK_ACCESS_FS_MAKE_CHAR;
130 perm |= LANDLOCK_ACCESS_FS_MAKE_DIR;
131 perm |= LANDLOCK_ACCESS_FS_MAKE_REG;
132 perm |= LANDLOCK_ACCESS_FS_MAKE_SOCK;
133 perm |= LANDLOCK_ACCESS_FS_MAKE_FIFO;
134 perm |= LANDLOCK_ACCESS_FS_MAKE_BLOCK;
135 perm |= LANDLOCK_ACCESS_FS_MAKE_SYM;
146 landlock_unveil_path(const char *path, int permissions)
148 struct landlock_path_beneath_attr pb;
150 int fd, err, saved_errno;
151 char fpath[PATH_MAX];
153 pb.allowed_access = permissions;
154 if ((pb.parent_fd = open(path, O_PATH)) == -1)
157 if (fstat(pb.parent_fd, &sb) == -1)
160 if (!S_ISDIR(sb.st_mode)) {
163 if (strlcpy(fpath, path, sizeof(fpath)) >= sizeof(fpath)) {
164 errno = ENAMETOOLONG;
168 permissions |= LANDLOCK_ACCESS_FS_READ_FILE;
169 permissions |= LANDLOCK_ACCESS_FS_READ_DIR;
170 return landlock_unveil_path(dirname(fpath), permissions);
173 err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
182 landlock_unveil(const char *path, const char *permissions)
186 if (landlock_fd == -1) {
187 if ((landlock_fd = open_landlock()) == -1)
190 /* XXX: use rpath on the current executable */
191 if (landlock_unveil("/lib64", "rx") == -1)
195 if (path == NULL && permissions == NULL)
196 return landlock_apply();
199 permissions == NULL ||
200 (perms = parse_permissions(permissions)) == -1) {
205 return landlock_unveil_path(path, perms);
211 if ((landlock_fd = open_landlock()) == -1)
214 return landlock_apply();