2021-01-23 12:28:44 +01:00
|
|
|
/*
|
2021-01-23 12:29:02 +01:00
|
|
|
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
|
2021-01-23 12:28:44 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
#include "gmid.h"
|
|
|
|
|
2021-07-02 11:26:59 +02:00
|
|
|
#if DISABLE_SANDBOX
|
|
|
|
|
|
|
|
#warning "Sandbox disabled! Please report issues upstream instead of disabling the sandbox."
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_server_process(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_executor_process(void)
|
|
|
|
{
|
2021-10-18 12:05:55 +02:00
|
|
|
log_notice(NULL, "Sandbox disabled! "
|
2021-07-02 11:26:59 +02:00
|
|
|
"Please report issues upstream instead of disabling the sandbox.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_logger_process(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__FreeBSD__)
|
2021-01-15 15:03:45 +01:00
|
|
|
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
|
|
|
|
void
|
2021-03-20 09:42:08 +01:00
|
|
|
sandbox_server_process(void)
|
|
|
|
{
|
|
|
|
if (cap_enter() == -1)
|
|
|
|
fatal("cap_enter");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_executor_process(void)
|
|
|
|
{
|
2021-07-07 11:46:37 +02:00
|
|
|
/*
|
|
|
|
* We cannot capsicum the executor process because it needs to
|
|
|
|
* fork(2)+execve(2) cgi scripts
|
|
|
|
*/
|
2021-03-20 09:42:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_logger_process(void)
|
2021-01-15 15:03:45 +01:00
|
|
|
{
|
|
|
|
if (cap_enter() == -1)
|
2021-02-11 10:07:28 +01:00
|
|
|
fatal("cap_enter");
|
2021-01-15 15:03:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
2021-09-26 22:01:32 +02:00
|
|
|
#include <sys/ioctl.h>
|
2021-01-17 10:34:27 +01:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <linux/audit.h>
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/seccomp.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2021-01-20 17:09:04 +01:00
|
|
|
#include <fcntl.h>
|
2021-01-17 10:34:27 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-09-19 19:08:12 +02:00
|
|
|
#if HAVE_LANDLOCK
|
|
|
|
# include "landlock_shim.h"
|
|
|
|
#endif
|
|
|
|
|
2021-07-02 11:11:40 +02:00
|
|
|
/* uncomment to enable debugging. ONLY FOR DEVELOPMENT */
|
|
|
|
/* #define SC_DEBUG */
|
|
|
|
|
|
|
|
#ifdef SC_DEBUG
|
|
|
|
# define SC_FAIL SECCOMP_RET_TRAP
|
|
|
|
#else
|
|
|
|
# define SC_FAIL SECCOMP_RET_KILL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SC_ARG_LO 0
|
|
|
|
# define SC_ARG_HI sizeof(uint32_t)
|
|
|
|
#elif (BYTE_ORDER == BIG_ENDIAN)
|
|
|
|
# define SC_ARG_LO sizeof(uint32_t)
|
|
|
|
# define SC_ARG_HI 0
|
|
|
|
#else
|
|
|
|
# error "Uknown endian"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* make the filter more readable */
|
|
|
|
#define SC_ALLOW(nr) \
|
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_##nr, 0, 1), \
|
|
|
|
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SC_ALLOW_ARG and the SECCOMP_AUDIT_ARCH below are courtesy of
|
|
|
|
* https://roy.marples.name/git/dhcpcd/blob/HEAD:/src/privsep-linux.c
|
|
|
|
*/
|
|
|
|
#define SC_ALLOW_ARG(_nr, _arg, _val) \
|
|
|
|
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 6), \
|
|
|
|
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_LO), \
|
|
|
|
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \
|
|
|
|
((_val) & 0xffffffff), 0, 3), \
|
|
|
|
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_HI), \
|
|
|
|
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \
|
|
|
|
(((uint32_t)((uint64_t)(_val) >> 32)) & 0xffffffff), 0, 1), \
|
|
|
|
BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), \
|
|
|
|
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, nr))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I personally find this quite nutty. Why can a system header not
|
|
|
|
* define a default for this?
|
|
|
|
*/
|
2021-01-17 10:34:27 +01:00
|
|
|
#if defined(__i386__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
|
2021-07-02 11:11:40 +02:00
|
|
|
#elif defined(__arc__)
|
|
|
|
# if defined(__A7__)
|
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACT
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACTBE
|
|
|
|
# endif
|
|
|
|
# elif defined(__HS__)
|
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2BE
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# error "Platform does not support seccomp filter yet"
|
|
|
|
# endif
|
2021-01-17 10:34:27 +01:00
|
|
|
#elif defined(__arm__)
|
2021-07-02 11:11:40 +02:00
|
|
|
# ifndef EM_ARM
|
|
|
|
# define EM_ARM 40
|
|
|
|
# endif
|
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARMEB
|
|
|
|
# endif
|
2021-01-17 10:34:27 +01:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
|
2021-07-02 11:11:40 +02:00
|
|
|
#elif defined(__alpha__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ALPHA
|
|
|
|
#elif defined(__hppa__)
|
|
|
|
# if defined(__LP64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC64
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC
|
|
|
|
# endif
|
|
|
|
#elif defined(__ia64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_IA64
|
|
|
|
#elif defined(__microblaze__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MICROBLAZE
|
|
|
|
#elif defined(__m68k__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_M68K
|
2021-01-17 10:34:27 +01:00
|
|
|
#elif defined(__mips__)
|
2021-07-02 11:11:40 +02:00
|
|
|
# if defined(__MIPSEL__)
|
|
|
|
# if defined(__LP64__)
|
2021-01-17 10:34:27 +01:00
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL64
|
2021-07-02 11:11:40 +02:00
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL
|
2021-01-17 10:34:27 +01:00
|
|
|
# endif
|
2021-07-02 11:11:40 +02:00
|
|
|
# elif defined(__LP64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS64
|
2021-01-17 10:34:27 +01:00
|
|
|
# else
|
2021-07-02 11:11:40 +02:00
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS
|
|
|
|
# endif
|
|
|
|
#elif defined(__nds32__)
|
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32
|
|
|
|
#else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32BE
|
|
|
|
#endif
|
|
|
|
#elif defined(__nios2__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NIOS2
|
|
|
|
#elif defined(__or1k__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_OPENRISC
|
|
|
|
#elif defined(__powerpc64__)
|
2021-07-03 19:38:50 +02:00
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64LE
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64
|
|
|
|
# endif
|
2021-07-02 11:11:40 +02:00
|
|
|
#elif defined(__powerpc__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC
|
|
|
|
#elif defined(__riscv)
|
|
|
|
# if defined(__LP64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV32
|
|
|
|
# endif
|
|
|
|
#elif defined(__s390x__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390X
|
|
|
|
#elif defined(__s390__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390
|
|
|
|
#elif defined(__sh__)
|
|
|
|
# if defined(__LP64__)
|
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL64
|
2021-01-17 10:34:27 +01:00
|
|
|
# else
|
2021-07-02 11:11:40 +02:00
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH64
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# if (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH
|
2021-01-17 10:34:27 +01:00
|
|
|
# endif
|
|
|
|
# endif
|
2021-07-02 11:11:40 +02:00
|
|
|
#elif defined(__sparc__)
|
|
|
|
# if defined(__arch64__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC64
|
|
|
|
# else
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC
|
|
|
|
# endif
|
|
|
|
#elif defined(__xtensa__)
|
|
|
|
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_XTENSA
|
2021-01-17 10:34:27 +01:00
|
|
|
#else
|
|
|
|
# error "Platform does not support seccomp filter yet"
|
|
|
|
#endif
|
|
|
|
|
2022-03-19 12:02:42 +01:00
|
|
|
static const struct sock_filter filter[] = {
|
2021-07-02 11:11:40 +02:00
|
|
|
/* load the *current* architecture */
|
|
|
|
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
|
|
|
|
(offsetof(struct seccomp_data, arch))),
|
|
|
|
/* ensure it's the same that we've been compiled on */
|
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K,
|
|
|
|
SECCOMP_AUDIT_ARCH, 1, 0),
|
|
|
|
/* if not, kill the program */
|
|
|
|
BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
|
|
|
|
|
|
|
|
/* load the syscall number */
|
|
|
|
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
|
|
|
|
(offsetof(struct seccomp_data, nr))),
|
|
|
|
|
|
|
|
#ifdef __NR_accept
|
|
|
|
SC_ALLOW(accept),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_accept4
|
|
|
|
SC_ALLOW(accept4),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_brk
|
|
|
|
SC_ALLOW(brk),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_clock_gettime
|
|
|
|
SC_ALLOW(clock_gettime),
|
|
|
|
#endif
|
|
|
|
#if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
|
|
|
|
SECCOMP_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_clock_gettime64
|
|
|
|
SC_ALLOW(clock_gettime64),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_close
|
|
|
|
SC_ALLOW(close),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_ctl
|
|
|
|
SC_ALLOW(epoll_ctl),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_pwait
|
|
|
|
SC_ALLOW(epoll_pwait),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_wait
|
|
|
|
SC_ALLOW(epoll_wait),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_exit
|
|
|
|
SC_ALLOW(exit),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_exit_group
|
|
|
|
SC_ALLOW(exit_group),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fcntl
|
|
|
|
SC_ALLOW(fcntl),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fcntl64
|
|
|
|
SC_ALLOW(fcntl64),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstat
|
|
|
|
SC_ALLOW(fstat),
|
|
|
|
#endif
|
2021-07-23 08:50:30 +02:00
|
|
|
#ifdef __NR_fstat64
|
|
|
|
SC_ALLOW(fstat64),
|
|
|
|
#endif
|
2022-02-13 16:32:10 +01:00
|
|
|
#ifdef __NR_fstatat64
|
|
|
|
SC_ALLOW(fstatat64),
|
|
|
|
#endif
|
2021-07-02 11:11:40 +02:00
|
|
|
#ifdef __NR_getdents64
|
|
|
|
SC_ALLOW(getdents64),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_getpid
|
|
|
|
SC_ALLOW(getpid),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_getrandom
|
|
|
|
SC_ALLOW(getrandom),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_gettimeofday
|
|
|
|
SC_ALLOW(gettimeofday),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_ioctl
|
2021-09-26 22:00:38 +02:00
|
|
|
/* allow ioctl on fd 1, glibc doing stuff? */
|
2021-10-18 12:05:55 +02:00
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 0, 1),
|
2021-09-26 22:00:38 +02:00
|
|
|
/* allow FIONREAD needed by libevent */
|
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 1, FIONREAD),
|
2021-07-02 11:11:40 +02:00
|
|
|
#endif
|
2022-02-13 16:32:10 +01:00
|
|
|
#ifdef __NR__llseek
|
|
|
|
SC_ALLOW(_llseek),
|
|
|
|
#endif
|
2021-07-02 11:11:40 +02:00
|
|
|
#ifdef __NR_lseek
|
|
|
|
SC_ALLOW(lseek),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_madvise
|
|
|
|
SC_ALLOW(madvise),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_mmap
|
|
|
|
SC_ALLOW(mmap),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_mmap2
|
|
|
|
SC_ALLOW(mmap2),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_munmap
|
|
|
|
SC_ALLOW(munmap),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_newfstatat
|
|
|
|
SC_ALLOW(newfstatat),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_oldfstat
|
|
|
|
SC_ALLOW(oldfstat),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_openat
|
2022-02-13 17:20:27 +01:00
|
|
|
SC_ALLOW_ARG(__NR_openat, 3, O_RDONLY),
|
2021-07-02 11:11:40 +02:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_prlimit64
|
|
|
|
SC_ALLOW(prlimit64),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_read
|
|
|
|
SC_ALLOW(read),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_recvmsg
|
|
|
|
SC_ALLOW(recvmsg),
|
|
|
|
#endif
|
2021-07-23 08:55:32 +02:00
|
|
|
#ifdef __NR_readv
|
|
|
|
SC_ALLOW(readv),
|
2021-07-02 11:11:40 +02:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_rt_sigaction
|
|
|
|
SC_ALLOW(rt_sigaction),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_rt_sigreturn
|
|
|
|
SC_ALLOW(rt_sigreturn),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_sendmsg
|
|
|
|
SC_ALLOW(sendmsg),
|
|
|
|
#endif
|
2022-02-13 16:32:10 +01:00
|
|
|
#ifdef __NR_sigreturn
|
|
|
|
SC_ALLOW(sigreturn),
|
|
|
|
#endif
|
2021-07-02 11:11:40 +02:00
|
|
|
#ifdef __NR_statx
|
|
|
|
SC_ALLOW(statx),
|
|
|
|
#endif
|
2021-12-11 10:08:50 +01:00
|
|
|
#ifdef __NR_ugetrlimit
|
|
|
|
SC_ALLOW(ugetrlimit),
|
|
|
|
#endif
|
2021-07-02 11:11:40 +02:00
|
|
|
#ifdef __NR_write
|
|
|
|
SC_ALLOW(write),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_writev
|
|
|
|
SC_ALLOW(writev),
|
2021-01-17 10:34:27 +01:00
|
|
|
#endif
|
|
|
|
|
2021-07-03 20:30:07 +02:00
|
|
|
/* disallow everything else */
|
2021-07-02 11:11:40 +02:00
|
|
|
BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
|
|
|
|
};
|
2021-01-17 10:34:27 +01:00
|
|
|
|
|
|
|
#ifdef SC_DEBUG
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
sandbox_seccomp_violation(int signum, siginfo_t *info, void *ctx)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)\n",
|
|
|
|
__func__, info->si_arch, info->si_syscall, info->si_call_addr);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sandbox_seccomp_catch_sigsys(void)
|
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
sigset_t mask;
|
|
|
|
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGSYS);
|
|
|
|
|
|
|
|
act.sa_sigaction = &sandbox_seccomp_violation;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
2021-02-11 10:07:28 +01:00
|
|
|
if (sigaction(SIGSYS, &act, NULL) == -1)
|
|
|
|
fatal("%s: sigaction(SIGSYS): %s",
|
2021-01-17 10:34:27 +01:00
|
|
|
__func__, strerror(errno));
|
2021-02-11 10:07:28 +01:00
|
|
|
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
|
|
|
|
fatal("%s: sigprocmask(SIGSYS): %s\n",
|
2021-01-17 10:34:27 +01:00
|
|
|
__func__, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif /* SC_DEBUG */
|
|
|
|
|
2021-09-19 19:08:12 +02:00
|
|
|
#if HAVE_LANDLOCK
|
2021-09-19 19:08:12 +02:00
|
|
|
static inline int
|
2021-09-25 10:47:29 +02:00
|
|
|
open_landlock(void)
|
2021-09-19 19:08:12 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2022-03-19 12:02:42 +01:00
|
|
|
const struct landlock_ruleset_attr attr = {
|
2022-02-10 23:29:51 +01:00
|
|
|
.handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE |
|
|
|
|
LANDLOCK_ACCESS_FS_READ_FILE |
|
|
|
|
LANDLOCK_ACCESS_FS_READ_DIR |
|
|
|
|
LANDLOCK_ACCESS_FS_WRITE_FILE |
|
|
|
|
LANDLOCK_ACCESS_FS_REMOVE_DIR |
|
|
|
|
LANDLOCK_ACCESS_FS_REMOVE_FILE |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_CHAR |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_DIR |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_REG |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_SOCK |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_FIFO |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_BLOCK |
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_SYM,
|
2021-09-25 10:47:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
|
2021-09-19 19:08:12 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOSYS:
|
|
|
|
fatal("%s: failed to create ruleset. "
|
|
|
|
"Landlock doesn't seem to be supported by the "
|
|
|
|
"current kernel.", __func__);
|
|
|
|
case EOPNOTSUPP:
|
|
|
|
log_warn(NULL, "%s: failed to create ruleset. "
|
|
|
|
"Landlock seems to be currently disabled; "
|
|
|
|
"continuing without it.", __func__);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("%s: failed to create ruleset: %s",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
static int
|
|
|
|
landlock_unveil_path(int landlock_fd, const char *path, int perms)
|
|
|
|
{
|
|
|
|
struct landlock_path_beneath_attr pb;
|
|
|
|
int err, saved_errno;
|
|
|
|
|
|
|
|
pb.allowed_access = perms;
|
|
|
|
|
|
|
|
if ((pb.parent_fd = open(path, O_PATH)) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = landlock_add_rule(landlock_fd, LANDLOCK_RULE_PATH_BENEATH,
|
|
|
|
&pb, 0);
|
|
|
|
saved_errno = errno;
|
|
|
|
close(pb.parent_fd);
|
|
|
|
errno = saved_errno;
|
|
|
|
return err ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
landlock_apply(int fd)
|
|
|
|
{
|
|
|
|
int r, saved_errno;
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = landlock_restrict_self(fd, 0);
|
|
|
|
saved_errno = errno;
|
|
|
|
close(fd);
|
|
|
|
errno = saved_errno;
|
|
|
|
return r ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2021-09-19 19:08:12 +02:00
|
|
|
static int
|
|
|
|
server_landlock(void)
|
|
|
|
{
|
2021-09-25 10:47:29 +02:00
|
|
|
int fd, perms;
|
2021-09-19 19:08:12 +02:00
|
|
|
struct vhost *h;
|
|
|
|
struct location *l;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are all the actions allowed for the root directories
|
2021-09-25 10:47:29 +02:00
|
|
|
* of the vhosts.
|
2021-09-19 19:08:12 +02:00
|
|
|
*/
|
2021-09-25 10:47:29 +02:00
|
|
|
perms = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR;
|
2021-09-19 19:08:12 +02:00
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
if ((fd = open_landlock()) == -1)
|
|
|
|
return 0;
|
2021-09-19 19:08:12 +02:00
|
|
|
|
|
|
|
TAILQ_FOREACH(h, &hosts, vhosts) {
|
|
|
|
TAILQ_FOREACH(l, &h->locations, locations) {
|
|
|
|
if (l->dir == NULL)
|
|
|
|
continue;
|
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
if (landlock_unveil_path(fd, l->dir, perms) == -1)
|
|
|
|
fatal("%s: landlock_unveil_path(%s): %s",
|
2021-09-19 19:08:12 +02:00
|
|
|
__func__, l->dir, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
return landlock_apply(fd);
|
2021-09-19 19:08:12 +02:00
|
|
|
}
|
2021-09-19 19:08:12 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
logger_landlock(void)
|
|
|
|
{
|
2021-09-25 10:47:29 +02:00
|
|
|
int fd;
|
2021-09-19 19:08:12 +02:00
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
if ((fd = open_landlock()) == -1)
|
|
|
|
return 0;
|
2021-09-19 19:08:12 +02:00
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
/* no rules. the logger doesn't need fs access at all. */
|
2021-09-19 19:08:12 +02:00
|
|
|
|
2021-09-25 10:47:29 +02:00
|
|
|
return landlock_apply(fd);
|
2021-09-19 19:08:12 +02:00
|
|
|
}
|
2021-09-19 19:08:12 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
void
|
2021-03-20 09:42:08 +01:00
|
|
|
sandbox_server_process(void)
|
2021-01-15 15:03:45 +01:00
|
|
|
{
|
2022-03-19 12:02:42 +01:00
|
|
|
const struct sock_fprog prog = {
|
2021-01-17 10:34:27 +01:00
|
|
|
.len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
|
|
|
|
.filter = filter,
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef SC_DEBUG
|
|
|
|
sandbox_seccomp_catch_sigsys();
|
|
|
|
#endif
|
|
|
|
|
2021-02-11 10:07:28 +01:00
|
|
|
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
|
|
|
|
fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
|
2021-01-17 10:34:27 +01:00
|
|
|
__func__, strerror(errno));
|
|
|
|
|
2021-09-19 19:08:12 +02:00
|
|
|
#if HAVE_LANDLOCK
|
2021-09-25 10:47:29 +02:00
|
|
|
if (server_landlock() == -1)
|
|
|
|
fatal("%s: server_landlock: %s",
|
|
|
|
__func__, strerror(errno));
|
2021-09-19 19:08:12 +02:00
|
|
|
#endif
|
|
|
|
|
2021-02-11 10:07:28 +01:00
|
|
|
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1)
|
|
|
|
fatal("%s: prctl(PR_SET_SECCOMP): %s\n",
|
2021-01-17 10:34:27 +01:00
|
|
|
__func__, strerror(errno));
|
2021-01-15 15:03:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 09:42:08 +01:00
|
|
|
void
|
|
|
|
sandbox_executor_process(void)
|
|
|
|
{
|
2021-07-07 11:46:37 +02:00
|
|
|
/*
|
|
|
|
* We cannot use seccomp for the executor process because we
|
2021-03-20 09:42:08 +01:00
|
|
|
* don't know what the child will do. Also, our filter will
|
|
|
|
* be inherited so the child cannot set its own seccomp
|
2021-07-07 11:46:37 +02:00
|
|
|
* policy.
|
|
|
|
*/
|
2021-03-20 09:42:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_logger_process(void)
|
|
|
|
{
|
2021-07-07 11:46:37 +02:00
|
|
|
/*
|
2021-09-19 19:08:12 +02:00
|
|
|
* Here we could use a seccomp filter to allow only recvfd,
|
|
|
|
* write/writev and memory allocations, but syslog is a beast
|
|
|
|
* and I don't know what syscalls it could end up doing.
|
|
|
|
* Landlock is a simpler beast, use it to disallow any file
|
|
|
|
* sytsem access.
|
2021-07-07 11:46:37 +02:00
|
|
|
*/
|
2021-09-19 19:08:12 +02:00
|
|
|
|
2021-10-02 19:20:10 +02:00
|
|
|
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
|
|
|
|
fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
|
2021-09-19 19:08:12 +02:00
|
|
|
#if HAVE_LANDLOCK
|
2021-09-25 10:47:29 +02:00
|
|
|
if (logger_landlock() == -1)
|
|
|
|
fatal("%s: logger_landlock: %s",
|
2021-09-19 19:08:12 +02:00
|
|
|
__func__, strerror(errno));
|
|
|
|
#endif
|
|
|
|
|
2021-03-20 09:42:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
void
|
2021-03-20 09:42:08 +01:00
|
|
|
sandbox_server_process(void)
|
2021-01-15 15:03:45 +01:00
|
|
|
{
|
2021-04-30 19:16:34 +02:00
|
|
|
struct vhost *h;
|
|
|
|
struct location *l;
|
2021-01-15 15:03:45 +01:00
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_FOREACH(h, &hosts, vhosts) {
|
2021-04-30 19:16:34 +02:00
|
|
|
TAILQ_FOREACH(l, &h->locations, locations) {
|
|
|
|
if (l->dir == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (unveil(l->dir, "r") == -1)
|
|
|
|
fatal("unveil %s for domain %s",
|
|
|
|
l->dir,
|
|
|
|
h->domain);
|
|
|
|
}
|
2021-01-15 15:03:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
if (pledge("stdio recvfd rpath inet", NULL) == -1)
|
2021-02-11 10:07:28 +01:00
|
|
|
fatal("pledge");
|
2021-01-15 15:03:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 09:42:08 +01:00
|
|
|
void
|
|
|
|
sandbox_executor_process(void)
|
|
|
|
{
|
2021-03-31 18:32:18 +02:00
|
|
|
struct vhost *h;
|
2021-04-30 19:16:34 +02:00
|
|
|
struct location *l;
|
2021-05-09 20:23:36 +02:00
|
|
|
struct fcgi *f;
|
|
|
|
size_t i;
|
2021-03-20 09:42:08 +01:00
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_FOREACH(h, &hosts, vhosts) {
|
2021-04-30 19:16:34 +02:00
|
|
|
TAILQ_FOREACH(l, &h->locations, locations) {
|
|
|
|
if (l->dir == NULL)
|
|
|
|
continue;
|
|
|
|
|
2021-07-09 10:12:05 +02:00
|
|
|
/* r so we can chdir into the directory */
|
2021-04-30 19:16:34 +02:00
|
|
|
if (unveil(l->dir, "rx") == -1)
|
|
|
|
fatal("unveil %s for domain %s",
|
|
|
|
l->dir, h->domain);
|
|
|
|
}
|
2021-03-20 09:42:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:23:36 +02:00
|
|
|
for (i = 0; i < FCGI_MAX; i++) {
|
2021-10-18 12:05:55 +02:00
|
|
|
f = &fcgi[i];
|
2021-05-09 20:23:36 +02:00
|
|
|
if (f->path != NULL) {
|
|
|
|
if (unveil(f->path, "rw") == -1)
|
|
|
|
fatal("unveil %s", f->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f->prog != NULL) {
|
|
|
|
if (unveil(f->prog, "rx") == -1)
|
|
|
|
fatal("unveil %s", f->prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rpath: to chdir into the correct directory
|
|
|
|
* proc exec: CGI
|
|
|
|
* dns inet unix: FastCGI
|
|
|
|
*/
|
|
|
|
if (pledge("stdio rpath sendfd proc exec dns inet unix", NULL))
|
2021-03-20 09:42:08 +01:00
|
|
|
err(1, "pledge");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_logger_process(void)
|
|
|
|
{
|
2021-06-15 10:06:10 +02:00
|
|
|
if (pledge("stdio recvfd", NULL) == -1)
|
2021-03-20 09:42:08 +01:00
|
|
|
err(1, "pledge");
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
#else
|
|
|
|
|
2021-03-20 09:42:08 +01:00
|
|
|
#warning "No sandbox method known for this OS"
|
|
|
|
|
|
|
|
void
|
|
|
|
sandbox_server_process(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
void
|
2021-03-20 09:42:08 +01:00
|
|
|
sandbox_executor_process(void)
|
2021-01-15 15:03:45 +01:00
|
|
|
{
|
2021-02-11 10:04:47 +01:00
|
|
|
log_notice(NULL, "no sandbox method known for this OS");
|
2021-01-15 15:03:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 09:42:08 +01:00
|
|
|
void
|
|
|
|
sandbox_logger_process(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
#endif
|