about summary refs log tree commit diff stats
path: root/lib/chaseccomp/chaseccomp.c
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/chaseccomp.c
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/chaseccomp.c')
-rw-r--r--lib/chaseccomp/chaseccomp.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/chaseccomp/chaseccomp.c b/lib/chaseccomp/chaseccomp.c
new file mode 100644
index 00000000..9001b973
--- /dev/null
+++ b/lib/chaseccomp/chaseccomp.c
@@ -0,0 +1,48 @@
+/*
+ * ref. seccomp(2)
+ * also bpf(4), except I can't find it on Linux... check a BSD.
+ */
+
+#include <stdlib.h>
+#include <stddef.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <fcntl.h>
+#include <stdint.h>
+
+#include "chaseccomp.h"
+
+int cha_enter_buffer_sandbox(void)
+{
+	struct sock_filter filter[] = {
+#include "chasc_buffer.h"
+	};
+	struct sock_fprog prog = { .len = COUNTOF(filter), .filter = filter };
+
+	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
+		return 0;
+	if (syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog))
+		return 0;
+	return 1;
+}
+
+int cha_enter_network_sandbox(void)
+{
+	struct sock_filter filter[] = {
+#include "chasc_network.h"
+	};
+	struct sock_fprog prog = { .len = COUNTOF(filter), .filter = filter };
+
+	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
+		return 0;
+	if (syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog))
+		return 0;
+	return 1;
+}