fix(systemd_hardening): disable and mask the socket of a listed service too
Build / build (pull_request) Successful in 18s
Lint / lint (pull_request) Successful in 1m11s
Molecule / molecule (pull_request) Successful in 1m2s

Look up every listed name as a socket and as a service and act on each
installed unit, the socket first. A service whose socket stays active is
started again by the next connection to its port.

Assisted-by: Claude Code
This commit is contained in:
Dragonish committed 2026-11-23 11:18:49 +00:00
1 parent 545f2fe94a
commit f256c1c7e5
8 files changed
+101 -33

No files matched your search

@@ -0,0 +1,8 @@
---
bugfixes:
- >-
systemd_hardening - Disable and mask the socket of a listed service as well. A service whose socket stayed active was started again by
the next connection to its port (``rpcbind`` listens on port 111 through ``rpcbind.socket``). Every name in the disable and mask lists
is now looked up as a socket and as a service, with or without a ``.service`` or ``.socket`` suffix, and each installed unit is acted
on, the socket first. A socket that belongs to a listed name, such as ``rpcbind.socket``, is now stopped and disabled, or masked,
where before it was left alone.
+3 -2
View File
@@ -4,7 +4,7 @@ Disables and masks risky services, writes a systemd manager hardening drop-in an
## 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.
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 (a socket and a service for the ones that ship both, a service only and a socket only for the others), because the test image installs none of the services the role disables or masks. It checks that the stub sockets and services end up stopped and disabled or masked, that a listed unit that does not exist is skipped, 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
@@ -49,7 +49,8 @@ Only `always` (the `systemd_hardening_system_type` assert) works as intended. `d
## 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.
- Every name in `systemd_hardening_disable_services` and `systemd_hardening_mask_services` is looked up as a socket and as a service, with or without the `.service` or `.socket` suffix. Only units that systemd has a unit file for (`LoadState` is not `not-found`) are disabled or masked, the socket first: a stopped service whose socket still listens is started again by the next connection. A name with only a service, only a socket, or neither is fine. 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.
- `systemctl disable` does nothing for a static unit (one with no `[Install]` section), so the systemd module can report `changed` on every run for it; list only names whose units can be enabled, and note that this now applies to a listed name's socket as well.
- 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.
@@ -7,7 +7,9 @@ argument_specs:
Validates C(systemd_hardening_system_type) against C(systemd_hardening_profiles), then disables the unwanted services and masks the high-risk
services that have an installed unit file, writes C(/etc/systemd/system.conf.d/hardening.conf) and renders a drop-in for every
unit in C(systemd_hardening_services).
- Units that are not installed are skipped. Handlers are flushed at the end of the role.
- >-
Every listed name is handled as a socket and as a service, the socket first; units that are not installed are skipped.
Handlers are flushed at the end of the role.
- >-
The role also reads C(systemd_hardening_disable_services_extra) (default is the profile entry for C(systemd_hardening_system_type)) and the
derived C(systemd_hardening_disable_services_effective) (the base list plus the extra list, a result, do not set it). They are
@@ -35,13 +37,17 @@ argument_specs:
elements: str
default: [rpcbind, avahi-daemon, cups, bluetooth, nfs-server]
description:
- Services stopped and disabled on every system type, with or without the C(.service) suffix. Only installed units are touched.
- >-
Services stopped and disabled on every system type, with or without the C(.service) or C(.socket) suffix, together with the
socket of the same name. Only installed units are touched.
systemd_hardening_mask_services:
type: list
elements: str
default: [telnet, rsh, rlogin, xinetd]
description:
- Services masked and stopped. Only installed units are touched.
- >-
Services masked and stopped, with or without the C(.service) or C(.socket) suffix, together with the socket of the same name.
Only installed units are touched.
systemd_hardening_services:
type: dict
default: {}
@@ -1,8 +1,12 @@
---
# The image installs none of the services the role touches, so the units it acts on are
# stubs in /usr/lib/systemd/system (not /etc: `systemctl mask` refuses to replace a regular
# file there). Nothing is started. rpcbind is enabled so the role has something to
# disable; telnet only needs to exist; testsvc receives the drop-in.
# file there). rpcbind and telnet are a socket and a service each, as the real packages ship them;
# cups has only a service and bluetooth only a socket (its Service= is a stub of another name:
# systemd refuses to start a socket whose service is not loaded), so the role meets every shape. The sockets
# listen on a unix socket under /run and are started, so the role has something to stop. rpcbind,
# cups and bluetooth are enabled so the role has something to disable; telnet only needs to
# exist; testsvc receives the drop-in.
- name: Prepare
hosts: all
gather_facts: false
@@ -22,10 +26,35 @@
owner: root
group: root
mode: "0644"
loop: [rpcbind, telnet, testsvc]
loop: [rpcbind, telnet, cups, testsvc, molecule-bluetooth-target]
- name: Enable rpcbind and reload systemd
- name: Install the stub socket unit files
ansible.builtin.copy:
dest: "/usr/lib/systemd/system/{{ item }}.socket"
content: |
[Unit]
Description=molecule stub socket for {{ item }}
[Socket]
ListenStream=/run/molecule-{{ item }}.sock
{% if item == 'bluetooth' %}
Service=molecule-bluetooth-target.service
{% endif %}
[Install]
WantedBy=sockets.target
owner: root
group: root
mode: "0644"
loop: [rpcbind, telnet, bluetooth]
- name: Reload systemd
ansible.builtin.systemd:
name: rpcbind
enabled: true
daemon_reload: true
- name: Enable and start the stub sockets and services
ansible.builtin.systemd:
name: "{{ item }}"
enabled: true
state: started
loop: [rpcbind.socket, telnet.socket, bluetooth.socket, rpcbind.service, cups.service, telnet.service]
@@ -8,28 +8,45 @@
tasks:
- name: Ask systemd about the stub units
ansible.builtin.systemd:
name: "{{ item }}.service"
loop: [rpcbind, telnet]
name: "{{ item }}"
loop: [rpcbind.socket, rpcbind.service, cups.service, bluetooth.socket, telnet.socket, telnet.service]
register: systemd_hardening_verify_units
check_mode: true
- name: The disabled unit is disabled and the masked unit is masked
- name: The disabled units, sockets and services alike, are disabled and stopped
ansible.builtin.assert:
that:
- systemd_hardening_verify_units.results[0].status.UnitFileState == "disabled"
- systemd_hardening_verify_units.results[1].status.UnitFileState == "masked"
- item.status.UnitFileState == "disabled"
- item.status.ActiveState == "inactive"
quiet: true
loop: "{{ systemd_hardening_verify_units.results | rejectattr('item', 'match', '^telnet[.]') | list }}"
loop_control:
label: "{{ item.item }}"
- 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
- name: The masked units, socket and service, are masked and stopped
ansible.builtin.assert:
that:
- item.status.UnitFileState == "masked"
- item.status.ActiveState == "inactive"
quiet: true
loop: "{{ systemd_hardening_verify_units.results | selectattr('item', 'match', '^telnet[.]') | list }}"
loop_control:
label: "{{ item.item }}"
- name: Ask systemd about the listed units that are not installed
ansible.builtin.command: systemctl show --property=LoadState {{ item }} # noqa: command-instead-of-module
loop: [cups.socket, bluetooth.service, rsh.service, rsh.socket]
register: systemd_hardening_verify_absent
changed_when: false
- name: The missing unit was skipped, not masked
- name: The missing units were skipped, not masked or created
ansible.builtin.assert:
that:
- systemd_hardening_verify_missing.stdout == "LoadState=not-found"
- item.stdout == "LoadState=not-found"
quiet: true
loop: "{{ systemd_hardening_verify_absent.results }}"
loop_control:
label: "{{ item.item }}"
- name: Read the manager drop-in
ansible.builtin.slurp:
@@ -1,11 +1,12 @@
---
# Every installed socket and service of a listed name is stopped and disabled, the socket first: a
# stopped service whose socket is still listening is started again by the next connection.
- name: Disable unwanted services if present
become: true
ansible.builtin.systemd:
name: "{{ item }}"
enabled: false
state: stopped
loop: "{{ systemd_hardening_disable_services_effective }}"
# Only act on installed units; items may be given with or without the
# ".service" suffix.
when: (item | regex_replace('\.service$', '') ~ '.service') in systemd_hardening_installed_units
loop: >-
{{ systemd_hardening_disable_services_effective | map('regex_replace', '[.](service|socket)$', '') | unique
| product(['socket', 'service']) | map('join', '.') | select('in', systemd_hardening_installed_units) | list }}
+9 -5
View File
@@ -25,14 +25,18 @@
# 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
# Every listed name is looked up as a socket and as a service, socket first: stopping a service
# whose socket is still listening does not close the port, systemd starts the service again on the
# next connection. A name may be given with or without .service or .socket.
- name: Find which of the listed 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 }}
| map('regex_replace', '[.](service|socket)$', '') | unique
| product(['socket', 'service']) | map('join', '.') | list }}
block:
- name: Look up the listed service units # noqa: command-instead-of-module
- name: Look up the listed units # noqa: command-instead-of-module
become: true
ansible.builtin.command:
argv: "{{ ['systemctl', 'show', '--property=LoadState', '--'] + __systemd_hardening_probe_units }}"
@@ -41,7 +45,7 @@
check_mode: false
when: __systemd_hardening_probe_units | length > 0
- name: Set installed service units
- name: Set installed units
ansible.builtin.set_fact:
systemd_hardening_installed_units: >-
{{ __systemd_hardening_probe_units | zip(__systemd_hardening_probe_states)
@@ -50,7 +54,7 @@
__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
- name: Set no installed units
ansible.builtin.set_fact:
systemd_hardening_installed_units: []
when: __systemd_hardening_probe_units | length == 0
@@ -1,10 +1,12 @@
---
# Same shape as disable_services.yml: every installed socket and service of a listed name is masked, the socket first.
# A unit that is not installed can't be started, so masking it is unnecessary.
- name: Mask high-risk services
become: true
ansible.builtin.systemd:
name: "{{ item }}"
masked: true
state: stopped
loop: "{{ systemd_hardening_mask_services }}"
# A unit that isn't installed can't be started, so masking it is unnecessary.
when: (item | regex_replace('\.service$', '') ~ '.service') in systemd_hardening_installed_units
loop: >-
{{ systemd_hardening_mask_services | map('regex_replace', '[.](service|socket)$', '') | unique
| product(['socket', 'service']) | map('join', '.') | select('in', systemd_hardening_installed_units) | list }}