summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--data.c39
-rw-r--r--sandbox.c188
-rw-r--r--snac.h2
4 files changed, 194 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index 600419d..225031a 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CFLAGS?=-g -Wall -Wextra -pedantic
4 4
5all: snac 5all: snac
6 6
7snac: snac.o main.o data.o http.o httpd.o webfinger.o \ 7snac: snac.o main.o sandbox.o data.o http.o httpd.o webfinger.o \
8 activitypub.o html.o utils.o format.o upgrade.o mastoapi.o 8 activitypub.o html.o utils.o format.o upgrade.o mastoapi.o
9 $(CC) $(CFLAGS) -L/usr/local/lib *.o -lcurl -lcrypto $(LDFLAGS) -pthread -o $@ 9 $(CC) $(CFLAGS) -L/usr/local/lib *.o -lcurl -lcrypto $(LDFLAGS) -pthread -o $@
10 10
@@ -36,6 +36,8 @@ uninstall:
36activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \ 36activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
37 xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h snac.h \ 37 xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h snac.h \
38 http_codes.h 38 http_codes.h
39sandbox.o: sandbox.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h \
40 xs_glob.h xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h snac.h
39data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \ 41data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
40 xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h snac.h \ 42 xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h snac.h \
41 http_codes.h 43 http_codes.h
diff --git a/data.c b/data.c
index f2fa521..6f93098 100644
--- a/data.c
+++ b/data.c
@@ -115,44 +115,7 @@ int srv_open(const char *basedir, int auto_upgrade)
115#define st_mtim st_mtimespec 115#define st_mtim st_mtimespec
116#endif 116#endif
117 117
118#ifdef __OpenBSD__ 118 sbox_enter(srv_basedir);
119 if (xs_is_true(xs_dict_get(srv_config, "disable_openbsd_security"))) {
120 srv_debug(1, xs_dup("OpenBSD security disabled by admin"));
121 }
122 else {
123 int smail = !xs_is_true(xs_dict_get(srv_config, "disable_email_notifications"));
124 const char *address = xs_dict_get(srv_config, "address");
125
126 srv_debug(1, xs_fmt("Calling unveil()"));
127 unveil(basedir, "rwc");
128 unveil("/tmp", "rwc");
129 unveil("/etc/resolv.conf", "r");
130 unveil("/etc/hosts", "r");
131 unveil("/etc/ssl/openssl.cnf", "r");
132 unveil("/etc/ssl/cert.pem", "r");
133 unveil("/usr/share/zoneinfo", "r");
134
135 if (smail)
136 unveil("/usr/sbin/sendmail", "x");
137
138 if (*address == '/')
139 unveil(address, "rwc");
140
141 unveil(NULL, NULL);
142
143 srv_debug(1, xs_fmt("Calling pledge()"));
144
145 xs *p = xs_str_new("stdio rpath wpath cpath flock inet proc dns fattr");
146
147 if (smail)
148 p = xs_str_cat(p, " exec");
149
150 if (*address == '/')
151 p = xs_str_cat(p, " unix");
152
153 pledge(p, NULL);
154 }
155#endif /* __OpenBSD__ */
156 119
157 /* read (and drop) emojis.json, possibly creating it */ 120 /* read (and drop) emojis.json, possibly creating it */
158 xs_free(emojis()); 121 xs_free(emojis());
diff --git a/sandbox.c b/sandbox.c
new file mode 100644
index 0000000..c45587a
--- /dev/null
+++ b/sandbox.c
@@ -0,0 +1,188 @@
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 <sys/syscall.h>
11# include <sys/prctl.h>
12# include <stdint.h>
13# include <fcntl.h>
14#endif
15
16void sbox_enter(const char *basedir)
17{
18 if (xs_is_true(xs_dict_get(srv_config, "disable_openbsd_security"))) {
19 srv_log(xs_dup("disable_openbsd_security is deprecated. Use disable_sandbox instead."));
20 return;
21 }
22 if (xs_is_true(xs_dict_get(srv_config, "disable_sandbox"))) {
23 srv_debug(0, xs_dup("Sandbox disabled by admin"));
24 return;
25 }
26
27 const char *address = xs_dict_get(srv_config, "address");
28
29#if defined (__OpenBSD__)
30 int smail = !xs_is_true(xs_dict_get(srv_config, "disable_email_notifications"));
31
32 srv_debug(1, xs_fmt("Calling unveil()"));
33 unveil(basedir, "rwc");
34 unveil("/tmp", "rwc");
35 unveil("/etc/resolv.conf", "r");
36 unveil("/etc/hosts", "r");
37 unveil("/etc/ssl/openssl.cnf", "r");
38 unveil("/etc/ssl/cert.pem", "r");
39 unveil("/usr/share/zoneinfo", "r");
40
41 if (smail)
42 unveil("/usr/sbin/sendmail", "x");
43
44 if (*address == '/')
45 unveil(address, "rwc");
46
47 unveil(NULL, NULL);
48
49 srv_debug(1, xs_fmt("Calling pledge()"));
50
51 xs *p = xs_str_new("stdio rpath wpath cpath flock inet proc dns fattr");
52
53 if (smail)
54 p = xs_str_cat(p, " exec");
55
56 if (*address == '/')
57 p = xs_str_cat(p, " unix");
58
59 pledge(p, NULL);
60
61 xs_free(p);
62#elif defined (__linux__)
63 int error, ruleset_fd, abi;
64 struct landlock_ruleset_attr rules = {0};
65 struct landlock_path_beneath_attr path = {0};
66 struct landlock_net_port_attr net = {0};
67
68 rules.handled_access_fs =
69 LANDLOCK_ACCESS_FS_EXECUTE |
70 LANDLOCK_ACCESS_FS_WRITE_FILE |
71 LANDLOCK_ACCESS_FS_READ_FILE |
72 LANDLOCK_ACCESS_FS_REMOVE_DIR |
73 LANDLOCK_ACCESS_FS_REMOVE_FILE |
74 LANDLOCK_ACCESS_FS_MAKE_CHAR |
75 LANDLOCK_ACCESS_FS_MAKE_DIR |
76 LANDLOCK_ACCESS_FS_MAKE_REG |
77 LANDLOCK_ACCESS_FS_MAKE_SOCK |
78 LANDLOCK_ACCESS_FS_MAKE_FIFO |
79 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
80 LANDLOCK_ACCESS_FS_MAKE_SYM |
81 LANDLOCK_ACCESS_FS_REFER |
82 LANDLOCK_ACCESS_FS_TRUNCATE |
83 LANDLOCK_ACCESS_FS_IOCTL_DEV;
84 rules.handled_access_net =
85 LANDLOCK_ACCESS_NET_BIND_TCP |
86 LANDLOCK_ACCESS_NET_CONNECT_TCP;
87
88 abi = syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
89 switch (abi) {
90 case -1:
91 srv_debug(0, xs_dup("Kernel without landlock support"));
92 return;
93 case 1:
94 rules.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
95 __attribute__((fallthrough));
96 case 2:
97 rules.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
98 __attribute__((fallthrough));
99 case 3:
100 rules.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP);
101 __attribute__((fallthrough));
102 case 4:
103 rules.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
104 }
105 srv_debug(1, xs_fmt("lanlock abi: %d", abi));
106
107 ruleset_fd = syscall(SYS_landlock_create_ruleset, &rules, sizeof(struct landlock_ruleset_attr), 0);
108 if (ruleset_fd == -1) {
109 srv_debug(0, xs_fmt("landlock_create_ruleset failed: %s", strerror(errno)));
110 return;
111 }
112
113#define LL_R LANDLOCK_ACCESS_FS_READ_FILE
114#define LL_X LANDLOCK_ACCESS_FS_EXECUTE
115#define LL_RWCF (LL_R | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_TRUNCATE | LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_REFER)
116#define LL_RWCD (LL_RWCF | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_REMOVE_DIR)
117#define LL_UNIX (LL_R | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_MAKE_SOCK)
118#define LL_CONN LANDLOCK_ACCESS_NET_CONNECT_TCP
119#define LL_BIND LANDLOCK_ACCESS_NET_BIND_TCP
120
121#define LANDLOCK_PATH(p, r) do {\
122 path.allowed_access = r;\
123 if (abi < 2)\
124 path.allowed_access &= ~LANDLOCK_ACCESS_FS_REFER;\
125 if (abi < 3)\
126 path.allowed_access &= ~LANDLOCK_ACCESS_FS_TRUNCATE;\
127 path.parent_fd = open(p, O_PATH | O_CLOEXEC);\
128 if (path.parent_fd == -1) {\
129 srv_debug(2, xs_fmt("open %s: %s", p, strerror(errno)));\
130 goto close;\
131 }\
132 error = syscall(SYS_landlock_add_rule, ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, &path, 0); \
133 if (error) {\
134 srv_debug(0, xs_fmt("LANDLOCK_PATH(%s): %s", p, strerror(errno)));\
135 goto close;\
136 }\
137} while (0)
138
139#define LANDLOCK_PORT(p, r) do {\
140 uint16_t _p = p;\
141 net.port = _p;\
142 net.allowed_access = r;\
143 error = syscall(SYS_landlock_add_rule, ruleset_fd, LANDLOCK_RULE_NET_PORT, &net, 0);\
144 if (error) {\
145 srv_debug(0, xs_fmt("LANDLOCK_PORT(%d): %s", _p, strerror(errno)));\
146 goto close;\
147 }\
148} while (0)
149
150 LANDLOCK_PATH(basedir, LL_RWCD);
151 LANDLOCK_PATH("/tmp", LL_RWCD);
152#ifndef WITHOUT_SHM
153 LANDLOCK_PATH("/dev/shm", LL_RWCF);
154#endif
155 LANDLOCK_PATH("/etc/resolv.conf", LL_R );
156 LANDLOCK_PATH("/etc/hosts", LL_R );
157 LANDLOCK_PATH("/etc/ssl/openssl.cnf", LL_R );
158 LANDLOCK_PATH("/etc/ssl/cert.pem", LL_R );
159 LANDLOCK_PATH("/usr/share/zoneinfo", LL_R );
160
161 if (*address == '/')
162 LANDLOCK_PATH(address, LL_UNIX);
163
164 if (abi > 3) {
165 if (*address != '/') {
166 LANDLOCK_PORT(
167 (uint16_t)xs_number_get(xs_dict_get(srv_config, "port")), LL_BIND);
168 }
169
170 LANDLOCK_PORT(80, LL_CONN);
171 LANDLOCK_PORT(443, LL_CONN);
172 }
173
174 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
175 srv_debug(0, xs_fmt("prctl SET_NO_NEW_PRIVS: %s", strerror(errno)));
176 goto close;
177 }
178
179 if (syscall(SYS_landlock_restrict_self, ruleset_fd, 0))
180 srv_debug(0, xs_fmt("landlock_restrict_self: %s", strerror(errno)));
181
182 srv_log(xs_dup("landlocked"));
183
184close:
185 close(ruleset_fd);
186
187#endif
188}
diff --git a/snac.h b/snac.h
index 8020978..ff02539 100644
--- a/snac.h
+++ b/snac.h
@@ -75,6 +75,8 @@ void snac_log(snac *user, xs_str *str);
75int srv_open(const char *basedir, int auto_upgrade); 75int srv_open(const char *basedir, int auto_upgrade);
76void srv_free(void); 76void srv_free(void);
77 77
78void sbox_enter(const char *basedir);
79
78int user_open(snac *snac, const char *uid); 80int user_open(snac *snac, const char *uid);
79void user_free(snac *snac); 81void user_free(snac *snac);
80xs_list *user_list(void); 82xs_list *user_list(void);