How interfaces are implemented
interfaces · implementation for snapd developers
The in-memory repository of plugs, slots and connections; the eleven security backends that turn connections into profiles; the connect task that ties them together; and the declarations the policy package reads.
On the map: the gatekeepers' hall, behind the gatehouse
01 The repository and the backends
The repository holds interfaces, plugs, slots and connections in memory; connections are persisted in state as conns and reloaded at start. Setting up a snap's security runs every backend, each building one kind of policy from the interfaces' snippets.
You run snap connect for the photo editor's camera. What in snapd actually changes? At the centre is the interface repository, in the interfaces package. It holds every interface, every plug and slot of every installed snap, and every connection, all in memory. The connections are also saved in snapd's state, and reloaded into the repository when snapd starts. When a snap's connections change, snapd sets up that snap's security again. Each backend asks the repository for the snippets that the snap's plugs and slots contribute, and writes one kind of policy. There are eleven backends, from AppArmor and seccomp to udev and symlinks.
02 A connection is a change for snapd developers
A connect-snap change holds optional prepare-plug- and prepare-slot- hooks, one connect task that checks policy, connects in the repository and sets up both snaps' security, then optional connect-slot- and connect-plug- hooks.
snap connect doesn't touch the repository directly. The daemon creates a connect-snap change. If either snap has a prepare hook for that plug or slot, it runs first and may set attributes. Then a single connect task does the work: it checks the policy, connects the pair in the repository and sets up security for both snaps, slot side first, before saving the connection in state. Any connect hooks run after it. The interface manager in overlord/ifacestate owns these handlers, along with disconnect, auto-connect and the hotplug tasks. Because they're tasks, they survive a restart, and they're undone if a later task fails.
03 The declarations
The base declaration is composed from every interface's built-in rules; a store snap-declaration rule replaces the base rule for that interface. Rules allow or deny installation, connection and auto-connection. camera's base slot rule has deny-auto-connection: true.
Before an install or a connection goes ahead, the policy package looks for a rule. The base declaration is built into snapd, put together from the rules each interface brings. A snap-declaration comes from the store, for one snap, and where it has a rule for an interface, that rule replaces the base one entirely. Each rule can allow or deny-installation, connection and auto-connection, with constraints on snap type, snap-id, publisher, attributes and the device. For the camera, the base slot rule denies auto-connection. That's why the photo editor's camera plug waits for you.
04 The first rule found decides for snapd developers for app developers
ConnectCandidate.check takes the first rule that exists: plug snap-declaration, slot snap-declaration, base plug rule, base slot rule. mount-control's base slot deny-connection: true is not read. Each kind reads only its own sub-rules, so mir auto-connects although manual connection is denied.
Which rule, then? The check method of ConnectCandidate looks in a fixed order: the plug snap's snap-declaration, the slot snap's, the base declaration's plug rule, then its slot rule. The first rule that exists decides, whether it matches or not, and the rest aren't read. Two results surprise people. mount-control has a base plug rule, so its base slot rule, deny-connection, is not read at all. And each kind reads only its own sub-rules. mir's slot rule denies connection but says nothing about auto-connection, so between store snaps snap connect is refused, while auto-connect goes ahead.
05 Store rules and the dangerous flag
A store rule is a rule in the snap's own snap-declaration. Super-privileged means allow-installation: false in the base declaration. A --dangerous install has no snap-id: only the slots get a minimal check, auto-connection follows the base declaration, and snap connect evaluates no connection rule.
"Needs a store rule" means the snap's own snap-declaration, from the store, has a rule for that interface. "super-privileged" means the base declaration says allow-installation: false, so a store snap with that plug won't install without such a rule. Now build photo-editor yourself with a snapd-control plug, and install it with --dangerous. It has no snap-id, so there's no snap-declaration, and only a minimal check of its slots runs. It installs. It doesn't auto-connect: the base plug rule denies that. And snap connect works, because with either side unasserted, no connection rule is read.
06 Where to go from here
Roads: the policy district, the overlord, confinement, sandbox internals and hotplug, and interface hooks.
A repository, eleven backends, a connect task and the declarations it reads. Here's where each piece goes deeper. The policy district has the rules in full: the base declaration, snap-declaration rules, how one rule is evaluated, and where the interface manager asks. The overlord explains changes, tasks and undo. Confinement shows the profiles the backends write. Sandbox internals show how a new connection reaches a running app's mounts, and how hotplug adds slots. And interface hooks run around the connect task.
Read more
Security backends
Registered in this order by interfaces/backends/backends.go; the order matters (the systemd backend must run before AppArmor for gpio).
| Backend | What it writes |
|---|---|
systemd | systemd units for one-shot jobs that interfaces need, such as GPIO exports |
seccomp | the seccomp filter of each app and hook |
dbus | D-Bus bus policy files |
udev | udev rules that tag devices for the snap's device cgroup |
mount | the snap's mount profile, read by snap-update-ns |
kmod | kernel modules to load on behalf of interfaces |
polkit | polkit policy files |
ldconfig | linker configuration for driver libraries, and a rebuilt linker cache |
configfiles | configuration files that interfaces need on the host; classic only |
symlinks | symbolic links that interfaces need on the host; classic only |
apparmor | the AppArmor profile of each app and hook; registered only when the kernel has some AppArmor support |
Task kinds of the interface manager
connect, disconnect, setup-profiles, remove-profiles, discard-conns, auto-connect, auto-disconnect, hotplug-add-slot, hotplug-connect, hotplug-update-slot, hotplug-remove-slot, hotplug-disconnect, hotplug-seq-wait, regenerate-security-profiles, process-delayed-security-backend-effects, apply-delayed-snap-security-backend-effects, transition-ubuntu-core.
A connect-snap change
| Task | Present when | Undo |
|---|---|---|
run-hook prepare-plug-<plug> | the plug snap has that hook | unprepare-plug-<plug> |
run-hook prepare-slot-<slot> | the slot snap has that hook | unprepare-slot-<slot> |
connect | always | removes the connection and sets up both snaps again |
run-hook connect-slot-<slot> | the slot snap has that hook | disconnect-slot-<slot> |
run-hook connect-plug-<plug> | the plug snap has that hook | disconnect-plug-<plug> |
The connect task sets up the slot snap's security, then the plug snap's, unless it was created with delayed-setup-profiles. Auto-connection does that: it adds its connect tasks together with one setup-profiles task, which sets the snaps up once for all of them.
Disconnecting
| Disconnect | What stays in state |
|---|---|
| a manual connection | nothing |
| an auto-connection, by the user | the connection, marked undesired; auto-connect skips it |
with --forget | nothing |
during snap removal (auto-disconnect) | nothing |
| by hotplug removal of a device | the connection, marked hotplug-gone |
Rule lookup
| Order | Rule | Source |
|---|---|---|
| 1 | plug rule of the plug snap's snap-declaration | store |
| 2 | slot rule of the slot snap's snap-declaration | store |
| 3 | plug rule of the base declaration | snapd |
| 4 | slot rule of the base declaration | snapd |
The first rule that exists decides. Within it, the asked kind (installation, connection or auto-connection) reads its deny-* sub-rule, then its allow-* sub-rule.
| Situation | Installation check | Connection check by snap connect | Auto-connection |
|---|---|---|---|
| Both snaps from the store | full, per plug and slot | evaluated | evaluated |
Either snap installed with --dangerous | the snap's slots only, base allow-installation only | skipped | evaluated with the other side's snap-declaration or the base declaration |
Further reading
- Security policies: how interfaces, AppArmor, seccomp and the device cgroup fit together.
- Super-privileged interfaces: which interfaces need store approval to install.
- Interface auto-connection: the auto-connection process and its constraints.
- Interface hooks: the prepare and connect hooks around a connection.
Roads from here
INTERFACES AND CONFINEMENT
- Interfaces — back to the interfaces overview
- Confinement — the profiles themselves: AppArmor and seccomp
- Interfaces by classification — all interfaces grouped by how they are built
RUNNING SNAPS
- The overlord — connect and disconnect are tasks in a change
HOOKS · code snapd runs inside a snap
- Hooks: interface hooks — the hooks around the connect task
POLICY · who may install and connect what
- Interface policy: the questions snapd asks — the questions snapd asks before install and connect
- How a rule is evaluated — how one rule is evaluated in full
- When snapd asks — where the interface manager asks
SANDBOX INTERNALS · the mount namespace
- snap-update-ns and the mount profiles — how a new connection reaches a running app
- Hotplug slots — slots that appear with devices