fix(systemd_hardening): disable and mask the socket of a listed service too
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:
1 parent
545f2fe94a
commit
f256c1c7e5
8 files changed
+101
-33
No files matched your search
@@ -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 }}
|
||||
@@ -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 }}
|
||||
Reference in new issue
Block a user