2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #warning "Sandbox disabled! Please report issues upstream instead of disabling the sandbox."
24 sandbox_server_process(void)
30 sandbox_executor_process(void)
32 log_notice(NULL, "Sandbox disabled! "
33 "Please report issues upstream instead of disabling the sandbox.");
37 sandbox_logger_process(void)
42 #elif defined(__FreeBSD__)
44 #include <sys/capsicum.h>
47 sandbox_server_process(void)
49 if (cap_enter() == -1)
54 sandbox_executor_process(void)
57 * We cannot capsicum the executor process because it needs to
58 * fork(2)+execve(2) cgi scripts
64 sandbox_logger_process(void)
66 if (cap_enter() == -1)
70 #elif defined(__linux__)
72 #include <sys/ioctl.h>
73 #include <sys/prctl.h>
74 #include <sys/syscall.h>
75 #include <sys/syscall.h>
76 #include <sys/types.h>
78 #include <linux/audit.h>
79 #include <linux/filter.h>
80 #include <linux/seccomp.h>
89 # include "landlock_shim.h"
92 /* uncomment to enable debugging. ONLY FOR DEVELOPMENT */
93 /* #define SC_DEBUG */
96 # define SC_FAIL SECCOMP_RET_TRAP
98 # define SC_FAIL SECCOMP_RET_KILL
101 #if (BYTE_ORDER == LITTLE_ENDIAN)
103 # define SC_ARG_HI sizeof(uint32_t)
104 #elif (BYTE_ORDER == BIG_ENDIAN)
105 # define SC_ARG_LO sizeof(uint32_t)
108 # error "Uknown endian"
111 /* make the filter more readable */
112 #define SC_ALLOW(nr) \
113 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_##nr, 0, 1), \
114 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
117 * SC_ALLOW_ARG and the SECCOMP_AUDIT_ARCH below are courtesy of
118 * https://roy.marples.name/git/dhcpcd/blob/HEAD:/src/privsep-linux.c
120 #define SC_ALLOW_ARG(_nr, _arg, _val) \
121 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 6), \
122 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
123 offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_LO), \
124 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \
125 ((_val) & 0xffffffff), 0, 3), \
126 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
127 offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_HI), \
128 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \
129 (((uint32_t)((uint64_t)(_val) >> 32)) & 0xffffffff), 0, 1), \
130 BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), \
131 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
132 offsetof(struct seccomp_data, nr))
135 * I personally find this quite nutty. Why can a system header not
136 * define a default for this?
138 #if defined(__i386__)
139 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
140 #elif defined(__x86_64__)
141 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
142 #elif defined(__arc__)
144 # if (BYTE_ORDER == LITTLE_ENDIAN)
145 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACT
147 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACTBE
149 # elif defined(__HS__)
150 # if (BYTE_ORDER == LITTLE_ENDIAN)
151 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2
153 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2BE
156 # error "Platform does not support seccomp filter yet"
158 #elif defined(__arm__)
162 # if (BYTE_ORDER == LITTLE_ENDIAN)
163 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
165 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARMEB
167 #elif defined(__aarch64__)
168 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
169 #elif defined(__alpha__)
170 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ALPHA
171 #elif defined(__hppa__)
172 # if defined(__LP64__)
173 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC64
175 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC
177 #elif defined(__ia64__)
178 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_IA64
179 #elif defined(__microblaze__)
180 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MICROBLAZE
181 #elif defined(__m68k__)
182 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_M68K
183 #elif defined(__mips__)
184 # if defined(__MIPSEL__)
185 # if defined(__LP64__)
186 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL64
188 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL
190 # elif defined(__LP64__)
191 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS64
193 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS
195 #elif defined(__nds32__)
196 # if (BYTE_ORDER == LITTLE_ENDIAN)
197 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32
199 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32BE
201 #elif defined(__nios2__)
202 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NIOS2
203 #elif defined(__or1k__)
204 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_OPENRISC
205 #elif defined(__powerpc64__)
206 # if (BYTE_ORDER == LITTLE_ENDIAN)
207 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64LE
209 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64
211 #elif defined(__powerpc__)
212 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC
213 #elif defined(__riscv)
214 # if defined(__LP64__)
215 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64
217 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV32
219 #elif defined(__s390x__)
220 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390X
221 #elif defined(__s390__)
222 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390
223 #elif defined(__sh__)
224 # if defined(__LP64__)
225 # if (BYTE_ORDER == LITTLE_ENDIAN)
226 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL64
228 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH64
231 # if (BYTE_ORDER == LITTLE_ENDIAN)
232 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL
234 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH
237 #elif defined(__sparc__)
238 # if defined(__arch64__)
239 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC64
241 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC
243 #elif defined(__xtensa__)
244 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_XTENSA
246 # error "Platform does not support seccomp filter yet"
249 static struct sock_filter filter[] = {
250 /* load the *current* architecture */
251 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
252 (offsetof(struct seccomp_data, arch))),
253 /* ensure it's the same that we've been compiled on */
254 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K,
255 SECCOMP_AUDIT_ARCH, 1, 0),
256 /* if not, kill the program */
257 BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
259 /* load the syscall number */
260 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
261 (offsetof(struct seccomp_data, nr))),
272 #ifdef __NR_clock_gettime
273 SC_ALLOW(clock_gettime),
275 #if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
276 SECCOMP_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT),
278 #ifdef __NR_clock_gettime64
279 SC_ALLOW(clock_gettime64),
284 #ifdef __NR_epoll_ctl
287 #ifdef __NR_epoll_pwait
288 SC_ALLOW(epoll_pwait),
290 #ifdef __NR_epoll_wait
291 SC_ALLOW(epoll_wait),
296 #ifdef __NR_exit_group
297 SC_ALLOW(exit_group),
311 #ifdef __NR_getdents64
312 SC_ALLOW(getdents64),
317 #ifdef __NR_getrandom
320 #ifdef __NR_gettimeofday
321 SC_ALLOW(gettimeofday),
324 /* allow ioctl on fd 1, glibc doing stuff? */
325 SC_ALLOW_ARG(__NR_ioctl, 0, 1),
326 /* allow FIONREAD needed by libevent */
327 SC_ALLOW_ARG(__NR_ioctl, 1, FIONREAD),
344 #ifdef __NR_newfstatat
345 SC_ALLOW(newfstatat),
353 #ifdef __NR_prlimit64
365 #ifdef __NR_rt_sigaction
366 SC_ALLOW(rt_sigaction),
368 #ifdef __NR_rt_sigreturn
369 SC_ALLOW(rt_sigreturn),
377 #ifdef __NR_ugetrlimit
378 SC_ALLOW(ugetrlimit),
387 /* disallow everything else */
388 BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
397 sandbox_seccomp_violation(int signum, siginfo_t *info, void *ctx)
399 fprintf(stderr, "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)\n",
400 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
405 sandbox_seccomp_catch_sigsys(void)
407 struct sigaction act;
410 memset(&act, 0, sizeof(act));
412 sigaddset(&mask, SIGSYS);
414 act.sa_sigaction = &sandbox_seccomp_violation;
415 act.sa_flags = SA_SIGINFO;
416 if (sigaction(SIGSYS, &act, NULL) == -1)
417 fatal("%s: sigaction(SIGSYS): %s",
418 __func__, strerror(errno));
420 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
421 fatal("%s: sigprocmask(SIGSYS): %s\n",
422 __func__, strerror(errno));
424 #endif /* SC_DEBUG */
432 struct landlock_ruleset_attr attr = {
433 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
434 LANDLOCK_ACCESS_FS_READ_FILE |
435 LANDLOCK_ACCESS_FS_READ_DIR |
436 LANDLOCK_ACCESS_FS_WRITE_FILE |
437 LANDLOCK_ACCESS_FS_REMOVE_DIR |
438 LANDLOCK_ACCESS_FS_REMOVE_FILE |
439 LANDLOCK_ACCESS_FS_MAKE_CHAR |
440 LANDLOCK_ACCESS_FS_MAKE_DIR |
441 LANDLOCK_ACCESS_FS_MAKE_REG |
442 LANDLOCK_ACCESS_FS_MAKE_SOCK |
443 LANDLOCK_ACCESS_FS_MAKE_FIFO |
444 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
445 LANDLOCK_ACCESS_FS_MAKE_SYM,
448 fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
452 fatal("%s: failed to create ruleset. "
453 "Landlock doesn't seem to be supported by the "
454 "current kernel.", __func__);
456 log_warn(NULL, "%s: failed to create ruleset. "
457 "Landlock seems to be currently disabled; "
458 "continuing without it.", __func__);
461 fatal("%s: failed to create ruleset: %s",
462 __func__, strerror(errno));
470 landlock_unveil_path(int landlock_fd, const char *path, int perms)
472 struct landlock_path_beneath_attr pb;
473 int err, saved_errno;
475 pb.allowed_access = perms;
477 if ((pb.parent_fd = open(path, O_PATH)) == -1)
480 err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
489 landlock_apply(int fd)
496 r = landlock_restrict_self(fd, 0);
504 server_landlock(void)
511 * These are all the actions allowed for the root directories
514 perms = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR;
516 if ((fd = open_landlock()) == -1)
519 TAILQ_FOREACH(h, &hosts, vhosts) {
520 TAILQ_FOREACH(l, &h->locations, locations) {
524 if (landlock_unveil_path(fd, l->dir, perms) == -1)
525 fatal("%s: landlock_unveil_path(%s): %s",
526 __func__, l->dir, strerror(errno));
530 return landlock_apply(fd);
534 logger_landlock(void)
538 if ((fd = open_landlock()) == -1)
541 /* no rules. the logger doesn't need fs access at all. */
543 return landlock_apply(fd);
548 sandbox_server_process(void)
550 struct sock_fprog prog = {
551 .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
556 sandbox_seccomp_catch_sigsys();
559 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
560 fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
561 __func__, strerror(errno));
564 if (server_landlock() == -1)
565 fatal("%s: server_landlock: %s",
566 __func__, strerror(errno));
569 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1)
570 fatal("%s: prctl(PR_SET_SECCOMP): %s\n",
571 __func__, strerror(errno));
575 sandbox_executor_process(void)
578 * We cannot use seccomp for the executor process because we
579 * don't know what the child will do. Also, our filter will
580 * be inherited so the child cannot set its own seccomp
587 sandbox_logger_process(void)
590 * Here we could use a seccomp filter to allow only recvfd,
591 * write/writev and memory allocations, but syslog is a beast
592 * and I don't know what syscalls it could end up doing.
593 * Landlock is a simpler beast, use it to disallow any file
597 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
598 fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
599 __func__, strerror(errno));
602 if (logger_landlock() == -1)
603 fatal("%s: logger_landlock: %s",
604 __func__, strerror(errno));
610 #elif defined(__OpenBSD__)
615 sandbox_server_process(void)
620 TAILQ_FOREACH(h, &hosts, vhosts) {
621 TAILQ_FOREACH(l, &h->locations, locations) {
625 if (unveil(l->dir, "r") == -1)
626 fatal("unveil %s for domain %s",
632 if (pledge("stdio recvfd rpath inet", NULL) == -1)
637 sandbox_executor_process(void)
644 TAILQ_FOREACH(h, &hosts, vhosts) {
645 TAILQ_FOREACH(l, &h->locations, locations) {
649 /* r so we can chdir into the directory */
650 if (unveil(l->dir, "rx") == -1)
651 fatal("unveil %s for domain %s",
656 for (i = 0; i < FCGI_MAX; i++) {
658 if (f->path != NULL) {
659 if (unveil(f->path, "rw") == -1)
660 fatal("unveil %s", f->path);
663 if (f->prog != NULL) {
664 if (unveil(f->prog, "rx") == -1)
665 fatal("unveil %s", f->prog);
670 * rpath: to chdir into the correct directory
672 * dns inet unix: FastCGI
674 if (pledge("stdio rpath sendfd proc exec dns inet unix", NULL))
679 sandbox_logger_process(void)
681 if (pledge("stdio recvfd", NULL) == -1)
687 #warning "No sandbox method known for this OS"
690 sandbox_server_process(void)
696 sandbox_executor_process(void)
698 log_notice(NULL, "no sandbox method known for this OS");
702 sandbox_logger_process(void)