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
57 lines
4.3 KiB
Markdown
57 lines
4.3 KiB
Markdown
# systemd_hardening
|
|
|
|
Disables and masks risky services, writes a systemd manager hardening drop-in and renders opt-in per-service sandboxing drop-ins. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth.
|
|
|
|
## Supported platforms
|
|
|
|
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
|
|
|
|
- `systemd_hardening_system_type` is required and has no default (the role does not read a global such as `system_type`; map it in the inventory). The role fails if it is undefined or not a key of `systemd_hardening_profiles`.
|
|
- Use it by its fully qualified name, `mortis.kamino.systemd_hardening`.
|
|
- Run it after your baseline role and before the roles that manage firewalls and jails.
|
|
- Needs privilege escalation: tasks use `become`.
|
|
|
|
## Role variables
|
|
|
|
Two variables below, `systemd_hardening_disable_services_extra` and `systemd_hardening_disable_services_effective`, are deliberately not in the spec: their defaults index the profile by `systemd_hardening_system_type`, and the spec is templated before the role's assert runs, so an invalid `systemd_hardening_system_type` would otherwise fail with a templating error instead of the role's own message.
|
|
|
|
| Variable | Default | Required | Description |
|
|
|---|---|---|---|
|
|
| `systemd_hardening_system_type` | none | yes | Profile key. Every play that includes the role sets it. |
|
|
| `systemd_hardening_profiles` | `pve`, `ubuntu_lxc`, `ubuntu_vm` profiles | no | Per type `disable_services_extra`. Overriding replaces all profiles. |
|
|
| `systemd_hardening_disable_services` | `rpcbind`, `avahi-daemon`, `cups`, `bluetooth`, `nfs-server` | no | Disabled on every type. |
|
|
| `systemd_hardening_disable_services_extra` | profile entry for `systemd_hardening_system_type` | no | Added to the list above. |
|
|
| `systemd_hardening_disable_services_effective` | base plus extra | no | Result, do not set. |
|
|
| `systemd_hardening_mask_services` | `telnet`, `rsh`, `rlogin`, `xinetd` | no | Masked. |
|
|
| `systemd_hardening_services` | `{}` | no | Unit to directive map for drop-ins. |
|
|
|
|
## 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 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
|
|
|
|
```yaml
|
|
- name: Harden LXC services
|
|
hosts: lxc_guests
|
|
vars:
|
|
systemd_hardening_system_type: ubuntu_lxc
|
|
roles:
|
|
- role: mortis.kamino.systemd_hardening
|
|
vars:
|
|
systemd_hardening_services:
|
|
myservice:
|
|
ProtectSystem: strict
|
|
NoNewPrivileges: true
|
|
```
|
|
|
|
## Notes
|
|
|
|
- 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.
|
|
- `no_log` is not used: the role handles no secrets.
|