feat(ufw): filter Docker-published ports through DOCKER-USER (proposal) #158

Open
opened 2026-11-15 12:32:59 +00:00 by Dragonish · 2 comments
Collaborator

What

Opt-in filtering of the ports that Docker publishes, from the ufw role: ufw_docker_enforce (default false) and ufw_docker_user_ports, a list of {port, proto, from[], comment}. Proposal first, because it is a design decision for the role and I would like the maintainer's view before writing it.

Why

The docker role README says it plainly: "Docker bypasses UFW for published ports (-p), so UFW rules do not restrict container ports." On a host that runs both roles, a ufw rule set that looks closed is open for every published container port. In a home lab that typically means an admin UI or database on a container is reachable from every VLAN even though ufw status says otherwise. The only supported place to filter that traffic is Docker's DOCKER-USER chain, which ufw does not manage.

Proposed mechanism (one option)

A second *filter section is appended to /etc/ufw/after.rules with blockinfile, validated with iptables-restore --test --noflush. ufw loads it on every reload and at boot, before Docker starts. It declares the DOCKER-USER chain, which Docker then uses and leaves alone:

  • Allow RELATED,ESTABLISHED.
  • For each published port: RETURN for each allowed source, then DROP the rest.
  • Matching uses -m conntrack --ctorigdst <default address> --ctorigdstport <port> on the default interface, so container-to-container and egress traffic is untouched (DNAT has already happened by the time DOCKER-USER sees a packet, so the original destination has to be matched).
  • Optional catch-all DROP for published ports that are not listed.

It is opt-in, so nothing changes for existing consumers. I run this in my own repository.

Things to decide together

  • Mechanism. The alternatives are ufw-docker-style rules in before.rules, or Docker's --iptables=false (which breaks container networking). Is the after.rules block acceptable, or is there a design you prefer?
  • IPv4 only. The block is IPv4. Is a stated limitation enough, or should it cover IPv6 from the start?
  • Ordering. A partial apply can lock a host out. The block must be written and ufw reloaded before any default-deny change. main.yml runs defaults.yml before rules.yml and has no handler flush today, so the PR would need a meta: flush_handlers at the right point.
  • Testing. Docker plus iptables in the molecule container is awkward. I would test the rendered block with iptables-restore --test and the template output, and say clearly in the PR what molecule cannot cover.

Scope

Role vars carry the ufw_ prefix, typed in argument_specs.yml, one new task file and one template, README, a changelog fragment and a molecule scenario step. No homelab names or inventory coupling (our version resolves allowed sources from inventory; that part would not be included).

## What Opt-in filtering of the ports that Docker publishes, from the `ufw` role: `ufw_docker_enforce` (default `false`) and `ufw_docker_user_ports`, a list of `{port, proto, from[], comment}`. Proposal first, because it is a design decision for the role and I would like the maintainer's view before writing it. ## Why The `docker` role README says it plainly: "Docker bypasses UFW for published ports (`-p`), so UFW rules do not restrict container ports." On a host that runs both roles, a `ufw` rule set that looks closed is open for every published container port. In a home lab that typically means an admin UI or database on a container is reachable from every VLAN even though `ufw status` says otherwise. The only supported place to filter that traffic is Docker's `DOCKER-USER` chain, which ufw does not manage. ## Proposed mechanism (one option) A second `*filter` section is appended to `/etc/ufw/after.rules` with `blockinfile`, validated with `iptables-restore --test --noflush`. ufw loads it on every reload and at boot, before Docker starts. It declares the `DOCKER-USER` chain, which Docker then uses and leaves alone: - Allow `RELATED,ESTABLISHED`. - For each published port: `RETURN` for each allowed source, then `DROP` the rest. - Matching uses `-m conntrack --ctorigdst <default address> --ctorigdstport <port>` on the default interface, so container-to-container and egress traffic is untouched (DNAT has already happened by the time `DOCKER-USER` sees a packet, so the original destination has to be matched). - Optional catch-all `DROP` for published ports that are not listed. It is opt-in, so nothing changes for existing consumers. I run this in my own repository. ## Things to decide together - **Mechanism.** The alternatives are `ufw-docker`-style rules in `before.rules`, or Docker's `--iptables=false` (which breaks container networking). Is the `after.rules` block acceptable, or is there a design you prefer? - **IPv4 only.** The block is IPv4. Is a stated limitation enough, or should it cover IPv6 from the start? - **Ordering.** A partial apply can lock a host out. The block must be written and `ufw` reloaded before any default-deny change. `main.yml` runs `defaults.yml` before `rules.yml` and has no handler flush today, so the PR would need a `meta: flush_handlers` at the right point. - **Testing.** Docker plus iptables in the molecule container is awkward. I would test the rendered block with `iptables-restore --test` and the template output, and say clearly in the PR what molecule cannot cover. ## Scope Role vars carry the `ufw_` prefix, typed in `argument_specs.yml`, one new task file and one template, README, a changelog fragment and a molecule scenario step. No homelab names or inventory coupling (our version resolves allowed sources from inventory; that part would not be included).
Dragonish added the Kind/Security label 2026-11-15 12:32:59 +00:00
Defame1297 added the
Priority
Medium
3
Status
Needs Triage
labels 2026-11-15 12:47:23 +00:00
Owner

Triage notes on this proposal.

The problem is real. The docker role README says Docker bypasses UFW for published ports, and the ufw role doesn't touch DOCKER-USER. A host running both roles can look closed in ufw status while container ports are reachable from every VLAN. An opt-in switch that changes nothing for existing consumers is a reasonable shape.

The mechanism needs a decision before any code is written. This is the most invasive change proposed so far.

  • Hand-written iptables in after.rules creates a second rule model next to ufw's own. The established alternative (the ufw-docker approach) jumps DOCKER-USER into ufw's forward chain, so rules are managed with ufw route and show up in ufw status. Matching on the original destination port, as proposed here, is arguably nicer for operators. Please compare the two and say which you would pick and why. The maintainer decides.
  • IPv4-only is acceptable only if the README and argument_specs say so prominently. A host with IPv6 published ports would otherwise be silently unfiltered.
  • The lockout ordering (block written and ufw reloaded before any default-deny change) needs a concrete answer, not "we'll see in the PR". Please describe where meta: flush_handlers goes and what happens on a partial apply.
  • Molecule can't run Docker with iptables here, so the core behaviour would ship tested only through iptables-restore --test and template output. The PR would need to say that plainly.

Provenance. You say you run this in your own repository. Please confirm you are happy to contribute it under GPL-3.0-or-later, and that no homelab names or inventory coupling come with it (your scope section says none).

Status: keeping the problem as Priority/Medium, but this stays out of Ready for Agent until the mechanism is agreed. Please reply with your preferred mechanism.

Triage notes on this proposal. **The problem is real.** The `docker` role README says Docker bypasses UFW for published ports, and the `ufw` role doesn't touch `DOCKER-USER`. A host running both roles can look closed in `ufw status` while container ports are reachable from every VLAN. An opt-in switch that changes nothing for existing consumers is a reasonable shape. **The mechanism needs a decision before any code is written.** This is the most invasive change proposed so far. - Hand-written iptables in `after.rules` creates a second rule model next to ufw's own. The established alternative (the `ufw-docker` approach) jumps `DOCKER-USER` into ufw's forward chain, so rules are managed with `ufw route` and show up in `ufw status`. Matching on the original destination port, as proposed here, is arguably nicer for operators. Please compare the two and say which you would pick and why. The maintainer decides. - IPv4-only is acceptable only if the README and `argument_specs` say so prominently. A host with IPv6 published ports would otherwise be silently unfiltered. - The lockout ordering (block written and ufw reloaded before any default-deny change) needs a concrete answer, not "we'll see in the PR". Please describe where `meta: flush_handlers` goes and what happens on a partial apply. - Molecule can't run Docker with iptables here, so the core behaviour would ship tested only through `iptables-restore --test` and template output. The PR would need to say that plainly. **Provenance.** You say you run this in your own repository. Please confirm you are happy to contribute it under GPL-3.0-or-later, and that no homelab names or inventory coupling come with it (your scope section says none). Status: keeping the problem as Priority/Medium, but this stays out of `Ready for Agent` until the mechanism is agreed. Please reply with your preferred mechanism.
Claude added
Status
Need More Info
2
and removed
Status
Needs Triage
labels 2026-11-15 13:21:46 +00:00
Author
Collaborator

Thanks for the triage. Answers in the order of your notes. I will not open a PR until you mark this Ready for Agent.

1. Mechanism: my preference, and why. I compared the two against the ufw-docker README (not from memory).

  • ufw-docker style: DOCKER-USER jumps to ufw-user-forward, rules are ufw route allow ... and show up in ufw status. That is its real strength. But rules match the container port after DNAT (publish -p 8080:80 and you write 80), several containers on the same port need container IPs, and its rules return every RFC1918 source (10/8, 172.16/12, 192.168/16) untouched. It separates public from private. It does not separate one private VLAN from another.
  • This proposal: matches the published address and port (--ctorigdst/--ctorigdstport), with an allow list of sources per port. -p 8080:80 is written as 8080, and it can restrict a VLAN from another VLAN, which is the case that matters on a segmented home lab. The costs: it is a second rule model that does not appear in ufw status, and it is IPv4 only.
  • Recommendation: this one, opt-in (nothing changes for anyone who does not set ufw_docker_enforce). The README will have an "Alternatives and design choice" section that names the ufw-docker approach, says who it fits better (public versus private only), and states that the maintainer decided. If you prefer the ufw route model, I will write that instead; I will not offer both behind a switch, it doubles what you have to review.

2. IPv6. The README and argument_specs already say IPv4 only, in bold. I will also add a warning task that fires when /etc/default/ufw has IPV6=yes while the feature is on, so a dual-stack host does not read "enforced" as "everything is filtered". I will not add ip6tables rules in this PR.

3. Lockout ordering. The order in main.yml is: before_rules.yml, then docker_user.yml (writes the block), then meta: flush_handlers (loads it with a ufw reload), then defaults.yml (the default policies), rules.yml, enable.yml.

  • The block is written with blockinfile and validate: iptables-restore --test --noflush, so an unparseable file is never written.
  • If writing the block or the reload fails, the play stops before defaults.yml: the host keeps its old policy.
  • If defaults.yml fails after the reload, the block is already loaded. It only drops the listed ports (and, with ufw_docker_drop_unlisted: true, other published ports on the default address), so there is no state where a deny default exists without the block.
  • One side effect: the flush also loads before.rules before the defaults, which it did not do before. I think that is harmless and it is in the changelog, but tell me if you would rather keep the reload where it is.

4. Testing. Molecule cannot run Docker with iptables on the runner, so the PR will say plainly that real behaviour is not covered there: it asserts the block in after.rules, that iptables-restore --test accepts the whole file, idempotence, removal with the feature off, and rejected inputs. Before I open the PR I will also apply it once on a throwaway VM with real Docker and report the result in the PR.

5. Provenance. Yes: I contribute this under GPL-3.0-or-later. I wrote it, it comes with no homelab names or inventory coupling (our version resolves allowed sources from inventory and that part is not included), and I did not copy any rules from ufw-docker or any other project; I only compared with it.

I am marking this ready for your input on the mechanism.

Assisted-by: Claude Code

Thanks for the triage. Answers in the order of your notes. I will not open a PR until you mark this `Ready for Agent`. **1. Mechanism: my preference, and why.** I compared the two against the `ufw-docker` README (not from memory). - **`ufw-docker` style:** `DOCKER-USER` jumps to `ufw-user-forward`, rules are `ufw route allow ...` and show up in `ufw status`. That is its real strength. But rules match the *container* port after DNAT (publish `-p 8080:80` and you write `80`), several containers on the same port need container IPs, and its rules return every RFC1918 source (`10/8`, `172.16/12`, `192.168/16`) untouched. It separates public from private. It does not separate one private VLAN from another. - **This proposal:** matches the *published* address and port (`--ctorigdst`/`--ctorigdstport`), with an allow list of sources per port. `-p 8080:80` is written as `8080`, and it can restrict a VLAN from another VLAN, which is the case that matters on a segmented home lab. The costs: it is a second rule model that does not appear in `ufw status`, and it is IPv4 only. - **Recommendation:** this one, opt-in (nothing changes for anyone who does not set `ufw_docker_enforce`). The README will have an "Alternatives and design choice" section that names the `ufw-docker` approach, says who it fits better (public versus private only), and states that the maintainer decided. If you prefer the `ufw route` model, I will write that instead; I will not offer both behind a switch, it doubles what you have to review. **2. IPv6.** The README and `argument_specs` already say IPv4 only, in bold. I will also add a warning task that fires when `/etc/default/ufw` has `IPV6=yes` while the feature is on, so a dual-stack host does not read "enforced" as "everything is filtered". I will not add `ip6tables` rules in this PR. **3. Lockout ordering.** The order in `main.yml` is: `before_rules.yml`, then `docker_user.yml` (writes the block), then `meta: flush_handlers` (loads it with a ufw reload), then `defaults.yml` (the default policies), `rules.yml`, `enable.yml`. - The block is written with `blockinfile` and `validate: iptables-restore --test --noflush`, so an unparseable file is never written. - If writing the block or the reload fails, the play stops before `defaults.yml`: the host keeps its old policy. - If `defaults.yml` fails after the reload, the block is already loaded. It only drops the listed ports (and, with `ufw_docker_drop_unlisted: true`, other published ports on the default address), so there is no state where a deny default exists without the block. - One side effect: the flush also loads `before.rules` before the defaults, which it did not do before. I think that is harmless and it is in the changelog, but tell me if you would rather keep the reload where it is. **4. Testing.** Molecule cannot run Docker with iptables on the runner, so the PR will say plainly that real behaviour is not covered there: it asserts the block in `after.rules`, that `iptables-restore --test` accepts the whole file, idempotence, removal with the feature off, and rejected inputs. Before I open the PR I will also apply it once on a throwaway VM with real Docker and report the result in the PR. **5. Provenance.** Yes: I contribute this under GPL-3.0-or-later. I wrote it, it comes with no homelab names or inventory coupling (our version resolves allowed sources from inventory and that part is not included), and I did not copy any rules from `ufw-docker` or any other project; I only compared with it. I am marking this ready for your input on the mechanism. Assisted-by: Claude Code
Dragonish added
Status
Ready for Human
and removed
Status
Need More Info
2
labels 2026-11-15 14:14:03 +00:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#158