fix(system_baseline): persist sysctls in an own sysctl.d drop-in
Build / build (pull_request) Successful in 1m5s
Lint / lint (pull_request) Successful in 4m54s
Molecule / select (pull_request) Successful in 3s
Molecule / scenarios (pull_request) Successful in 18m54s
Molecule / Result (pull_request) Successful in 0s

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:
Defame1297 committed 2026-12-03 17:23:51 +00:00
1 parent cf99893855
commit d88e5d237a
14 files changed
+282 -17

No files matched your search

@@ -45,9 +45,9 @@ platforms:
provisioner:
name: ansible
options:
# sysctl and tmpfs mounts need a privileged container; a full upgrade adds only
# time; the timezone needs timedatectl. The rest of the role runs for real.
skip-tags: molecule-notest,notest,sysctl,secure_tmp,update,timezone
# Applying sysctls (molecule-notest) and tmpfs mounts need a privileged container; a full upgrade
# adds only time; the timezone needs timedatectl. The rest of the role runs for real.
skip-tags: molecule-notest,notest,secure_tmp,update,timezone
verifier:
name: ansible
scenario:
@@ -1,14 +1,15 @@
---
# A package the role must remove, and the locales tool the role assumes exists
# (it does on a stock Ubuntu host). jq (system_baseline_extra_packages) must be absent, so that the role is what installs it, and ed
# (system_baseline_extra_remove_packages, on no profile list) must be present, so that the role is what purges it.
# (system_baseline_extra_remove_packages, on no profile list) must be present, so that the role is what purges it. procps has the sysctl
# command verify needs.
- name: Prepare
hosts: all
gather_facts: false
tasks:
- name: Install a package on the role's remove list and locale-gen
- name: Install packages the role removes and tools it assumes
ansible.builtin.apt:
name: [telnet, locales, ed]
name: [telnet, locales, ed, procps]
state: present
update_cache: true
@@ -16,3 +17,13 @@
ansible.builtin.apt:
name: jq
state: absent
- name: Seed /etc/sysctl.conf with keys of the role and lines it must leave alone
ansible.builtin.copy:
dest: /etc/sysctl.conf
mode: "0644"
content: |
# net.ipv4.tcp_syncookies=1
net.ipv4.tcp_syncookies=1
vm.max_map_count = 262144
net.ipv4.conf.all.accept_redirects = 0
@@ -0,0 +1,18 @@
---
# One refusal: the role must stop on validation, before it changes anything, with a message that names the variable.
- name: Run the role with a bad system_baseline_sysctl_file
block:
- name: Include system_baseline
ansible.builtin.include_role:
name: mortis.kamino.system_baseline
vars:
system_baseline_system_type: ubuntu_lxc
system_baseline_sysctl_file: "{{ system_baseline_refused_sysctl_file }}"
- name: Unreachable when validation works
ansible.builtin.fail:
msg: system_baseline ran with an invalid system_baseline_sysctl_file
rescue:
- name: The validation stopped the play
ansible.builtin.assert:
that: "'system_baseline_sysctl_file must be' in ansible_failed_result.msg | default('')"
quiet: true
@@ -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.