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/prctl.h>
73 #include <sys/syscall.h>
74 #include <sys/syscall.h>
75 #include <sys/types.h>
77 #include <linux/audit.h>
78 #include <linux/filter.h>
79 #include <linux/seccomp.h>
87 /* uncomment to enable debugging. ONLY FOR DEVELOPMENT */
88 /* #define SC_DEBUG */
91 # define SC_FAIL SECCOMP_RET_TRAP
93 # define SC_FAIL SECCOMP_RET_KILL
96 #if (BYTE_ORDER == LITTLE_ENDIAN)
98 # define SC_ARG_HI sizeof(uint32_t)
99 #elif (BYTE_ORDER == BIG_ENDIAN)
100 # define SC_ARG_LO sizeof(uint32_t)
103 # error "Uknown endian"
106 /* make the filter more readable */
107 #define SC_ALLOW(nr) \
108 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_##nr, 0, 1), \
109 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
112 * SC_ALLOW_ARG and the SECCOMP_AUDIT_ARCH below are courtesy of
113 * https://roy.marples.name/git/dhcpcd/blob/HEAD:/src/privsep-linux.c
115 #define SC_ALLOW_ARG(_nr, _arg, _val) \
116 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 6), \
117 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
118 offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_LO), \
119 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \
120 ((_val) & 0xffffffff), 0, 3), \
121 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
122 offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_HI), \
123 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \
124 (((uint32_t)((uint64_t)(_val) >> 32)) & 0xffffffff), 0, 1), \
125 BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), \
126 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
127 offsetof(struct seccomp_data, nr))
130 * I personally find this quite nutty. Why can a system header not
131 * define a default for this?
133 #if defined(__i386__)
134 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
135 #elif defined(__x86_64__)
136 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
137 #elif defined(__arc__)
139 # if (BYTE_ORDER == LITTLE_ENDIAN)
140 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACT
142 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACTBE
144 # elif defined(__HS__)
145 # if (BYTE_ORDER == LITTLE_ENDIAN)
146 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2
148 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2BE
151 # error "Platform does not support seccomp filter yet"
153 #elif defined(__arm__)
157 # if (BYTE_ORDER == LITTLE_ENDIAN)
158 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
160 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARMEB
162 #elif defined(__aarch64__)
163 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
164 #elif defined(__alpha__)
165 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ALPHA
166 #elif defined(__hppa__)
167 # if defined(__LP64__)
168 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC64
170 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC
172 #elif defined(__ia64__)
173 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_IA64
174 #elif defined(__microblaze__)
175 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MICROBLAZE
176 #elif defined(__m68k__)
177 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_M68K
178 #elif defined(__mips__)
179 # if defined(__MIPSEL__)
180 # if defined(__LP64__)
181 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL64
183 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL
185 # elif defined(__LP64__)
186 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS64
188 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS
190 #elif defined(__nds32__)
191 # if (BYTE_ORDER == LITTLE_ENDIAN)
192 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32
194 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32BE
196 #elif defined(__nios2__)
197 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NIOS2
198 #elif defined(__or1k__)
199 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_OPENRISC
200 #elif defined(__powerpc64__)
201 # if (BYTE_ORDER == LITTLE_ENDIAN)
202 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64LE
204 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64
206 #elif defined(__powerpc__)
207 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC
208 #elif defined(__riscv)
209 # if defined(__LP64__)
210 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64
212 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV32
214 #elif defined(__s390x__)
215 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390X
216 #elif defined(__s390__)
217 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390
218 #elif defined(__sh__)
219 # if defined(__LP64__)
220 # if (BYTE_ORDER == LITTLE_ENDIAN)
221 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL64
223 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH64
226 # if (BYTE_ORDER == LITTLE_ENDIAN)
227 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL
229 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH
232 #elif defined(__sparc__)
233 # if defined(__arch64__)
234 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC64
236 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC
238 #elif defined(__xtensa__)
239 # define SECCOMP_AUDIT_ARCH AUDIT_ARCH_XTENSA
241 # error "Platform does not support seccomp filter yet"
244 static struct sock_filter filter[] = {
245 /* load the *current* architecture */
246 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
247 (offsetof(struct seccomp_data, arch))),
248 /* ensure it's the same that we've been compiled on */
249 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K,
250 SECCOMP_AUDIT_ARCH, 1, 0),
251 /* if not, kill the program */
252 BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
254 /* load the syscall number */
255 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
256 (offsetof(struct seccomp_data, nr))),
267 #ifdef __NR_clock_gettime
268 SC_ALLOW(clock_gettime),
270 #if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
271 SECCOMP_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT),
273 #ifdef __NR_clock_gettime64
274 SC_ALLOW(clock_gettime64),
279 #ifdef __NR_epoll_ctl
282 #ifdef __NR_epoll_pwait
283 SC_ALLOW(epoll_pwait),
285 #ifdef __NR_epoll_wait
286 SC_ALLOW(epoll_wait),
291 #ifdef __NR_exit_group
292 SC_ALLOW(exit_group),
306 #ifdef __NR_getdents64
307 SC_ALLOW(getdents64),
312 #ifdef __NR_getrandom
315 #ifdef __NR_gettimeofday
316 SC_ALLOW(gettimeofday),
319 /* allow ioctl only on fd 1, glibc doing stuff? */
320 SC_ALLOW_ARG(__NR_ioctl, 0, 1),
337 #ifdef __NR_newfstatat
338 SC_ALLOW(newfstatat),
346 #ifdef __NR_prlimit64
358 #ifdef __NR_rt_sigaction
359 SC_ALLOW(rt_sigaction),
361 #ifdef __NR_rt_sigreturn
362 SC_ALLOW(rt_sigreturn),
377 /* disallow everything else */
378 BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
387 sandbox_seccomp_violation(int signum, siginfo_t *info, void *ctx)
392 fprintf(stderr, "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)\n",
393 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
398 sandbox_seccomp_catch_sigsys(void)
400 struct sigaction act;
403 memset(&act, 0, sizeof(act));
405 sigaddset(&mask, SIGSYS);
407 act.sa_sigaction = &sandbox_seccomp_violation;
408 act.sa_flags = SA_SIGINFO;
409 if (sigaction(SIGSYS, &act, NULL) == -1)
410 fatal("%s: sigaction(SIGSYS): %s",
411 __func__, strerror(errno));
413 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
414 fatal("%s: sigprocmask(SIGSYS): %s\n",
415 __func__, strerror(errno));
417 #endif /* SC_DEBUG */
420 sandbox_server_process(void)
422 struct sock_fprog prog = {
423 .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
428 sandbox_seccomp_catch_sigsys();
431 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
432 fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
433 __func__, strerror(errno));
435 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1)
436 fatal("%s: prctl(PR_SET_SECCOMP): %s\n",
437 __func__, strerror(errno));
441 sandbox_executor_process(void)
444 * We cannot use seccomp for the executor process because we
445 * don't know what the child will do. Also, our filter will
446 * be inherited so the child cannot set its own seccomp
453 sandbox_logger_process(void)
456 * To be honest, here we could use a seccomp policy to only
457 * allow writev(2) and memory allocations.
462 #elif defined(__OpenBSD__)
467 sandbox_server_process(void)
472 TAILQ_FOREACH(h, &hosts, vhosts) {
473 TAILQ_FOREACH(l, &h->locations, locations) {
477 if (unveil(l->dir, "r") == -1)
478 fatal("unveil %s for domain %s",
484 if (pledge("stdio recvfd rpath inet", NULL) == -1)
489 sandbox_executor_process(void)
496 TAILQ_FOREACH(h, &hosts, vhosts) {
497 TAILQ_FOREACH(l, &h->locations, locations) {
501 /* r so we can chdir into the directory */
502 if (unveil(l->dir, "rx") == -1)
503 fatal("unveil %s for domain %s",
508 for (i = 0; i < FCGI_MAX; i++) {
510 if (f->path != NULL) {
511 if (unveil(f->path, "rw") == -1)
512 fatal("unveil %s", f->path);
515 if (f->prog != NULL) {
516 if (unveil(f->prog, "rx") == -1)
517 fatal("unveil %s", f->prog);
522 * rpath: to chdir into the correct directory
524 * dns inet unix: FastCGI
526 if (pledge("stdio rpath sendfd proc exec dns inet unix", NULL))
531 sandbox_logger_process(void)
533 if (pledge("stdio recvfd", NULL) == -1)
539 #warning "No sandbox method known for this OS"
542 sandbox_server_process(void)
548 sandbox_executor_process(void)
550 log_notice(NULL, "no sandbox method known for this OS");
554 sandbox_logger_process(void)