summaryrefslogtreecommitdiff
path: root/landloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'landloc.h')
-rw-r--r--landloc.h153
1 files changed, 153 insertions, 0 deletions
diff --git a/landloc.h b/landloc.h
new file mode 100644
index 0000000..c5b849a
--- /dev/null
+++ b/landloc.h
@@ -0,0 +1,153 @@
1/**
2 * Zero-Clause BSD
3 * ===============
4 *
5 * Copyright 2024 shtrophic <christoph@liebender.dev>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for
8 * any purpose with or without fee is hereby granted.
9 *
10 * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
13 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
14 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
15 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20/**
21 * Usage example:
22 *
23
24LL_BEGIN(my_sandbox_function, const char *rw_path) {
25
26 LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE);
27 LL_PORT(443, LANDLOCK_ACCESS_NET_CONNECT_TCP);
28
29} LL_END
30
31int main(void) {
32
33 int status = my_sandbox_function("some/path");
34
35 if (status != 0) {
36 // error
37 }
38
39}
40 */
41
42#ifndef __LANDLOC_H__
43#define __LANDLOC_H__
44
45#ifndef __linux__
46#error "no landlock without linux"
47#endif
48
49#include <unistd.h>
50#include <linux/landlock.h>
51#include <sys/syscall.h>
52#include <sys/prctl.h>
53#include <fcntl.h>
54
55#ifndef O_PATH
56#define O_PATH 010000000
57#endif
58
59#ifndef LL_PRINTERR
60#define LL_PRINTERR(fmt, ...) (void)fmt;
61#else
62#include <string.h>
63#include <errno.h>
64#endif
65
66#define LL_FS_ALL (\
67 LANDLOCK_ACCESS_FS_EXECUTE |\
68 LANDLOCK_ACCESS_FS_WRITE_FILE |\
69 LANDLOCK_ACCESS_FS_READ_FILE |\
70 LANDLOCK_ACCESS_FS_READ_DIR |\
71 LANDLOCK_ACCESS_FS_REMOVE_DIR |\
72 LANDLOCK_ACCESS_FS_REMOVE_FILE |\
73 LANDLOCK_ACCESS_FS_MAKE_CHAR |\
74 LANDLOCK_ACCESS_FS_MAKE_DIR |\
75 LANDLOCK_ACCESS_FS_MAKE_REG |\
76 LANDLOCK_ACCESS_FS_MAKE_SOCK |\
77 LANDLOCK_ACCESS_FS_MAKE_FIFO |\
78 LANDLOCK_ACCESS_FS_MAKE_BLOCK |\
79 LANDLOCK_ACCESS_FS_MAKE_SYM |\
80 LANDLOCK_ACCESS_FS_REFER |\
81 LANDLOCK_ACCESS_FS_TRUNCATE |\
82 LANDLOCK_ACCESS_FS_IOCTL_DEV )
83
84#define LL_NET_ALL (\
85 LANDLOCK_ACCESS_NET_BIND_TCP |\
86 LANDLOCK_ACCESS_NET_CONNECT_TCP )
87
88#define LL_BEGIN(function, ...) int function(__VA_ARGS__) {\
89 int ll_rule_fd, ll_abi;\
90 struct landlock_ruleset_attr __rattr = {0};\
91 struct landlock_path_beneath_attr __pattr = {0};\
92 struct landlock_net_port_attr __nattr = {0};\
93 int __err = 0;\
94 __rattr.handled_access_fs = LL_FS_ALL;\
95 __rattr.handled_access_net = LL_NET_ALL;\
96 ll_abi = (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);\
97 switch (ll_abi) {\
98 case -1: return -1;\
99 case 1: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; __attribute__((fallthrough));\
100 case 2: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; __attribute__((fallthrough));\
101 case 3: __rattr.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP); __attribute__((fallthrough));\
102 case 4: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;\
103 default: break;\
104 }\
105 ll_rule_fd = (int)syscall(SYS_landlock_create_ruleset, &__rattr, sizeof(struct landlock_ruleset_attr), 0);\
106 if (-1 == ll_rule_fd) {\
107 LL_PRINTERR("landlock_create_ruleset: %s", strerror(errno));\
108 return -1;\
109 }
110
111#define LL_END \
112 __err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);\
113 if (-1 == __err) {\
114 LL_PRINTERR("set_no_new_privs: %s", strerror(errno));\
115 goto __close;\
116 }\
117 __err = (int)syscall(SYS_landlock_restrict_self, ll_rule_fd, 0);\
118 if (__err)\
119 LL_PRINTERR("landlock_restrict_self: %s", strerror(errno));\
120 __close: close(ll_rule_fd);\
121 return __err; }
122
123#define LL_PATH(p, rules) do {\
124 const char *__path = (p);\
125 __pattr.allowed_access = (rules) & __rattr.handled_access_fs;\
126 __pattr.parent_fd = open(__path, O_PATH | O_CLOEXEC);\
127 if (-1 == __pattr.parent_fd) {\
128 LL_PRINTERR("open(%s): %s", __path, strerror(errno));\
129 __err = -1;\
130 goto __close;\
131 }\
132 __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_PATH_BENEATH, &__pattr, 0);\
133 if (__err) {\
134 LL_PRINTERR("landlock_add_rule(%s): %s", __path, strerror(errno));\
135 goto __close;\
136 }\
137 close(__pattr.parent_fd);\
138} while (0)
139
140#define LL_PORT(p, rules) do {\
141 if (ll_abi > 3) {\
142 unsigned short __port = (p);\
143 __nattr.allowed_access = (rules);\
144 __nattr.port = __port;\
145 __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
146 if (__err) {\
147 LL_PRINTERR("landlock_add_rule(%u): %s", __port, strerror(errno));\
148 goto __close;\
149 }\
150 }\
151} while (0)
152
153#endif /* __LANDLOC_H__ */