fix(ufw): mask nftables.service so an upgrade cannot flush the rules
When nftables.service is enabled, the nftables package's postinst restarts it on upgrade. The stock /etc/nftables.conf begins with `flush ruleset` and the unit's ExecStop flushes too, so every rule ufw and Docker loaded was deleted until they reloaded. The role now masks the unit without stopping it, gated on ufw_enabled and the new opt-out input ufw_mask_nftables. systemd refuses every job but a stop on a masked unit, so the postinst's try-restart fails before anything runs (the postinst ignores the failure) and the unit does not start at boot. Masking works when the unit is absent too. Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
f7da80b326
commit
c617bee02a
9 files changed
+195
-2
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
minor_changes:
|
||||
- >-
|
||||
ufw - New input ``ufw_mask_nftables`` (default ``true``) and tag ``ufw_nftables``. Set the input to ``false``
|
||||
on a host that loads its own ruleset through ``nftables.service``; the role never unmasks the unit.
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
bugfixes:
|
||||
- >-
|
||||
ufw - An upgrade of the ``nftables`` package no longer deletes the firewall rules on hosts where
|
||||
``nftables.service`` is enabled. The package's postinst restarts that unit, whose stock ``/etc/nftables.conf``
|
||||
begins with ``flush ruleset``, so the rules ufw and Docker loaded were gone until they reloaded. The role now
|
||||
masks ``nftables.service`` without stopping it whenever ``ufw_enabled`` is true, also when the unit or the
|
||||
package is absent, so the first run reports ``changed`` once. A host that loads its own ruleset through
|
||||
``nftables.service`` (NAT tables or sets, for example) would lose it at the next reboot without a warning, because
|
||||
a masked unit does not start: set ``ufw_mask_nftables: false`` on such a host before upgrading.
|
||||
+5
-2
@@ -29,6 +29,7 @@ Main entry point:
|
||||
| `ufw_manage_rules` | `true` | no | Skip the `ufw_rules` schema check, the lock-out guard and rule application when false. Before-rules validation and `before.rules`, defaults and enable still run (gated on `ufw_enabled`). |
|
||||
| `ufw_defaults` | `incoming: deny`, `outgoing: allow`, `routed: deny` | no | Default policies. All three keys are read, so override all of them. |
|
||||
| `ufw_logging` | `"on"` | no | UFW logging level. |
|
||||
| `ufw_mask_nftables` | `true` | no | Mask `nftables.service` without stopping it, so a package upgrade cannot flush the rules ufw loaded (see Notes). Set false on a host that loads its own ruleset through that unit. Gated on `ufw_enabled`. |
|
||||
| `ufw_require_ssh_allow` | `true` | no | Enable the SSH lock-out guard. |
|
||||
| `ufw_ssh_port` | `ansible_port` or 22 | no | TCP port the guard looks for. |
|
||||
| `ufw_rules` | `[]` | no | Base rule list: `rule` (allow, deny, reject, limit), `port`, `proto`, `from`, `to`, `interface`, `direction`, `comment`, `delete` (see Notes). |
|
||||
@@ -117,7 +118,7 @@ An **orphan** is a rule tagged `ansible-ufw[<owner>:<key>]` whose owner is neith
|
||||
|
||||
## Tags
|
||||
|
||||
`ufw` (everything), `ufw_install`, `ufw_sysctl`, `ufw_aggregate`, `ufw_validate`, `ufw_before_rules`, `ufw_defaults`, `ufw_rules`, `ufw_enable`.
|
||||
`ufw` (everything), `ufw_install`, `ufw_sysctl`, `ufw_aggregate`, `ufw_validate`, `ufw_nftables`, `ufw_before_rules`, `ufw_defaults`, `ufw_rules`, `ufw_enable`.
|
||||
|
||||
Skipping `ufw_aggregate` leaves the lists the later tasks read (`ufw_aggregated_rules`, `ufw_aggregated_before_rules`) undefined or stale, so do not select a later tag alone.
|
||||
|
||||
@@ -144,10 +145,11 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_
|
||||
- Verify then reads `ufw status verbose` and `ufw show added`: ufw is active with default deny incoming, the base rules are loaded, the deleted rule is gone, the manual and SSH rules are untouched, the service rules carry their owner tag, stale and orphan rules are deleted only where purging was on and only when they cannot carry SSH.
|
||||
- Verify checks `before.rules`: the loopback accept, the INVALID-state drop behind its logging rule and in its place among the input rules.
|
||||
- Verify runs `service_rules` under the `check_mode` keyword and checks that the rule list did not change.
|
||||
- Verify checks the nftables mask: prepare installs `nftables`, enables and starts its unit; after the converge the unit is masked and still active, ufw's chains survive `apt-get install --reinstall nftables` (whose postinst runs the upgrade restart), the same reinstall with the mask removed flushes them (the control), and after the package is purged and the mask removed the role masks the absent unit again without failing.
|
||||
|
||||
The scenario `molecule/check-absent/` runs on the same two platforms: prepare purges ufw and refreshes the APT cache, then `check` runs the main entry point and `service_rules` under `--check`, which must pass with ufw not installed. Nothing is applied.
|
||||
|
||||
Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set, and `orphan_rules` runs in check mode only under `--check` (the `check` steps), not under the `check_mode` keyword.
|
||||
Not covered: `ufw_mask_nftables: false` is never set, `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set, and `orphan_rules` runs in check mode only under `--check` (the `check` steps), not under the `check_mode` keyword.
|
||||
|
||||
## Notes
|
||||
|
||||
@@ -158,5 +160,6 @@ Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set
|
||||
- Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, and each rule needs `port`, `from`, `to` or `interface`.
|
||||
- UFW is only re-enabled when `ufw status verbose` shows it inactive or the logging line differs, because the module rewrites rules and re-applies sysctl on every call.
|
||||
- The `Reload UFW` handler runs `ufw reload`.
|
||||
- nftables: when `nftables.service` is enabled (a preset can do that), the `nftables` package's postinst restarts it on upgrade (`dh_installsystemd --no-enable --no-start --restart-after-upgrade` emits `deb-systemd-invoke try-restart`), and both the restart and the unit's `ExecStop=/usr/sbin/nft flush ruleset` delete every rule ufw (and Docker) loaded, since the stock `/etc/nftables.conf` begins with `flush ruleset`. The role masks the unit and does not stop it, so nothing is flushed during the run. systemd refuses every job but a stop on a masked unit, so the postinst's restart fails before anything runs and the postinst ignores the failure (`|| true`); at boot the unit is not started either. The mask also applies when the unit or the package is absent, so a later install cannot enable it; the package's postinst removes only a mask its own helper created. The role never unmasks: with `ufw_mask_nftables: false` or `ufw_enabled: false` an existing mask stays. `--check` reports the mask without applying it. When `/etc/systemd/system/nftables.service` is a unit file of its own rather than a link, `systemctl mask` refuses with "already exists" and the play fails; set `ufw_mask_nftables: false` on such a host.
|
||||
- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host without ufw the preview reports the install and skips the configuration (`before.rules`, default policies, rules, enable), which needs the `ufw` binary.
|
||||
- `no_log` is not used: the role handles no secrets.
|
||||
@@ -9,6 +9,10 @@ ufw_defaults:
|
||||
|
||||
ufw_logging: "on"
|
||||
|
||||
# Mask nftables.service (not stopped), so a restart by a package upgrade cannot flush the rules
|
||||
# ufw loaded. false: leave the unit alone, for hosts that load their own ruleset through it.
|
||||
ufw_mask_nftables: true
|
||||
|
||||
# Lock-out guard: with a deny/reject incoming default the play refuses to run
|
||||
# unless an allow/limit rule for this TCP port is in the rule list, since the
|
||||
# default is applied before the rules and a missing rule cuts the SSH session.
|
||||
|
||||
@@ -49,6 +49,18 @@ argument_specs:
|
||||
choices: ["on", "off", low, medium, high, full]
|
||||
description:
|
||||
- UFW logging setting. UFW is only re-enabled when it is inactive or its current logging line differs from this value.
|
||||
ufw_mask_nftables:
|
||||
type: bool
|
||||
default: true
|
||||
description:
|
||||
- >-
|
||||
When true (and C(ufw_enabled) is true) C(nftables.service) is masked, without being stopped, also when the
|
||||
unit or the C(nftables) package is absent. A restart of the enabled unit, which the package runs on upgrade,
|
||||
loads the stock C(/etc/nftables.conf) that begins with C(flush ruleset), and its stop flushes the ruleset,
|
||||
which deletes the rules ufw loaded. A masked unit is never started, also not at boot.
|
||||
- >-
|
||||
Set false on hosts that load their own ruleset through C(nftables.service). The role never unmasks the unit,
|
||||
so switching to false leaves an existing mask in place.
|
||||
ufw_require_ssh_allow:
|
||||
type: bool
|
||||
default: true
|
||||
|
||||
@@ -84,3 +84,40 @@
|
||||
- {port: "40:50", comment: "ansible-ufw[svc_c:6666666663]: range"}
|
||||
loop_control:
|
||||
loop_var: ufw_prepare_rule
|
||||
|
||||
# An enabled, active nftables.service with the stock /etc/nftables.conf, as a preset leaves
|
||||
# it; verify.yml checks that the role's mask keeps a package upgrade from flushing ufw's rules.
|
||||
- name: Install nftables
|
||||
ansible.builtin.apt:
|
||||
name: nftables
|
||||
state: present
|
||||
|
||||
# A policy-rc.d that refuses actions would make the postinst skip the restart verify.yml tests.
|
||||
- name: Let maintainer scripts act on units as on a host
|
||||
ansible.builtin.file:
|
||||
path: /usr/sbin/policy-rc.d
|
||||
state: absent
|
||||
|
||||
# ProtectSystem and ProtectHome need a mount namespace, which needs CAP_SYS_ADMIN; without
|
||||
# it the unit fails with 226/NAMESPACE in this unprivileged container.
|
||||
- name: Create the drop-in directory of nftables.service
|
||||
ansible.builtin.file:
|
||||
path: /etc/systemd/system/nftables.service.d
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: Disable the namespace-based sandboxing of nftables.service for the container
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/nftables.service.d/molecule.conf
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Service]
|
||||
ProtectSystem=no
|
||||
ProtectHome=no
|
||||
|
||||
- name: Enable and start nftables.service
|
||||
ansible.builtin.systemd_service:
|
||||
name: nftables.service
|
||||
enabled: true
|
||||
state: started
|
||||
daemon_reload: true
|
||||
@@ -521,3 +521,110 @@
|
||||
- ufw_verify_chk_after.stdout_lines == ufw_verify_chk_before.stdout_lines
|
||||
- ufw_verify_chk_after.stdout_lines | select('search', 'svc_chk|7701') | list | length == 0
|
||||
quiet: true
|
||||
|
||||
# prepare.yml left nftables.service enabled and active. The package's postinst runs
|
||||
# `deb-systemd-invoke try-restart nftables.service` on upgrade (and on a reinstall, which passes
|
||||
# the old version too); the stock config and ExecStop flush the ruleset. The masked case runs
|
||||
# first, then a control without the mask proves the reinstall reaches the unit here, then the
|
||||
# absent-package case. Last, because the control flushes ufw's rules.
|
||||
- name: The nftables mask keeps a package upgrade from flushing the rules
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
ufw_verify_nft_reinstall:
|
||||
- apt-get
|
||||
- install
|
||||
- --reinstall
|
||||
- -y
|
||||
- -o
|
||||
- Dpkg::Options::=--force-confold
|
||||
- nftables
|
||||
tasks:
|
||||
# A mask request in check mode changes nothing and returns the unit's properties.
|
||||
- name: Read the state of nftables.service
|
||||
ansible.builtin.systemd_service:
|
||||
name: nftables.service
|
||||
masked: true
|
||||
check_mode: true
|
||||
register: ufw_verify_nft_unit
|
||||
|
||||
- name: The role masked the unit and did not stop it
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ufw_verify_nft_unit is not changed
|
||||
- ufw_verify_nft_unit.status.LoadState == 'masked'
|
||||
- ufw_verify_nft_unit.status.ActiveState == 'active'
|
||||
quiet: true
|
||||
|
||||
- name: Reinstall nftables while the unit is masked
|
||||
ansible.builtin.command:
|
||||
argv: "{{ ufw_verify_nft_reinstall }}"
|
||||
environment: {DEBIAN_FRONTEND: noninteractive}
|
||||
changed_when: true
|
||||
|
||||
- name: Read the iptables rules after the masked reinstall
|
||||
ansible.builtin.command: iptables -S
|
||||
register: ufw_verify_nft_masked_rules
|
||||
changed_when: false
|
||||
|
||||
- name: The rules of ufw survived the reinstall
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ufw_verify_nft_masked_rules.stdout_lines | select('search', '^-N ufw-user-input$') | list | length == 1
|
||||
- ufw_verify_nft_masked_rules.stdout_lines | select('search', '^-A ufw-user-input .*--dport 22 ') | list | length > 0
|
||||
quiet: true
|
||||
|
||||
- name: Remove the mask for the control case
|
||||
ansible.builtin.systemd_service:
|
||||
name: nftables.service
|
||||
masked: false
|
||||
|
||||
- name: Reinstall nftables while the unit is enabled and active
|
||||
ansible.builtin.command:
|
||||
argv: "{{ ufw_verify_nft_reinstall }}"
|
||||
environment: {DEBIAN_FRONTEND: noninteractive}
|
||||
changed_when: true
|
||||
|
||||
- name: Read the iptables rules after the unmasked reinstall
|
||||
ansible.builtin.command: iptables -S
|
||||
register: ufw_verify_nft_unmasked_rules
|
||||
changed_when: false
|
||||
|
||||
- name: Without the mask the reinstall flushed the rules of ufw
|
||||
ansible.builtin.assert:
|
||||
that: ufw_verify_nft_unmasked_rules.stdout_lines | select('search', '^-N ufw-user-input$') | list | length == 0
|
||||
quiet: true
|
||||
|
||||
- name: Purge nftables
|
||||
ansible.builtin.apt:
|
||||
name: nftables
|
||||
state: absent
|
||||
purge: true
|
||||
|
||||
- name: Read the state of nftables.service with the package gone
|
||||
ansible.builtin.systemd_service:
|
||||
name: nftables.service
|
||||
masked: true
|
||||
check_mode: true
|
||||
register: ufw_verify_nft_gone
|
||||
|
||||
- name: Run the role on a host without the nftables package
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.ufw
|
||||
vars:
|
||||
ufw_rules:
|
||||
- {rule: allow, port: 22, proto: tcp, comment: ssh}
|
||||
|
||||
- name: Read the state of nftables.service after the role
|
||||
ansible.builtin.systemd_service:
|
||||
name: nftables.service
|
||||
masked: true
|
||||
check_mode: true
|
||||
register: ufw_verify_nft_absent
|
||||
|
||||
- name: The role masked the absent unit without failing
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ufw_verify_nft_gone is changed
|
||||
- ufw_verify_nft_absent is not changed
|
||||
quiet: true
|
||||
@@ -35,6 +35,10 @@
|
||||
ansible.builtin.import_tasks: validate.yml
|
||||
tags: [ufw, ufw_validate]
|
||||
|
||||
- name: Import UFW nftables task
|
||||
ansible.builtin.import_tasks: nftables.yml
|
||||
tags: [ufw, ufw_nftables]
|
||||
|
||||
- name: Import UFW before rules task
|
||||
ansible.builtin.import_tasks: before_rules.yml
|
||||
tags: [ufw, ufw_before_rules]
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
# A restart of nftables.service (on a package upgrade) flushes the rules ufw loaded. Mask only:
|
||||
# a stop would flush them now. README Notes has the detail.
|
||||
- name: Mask nftables.service without stopping it
|
||||
become: true
|
||||
ansible.builtin.systemd_service:
|
||||
name: nftables.service
|
||||
masked: true
|
||||
when:
|
||||
- ufw_enabled
|
||||
- ufw_mask_nftables
|
||||
Reference in new issue
Block a user