Skip to content

docs(adr): ADR-0043 — systemd Type=notify service, first-party readiness protocol, reference unit (Proposed) - #197

Merged
gonzafg2 merged 4 commits into
mainfrom
docs/adr-0043-systemd
Oct 13, 2026
Merged

gonzafg2 merged 4 commits into
mainfrom
docs/adr-0043-systemd

Conversation

@gonzafg2

Copy link
Copy Markdown
Owner

Summary

ADR-0043 decides how QuantumSSH runs under systemd, the last Phase-2 workstream before the 0.1.0 freeze (#109; the scoping note places it in the ADR lane).

  • Readiness protocol, first-party. The binary sends READY=1 once its port accepts connections and its signal handlers are installed. At the first SIGTERM/SIGINT it sends STOPPING=1 with EXTEND_TIMEOUT_USEC= set to the drain plus 5 s. Each message is one AF_UNIX datagram over std's UnixDatagram.
    • There is no libsystemd, no crate and no unsafe.
    • This follows OpenSSH, which dropped the libsystemd link after CVE-2024-3094 and has used a standalone implementation since 9.8p1.
  • Reference unit contrib/systemd/quantumssh.service:
    • Type=notify and NotifyAccess=main, written out because it is load-bearing: sessions are processes of the unit.
    • User=quantumssh, per ADR-0016.
    • KillMode=mixed: ADR-0028's drain runs unchanged, and processes that escaped their channel are killed at stop.
    • Restart=on-failure and NoNewPrivileges=yes.
  • What the unit leaves out, and why:
    • No capability for port 22. An ambient capability is inherited by every session, so the guide recommends an nftables redirect.
    • No sandboxing directives. Each one would also confine every session, which is a Phase-3 or operator decision.
    • Socket activation, DynamicUser= and the watchdog are rejected or deferred, with reasons.
  • Operator guide docs/deployment.md. It also writes the operator guidance ADR-0016 left open.
  • Verification in CI. The reference unit runs under a real systemd, inside a container built from the interop gate's pinned Debian image. The runner's OpenSSH 9.6 lacks the hybrid KEX, which is why a container is needed.

The ADR stays Proposed. The implementing PR (stacked on this one) flips it to Accepted.

Refs #109

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that changes existing behaviour)
  • Documentation only
  • Refactor / chore / CI / build

Changes

  • docs/adr/0043-systemd-notify-service-and-reference-unit.md (new, Proposed).

Evidence behind the decision

  • systemd v262 sources:
    • man/sd_notify.xml: address forms, "Standalone Implementations", and the READY=1, STOPPING=1 and EXTEND_TIMEOUT_USEC= entries;
    • man/notify-selfcontained-example.py: "guaranteed to be stable";
    • man/systemd.kill.xml: KillMode=mixed;
    • man/systemd.service.xml: Type=notify forces NotifyAccess=main;
    • man/systemd.exec.xml: NoNewPrivileges= and AmbientCapabilities=;
    • meson_options.txt: the 90 s default stop timeout.
  • src/core/service.c at v262:
    • :5516 handles STOPPING=1 before :5746 handles EXTEND_TIMEOUT_USEC=, so one datagram is enough;
    • :436: the extension never shortens the timer;
    • :2714: stop-by-notify sends no signal.
  • OpenSSH: openbsd-compat/port-linux.c:133-188 at V_10_6_P1, and commit 08f5792 (2024-04-03, "Standalone implementation that does not depend on libsystemd"). It is absent at V_9_7_P1 and present at V_9_8_P1.
  • Checked against a real systemd (257.13, Debian trixie) in the implementing PR's container:
    • with TimeoutStopSec=2 and a 4 s drain, the stop ends with Result=success;
    • with a mutant binary that sends STOPPING=1 without the extension, the same stop ends with State 'stop-sigterm' timed out. Killing. and Result=timeout;
    • with KillMode=control-group, sessions die at once and the drain is lost (stopped after 0 s).

Checklist

  • My commits are signed off under the Developer Certificate of Origin (git commit -s).
  • My commit messages follow Conventional Commits.
  • I have run cargo fmt --all and cargo clippy --workspace --all-targets -- -D warnings locally. (Documentation only.)
  • I have added or updated tests where appropriate. (Documentation only; the tests are in the implementing PR.)
  • I have updated documentation (rustdoc, docs/, or README.md) where appropriate.
  • I have added a CHANGELOG.md entry under ## [Unreleased] if this change is user-visible. (Not user-visible; the implementing PR adds it.)
  • If this change introduces a new dependency, I have justified it in the PR description and verified it passes cargo deny check. (No new dependency.)

Security considerations

  • No new code path, dependency or unsafe. The decision explicitly rejects linking libsystemd (the CVE-2024-3094 path) and socket activation, which would need FromRawFd on the listener.
  • NotifyAccess=main keeps sessions, which are processes of the unit, from driving the service manager.
  • NoNewPrivileges=yes removes setuid/setgid elevation from every session.
  • No ambient capability, which every session would inherit.
  • Sandboxing is left to the operator and documented per directive, because it confines sessions too (threat model §8.8; per-user confinement is Phase 3, design: tracking issue for §8.12 closure — Phase 3 privilege separation RFC #43).

Automated reviews

PRs authored by dependabot[bot] (and other bots) do not trigger opencode or Claude Code Review. Codex reviews through the Codex cloud GitHub App (ADR-0037).
They are covered by CI (commit-lint, build (ubuntu-latest) and build (macos-latest) — fmt, clippy, tests, release build —, cargo deny, openssh-interop; cargo audit runs weekly, not per PR) and manual review when needed.

https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW

…ess protocol, reference unit (Proposed)

- The binary sends READY=1 once listening and STOPPING=1 with
  EXTEND_TIMEOUT_USEC (drain + 5 s) at the first SIGTERM/SIGINT, as one
  AF_UNIX datagram over std's UnixDatagram: no libsystemd, no crate, no
  unsafe (as OpenSSH 9.8p1 did after CVE-2024-3094).
- Reference unit contrib/systemd/quantumssh.service: Type=notify,
  NotifyAccess=main, User=quantumssh, KillMode=mixed (ADR-0028's drain
  unchanged; cgroup leftovers killed at stop), Restart=on-failure,
  NoNewPrivileges=yes; no capability for port 22 and no sandboxing that
  would also confine sessions.
- docs/deployment.md is the operator guide; CI runs the unit under real
  systemd on the Ubuntu runner.

Refs #109

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>
@gonzafg2 gonzafg2 added the documentation Improvements or additions to documentation label Oct 13, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T04:19:33.303767Z 5a48d3a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: 3dca9dd7-5bf6-4f2b-9b80-046616f0fc59)

github run

@claude

claude Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5a48d3ac2b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…s in $HOME (RFC-0002, #199)

Codex on #197: under the unit the working directory is `/`, and the
server did not set the children's. #199 fixes that in the server, for
every deployment; the unit needs no WorkingDirectory=.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>
@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: b8c0c2f4-1eb2-47c8-a5df-77c19eddb9cd)

github run

@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: 04df71ac-1b93-4d67-883b-99e623a1b18f)

github run

gonzafg2 added a commit that referenced this pull request Oct 13, 2026
…-0002) (#199)

RFC-0002's execution contract sets the child's cwd to the service
account's $HOME, but spawn and spawn_pty never set a working directory:
every exec and shell inherited the directory the server was started
from — `/` under systemd, the crate directory in the tests.

Both now call current_dir with the server's own HOME (the value the
sanitised environment passes on), falling back to `/` when HOME is
unset, relative or not a directory.

Tests: `pwd -P` on pipes (accept.rs) and on a PTY (pty.rs) returns the
canonical HOME; both fail without the fix (they print the crate
directory).

Found by Codex on #197.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW

Signed-off-by: Gonzalo Fleming <[email protected]>
@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: 58f37e95-e4e4-4ab1-bd8a-3978a47a2e85)

github run

@gonzafg2
gonzafg2 merged commit 91d2a8c into main Oct 13, 2026
10 of 11 checks passed
@gonzafg2
gonzafg2 deleted the docs/adr-0043-systemd branch October 13, 2026 06:07
gonzafg2 added a commit that referenced this pull request Oct 13, 2026
Resolves the conflicts with #197 (squashed ADR-0043: keep this branch's
Accepted status and Links), #201 (Server::serve: its # Errors doc with
this branch's loop_rx signature) and the CHANGELOG Fixed section.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>
gonzafg2 added a commit that referenced this pull request Oct 13, 2026
…de (ADR-0043) (#198)

* docs(adr): ADR-0043 — systemd Type=notify service, first-party readiness protocol, reference unit (Proposed)

- The binary sends READY=1 once listening and STOPPING=1 with
  EXTEND_TIMEOUT_USEC (drain + 5 s) at the first SIGTERM/SIGINT, as one
  AF_UNIX datagram over std's UnixDatagram: no libsystemd, no crate, no
  unsafe (as OpenSSH 9.8p1 did after CVE-2024-3094).
- Reference unit contrib/systemd/quantumssh.service: Type=notify,
  NotifyAccess=main, User=quantumssh, KillMode=mixed (ADR-0028's drain
  unchanged; cgroup leftovers killed at stop), Restart=on-failure,
  NoNewPrivileges=yes; no capability for port 22 and no sandboxing that
  would also confine sessions.
- docs/deployment.md is the operator guide; CI runs the unit under real
  systemd on the Ubuntu runner.

Refs #109

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* feat(systemd): Type=notify readiness, reference unit and operator guide (ADR-0043)

- notify.rs (binary): the sd_notify datagram protocol on std's
  UnixDatagram, path and Linux abstract addresses; no libsystemd, no
  crate, no unsafe. READY=1 after the listener is bound and the
  SIGTERM/SIGINT handlers are installed; STOPPING=1 with
  EXTEND_TIMEOUT_USEC (drain + 5 s) at the first signal.
- The signal handlers are now installed before the listener task is
  spawned, so a stop that follows READY=1 always drains.
- contrib/systemd/quantumssh.service: Type=notify, NotifyAccess=main,
  User=quantumssh, KillMode=mixed, Restart=on-failure,
  NoNewPrivileges=yes.
- docs/deployment.md: service account, StrictModes file layout, config,
  port 22 (nftables redirect, or the capability and its cost), logs,
  stop/restart/reload, and what each sandboxing directive does to
  sessions.
- tests/systemd: the reference unit under a real systemd, in a
  container built from the interop gate's pinned Debian image; run by a
  new step in build (ubuntu-latest).
- Unit tests for the sender and an integration test that runs the
  binary with NOTIFY_SOCKET.

Refs #109

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* docs(adr): accept ADR-0043 with its implementation; README and Phase-2 scoping mark systemd landed

- ADR-0043 flips to Accepted with this implementing PR (#198); the date
  assumes a merge on 2026-10-13 (adjust if merged another day). Links
  name the implementing files.
- README's Phase-2 roadmap and the scoping note's workstream row and
  ordering mark systemd integration as landed.

Refs #109

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* fix(exec): start session children in the service account's $HOME (RFC-0002)

RFC-0002's execution contract sets the child's cwd to the service
account's $HOME, but spawn and spawn_pty never set a working directory:
every exec and shell inherited the directory the server was started
from — `/` under systemd, the crate directory in the tests.

Both now call current_dir with the server's own HOME (the value the
sanitised environment passes on), falling back to `/` when HOME is
unset, relative or not a directory.

Tests: `pwd -P` on pipes (accept.rs) and on a PTY (pty.rs) returns the
canonical HOME; both fail without the fix (they print the crate
directory).

Found by Codex on #197.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* docs(adr): ADR-0043 — no WorkingDirectory=; the server starts sessions in $HOME (RFC-0002, #199)

Codex on #197: under the unit the working directory is `/`, and the
server did not set the children's. #199 fixes that in the server, for
every deployment; the unit needs no WorkingDirectory=.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* test(systemd): the reference-unit check asserts the session runs in $HOME

systemd starts the server in `/`; with #199 merged in, a session's
`pwd -P` is the account's home. The check fails with the pre-#199
binary and passes with it.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* fix(server): the accept loop's shutdown receiver exists before any signal

Codex on #198: serve subscribed its receiver when its future first ran,
and the binary dropped the channel's initial receiver. A broadcast only
reaches receivers that exist when it is sent, so a SIGTERM delivered
right after READY=1 — a stop job systemd queued during the start — could
be broadcast before serve subscribed, and the drain deadline was lost.

Server::serve now takes the loop's receiver: the one broadcast::channel
returns, alive from the channel's creation. The binary and the test
support pass it instead of dropping it.

Test: shutdown_sent_before_serve_first_runs_is_not_lost sends the
deadline before the spawned serve is first polled; with the old
subscribe-on-first-run it fails (SendError: no receiver).

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* docs(deployment): no SFTP in the guide until it lands (#196)

Codex on #198: SFTP is not on main yet, so the guide must not present it
as available. Sessions start in the home; ProtectSystem=strict's row
names every write instead of SFTP uploads.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

* docs(adr): ADR-0043 §4 — ProtectSystem=strict example without SFTP, which is not on main yet

Codex on #198 (the same text is in the guide).

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>

---------

Signed-off-by: Gonzalo Fleming <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant