aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-23 17:35:03 +0200
committerbptato <nincsnevem662@gmail.com>2024-10-23 19:08:39 +0200
commit5c24b64fe5b242bdba68fe3d48489bed26b48eae (patch)
treec7408078c44792314cac43cfa23ae5e56996630e /src
parentb0bb61e830e76169f41db186b5946ae6859f4e6a (diff)
downloadchawan-5c24b64fe5b242bdba68fe3d48489bed26b48eae.tar.gz
sandbox: replace libseccomp with chaseccomp
This drops libseccomp as a dependency. Also, move the capsicum/pledge definitions from bindings to sandbox.nim because they are only used there. Interestingly, after integrating chaseccomp I found that the stbi process would mysteriously crash by a getrandom(2) syscall. Closer investigation revealed it is only called on the initialization of glibc's malloc; presumably it had never surfaced before because libseccomp would always allocate before entering the sandbox. So I've added getrandom to our filter as well.
Diffstat (limited to 'src')
-rw-r--r--src/bindings/capsicum.nim6
-rw-r--r--src/bindings/libseccomp.nim52
-rw-r--r--src/bindings/pledge.nim6
-rw-r--r--src/utils/sandbox.nim205
4 files changed, 29 insertions, 240 deletions
diff --git a/src/bindings/capsicum.nim b/src/bindings/capsicum.nim
deleted file mode 100644
index e01c3efb..00000000
--- a/src/bindings/capsicum.nim
+++ /dev/null
@@ -1,6 +0,0 @@
-{.push header: "sys/capsicum.h", importc.}
-
-proc cap_enter*(): cint
-proc cap_getmode*(modep: ptr cuint): cint
-
-{.pop.}
diff --git a/src/bindings/libseccomp.nim b/src/bindings/libseccomp.nim
deleted file mode 100644
index 3f02e4d9..00000000
--- a/src/bindings/libseccomp.nim
+++ /dev/null
@@ -1,52 +0,0 @@
-import std/macros
-
-const seccomp = (proc(): string =
- let res = staticExec("pkg-config --libs --silence-errors libseccomp")
- if res == "":
- error("Couldn't find libseccomp on your computer! Please install " &
- "libseccomp (e.g. apt install libseccomp-dev), or build with " &
- "`make CHA_DANGER_DISABLE_SANDBOX=1'.")
- return res
-)()
-
-type
- scmp_filter_ctx* = distinct pointer
-
- scmp_datum_t* = uint64
-
- scmp_compare* {.size: sizeof(cint).} = enum
- N_SCMP_CMP_MIN = 0
- SCMP_CMP_NE = 1 # not equal
- SCMP_CMP_LT = 2 # less than
- SCMP_CMP_LE = 3 # less than or equal
- SCMP_CMP_EQ = 4 # equal
- SCMP_CMP_GE = 5 # greater than or equal
- SCMP_CMP_GT = 6 # greater than
- SCMP_CMP_MASKED_EQ = 7 # masked equality
-
- scmp_arg_cmp* = object
- arg*: cuint
- op*: scmp_compare
- datum_a*: scmp_datum_t
- datum_b*: scmp_datum_t
-
-{.push importc.}
-{.passl: seccomp.}
-
-const SCMP_ACT_KILL_PROCESS* = 0x80000000u32
-const SCMP_ACT_ALLOW* = 0x7FFF0000u32
-const SCMP_ACT_TRAP* = 0x00030000u32
-
-template SCMP_ACT_ERRNO*(x: uint16): uint32 =
- 0x50000u32 or x
-
-proc seccomp_init*(def_action: uint32): scmp_filter_ctx
-proc seccomp_reset*(ctx: scmp_filter_ctx; def_action: uint32): cint
-proc seccomp_syscall_resolve_name*(name: cstring): cint
-proc seccomp_syscall_resolve_name_rewrite*(name: cstring): cint
-proc seccomp_rule_add*(ctx: scmp_filter_ctx; action: uint32; syscall: cint;
- arg_cnt: cuint): cint {.varargs.}
-proc seccomp_load*(ctx: scmp_filter_ctx): cint
-proc seccomp_release*(ctx: scmp_filter_ctx)
-
-{.pop.}
diff --git a/src/bindings/pledge.nim b/src/bindings/pledge.nim
deleted file mode 100644
index 8d265fe6..00000000
--- a/src/bindings/pledge.nim
+++ /dev/null
@@ -1,6 +0,0 @@
-{.push header: "<unistd.h>", importc.}
-
-proc pledge*(promises, execpromises: cstring): cint
-proc unveil*(path, permissions: cstring): cint
-
-{.pop.}
diff --git a/src/utils/sandbox.nim b/src/utils/sandbox.nim
index 493ee718..67c0315e 100644
--- a/src/utils/sandbox.nim
+++ b/src/utils/sandbox.nim
@@ -5,6 +5,12 @@
# even execute untrusted code (JS, with an engine written in C). So the
# main goal is to give buffers as few permissions as possible.
#
+# Aside from sandboxing in buffer processes, we also have a more
+# restrictive "network" sandbox that is intended for CGI processes that
+# just read/write from/to the network and stdin/stdout. At the moment this
+# is used in the HTTP process and all image manipulation processes (codecs,
+# resize).
+#
# On FreeBSD, we create a file descriptor to the directory sockets
# reside in, and then use that for manipulating our sockets.
#
@@ -14,22 +20,14 @@
#
# On OpenBSD, we pledge the minimum amount of promises we need, and
# do not unveil anything. It seems to be roughly equivalent to the
-# security we get with FreeBSD Capsicum.
+# security we get with FreeBSD Capsicum, except connect(3) can connect
+# to any UNIX domain socket on the file system.
#
-# On Linux, we use libseccomp so that I don't have to manually write
-# BPF filters.
-# Sandboxing on Linux is at the moment slightly less safe than on the
-# two BSDs, because a rogue buffer could in theory connect to whatever
-# open UNIX domain socket on the system that the user has access to.
-#TODO look into integrating Landlock to fix this.
+# On Linux, we use chaseccomp which is a very dumb BPF assembler for
+# seccomp-bpf. Like the OpenBSD filter, this does not prevent a
+# connect(3) to UNIX domain sockets that we do not have access to.
#
-# We do not have OS-level sandboxing on other systems (yet).
-#
-# Aside from sandboxing in buffer processes, we also have a more
-# restrictive "network" sandbox that is intended for CGI processes that
-# just read/write from/to the network and stdin/stdout. At the moment this
-# is only used in the HTTP process.
-#TODO add it to more CGI scripts
+# We do not have syscall sandboxing on other systems (yet).
const disableSandbox {.booldefine.} = false
@@ -37,7 +35,7 @@ type SandboxType* = enum
stNone = "no sandbox"
stCapsicum = "capsicum"
stPledge = "pledge"
- stLibSeccomp = "libseccomp"
+ stSeccomp = "seccomp-bpf"
const SandboxMode* = when disableSandbox:
stNone
@@ -46,12 +44,12 @@ elif defined(freebsd):
elif defined(openbsd):
stPledge
elif defined(linux):
- stLibSeccomp
+ stSeccomp
else:
stNone
when SandboxMode == stCapsicum:
- import bindings/capsicum
+ proc cap_enter(): cint {.importc, cdecl, header: "<sys/capsicum.h>".}
proc enterBufferSandbox*(sockPath: string) =
# per man:cap_enter(2), it may return ENOSYS if the kernel was compiled
@@ -64,8 +62,10 @@ when SandboxMode == stCapsicum:
# no difference between buffer; Capsicum is quite straightforward
# to use in this regard.
discard cap_enter()
+
elif SandboxMode == stPledge:
- import bindings/pledge
+ proc pledge(promises, execpromises: cstring): cint {.importc, cdecl,
+ header: "<unistd.h>".}
proc enterBufferSandbox*(sockPath: string) =
# take whatever we need to
@@ -77,173 +77,26 @@ elif SandboxMode == stPledge:
proc enterNetworkSandbox*() =
# we don't need much to write out data from sockets to stdout.
doAssert pledge("stdio", nil) == 0
-elif SandboxMode == stLibSeccomp:
+
+elif SandboxMode == stSeccomp:
+ {.passl: "lib/chaseccomp/chaseccomp.o".}
+
import std/posix
- import bindings/libseccomp
-
- when defined(android):
- let PR_SET_VMA {.importc, header: "<sys/prctl.h>", nodecl.}: cint
- let PR_SET_VMA_ANON_NAME {.importc, header: "<sys/prctl.h>", nodecl.}: cint
-
- proc allowBionic(ctx: scmp_filter_ctx) =
- # Things needed for bionic libc. Tested with Termux.
- const androidAllowList = [
- cstring"rt_sigprocmask",
- "madvise"
- ]
- for it in androidAllowList:
- let syscall = seccomp_syscall_resolve_name(it)
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall, 0) == 0
- # bionic likes to set this very much. In fact, it was added to
- # the kernel by Android devs.
- block allowAnonVMAName:
- let syscall = seccomp_syscall_resolve_name("prctl")
- let arg0 = scmp_arg_cmp(
- arg: 0, # op
- op: SCMP_CMP_EQ, # equals
- datum_a: uint64(PR_SET_VMA)
- )
- let arg1 = scmp_arg_cmp(
- arg: 1, # attr
- op: SCMP_CMP_EQ, # equals
- datum_a: uint64(PR_SET_VMA_ANON_NAME)
- )
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall, 2, arg0,
- arg1) == 0
- # We have to be careful with this one; PROT_EXEC will happily set
- # memory as executable, which is certainly not what we want.
- # Now, bionic seems to be calling this from mutate(), ergo we
- # should be fine just allowing PROT_READ and PROT_READ | PROT_WRITE.
- block allowMprotect:
- let syscall = seccomp_syscall_resolve_name("mprotect")
- let arg2 = scmp_arg_cmp(
- arg: 2, # attr
- op: SCMP_CMP_LE, # less than or equals
- datum_a: 3 # PROT_READ | PROT_WRITE
- )
- # Note that libseccomp can't really express multiple comparisons.
- # However, we are lucky, and we only have to "excessively" allow
- # PROT_WRITE (w/o PROT_READ) and PROT_NONE, which does no harm.
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall, 1, arg2) == 0
-
- proc blockStat(ctx: scmp_filter_ctx) =
- # glibc calls fstat and its variants on fread, and it's quite hard
- # to ensure we never use it. Plus, in older glibc versions (< 2.39),
- # fstat is implemented as fstatat, and allowing that would imply
- # access to arbitrary paths. So for consistency, we make all of them
- # return an error.
- #
- # The offending function is _IO_file_doallocate; it doesn't actually
- # look at errno, so EPERM should work fine.
- const err = SCMP_ACT_ERRNO(1u16)
- const fstatList = [
- cstring"fstat",
- "fstat64",
- "fstatat64",
- "newfstatat",
- "statx"
- ]
- for it in fstatList:
- let syscall = seccomp_syscall_resolve_name(it)
- doAssert seccomp_rule_add(ctx, err, syscall, 0) == 0
-
- proc allowFcntl(ctx: scmp_filter_ctx) =
- # only allow F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL
- # (F_SETFL is 4, the other ones are 0-3)
- let syscall = seccomp_syscall_resolve_name("fcntl")
- let syscall2 = seccomp_syscall_resolve_name("fcntl64")
- let arg1 = scmp_arg_cmp(
- arg: 1, # cmd
- op: SCMP_CMP_LE, # less than or equals
- datum_a: 4 # F_SETFL (includes the above mentioned ones)
- )
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall, 1, arg1) == 0
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall2, 1, arg1) == 0
+
+ proc cha_enter_buffer_sandbox(): cint {.importc, cdecl.}
+ proc cha_enter_network_sandbox(): cint {.importc, cdecl.}
proc enterBufferSandbox*(sockPath: string) =
onSignal SIGSYS:
discard sig
raise newException(Defect, "Sandbox violation in buffer")
- let ctx = seccomp_init(SCMP_ACT_TRAP)
- doAssert pointer(ctx) != nil
- const allowList = [
- cstring"accept", # for incoming requests to our controlling socket
- "accept4", # for when accept is implemented as accept4
- "bind", # for outgoing requests to loader
- "brk", # memory allocation
- "clock_gettime", # used by QuickJS in atomics and cpuTime()
- "clock_gettime64", # used instead of clock_gettime on some platforms
- "clone", # for when fork is implemented as clone
- "close", # duh
- "connect", # for outgoing requests to loader
- "exit_group", # for quit
- "fork", # for when fork is really fork
- "futex", # bionic libc & WSL both need it
- "getpid", # for determining current PID after we fork
- "gettimeofday", # used by QuickJS in Date.now()
- "lseek", # glibc calls lseek on open files at exit
- "mmap", # memory allocation
- "mmap2", # memory allocation
- "mremap", # memory allocation
- "munmap", # memory allocation
- "pipe", # for pipes to child process
- "pipe2", # for when pipe is implemented as pipe2
- "poll", "ppoll", # for polling (sometimes implemented as ppoll, see musl)
- "read", # for reading from sockets
- "recvmsg", # for passing fds
- "restart_syscall", # for resuming poll on SIGCONT
- "rt_sigreturn", # for when sigreturn is implemented as rt_sigreturn
- "sendmsg", # for passing fds
- "set_robust_list", # glibc seems to need it for whatever reason
- "sigreturn", # called by signal trampoline
- "write" # for writing to sockets
- ]
- for it in allowList:
- let syscall = seccomp_syscall_resolve_name(it)
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall, 0) == 0
- block allowUnixSockets:
- # only allow creation of UNIX domain sockets.
- let syscall = seccomp_syscall_resolve_name("socket")
- let arg0 = scmp_arg_cmp(
- arg: 0, # domain
- op: SCMP_CMP_EQ, # equals
- datum_a: 1 # PF_LOCAL == PF_UNIX == AF_UNIX
- )
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscall, 1, arg0) == 0
- ctx.allowFcntl()
- ctx.blockStat()
- when defined(android):
- ctx.allowBionic()
- doAssert seccomp_load(ctx) == 0
- seccomp_release(ctx)
+ doAssert cha_enter_buffer_sandbox() == 1
proc enterNetworkSandbox*() =
- onSignal SIGSYS:
- discard sig
- raise newException(Defect, "Sandbox violation in network process")
- let ctx = seccomp_init(SCMP_ACT_KILL_PROCESS)
- doAssert pointer(ctx) != nil
- const allowList = [
- cstring"close", "exit_group", # duh
- "read", "write", "recv", "send", "recvfrom", "sendto", # socket i/o
- "lseek", # glibc calls lseek on open files at exit
- "mmap", "mmap2", "mremap", "munmap", "brk", # memory allocation
- "poll", "ppoll", # curl, ansi2html needs poll
- "restart_syscall", # for resuming poll on SIGCONT
- "getpid", # used indirectly by OpenSSL EVP_RAND_CTX_new (through drbg)
- "futex", # bionic libc & WSL both need it
- ]
- for it in allowList:
- doAssert seccomp_rule_add(ctx, SCMP_ACT_ALLOW,
- seccomp_syscall_resolve_name(it), 0) == 0
- ctx.allowFcntl()
- ctx.blockStat()
- when defined(android):
- ctx.allowBionic()
- doAssert seccomp_load(ctx) == 0
- seccomp_release(ctx)
+ doAssert cha_enter_network_sandbox() == 1
+
else:
- {.warning: "Building without OS-level sandboxing!".}
+ {.warning: "Building without syscall sandboxing!".}
proc enterBufferSandbox*(sockPath: string) =
discard