RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Move the theme helper's seccomp filter onto the shared builder
oxydizere390fcdd03b8
diff --git a/build.zig b/build.zigindex ffc515c..b9e77e8 100644--- a/build.zig+++ b/build.zig@@ -123,6 +123,7 @@ pub fn build(b: *std.Build) void {theme_helper.root_module.addImport("config", config_mod);theme_helper.root_module.addImport("ui", ui_mod);theme_helper.root_module.addImport("i18n", i18n_mod);+ theme_helper.root_module.addImport("platform", platform_mod);theme_helper.root_module.addCSourceFile(.{ .file = b.path("src/theme_helper/native.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });inline for (.{ "libarchive", "librsvg-2.0", "gdk-pixbuf-2.0" }) |lib| theme_helper.root_module.linkSystemLibrary(lib, .{});addThumbCodec(b, theme_helper.root_module);diff --git a/build/tests.zig b/build/tests.zigindex b197baa..2e6afe6 100644--- a/build/tests.zig+++ b/build/tests.zig@@ -107,6 +107,9 @@ pub fn addZig(ctx: Context, session: Session) ZigTests {const run_platform_tests = b.addRunArtifact(b.addTest(.{ .root_module = ctx.platform_mod }));test_step.dependOn(&run_platform_tests.step);b.step("test-platform", "Test child handles, file watching and the program's own path").dependOn(&run_platform_tests.step);+ const theme_policy_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/theme_helper/syscall_policy.zig"), .target = target, .optimize = optimize, .link_libc = true }) });+ theme_policy_tests.root_module.addImport("platform", ctx.platform_mod);+ test_step.dependOn(&b.addRunArtifact(theme_policy_tests).step);const i18n_tool_tests = b.addTest(.{ .root_module = i18n_tool.root_module });test_step.dependOn(&b.addRunArtifact(i18n_tool_tests).step);b.step("test-i18n", "Test locale resolution, catalogs and formatting").dependOn(&run_i18n_tests.step);diff --git a/docs/plans/freebsd.md b/docs/plans/freebsd.mdindex dffdb8a..907c42c 100644--- a/docs/plans/freebsd.md+++ b/docs/plans/freebsd.md@@ -49,7 +49,7 @@ translate. About 47 Zig files call `std.os.linux.*` directly.The PDF sandbox (`pdf/sandbox.c` + `pdf/syscall_policy.zig`: raw BPF through`platform/seccomp.zig`, + Landlock) and the theme helper-(`theme_helper/native.c`: raw BPF + Landlock) have no FreeBSD counterpart+(`theme_helper/native.c` + `syscall_policy.zig`: the same builder, + Landlock) have no FreeBSD counterpartshort of a rewrite on **Capsicum** (`cap_enter`, `cap_rights_limit` onpre-opened directory fds). The design fits, since both already preload beforethe sandbox closes (`compose.preload`), but capability mode forbids every@@ -119,12 +119,17 @@ ReleaseSafe, `zig build test` and affected integration suites.`/proc/self/exe` readers.4. **Raw syscalls to libc** where libc has the call (fork, execve, wait4,close, read, getuid, …), keeping `std.os.linux` for what has no libc form.-5. **One sandbox interface:** (half done) the PDF sandbox no longer uses- libseccomp: `platform/seccomp.zig` builds and loads the filter and+5. **One sandbox interface:** (syscall filters done) the PDF sandbox no longer+ uses libseccomp: `platform/seccomp.zig` builds and loads the filter and`pdf/syscall_policy.zig` holds its allow-list, checked verdict for verdict- against the libseccomp program it replaced. Left: move the theme helper's- C filter onto the same builder and give both one "restrict to these- directories" entry point, which Capsicum later implements.+ against the libseccomp program it replaced; the theme helper's filter+ (`theme_helper/syscall_policy.zig`: no `socket`/`connect`) uses the same+ builder, checked the same way against its old C filter. Left: give both+ sandboxes one "restrict to these directories" entry point, which Capsicum+ later implements. Their Landlock code differs on purpose (PDF: read-only+ trees, ABI 3 required, fails closed; theme helper: optionally writable root+ and `/usr` executable, runs without Landlock on older kernels), so settle+ what the entry point promises before moving either.6. **Process listing:** keep `/proc` parsing behind the system monitor'scollector interface (shared with `ipc/processes.zig`).7. **Paths:** replace fixed `/usr/bin/…` tool paths with `PATH` lookup.diff --git a/src/platform/AGENTS.md b/src/platform/AGENTS.mdindex e02f68d..477700d 100644--- a/src/platform/AGENTS.md+++ b/src/platform/AGENTS.md@@ -2,8 +2,10 @@The one place the OS shows: child handles (`child.zig`), directory watching(`watch.zig`), the program's own path (`exe.zig`), seccomp-BPF filters-(`seccomp.zig`), and `child.h`/`self_exe.h` for C and C++. Std only, no compositor imports. Linux today; the FreeBSD port-(`docs/plans/freebsd.md`) adds `freebsd_*.zig` parts without changing callers.+(`seccomp.zig`, for the PDF viewer and the theme helper), and+`child.h`/`self_exe.h` for C and C++. Std only, no compositor imports. Linux+today; the FreeBSD port (`docs/plans/freebsd.md`) adds `freebsd_*.zig` parts+without changing callers.* **A `Handle` is not a pid.** Hold it for as long as you may signal; a bare`kill(pid)` is only safe while you have not reaped the child. `pid()` is@@ -22,7 +24,8 @@ The one place the OS shows: child handles (`child.zig`), directory watchingand nothing allocates. Rules match in order; `ruleArg` compares all 64 bitsand falls through on a mismatch. The header kills a foreign arch and an x32number, but not -1 (a tracer's skip number: it gets the default action, as- libseccomp did). Use `seccomp.number("name")` (null where an architecture+ libseccomp did; under an allow-by-default policy such as the theme helper's+ it reaches the kernel, which answers ENOSYS). Use `seccomp.number("name")` (null where an architecturedropped the call) and keep optional legacy names in a separate list fromrequired ones, so a typo is a compile error and not a silent hole. Load with`install` only after `PR_SET_NO_NEW_PRIVS`, single-threaded; it goes throughdiff --git a/src/theme_helper/native.c b/src/theme_helper/native.cindex c6dad67..b3732c1 100644--- a/src/theme_helper/native.c+++ b/src/theme_helper/native.c@@ -10,9 +10,6 @@#include <errno.h>#include <fcntl.h>#include <linux/landlock.h>-#include <linux/filter.h>-#include <linux/seccomp.h>-#include <linux/audit.h>#include <stddef.h>#include <stdint.h>#include <stdio.h>@@ -33,6 +30,8 @@ static char problem[4096];static unsigned entries;static uint64_t total;extern int rediwm_theme_safe_path(const char *);+// The syscall policy is built and loaded in syscall_policy.zig.+extern int rediwm_theme_syscall_policy(void);const char *rediwm_theme_problem(void) { return problem; }static int fail(const char *file, const char *rule) {snprintf(problem, sizeof(problem), "%s: %s", file, rule); return -1;@@ -373,28 +372,7 @@ static int parser_sandbox(const char *root, const char *source, int writable) {int rc=syscall(SYS_landlock_restrict_self,rules,0);close(rules);if(rc<0)return -1;}#endif-#if defined(__x86_64__)- const uint32_t expected_arch=AUDIT_ARCH_X86_64;-#elif defined(__aarch64__)- const uint32_t expected_arch=AUDIT_ARCH_AARCH64;-#else- return -1;-#endif- struct sock_filter filter[]={- BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,arch)),- BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,expected_arch,1,0),- BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_KILL_PROCESS),- BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,nr)),- BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K,0x40000000,0,1),- BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_KILL_PROCESS),- BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_socket,0,1),- BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ERRNO|EPERM),- BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_connect,0,1),- BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ERRNO|EPERM),- BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ALLOW)- };- struct sock_fprog prog={.len=sizeof(filter)/sizeof(filter[0]),.filter=filter};- return prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&prog);+ return rediwm_theme_syscall_policy();}int rediwm_theme_validate_isolated(const char *root) {struct stat st;if(lstat(root,&st)<0||!S_ISDIR(st.st_mode))return fail(root,"pack root must be a directory");diff --git a/src/theme_helper/syscall_policy.zig b/src/theme_helper/syscall_policy.zignew file mode 100644index 0000000..b348a16--- /dev/null+++ b/src/theme_helper/syscall_policy.zig@@ -0,0 +1,47 @@+//! The theme parsers' syscall policy, loaded by `native.c` in the disposable+//! child after its filesystem policy: no socket may be created or connected.+//! Everything else is allowed; Landlock and the memory limit confine the rest.+const std = @import("std");+const seccomp = @import("platform").seccomp;++pub fn program(buf: []seccomp.Insn) seccomp.Builder.Error![]const seccomp.Insn {+ var b = try seccomp.Builder.init(buf);+ try b.rule(seccomp.number("socket").?, seccomp.errno(.PERM));+ try b.rule(seccomp.number("connect").?, seccomp.errno(.PERM));+ return b.finish(seccomp.allow);+}++/// Called by `native.c` once the filesystem policy is in place: 0 or -1.+export fn rediwm_theme_syscall_policy() c_int {+ var buf: [32]seccomp.Insn = undefined;+ const filter = program(&buf) catch return -1;+ seccomp.install(filter) catch return -1;+ return 0;+}++fn call(nr: u32, arch: u32) std.os.linux.SECCOMP.data {+ return .{+ .nr = @bitCast(nr),+ .arch = arch,+ .instruction_pointer = 0,+ .arg0 = 0,+ .arg1 = 0,+ .arg2 = 0,+ .arg3 = 0,+ .arg4 = 0,+ .arg5 = 0,+ };+}++test "sockets are refused, other calls pass, foreign ABIs are killed" {+ var buf: [32]seccomp.Insn = undefined;+ const p = try program(&buf);+ const native = seccomp.audit_arch;+ for ([_]u32{ seccomp.number("socket").?, seccomp.number("connect").? }) |nr| {+ try std.testing.expectEqual(seccomp.errno(.PERM), seccomp.evaluate(p, call(nr, native)));+ }+ for ([_]u32{ seccomp.number("read").?, seccomp.number("openat").?, seccomp.number("mmap").?, seccomp.number("socketpair").? }) |nr| {+ try std.testing.expectEqual(seccomp.allow, seccomp.evaluate(p, call(nr, native)));+ }+ try std.testing.expectEqual(seccomp.kill_process, seccomp.evaluate(p, call(seccomp.number("read").?, native ^ 1)));+}diff --git a/src/theme_main.zig b/src/theme_main.zigindex d04e6f2..ab7d484 100644--- a/src/theme_main.zig+++ b/src/theme_main.zig@@ -3,6 +3,11 @@ const std = @import("std");const packs = @import("config").theme_pack;const theme = @import("ui").theme;const archive = @import("files/archive.zig");++// `native.c` calls the exported syscall policy.+comptime {+ _ = @import("theme_helper/syscall_policy.zig");+}extern fn rediwm_theme_stage_isolated([*:0]const u8, [*:0]const u8) c_int;extern fn rediwm_theme_validate_isolated([*:0]const u8) c_int;extern fn rediwm_theme_rollback([*:0]const u8, [*:0]const u8) c_int;