summaryrefslogtreecommitdiff
path: root/sandbox.c
diff options
context:
space:
mode:
authorGravatar shtrophic2024-11-12 21:01:09 +0100
committerGravatar shtrophic2024-11-16 00:54:12 +0100
commit75f615905629f64f40363161281d640010153d64 (patch)
treeafd40445102e4bac6da69e55ee96a0f97dcb3479 /sandbox.c
parentMinor tweak to proxy code (some URLs were not correctly formed). (diff)
downloadpenes-snac2-75f615905629f64f40363161281d640010153d64.tar.gz
penes-snac2-75f615905629f64f40363161281d640010153d64.tar.xz
penes-snac2-75f615905629f64f40363161281d640010153d64.zip
sandboxing port to linux via landlock
Diffstat (limited to 'sandbox.c')
-rw-r--r--sandbox.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/sandbox.c b/sandbox.c
new file mode 100644
index 0000000..f83a947
--- /dev/null
+++ b/sandbox.c
@@ -0,0 +1,184 @@
1#include "xs.h"
2
3#include "snac.h"
4
5#include <unistd.h>
6
7#if defined (__linux__)
8# define __USE_GNU
9# include <linux/landlock.h>
10# include <linux/prctl.h>
11# include <sys/syscall.h>
12# include <sys/prctl.h>
13# include <fcntl.h>
14# include <arpa/inet.h>
15#endif
16
17void sbox_enter(const char *basedir)
18{
19 if (xs_is_true(xs_dict_get(srv_config, "disable_openbsd_security"))) {
20 srv_log(xs_dup("disable_openbsd_security is deprecated. Use disable_sandbox instead."));
21 return;
22 }
23 if (xs_is_true(xs_dict_get(srv_config, "disable_sandbox"))) {
24 srv_debug(0, xs_dup("Sandbox disabled by admin"));
25 return;
26 }
27
28 const char *address = xs_dict_get(srv_config, "address");
29
30#if defined (__OpenBSD__)
31 int smail = !xs_is_true(xs_dict_get(srv_config, "disable_email_notifications"));
32
33 srv_debug(1, xs_fmt("Calling unveil()"));
34 unveil(basedir, "rwc");
35 unveil("/tmp", "rwc");
36 unveil("/etc/resolv.conf", "r");
37 unveil("/etc/hosts", "r");
38 unveil("/etc/ssl/openssl.cnf", "r");
39 unveil("/etc/ssl/cert.pem", "r");
40 unveil("/usr/share/zoneinfo", "r");
41
42 if (smail)
43 unveil("/usr/sbin/sendmail", "x");
44
45 if (*address == '/')
46 unveil(address, "rwc");
47
48 unveil(NULL, NULL);
49
50 srv_debug(1, xs_fmt("Calling pledge()"));
51
52 xs *p = xs_str_new("stdio rpath wpath cpath flock inet proc dns fattr");
53
54 if (smail)
55 p = xs_str_cat(p, " exec");
56
57 if (*address == '/')
58 p = xs_str_cat(p, " unix");
59
60 pledge(p, NULL);
61
62 xs_free(p);
63#elif defined (__linux__)
64 int error, ruleset_fd, abi;
65 struct landlock_ruleset_attr rules = {0};
66 struct landlock_path_beneath_attr path = {0};
67 struct landlock_net_port_attr net = {0};
68
69 rules.handled_access_fs =
70 LANDLOCK_ACCESS_FS_EXECUTE |
71 LANDLOCK_ACCESS_FS_WRITE_FILE |
72 LANDLOCK_ACCESS_FS_READ_FILE |
73 LANDLOCK_ACCESS_FS_REMOVE_DIR |
74 LANDLOCK_ACCESS_FS_REMOVE_FILE |
75 LANDLOCK_ACCESS_FS_MAKE_CHAR |
76 LANDLOCK_ACCESS_FS_MAKE_DIR |
77 LANDLOCK_ACCESS_FS_MAKE_REG |
78 LANDLOCK_ACCESS_FS_MAKE_SOCK |
79 LANDLOCK_ACCESS_FS_MAKE_FIFO |
80 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
81 LANDLOCK_ACCESS_FS_MAKE_SYM |
82 LANDLOCK_ACCESS_FS_REFER |
83 LANDLOCK_ACCESS_FS_TRUNCATE |
84 LANDLOCK_ACCESS_FS_IOCTL_DEV;
85 rules.handled_access_net =
86 LANDLOCK_ACCESS_NET_BIND_TCP |
87 LANDLOCK_ACCESS_NET_CONNECT_TCP;
88
89 abi = syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
90 switch (abi) {
91 case -1:
92 srv_debug(0, xs_dup("Kernel without landlock support"));
93 return;
94 case 1:
95 rules.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
96 __attribute__((fallthrough));
97 case 2:
98 rules.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
99 __attribute__((fallthrough));
100 case 3:
101 rules.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP);
102 __attribute__((fallthrough));
103 case 4:
104 rules.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
105 }
106 srv_debug(1, xs_fmt("lanlock abi: %d", abi));
107
108 ruleset_fd = syscall(SYS_landlock_create_ruleset, &rules, sizeof(struct landlock_ruleset_attr), 0);
109 if (ruleset_fd == -1) {
110 srv_debug(0, xs_fmt("landlock_create_ruleset failed: %s", strerror(errno)));
111 return;
112 }
113
114#define LL_R LANDLOCK_ACCESS_FS_READ_FILE
115#define LL_X LANDLOCK_ACCESS_FS_EXECUTE
116#define LL_RWC (LL_R | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_TRUNCATE)
117#define LL_UNX (LL_R | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_MAKE_SOCK)
118#define LL_CON LANDLOCK_ACCESS_NET_CONNECT_TCP
119#define LL_BND LANDLOCK_ACCESS_NET_BIND_TCP
120
121#define LANDLOCK_PATH(p, r) do {\
122 path.allowed_access = r;\
123 if (abi < 3)\
124 path.allowed_access &= ~LANDLOCK_ACCESS_FS_TRUNCATE;\
125 path.parent_fd = open(p, O_PATH | O_CLOEXEC);\
126 if (path.parent_fd == -1) {\
127 srv_debug(2, xs_fmt("open %s: %s", p, strerror(errno)));\
128 goto close;\
129 }\
130 error = syscall(SYS_landlock_add_rule, ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, &path, 0); \
131 if (error) {\
132 srv_debug(0, xs_fmt("LANDLOCK_PATH(%s): %s", p, strerror(errno)));\
133 goto close;\
134 }\
135} while (0)
136
137#define LANDLOCK_PORT(p, r) do {\
138 uint16_t _p = p;\
139 net.port = _p;\
140 net.allowed_access = r;\
141 error = syscall(SYS_landlock_add_rule, ruleset_fd, LANDLOCK_RULE_NET_PORT, &net, 0);\
142 if (error) {\
143 srv_debug(0, xs_fmt("LANDLOCK_PORT(%d): %s", _p, strerror(errno)));\
144 goto close;\
145 }\
146} while (0)
147
148 LANDLOCK_PATH(basedir, LL_RWC);
149 LANDLOCK_PATH("/tmp", LL_RWC);
150 LANDLOCK_PATH("/dev/shm", LL_RWC);
151 LANDLOCK_PATH("/etc/resolv.conf", LL_R );
152 LANDLOCK_PATH("/etc/hosts", LL_R );
153 LANDLOCK_PATH("/etc/ssl/openssl.cnf", LL_R );
154 LANDLOCK_PATH("/etc/ssl/cert.pem", LL_R );
155 LANDLOCK_PATH("/usr/share/zoneinfo", LL_R );
156
157 if (*address == '/')
158 LANDLOCK_PATH(address, LL_UNX);
159
160 if (abi > 3) {
161 if (*address != '/') {
162 LANDLOCK_PORT(
163 (uint16_t)xs_number_get(xs_dict_get(srv_config, "port")), LL_BND);
164 }
165
166 LANDLOCK_PORT(80, LL_CON);
167 LANDLOCK_PORT(443, LL_CON);
168 }
169
170 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
171 srv_debug(0, xs_fmt("prctl SET_NO_NEW_PRIVS: %s", strerror(errno)));
172 goto close;
173 }
174
175 if (syscall(SYS_landlock_restrict_self, ruleset_fd, 0))
176 srv_debug(0, xs_fmt("landlock_restrict_self: %s", strerror(errno)));
177
178 srv_log(xs_dup("landlocked"));
179
180close:
181 close(ruleset_fd);
182
183#endif
184}