Confinement
the sandbox for app developers for Ubuntu Core developers for snapd developers
Every app process is started through snap-confine. It sets up a device cgroup and a mount namespace and arms the AppArmor profile and the seccomp filter that snapd's security backends wrote from the snap's connected interfaces. Strict confinement enforces them and devmode only logs; classic confinement has no sandbox.
On the map: the city walls
01 Every process starts in snap-confine
snap run and systemd units start every app through snap-confine, a privileged C program that builds the sandbox for the security tag snap.<snap>.<app> and then execs snap-exec and the app.
You click the photo-editor icon. What starts first? Not the editor. The launcher runs snap run, and snap run hands over to snap-confine, a small C program with just enough privilege to build a sandbox. It's given a security tag: snap.photo-editor.photo-editor, the snap's name and then the app's. snap-confine builds the sandbox for that tag and only then hands over to snap-exec, which starts the editor itself. A service goes the same way, because its systemd unit runs snap run too. And since the tag names the app, two apps of one snap can get different rules.
02 What snap-confine sets up for app developers for snapd developers
In order: a device cgroup (by default on core24 and newer bases, otherwise once a device is tagged), the mount namespace that snap-confine builds and snap-update-ns completes with interface and layout mounts, shared by every process of the snap, the AppArmor profile via change_onexec, the seccomp filter, then exec.
Between your click and the editor's first instruction, snap-confine puts four layers in place, in this order. First a device-cgroup, so the app reaches only the devices an interface granted. Snaps on core24 and newer bases get one by default. Second, the mount namespace: a private view of the files, with the base snap as root and the snap at $SNAP, which snap-update-ns completes with interface and layout mounts. Third, the AppArmor profile, armed to apply at exec, so it covers the app from its first instruction. Fourth, the seccomp filter of allowed system calls. Then snap-confine execs snap-exec, which starts the app.
03 Where the profiles come from for app developers for snapd developers
On install, refresh and every connection change, snapd's security backends regenerate the snap's profiles from the snippets of its connected interfaces: AppArmor profiles in /var/lib/snapd/apparmor/profiles, compiled seccomp filters in /var/lib/snapd/seccomp/bpf. snap-confine only applies them.
snap-confine applies the profiles, but it doesn't write them. snapd does. Say you connect photo-editor's home plug. snapd asks each interface connected to the snap for its piece of policy, and hands the pieces to its security backends: AppArmor, seccomp, mounts, udev and a few more. Home's piece is a set of AppArmor rules for your home folder. The AppArmor backend writes one profile per app under /var/lib/snapd/apparmor/profiles and loads it into the kernel. The seccomp backend compiles a filter under /var/lib/snapd/seccomp/bpf. The same happens on every install and refresh. snap-confine only reads the results.
04 Strict, devmode, classic
Strict enforces the sandbox. Devmode loads the same profiles in complain mode and logs violations; install with --devmode. Classic has no sandbox, needs --classic, works only on classic systems and is vetted by the store. Denials appear in the kernel log.
Not every snap wants the whole sandbox, so a snap declares a confinement mode. Strict is the default, and it's everything you've just seen: the app gets only what its connected interfaces allow. Devmode keeps the same profiles in complain mode, so a violation is logged instead of blocked. That's how you find the interfaces your snap needs while you build it, installing it with the --devmode flag. Classic has no sandbox at all, like a traditional package. You install it with the --classic flag, only on classic systems, and the store reviews every classic snap. Denials land in the kernel log.
Read more
The start of a confined app
| Step | Who | What |
|---|---|---|
| 1 | snap run (from a launcher, a shell or a systemd unit) | works out the app and its security tag snap.<snap>.<app> (hooks: snap.<snap>.hook.<hook>) and runs snap-confine |
| 2 | snap-confine | sets up the sandbox for that tag (below) |
| 3 | snap-confine | execv of snap-exec, under the app's AppArmor profile and seccomp filter |
| 4 | snap-exec | runs the app's command, through any command-chain |
snap-confine is privileged. On Ubuntu it gets its privileges from file capabilities set by the package (cap_sys_admin, cap_dac_override, cap_sys_chroot and others), not from the setuid bit. It drops every capability before it runs snap-exec for a regular user.
What snap-confine sets up, in order
| Layer | Detail |
|---|---|
| Device cgroup | Required for snaps on core24 and newer bases, also when no device is tagged. For core, core16, core18, core20 and core22 it is created only when udev has tagged a device for the snap. Skipped inside a container, for a snap that manages its own device cgroup, and for devmode and classic snaps not in jailmode |
| Mount namespace | One per snap instance, shared by all its apps and hooks and preserved in /run/snapd/ns/<snap>.mnt between runs. On the first run snap-confine builds it: the base snap as /, host directories such as /home, /run and /var/snap bound in, a private /tmp, then snap-update-ns applies the interface and layout mounts. Later runs join it. See the mount namespace |
| AppArmor | aa_change_onexec to the profile named after the security tag, so it applies from the first instruction after execv |
| seccomp | the compiled filter /var/lib/snapd/seccomp/bpf/<tag>.bin2 is loaded |
Classic snaps skip the private mount namespace. Devmode snaps keep the namespace but, like classic snaps, get no device cgroup.
Files the backends write
| Backend | Output |
|---|---|
| AppArmor | /var/lib/snapd/apparmor/profiles/<tag>, loaded into the kernel with apparmor_parser --replace |
| seccomp | /var/lib/snapd/seccomp/bpf/<tag>.src, compiled to <tag>.bin2 |
| mount | /var/lib/snapd/mount/snap.<snap>.fstab and .user-fstab, applied by snap-update-ns |
| udev | rules that tag the devices an interface grants |
| others | systemd, D-Bus, kmod, polkit, ldconfig, configuration files, symlinks |
The backends run for the setup-profiles task of an install or refresh, and in the connect and disconnect tasks. AppArmor profiles are replaced in the kernel, so running processes get the new rules. A seccomp filter cannot be changed in a running process; a new filter applies from the next start. snap-update-ns changes a preserved mount namespace in place.
Confinement modes
| Mode | AppArmor | seccomp | How it is installed |
|---|---|---|---|
| strict | enforced | enforced | the default |
| devmode | complain: logged, not denied | complain: logged where the kernel supports logging | snap install --devmode; devmode snaps cannot be released to the stable channel and do not refresh automatically |
| classic | no sandbox: an unconfined or complain profile | unrestricted | snap install --classic; refused on Ubuntu Core with snap "<name>" requires classic confinement which is only available on classic systems |
--jailmode applies strict confinement to a devmode snap.
Seeing denials
- AppArmor denials: kernel log lines with
apparmor="DENIED", for example withjournalctl -k. - seccomp: audit lines of type
SECCOMPwith the system call number. snap run --strace <app>traces an app under its confinement.
Further reading
- Snap confinement: the confinement levels and what each permits.
- Security policies: AppArmor, seccomp, cgroups and namespaces as snapd uses them.
- Classic confinement: what a classic snap gives up and why the store vets every one.
- Debug snaps: finding denials and tracing a confined app.
Roads from here
INTERFACES AND CONFINEMENT
- Interfaces — interfaces are the openings in confinement
- How interfaces are implemented — the backends that write the profiles
- The home interface — one interface's rules inside a profile
- The network interface — another interface's policy in detail
- Classic confinement: when the sandbox is switched off — when the sandbox is switched off
RUNNING SNAPS
- The overlord — setup-profiles is a task
DESKTOP · snaps in a user session
- AppArmor prompting — prompts that extend the sandbox at run time
SANDBOX INTERNALS · the mount namespace
- The per-snap mount namespace — how the mount namespace is built and kept
- snap-update-ns and the mount profiles — how interface and layout mounts are applied