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
94 lines
3.7 KiB
YAML
94 lines
3.7 KiB
YAML
---
|
|
# What the role is for: the installed risky units are disabled or masked, the manager
|
|
# drop-in is in place, and the opt-in drop-in renders and is loaded by systemd. The
|
|
# directives restate the converge vars, not the role's whole policy.
|
|
- name: Verify
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Ask systemd about the stub units
|
|
ansible.builtin.systemd:
|
|
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 units, sockets and services alike, are disabled and stopped
|
|
ansible.builtin.assert:
|
|
that:
|
|
- 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: 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 units were skipped, not masked or created
|
|
ansible.builtin.assert:
|
|
that:
|
|
- 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:
|
|
src: /etc/systemd/system.conf.d/hardening.conf
|
|
register: systemd_hardening_verify_manager
|
|
|
|
- name: The manager drop-in holds the limits
|
|
ansible.builtin.assert:
|
|
that:
|
|
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('[Manager]')
|
|
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('DefaultLimitNOFILE=65535')
|
|
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('DefaultLimitCORE=0')
|
|
quiet: true
|
|
|
|
- name: Read the service drop-in
|
|
ansible.builtin.slurp:
|
|
src: /etc/systemd/system/testsvc.service.d/hardening.conf
|
|
register: systemd_hardening_verify_dropin
|
|
|
|
- name: The drop-in renders booleans, lists and plain values
|
|
ansible.builtin.assert:
|
|
that:
|
|
- (systemd_hardening_verify_dropin.content | b64decode).split('\n') | select | list == systemd_hardening_verify_expected
|
|
quiet: true
|
|
vars:
|
|
systemd_hardening_verify_expected:
|
|
- "[Service]"
|
|
- ProtectSystem=strict
|
|
- NoNewPrivileges=yes
|
|
- ReadWritePaths=/var/tmp /run
|
|
- LimitNOFILE=1024
|
|
|
|
- name: Ask systemd about the drop-in service
|
|
ansible.builtin.systemd:
|
|
name: testsvc.service
|
|
register: systemd_hardening_verify_testsvc
|
|
check_mode: true
|
|
|
|
- name: The daemon reload made systemd load the drop-in
|
|
ansible.builtin.assert:
|
|
that:
|
|
- systemd_hardening_verify_testsvc.status.NoNewPrivileges == "yes"
|
|
- systemd_hardening_verify_testsvc.status.ProtectSystem == "strict"
|
|
quiet: true
|