From f0b68283fe80c781d6689599afb0561c142473e8 Mon Sep 17 00:00:00 2001 From: onyks-os <129986281+onyks-os@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:24:49 +0100 Subject: [PATCH 1/2] Depend on polkit so the packaged watchdog can heal Tor Every native package creates the ttp-watchdog account, so the watchdog runs as that account and restarts a stopped or crashed ttp-tor.service through polkit (50-ttp-watchdog.rules). Only polkitd reads that rule, and no package depended on it. Measured on Debian 13 (systemd as PID 1, the packaged account and rule, a stand-in ttp-tor.service): without polkitd `systemctl restart ttp-tor.service` as ttp-watchdog is "Access denied"; with polkitd 126 it succeeds, while another unit and another account are still refused. In the watchdog a refused restart is a failed heal, which engages the emergency killswitch: no leak, but a Tor restart left the host without network. The .deb now depends on polkitd, the .rpm and the PKGBUILD on polkit. test_packaging_dependencies.py requires it of all three (it failed before). The package smoke test also requires the account, polkitd and a readable rule; run on the released 0.5.1 .deb it fails, and the new .deb passes on Debian 13 and Ubuntu 24.04. The VM jobs install TTP with pip, where the watchdog runs as root, and the chaos sweep counts a killswitch as contained, so none of them could see this. scripts/vm/lifecycle/heal.sh installs the .deb in a Debian guest, stops ttp-tor and requires a heal without a killswitch, then masks polkit and requires the same fault to end in the killswitch, so the check can fail. It runs as a new job in lifecycle.yml. Co-Authored-By: Claude Opus 5.5 Signed-off-by: onyks-os <129986281+onyks-os@users.noreply.github.com> --- .github/workflows/lifecycle.yml | 45 +++++++++++ CHANGELOG.md | 19 +++++ docs/security-assessment.md | 2 +- docs/web/explanation/watchdog-fsm.md | 6 +- docs/web/how-to/vm-testing.md | 20 ++++- docs/web/reference/dependencies.md | 14 +++- docs/web/release-notes/changelog.md | 19 +++++ packaging/PKGBUILD | 4 +- packaging/build_deb.sh | 2 +- packaging/smoke_test.sh | 4 + packaging/ttp.spec | 3 + scripts/vm/lifecycle/heal.sh | 114 +++++++++++++++++++++++++++ tests/test_packaging_dependencies.py | 20 +++++ 13 files changed, 264 insertions(+), 8 deletions(-) create mode 100755 scripts/vm/lifecycle/heal.sh diff --git a/.github/workflows/lifecycle.yml b/.github/workflows/lifecycle.yml index 1fe47a1..5102288 100644 --- a/.github/workflows/lifecycle.yml +++ b/.github/workflows/lifecycle.yml @@ -30,6 +30,8 @@ on: - "ttp/tor_config.py" - "ttp/virtual_addresses.py" - "ttp/watchdog/**" + - "ttp/resources/polkit/**" + - "packaging/build_deb.sh" - "ttp/vpn.py" - "ttp/vpn_guard.py" - "tests/chaos_monkey.py" @@ -146,6 +148,49 @@ jobs: # - independent, no bypass: no canary either, because its bypass rule is # something the watchdog checks - with it configured, a flushed table was # caught for a reason a normal session does not have (#80). + heal: + # The other jobs install TTP with pip, where the watchdog runs as root. + # This one installs the .deb, so the watchdog runs as ttp-watchdog and + # restarts Tor through polkit, as it does for anyone who installs a package. + name: Watchdog heals Tor as packaged (VM, debian) + runs-on: ubuntu-latest + timeout-minutes: 40 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + + - name: Enable KVM for the runner user + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' \ + | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + + - name: Install QEMU + run: | + sudo apt-get update -q + sudo apt-get install -y -q --no-install-recommends qemu-system-x86 qemu-utils genisoimage + + - name: Build the .deb + run: packaging/build_deb.sh + + - name: Heal Tor in the guest + env: + VM_WORK: ${{ runner.temp }}/vm + run: scripts/vm/lifecycle/heal.sh packaging/transparent-tor-proxy_*_all.deb + + - name: Upload the scenario's logs + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: heal-vm-debian + path: | + ${{ runner.temp }}/vm/results.txt + ${{ runner.temp }}/vm/heal-*.log + ${{ runner.temp }}/vm/cloud-init.txt + if-no-files-found: warn + chaos: name: Watchdog chaos sweep (VM, ${{ matrix.variant }}) runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 4035b8a..70bbed7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- The `.deb`, `.rpm` and PKGBUILD did not depend on polkit. Each creates the + `ttp-watchdog` account, so the watchdog runs as that account and restarts a + stopped or crashed Tor through polkit (`50-ttp-watchdog.rules`), and only + polkit's daemon reads that rule. Without it, systemd answered "Access denied" + (measured on Debian 13), the heal failed, and the watchdog engaged the + emergency killswitch. Nothing leaked, but a Tor restart left the host with no + network until the user intervened. This happens on hosts without polkit, such + as servers and minimal installs; desktops usually have it. The packages now + depend on `polkitd` (Debian/Ubuntu) or `polkit` (Fedora, Arch). + - `ttp start` interrupted with Ctrl-C or SIGTERM before it recorded its session undid nothing and exited `0`. It printed "Signal received, restoring network...", but the nftables table, the DNS overlay and the managed Tor @@ -35,6 +45,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Tests +- `scripts/vm/lifecycle/heal.sh` installs the `.deb` in a Debian 13 VM, where + the watchdog runs as `ttp-watchdog` as it does for anyone who installs a + package. It stops `ttp-tor` and requires the watchdog to restart it without a + killswitch. As a control, the same fault with polkit masked must end in the + killswitch. The other VM jobs install with pip, where the watchdog runs as + root, and the chaos sweep counts a killswitch as contained, so neither could + see the missing dependency below. The package smoke tests now also require + the account, polkitd and a readable rule. + - The lifecycle VM judges its phases with NSE 2.6.0, and the `nse` extra requires `network-sandbox-engine>=2.6.0`. In the guest, NSE's stimulus streams run as the non-bypassed user between two beacons that carry the guest's clocks. diff --git a/docs/security-assessment.md b/docs/security-assessment.md index c346b6b..5940e04 100644 --- a/docs/security-assessment.md +++ b/docs/security-assessment.md @@ -211,7 +211,7 @@ TTP is designed to achieve the following security properties, in order of priori | Threat | STRIDE Category | Description | Mitigation | Residual Risk | | :--- | :--- | :--- | :--- | :--- | | Watchdog process compromise | Elevation of Privilege | An attacker exploits the watchdog to execute arbitrary code with full root privileges | The watchdog drops all root privileges, running under the unprivileged user `ttp-watchdog` with `NoNewPrivileges=yes` and the minimal set of capabilities (`CAP_NET_ADMIN`). | **Low.** The watchdog holds `CAP_NET_ADMIN`, so it can program nftables directly; it has no write access to system directories and cannot run administrative shell commands. | -| Insecure Polkit rules authorization | Tampering | An attacker bypasses authentication checks to start/stop the watchdog service | Polkit rules explicitly restrict starting/stopping `ttp-watchdog.service` to root users and the TTP controller. | **Low.** Standard Polkit controls prevent unauthorized service state tampering. | +| Insecure Polkit rules authorization | Tampering | An attacker bypasses authentication checks to start/stop the watchdog service | `50-ttp-watchdog.rules` grants `manage-units` to the `ttp-watchdog` account alone, and only for `ttp-tor.service` and `ttp-watchdog.service`. Measured on Debian 13 with polkitd 126: `ttp-watchdog` can restart `ttp-tor`, but not another unit, and another account cannot restart `ttp-tor`. The native packages depend on polkit, which reads the rule. | **Low.** Standard Polkit controls prevent unauthorized service state tampering. | ### 3.8 systemd-resolved — DNS Resolution Edge Cases diff --git a/docs/web/explanation/watchdog-fsm.md b/docs/web/explanation/watchdog-fsm.md index 6e44b89..aed6bf0 100644 --- a/docs/web/explanation/watchdog-fsm.md +++ b/docs/web/explanation/watchdog-fsm.md @@ -27,7 +27,11 @@ running `ttp watchdog run`: - as the `ttp-watchdog` account when it exists, with only `CAP_NET_ADMIN` and `NoNewPrivileges=yes`. It can read the session lock but not write it, and it - writes only to its own directory, `/run/ttp/watchdog`; + writes only to its own directory, `/run/ttp/watchdog`. The native packages + create this account. They also depend on polkit, because the watchdog restarts + `ttp-tor.service` through `50-ttp-watchdog.rules`, and without polkitd that + restart is refused. Without the account, as after a pip install, the + watchdog runs as root; - as `Type=notify`: it reports **ready** only after its event subscriptions are in place and its first complete evaluation has finished (`readiness_timeout`, 30 s). `ttp start` waits for that first healthy observation. A watchdog that diff --git a/docs/web/how-to/vm-testing.md b/docs/web/how-to/vm-testing.md index e5e5589..9c73794 100644 --- a/docs/web/how-to/vm-testing.md +++ b/docs/web/how-to/vm-testing.md @@ -153,7 +153,25 @@ purpose. It collects the log and exit code through QEMU Guest Agent over virtio-serial, independently of SSH and the guest's firewall. Failure to retrieve the result remains an error; it never counts as containment. -Both scripts run in CI from `.github/workflows/lifecycle.yml`. See +Both install TTP with pip, so no `ttp-watchdog` account exists and the watchdog +runs as root. `scripts/vm/lifecycle/heal.sh ` installs the `.deb` +instead, as a user would. In that setup the watchdog runs as `ttp-watchdog` and +restarts Tor through polkit. The script runs three checks: + +1. polkitd came with the package; +2. after `systemctl stop ttp-tor`, the watchdog restarted Tor and no killswitch + engaged; +3. as a control, with polkit masked the same fault ends in the killswitch. + +The chaos sweep counts a killswitch as contained, so it cannot tell a healed +Tor from a refused restart; this scenario can: + +```bash +packaging/build_deb.sh +VM_WORK=~/.cache/ttp-heal-vm scripts/vm/lifecycle/heal.sh packaging/transparent-tor-proxy_*_all.deb +``` + +These scripts run in CI from `.github/workflows/lifecycle.yml`. See `docs/security-assessment.md` section 4.3 for what it asserts and what it found. ### Required VPN with real Tor diff --git a/docs/web/reference/dependencies.md b/docs/web/reference/dependencies.md index 305d891..fc0b41a 100644 --- a/docs/web/reference/dependencies.md +++ b/docs/web/reference/dependencies.md @@ -82,11 +82,19 @@ fails if a runtime dependency in `pyproject.toml` is missing from any of them, a CI installs every package in a clean container before a release is signed (`packaging/smoke_test.sh`). +Every package creates the `ttp-watchdog` account, so its watchdog runs as that +account and restarts `ttp-tor.service` through polkit +(`/usr/share/polkit-1/rules.d/50-ttp-watchdog.rules`). The packages therefore +depend on polkit's daemon, which reads that rule. Without it, systemd refuses the +restart, and a stopped Tor makes the watchdog engage the emergency killswitch +instead of healing the session. A pip install has no such account: there the +watchdog runs as root and does not need polkit. + === "Debian / Ubuntu (.deb)" ```text Depends: python3 (>= 3.12), python3-typer, python3-rich (>= 13.7.1), python3-stem, - python3-transitions (>= 0.9.0), nftables, tor, apparmor + python3-transitions (>= 0.9.0), nftables, tor, apparmor, polkitd Suggests: network-manager, wireguard-tools ``` @@ -96,7 +104,7 @@ CI installs every package in a clean container before a release is signed ```text Requires: python3 >= 3.12, python3-typer, python3-rich, python3-stem, python3-six, - nftables, tor, policycoreutils + nftables, tor, policycoreutils, polkit Suggests: NetworkManager, wireguard-tools Provides: bundled(python3dist(transitions)) = 0.9.3 ``` @@ -110,7 +118,7 @@ CI installs every package in a clean container before a release is signed === "Arch Linux (PKGBUILD)" ```text - depends=('python>=3.12' 'python-typer' 'python-rich' 'python-stem' 'python-six' 'nftables' 'tor') + depends=('python>=3.12' 'python-typer' 'python-rich' 'python-stem' 'python-six' 'nftables' 'tor' 'polkit') ``` `transitions` is only in the AUR, so the PKGBUILD bundles it the same way as the diff --git a/docs/web/release-notes/changelog.md b/docs/web/release-notes/changelog.md index df24e89..e1ea588 100644 --- a/docs/web/release-notes/changelog.md +++ b/docs/web/release-notes/changelog.md @@ -9,6 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- The `.deb`, `.rpm` and PKGBUILD did not depend on polkit. Each creates the + `ttp-watchdog` account, so the watchdog runs as that account and restarts a + stopped or crashed Tor through polkit (`50-ttp-watchdog.rules`), and only + polkit's daemon reads that rule. Without it, systemd answered "Access denied" + (measured on Debian 13), the heal failed, and the watchdog engaged the + emergency killswitch. Nothing leaked, but a Tor restart left the host with no + network until the user intervened. This happens on hosts without polkit, such + as servers and minimal installs; desktops usually have it. The packages now + depend on `polkitd` (Debian/Ubuntu) or `polkit` (Fedora, Arch). + - `ttp start` interrupted with Ctrl-C or SIGTERM before it recorded its session undid nothing and exited `0`. It printed "Signal received, restoring network...", but the nftables table, the DNS overlay and the managed Tor @@ -30,6 +40,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Tests +- `scripts/vm/lifecycle/heal.sh` installs the `.deb` in a Debian 13 VM, where + the watchdog runs as `ttp-watchdog` as it does for anyone who installs a + package. It stops `ttp-tor` and requires the watchdog to restart it without a + killswitch. As a control, the same fault with polkit masked must end in the + killswitch. The other VM jobs install with pip, where the watchdog runs as + root, and the chaos sweep counts a killswitch as contained, so neither could + see the missing dependency below. The package smoke tests now also require + the account, polkitd and a readable rule. + - The lifecycle VM judges its phases with NSE 2.6.0, and the `nse` extra requires `network-sandbox-engine>=2.6.0`. In the guest, NSE's stimulus streams run as the non-bypassed user between two beacons that carry the guest's clocks. diff --git a/packaging/PKGBUILD b/packaging/PKGBUILD index 0278bb1..7ba38b0 100644 --- a/packaging/PKGBUILD +++ b/packaging/PKGBUILD @@ -49,7 +49,9 @@ license=('MIT') optdepends=('networkmanager: required VPN mode' 'wireguard-tools: required VPN mode' 'apparmor: required for managed Tor when the kernel selects AppArmor') -depends=('python>=3.12' 'python-typer' 'python-rich' 'python-stem' 'python-six' 'nftables' 'tor') +# polkit: its daemon reads 50-ttp-watchdog.rules, which lets ttp-watchdog +# restart ttp-tor; without it the watchdog's Tor heal is refused. +depends=('python>=3.12' 'python-typer' 'python-rich' 'python-stem' 'python-six' 'nftables' 'tor' 'polkit') # Build dependencies. These are only needed temporarily while creating the package. # python-hatchling is the build backend; with --no-isolation it must be installed. diff --git a/packaging/build_deb.sh b/packaging/build_deb.sh index e8c32a2..8ad5f6f 100755 --- a/packaging/build_deb.sh +++ b/packaging/build_deb.sh @@ -146,7 +146,7 @@ Section: net Priority: optional Homepage: https://github.com/onyks-os/TransparentTorProxy License: MIT -Depends: python3 (>= 3.12), python3-typer, python3-rich (>= 13.7.1), python3-stem, python3-transitions (>= 0.9.0), nftables, tor, apparmor +Depends: python3 (>= 3.12), python3-typer, python3-rich (>= 13.7.1), python3-stem, python3-transitions (>= 0.9.0), nftables, tor, apparmor, polkitd Suggests: network-manager, wireguard-tools Description: Transparent Tor Proxy A Linux CLI tool that transparently routes all system traffic through the Tor network. diff --git a/packaging/smoke_test.sh b/packaging/smoke_test.sh index 18c8b02..a61e7f7 100755 --- a/packaging/smoke_test.sh +++ b/packaging/smoke_test.sh @@ -39,6 +39,10 @@ python3 -c "import sys; from ttp.watchdog.fsm import load_machine; m = load_mach # And everything ttp stop imports must import even so. python3 -c "import ttp.commands.lifecycle, ttp.commands.stop_restart, ttp.watchdog" echo "ok: teardown modules import" +getent passwd ttp-watchdog > /dev/null || { echo "FAIL: no ttp-watchdog account"; exit 1; } +test -x /usr/lib/polkit-1/polkitd || { echo "FAIL: polkitd is not installed, so the watchdog cannot restart ttp-tor"; exit 1; } +test -r /usr/share/polkit-1/rules.d/50-ttp-watchdog.rules || { echo "FAIL: no readable 50-ttp-watchdog.rules"; exit 1; } +echo "ok: ttp-watchdog account, polkitd and its rule" ' stage="$(mktemp -d)" diff --git a/packaging/ttp.spec b/packaging/ttp.spec index 231bd1d..f8c71f9 100644 --- a/packaging/ttp.spec +++ b/packaging/ttp.spec @@ -48,6 +48,9 @@ Requires: tor Suggests: NetworkManager Suggests: wireguard-tools Requires: policycoreutils +# polkitd reads 50-ttp-watchdog.rules, which lets ttp-watchdog restart ttp-tor; +# without it the watchdog's Tor heal is refused and it trips the killswitch. +Requires: polkit Requires: python3-six Provides: bundled(python3dist(transitions)) = @@TRANSITIONS_VERSION@@ diff --git a/scripts/vm/lifecycle/heal.sh b/scripts/vm/lifecycle/heal.sh new file mode 100755 index 0000000..3299242 --- /dev/null +++ b/scripts/vm/lifecycle/heal.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash +# Copyright (c) 2026 onyks-os +# SPDX-License-Identifier: MIT +# +# The watchdog heals Tor as the native package installs it. +# +# The other VM scenarios install TTP with pip, where no ttp-watchdog account +# exists and the watchdog runs as root. Every native package creates that +# account, so the packaged watchdog restarts ttp-tor.service as ttp-watchdog, +# which systemd allows only through polkit (50-ttp-watchdog.rules, read by +# polkitd). Up to 0.5.1 no package depended on polkit: without polkitd the +# restart is refused and the watchdog trips the emergency killswitch. The chaos +# sweep counts a killswitch as contained, so it cannot see this. +# +# VM_WORK=~/.cache/ttp-heal-vm scripts/vm/lifecycle/heal.sh packaging/transparent-tor-proxy_*.deb +# +# 1. Install the .deb with apt (its dependencies included), start a session +# with the watchdog, and require the watchdog unit to run as ttp-watchdog. +# 2. Stop ttp-tor. Pass when the watchdog restarted it and no killswitch is in +# place. +# 3. Control: mask polkit and stop ttp-tor again. The killswitch must engage +# and the watchdog must log the refused restart. Otherwise step 2 could not +# have failed, and the run is void. +# +# Writes results.txt and heal-watchdog.log into $VM_WORK. Needs what run.sh +# needs, a Debian guest (VM_DISTRO=debian) and no root. + +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +deb="$(cd "$(dirname "${1:?usage: heal.sh }")" && pwd)/$(basename "$1")" +[ -f "$deb" ] || { echo "no such package: $1" >&2; exit 2; } +: "${VM_WORK:?set VM_WORK to a working directory with a few GB free}" +[ "${VM_DISTRO:-debian}" = debian ] || { echo "heal.sh installs a .deb: VM_DISTRO must be debian" >&2; exit 2; } +mkdir -p "$VM_WORK" +# shellcheck source=scripts/vm/lifecycle/vm.sh +source "$HERE/vm.sh" + +RESULTS="$VM_WORK/results.txt" +: >"$RESULTS" +failed=0 +result() { # PASS|FAIL name reason + printf '%-4s %-45s %s\n' "$1" "$2" "$3" | tee -a "$RESULTS" + [ "$1" = PASS ] || failed=1 +} + +# Through QGA: the killswitch drops inbound traffic, SSH included. +killswitch() { vm_guest 'nft list table inet ttp 2>/dev/null' | grep -q 'chain filter_input'; } +tor_active() { [ "$(vm_guest 'systemctl is-active ttp-tor' || true)" = active ]; } +watchdog_said() { vm_guest "journalctl -u ttp-watchdog -o cat --no-pager" | grep -qF "$1"; } + +trap 'vm_guest "journalctl -u ttp-watchdog -o cat --no-pager" >"$VM_WORK/heal-watchdog.log" 2>&1 || true; vm_kill' EXIT + +vm_kill +vm_fetch_image +vm_prepare +vm_boot +vm_wait_ready 600 + +vm_ssh 'cat >/tmp/ttp.deb' <"$deb" +vm_ssh 'sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq /tmp/ttp.deb' \ + >"$VM_WORK/heal-install.log" 2>&1 || { tail -20 "$VM_WORK/heal-install.log"; exit 1; } +vm_log "installed $(basename "$deb"); account: $(vm_ssh 'getent passwd ttp-watchdog' || echo none)" +vm_ssh 'test -x /usr/lib/polkit-1/polkitd' || { result FAIL "polkitd installed with the package" "absent"; exit 1; } +result PASS "polkitd installed with the package" "/usr/lib/polkit-1/polkitd" + +vm_ssh 'sudo ttp start --watchdog --bootstrap-timeout 300' >"$VM_WORK/heal-start.log" 2>&1 || + { tail -20 "$VM_WORK/heal-start.log"; vm_log "ttp start failed"; exit 1; } +deadline=$((SECONDS + 60)) +until [ "$(vm_ssh 'systemctl is-active ttp-watchdog' || true)" = active ]; do + [ "$SECONDS" -lt "$deadline" ] || { result FAIL "watchdog runs as ttp-watchdog" "unit not active"; exit 1; } + sleep 2 +done +user="$(vm_ssh 'systemctl show ttp-watchdog -p User --value')" +if [ "$user" = ttp-watchdog ]; then + result PASS "watchdog runs as ttp-watchdog" "User=$user" +else + result FAIL "watchdog runs as ttp-watchdog" "User=${user:-root}: this scenario would not test polkit" + exit 1 +fi + +# 2. A Tor stop the watchdog must heal: one check interval (15 s) plus the restart. +vm_ssh 'sudo systemctl stop ttp-tor' +vm_log "ttp-tor stopped; waiting for the watchdog" +deadline=$((SECONDS + 90)) +until watchdog_said "Restarted Tor service successfully" && tor_active; do + if killswitch; then + result FAIL "watchdog heals a stopped Tor" "killswitch engaged instead" + exit 1 + fi + [ "$SECONDS" -lt "$deadline" ] || { result FAIL "watchdog heals a stopped Tor" "no restart within 90 s"; exit 1; } + sleep 3 +done +sleep 5 # the heal is logged before the next check could still trip the killswitch +if killswitch; then + result FAIL "watchdog heals a stopped Tor" "restarted, then the killswitch engaged" + exit 1 +fi +result PASS "watchdog heals a stopped Tor" "ttp-tor restarted by ttp-watchdog, no killswitch" + +# 3. Control: the same fault without polkit must end in the killswitch. +vm_ssh 'sudo systemctl mask --now polkit.service && sudo systemctl stop ttp-tor' +vm_log "polkit masked and ttp-tor stopped again; waiting for the killswitch" +deadline=$((SECONDS + 90)) +until killswitch; do + [ "$SECONDS" -lt "$deadline" ] || break + sleep 3 +done +if killswitch && watchdog_said "Auto-healing command failed for 'tor'"; then + result PASS "control: without polkit the heal is refused" "killswitch engaged, restart refused" +else + result FAIL "control: without polkit the heal is refused" "no killswitch or no refused restart: the check above proves nothing" +fi + +exit "$failed" diff --git a/tests/test_packaging_dependencies.py b/tests/test_packaging_dependencies.py index 043efc6..5d8bcac 100644 --- a/tests/test_packaging_dependencies.py +++ b/tests/test_packaging_dependencies.py @@ -180,3 +180,23 @@ def test_the_indirect_pins_are_ubuntu_24_04s_too(): does not see them; their pins must still be the oldest distribution's.""" assert OLDEST["six"] == "1.16.0" assert OLDEST["click"] == "8.1.6" + + +#: The package that provides polkitd, the daemon that reads .rules files, per format. +POLKIT_DAEMON = {"deb": "polkitd", "rpm": "polkit", "arch": "polkit"} +POLKIT_RULE = "50-ttp-watchdog.rules" + + +def test_every_package_that_ships_the_polkit_rule_depends_on_polkitd(): + """The rule is read by polkitd alone; without it the rule does nothing. + + Every native package creates the ttp-watchdog account, so the watchdog runs + as that user and restarts ttp-tor.service through polkit. Without polkitd, + systemd answers "Access denied" (measured on Debian 13), the heal fails and + the watchdog trips the emergency killswitch: a Tor restart becomes a host + with no network. None of the three packages depended on polkit up to 0.5.1. + """ + assert POLKIT_RULE in BUILD_DEB and POLKIT_RULE in SPEC and POLKIT_RULE in PKGBUILD + assert POLKIT_DAEMON["deb"] in _deb_depends(), "the .deb ships the polkit rule but does not depend on polkitd" + assert POLKIT_DAEMON["rpm"] in _spec_requires(), "the .rpm ships the polkit rule but does not require polkit" + assert POLKIT_DAEMON["arch"] in _arch_depends(), "the PKGBUILD ships the polkit rule but does not depend on polkit" From 914b6f4cc0cc91f42a2bae7418fd785af4d2d008 Mon Sep 17 00:00:00 2001 From: onyks-os <129986281+onyks-os@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:39:06 +0100 Subject: [PATCH 2/2] Judge the heal by what the confined watchdog actually logs The first run of heal.sh waited for "Restarted Tor service successfully" and timed out. That line is logged at INFO, and the watchdog running as ttp-watchdog cannot open root's log file, so only WARNING and above reach its journal: the run shows "Initiating auto-healing for failed component 'tor'" and no error after it. The heal is now judged by: the watchdog's healing line, a new "Started ttp-tor.service" from systemd, ttp-tor active, and, past the next 15 s check, no killswitch and neither "Failed to restart Tor service" nor "Local integrity failure in tor". The control looks for the refused restart that integrity.py logs on the supervisor's path, not the FSM's message, which that path never emits. The journals of ttp-tor and polkit are kept with the logs. Co-Authored-By: Claude Opus 5.5 Signed-off-by: onyks-os <129986281+onyks-os@users.noreply.github.com> --- scripts/vm/lifecycle/heal.sh | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/scripts/vm/lifecycle/heal.sh b/scripts/vm/lifecycle/heal.sh index 3299242..ccc406b 100755 --- a/scripts/vm/lifecycle/heal.sh +++ b/scripts/vm/lifecycle/heal.sh @@ -46,9 +46,21 @@ result() { # PASS|FAIL name reason # Through QGA: the killswitch drops inbound traffic, SSH included. killswitch() { vm_guest 'nft list table inet ttp 2>/dev/null' | grep -q 'chain filter_input'; } tor_active() { [ "$(vm_guest 'systemctl is-active ttp-tor' || true)" = active ]; } -watchdog_said() { vm_guest "journalctl -u ttp-watchdog -o cat --no-pager" | grep -qF "$1"; } +# The confined watchdog cannot open root's log file, so only WARNING and above +# reach its journal: a successful heal logs nothing there. These are the lines +# that do appear (ttp/watchdog/supervisor.py and integrity.py). +watchdog_said() { vm_guest "journalctl -u ttp-watchdog -o cat --no-pager" | grep -cF "$1" || true; } +HEALING="Initiating auto-healing for failed component 'tor'" +REFUSED="Failed to restart Tor service" +UNHEALED="Local integrity failure in tor" +tor_starts() { vm_guest "journalctl -u ttp-tor -o cat --no-pager" | grep -c '^Started ttp-tor.service' || true; } -trap 'vm_guest "journalctl -u ttp-watchdog -o cat --no-pager" >"$VM_WORK/heal-watchdog.log" 2>&1 || true; vm_kill' EXIT +# shellcheck disable=SC2329 # collect runs from the EXIT trap +collect() { + vm_guest "journalctl -u ttp-watchdog --no-pager" >"$VM_WORK/heal-watchdog.log" 2>&1 || true + vm_guest "journalctl -u ttp-tor -u polkit --no-pager" >"$VM_WORK/heal-tor.log" 2>&1 || true +} +trap 'collect; vm_kill' EXIT vm_kill vm_fetch_image @@ -79,21 +91,28 @@ else fi # 2. A Tor stop the watchdog must heal: one check interval (15 s) plus the restart. +starts_before="$(tor_starts)" vm_ssh 'sudo systemctl stop ttp-tor' -vm_log "ttp-tor stopped; waiting for the watchdog" +vm_log "ttp-tor stopped (started $starts_before times so far); waiting for the watchdog" deadline=$((SECONDS + 90)) -until watchdog_said "Restarted Tor service successfully" && tor_active; do +until [ "$(watchdog_said "$HEALING")" -ge 1 ] && [ "$(tor_starts)" -gt "$starts_before" ] && tor_active; do if killswitch; then result FAIL "watchdog heals a stopped Tor" "killswitch engaged instead" exit 1 fi - [ "$SECONDS" -lt "$deadline" ] || { result FAIL "watchdog heals a stopped Tor" "no restart within 90 s"; exit 1; } + [ "$SECONDS" -lt "$deadline" ] || { result FAIL "watchdog heals a stopped Tor" "no heal and restart within 90 s"; exit 1; } sleep 3 done -sleep 5 # the heal is logged before the next check could still trip the killswitch +sleep 20 # past the next 15 s check, which re-evaluates the healed session if killswitch; then result FAIL "watchdog heals a stopped Tor" "restarted, then the killswitch engaged" exit 1 +elif [ "$(watchdog_said "$REFUSED")" -gt 0 ] || [ "$(watchdog_said "$UNHEALED")" -gt 0 ]; then + result FAIL "watchdog heals a stopped Tor" "Tor is up, but the watchdog logged a failed heal" + exit 1 +elif ! tor_active; then + result FAIL "watchdog heals a stopped Tor" "ttp-tor went down again" + exit 1 fi result PASS "watchdog heals a stopped Tor" "ttp-tor restarted by ttp-watchdog, no killswitch" @@ -105,7 +124,7 @@ until killswitch; do [ "$SECONDS" -lt "$deadline" ] || break sleep 3 done -if killswitch && watchdog_said "Auto-healing command failed for 'tor'"; then +if killswitch && [ "$(watchdog_said "$REFUSED")" -gt 0 ]; then result PASS "control: without polkit the heal is refused" "killswitch engaged, restart refused" else result FAIL "control: without polkit the heal is refused" "no killswitch or no refused restart: the check above proves nothing"