aboutsummaryrefslogtreecommitdiffstats
path: root/lib/chaseccomp/gen_syscalls
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 /lib/chaseccomp/gen_syscalls
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 'lib/chaseccomp/gen_syscalls')
-rwxr-xr-xlib/chaseccomp/gen_syscalls88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/chaseccomp/gen_syscalls b/lib/chaseccomp/gen_syscalls
new file mode 100755
index 00000000..a4d7d5b5
--- /dev/null
+++ b/lib/chaseccomp/gen_syscalls
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+die() {
+ echo "$*" >&2
+ exit 1
+}
+
+test "$1" || die "usage: gen_syscalls [file]"
+
+find_label() {
+ printf '%s\n' "$labels" | while read -r line
+ do case $line in
+ "$1 "*) printf '%d\n' "${line#* }"
+ break
+ ;;
+ esac
+ done
+}
+
+line_cut_next() {
+ next=${line%% *}
+ oline=$line
+ line=${line#* }
+ if test "$line" = "$oline"; then line= ; fi
+}
+
+cut_label() {
+ line_cut_next
+ label=$(find_label "$next")
+ test "$label" || die "missing label $next"
+ label=$(($label - 1 - $ip))
+}
+
+put_cmp() {
+ line_cut_next
+ val=$next
+ cut_label
+ printf '\t%s(%s, %d),\n' "$inst" "$val" "$label"
+}
+
+put_load() {
+ line_cut_next
+ printf '\t%s(%s),\n' "$inst" "$next"
+}
+
+put_ret() {
+ line_cut_next
+ case $next in
+ allow) val=SECCOMP_RET_ALLOW ;;
+ trap) val=SECCOMP_RET_TRAP ;;
+ kill) val=SECCOMP_RET_KILL_PROCESS ;;
+ errno) val="SECCOMP_RET_ERRNO | ($line & SECCOMP_RET_DATA)" ;;
+ *) die "wrong retval $line" ;;
+ esac
+ printf '\t%s(%s),\n' "$inst" "$val"
+}
+
+ip=0
+while read -r line
+do line=${line%%#*}
+ case $line in
+ ''|define' '*) ;;
+ ': '*) line_cut_next
+ if test -n "$labels"
+ then labels="$labels
+$line $ip"
+ else labels="$line $ip"
+ fi
+ ;;
+ *) ip=$((ip + 1)) ;;
+ esac
+done < "$1"
+
+ip=0
+while read -r line
+do line_cut_next
+ case $next in
+ :) continue ;;
+ define) printf '#%s %s\n' "$next $line" ; continue ;;
+ ret) inst=CHA_BPF_RET put_ret ;;
+ ifeq) inst=CHA_BPF_JE put_cmp ;;
+ ifne) inst=CHA_BPF_JNE put_cmp ;;
+ ifle) inst=CHA_BPF_JLE put_cmp ;;
+ load) inst=CHA_BPF_LOAD put_load ;;
+ *) die "unexpected instruction $inst" ;;
+ esac
+ ip=$(($ip + 1))
+done < "$1"