Create root's trigger files as pi and tighten the root login check #145

Merged
aluminum-ice merged 1 commit from root-boundary-hardening into main 2026-11-01 22:42:13 +00:00
Owner

What this PR does

Tightens what the root helpers do on the device's behalf:

  • Trigger files are created by pi, not root. agent train-now (also used by the dashboard's Train now and the auto-train on an algorithm switch) and remote-train now used to create their request file as root and then chown it. They now go through a new pi_touch helper in kestrel.sh: the file is created as pi (run_as_pi), with O_NOFOLLOW, and is born pi-owned, so nothing is chowned. A missing pi directory is created by pi_dir (install -d -o pi under its root-owned parent). agent import now has root read the model and pi write it into the agent dir (temp file, renamed into place) instead of install -o pi.
  • [plugins.remote-train] agent_dir is terminal-only (root_only), and root's rt_agent_dir uses it only when it is an absolute path under /var/lib/kestrel/.
  • The root login check (plugins-ctl checkauth, behind [web] auth = "system"):
    • accepts only a person's account: uid ≥ 1000, not nobody, with a login shell;
    • runs crypt once in every case (against a fixed dummy setting for an unknown or refused account), so the reply time is the same either way;
    • shares one failure count across every caller, kept root-only in /run/kestrel-auth (O_NOFOLLOW, flock, a 0700 directory it checks). After 5 failures in 10 minutes, checks are spaced out (0.25 s doubling, at most 4 s apart). A check that would have to wait more than 30 s is answered {"ok": false, "error": "busy"} at once. A success clears the count. If the state can't be used, checks go ahead without it rather than lock the owner out.
  • The updates options root's engine reads are terminal-only: beta, timeout and backup_timeout_seconds join repo, host and token as root_only. The Plugins page shows them read-only; sudo kestrel plugins set updates beta=true sets them. Root also clamps what it reads: timeout to 5–120 s, and backup_timeout_seconds to 30–3600 s (0 still means "no bound").

Checklist

  • Lint passes — make lint (ansible --syntax-check, yamllint, shellcheck --severity=error, and the real-data check). The Lint workflow runs this on the PR.
  • Tests pass — make test (the kestrel_pi pytest suite and the real-data check's tests). The Test workflow runs this on the PR.
  • Nothing real: no secrets, no personal information, no real data. This is a public repository. The diff contains no real account passwords or hashes, Wi-Fi passphrases, private keys, API tokens, .env files, real names, personal emails, device serials, and no real captured or device data: SSIDs, MAC addresses, GPS coordinates, capture files, diagnostics bundles, logs, or screenshots of a real device — placeholders only. The real-data check catches MACs, emails, keys and capture files; real SSIDs, coordinates and screenshots are on you.
  • Consent gates intact. Capture and network plugins still ship disabled, enabling them still takes the authorized-use acknowledgment (never pre-ticked or given by code), and the capture whitelist is still enforced.
  • Docs updated in the same PR, if this changes something an operator sees or configures: the README package table, the relevant page under docs/, and the on-device operator guide (GUIDE_MD in kestrel_pi/userguide.py) where it applies.
  • Commit messages are short and imperative.

Release impact

  • App-layer-safe — changes only files the in-place update engine carries: the kestrel_pi package, plugins/*.py, systemd units, the journald drop-in, a lib script already in the engine's LIB_SCRIPTS, or the CLI.
  • Reflash-class — touches a build input the engine does not carry onto a running device: apt or pip packages, boot options / config.txt / cmdline, kernel modules, the base image or partition size, builder/config.json, builder/config.yml, the Makefile, the Release workflow (.forgejo/workflows/main.yml), a shipped config default (config.toml / default.conf), or a brand-new lib script.

Notes for review

  • Files: kestrel.sh (the CLI), plugins-ctl.py and update-apply.py (both in LIB_SCRIPTS), and the updates and remote-train plugins. No new lib script and no unit change: the backoff directory is created by the helper itself.
  • Existing settings are kept. A device that already has beta = true or a custom agent_dir keeps it; only who can change it changes.
  • Testers: turning betas on (or off) is now sudo kestrel plugins set updates beta=true|false. The docs and the on-device guide say so; the next beta checklist will too.
  • Tests:
    • test_root_writes.py (new) runs pi_touch's snippet: it creates a missing file, leaves an existing one, and refuses a planted symlink with the target untouched. It also fails if kestrel.sh gains a root : >, path chown or install -o pi into the agent dir or capture store, and checks those patterns still catch the old code.
    • test_plugins_apply.py: the account policy (root, system accounts, nobody, no-login and unknown accounts are refused, each still costing one crypt); the backoff (free failures, growing spacing, "busy" past 30 s, forgetting after the window, refusing a symlinked state file or a shared directory, the owner still checked when the state is unusable); and the new root-only options refused through the socket.
    • test_update_apply.py: the clamps.
  • Coming next: the dashboard side of the login change (its own backoff on every route, and a "couldn't check, try again" answer for busy) follows in the next PR. Until then the dashboard shows busy as a failed sign-in.
  • Locally: the suite passes except the two tests that depend on this host (the daemon SIGHUP test and the GPS serial-device test); CI runs both.

🤖 Generated with Claude Code

## What this PR does Tightens what the root helpers do on the device's behalf: - **Trigger files are created by `pi`, not root.** `agent train-now` (also used by the dashboard's Train now and the auto-train on an algorithm switch) and `remote-train now` used to create their request file as root and then chown it. They now go through a new `pi_touch` helper in `kestrel.sh`: the file is created *as `pi`* (`run_as_pi`), with `O_NOFOLLOW`, and is born pi-owned, so nothing is chowned. A missing pi directory is created by `pi_dir` (`install -d -o pi` under its root-owned parent). `agent import` now has root read the model and `pi` write it into the agent dir (temp file, renamed into place) instead of `install -o pi`. - **`[plugins.remote-train] agent_dir` is terminal-only (`root_only`),** and root's `rt_agent_dir` uses it only when it is an absolute path under `/var/lib/kestrel/`. - **The root login check (`plugins-ctl checkauth`, behind `[web] auth = "system"`):** - accepts only a person's account: uid ≥ 1000, not `nobody`, with a login shell; - runs crypt once in every case (against a fixed dummy setting for an unknown or refused account), so the reply time is the same either way; - shares one failure count across every caller, kept root-only in `/run/kestrel-auth` (`O_NOFOLLOW`, `flock`, a 0700 directory it checks). After 5 failures in 10 minutes, checks are spaced out (0.25 s doubling, at most 4 s apart). A check that would have to wait more than 30 s is answered `{"ok": false, "error": "busy"}` at once. A success clears the count. If the state can't be used, checks go ahead without it rather than lock the owner out. - **The `updates` options root's engine reads are terminal-only:** `beta`, `timeout` and `backup_timeout_seconds` join `repo`, `host` and `token` as `root_only`. The Plugins page shows them read-only; `sudo kestrel plugins set updates beta=true` sets them. Root also clamps what it reads: `timeout` to 5–120 s, and `backup_timeout_seconds` to 30–3600 s (0 still means "no bound"). ## Checklist - [x] **Lint passes** — `make lint` (ansible `--syntax-check`, `yamllint`, `shellcheck --severity=error`, and the real-data check). The **Lint** workflow runs this on the PR. - [x] **Tests pass** — `make test` (the `kestrel_pi` pytest suite and the real-data check's tests). The **Test** workflow runs this on the PR. - [x] **Nothing real: no secrets, no personal information, no real data.** This is a public repository. The diff contains no real account passwords or hashes, Wi-Fi passphrases, private keys, API tokens, `.env` files, real names, personal emails, device serials, and no real captured or device data: SSIDs, MAC addresses, GPS coordinates, capture files, diagnostics bundles, logs, or screenshots of a real device — placeholders only. The real-data check catches MACs, emails, keys and capture files; real SSIDs, coordinates and screenshots are on you. - [x] **Consent gates intact.** Capture and network plugins still ship disabled, enabling them still takes the authorized-use acknowledgment (never pre-ticked or given by code), and the capture whitelist is still enforced. - [x] **Docs updated in the same PR**, if this changes something an operator sees or configures: the README package table, the relevant page under `docs/`, and the on-device operator guide (`GUIDE_MD` in `kestrel_pi/userguide.py`) where it applies. - [x] Commit messages are short and imperative. ## Release impact - [x] **App-layer-safe** — changes only files the in-place update engine carries: the `kestrel_pi` package, `plugins/*.py`, systemd units, the journald drop-in, a lib script already in the engine's `LIB_SCRIPTS`, or the CLI. - [ ] **Reflash-class** — touches a build input the engine does not carry onto a running device: apt or pip packages, boot options / `config.txt` / `cmdline`, kernel modules, the base image or partition size, `builder/config.json`, `builder/config.yml`, the `Makefile`, the Release workflow (`.forgejo/workflows/main.yml`), a shipped config default (`config.toml` / `default.conf`), or a brand-new lib script. ## Notes for review - **Files:** `kestrel.sh` (the CLI), `plugins-ctl.py` and `update-apply.py` (both in `LIB_SCRIPTS`), and the `updates` and `remote-train` plugins. No new lib script and no unit change: the backoff directory is created by the helper itself. - **Existing settings are kept.** A device that already has `beta = true` or a custom `agent_dir` keeps it; only who can change it changes. - **Testers:** turning betas on (or off) is now `sudo kestrel plugins set updates beta=true|false`. The docs and the on-device guide say so; the next beta checklist will too. - **Tests:** - `test_root_writes.py` (new) runs `pi_touch`'s snippet: it creates a missing file, leaves an existing one, and refuses a planted symlink with the target untouched. It also fails if `kestrel.sh` gains a root `: >`, path `chown` or `install -o pi` into the agent dir or capture store, and checks those patterns still catch the old code. - `test_plugins_apply.py`: the account policy (root, system accounts, nobody, no-login and unknown accounts are refused, each still costing one crypt); the backoff (free failures, growing spacing, "busy" past 30 s, forgetting after the window, refusing a symlinked state file or a shared directory, the owner still checked when the state is unusable); and the new root-only options refused through the socket. - `test_update_apply.py`: the clamps. - **Coming next:** the dashboard side of the login change (its own backoff on every route, and a "couldn't check, try again" answer for `busy`) follows in the next PR. Until then the dashboard shows `busy` as a failed sign-in. - **Locally:** the suite passes except the two tests that depend on this host (the daemon SIGHUP test and the GPS serial-device test); CI runs both. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Create root's trigger files as pi and tighten the root login check
All checks were successful
Test / test (push) Successful in 33s
Lint / lint (push) Successful in 34s
Test / test (pull_request) Successful in 31s
Lint / lint (pull_request) Successful in 34s
092d052bbe
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aluminum-ice deleted branch root-boundary-hardening 2026-11-01 22:42:13 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
aluminum-ice/kestrel!145
No description provided.