fix(systemd_hardening): look up the listed units instead of listing every unit file
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
This commit is contained in:
1 parent
5962b9b6fb
commit
6c2d124b3f
4 files changed
+54
-17
No files matched your search
@@ -0,0 +1,8 @@
|
||||
---
|
||||
bugfixes:
|
||||
- >-
|
||||
systemd_hardening - Look up the listed units with one ``systemctl show`` instead of listing every unit
|
||||
file. The listing can keep PID 1 busy for seconds on a host with slow storage, and the cost grows with
|
||||
the number of unit files. A unit is still skipped when it has no unit file. An instance name such as
|
||||
``[email protected]`` is now matched when its template exists, and a template name such as
|
||||
``[email protected]`` in the lists now makes the lookup fail instead of matching.
|
||||
@@ -4,7 +4,7 @@ Disables and masks risky services, writes a systemd manager hardening drop-in an
|
||||
|
||||
## Supported platforms
|
||||
|
||||
Hosts with systemd and `systemctl list-unit-files`: Ubuntu 24.04 (noble) LXCs, Ubuntu 22.04 (jammy) VMs and Debian 13 (trixie) Proxmox hosts. The role is applied to the three system types in `systemd_hardening_profiles`: `pve` (Proxmox hosts), `ubuntu_lxc` and `ubuntu_vm`. The molecule scenario (`molecule/default/`, CI only) runs the `ubuntu_lxc` profile on Ubuntu 24.04 against stub unit files, because the test image installs none of the services the role disables or masks. It checks the disable and mask results, the manager and service drop-ins (loaded by systemd after the reload) and the refusals; it cannot show that a real `rpcbind` or similar stops. The `pve` and `ubuntu_vm` profiles and Ubuntu 22.04 and Debian 13 are not verified by a test.
|
||||
Hosts with systemd: Ubuntu 24.04 (noble) LXCs, Ubuntu 22.04 (jammy) VMs and Debian 13 (trixie) Proxmox hosts. The role is applied to the three system types in `systemd_hardening_profiles`: `pve` (Proxmox hosts), `ubuntu_lxc` and `ubuntu_vm`. The molecule scenario (`molecule/default/`, CI only) runs the `ubuntu_lxc` profile on Ubuntu 24.04 against stub unit files, because the test image installs none of the services the role disables or masks. It checks the disable and mask results, the manager and service drop-ins (loaded by systemd after the reload) and the refusals; it cannot show that a real `rpcbind` or similar stops. The `pve` and `ubuntu_vm` profiles and Ubuntu 22.04 and Debian 13 are not verified by a test.
|
||||
|
||||
## Requirements and run order
|
||||
|
||||
@@ -29,7 +29,7 @@ Two variables below, `systemd_hardening_disable_services_extra` and `systemd_har
|
||||
|
||||
## Tags
|
||||
|
||||
Only `always` (the `systemd_hardening_system_type` assert) works as intended. `disable`, `mask`, `harden-manager` and `dropins` sit on the `include_tasks` lines in `tasks/main.yml`, and tags on a dynamic include select only the include itself: the included tasks are untagged, so they are skipped. `--tags disable` or `--tags mask` therefore runs only the assert and the unit listing, and `--tags harden-manager` or `--tags dropins` runs only the assert. Do not use these tags for selective runs; run the role untagged.
|
||||
Only `always` (the `systemd_hardening_system_type` assert) works as intended. `disable`, `mask`, `harden-manager` and `dropins` sit on the `include_tasks` lines in `tasks/main.yml`, and tags on a dynamic include select only the include itself: the included tasks are untagged, so they are skipped. `--tags disable` or `--tags mask` therefore runs only the assert and the unit lookup, and `--tags harden-manager` or `--tags dropins` runs only the assert. Do not use these tags for selective runs; run the role untagged.
|
||||
|
||||
## Example play
|
||||
|
||||
@@ -49,7 +49,7 @@ Only `always` (the `systemd_hardening_system_type` assert) works as intended. `d
|
||||
|
||||
## Notes
|
||||
|
||||
- Only units that appear in `systemctl list-unit-files --type=service` are disabled or masked, with or without the `.service` suffix.
|
||||
- Only units that systemd has a unit file for (`LoadState` is not `not-found`) are disabled or masked, with or without the `.service` suffix. The role asks about the listed units only, with one `systemctl show`, and does not list every unit file. A template name such as `[email protected]` is not supported: that call fails.
|
||||
- The manager drop-in (`DefaultLimitNOFILE`, `DefaultLimitNPROC`, `DefaultLimitCORE`, and task, memory and CPU accounting) is written verbatim by `tasks/harden_manager.yml` and has no variable.
|
||||
- Drop-in and manager changes notify `systemd reload`, which runs `daemon-reload`. Handlers are flushed at the end of the role.
|
||||
- Policy lives in defaults, not `vars/`, so group_vars and host_vars can override it.
|
||||
|
||||
@@ -20,6 +20,17 @@
|
||||
- systemd_hardening_verify_units.results[1].status.UnitFileState == "masked"
|
||||
quiet: true
|
||||
|
||||
- name: Ask systemd about a listed unit that is not installed
|
||||
ansible.builtin.command: systemctl show --property=LoadState rsh.service # noqa: command-instead-of-module
|
||||
register: systemd_hardening_verify_missing
|
||||
changed_when: false
|
||||
|
||||
- name: The missing unit was skipped, not masked
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- systemd_hardening_verify_missing.stdout == "LoadState=not-found"
|
||||
quiet: true
|
||||
|
||||
- name: Read the manager drop-in
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/systemd/system.conf.d/hardening.conf
|
||||
|
||||
@@ -20,22 +20,40 @@
|
||||
success_msg: "systemd_hardening_system_type '{{ systemd_hardening_system_type }}' is valid."
|
||||
tags: always
|
||||
|
||||
# Not service_facts: it returns a fact named `services`, which (with
|
||||
# inject_facts_as_vars) overrides any inventory variable of that name, such as
|
||||
# a list of services a playbook installs. Only installed unit files are listed,
|
||||
# so units that are merely referenced (LoadState not-found) are excluded.
|
||||
- name: List installed service unit files # noqa: command-instead-of-module
|
||||
become: true
|
||||
ansible.builtin.command: systemctl list-unit-files --type=service --no-legend --plain
|
||||
register: systemd_hardening_unit_files
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
# Ask systemd about the listed units only: listing every unit file keeps PID 1 busy for as long as
|
||||
# the host has unit files, which on slow storage can outlast a short hardware watchdog. `systemctl show`
|
||||
# prints one block per argument in the order given, `LoadState=not-found` for a unit with no unit
|
||||
# file, and keeps the order for an alias. Not service_facts: it sets a fact named `services`, which
|
||||
# overrides an inventory variable of that name.
|
||||
- name: Find which of the listed service units are installed
|
||||
tags: [disable, mask]
|
||||
vars:
|
||||
__systemd_hardening_probe_units: >-
|
||||
{{ (systemd_hardening_disable_services_effective + systemd_hardening_mask_services)
|
||||
| map('regex_replace', '[.]service$', '') | unique | map('regex_replace', '$', '.service') | list }}
|
||||
block:
|
||||
- name: Look up the listed service units # noqa: command-instead-of-module
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
argv: "{{ ['systemctl', 'show', '--property=LoadState', '--'] + __systemd_hardening_probe_units }}"
|
||||
register: __systemd_hardening_probe
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
when: __systemd_hardening_probe_units | length > 0
|
||||
|
||||
- name: Set installed service units
|
||||
ansible.builtin.set_fact:
|
||||
systemd_hardening_installed_units: "{{ systemd_hardening_unit_files.stdout_lines | map('split') | map('first') | list }}"
|
||||
tags: [disable, mask]
|
||||
- name: Set installed service units
|
||||
ansible.builtin.set_fact:
|
||||
systemd_hardening_installed_units: >-
|
||||
{{ __systemd_hardening_probe_units | zip(__systemd_hardening_probe_states)
|
||||
| rejectattr('1', 'eq', 'LoadState=not-found') | map(attribute='0') | list }}
|
||||
vars:
|
||||
__systemd_hardening_probe_states: "{{ (__systemd_hardening_probe.stdout | default('')).strip().split('\n\n') }}"
|
||||
when: __systemd_hardening_probe_units | length > 0
|
||||
|
||||
- name: Set no installed service units
|
||||
ansible.builtin.set_fact:
|
||||
systemd_hardening_installed_units: []
|
||||
when: __systemd_hardening_probe_units | length == 0
|
||||
|
||||
- name: Disable unwanted services
|
||||
ansible.builtin.include_tasks: disable_services.yml
|
||||
|
||||
Reference in new issue
Block a user