Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@openbsd.org>
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 <linux/landlock.h>
18 #include <linux/prctl.h>
20 #include <sys/prctl.h>
21 #include <sys/syscall.h>
23 #include <errno.h>
24 #include <unistd.h>
26 #include "got_compat.h"
28 /*
29 * What's the deal with landlock? While distro with linux >= 5.13
30 * have the struct declarations, libc wrappers are missing. The
31 * sample landlock code provided by the authors includes these "shims"
32 * in their example for the landlock API until libc provides them.
33 */
35 #ifndef landlock_create_ruleset
36 static inline int
37 landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
38 __u32 flags)
39 {
40 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
41 }
42 #endif
44 #ifndef landlock_add_rule
45 static inline int
46 landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
47 const void *attr, __u32 flags)
48 {
49 return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
50 }
51 #endif
53 #ifndef landlock_restrict_self
54 static inline int
55 landlock_restrict_self(int ruleset_fd, __u32 flags)
56 {
57 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
58 }
59 #endif
61 /*
62 * Revoke any fs access.
63 */
64 int
65 landlock_no_fs(void)
66 {
67 struct landlock_ruleset_attr rattr = {
68 /*
69 * List all capabilities currently defined by landlock.
70 * Failure in doing so will implicitly allow those actions
71 * (i.e. omitting READ_FILE will allow to read _any_ file.)
72 */
73 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE | \
74 LANDLOCK_ACCESS_FS_READ_FILE | \
75 LANDLOCK_ACCESS_FS_READ_DIR | \
76 LANDLOCK_ACCESS_FS_WRITE_FILE | \
77 LANDLOCK_ACCESS_FS_REMOVE_DIR | \
78 LANDLOCK_ACCESS_FS_REMOVE_FILE | \
79 LANDLOCK_ACCESS_FS_MAKE_CHAR | \
80 LANDLOCK_ACCESS_FS_MAKE_DIR | \
81 LANDLOCK_ACCESS_FS_MAKE_REG | \
82 LANDLOCK_ACCESS_FS_MAKE_SOCK | \
83 LANDLOCK_ACCESS_FS_MAKE_FIFO | \
84 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
85 LANDLOCK_ACCESS_FS_MAKE_SYM,
86 };
87 int fd, saved_errno;
89 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
90 return -1;
92 fd = landlock_create_ruleset(&rattr, sizeof(rattr), 0);
93 if (fd == -1) {
94 /* this kernel doesn't have landlock built in */
95 if (errno == ENOSYS || errno == EOPNOTSUPP)
96 return 0;
97 return -1;
98 }
100 if (landlock_restrict_self(fd, 0)) {
101 saved_errno = errno;
102 close(fd);
103 errno = saved_errno;
104 return -1;
107 close(fd);
108 return 0;