Skip to content
systemcoding.dev
Security

Build a seccomp Allowlist by Recording a Program's Syscalls With strace

Record a program's system calls with strace, turn the list into a libseccomp allowlist, and test it with a logging default before you switch to kill.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, Rust and GNU logos.

Nobody knows by heart which system calls a real program makes. A trivial cat touches a dozen before it prints a byte, and the list changes between libc versions. Guessing produces a filter that kills the program on its first request, or one so wide it protects nothing. The practical method is to record what the program actually does, convert the record into an allowlist, and tighten it in stages. This post shows that workflow with strace 6.8 and libseccomp 2.5.5 on Linux 6.12. It builds on the libseccomp filter example, and the sandbox it feeds belongs to the complete guide to sandboxing Linux processes.

What recording can and cannot tell you

strace attaches through ptrace and prints each system call the program makes. A recording is therefore a list of the calls that happened on the runs you watched. It is not a list of the calls the program can make. Error paths, rare commands, a retry after a failed network call and the code that runs only on the first day of a month are all absent unless you exercise them. That is the central risk of this method, and the stages below exist to contain it.

Treat the recorded list as a lower bound. Add calls you know the runtime needs but your test did not reach, and review every entry as a decision rather than a copy-paste.

Step 1: record

Run the program under strace with -f to follow child threads and processes, and write the output to a file so it does not mix with the program's own output.

Shell
strace -f -o trace.txt -- ./report-tool --input sample.csv --output out.txt
tail -n 4 trace.txt
Text
4127  write(1, "rows: 1204\n", 11)      = 11
4127  close(3)                          = 0
4127  exit_group(0)                     = ?
4127  +++ exited with 0 +++

Each line starts with the process or thread ID because -f is on. The sample program here, report-tool, is a stand-in: any program works. Its PIDs and read sizes will differ on your machine.

A single run records only that run. Use more than one, covering the normal path and at least one failure, such as a missing input file, because the error path calls things the happy path does not (a write to standard error, for example).

Step 2: extract the names

The trace holds more than names, so reduce it to a sorted unique list. Strip the leading PID, keep the text before the first parenthesis, and drop lines that are not calls (exit notices and signal lines).

Shell
sed -E 's/^[0-9]+ +//' trace.txt \
  | grep -E '^[a-z_0-9]+\(' \
  | sed -E 's/\(.*//' \
  | sort -u > syscalls.txt
cat syscalls.txt
Text
access
arch_prctl
brk
close
execve
exit_group
fstat
getrandom
mmap
mprotect
munmap
newfstatat
openat
pread64
prlimit64
read
rseq
set_robust_list
set_tid_address
write

This is a plausible list for a small C program linked against glibc 2.40. Yours will differ, because the dynamic loader, the allocator and the C library each add calls and the set changes with versions. Two entries deserve comment. execve is there because strace launched the program, and the filter you build will be loaded after the program has already started, so you should remove it unless the program legitimately starts others. access is the loader probing for /etc/ld.so.preload, and it only happens at start-up, which tells you the filter belongs after start-up, not before.

Step 3: generate the filter code

Turn the list into libseccomp calls with a few lines of shell, so the allowlist is data and never hand-typed. This writes a C source file that you include into the program.

Shell
{
  echo '/* generated from syscalls.txt */'
  echo '#include <seccomp.h>'
  echo 'int install_filter(void) {'
  echo '  scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_LOG);'
  echo '  if (!ctx) return -1;'
  while read -r name; do
    echo "  seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS($name), 0);"
  done < syscalls.txt
  echo '  int rc = seccomp_load(ctx);'
  echo '  seccomp_release(ctx);'
  echo '  return rc;'
  echo '}'
} > filter_gen.c
head -n 8 filter_gen.c
Text
/* generated from syscalls.txt */
#include <seccomp.h>
int install_filter(void) {
  scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_LOG);
  if (!ctx) return -1;
  seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(access), 0);
  seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(arch_prctl), 0);

The default action is SCMP_ACT_LOG, deliberately. With it, a call that is not on the list still runs, and the kernel writes an audit record. That stage exists to find what you missed.

Step 4: watch the log, widen the list

Run the program again, through its real workflows, with the filter in log mode. The kernel emits an audit record of type SECCOMP for each unlisted call. Read them with:

Shell
sudo ausearch -m SECCOMP --start recent | grep -o 'syscall=[0-9]*' | sort | uniq -c
Text
      3 syscall=318

The number is the system call number; on x86-64, 318 is getrandom. Translate numbers with ausyscall:

Shell
ausyscall x86_64 318
Text
getrandom

If getrandom was missing from the recording (it can be absent when a run happens to hit a seeded path), add it to syscalls.txt and regenerate. Repeat until a full pass through every workflow, including the failure cases, produces no records. A systemd service gets the same facility without code through SystemCallFilter= with SystemCallLog=, which logs calls outside a set.

Step 5: tighten and switch to enforcement

Once the log is quiet, change the default to a denial. SCMP_ACT_ERRNO(EPERM) first, because it keeps the program alive and visible; SCMP_ACT_KILL_PROCESS after you trust the list. Then prune. Go through the list and ask, for each entry, whether it is needed after start-up. Remove execve, and consider removing calls only used while loading libraries (access, pread64 on the ELF headers) if you load the filter after all libraries are mapped and lazy binding is resolved.

Then narrow the dangerous ones with arguments. mmap and mprotect with PROT_EXEC set are how injected code gets executed, so a program that never needs executable memory should have a rule that denies it. openat cannot be narrowed by path, and that is a reason to pair the filter with a read-only mount or a Landlock ruleset.

Record in the environment you will ship

The system call set depends on the C library and kernel. A trace on a glibc 2.40 laptop will not match a musl container, and a filter built from it can kill the program on the other system. Record on the same image, libc and kernel family you deploy, and regenerate when any of them changes. We keep the generator script in the repository next to the program, so the list is rebuilt rather than inherited.

Where recording fails

Programs with several modes of operation produce a different list in each, and unexercised modes are the main reason a recorded list misses calls. Runtimes that compile code at run time, such as a JIT, call mprotect and mmap in patterns that vary by workload. Software that handles signals adds rt_sigreturn, sigaltstack and others only when a signal arrives. In those cases use the recording to learn the shape, then review the list against the runtime's documentation and test the odd paths deliberately.

Keep the recording honest

Write down what each recording covered: the input files, the command-line flags, the failure cases and the date. A list with no provenance is hard to trust and impossible to refresh. We store the trace summary next to the generated filter in the repository, with a one-line note naming the libc version it was taken on. When a reviewer sees a new entry in a later diff, the note tells them which change in the program or the platform caused it. It also makes removals easy to justify, since a call that disappears from the trace after a library upgrade can be dropped from the list instead of lingering as dead permission.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.