Skip to content

Q: use a whitelist, and notify when the process tries to use a syscall that is not on the whitelist #420

@godalming123

Description

@godalming123

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", .{});
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions