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

▶ Watch this deck — 4 scenes, ~3 min

01 Every process starts in snap-confine

CONFINEMENT snap runfrom the launcher snap-confineprivileged C program: builds the sandbox snap.photo-editor.photo-editorthe security tag: snap, then app snap-execthe editor a service's systemd unit the sandbox is built per tag, so two apps of one snap can have different rules
The chain from snap run, or a systemd unit, through snap-confine to snap-exec and the photo-editor app, carrying the security tag snap.photo-editor.photo-editor

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

FOUR LAYERS, IN ORDER 1 · device cgrouponly devices an interface granted; by default on core24 and newer bases, else once a device is tagged 2 · mount namespacebuilt by snap-confine: base snap as root, the snap at $SNAP; snap-update-ns adds the mounts; shared per snap 3 · AppArmor profileselected with change_onexec, so it applies from the app's first instruction 4 · seccomp filtera compiled BPF list of allowed system calls and arguments execv(snap-exec) → the app
Four layers of the sandbox set up in order: device cgroup, mount namespace, AppArmor profile, seccomp filter, then exec

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

WHERE PROFILES COME FROM connected interfaceshome: owner @{HOME}/… security backendsAppArmor · seccomp · mount … /var/lib/snapd/apparmor/profiles/snap.photo-editor.photo-editor/var/lib/snapd/seccomp/bpf/snap.photo-editor.photo-editor.bin2 the AppArmor profile is loaded into the kernel on install, refresh and every connection change snap-confine reads them
Connected interfaces such as home give snippets to snapd's security backends, which write AppArmor profiles and compiled seccomp filters per app; snap-confine reads them

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

MODES strictthe defaulteverything enforcedonly what interfaces allow devmodesame profiles, complain modeviolations logged, not blockedfor finding the interfaces--devmode classicno sandboxclassic systems onlyvetted by the store--classic journalctl -k | grep -E 'apparmor="DENIED"|SECCOMP'where denials show up
Three confinement modes side by side: strict enforces, devmode logs, classic has no sandbox; denials show in the kernel log

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

StepWhoWhat
1snap 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
2snap-confinesets up the sandbox for that tag (below)
3snap-confineexecv of snap-exec, under the app's AppArmor profile and seccomp filter
4snap-execruns 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

LayerDetail
Device cgroupRequired 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 namespaceOne 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
AppArmoraa_change_onexec to the profile named after the security tag, so it applies from the first instruction after execv
seccompthe 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

BackendOutput
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
udevrules that tag the devices an interface grants
otherssystemd, 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

ModeAppArmorseccompHow it is installed
strictenforcedenforcedthe default
devmodecomplain: logged, not deniedcomplain: logged where the kernel supports loggingsnap install --devmode; devmode snaps cannot be released to the stable channel and do not refresh automatically
classicno sandbox: an unconfined or complain profileunrestrictedsnap 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 with journalctl -k.
  • seccomp: audit lines of type SECCOMP with the system call number.
  • snap run --strace <app> traces an app under its confinement.

Further reading

Roads from here

INTERFACES AND CONFINEMENT

RUNNING SNAPS

DESKTOP · snaps in a user session

SANDBOX INTERNALS · the mount namespace