fix(systemd_hardening): look up the listed units instead of listing every unit file #298

Merged
Dragonish merged 2 commits from fix/283-systemd-hardening-probe into main 2026-11-22 20:09:26 +00:00
Collaborator

What and why

Closes #283

systemd_hardening lists every service unit file on each run, only to learn whether the roughly ten units in its disable and mask lists are installed. The listing goes through PID 1 and its cost grows with the number of unit files. The role now runs one systemctl show --property=LoadState over the listed units. A unit with no unit file reports not-found and is skipped, as before. systemd_hardening_installed_units keeps its name, so disable_services.yml and mask_services.yml are unchanged (not renamed, to keep the diff to the task). The README, a bugfixes fragment and a Molecule assertion that a listed-but-missing unit (rsh) is skipped and not masked are updated.

Behaviour changes

  • A template name such as [email protected] in either list now makes the lookup fail (systemctl show exits 1); the old listing matched it. I chose a loud failure over silently skipping a unit the consumer asked to disable. Is that the handling you want, or should templates be skipped or supported?
  • An instance name such as [email protected] is now matched when its template exists; the listing never matched it.

Verified

  • pre-commit run (including ansible-lint production profile, yamllint, the changelog-fragment lint and the Conventional Commit check) passes locally. ansible-test sanity runs in the pre-push hook.
  • On five Ubuntu 24.04 and Debian hosts (systemd 255), the installed set from the new task block equals the set from the old listing for the default lists plus an alias (sshd), a .service suffix, a duplicate and a missing unit. Empty lists, one entry, all-missing entries and check mode also behave correctly.
  • Result order for an alias: systemctl show -- sshd.service ssh.service returns the target's Id for the alias, so results are paired by position, not by Id.

Not verified

  • Molecule has not run (CI only); the new assertion has not run.
  • The role's own --type=service call was never shown to trip a hardware watchdog; only the socket-type listing was measured, so the motivation is a cost argument and not proof for this exact call.
  • Debian 13 and Ubuntu 22.04 are covered only by the read-only comparison above, not by a role run. A template name was tested to fail as described; instance names beyond one template were tested on one host.

  • I confirm third-party material is recorded in NOTICE and REUSE.toml, or there is none.

Assisted-by: Claude Code

## What and why Closes #283 `systemd_hardening` lists every service unit file on each run, only to learn whether the roughly ten units in its disable and mask lists are installed. The listing goes through PID 1 and its cost grows with the number of unit files. The role now runs one `systemctl show --property=LoadState` over the listed units. A unit with no unit file reports `not-found` and is skipped, as before. `systemd_hardening_installed_units` keeps its name, so `disable_services.yml` and `mask_services.yml` are unchanged (not renamed, to keep the diff to the task). The README, a `bugfixes` fragment and a Molecule assertion that a listed-but-missing unit (`rsh`) is skipped and not masked are updated. **Behaviour changes** - A template name such as `[email protected]` in either list now makes the lookup fail (`systemctl show` exits 1); the old listing matched it. I chose a loud failure over silently skipping a unit the consumer asked to disable. Is that the handling you want, or should templates be skipped or supported? - An instance name such as `[email protected]` is now matched when its template exists; the listing never matched it. ## Verified - `pre-commit run` (including ansible-lint production profile, yamllint, the changelog-fragment lint and the Conventional Commit check) passes locally. `ansible-test sanity` runs in the pre-push hook. - On five Ubuntu 24.04 and Debian hosts (systemd 255), the installed set from the new task block equals the set from the old listing for the default lists plus an alias (`sshd`), a `.service` suffix, a duplicate and a missing unit. Empty lists, one entry, all-missing entries and check mode also behave correctly. - Result order for an alias: `systemctl show -- sshd.service ssh.service` returns the target's `Id` for the alias, so results are paired by position, not by `Id`. ## Not verified - Molecule has not run (CI only); the new assertion has not run. - The role's own `--type=service` call was never shown to trip a hardware watchdog; only the socket-type listing was measured, so the motivation is a cost argument and not proof for this exact call. - Debian 13 and Ubuntu 22.04 are covered only by the read-only comparison above, not by a role run. A template name was tested to fail as described; instance names beyond one template were tested on one host. --- - [x] I confirm third-party material is recorded in `NOTICE` and `REUSE.toml`, or there is none. Assisted-by: Claude Code
Dragonish added 1 commit 2026-11-22 19:48:54 +00:00
fix(systemd_hardening): look up the listed units instead of listing every unit file
Build / build (pull_request) Successful in 20s
Molecule / molecule (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 38s
6c2d124b3f
The role listed every service unit file to answer one question: is each
unit in its disable and mask lists installed? The listing keeps PID 1 busy
for as long as the host has unit files. Ask about the listed units only,
with one `systemctl show --property=LoadState`; a unit with no unit file
reports `not-found` and is skipped as before.

An instance name such as [email protected] is now matched when its template
exists. A template name such as [email protected] makes the lookup fail, where
the listing matched it.

Closes #283

Assisted-by: Claude Code
Defame1297 added 1 commit 2026-11-22 20:03:10 +00:00
Merge branch 'main' into fix/283-systemd-hardening-probe
Build / build (pull_request) Successful in 25s
Molecule / molecule (pull_request) Successful in 55s
Lint / lint (pull_request) Successful in 1m22s
4dc221c17f
Defame1297 approved these changes 2026-11-22 20:03:16 +00:00
Dragonish merged commit 03ed536a14 into main 2026-11-22 20:09:26 +00:00
Dragonish deleted branch fix/283-systemd-hardening-probe 2026-11-22 20:09:27 +00:00
Dragonish referenced this issue from a commit 2026-11-23 07:53:51 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#298