fix(system_baseline): persist sysctls in an own sysctl.d drop-in
systemd-sysctl on Debian 13 does not read /etc/sysctl.conf, so the hardened values were lost on reboot. The role now applies the live values on their own, writes them to a drop-in under /etc/sysctl.d and removes its keys from /etc/sysctl.conf. Refs #510 Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
cf99893855
commit
d88e5d237a
14 files changed
+282
-17
No files matched your search
@@ -151,6 +151,25 @@
|
||||
- "200M\nStorage=none"
|
||||
- 300
|
||||
|
||||
# The role overwrites the whole drop-in: /etc/sysctl.conf, the link to it, another directory and a name systemd-sysctl does not read
|
||||
# each stop the run before any change.
|
||||
- name: A bad sysctl drop-in path is refused
|
||||
hosts: localhost
|
||||
connection: local
|
||||
check_mode: true
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Refuse each bad value
|
||||
ansible.builtin.include_tasks: tasks/refuse_sysctl_file.yml
|
||||
vars:
|
||||
system_baseline_refused_sysctl_file: "{{ item }}"
|
||||
loop:
|
||||
- /etc/sysctl.conf
|
||||
- /etc/sysctl.d/99-sysctl.conf
|
||||
- /etc/sysctl.d/../sysctl.conf
|
||||
- /etc/sysctl.d/baseline
|
||||
- ""
|
||||
|
||||
# A quote, a null item and a bare string (not a list) each stop the run: the bare string would otherwise be split into one-character
|
||||
# patterns, and the null item would become an empty pattern that matches every package.
|
||||
- name: A bad package blacklist is refused
|
||||
@@ -818,6 +837,123 @@
|
||||
- {type: pve, input: false, running: false, asked: false, answer: [{type: vzdump, upid: a, starttime: 1}]}
|
||||
- {type: ubuntu_lxc, input: true, running: false, asked: false, answer: [{type: vzdump, upid: a, starttime: 1}], tasks_from: update}
|
||||
|
||||
# The converge persisted the ubuntu_lxc list and cleaned /etc/sysctl.conf. The rest runs the task files directly: persisting a shorter list
|
||||
# without /etc/sysctl.conf, then the runtime apply against the container's read-only /proc/sys.
|
||||
- name: The sysctl hardening is persisted in its own drop-in
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
system_baseline_system_type: ubuntu_lxc
|
||||
tasks:
|
||||
- name: Read the drop-in and /etc/sysctl.conf
|
||||
ansible.builtin.command:
|
||||
argv: [cat, "{{ item }}"]
|
||||
loop:
|
||||
- /etc/sysctl.d/99-zz-system-baseline.conf
|
||||
- /etc/sysctl.conf
|
||||
register: system_baseline_verify_sysctl
|
||||
changed_when: false
|
||||
|
||||
- name: The drop-in holds the profile's keys and only they left /etc/sysctl.conf
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- system_baseline_verify_sysctl.results[0].stdout_lines | reject('match', '#') | list == system_baseline_verify_sysctl_expected
|
||||
- system_baseline_verify_sysctl.results[1].stdout_lines == ['# net.ipv4.tcp_syncookies=1', 'vm.max_map_count = 262144']
|
||||
quiet: true
|
||||
vars:
|
||||
system_baseline_verify_sysctl_expected:
|
||||
- net.ipv4.tcp_syncookies=1
|
||||
- net.ipv4.conf.all.accept_source_route=0
|
||||
- net.ipv4.conf.all.accept_redirects=0
|
||||
- net.ipv4.conf.all.secure_redirects=0
|
||||
- net.ipv4.icmp_echo_ignore_broadcasts=1
|
||||
- net.ipv4.icmp_ignore_bogus_error_responses=1
|
||||
|
||||
- name: Remove /etc/sysctl.conf
|
||||
ansible.builtin.file:
|
||||
path: /etc/sysctl.conf
|
||||
state: absent
|
||||
|
||||
- name: Persist a list that repeats one key
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.system_baseline
|
||||
tasks_from: sysctl
|
||||
vars:
|
||||
system_baseline_sysctl_hardening:
|
||||
- { name: net.ipv4.tcp_syncookies, value: 0 }
|
||||
- { name: net.ipv4.tcp_syncookies, value: "yes" }
|
||||
|
||||
- name: Read the drop-in again
|
||||
ansible.builtin.command:
|
||||
argv: [cat, /etc/sysctl.d/99-zz-system-baseline.conf]
|
||||
register: system_baseline_verify_sysctl_short
|
||||
changed_when: false
|
||||
|
||||
- name: Stat /etc/sysctl.conf
|
||||
ansible.builtin.stat:
|
||||
path: /etc/sysctl.conf
|
||||
register: system_baseline_verify_sysctl_conf
|
||||
|
||||
- name: Only the last entry is left and /etc/sysctl.conf was not created
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- system_baseline_verify_sysctl_short.stdout_lines | reject('match', '#') | list == ['net.ipv4.tcp_syncookies=1']
|
||||
- not system_baseline_verify_sysctl_conf.stat.exists
|
||||
quiet: true
|
||||
|
||||
- name: Read a live value
|
||||
ansible.builtin.command:
|
||||
argv: [cat, /proc/sys/net/ipv4/tcp_syncookies]
|
||||
register: system_baseline_verify_sysctl_live
|
||||
changed_when: false
|
||||
|
||||
- name: Apply the value the kernel already has
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.system_baseline
|
||||
tasks_from: sysctl_apply
|
||||
vars:
|
||||
system_baseline_sysctl_hardening:
|
||||
- { name: net.ipv4.tcp_syncookies, value: "{{ system_baseline_verify_sysctl_live.stdout }}" }
|
||||
|
||||
- name: Nothing was applied
|
||||
ansible.builtin.assert:
|
||||
that: __system_baseline_sysctl_apply is not changed
|
||||
quiet: true
|
||||
|
||||
- name: Preview a different value in check mode
|
||||
check_mode: true
|
||||
block:
|
||||
- name: Include system_baseline, tasks_from sysctl_apply
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.system_baseline
|
||||
tasks_from: sysctl_apply
|
||||
vars:
|
||||
system_baseline_sysctl_hardening:
|
||||
- { name: net.ipv4.tcp_syncookies, value: "{{ 1 - system_baseline_verify_sysctl_live.stdout | int }}" }
|
||||
|
||||
- name: The pending change is reported
|
||||
ansible.builtin.assert:
|
||||
that: __system_baseline_sysctl_apply is changed
|
||||
quiet: true
|
||||
|
||||
- name: Apply a value that cannot be set
|
||||
block:
|
||||
- name: Include system_baseline, tasks_from sysctl_apply
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.system_baseline
|
||||
tasks_from: sysctl_apply
|
||||
vars:
|
||||
system_baseline_sysctl_hardening:
|
||||
- { name: net.ipv4.tcp_syncookies, value: "{{ 1 - system_baseline_verify_sysctl_live.stdout | int }}" }
|
||||
- name: Unreachable when a refused value fails
|
||||
ansible.builtin.fail:
|
||||
msg: system_baseline ignored a sysctl that could not be set
|
||||
rescue:
|
||||
- name: The refused write stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: ansible_failed_task.name is search('Apply the sysctl hardening settings')
|
||||
quiet: true
|
||||
|
||||
# An empty system_baseline_journal_max_use leaves journald alone: with the drop-in gone, the role neither writes it again nor restarts
|
||||
# systemd-journald. It runs last, because it applies the role with its defaults (the converge's tmux autostart and apt-daily timeout are
|
||||
# not set here) and the checks above read what the converge wrote.
|
||||
|
||||
Reference in new issue
Block a user