Skip to content

power-guard: root daemon runs the env-derived POWER_GUARD_SLEEP_CMD through shell=True — parse to argv and drop the shell #9602

Closed

Description

@dezren39

Finding (verified 2026-11-13, Scheduled Suggestions pass on issue #8324)

nixos/services/power-guard/power-guard.py executes an environment-derived command string through a shell, as root:

  • Line 22 (module docstring): "Runs as root in production (systemd unit from the NixOS flake)."
  • Line 94: SLEEP_CMD = os.environ.get("POWER_GUARD_SLEEP_CMD", "systemctl hybrid-sleep")
  • Line 231 (in main(), after a confirmed critical state): subprocess.run(SLEEP_CMD, shell=True)

The systemd unit in nixos/services/power-guard/default.nix sets no environment, so today the variable is only a test/manual hook (test_power_guard.py line 179 sets POWER_GUARD_SLEEP_CMD=f"touch {self.marker}"). The env source is therefore service/root-controlled, not remote input — this is a hardening item, not a live exploit path. It still matters:

  • A root daemon should not need a shell to run a two-word command; shell=True turns any future wiring of this variable to a less-trusted source (a NixOS option, a drop-in, a test harness running unprivileged input) into root shell execution with no further code change.
  • Shell parsing of the override is a surprise surface: quoting/metacharacters in an otherwise legitimate multi-word command change its meaning silently.

Suggested fix shape

  • Parse once at startup with shlex.split, validate the result is a non-empty argv, and run subprocess.run(argv) with no shell — mirroring the file's existing fail-closed-at-startup treatment of timing overrides (timing_from_env, lines 71-83).
  • Update the test hook usage in test_power_guard.py (line 179) accordingly; touch <marker> still works as an argv form.
  • Alternatively, if shell semantics are intentionally kept for the test hook, restrict the production default path to a fixed argv and document why the hook is trusted.

Related but distinct existing items (different defects in the same file, checked before filing): issue #8182 (release() forgets a still-running systemd-inhibit child) and issue #6534 (dead systemd-inhibit child never noticed). Neither covers the shell=True sleep command.

Found by: repo-worker-code-d, lane code-d, fleet queue fallback mode (queue backend down), suggestions pass for issue #8324. Evidence read from main via the GitHub API on 2026-11-13.

Activity

  1. added
    kind/securitySecurity issue or security hardening
    task/small-fixQueue packet kind: bounded fix with reproduction and targeted tests
    priority/normalOrdinary priority (the default)
    area/nixosNixOS modules and machine configuration
    effort/sSmall: expected under 2 hours of work
    app/power-guardnixos/services/power-guard battery confirmation daemon
    on Nov 13, 2026
  2. dezren39 commented on Nov 17, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · triaged

    • Agent: repo-queue-worker-shift (lane any-d)
    • Session / ID: task 19049 (repo-health-code-pilot)
    • Outcome: Triaged — verified hardening finding, already fully labeled at filing; fix follow-on enqueued as task 20432.

    Triage verdict: the issue arrived triaged (kind/security, task/small-fix, priority/normal, effort/s, area/nixos, app/power-guard — all set by its filer, a fleet lane's verified 2026-11-13 finding). This triage confirms it fresh and routes the fix.

    Fresh evidence (2026-11-17): issue #9602 OPEN, no hard stops, zero comments. The defect is still live on current main: SLEEP_CMD = os.environ.get("POWER_GUARD_SLEEP_CMD", "systemctl hybrid-sleep") at nixos/services/power-guard/power-guard.py:95 and subprocess.run(SLEEP_CMD, shell=True) at line 411 (line numbers drifted from the issue's 94/231; same code). Test hook at test_power_guard.py:181.

    First concrete step for task 20432: parse SLEEP_CMD once at startup with shlex.split, validate non-empty argv (fail closed like timing_from_env), run subprocess.run(argv) with no shell, and update the test hook to argv form. Acceptance check: power-guard test suite green and no shell=True remaining on the sleep-command path. Hardening, not a live exploit path — the PR should say so plainly.

  3. added
    status/triagedTriaged and routed (see agent note)
    and removed
    queue/claimedQueue worker lifecycle state
    work/assignedQueue worker lifecycle state
    on Nov 17, 2026
  4. dezren39 commented on Nov 18, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

    This task was claimed and immediately revalidated. Fresh GitHub state showed the issue CLOSED and PR #11461 MERGED at 2121e7abed18b4ee22227e581cb9cf29cdb7c233; no parallel implementation was started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

app/power-guardnixos/services/power-guard battery confirmation daemonarea/nixosNixOS modules and machine configurationeffort/sSmall: expected under 2 hours of workkind/securitySecurity issue or security hardeningpriority/normalOrdinary priority (the default)queue/queuedHas a queued queue taskstatus/triagedTriaged and routed (see agent note)task/small-fixQueue packet kind: bounded fix with reproduction and targeted tests

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions