0012 · klavd daemon: local engine, socket with peer UID and line-delimited JSON protocol
- Status: accepted
- Date: 2026-11-02
Context
Phase 3 calls for a daemon (klavd) that keeps the account unlocked in memory. It must serve the CLI and, later, the extension (phase 5, native messaging) and Touch ID unlock. The roadmap requires three things:
- a Unix socket with access control;
- an idle lock;
- that the CLI uses the daemon when it is running and, if not, unlocks on its own.
Three things need deciding: what the daemon does, how it talks to the CLI and how it is protected.
Decision
The daemon is the engine.
klavdholds theUnlockedfromklav-clientand runs the high-level operations: list, show, create, edit, resolve references, TOTP and attachments. The CLI sends it requests and shows the results.- The CLI never receives the vault keys, only the values it asks for.
- Rejected alternative: having the daemon hand the local key to the CLI so the CLI decrypts. It would be simpler, but any process that talks to the daemon would walk away with the key that opens the whole account, not just one value.
Its own protocol, minimal and versioned: line-delimited JSON over the socket. Each message carries
v(version) andid. Operations are anenumwithopanddeny_unknown_fields.- It is documented in
klavd.mdand tested inprotocol.rs: the wire format is fixed. - HTTP over the socket, gRPC and JSON-RPC were rejected: they add dependencies and surface with no gain for a single local client.
- Native messaging (phase 5) also uses JSON, so the same
Requestwill work there.
- It is documented in
Access control:
- the socket is inside
$KLAV_HOME(0700) and has mode 0600; - on top of that, each connection is checked against the peer's UID: Tokio's
peer_cred(), which usesgetpeereidon macOS andSO_PEERCREDon Linux. If the UID isn't the daemon's, the connection is closed.
There is no protection against another process of the same user: with that UID it can read the files in
$KLAV_HOMEand the process memory anyway (out of scope, threat model §6). That line will be crossed by the extension's signature (phase 5) and the approvals (phase 4).- the socket is inside
Locking:
- on idle (
--idle-lock, 15 min by default); - on sleep, detected from the difference between the wall clock and the monotonic clock, with no dependencies and no OS-specific code;
- on screen lock on macOS, with the Touch ID shim (ADR 0013). On Linux, screen lock through logind/D-Bus is still pending.
A single daemon per
KLAV_HOME, with a file lock. It stops if its directory disappears.- on idle (
On-demand start:
klav unlockstartsklavdif it isn't running (in its own process group) and waits for the socket. The other commands use the daemon if it is there, and otherwise direct mode.--no-daemonorKLAV_NO_DAEMON=1force direct mode. To start it with the login session there are launchd and systemd examples inklavd.md.Sync in the daemon (for reads, changed by ADR 0026: they answer from the local copy and sync in the background): before an operation it syncs if the last sync was more than 30 s ago. With no network it answers from the cache with a warning; a rollback is an error (
rollback, the CLI exits with 3; since layer 10 of ADR 0030, only insync: in every other operation it is a warning). It shares session renewal with the CLI in direct mode through thesession.locklock (ADR 0011).
Consequences
- The CLI has two paths (direct and daemon) with the same interface (
Backend). The tests cover both. - Secrets on the socket: the password in
unlock, and the revealed and resolved values in the responses. They travel in plaintext inside the machine, only for the same UID, in buffers that are zeroed.serde_json's internal copies can't be guaranteed (T33).mlockis postponed (ADR 0013). - Old clients: a client with another protocol version gets
bad_requestwith a clear message. Bumping the version means updatingklavandklavdtogether, which ship together.