udwall is a declarative tool to manage UFW and Docker firewall rules using a single Python configuration file. It fixes the Docker security flaw where containers bypass UFW, and it automates rule management so you never have to run manual ufw allow commands again.
UFW is a popular iptables front end on Ubuntu that makes it easy to manage firewall rules. However, when Docker is installed, Docker modifies iptables directly, bypassing UFW rules. This means published ports (e.g., -p 8080:80) are accessible from the outside world, even if UFW is set to deny them.
The issue is detailed as follows (Source: ufw-docker/problem):
- UFW is enabled on a server that provides external services, and all incoming connections that are not allowed are blocked by default.
- Run a Docker container on the server and use the
-poption to publish ports for that container on all IP addresses. For example:docker run -d --name httpd -p 0.0.0.0:8080:80 httpd:alpine. This command will run an httpd service and publish port 80 of the container to port 8080 of the server. - UFW will not block external requests to port 8080. Even the command
ufw deny 8080will not prevent external access to this port because Docker's iptables rules take precedence. - This is a serious security flaw, as internal services can be inadvertently exposed to the public internet.
- Searching for "ufw docker" on the web reveals a lot of discussion on this critical security flaw (source):
The tool ufw-docker solved these issues but had a few drawbacks:
- It fixed the Docker security flaw where containers bypass UFW.
- Prerequisites: It required downloading a script to
/usr/local/binand running it with sudo. - Mechanism: It modified the
/etc/ufw/after.rulesfile to add a customDOCKER-USERchain that correctly filters traffic destined for Docker containers, ensuring UFW rules are respected. (See ufw-docker README for more details).
- Manual Steps: It required a lot of manual steps to manage rules for each container.
- Persistence Issues: Whenever UFW was disabled, Docker ports were still blocked (or rules persisted unexpectedly).
- Difficult Uninstall: To uninstall
ufw-docker, you historically needed to remove iptables rules manually and restart the server (source).Note: Recently
ufw-dockeradded an uninstall command to remove the configuration (source).
udwall is a declarative tool to manage UFW and Docker firewall rules using a single configuration file.
- It fixes the Docker security flaw where containers bypass UFW.
- It automates rule management so you never have to run manual
ufwcommands again. - Configuration as Code: Define your entire firewall state in one file (
udwall.conf). - True Synchronization:
udwallperforms atomic updates, removing old unused rules and applying new ones automatically. - Safety First: Automatically backs up
/etc/ufwandiptablesbefore every change.
You can install udwall with a single command:
curl -fsSL https://raw.githubusercontent.com/HexmosTech/udwall/main/install.sh | sudo bashTo install a specific version (e.g., v0.0.2), run:
curl -fsSL https://raw.githubusercontent.com/HexmosTech/udwall/main/install.sh | sudo bash -s -- --v0.0.2This script will:
- Check for dependencies (
python3,ufw,curl). - Download
udwallto/usr/local/bin/udwall. - Set up a default configuration at
/etc/udwall/udwall.conf.
Currently udwall supports the following rule patterns:
- Docker Forwarding (Any IP): Allow traffic to a Docker container from anywhere.
ufw route allow from any to any port <PORT> proto tcp
- Host Service (Any IP): Allow traffic to a service on the host (e.g., PostgreSQL) from anywhere.
ufw allow <PORT>
- Docker Forwarding (Specific IP): Allow traffic to a Docker container only from a specific IP.
ufw route allow from <IP> to any port <PORT> proto tcp
- Host Service (Specific IP): Allow traffic to a host service only from a specific IP.
ufw allow from <IP> to any port <PORT> proto tcp
- Rule Deletion: Setting
isEnabled: falsewill automatically generate the correspondingdeletecommand for any of the above patterns.
Follow these simple steps to configure and activate udwall on your system. This process ensures your current firewall state is captured and safely managed going forward.
Note:
udwallrequiressudoprivileges.
You can create a configuration file manually or use the --create command to generate one from your current live UFW rules.
sudo udwall --createThis creates a udwall.conf file in /etc/udwall/udwall.conf.
You can create a backup of your current UFW rules with the --backup command.
sudo udwall --backupThis creates a timestamped backup in /home/ubuntu/backup/firewall-backup/, containing both iptables and UFW rules.
Edit the configuration file at /etc/udwall/udwall.conf.
# udwall.conf
rules = [
# Allow SSH access from any source
{'from': 'any', 'connectionType': 'tcp', 'to': 'OpenSSH', 'isDockerServed': False, 'isEnabled': True},
# Allow HTTP and HTTPS traffic to the host
{'from': 'any', 'connectionType': 'tcp', 'to': 80, 'isDockerServed': False, 'isEnabled': True},
{'from': 'any', 'connectionType': 'tcp', 'to': 443, 'isDockerServed': False, 'isEnabled': True},
# Allow traffic to a Docker container on port 8080 from a specific IP
{'from': '192.168.1.100', 'connectionType': 'tcp', 'to': 8080, 'isDockerServed': True, 'isEnabled': True},
# Allow a UDP port range for an application like Mosh
{'from': 'any', 'connectionType': 'udp', 'to': '60000:61000', 'isDockerServed': False, 'isEnabled': True},
]This will back up your current state, remove undefined rules, and apply the new ones based on the configuration file.
sudo udwall --applyBackups are stored in /home/ubuntu/backup/firewall-backup/.
This sets up the iptables rules required to make Docker respect UFW.
sudo udwall --enableThis removes the iptables rules and custom chains, effectively disabling the Docker-UFW integration.
sudo udwall --disable| Command | Description |
|---|---|
sudo udwall --enable |
Initialize: Sets up the Docker-UFW integration and enables UFW. Run this first. |
sudo udwall --apply |
Apply Rules: Reads udwall.conf, backs up current state, and applies the new firewall rules. |
sudo udwall --dry-run |
Preview: Shows exactly which ufw commands would be run, without making any changes. |
sudo udwall --create |
Import: Generates a udwall.conf file at /etc/udwall/udwall.conf based on your current active UFW rules. |
sudo udwall --backup |
Backup: Manually creates a timestamped backup of /etc/ufw and iptables rules in /home/ubuntu/backup/. |
sudo udwall --status |
Check Status: Displays the current UFW status and active rules (numbered). |
sudo udwall --disable |
Uninstall: Removes the Docker-UFW integration, deletes custom chains, and disables UFW. |
sudo udwall --version |
Version: Displays the installed version of udwall. |
sudo udwall --help |
Help: Shows the help message and available options. |
The core iptables logic to fix the Docker/UFW security flaw is based on the work by chaifeng/ufw-docker. udwall extends this by adding declarative state management.