❯ Docker and ufw: pf Keeps the Only Ruleset
Seven chains of Docker's own sit in front of your firewall, and ufw goes on saying deny. On FreeBSD, pf reads one file, and a program may write only where that file allows it.
⠀
In June 2021 NewsBlur, a news reader with a loyal following, moved its MongoDB server into a container. ufw was switched on and admitted only the project's own internal servers, precisely as the tutorials recommend. A little over three hours later the database was empty, with a ransom note for 0.03 bitcoin in its place. The post-mortem found the door quickly: "When I containerized MongoDB, Docker helpfully inserted an allow rule into iptables, opening up MongoDB to the world." The firewall still described itself as active, and it simply was not the thing deciding.
⠀
■ WHY DOCKER WRITES ITS OWN RULES
⠀
The reason deserves to come first. A container on Linux lives on a private bridge, and -p 8080:80 promises that a request to port 8080 on the host arrives at port 80 inside. To keep that promise the kernel must rewrite the packet's destination before it decides where the packet goes. Docker cannot know whether the host runs ufw, firewalld, a hand-written nftables file or nothing at all, so it programs the kernel itself, and on a server the firewall now has two authors, one of whom comes in round the back.
⠀
■ FOUR FLOORS
⠀
Docker's documentation explains why ufw never gets a say. ufw keeps its rules in the INPUT chain, whilst published traffic is diverted earlier, in the nat table: "Packets are routed before the firewall rules can be applied, effectively ignoring your firewall configuration."
⠀
In the bridge driver of release 29.10.1 I counted seven chains of its own, every name beginning with DOCKER, plus DOCKER-USER from the documentation for the administrator's exceptions. All of it is kept again for IPv6, and every published port gets a DNAT rule and an ACCEPT rule ahead of anything ufw has said. With rare candour, the same source file admits that one of its functions does something with SCTP that is "broken, and disabled-by-default".
⠀
■ THE HOLES IN THE FLOORS
⠀
The issue "docker and ufw serious problems" was opened in March 2014 and closed four years and 157 comments later, with the behaviour still in the documentation today. Until version 28.0, a port published only on localhost answered machines on the same network segment, which is not quite cricket for an address that means "this machine only". In July 2025 came CVE-2025-54388: reloading firewalld wiped the container rules, and published ports, localhost ones included, became reachable from remote hosts. Nobody had attacked anything; one layer had tidied up after itself and taken another layer's work with it.
⠀
A security group at a cloud provider would have caught that port, but a rented virtual server or a machine in a company rack usually has only its own firewall in front of it.
⠀
■ WHO COLLECTS AND WHO PAYS
⠀
The vendor keeps the command that always works. The administrator pays in rules outside the firewall they set up, written in raw iptables with connection tracking into DOCKER-USER and again for IPv6, or with a localhost address in front of every published port in every compose file until the first colleague forgets one. Which ports are open now has several answers, and ufw status is only one of them.
⠀
■ ONE FILE ON FREEBSD
⠀
pf reads its rules from pf.conf, and FreeBSD ships without one. A program may add rules only inside an anchor, and pf evaluates an anchor's rules when the main ruleset reaches it, so an anchor the file does not name is never reached. pfctl -sA lists every one that exists.
⠀
The server behind this blog runs FreeBSD 15.1, and this morning it gave me its numbers, read-only. Of 95 lines, 37 do any work, and they load as 22 filter rules plus 10 that translate addresses for the jails. The web jail gets its traffic through rdr rules without the pass keyword, so every redirected packet still meets a pass rule written by the same hand. One line names an anchor, for blocklistd from the base system, which locks out addresses that keep failing at the SSH login: one rule and a table of fifteen addresses (fifteen this morning; the number changes, the rule does not). Its helper script can only ever write a block.
⠀
■ THE LIMIT
⠀
Podman on FreeBSD can skip the filter too, since its port forwards are written as rdr pass, but only once the administrator has added its anchor to pf.conf as the Handbook instructs, and the port is marked experimental. An rdr line and a pass line by hand take longer than -p, and they then serve as their own documentation. Linux has repairs, from DOCKER-USER to rootless mode, each one more setting to know on top of the default. And NewsBlur's MongoDB took connections without a password, a risk on any system, pf included.
⠀
A firewall is a list of promises, and on FreeBSD only the person who signs them is allowed to make them.
⠀
#LeanSoftware #FreeBSD #Docker #Linux #Security #SysAdmin
👉 Full essay: https://vivianvoss.net/blog/pf-ruleset
⠀
Disclaimer: these pieces are written between half past seven and half past ten most mornings. AI serves as my secretary and as a second pair of eyes on the facts, so that what reaches you has been checked. Nothing here is automated apart from the deployment to the blog. The argument, the opinions, the examples and the measurements are mine.
⠀
• NewsBlur, the post-mortem: https://blog.newsblur.com/2021/06/28/story-of-a-hacking/
• Docker, packet filtering and firewalls: https://docs.docker.com/engine/network/packet-filtering-firewalls/
• Docker, iptables and DOCKER-USER: https://docs.docker.com/engine/network/firewall-iptables/
• Docker, nftables backend: https://docs.docker.com/engine/network/firewall-nftables/
• Docker, port publishing: https://docs.docker.com/engine/network/port-publishing/
• Docker 29.10.1, iptabler.go: https://github.com/moby/moby/blob/docker-v29.10.1/daemon/libnetwork/drivers/bridge/internal/iptabler/iptabler.go
• Docker 29.10.1, port.go: https://github.com/moby/moby/blob/docker-v29.10.1/daemon/libnetwork/drivers/bridge/internal/iptabler/port.go
• moby issue 4737: https://github.com/moby/moby/issues/4737
• moby issue 45610: https://github.com/moby/moby/issues/45610
• CVE-2025-54388: https://github.com/moby/moby/security/advisories/GHSA-x4rx-4gw3-53p4
• FreeBSD, pf.conf(5): https://github.com/freebsd/freebsd-src/blob/releng/15.1/share/man/man5/pf.conf.5
• FreeBSD Handbook, Firewalls: https://docs.freebsd.org/en/books/handbook/firewalls/
• FreeBSD, blocklistd-helper: https://github.com/freebsd/freebsd-src/blob/releng/15.1/contrib/blocklist/libexec/blocklistd-helper
• FreeBSD, rc.conf defaults: https://github.com/freebsd/freebsd-src/blob/releng/15.1/libexec/rc/rc.conf
• FreeBSD Handbook, Containers: https://docs.freebsd.org/en/books/handbook/containers/
• FreeBSD container networking, portmap.go: https://github.com/dfr/plugins/blob/freebsd-v0.6/plugins/freebsd/meta/portmap/portmap.go
• FreeBSD ports, sysutils/podman: https://github.com/freebsd/freebsd-ports/blob/main/sysutils/podman/pkg-message
• Vivian Voss, Integrated by Design: https://vossscher-verlag.com/en/books/integrated-by-design
⠀