Remove ufw or Mask the ufw Service

Description

When nftables or iptables configures the firewall, the Uncomplicated Firewall (ufw) is removed, or it is inactive with its service masked. Remove the ufw package with the following command:

$ sudo apt purge ufw

or disable ufw and mask its service with the following commands:

$ sudo ufw disable
$ sudo systemctl stop ufw.service
$ sudo systemctl mask ufw.service

Rationale

Running ufw along with another firewall utility may lead to conflicts and unexpected results.

Remediation

Shell script

The following script can be run on the host to remediate the issue.

#!/bin/bash

# Remediation is applicable only in certain platforms
if dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$' && { ( ( dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$' && ( dpkg-query --show --showformat='${db:Status-Status}' 'iptables-persistent' 2>/dev/null | grep -q '^installed$' || { dpkg-query --show --showformat='${db:Status-Status}' 'nftables' 2>/dev/null | grep -q '^installed$' && systemctl is-enabled nftables.service 2>/dev/null | grep -q '^enabled'; } ) ) ); }; then

SYSTEMCTL_EXEC='/usr/bin/systemctl'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
  "$SYSTEMCTL_EXEC" stop 'ufw.service'
fi
"$SYSTEMCTL_EXEC" disable 'ufw.service'
"$SYSTEMCTL_EXEC" mask 'ufw.service'
# Disable socket activation if we have a unit file for it
if "$SYSTEMCTL_EXEC" -q list-unit-files ufw.socket; then
    if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
      "$SYSTEMCTL_EXEC" stop 'ufw.socket'
    fi
    "$SYSTEMCTL_EXEC" mask 'ufw.socket'
fi
# The service may not be running because it has been started and failed,
# so let's reset the state so OVAL checks pass.
# Service should be 'inactive', not 'failed' after reboot though.
"$SYSTEMCTL_EXEC" reset-failed 'ufw.service' || true

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi

Ansible playbook

The following playbook can be run with Ansible to remediate the issue.

- name: Gather the package facts
  package_facts:
    manager: auto
  tags:
  - disable_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - service_ufw_disabled

- name: Gather the service facts
  ansible.builtin.service_facts: null
  tags:
  - disable_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - service_ufw_disabled

- name: Remove ufw or Mask the ufw Service - Disable service ufw
  block:

  - name: Remove ufw or Mask the ufw Service - Collect systemd Services Present in
      the System
    ansible.builtin.service_facts: null
    check_mode: false

  - name: Remove ufw or Mask the ufw Service - Ensure ufw.service is Masked
    ansible.builtin.systemd:
      name: ufw.service
      state: stopped
      enabled: false
      masked: true
    when: '"ufw.service" in ansible_facts.services'

  - name: Unit Socket Exists - ufw.socket
    ansible.builtin.find:
      paths:
      - /etc/systemd/system
      - /run/systemd/system
      - /usr/lib/systemd/system
      - /lib/systemd/system
      - /usr/local/lib/systemd/system
      patterns: ufw.socket
      file_type: any
    register: socket_file_exists
    check_mode: false

  - name: Remove ufw or Mask the ufw Service - Disable Socket ufw
    ansible.builtin.systemd:
      name: ufw.socket
      enabled: false
      state: stopped
      masked: true
    when: socket_file_exists.matched > 0
  tags:
  - disable_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - service_ufw_disabled
  - special_service_block
  when:
  - '"linux-base" in ansible_facts.packages'
  - ( "linux-base" in ansible_facts.packages and ( "iptables-persistent" in ansible_facts.packages
    or ("nftables" in ansible_facts.packages and (((ansible_facts.services | default({}))["nftables.service"]
    | default({})).status | default("")) in ["enabled", "enabled-runtime"]) ) )