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,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:
|
||||
|
||||
Reference in new issue
Block a user