<<
path:
root/public/0x2lib.git/html/tests/seccomp.jai
blob: cfbd11f4bb7b55ad22151be17bd87be90ef3dc04
[raw]
[clear marker]
2This example is a bit sophisticated, because I'm showing a way how to
3set up a white-list using metaprogramming capabilities from Jai.
5For generating a white-list of syscalls you can use my tool in
6`tools/seccomp_collect_syscalls.py`.
8This records via `strace` syscalls and prints a ready to use array you can copy/paste here.
9Sometimes you want to only restrict syscalls from a specific point, in that case the same
10tool looks after a specific marker and only after that it will record syscalls.
12You can just print this marker in any place of your program:
14 log("---- SECCOMP BOUNDARY ----");
17Look at `sec_seccomp_init()` how to set this up.
24 log("Seccomp pre init. Writing to a FD is allowed!");
26 log("Seccomp post init. Writing to a FD is NOT allowed!");
27 log("If you can read this, something went wrong!");
36/** Copy pasta from seccomp_collect_syscalls.py */
37SECCOMP_ALLOWED_SYSCALLS :: string.[
45 //! "WRITE", /** Disallow writing to a FD so we can test it! */
65sec_seccomp_init :: () {
66 SECCOMP_TEMPLATE :: "had_error |= seccomp_rule_add(ctx, .ALLOW, .%);\n";
69 /** Tell seccomp what to do if a violation happened. */
70 ctx := seccomp_init(.KILL_PROCESS);
72 /** For development, you can just log violations. You can view them here:
77 ctx := seccomp_init(.LOG);
79 defer seccomp_release(ctx);
81 if !ctx { log_error("Init failed"); exit(1); }
85 /** This generates the `seccomp_rule_add()` functions based of the white-list. */
88 for SECCOMP_ALLOWED_SYSCALLS {
89 template := tprint(SECCOMP_TEMPLATE, it);
90 append(*buf, template);
92 s := builder_to_string(*buf);
96 /** You can also add rules here. */
97 had_error |= seccomp_rule_add(ctx, .ALLOW, .EXIT);
98 had_error |= seccomp_rule_add(ctx, .ALLOW, .EXIT_GROUP);
101 log_error("Could not add rules.");
105 if seccomp_load(ctx) < 0 {
106 log_error("Could not load context into kernel");
110 #if SECCOMP_ARMED then log("Seccomp is armed.");
111 else log("Seccomp is in log mode! NOT ARMED!");
113 /** After this point every syscall not in the white-list is blocked. */
118#import,file "../0x2_Seccomp.jai";