From 74e574ee80d4b88eea508d46d413ad7bd02bd9f9 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Thu, 3 Dec 2026 17:30:49 +0000 Subject: [PATCH] fix(system_baseline): refuse ICMP redirects on every interface With forwarding off the kernel accepts redirects when either `all` or the interface's own value is 1, and over IPv6 only the interface's value counts. The profiles now also set `default` and, with a wildcard, every existing interface, over IPv4 and IPv6. Refs #511 Assisted-by: Claude Code:claude-opus-5-5 --- .../511-system-baseline-icmp-redirects.yml | 5 ++ .../511-system-baseline-sysctl-wildcard.yml | 4 + roles/system_baseline/README.md | 9 +- roles/system_baseline/defaults/main.yml | 18 ++++ .../molecule/default/verify.yml | 85 +++++++++++++++++++ roles/system_baseline/tasks/sysctl_apply.yml | 47 +++++++++- 6 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/511-system-baseline-icmp-redirects.yml create mode 100644 changelogs/fragments/511-system-baseline-sysctl-wildcard.yml diff --git a/changelogs/fragments/511-system-baseline-icmp-redirects.yml b/changelogs/fragments/511-system-baseline-icmp-redirects.yml new file mode 100644 index 0000000..d4d6bf0 --- /dev/null +++ b/changelogs/fragments/511-system-baseline-icmp-redirects.yml @@ -0,0 +1,5 @@ +--- +security_fixes: + - >- + system_baseline - Every profile now refuses ICMP redirects on every interface, over IPv4 and IPv6. Before, only + ``net.ipv4.conf.all`` was set, so a host that does not forward still accepted them on its interfaces. diff --git a/changelogs/fragments/511-system-baseline-sysctl-wildcard.yml b/changelogs/fragments/511-system-baseline-sysctl-wildcard.yml new file mode 100644 index 0000000..9b47a5c --- /dev/null +++ b/changelogs/fragments/511-system-baseline-sysctl-wildcard.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - >- + system_baseline - A name in ``system_baseline_sysctl_hardening`` may contain ``*``, which sets the key on every interface. diff --git a/roles/system_baseline/README.md b/roles/system_baseline/README.md index 0ff0233..b714319 100644 --- a/roles/system_baseline/README.md +++ b/roles/system_baseline/README.md @@ -43,7 +43,7 @@ templating error instead of the role's own message. | `system_baseline_unattended_upgrade_settings` | from profile | no | Keys for `50unattended-upgrades` | | `system_baseline_unattended_upgrade_origins` | from profile | no | Pinned `Origins-Pattern` entries; `[]` keeps the distribution default (see below) | | `system_baseline_unattended_upgrade_package_blacklist` | `[]` | no | Packages unattended-upgrades must not touch, as regular expressions (see below) | -| `system_baseline_sysctl_hardening` | from profile | no | List of `name` and `value` sysctl pairs | +| `system_baseline_sysctl_hardening` | from profile | no | List of `name` and `value` sysctl pairs; `*` in a name matches every interface (see "Sysctl hardening") | | `system_baseline_sysctl_file` | `/etc/sysctl.d/99-zz-system-baseline.conf` | no | Drop-in that persists the sysctl pairs: a `.conf` file directly under `/etc/sysctl.d` (see "Sysctl hardening") | | `system_baseline_secure_tmp_mounts` | from profile | no | Paths remounted as tmpfs with `noexec,nosuid,nodev` | | `system_baseline_journal_max_use` | `200M` | no | Upper bound of the persistent journal (journald `SystemMaxUse`): a number with an optional upper-case `B`, `K`, `M`, `G`, `T`, `P` or `E`, such as `1.5G`; a bare number is bytes. journald does not take `infinity`. Empty leaves the host alone and removes nothing a former run wrote (see "Leaving journald alone") | @@ -137,12 +137,12 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_ - Verify refuses bad items in `system_baseline_extra_packages` and `system_baseline_extra_remove_packages` (whitespace, a null, an empty string, a trailing minus, upper case, a bare string) and a package that one input installs and another purges; converge applies the `ubuntu_lxc` profile with `jq` as an extra package and `ed` as an extra removal; verify checks that `jq` is installed, `ed` is gone and the profile's packages are still there, and that an extra `tmux` installs the snippet where a flat list without it installs none. - Verify also checks that a bad `system_baseline_apt_tmpdir` (under `/tmp` or `/var/tmp`, relative, with `..`, empty) is refused in the main flow and in the `update` entry point. It reads `tasks/update.yml` and checks that the scratch directory is created with mode `0700`, that both apt tasks set `TMPDIR` to it, that the connection is reset afterwards, and that the vzdump guard is imported before the upgrade, whose `when` reads the guard's fact: the scenario skips the `update` tag, so the upgrade itself never runs there. - Verify runs the vzdump guard in check mode on the Ubuntu container with `system_baseline_system_type: pve` and a `pvesh` stub, in seven cases (a running, a finished or no vzdump task, the input off, `ubuntu_lxc`); six run the guard's task file alone and the `ubuntu_lxc` case runs the whole `update` entry point. Each case asserts the fact that decides the upgrade and that `pvesh` was run, with `--source active`, only when it should be. It does not assert that the apt task itself was skipped. The stub stands for the real answer, which no CI image has. -- Prepare seeds `/etc/sysctl.conf` with keys of the role and other lines. Verify checks that the drop-in holds exactly the profile's keys, that only the role's keys left `/etc/sysctl.conf`, that a key removed from the list leaves the drop-in and that a missing `/etc/sysctl.conf` is not created. It runs the runtime apply on the container's read-only `/proc/sys`: no change for the live value, a reported change in check mode and a failure for a value that cannot be set. +- Prepare seeds `/etc/sysctl.conf` with keys of the role and other lines. Verify checks that the drop-in holds exactly the profile's keys, that only the role's keys left `/etc/sysctl.conf`, that a key removed from the list leaves the drop-in and that a missing `/etc/sysctl.conf` is not created. It runs the runtime apply on the container's read-only `/proc/sys`: no change for the live value, a reported change in check mode and a failure for a value that cannot be set, each for a plain key and for a `*`, and a failure for a `*` that matches no key. - Verify ends by running the role once with `system_baseline_journal_max_use: ""` after removing the drop-in, and asserts that no drop-in is written and that `systemd-journald` was not restarted. - Verify runs the `reboot` entry point with `system_baseline_reboot_if_required: false`, without and with `/var/run/reboot-required`, and checks the fact and that the host did not reboot. - The `pve` origins pin is applied to the Ubuntu container and read back with `apt-config dump`, then removed again. The package blacklist is written, read back as a list, left alone by a second run and removed by an empty list. -Not covered: Molecule skips the sysctl runtime apply in the role run, `secure_tmp`, `update` and `timezone` (no privileged container); see `molecule/default/molecule.yml`. So `system_baseline_update` is checked only for its default; the skip itself is not exercised by a test. No test writes a live sysctl value or reboots to see `systemd-sysctl` load the drop-in. No host reboots, and the `pve` profile is not converged: Debian 13 is not verified by a test. +Not covered: Molecule skips the sysctl runtime apply in the role run, `secure_tmp`, `update` and `timezone` (no privileged container); see `molecule/default/molecule.yml`. So `system_baseline_update` is checked only for its default; the skip itself is not exercised by a test. No test writes a live sysctl value, runs without IPv6 or reboots to see `systemd-sysctl` load the drop-in. No host reboots, and the `pve` profile is not converged: Debian 13 is not verified by a test. ## Notes @@ -234,6 +234,9 @@ An upgrade of a Proxmox node can restart Proxmox services while a backup holds a The role sets each pair of `system_baseline_sysctl_hardening` on the running kernel when the live value differs, then writes the list to `system_baseline_sysctl_file`, which `systemd-sysctl` loads at boot. A value that cannot be set stops the run before the file is written. `--tags sysctl` does both. +- Every profile refuses ICMP redirects on every interface: `accept_redirects` is `0` for `all`, `default` and `*` (each interface that exists) over IPv4 and IPv6. `secure_redirects` is `0` for `all` and `default` over IPv4; the kernel reads it only for an interface that accepts redirects. An inventory that overrides the list or a profile's `sysctl_hardening` has to add these keys itself. +- A `*` sets every matching key except those with an entry of their own, so `net.ipv4.conf.eth1.accept_redirects` in the list keeps its value. At boot an entry in any other `sysctl.d` file also wins over a `*`. Write names with dots, and a dot in an interface name as a slash: `net.ipv4.conf.eth0/100.accept_redirects`. Use a `*` only for keys with a single-word value, and no two patterns that match the same key. +- Without IPv6 in the kernel the role skips the `net.ipv6` keys on the running kernel and still persists them. `systemd-sysctl` ignores them at boot; `sysctl -p` on the drop-in reports them as missing and exits 1. - The role owns the whole drop-in: a key removed from the list leaves the file, and its live value stays until the next reboot. When the list repeats a key, the last entry wins. - At boot the last file that sets a key wins, in the order of the file names. The default sorts after `99-sysctl.conf`, but a name that starts with a letter loads later: for example Proxmox VE's `/usr/lib/sysctl.d/pve-firewall.conf`, which sets `net.ipv4.conf.all.rp_filter`. To override such a file, choose a name that sorts after it. - Changing `system_baseline_sysctl_file` does not remove the old file, which is still loaded at boot; remove it by hand. diff --git a/roles/system_baseline/defaults/main.yml b/roles/system_baseline/defaults/main.yml index a46ba4a..6596294 100644 --- a/roles/system_baseline/defaults/main.yml +++ b/roles/system_baseline/defaults/main.yml @@ -110,7 +110,13 @@ system_baseline_profiles: - { name: "kernel.randomize_va_space", value: 2 } - { name: "net.ipv4.conf.all.accept_source_route", value: 0 } - { name: "net.ipv4.conf.all.accept_redirects", value: 0 } + - { name: "net.ipv4.conf.default.accept_redirects", value: 0 } + - { name: "net.ipv4.conf.*.accept_redirects", value: 0 } - { name: "net.ipv4.conf.all.secure_redirects", value: 0 } + - { name: "net.ipv4.conf.default.secure_redirects", value: 0 } + - { name: "net.ipv6.conf.all.accept_redirects", value: 0 } + - { name: "net.ipv6.conf.default.accept_redirects", value: 0 } + - { name: "net.ipv6.conf.*.accept_redirects", value: 0 } - { name: "net.ipv4.icmp_echo_ignore_broadcasts", value: 1 } - { name: "net.ipv4.icmp_ignore_bogus_error_responses", value: 1 } - { name: "net.ipv4.conf.all.log_martians", value: 1 } @@ -173,7 +179,13 @@ system_baseline_profiles: - { name: "net.ipv4.tcp_syncookies", value: 1 } - { name: "net.ipv4.conf.all.accept_source_route", value: 0 } - { name: "net.ipv4.conf.all.accept_redirects", value: 0 } + - { name: "net.ipv4.conf.default.accept_redirects", value: 0 } + - { name: "net.ipv4.conf.*.accept_redirects", value: 0 } - { name: "net.ipv4.conf.all.secure_redirects", value: 0 } + - { name: "net.ipv4.conf.default.secure_redirects", value: 0 } + - { name: "net.ipv6.conf.all.accept_redirects", value: 0 } + - { name: "net.ipv6.conf.default.accept_redirects", value: 0 } + - { name: "net.ipv6.conf.*.accept_redirects", value: 0 } - { name: "net.ipv4.icmp_echo_ignore_broadcasts", value: 1 } - { name: "net.ipv4.icmp_ignore_bogus_error_responses", value: 1 } @@ -234,7 +246,13 @@ system_baseline_profiles: - { name: "kernel.randomize_va_space", value: 2 } - { name: "net.ipv4.conf.all.accept_source_route", value: 0 } - { name: "net.ipv4.conf.all.accept_redirects", value: 0 } + - { name: "net.ipv4.conf.default.accept_redirects", value: 0 } + - { name: "net.ipv4.conf.*.accept_redirects", value: 0 } - { name: "net.ipv4.conf.all.secure_redirects", value: 0 } + - { name: "net.ipv4.conf.default.secure_redirects", value: 0 } + - { name: "net.ipv6.conf.all.accept_redirects", value: 0 } + - { name: "net.ipv6.conf.default.accept_redirects", value: 0 } + - { name: "net.ipv6.conf.*.accept_redirects", value: 0 } - { name: "net.ipv4.icmp_echo_ignore_broadcasts", value: 1 } - { name: "net.ipv4.icmp_ignore_bogus_error_responses", value: 1 } - { name: "net.ipv4.conf.all.log_martians", value: 1 } diff --git a/roles/system_baseline/molecule/default/verify.yml b/roles/system_baseline/molecule/default/verify.yml index f5aca7c..e3bd04f 100644 --- a/roles/system_baseline/molecule/default/verify.yml +++ b/roles/system_baseline/molecule/default/verify.yml @@ -865,7 +865,13 @@ - net.ipv4.tcp_syncookies=1 - net.ipv4.conf.all.accept_source_route=0 - net.ipv4.conf.all.accept_redirects=0 + - net.ipv4.conf.default.accept_redirects=0 + - net.ipv4.conf.*.accept_redirects=0 - net.ipv4.conf.all.secure_redirects=0 + - net.ipv4.conf.default.secure_redirects=0 + - net.ipv6.conf.all.accept_redirects=0 + - net.ipv6.conf.default.accept_redirects=0 + - net.ipv6.conf.*.accept_redirects=0 - net.ipv4.icmp_echo_ignore_broadcasts=1 - net.ipv4.icmp_ignore_bogus_error_responses=1 @@ -954,6 +960,85 @@ that: ansible_failed_task.name is search('Apply the sysctl hardening settings') quiet: true + # The container can inherit the runner host's values. The interfaces share the value of `default`; `all` has its own. + - name: Read the live arp_accept values + ansible.builtin.command: + argv: [cat, /proc/sys/net/ipv4/conf/all/arp_accept, /proc/sys/net/ipv4/conf/default/arp_accept] + register: system_baseline_verify_sysctl_arp + changed_when: false + + - name: Apply a wildcard value that every interface already has + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: sysctl_apply + vars: + system_baseline_sysctl_hardening: + - { name: net.ipv4.conf.all.arp_accept, value: "{{ system_baseline_verify_sysctl_arp.stdout_lines[0] }}" } + - { name: "net.ipv4.conf.*.arp_accept", value: "{{ system_baseline_verify_sysctl_arp.stdout_lines[1] }}" } + + - name: No interface was changed + ansible.builtin.assert: + that: __system_baseline_sysctl_wildcard_apply is not changed + quiet: true + + - name: Preview a different wildcard 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.conf.all.arp_accept, value: "{{ system_baseline_verify_sysctl_arp.stdout_lines[0] }}" } + - { name: "net.ipv4.conf.*.arp_accept", value: "{{ (system_baseline_verify_sysctl_arp.stdout_lines[1] == '0') | ternary(1, 0) }}" } + + - name: The pending change covers the interfaces and not the key with its own entry + ansible.builtin.assert: + that: + - __system_baseline_sysctl_wildcard_apply is changed + - system_baseline_verify_sysctl_cmd is search('net.ipv4.conf.lo.arp_accept=') + - system_baseline_verify_sysctl_cmd is not search('net.ipv4.conf.all.arp_accept=') + quiet: true + vars: + system_baseline_verify_sysctl_cmd: "{{ __system_baseline_sysctl_wildcard_apply.results[0].cmd | join(' ') }}" + + - name: Apply a wildcard 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.conf.*.arp_accept", value: "{{ (system_baseline_verify_sysctl_arp.stdout_lines[1] == '0') | ternary(1, 0) }}" } + - name: Unreachable when a refused wildcard value fails + ansible.builtin.fail: + msg: system_baseline ignored a sysctl wildcard that could not be set + rescue: + - name: The refused wildcard write stopped the play + ansible.builtin.assert: + that: ansible_failed_task.name is search('Apply the sysctl wildcards') + quiet: true + + - name: Apply a wildcard that matches no key + 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.conf.*.accept_redirect", value: 0 } + - name: Unreachable when a wildcard without a match fails + ansible.builtin.fail: + msg: system_baseline ignored a sysctl wildcard that matches no key + rescue: + - name: The wildcard without a match stopped the play + ansible.builtin.assert: + that: ansible_failed_task.name is search('Read the live values a wildcard matches') + 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. diff --git a/roles/system_baseline/tasks/sysctl_apply.yml b/roles/system_baseline/tasks/sysctl_apply.yml index ffd12b9..f93d1f2 100644 --- a/roles/system_baseline/tasks/sysctl_apply.yml +++ b/roles/system_baseline/tasks/sysctl_apply.yml @@ -4,13 +4,19 @@ - name: Read the live sysctl values ansible.builtin.command: argv: [/usr/sbin/sysctl, -e, -n, "{{ item.key }}"] - loop: "{{ __system_baseline_sysctl | dict2items }}" + loop: "{{ __system_baseline_sysctl | dict2items | rejectattr('key', 'search', '[*]') }}" register: __system_baseline_sysctl_live changed_when: false failed_when: false check_mode: false become: true +# The directory is missing when the kernel runs without IPv6; a mistyped IPv6 key still fails when it exists. +- name: Check that the kernel has IPv6 + ansible.builtin.stat: + path: /proc/sys/net/ipv6/conf + register: __system_baseline_sysctl_ipv6 + # `removes` names a path that always exists: with it, check mode reports the pending write as changed instead of skipping the command. # sysctl exits 0 for a read-only key and only warns (`setting key ...`), so stderr counts too. - name: Apply the sysctl hardening settings @@ -22,9 +28,46 @@ loop: "{{ __system_baseline_sysctl_live.results }}" loop_control: label: "{{ item.item.key }}={{ item.item.value }}" - when: item.stdout | default('') | split != item.item.value | split + when: + - item.stdout | default('') | split != item.item.value | split + - __system_baseline_sysctl_ipv6.stat.exists or item.item.key is not match('net[.]ipv6[.]') register: __system_baseline_sysctl_apply failed_when: >- __system_baseline_sysctl_apply.rc | default(0) != 0 or __system_baseline_sysctl_apply.stderr | default('') is search('setting key') become: true + +# After the plain keys: an interface created from here on inherits the new `default`. A wildcard without a match is a typo, as +# an unknown plain key is, unless the kernel has no IPv6. sysctl exits 0 when an interface disappears while it lists them. +- name: Read the live values a wildcard matches + ansible.builtin.command: + argv: [/usr/sbin/sysctl, -a, -e, -r, "^{{ item.key | replace('.', '[.]') | replace('*', '[^.]*') }}$"] + loop: "{{ __system_baseline_sysctl | dict2items | selectattr('key', 'search', '[*]') }}" + register: __system_baseline_sysctl_wildcard + changed_when: false + failed_when: >- + __system_baseline_sysctl_wildcard.rc != 0 + or (__system_baseline_sysctl_wildcard.stdout | length == 0 + and (__system_baseline_sysctl_ipv6.stat.exists or item.key is not match('net[.]ipv6[.]'))) + check_mode: false + become: true + +# A key with its own entry keeps that value, as at boot. -e skips an interface that is gone, unless it goes during the write itself. +- name: Apply the sysctl wildcards + ansible.builtin.command: + argv: "{{ ['/usr/sbin/sysctl', '-e', '-w'] + (__system_baseline_sysctl_pending | product([item.item.value]) | map('join', '=')) }}" + removes: /proc/sys + environment: + LC_ALL: C + vars: + __system_baseline_sysctl_pending: >- + {{ item.stdout_lines | reject('search', ' = ' ~ (item.item.value | regex_escape) ~ '$') + | map('regex_replace', ' = .*', '') | difference(__system_baseline_sysctl.keys() | list) }} + loop: "{{ __system_baseline_sysctl_wildcard.results }}" + loop_control: + label: "{{ item.item.key }}={{ item.item.value }}" + when: __system_baseline_sysctl_pending | length > 0 + register: __system_baseline_sysctl_wildcard_apply + failed_when: >- + __system_baseline_sysctl_wildcard_apply.stderr_lines | default([]) | reject('search', 'No such file or directory') | length > 0 + become: true