The registry said 127.0.0.1. Docker went.
Shipkeep is an open-source platform to deploy apps on your own servers. You give it an image reference, and an agent on the server asks the Docker daemon to pull it. The control plane refuses a registry whose host is, o
Shipkeep is an open-source platform to deploy apps on your own servers. You give it an image reference, and an agent on the server asks the Docker daemon to pull it.
The control plane refuses a registry whose host is, or resolves to, an address that is not public: no loopback, no private ranges, no 169.254.169.254. That check has tests, and they pass.
Then a security review asked a simple question: who actually makes the connection? Not the control plane. It checks a name, then sends the server a reference that still names that host. The Docker daemon on the server does the pull, with none of those checks.
So I built a lab to see what the daemon does when the registry is the attacker.
The lab
One real node: Docker Engine 29.10.0, containerd image store, Ubuntu 24.04. A small HTTPS registry with a valid certificate for its name, the kind anyone can get for a domain they own. Plain HTTP servers on 127.0.0.1 and on the node's private address, logging whatever reached them. Every pull went through POST /images/create, the call the agent makes.
Four things reached places the control plane would never allow.
- The name is resolved again, on the node. I pointed the node's answer for the registry name at a private address. The daemon pulled from it. A name that passed the control plane's check can answer differently a minute later: DNS rebinding.
-
Redirects are followed anywhere, in plain HTTP. A blob request answered
307 Location: http://127.0.0.1:8099/β¦. The daemon sent theGETthere. Same with the node's private address. -
The token service is called anywhere, in plain HTTP. A
401withBearer realm="http://127.0.0.1:8099/β¦". The daemon called it, with the repository scope in the query string. -
A name that resolves to loopback, on any port. Docker treats
127.0.0.0/8as an insecure registry by default. Version 29 does not fall back to HTTP, but it still opened the TCP connection and sent its TLS handshake to the port I chose.
This is not a Docker bug. The daemon is a client, and it trusts the registry you name. That is fine on a laptop. It is not fine when "the registry you name" can be typed by any user who can deploy an image.
These requests are blind: the response never reaches the user, and the pull fails on the digest or the token. But the path, the query and the port are the attacker's choice, and a closed port fails differently from an open one. That is enough to map the node's network, and to send a GET with side effects to an internal service: a cloud metadata endpoint, an admin page, a database's HTTP port. On a single-machine installation, the control plane itself is on that loopback.
An egress proxy, and why it is not enough
Docker reads HTTP_PROXY and HTTPS_PROXY for its registry traffic. Point them at a proxy you control, and every connection goes through something that can say no.
So each server now runs shipkeep-egress, a small mode of the agent binary, as its own systemd service and its own user, on 127.0.0.1:7480. It does very little:
-
CONNECTonly. Registry traffic stays HTTPS end to end, and the proxy never sees a path, a header or a token. A plain HTTP request, which is what cases 2 and 3 become, is refused. - Public addresses only. It resolves the name itself and refuses if any answer is not public, using the same list as the control plane. A test reads both lists, the Go one and the TypeScript one, and fails if they differ.
-
The check runs in the dialer. Not on the DNS answer, but in
net.Dialer.Control, on the address the socket is about to connect to. A second answer, a cache or a retry cannot change what is reached.
func guard(_, address string, _ syscall.RawConn) error {
parsed, err := netip.ParseAddrPort(address)
if err != nil || !Public(parsed.Addr()) {
return errNotPublic
}
return nil
}
- Bounded. 64 tunnels, 10 seconds to connect, one hour per tunnel. Each tunnel logs its host, its port and its outcome.
With the proxy in place, cases 1, 2 and 3 to a private address were refused. Cases 2 and 3 to 127.0.0.1 still reached the probe.
Go's proxy support never sends a request for localhost, 127.0.0.0/8 or ::1 to a proxy, whatever NO_PROXY says. The daemon is written in Go. A redirect to a loopback literal bypasses any proxy you configure.
A firewall rule for one process
Loopback had to be closed in the kernel, for the daemon only. Other services on the host must keep talking to each other, and containers have their own network namespace anyway.
nftables can match the cgroup of the process that owns a socket. So: reject new connections from the Docker daemon to loopback, except the proxy and the local DNS resolver.
table inet shipkeep_egress {
chain output {
type filter hook output priority filter; policy accept;
socket cgroupv2 level 1 "shipkeep.slice" ct state new jump docker
}
chain docker {
ip daddr 127.0.0.1 tcp dport 7480 return
meta l4proto { tcp, udp } ct original ip daddr 127.0.0.53 ct original proto-dst 53 return
ip daddr 127.0.0.0/8 reject
ip6 daddr ::1 reject
}
}
ct state new matters. docker-proxy, which serves published ports, runs in the daemon's cgroup too, and its replies to local clients must still get through.
That rule is not what I wrote first.
The first design broke in the lab
The first version matched system.slice/docker.service and was loaded by an ExecStartPre of the Docker unit. It worked, and it survived two restarts of Docker. Then the lab kept going.
nftables resolves a cgroup path to a numeric ID when the rule is loaded. The path must exist at that moment. The cgroup of docker.service is created at each start and is gone while Docker is stopped, at boot in particular. Restarting Docker gives a new ID, and a rule loaded earlier matches nothing.
nftables.service starts by flushing the whole ruleset. Ubuntu's /etc/nftables.conf begins with flush ruleset. A restart of that service removes the guard while Docker keeps running. It removes Docker's own NAT rules too, which is a separate surprise.
The fix is a slice. The Docker unit gets Slice=shipkeep.slice, a slice that holds the daemon and nothing else. It starts at boot, before nftables and Docker, and its cgroup exists whether Docker runs or not. Its ID does not change when Docker restarts.
The guard is then loaded in the same transaction as anything that could remove it. A drop-in for nftables.service replaces its start and its reload with one file that includes /etc/nftables.conf, then the guard. Its stop flushes, then loads the guard again. One nft transaction, so there is never a moment without the rule.
Two smaller ones:
-
The DNS exception failed when the node itself ran in a container. Docker's embedded resolver answers on
127.0.0.11:53, then DNATs to a random port. The rule saw the translated port. Matchingct original, the destination before translation, fixed it. - The proxy could not run, and the installer did not notice. The egress service could not execute the binary inside the agent's private directory. The daemon reports its proxy settings whether or not anything listens there, so every check looked green. The service now runs from a root-owned copy, and the installer waits until the proxy answers.
Settings drift
The proxy settings can change after the installation: someone edits daemon.json, which Docker applies before its environment, or adds a drop-in. So the installer refuses to continue when daemon.json sets a proxy, and the agent reads the daemon's settings (GET /info) before every pull. If they are not exactly the proxy's, it refuses with a cause of its own, egress_unguarded, and the deployment says why.
I checked that too: with NO_PROXY=* added later, the next deployment stopped before any download.
What it costs
- A registry that redirects to plain HTTP stops working. Docker Hub, GHCR, ECR and GCR redirect to HTTPS, so they keep working.
- A registry on a private network is unsupported. It already was. When it is supported, its exception will be an explicit server setting.
- Every pull of that daemon goes through the proxy, including
docker compose pullfor the control plane on a single machine. The guide says so. - Servers installed before this change need to run the installer again, and Docker restarts with it.
Results
On the lab node, after the change:
- A pull from Docker Hub went through the proxy, including the redirect to its CDN.
- The four attack registries were refused, and the probes received nothing.
- A deployment from the control plane succeeded.
- Loopback stayed refused for the daemon after Docker restarts, after every start, reload, restart and stop of nftables, and after reboots.
The design, the alternatives and the lab are in ADR 0023. The proxy is under 400 lines of Go, comments included, with nothing outside the standard library.
Where it stands
- Released in
v0.1.0-alpha.5. It has run in test containers with systemd, not on a real server yet. - If you installed an earlier release, run the server installer again on each server.
Where you can help
- You know nftables or systemd better than I do: read the guard and its drop-ins and tell me what I missed.
- You have a disposable Ubuntu 24.04 VM and a domain name: follow the installation guide and tell me where it breaks.
- First contribution: the good first issues each say what to change and when it is done.
And a question for the comments: what does the Docker daemon on your servers reach when a registry tells it to go somewhere else?
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.