ADR-0013: Run the snap’s daemon as snap_daemon, with its state in $SNAP_DATA/state¶
Status: accepted
Date: 2026-11-23
Context¶
snapd starts every snap daemon as root. Up to v0.1.1 the agent stayed root: a flaw in its TLS, MQTT or protobuf handling would have run with root’s rights inside the sandbox, and every file it wrote belonged to root.
snapd offers one unprivileged user for daemons, snap_daemon (uid and gid
584788 on every system), through system-usernames. The snap still starts
as root and must drop to the user itself; snapd’s documentation shows
setpriv --clear-groups --reuid snap_daemon --regid snap_daemon. Checked
on snapd 2.78 and in its policy sources:
With
system-usernames, the AppArmor profile addscapability setuid,setgidandchown, and the seccomp filter allowssetuid,setresuid,setgroups(0, ...)andchownonly to that user and to root. Root in the snap never hasCAP_DAC_OVERRIDE,CAP_DAC_READ_SEARCHorCAP_FOWNER: it cannot enter a directory of mode 0700 thatsnap_daemonowns, and cannot change the mode of a file it does not own.The profile allows writes to
/var/snap/<snap>/<revision>/**, not to the directory itself, so chown of$SNAP_DATAis denied even to root. The first attempt failed on exactly that, because the agent keptidentity/andwal/directly in$SNAP_DATA.The install hook and the post-refresh hook run before snapd starts the services; the configure hook runs after. A failing post-refresh hook rolls the refresh back.
snap revertskips the data copy and runs neither the pre-refresh nor the post-refresh hook (overlord/snapstate/snap.go).Non-root processes may run
snapctl get,snapctl servicesandsnapctl set-health, which is all the daemon uses.
Decision¶
The snap declares
system-usernames: {snap_daemon: shared}.The agent’s identity and local log move to
$SNAP_DATA/state, mode 0700, owned bysnap_daemon. Outside the snap,-data-diris unchanged.fleetwire-agent prepare-state, run as root, creates that directory or movesidentity/andwal/of v0.1.1 into it (internal/agent/statedir). It assembles the directory asstate.partialwith renames, changes the owner of every entry before the directory that holds it and fsyncs each change, and renames the result tostate. A crash at any step leaves each file in its old or its new place, and the next run finishes the move. Astatenext to the old entries is refused, not merged.The install and post-refresh hooks run
prepare-state, so a failed move rolls the refresh back to the old revision, whose copy of$SNAP_DATAis untouched. The daemon’s command chain runs it again before each start: after a revert, a snapshot restore or a deleted state directory no hook runs.The daemon’s command chain then drops to
snap_daemonwithfleetwire-agent drop-privileges, which callssetgroups(0, NULL),setresgidandsetresuidwith every id set to 584788, checks the result, and execs the daemon. Thefleetwire-agentcommand uses the same chain when run with sudo, sostatuscan read the files and no command leaves a root-owned file the daemon cannot open. The daemon refuses to run as root in the snap.
Alternatives considered¶
Stay root. Simplest, and what v0.1.1 did; it gives every parsing bug root’s rights.
setprivfrom the core24 base, the path snapd’s documentation shows. It worked on snapd 2.78, but snapd’s default AppArmor template allows a snap to execute/usr/bin/setprivonly from 2.68 on, and Ubuntu Core 24 ships snapd 2.63: on a real Ubuntu Core 24 device the daemon failed withexec: setpriv: Permission deniedand restarted every 5 s.drop-privilegesmakes the same calls in a process of its own that execs the daemon, so the daemon’s code still never runs as root.Drop privileges in the daemon’s own process (
syscall.Setuidafter the move). It works with the same seccomp rules, but the move, the Go runtime’s start-up and the daemon would share one process that starts as root.Keep the state in
$SNAP_COMMON. snapd does not version it, so a revert would not rewind the log. But v0.1.1 reads$SNAP_DATA, a later refresh from a reverted v0.1.1 would find two states to reconcile, and an older revision would have to read whatever format a newer one wrote.Move the files in the daemon only, without hooks. One place instead of three, but a failed move would leave the new revision restarting every 5 s instead of rolling the refresh back.
The
_daemon_user (uid 584792), which snapd 2.61 and later offer in place ofsnap_daemon. It needs snapd 2.61 on every device; switching later means another ownership change.
Consequences¶
The daemon has no capabilities and owns only its state directory. A
metrics.listenport below 1024 no longer works.Without sudo,
fleetwire-agent statusstill works through the daemon’s endpoint, but cannot read the files when the daemon is stopped.An administrator who writes a file into
state/as root must give it tosnap_daemon, as the recovery how-to shows.A revert to v0.1.1 runs on that revision’s copy of
$SNAP_DATAfrom the time of the refresh. Readings collected since stay in the newer revision’s directory, and seqs the server already stored are handed out again until its ack moves the seq forward; ingest drops those readings as duplicates. Run on Ubuntu Core gives a revert procedure that loses nothing. The same holds for any revert between two later revisions.