Blame


1 97799ccd 2022-02-06 thomas /*
2 97799ccd 2022-02-06 thomas * Copyright (c) 2021 Omar Polo <op@openbsd.org>
3 97799ccd 2022-02-06 thomas *
4 97799ccd 2022-02-06 thomas * Permission to use, copy, modify, and distribute this software for any
5 97799ccd 2022-02-06 thomas * purpose with or without fee is hereby granted, provided that the above
6 97799ccd 2022-02-06 thomas * copyright notice and this permission notice appear in all copies.
7 97799ccd 2022-02-06 thomas *
8 97799ccd 2022-02-06 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 97799ccd 2022-02-06 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 97799ccd 2022-02-06 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 97799ccd 2022-02-06 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 97799ccd 2022-02-06 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 97799ccd 2022-02-06 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 97799ccd 2022-02-06 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 97799ccd 2022-02-06 thomas */
16 97799ccd 2022-02-06 thomas
17 97799ccd 2022-02-06 thomas #include <linux/landlock.h>
18 97799ccd 2022-02-06 thomas
19 97799ccd 2022-02-06 thomas #include <sys/prctl.h>
20 97799ccd 2022-02-06 thomas #include <sys/syscall.h>
21 97799ccd 2022-02-06 thomas
22 97799ccd 2022-02-06 thomas #include <errno.h>
23 97799ccd 2022-02-06 thomas #include <unistd.h>
24 97799ccd 2022-02-06 thomas
25 97799ccd 2022-02-06 thomas #include "got_compat.h"
26 97799ccd 2022-02-06 thomas
27 97799ccd 2022-02-06 thomas /*
28 97799ccd 2022-02-06 thomas * What's the deal with landlock? While distro with linux >= 5.13
29 97799ccd 2022-02-06 thomas * have the struct declarations, libc wrappers are missing. The
30 97799ccd 2022-02-06 thomas * sample landlock code provided by the authors includes these "shims"
31 97799ccd 2022-02-06 thomas * in their example for the landlock API until libc provides them.
32 97799ccd 2022-02-06 thomas */
33 97799ccd 2022-02-06 thomas
34 97799ccd 2022-02-06 thomas #ifndef landlock_create_ruleset
35 97799ccd 2022-02-06 thomas static inline int
36 97799ccd 2022-02-06 thomas landlock_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size,
37 97799ccd 2022-02-06 thomas __u32 flags)
38 97799ccd 2022-02-06 thomas {
39 97799ccd 2022-02-06 thomas return syscall(__NR_landlock_create_ruleset, attr, size, flags);
40 97799ccd 2022-02-06 thomas }
41 97799ccd 2022-02-06 thomas #endif
42 97799ccd 2022-02-06 thomas
43 97799ccd 2022-02-06 thomas #ifndef landlock_add_rule
44 97799ccd 2022-02-06 thomas static inline int
45 97799ccd 2022-02-06 thomas landlock_add_rule(int ruleset_fd, enum landlock_rule_type type,
46 97799ccd 2022-02-06 thomas const void *attr, __u32 flags)
47 97799ccd 2022-02-06 thomas {
48 97799ccd 2022-02-06 thomas return syscall(__NR_landlock_add_rule, ruleset_fd, type, attr, flags);
49 97799ccd 2022-02-06 thomas }
50 97799ccd 2022-02-06 thomas #endif
51 97799ccd 2022-02-06 thomas
52 97799ccd 2022-02-06 thomas #ifndef landlock_restrict_self
53 97799ccd 2022-02-06 thomas static inline int
54 97799ccd 2022-02-06 thomas landlock_restrict_self(int ruleset_fd, __u32 flags)
55 97799ccd 2022-02-06 thomas {
56 97799ccd 2022-02-06 thomas return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
57 97799ccd 2022-02-06 thomas }
58 97799ccd 2022-02-06 thomas #endif
59 97799ccd 2022-02-06 thomas
60 97799ccd 2022-02-06 thomas /*
61 dc607f07 2023-07-16 op * Maybe we should ship with a full copy of the linux headers because
62 dc607f07 2023-07-16 op * you never know...
63 dc607f07 2023-07-16 op */
64 dc607f07 2023-07-16 op
65 dc607f07 2023-07-16 op #ifndef LANDLOCK_ACCESS_FS_REFER
66 dc607f07 2023-07-16 op #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
67 dc607f07 2023-07-16 op #endif
68 dc607f07 2023-07-16 op
69 dc607f07 2023-07-16 op #ifndef LANDLOCK_ACCESS_FS_TRUNCATE
70 dc607f07 2023-07-16 op #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
71 dc607f07 2023-07-16 op #endif
72 dc607f07 2023-07-16 op
73 dc607f07 2023-07-16 op /*
74 97799ccd 2022-02-06 thomas * Revoke any fs access.
75 97799ccd 2022-02-06 thomas */
76 97799ccd 2022-02-06 thomas int
77 97799ccd 2022-02-06 thomas landlock_no_fs(void)
78 97799ccd 2022-02-06 thomas {
79 97799ccd 2022-02-06 thomas struct landlock_ruleset_attr rattr = {
80 9cefc4c1 2022-02-11 op /*
81 9cefc4c1 2022-02-11 op * List all capabilities currently defined by landlock.
82 9cefc4c1 2022-02-11 op * Failure in doing so will implicitly allow those actions
83 9cefc4c1 2022-02-11 op * (i.e. omitting READ_FILE will allow to read _any_ file.)
84 97799ccd 2022-02-06 thomas */
85 faa3086d 2023-07-16 op .handled_access_fs =
86 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_EXECUTE |
87 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_READ_FILE |
88 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_READ_DIR |
89 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_WRITE_FILE |
90 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_REMOVE_DIR |
91 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_REMOVE_FILE |
92 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_CHAR |
93 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_DIR |
94 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_REG |
95 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_SOCK |
96 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_FIFO |
97 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_BLOCK |
98 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_MAKE_SYM |
99 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_REFER |
100 faa3086d 2023-07-16 op LANDLOCK_ACCESS_FS_TRUNCATE,
101 97799ccd 2022-02-06 thomas };
102 dc607f07 2023-07-16 op int fd, abi, saved_errno;
103 97799ccd 2022-02-06 thomas
104 97799ccd 2022-02-06 thomas if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
105 97799ccd 2022-02-06 thomas return -1;
106 97799ccd 2022-02-06 thomas
107 dc607f07 2023-07-16 op abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
108 dc607f07 2023-07-16 op if (abi == -1) {
109 97799ccd 2022-02-06 thomas /* this kernel doesn't have landlock built in */
110 5e997b01 2022-02-11 op if (errno == ENOSYS || errno == EOPNOTSUPP)
111 97799ccd 2022-02-06 thomas return 0;
112 97799ccd 2022-02-06 thomas return -1;
113 97799ccd 2022-02-06 thomas }
114 dc607f07 2023-07-16 op if (abi < 2)
115 dc607f07 2023-07-16 op rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
116 dc607f07 2023-07-16 op if (abi < 3)
117 dc607f07 2023-07-16 op rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
118 97799ccd 2022-02-06 thomas
119 dc607f07 2023-07-16 op fd = landlock_create_ruleset(&rattr, sizeof(rattr), 0);
120 dc607f07 2023-07-16 op if (fd == -1)
121 dc607f07 2023-07-16 op return -1;
122 dc607f07 2023-07-16 op
123 97799ccd 2022-02-06 thomas if (landlock_restrict_self(fd, 0)) {
124 97799ccd 2022-02-06 thomas saved_errno = errno;
125 97799ccd 2022-02-06 thomas close(fd);
126 97799ccd 2022-02-06 thomas errno = saved_errno;
127 97799ccd 2022-02-06 thomas return -1;
128 97799ccd 2022-02-06 thomas }
129 97799ccd 2022-02-06 thomas
130 97799ccd 2022-02-06 thomas close(fd);
131 97799ccd 2022-02-06 thomas return 0;
132 97799ccd 2022-02-06 thomas }