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),
303 #ifdef __NR_getdents64
304 SC_ALLOW(getdents64),
309 #ifdef __NR_getrandom
312 #ifdef __NR_gettimeofday
313 SC_ALLOW(gettimeofday),
316 /* allow ioctl only on fd 1, glibc doing stuff? */
317 SC_ALLOW_ARG(__NR_ioctl, 0, 1),
334 #ifdef __NR_newfstatat
335 SC_ALLOW(newfstatat),
343 #ifdef __NR_prlimit64
355 #ifdef __NR_rt_sigaction
356 SC_ALLOW(rt_sigaction),
358 #ifdef __NR_rt_sigreturn
359 SC_ALLOW(rt_sigreturn),
374 /* disallow everything else */
375 BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
384 sandbox_seccomp_violation(int signum, siginfo_t *info, void *ctx)
389 fprintf(stderr, "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)\n",
390 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
395 sandbox_seccomp_catch_sigsys(void)
397 struct sigaction act;
400 memset(&act, 0, sizeof(act));
402 sigaddset(&mask, SIGSYS);
404 act.sa_sigaction = &sandbox_seccomp_violation;
405 act.sa_flags = SA_SIGINFO;
406 if (sigaction(SIGSYS, &act, NULL) == -1)
407 fatal("%s: sigaction(SIGSYS): %s",
408 __func__, strerror(errno));
410 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
411 fatal("%s: sigprocmask(SIGSYS): %s\n",
412 __func__, strerror(errno));
414 #endif /* SC_DEBUG */
417 sandbox_server_process(void)
419 struct sock_fprog prog = {
420 .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
425 sandbox_seccomp_catch_sigsys();
428 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
429 fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
430 __func__, strerror(errno));
432 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1)
433 fatal("%s: prctl(PR_SET_SECCOMP): %s\n",
434 __func__, strerror(errno));
438 sandbox_executor_process(void)
441 * We cannot use seccomp for the executor process because we
442 * don't know what the child will do. Also, our filter will
443 * be inherited so the child cannot set its own seccomp
450 sandbox_logger_process(void)
453 * To be honest, here we could use a seccomp policy to only
454 * allow writev(2) and memory allocations.
459 #elif defined(__OpenBSD__)
464 sandbox_server_process(void)
469 TAILQ_FOREACH(h, &hosts, vhosts) {
470 TAILQ_FOREACH(l, &h->locations, locations) {
474 if (unveil(l->dir, "r") == -1)
475 fatal("unveil %s for domain %s",
481 if (pledge("stdio recvfd rpath inet", NULL) == -1)
486 sandbox_executor_process(void)
493 TAILQ_FOREACH(h, &hosts, vhosts) {
494 TAILQ_FOREACH(l, &h->locations, locations) {
498 /* r so we can chdir into the directory */
499 if (unveil(l->dir, "rx") == -1)
500 fatal("unveil %s for domain %s",
505 for (i = 0; i < FCGI_MAX; i++) {
507 if (f->path != NULL) {
508 if (unveil(f->path, "rw") == -1)
509 fatal("unveil %s", f->path);
512 if (f->prog != NULL) {
513 if (unveil(f->prog, "rx") == -1)
514 fatal("unveil %s", f->prog);
519 * rpath: to chdir into the correct directory
521 * dns inet unix: FastCGI
523 if (pledge("stdio rpath sendfd proc exec dns inet unix", NULL))
528 sandbox_logger_process(void)
530 if (pledge("stdio recvfd", NULL) == -1)
536 #warning "No sandbox method known for this OS"
539 sandbox_server_process(void)
545 sandbox_executor_process(void)
547 log_notice(NULL, "no sandbox method known for this OS");
551 sandbox_logger_process(void)