Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Seccomp filters and Linux capabilities

After this lesson: I can run a program under a seccomp allow-list and with its capabilities dropped.

65 min lesson10 commandsSandboxing lab: run untrusted code inside namespaces, cgroups, seccomp and gVisor on one private Ubuntu VM

Only the system calls on the list

Seccomp, short for secure computing, is a Linux feature that filters the system calls a process may make. A system call is how a program asks the kernel for anything: to open a file, start a process or open a network connection.

That matters because most of a kernel's attack surface sits behind calls an ordinary program never needs. A filter that allows only the calls on a short list shuts the rest.

By the end of this lesson

Note

I can run a program under a seccomp allow-list and with its capabilities dropped.

Key words

WordWhat it meansExample
System calla request from a program to the kernelopenat
Seccomp filtera kernel rule deciding each system call
Allow-listonly what is named is permitted
EPERMthe error Operation not permitted
Bounding setthe capabilities a process may ever hold
no_new_privsa flag that stops a process gaining powers

The cells students fill in are left blank in this preview.

An allow-list refuses everything else

A filter can name the calls to refuse, a deny-list, or the calls to allow, an allow-list. An allow-list is safer: a call nobody thought of is refused by default.

The lab's profiles/python.allow names about seventy calls that python3, cat and grep need. It has no socket, so no program under it can open a network connection. sbx seccomp loads the list, refuses every other call with EPERM, then starts the command.

Go to the lab folder

Command
cd ~/lab

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the top of the allow-list

Command
head -n 8 profiles/python.allow

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the key under the filter

Command
sbx seccomp profiles/python.allow -- python3 agent/agent.py secret

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Call out under the filter

Command
sbx seccomp profiles/python.allow -- python3 agent/agent.py call

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Ask the process for its seccomp mode

Command
sbx seccomp profiles/python.allow -- grep Seccomp /proc/self/status

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: decide calls against a list

Code: python
# A tiny model of an allow-list filter: every call not on the list is refused.
allow = ["read", "write", "openat", "close", "exit_group"]

def outcome(call):
    if call in allow:
        return "allowed"
    return "refused with EPERM"

for call in ["openat", "read", "socket", "connect", "write", "exit_group"]:
    print(call, "->", outcome(call))
# Add "socket" to allow. Which call is still refused?

A common mistake

Warning

Many people think a seccomp filter decides which files a program may read. It cannot. It sees the call and its numbers, not the text of a path behind a pointer. So the filter let the key be read: hiding files is the mount namespace's job.

Root is a bundle of capabilities

Root's power is split into about forty capabilities, such as cap_chown to change any file's owner and cap_sys_admin for mounts. The bounding set is the most a process may ever hold.

setpriv --bounding-set=-all empties it before starting a command, so even root then holds nothing. And no_new_privs stops a process gaining powers later, through sudo or a setuid program. A seccomp filter needs it, which is why sbx seccomp works without sudo.

Read root's capabilities

Command
sudo grep CapEff /proc/self/status

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read them with the bounding set dropped

Command
sudo setpriv --bounding-set=-all grep CapEff /proc/self/status

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make a file owned by root

Command
sudo touch /tmp/owned-by-root

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try to give it away without capabilities

Command
sudo setpriv --bounding-set=-all chown nobody /tmp/owned-by-root

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try sudo with no_new_privs set

Command
setpriv --no-new-privs sudo true

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

About this lab and its source: Sandboxing lab: run untrusted code inside namespaces, cgroups, seccomp and gVisor on one private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.