From c9d47ce91d5b2bd0416d5bec8b14e4d57fd63f62 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 12 Oct 2026 21:24:09 +0200 Subject: [PATCH] =?UTF-8?q?chore(memory):=20LRN-018=20+=20journal=20?= =?UTF-8?q?=E2=80=94=20hotfix=20sshd-reload-inactive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/memory/journal.md | 3 + .claude/memory/learnings.md | 12 ++++ .../2026-10-12-sshd-reload-inactive-0103.md | 19 +++++++ .../2026-10-12-sshd-reload-inactive-0103.md | 55 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 .claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md create mode 100644 .claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md diff --git a/.claude/memory/journal.md b/.claude/memory/journal.md index f136dcb..1354800 100644 --- a/.claude/memory/journal.md +++ b/.claude/memory/journal.md @@ -144,3 +144,6 @@ Done: `bind -n C-M-Up/Down/Left/Right select-pane` (root + copy-mode-vi, no vim ## 2026-10-11 — desktop + RDP offered, WSL default no Done: `offer_desktop()` (desktop [Y/n] → RDP [Y/n], WSL → [y/N] + hint), `confirm` default arg, `is_wsl()`. Trigger: WSL install lost DNS mid-run after ubuntu-desktop-minimal (hypothesis: NetworkManager/systemd-resolved rewrote /etc/resolv.conf), fail2ban/whois fetch failed. 8-case stub harness OK, shellcheck clean. Branch feature/desktop-rdp-prompt, commit 5d60269, pushed, not merged. + +## 2026-10-12 — WSL DNS confirmed + sshd hardening abort fixed +Done: WSL DNS root cause confirmed (ubuntu-desktop-minimal → systemd-resolved/NetworkManager rewrote resolv.conf); repair runbook (disable both, rm resolv.conf, `wsl --shutdown`) worked. Then install aborted at harden_sshd: `systemctl reload ssh` on inactive unit + `set -e`. Fix 64f06bb on bugfix/sshd-reload-inactive: `is-active` guard + `try-reload-or-restart`, `/run/sshd` created before `sshd -t` (Ubuntu 24.04: privsep dir exists only while ssh.service runs — challenge correctness MAJOR). Stub smoke both branches, shellcheck, semgrep PASS. Not merged. diff --git a/.claude/memory/learnings.md b/.claude/memory/learnings.md index 462fe10..db9a8cf 100644 --- a/.claude/memory/learnings.md +++ b/.claude/memory/learnings.md @@ -135,3 +135,15 @@ triggering pane as target: `set -pu @opt` needs no `-t`; `#{hook_pane}` quoting testable headless on a scratch pane (`new-window -d -P -F '#{pane_id}'`, assert `#{pane_in_mode}`); address by pane id, window-name plugin renames `-n` names. (5) `\;` inside tmux.conf '...' stays literal, then splits a bind's command list when the string is parsed by if-shell: works, no need to convert to `{ }` blocks. + +## LRN-018 — systemctl under `set -e`: guard reload, create /run/sshd before `sshd -t` +2026-10-12. Found fixing install.sh `harden_sshd` on WSL (commit 64f06bb). (1) Bare `systemctl reload ` +exits non-zero when unit inactive (" is not active, cannot reload.") → `set -e` kills installer. Guard: +`if systemctl is-active --quiet ; then sudo systemctl try-reload-or-restart ; else echo ...; fi`. +`try-reload-or-restart` exits 0 if unit stopped between check and call (no TOCTOU abort); `is-active` needs no +sudo, inside `if` never trips `set -e`. `restart` on inactive unit starts it, different semantic. (2) Ubuntu 24.04 +(systemd 255): `/run/sshd` created only by ssh.service `RuntimeDirectory=sshd`, no tmpfiles rule (systemd ≥ 256 +adds `20-systemd-ssh-generator.conf`). sshd never started → `sudo sshd -t` fails "Missing privilege separation +directory" → looks like config rejection. `sudo install -d -m 0755 /run/sshd` before `sshd -t`, idempotent. +(3) WSL: no sshd running by default, unit socket-activated or never started → any installer "validate + reload" +path must survive inactive unit. Stub `systemctl`/`sudo`/`sshd` on PATH in scratchpad to smoke both branches. diff --git a/.claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md b/.claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md new file mode 100644 index 0000000..1a17b94 --- /dev/null +++ b/.claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md @@ -0,0 +1,19 @@ +# CONTRACT — sshd-reload-inactive +- date: 2026-10-12 | flow: hotfix | branch: bugfix/sshd-reload-inactive +- status: active + +## REQUEST (verbatim — IMMUTABLE) +install.sh harden_sshd: `sudo systemctl reload ssh` fails under set -e when ssh.service is not active (WSL, socket-activated or never started) → "ssh.service is not active, cannot reload." aborts the whole install. Fix: reload only if `systemctl is-active --quiet ssh`, otherwise print that the drop-in is in place and applies at the next sshd start. Keep shellcheck clean, tabs. + +User message: "ok ca a fonctionne, par contre maintenant je bloque a Deploying sshd hardening drop-in +ssh.service is not active, cannot reload.. je sais pas si c'es tpour installer ssh-server ? Si oui demande aussi pour l'installer, si non il bloque pour quoi ?" + +## CLARIFICATIONS +none — request complete (openssh-server is already in the apt list; the block is the unconditional reload) + +## ACCEPTANCE CRITERIA +1. Symptom gone: harden_sshd no longer aborts the install when ssh.service is inactive (reload guarded by `systemctl is-active --quiet ssh`, else an informative echo). +2. Build/tests green: `bash -n install.sh` and `shellcheck install.sh` pass; indentation stays tabs. + +## FILE SCOPE +install.sh (harden_sshd only) diff --git a/.claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md b/.claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md new file mode 100644 index 0000000..3aaeaec --- /dev/null +++ b/.claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md @@ -0,0 +1,55 @@ +# PLAN — sshd-reload-inactive (hotfix) + +## Root cause +`install.sh` runs under `set -euo pipefail`. `harden_sshd()` (lines ~226-236) ends with an +unconditional `sudo systemctl reload ssh`. On WSL (and any host where sshd is socket-activated +or simply not started) ssh.service is inactive, systemctl prints +"ssh.service is not active, cannot reload." and exits non-zero, so `set -e` aborts the whole +install at "Deploying sshd hardening drop-in". openssh-server itself IS installed (apt list, +line ~730); the drop-in has already been installed and validated with `sshd -t` at that point. + +## Exact edit (install.sh, harden_sshd, tabs) — revised after challenge +1. Before `sudo sshd -t`, add one line so the validation also works when ssh.service is + inactive (privsep dir normally created by the unit's RuntimeDirectory; absent on Ubuntu + 24.04 when sshd never ran — challenge correctness MAJOR + robustness MINOR 2): +``` + sudo install -d -m 0755 /run/sshd +``` +2. Replace the unconditional reload +``` + sudo systemctl reload ssh +} +``` +with +``` + if systemctl is-active --quiet ssh; then + sudo systemctl try-reload-or-restart ssh + else + echo "ssh.service not running — drop-in in place, applies at the next sshd start" + fi +} +``` +`try-reload-or-restart` exits 0 if the unit stopped between the check and the call +(robustness MINOR 1); the `is-active` guard stays for the informative echo. +3. Update the function's header comment (one or two lines): /run/sshd is created so `sshd -t` + works without a running sshd; the reload happens only when sshd is running. + +## Smoke (robustness MINOR 3 — exercise the else branch for real) +In the scratchpad, extract `harden_sshd` from install.sh into a temp script with +`SCRIPT_DIR` pointing at the repo, prepend a stub dir to PATH where `systemctl` exits 3 for +`is-active` (and 0 otherwise), `sudo` runs its arguments without privilege, `sshd` exits 0; +run under `set -euo pipefail` and assert exit 0 + the "drop-in in place" echo. Then flip the +stub (`is-active` exits 0) and assert `try-reload-or-restart ssh` is what gets called. +Plus `bash -n install.sh` and `shellcheck install.sh`. + +## Constraints +- No other change; `offer_ssh_memory_guard` uses `systemctl restart ssh`, which starts an inactive + unit and is out of scope. +- shellcheck clean, `bash -n` clean, tabs. + +## Challenge summary +- correctness MAJOR (/run/sshd missing → sshd -t false reject) → closed by edit 1. +- robustness MINOR 1 (check-then-act race) → closed by `try-reload-or-restart`. +- robustness MINOR 2 (same /run/sshd risk) → closed by edit 1. +- robustness MINOR 3 (gate never runs the else path) → closed by the Smoke section. +- simplicity: SOLID, no findings.