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 /*
18 * This an implementation of an OpenBSD' unveil(2) compatible API on
19 * top of Linux' landlock.
20 */
22 #include <linux/landlock.h>
23 #include <linux/prctl.h>
25 #include <sys/prctl.h>
26 #include <sys/stat.h>
27 #include <sys/syscall.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <libgen.h>
32 #include <limits.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "got_compat.h"
38 /*
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.
43 */
45 #ifndef landlock_create_ruleset
46 static inline int
47 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
48 __u32 flags)
49 {
50 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
51 }
52 #endif
54 #ifndef landlock_add_rule
55 static inline int
56 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
57 const void *attr, __u32 flags)
58 {
59 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
60 }
61 #endif
63 #ifndef landlock_restrict_self
64 static inline int
65 landlock_restrict_self(int ruleset_fd, __u32 flags)
66 {
67 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
68 }
69 #endif
71 static int landlock_fd = -1;
73 static int
74 open_landlock(void)
75 {
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,
90 };
92 return landlock_create_ruleset(&rattr, sizeof(rattr), 0);
93 }
95 static int
96 landlock_apply(void)
97 {
98 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
99 return -1;
101 if (landlock_restrict_self(landlock_fd, 0))
102 return -1;
104 close(landlock_fd);
105 landlock_fd = -1;
106 return 0;
109 static int
110 parse_permissions(const char *permission)
112 int perm = 0;
114 for (; *permission; ++permission) {
115 switch (*permission) {
116 case 'r':
117 perm |= LANDLOCK_ACCESS_FS_READ_FILE;
118 perm |= LANDLOCK_ACCESS_FS_READ_DIR;
119 break;
120 case 'w':
121 perm |= LANDLOCK_ACCESS_FS_WRITE_FILE;
122 break;
123 case 'x':
124 perm |= LANDLOCK_ACCESS_FS_EXECUTE;
125 break;
126 case 'c':
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;
136 break;
137 default:
138 return -1;
142 return perm;
145 static int
146 landlock_unveil_path(const char *path, int permissions)
148 struct landlock_path_beneath_attr pb;
149 struct stat sb;
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)
155 return -1;
157 if (fstat(pb.parent_fd, &sb) == -1)
158 return -1;
160 if (!S_ISDIR(sb.st_mode)) {
161 close(pb.parent_fd);
163 if (strlcpy(fpath, path, sizeof(fpath)) >= sizeof(fpath)) {
164 errno = ENAMETOOLONG;
165 return -1;
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,
174 &pb, 0);
175 saved_errno = errno;
176 close(pb.parent_fd);
177 errno = saved_errno;
178 return err ? -1 : 0;
181 int
182 landlock_unveil(const char *path, const char *permissions)
184 int perms;
186 if (landlock_fd == -1) {
187 if ((landlock_fd = open_landlock()) == -1)
188 return -1;
190 /* XXX: use rpath on the current executable */
191 if (landlock_unveil("/lib64", "rx") == -1)
192 return -1;
195 if (path == NULL && permissions == NULL)
196 return landlock_apply();
198 if (path == NULL ||
199 permissions == NULL ||
200 (perms = parse_permissions(permissions)) == -1) {
201 errno = EINVAL;
202 return -1;
205 return landlock_unveil_path(path, perms);
208 int
209 landlock_no_fs(void)
211 if ((landlock_fd = open_landlock()) == -1)
212 return -1;
214 return landlock_apply();