summaryrefslogtreecommitdiff
path: root/landloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'landloc.h')
-rw-r--r--landloc.h175
1 files changed, 175 insertions, 0 deletions
diff --git a/landloc.h b/landloc.h
new file mode 100644
index 0000000..e1ade20
--- /dev/null
+++ b/landloc.h
@@ -0,0 +1,175 @@
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 * Repository: https://git.sr.ht/~shtrophic/landloc.h
22 */
23
24/**
25 * Usage:
26 *
27 * Define a sandboxing function using the LL_BEGIN(...) and LL_END macros.
28 * the arguments of LL_BEGIN are the function's signature.
29 * Between those macros, implement your sandbox using LL_PATH() and LL_PORT() macros.
30 * Calling LL_PATH() and LL_PORT() anywhere else will not work.
31 * You may prepend `static` before LL_BEGIN to make the function static.
32 * You need (should) wrap your sandboxing code in another set of braces:
33 *
34LL_BEGIN(my_sandbox_function, const char *rw_path) {
35
36 LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE);
37 LL_PORT(443, LANDLOCK_ACCESS_NET_CONNECT_TCP);
38
39} LL_END
40
41 *
42 * Then, call it in your application's code.
43 *
44
45int main(void) {
46
47 int status = my_sandbox_function("some/path");
48
49 if (status != 0) {
50 // error
51 }
52
53}
54
55 *
56 * You may define LL_PRINTERR(fmt, ...) before including this header to enable debug output:
57 *
58
59#define LL_PRINTERR(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
60#include "landloc.h"
61
62 */
63
64#ifndef __LANDLOC_H__
65#define __LANDLOC_H__
66
67#ifndef __linux__
68#error "no landlock without linux"
69#endif
70
71#include <unistd.h>
72#include <linux/landlock.h>
73#include <sys/syscall.h>
74#include <sys/prctl.h>
75#include <fcntl.h>
76
77#ifndef O_PATH
78#define O_PATH 010000000
79#endif
80
81#ifndef LL_PRINTERR
82#define LL_PRINTERR(fmt, ...) (void)fmt;
83#else
84#include <string.h>
85#include <errno.h>
86#endif
87
88#define LL_FS_ALL (\
89 LANDLOCK_ACCESS_FS_EXECUTE |\
90 LANDLOCK_ACCESS_FS_WRITE_FILE |\
91 LANDLOCK_ACCESS_FS_READ_FILE |\
92 LANDLOCK_ACCESS_FS_READ_DIR |\
93 LANDLOCK_ACCESS_FS_REMOVE_DIR |\
94 LANDLOCK_ACCESS_FS_REMOVE_FILE |\
95 LANDLOCK_ACCESS_FS_MAKE_CHAR |\
96 LANDLOCK_ACCESS_FS_MAKE_DIR |\
97 LANDLOCK_ACCESS_FS_MAKE_REG |\
98 LANDLOCK_ACCESS_FS_MAKE_SOCK |\
99 LANDLOCK_ACCESS_FS_MAKE_FIFO |\
100 LANDLOCK_ACCESS_FS_MAKE_BLOCK |\
101 LANDLOCK_ACCESS_FS_MAKE_SYM |\
102 LANDLOCK_ACCESS_FS_REFER |\
103 LANDLOCK_ACCESS_FS_TRUNCATE |\
104 LANDLOCK_ACCESS_FS_IOCTL_DEV )
105
106#define LL_NET_ALL (\
107 LANDLOCK_ACCESS_NET_BIND_TCP |\
108 LANDLOCK_ACCESS_NET_CONNECT_TCP )
109
110#define LL_BEGIN(function, ...) int function(__VA_ARGS__) {\
111 int ll_rule_fd, ll_abi;\
112 struct landlock_ruleset_attr __rattr = {0};\
113 struct landlock_path_beneath_attr __pattr = {0};\
114 struct landlock_net_port_attr __nattr = {0};\
115 int __err = 0;\
116 __rattr.handled_access_fs = LL_FS_ALL;\
117 __rattr.handled_access_net = LL_NET_ALL;\
118 ll_abi = (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);\
119 switch (ll_abi) {\
120 case -1: return -1;\
121 case 1: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; __attribute__((fallthrough));\
122 case 2: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; __attribute__((fallthrough));\
123 case 3: __rattr.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP); __attribute__((fallthrough));\
124 case 4: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;\
125 default: break;\
126 }\
127 ll_rule_fd = (int)syscall(SYS_landlock_create_ruleset, &__rattr, sizeof(struct landlock_ruleset_attr), 0);\
128 if (-1 == ll_rule_fd) {\
129 LL_PRINTERR("landlock_create_ruleset: %s", strerror(errno));\
130 return -1;\
131 }
132
133#define LL_END \
134 __err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);\
135 if (-1 == __err) {\
136 LL_PRINTERR("set_no_new_privs: %s", strerror(errno));\
137 goto __close;\
138 }\
139 __err = (int)syscall(SYS_landlock_restrict_self, ll_rule_fd, 0);\
140 if (__err)\
141 LL_PRINTERR("landlock_restrict_self: %s", strerror(errno));\
142 __close: close(ll_rule_fd);\
143 return __err; }
144
145#define LL_PATH(p, rules) do {\
146 const char *__path = (p);\
147 __pattr.allowed_access = (rules) & __rattr.handled_access_fs;\
148 __pattr.parent_fd = open(__path, O_PATH | O_CLOEXEC);\
149 if (-1 == __pattr.parent_fd) {\
150 LL_PRINTERR("open(%s): %s", __path, strerror(errno));\
151 __err = -1;\
152 goto __close;\
153 }\
154 __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_PATH_BENEATH, &__pattr, 0);\
155 if (__err) {\
156 LL_PRINTERR("landlock_add_rule(%s): %s", __path, strerror(errno));\
157 goto __close;\
158 }\
159 close(__pattr.parent_fd);\
160} while (0)
161
162#define LL_PORT(p, rules) do {\
163 unsigned short __port = (p);\
164 __nattr.allowed_access = (rules);\
165 if (ll_abi > 3) {\
166 __nattr.port = __port;\
167 __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
168 if (__err) {\
169 LL_PRINTERR("landlock_add_rule(%u): %s", __port, strerror(errno));\
170 goto __close;\
171 }\
172 }\
173} while (0)
174
175#endif /* __LANDLOC_H__ */