sandprint: Generating seccomp Profiles by Watching What a Service Does

A few weeks ago I looked at the seccomp profile shipped with one of our production services and realized I couldn’t say exactly what it allowed. It permitted 350 system calls. The service used maybe 30 on its hot path, generously counted. The other 320 were there because they were in the Docker default profile we’d copied, and they were in the Docker default because someone years ago decided it was easier to be permissive than specific.

The other common kind of seccomp profile I’ve seen is written by hand against a checklist. Those are tighter, and they cause an avoidable postmortem the day someone hits a code path nobody tested.

What I wanted was a tool that watches what the service actually does during a representative run and writes down that list. Rough versions exist: strace -c prints a syscall summary, and there are several scripts that pipe it through a JSON formatter. For reasons below, those didn’t fit, so I wrote one.

sandprint is a small Rust binary. It traces a target process, either by running a command or attaching to a running PID, using eBPF. It records every syscall the process and its children make, and at the end writes a tight allowlist in the format your sandbox needs: an OCI runtime profile, a systemd SystemCallFilter= line, a libseccomp C header or raw JSON. The code is at github.com/mtclinton/sandprint (Apache-2.0): about 2,500 lines of Rust and 100 lines of BPF C, small enough to read in an afternoon.

Example

$ sudo sandprint profile run -- ls /tmp
... INFO BPF tracer loaded and attached
... INFO tracing command pid=2409773
[directory listing]
... INFO child exited status=Exited(Pid(2409773), 0)
Observed 202 syscall events (27 unique syscalls)

   NR       COUNT  NAME
    9          36  mmap
  257          35  openat
    3          26  close
    5          23  fstat
    1          19  write
   59          11  execve
    0           9  read
   10           6  mprotect
  157           6  prctl
   12           5  brk
  217           5  getdents64
   ...

That’s ls /tmp under sandprint. 27 unique syscalls is the observed footprint of a directory listing: getdents64 and statx for the entries, write for the output, and the usual glibc startup. A profile built from this run allows 27 syscalls; a generic container default allows over 300.

$ sandprint profile generate --input trace.json --format oci > seccomp.json
$ wc -l seccomp.json
   71 seccomp.json

Put that in the OCI runtime spec under linux.seccomp and you have a working sandbox.

The BPF side

The kernel side is a BPF program of about a hundred lines, attached to three raw tracepoints:

  • sys_enter fires on every syscall entry. If the calling task is in the tracked set, the program pushes a 40-byte event onto a ring buffer.
  • sched_process_fork adds any child of a tracked task to the tracked set automatically, which is how process trees get followed with no userspace bookkeeping.
  • sched_process_exit removes dead tasks so the tracked-PID hashmap doesn’t grow without bound during a long trace.

The userspace side is one thread that polls the ring buffer until the target exits, then turns the event log into a canonical JSON trace.

The BPF program declares only what it needs. It reads task->tgid to identify the task group, but instead of pulling in the whole task_struct it declares a one-field stub:

struct task_struct {
    int tgid;
} __attribute__((preserve_access_index));

CO-RE (Compile Once, Run Everywhere) handles the rest: libbpf rewrites the field offset at load time using BTF (BPF Type Format) data from the running kernel. The same compiled object loads on 5.10, 6.1 and whatever Ubuntu’s newest LTS ships, without rebuilding. There’s no vmlinux.h in the repo and no per-distro build matrix. The cost is that a kernel without /sys/kernel/btf/vmlinux won’t load the program, which in practice isn’t an issue on any kernel from the last few years.

The program sticks to constructs the verifier accepts: bounded array indexing, no unbounded loops, atomic counter updates, and paired ring-buffer reserve and submit. The verifier doesn’t complain about it, which I appreciate after past arguments with it.

Why not strace

The first question about any new tool is whether an old one already does the job. strace -c has counted syscalls for a long time. It didn’t fit for two reasons.

Performance. ptrace-based tracing, which is what strace uses, slows the target down noticeably. Every traced syscall goes through ptrace’s wakeup machinery, so a performance- or timing-sensitive workload behaves differently under strace than in production, and you end up profiling strace instead of the service. eBPF tracing is close to free at this scale: the tracepoint fires anyway, the BPF program writes 40 bytes to a ring buffer, and userspace reads it asynchronously. There’s no signal and no extra syscall per traced call, and no per-call overhead shows up in benchmarks.

Exclusivity. Only one tool can ptrace a process at a time, so you can’t strace a process already under gdb, or the reverse. eBPF has no such limit. A process can be traced by sandprint, debugged with gdb and audited by auditd at the same time, without any of them knowing about the others.

The cost of eBPF is the capability requirement. The tracer needs CAP_BPF + CAP_PERFMON (or CAP_SYS_ADMIN on older kernels), and the kernel needs BTF at /sys/kernel/btf/vmlinux. Both are standard on recent kernels. On older or hardened kernels they aren’t, and you’re back to ptrace; sandprint isn’t the tool for those.

The limit of observation

A profile built from observation has a weakness a profile written from a spec doesn’t: it only contains what your run actually executed. If the test run you traced never hits the error path that calls prctl, the generated profile blocks prctl. The first time that error path runs in production, the kernel sends the process SIGSYS, and you have an avoidable incident.

This applies to every observation-based tool, not just sandprint: oci-seccomp-bpf-hook, syscall2seccomp, and the scripts that build profiles from strace -c output all share it. A generated profile is a starting point for tightening, not a verified result.

The workflow that’s worked for me:

  1. Run sandprint while exercising every code path you care about: CI integration tests, soak tests, error paths.
  2. Combine the traces into one profile with profile merge.
  3. Apply the profile in staging and watch kernel logs for SIGSYS and EPERM.
  4. Repeat.

The profile is wrong on the first run, less wrong on the third, and acceptably tight once it has seen everything your CI knows how to exercise.

profile diff between two runs turned out to have a side benefit I didn’t plan for: it shows which test paths exercise which syscalls, and, by absence, which paths add no new syscalls and might be pruned from the CI matrix. I use it more than any other part of the tool.

What’s next

A syscall allowlist alone is a much weaker sandbox than a syscall allowlist plus a path allowlist. The next feature is argument capture for open and openat: read the path argument as the call enters and emit an allowed-paths list next to the syscall list. It’s more involved (walking task->files->fdt->fd[i] for the fd case, or bpf_probe_read_user_str on the userspace pointer for the path case), but the kernel side isn’t more complicated than what sandprint already does for syscall numbers.

After that:

  • Network argument capture (socket family, type, protocol): the same mechanism on different arguments.
  • Container-runtime integration as a containerd NRI plugin or a runc hook. This is the biggest item: it would let you turn sandprint on for any container without changing the command line, and it connects to the OCI profile output that already exists.

None of these is architecturally hard; it’s a matter of finding the time.

The code is at github.com/mtclinton/sandprint, Apache-2.0. Issues and PRs are welcome, with the caveat that I’ll respond on a hobbyist’s schedule.