-
Notifications
You must be signed in to change notification settings - Fork 187
Open
Labels
Description
Hi, I was trying to develop a sandbox application where processes start with the bare minimum allowed syscalls to operate (read, write, exit, sigreturn) and then when they try to access more system calls, the user is notified and can either allow access or deny access and kill the process.
Currently I have the follwoing zig code:
const std = @import("std");
const c = @cImport({
// See https://github.com/ziglang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("seccomp.h");
});
/// Applies a seccomp filter where every syscall is disallowed, except the `allowed_syscalls`, and if the process violates this then it will be killed.
fn apply_seccomp_filter(comptime allowed_syscalls: []const c_int) !void {
var ctx = c.seccomp_init(c.SCMP_ACT_NOTIFY);
if (ctx == null) {
return error.FailedToInitialiseSeccomp;
}
defer c.seccomp_release(ctx);
for (allowed_syscalls) |allowed_syscall| {
if (c.seccomp_rule_add_exact(ctx, c.SCMP_ACT_ALLOW, allowed_syscall, 0) != 0) {
return error.FailedToAddSeccompRules;
}
}
if (c.seccomp_load(ctx) != 0) {
return error.FailedToLoadSeccompRules;
}
}
pub fn main() !void {
try apply_seccomp_filter(&[_]c_int{
c.__NR_read,
});
// Test if the seccomp filter has denied every syscall except read.
std.debug.print("Testing123", .{});
}