fix(systemd_hardening): look up the units the role lists instead of listing every unit file #283

Closed
opened 2026-11-21 21:46:17 +00:00 by Dragonish · 0 comments
Collaborator

Summary

systemd_hardening runs systemctl list-unit-files --type=service on every run, only to answer one question: is each of the roughly ten units in its disable and mask lists installed? On a slow host that one call can keep PID 1 busy for more than ten seconds. If a hardware watchdog is armed with a shorter timeout, the board is reset in the middle of the play. A lookup limited to the listed units costs milliseconds and needs no change to the role's behaviour.

Where

roles/systemd_hardening/tasks/main.yml ("List installed service unit files", then "Set installed service units"). The list is used in disable_services.yml and mask_services.yml, each only as unit in systemd_hardening_installed_units. Identical on main (0385dba) and on v1.8.0.

What I measured

Host: Raspberry Pi 4 (arm64), Ubuntu 24.04, root filesystem on a USB disk, systemd 255, a few hundred service and socket unit files. RuntimeWatchdogSec=10s in a system.conf.d drop-in, which is what arms the BCM2835 hardware watchdog.

  • systemctl list-unit-files --type=service,socket through PID 1 reset the board on every run (several runs), and --type=socket alone reset it in 6 of 6. The role itself runs --type=service; I did not run that exact variant through PID 1 on this board, so treat the next points as evidence for the call's cost and not as proof that the role's exact command resets it. The reset is the watchdog doing its job: systemd feeds it from PID 1's event loop, and while PID 1 is busy answering ListUnitFiles it does not. Sampling /proc/1 during the call showed PID 1 in state R (running, in userspace) in every 0.33 s sample for at least six seconds. The kernel's last ping reply came about ten seconds after the request, and vcgencmd get_rsts read 0x20 (the watchdog path), not 0x1000 (power-on).
  • The same listing done client-side, systemctl --root=/ list-unit-files, does not go through PID 1. It took about 13 s and never reset the board. strace -c of a client-side run shows 73,613 getdents64 calls, 12.4 s in total, which is the cost of re-reading the unit directories.
  • With the watchdog set to 60 s at runtime, three listings through PID 1 passed (RuntimeWatchdogSec=60s is the workaround I use on that board).
  • On an Ubuntu 24.04 container with 174 service unit files the full listing takes about 0.5 s. A lookup of three units, systemctl show --property=Id,LoadState ssh.service cups.service nonexistent-xyz.service, takes 12 ms, and prints LoadState=not-found with exit status 0 for a unit that does not exist.

So this is invisible on most hosts. It matters on slow storage with a short watchdog, and the cost of the listing grows with the number of installed units even where it does not reset anything.

Proposal

Ask only about the units the role will act on:

- name: Look up the listed units
  become: true
  ansible.builtin.command:
    argv: "{{ ['systemctl', 'show', '--property=Id,LoadState'] + probe_units }}"
  vars:
    probe_units: >-
      {{ (systemd_hardening_disable_services_effective + systemd_hardening_mask_services)
         | map('regex_replace', '\.service$', '') | unique | map('regex_replace', '$', '.service') | list }}
  register: systemd_hardening_probe
  changed_when: false
  check_mode: false
  when: probe_units | length > 0

A unit counts as installed when its LoadState is not not-found. systemctl show prints one block per argument in the order given, so the result can be paired with the input by position (I did not test aliases, see below). systemd_hardening_installed_units can keep its name, so disable_services.yml and mask_services.yml stay as they are. The when guard matters only if both lists are overridden to empty, because systemctl show with no argument describes the manager and not a unit. The sketch above ran on an Ubuntu 24.04 container with the default lists plus ssh: ten results in input order, only ssh loaded.

I wrote a similar variant for my own role, where it is merged but not yet proven on the Pi (that proof run is still to do). The sketch above is a simplified form of it and has only been run once, on the container above; it is not a test in your Molecule scenario.

Not verified

  • Template units ([email protected]) and units that exist only as aliases: I did not test either against the old behaviour. For an alias, systemctl show may report the Id of its target, which is why the result is paired by position.
  • The role's exact command (--type=service through PID 1) on the Pi, as said above.
  • Whether Pi-class hardware is a target you want to support. The platform declaration says Ubuntu, and this is Ubuntu 24.04 on arm64, so I assumed it is in scope. If it is not, the ten-unit lookup is still cheaper, but you may not want the change.
  • Whether ansible-test sanity or ansible-lint object to the argv form here.

Offer

If you agree and mark this Ready for Agent, I will send a PR with the change, a Molecule verify.yml assertion that a listed-but-missing unit is skipped and an installed one is disabled, and a changelog fragment.

Assisted-by: Claude Code

## Summary `systemd_hardening` runs `systemctl list-unit-files --type=service` on every run, only to answer one question: is each of the roughly ten units in its disable and mask lists installed? On a slow host that one call can keep PID 1 busy for more than ten seconds. If a hardware watchdog is armed with a shorter timeout, the board is reset in the middle of the play. A lookup limited to the listed units costs milliseconds and needs no change to the role's behaviour. ## Where `roles/systemd_hardening/tasks/main.yml` ("List installed service unit files", then "Set installed service units"). The list is used in `disable_services.yml` and `mask_services.yml`, each only as `unit in systemd_hardening_installed_units`. Identical on `main` (0385dba) and on `v1.8.0`. ## What I measured Host: Raspberry Pi 4 (arm64), Ubuntu 24.04, root filesystem on a USB disk, systemd 255, a few hundred service and socket unit files. `RuntimeWatchdogSec=10s` in a `system.conf.d` drop-in, which is what arms the BCM2835 hardware watchdog. - `systemctl list-unit-files --type=service,socket` through PID 1 reset the board on every run (several runs), and `--type=socket` alone reset it in 6 of 6. The role itself runs `--type=service`; I did not run that exact variant through PID 1 on this board, so treat the next points as evidence for the call's cost and not as proof that the role's exact command resets it. The reset is the watchdog doing its job: systemd feeds it from PID 1's event loop, and while PID 1 is busy answering `ListUnitFiles` it does not. Sampling `/proc/1` during the call showed PID 1 in state `R` (running, in userspace) in every 0.33 s sample for at least six seconds. The kernel's last ping reply came about ten seconds after the request, and `vcgencmd get_rsts` read `0x20` (the watchdog path), not `0x1000` (power-on). - The same listing done client-side, `systemctl --root=/ list-unit-files`, does not go through PID 1. It took about 13 s and never reset the board. `strace -c` of a client-side run shows 73,613 `getdents64` calls, 12.4 s in total, which is the cost of re-reading the unit directories. - With the watchdog set to 60 s at runtime, three listings through PID 1 passed (`RuntimeWatchdogSec=60s` is the workaround I use on that board). - On an Ubuntu 24.04 container with 174 service unit files the full listing takes about 0.5 s. A lookup of three units, `systemctl show --property=Id,LoadState ssh.service cups.service nonexistent-xyz.service`, takes 12 ms, and prints `LoadState=not-found` with exit status 0 for a unit that does not exist. So this is invisible on most hosts. It matters on slow storage with a short watchdog, and the cost of the listing grows with the number of installed units even where it does not reset anything. ## Proposal Ask only about the units the role will act on: ```yaml - name: Look up the listed units become: true ansible.builtin.command: argv: "{{ ['systemctl', 'show', '--property=Id,LoadState'] + probe_units }}" vars: probe_units: >- {{ (systemd_hardening_disable_services_effective + systemd_hardening_mask_services) | map('regex_replace', '\.service$', '') | unique | map('regex_replace', '$', '.service') | list }} register: systemd_hardening_probe changed_when: false check_mode: false when: probe_units | length > 0 ``` A unit counts as installed when its `LoadState` is not `not-found`. `systemctl show` prints one block per argument in the order given, so the result can be paired with the input by position (I did not test aliases, see below). `systemd_hardening_installed_units` can keep its name, so `disable_services.yml` and `mask_services.yml` stay as they are. The `when` guard matters only if both lists are overridden to empty, because `systemctl show` with no argument describes the manager and not a unit. The sketch above ran on an Ubuntu 24.04 container with the default lists plus `ssh`: ten results in input order, only `ssh` `loaded`. I wrote a similar variant for my own role, where it is merged but not yet proven on the Pi (that proof run is still to do). The sketch above is a simplified form of it and has only been run once, on the container above; it is not a test in your Molecule scenario. ## Not verified - Template units (`[email protected]`) and units that exist only as aliases: I did not test either against the old behaviour. For an alias, `systemctl show` may report the `Id` of its target, which is why the result is paired by position. - The role's exact command (`--type=service` through PID 1) on the Pi, as said above. - Whether Pi-class hardware is a target you want to support. The platform declaration says Ubuntu, and this is Ubuntu 24.04 on arm64, so I assumed it is in scope. If it is not, the ten-unit lookup is still cheaper, but you may not want the change. - Whether `ansible-test sanity` or `ansible-lint` object to the `argv` form here. ## Offer If you agree and mark this `Ready for Agent`, I will send a PR with the change, a Molecule `verify.yml` assertion that a listed-but-missing unit is skipped and an installed one is disabled, and a changelog fragment. Assisted-by: Claude Code
Dragonish added the
Status
Ready for Human
label 2026-11-21 21:46:17 +00:00
Dragonish referenced this issue from a commit 2026-11-23 07:53:51 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#283