What a sandbox can reach on the network
This page describes what a sandbox can reach on the network and which code enforces each rule. It is for a contributor who changes the sandbox network, the host firewall rules, the name servers or the listen address of the daemon. The last sections say which rules we checked on which engine, and which rules are not proven.
Summary
Section titled “Summary”| Destination | Result | Mechanism | Source |
|---|---|---|---|
| The internet | Permitted, if the sandbox has networking on | The bridge network boxfork of the engine |
Docker.EnsureNetwork |
| A different sandbox | Blocked on Docker. Permitted on Podman | The bridge option com.docker.network.bridge.enable_icc=false |
Docker.EnsureNetwork |
| The metadata service of a cloud | Blocked, where the daemon can set the host firewall | The chain BOXFORK-METADATA in iptables |
hostfw.Block, metadataGuard |
| A name server | Permitted. The helper corrects the resolver file under gVisor | The mode dns of the helper |
Manager.setDNS, fixResolv |
| A service that listens on the host | Permitted. Not blocked | None | See “Limits” |
| Anything, from a sandbox with networking off | No network | NetworkMode none |
Manager.boot |
The daemon itself never opens a network connection to a sandbox.
It controls each sandbox through the engine. See helper.md.
Thus a sandbox needs no open port, and Docker.Create publishes no port of a container to the host.
flowchart LR S["Sandbox with networking on"] -->|"to a different sandbox"| I["Bridge option enable_icc=false"] S -->|"to a blocked address"| M["Chain BOXFORK-METADATA"] S -->|"to the internet"| O["The host forwards the packet"] S -->|"to a service on the host"| H["No rule of boxfork"] I --> IB["Blocked on Docker"] M --> MB["REJECT, but port 53 passes"] N["Sandbox with networking off"] --> X["NetworkMode none: no network"]Source files
Section titled “Source files”| File | Contents |
|---|---|
| internal/engine/docker.go | EnsureNetwork, networkIsolated, NetworkSubnets, Remote, the dropped capabilities in Create. |
| internal/sandbox/manager.go | NetworkName, Manager.CheckNetwork, the network part of Manager.boot, Manager.setDNS. |
| internal/hostfw/hostfw.go | Block, Unblock, Blocked, Blocked6, Chain. |
| cmd/boxfork/metadata.go | metadataGuard and addrFlag. |
| internal/guest/resolv.go | The mode dns of the helper: fixResolv, readResolv, extServers, usable. |
| cmd/boxfork/main.go | The flags --listen, --tls-cert, --tls-key, --dns, --block-metadata, and the functions tlsConfig, serveOn, isLoopback. |
The network that the daemon creates
Section titled “The network that the daemon creates”All sandboxes with networking on share one bridge network.
Its name is the constant NetworkName, boxfork.
Docker.EnsureNetwork does this work:
- It reads the network with
GET /networks/boxfork. - If the network is not there, it creates a network with the driver
bridge. - It sets the option
com.docker.network.bridge.enable_icctofalseand the labelboxfork.managed=true. - It reads the network again and reports if the option is
false.
The daemon calls EnsureNetwork in two places.
Manager.CheckNetwork calls it one time when the daemon starts, and serve keeps the result.
Manager.boot calls it before each sandbox with networking on, because something can remove the network while the daemon runs.
Manager.boot starts each container spec with Network: "none".
It changes the value to boxfork only when the record has Network true.
The field network of a create request is true by default.
The order in boot for a sandbox with networking on is important:
sequenceDiagram participant B as Manager.boot participant E as Engine participant G as metadataGuard participant H as Helper B->>E: EnsureNetwork("boxfork") B->>G: Config.NetworkReady, which is metadataGuard.ensure G-->>B: nil, or an error that refuses the sandbox B->>E: Create with Network boxfork, CopyIn, Start B->>H: version (Manager.probe) B->>H: dns, as user 0, if the runtime is runsc or --dns is setThe firewall step comes before Create.
Thus no code of the client runs on a network that has no rules.
The sandbox-to-sandbox block
Section titled “The sandbox-to-sandbox block”With enable_icc set to false, the bridge of Docker drops traffic between its containers.
The daemon does not trust that the engine keeps the option.
networkIsolated reads the option from the engine, and EnsureNetwork gives that result.
serve uses the result in two ways:
- If it is false, the daemon writes a warning. The warning tells the operator to create sandboxes with networking off where the block is important.
GET /v1/infogives the value asnetwork_isolated, andboxfork infoshows it.
The daemon starts and creates sandboxes in the two cases. The check is a report and not a gate.
A network with the name boxfork can exist before the daemon creates it.
The daemon then uses it as it is, and network_isolated shows its real option.
One more measure is in Docker.Create.
It drops the capability NET_RAW for each container.
With raw sockets, a container can send packets with a false address on the shared bridge.
The metadata-service block
Section titled “The metadata-service block”Why it exists
Section titled “Why it exists”A cloud gives each machine a metadata service at a fixed address. The service gives the start-up script of the machine to each caller on the machine. If the machine has a role or a service account, the service also gives credentials for it. The traffic of a sandbox goes out through the machine. With no rule, code in a sandbox can ask the service too.
The blocked addresses
Section titled “The blocked addresses”The lists are in internal/hostfw/hostfw.go.
| Variable | Address | What is there |
|---|---|---|
Blocked |
169.254.0.0/16 |
The link-local range: the metadata service of AWS, Google Cloud, Azure, DigitalOcean, Hetzner, Oracle and others |
Blocked |
168.63.129.16/32 |
The host agent of Azure |
Blocked |
100.100.100.200/32 |
The metadata service of Alibaba Cloud |
Blocked |
192.0.0.192/32 |
The older metadata address of Oracle Cloud |
Blocked6 |
fd00:ec2::254/128 |
The metadata service of AWS through IPv6 |
Blocked6 |
fd20:ce::254/128 |
The metadata service of Google Cloud through IPv6 |
Blocked6 |
fd00:42::42/128 |
The metadata service of Scaleway |
The rules
Section titled “The rules”hostfw.Block gets the address ranges of the sandbox network.
It uses iptables for IPv4 ranges and ip6tables for IPv6 ranges.
It gives each command the option -w 5, thus the command waits a maximum of 5 seconds for the lock.
For each tool, the function block does these steps:
- It lists the chain
DOCKER-USER. If that fails, it givesErrUnavailable. - It makes sure that the chain
FORWARDsends traffic toDOCKER-USER. If not, it givesErrUnavailable. - It writes the chain
BOXFORK-METADATAwith one call ofiptables-restore --noflush. - For each range, it puts the rule
-s <range> -j BOXFORK-METADATAfirst inDOCKER-USER. - It removes the rules of boxfork that are not necessary: see
tidy.
The chain has three rules for each blocked address:
-A BOXFORK-METADATA -d 169.254.0.0/16 -p udp --dport 53 -j RETURN-A BOXFORK-METADATA -d 169.254.0.0/16 -p tcp --dport 53 -j RETURN-A BOXFORK-METADATA -d 169.254.0.0/16 -j REJECTThese are the reasons for the details:
- Port 53 passes. On some clouds the name server of the machine is at the same address as the metadata service.
REJECTfirst,DROPsecond. WithREJECTthe sender gets an answer immediately. If the kernel cannot doREJECT,blockwrites the chain again withDROP.- One call of
iptables-restore. The command replaces the chain in one step, thus there is no moment with an empty chain. --noflush. The command changes only the chain of boxfork and keeps all other rules.- First in
DOCKER-USER. A rule before the rule of boxfork can accept the traffic first. DOCKER-USER. Docker does not change this chain and examines it before its own rules.
Block is safe to call again at any time.
The function ours identifies a rule of boxfork: it has exactly the four words -s <range> -j BOXFORK-METADATA.
A rule for a range is in place when only rules of boxfork come before it.
If a different rule comes before it, block inserts a new copy at the top.
tidy then removes two types of rules:
- A second copy of a rule for the same range.
- A rule for a range that the sandbox network does not have now.
tidy deletes by rule number, from the last rule up.
Thus the numbers of the rules before it stay correct.
The reason for the second type: a rule for an old range applies to the next network that gets that range.
hostfw.Unblock removes all that Block set.
It removes the rules in DOCKER-USER first and the chain second.
This order is necessary: traffic that a rule sends to an empty chain passes.
When the daemon sets the rules
Section titled “When the daemon sets the rules”The type metadataGuard in cmd/boxfork/metadata.go decides when to call hostfw.Block.
The flag --block-metadata gives its mode.
| Mode | Constant | When the rules cannot be set |
|---|---|---|
auto (default) |
metaAuto |
The daemon starts and writes a warning. Sandboxes can reach the service. |
require |
metaRequire |
The daemon does not start. Later, it refuses each sandbox with networking on. |
off |
metaOff |
The daemon sets nothing, and it removes the rules of an earlier run with hostfw.Unblock. |
The guard sets the rules at three times:
| Time | Function | Behavior |
|---|---|---|
When the daemon starts, after Manager.CheckNetwork |
metadataGuard.start |
It sets the rules for the ranges of the network. |
| Each 30 seconds | metadataGuard.watch, metadataGuard.again |
It sets the rules again. A different program can clear them, and the network can be a new one. |
| Before each sandbox with networking on | metadataGuard.ensure, through Config.NetworkReady |
In auto: it sets the rules only if the ranges changed. In require: it sets them each time. |
The guard does not try in two cases, and ensure and watch then do nothing:
- The engine is on a different machine (
Docker.Remoteis true). The firewall of that machine is not for this daemon to set. - The daemon does not run on Linux.
The states of the guard in the modes auto and require:
stateDiagram-v2 [*] --> Blocked: start, the rules are set [*] --> NotBlocked: start in auto, the rules cannot be set [*] --> [*]: start in require, the rules cannot be set, the daemon stops Blocked --> NotBlocked: a pass fails, the log shows an error NotBlocked --> Blocked: a pass is successful, the log shows a note Blocked --> Blocked: a pass is successfulmetadataGuard.Blocked reports the result of the last pass.
GET /v1/info gives it as metadata_blocked and reads it again for each request.
The guard holds a mutex during each pass, because tidy deletes rules by number.
The warning in auto mode gives the operator three solutions:
- Run the daemon as root on the machine on which Docker runs.
- Block the address for the sandbox network in the firewall of the machine.
- Give the machine no role.
Name servers
Section titled “Name servers”The problem under gVisor
Section titled “The problem under gVisor”An engine puts its own name server at 127.0.0.11 for a network that it made.
That is the constant EngineResolver.
The name server answers through packet rules in the network namespace of the container.
Under gVisor, a sandbox has its own network stack and never meets those rules.
Thus no name resolves in such a sandbox until the resolver file changes.
What the daemon does
Section titled “What the daemon does”Manager.boot calls Manager.setDNS after Manager.probe when two conditions are true:
- The sandbox has networking on.
- The runtime is
runsc, or the daemon has one or more--dnsaddresses.
setDNS runs the helper as guest dns <addresses> with the user 0, because root owns the resolver file.
It waits a maximum of 30 seconds for one line.
The addresses are those of Config.DNS. With no --dns flag, there are none.
What the helper does
Section titled “What the helper does”fixResolv in internal/guest/resolv.go works on /etc/resolv.conf.
flowchart TD A["Read /etc/resolv.conf"] --> B{"Addresses in the arguments?"} B -->|yes| S["Use the arguments"] B -->|no| C{"A line: nameserver 127.0.0.11?"} C -->|no| K["Answer KEPT, change nothing"] C -->|yes| U["Use the addresses of the line # ExtServers"] S --> F["usable: remove bad addresses, IPv4 first, a maximum of 3"] U --> F F --> G{"One or more addresses left?"} G -->|no| N["Answer NONE, change nothing"] G -->|yes| W["Write the file, answer OK and the addresses"]The engine writes the comment line # ExtServers: [...] in the file.
It lists the name servers to which the resolver of the engine sends its questions.
extServers reads the list and removes the form host(...) around an address.
usable drops an address in these cases:
- It is not an IP address.
- It has a zone, such as
fe80::1%eth0. - It is a loopback address, the unspecified address, a multicast address or
255.255.255.255. - It is in the list a second time.
The new file contains a comment, one nameserver line for each address, and the old lines search, options, domain and sortlist.
The helper protects the write, because code in a sandbox can replace the file:
readResolvaccepts only a regular file. It opens it withO_NOFOLLOWandO_NONBLOCKand reads a maximum ofmaxResolv, 64 KiB.- The write opens the file with
O_NOFOLLOW. Root never writes through a link. - The write changes the file in place. The file is a mount of its own, and a rename cannot replace it.
The answers
Section titled “The answers”| Answer | Constant | What the daemon does |
|---|---|---|
OK <addresses> |
DNSSet |
Nothing. |
KEPT |
DNSKept |
Nothing. The file did not name the resolver of the engine. |
NONE |
DNSNone |
It writes one warning for the life of the daemon. The warning names the flag --dns. |
ERR <reason> |
DNSErr |
It writes a warning for the sandbox. |
| No line, or a timeout | none | It writes a warning for the sandbox. |
In all cases the daemon hands the sandbox to the client. A sandbox with no usable name server can reach the network by address.
The flag --dns
Section titled “The flag --dns”--dns takes one IP address and can be given many times.
addrFlag.Set refuses a value that is not an IP address.
It also refuses a loopback address and the unspecified address, because in a sandbox those addresses are the sandbox itself.
When --dns is set, the helper writes these addresses for each sandbox with networking on, on any runtime.
It does not look for the resolver of the engine first.
The value is part of Manager.setupOf, thus a change of --dns replaces the waiting sandboxes of each pool.
A sandbox with networking off
Section titled “A sandbox with networking off”A create request with network set to false gives a container with NetworkMode none.
For such a sandbox, Manager.boot skips three steps:
- It does not call
EnsureNetwork. - It does not call
Config.NetworkReady, thus--block-metadata requiredoes not refuse it. - It does not call
Manager.setDNS.
All functions of the daemon work for this sandbox, because commands and file operations go through the engine.
The trial containers of tryRuntime and Manager.checkSnapshots also use none.
The listen address and HTTPS of the daemon
Section titled “The listen address and HTTPS of the daemon”This section is about the API of the daemon, not about the traffic of a sandbox. It is here because a sandbox can reach a daemon that listens on more than the loopback address.
| Flag | Default | Behavior |
|---|---|---|
--listen |
127.0.0.1:7070 |
The address of the API. |
--tls-cert, --tls-key |
empty | With the two files, the daemon serves HTTPS. With none, it serves plain HTTP. |
serve in cmd/boxfork/main.go applies these rules:
tlsConfigreads the certificate before the daemon starts anything. A wrong path stops the daemon. It does not start on plain HTTP.- One file with no second file is an error.
- The minimum TLS version is 1.2.
serverefuses an argument that is not a flag. The flag parser drops all flags after such a word, and--tls-certcan be one of them.- If the daemon serves plain HTTP on an address that is not a loopback address, it writes a warning.
isLoopbackacceptslocalhostand each loopback IP address.
Each request needs the API key, also a request from a sandbox.
Invariants
Section titled “Invariants”A change must keep these rules.
- The daemon does not connect to a sandbox through the network.
- A container gets the network
boxforkonly when its record hasNetworktrue. The default inbootisnone. Config.NetworkReadyruns afterEnsureNetworkand beforeCreate.- The report
network_isolatedcomes from a read of the engine, not from the create request. hostfw.Blockis safe to call again. It leaves one rule for each range, first inDOCKER-USER.hostfwchanges only its own chain and its own rules inDOCKER-USER.- The chain always lets port 53 pass for a blocked address.
- Only one pass of the guard runs at a time.
- In
requiremode, no sandbox goes on the network when the rules cannot be set. - The helper writes the resolver file only as a regular file and never through a link.
- A failure of
setDNSdoes not fail the create request. - A daemon with a TLS flag does not start on plain HTTP.
Failure cases
Section titled “Failure cases”| Failure | Result |
|---|---|
The engine drops the option enable_icc |
network_isolated is false, and the daemon writes a warning. Sandboxes can reach one another. |
EnsureNetwork fails when the daemon starts |
serve stops with the error. |
EnsureNetwork fails for a sandbox |
The create request fails with “setting up the sandbox network”. |
The daemon is not root, or iptables is not there |
ErrUnavailable. In auto: a warning. In require: the daemon does not start. |
DOCKER-USER is not there, or FORWARD does not send traffic to it |
ErrUnavailable, with the same results. |
| A different program clears the firewall | In auto: sandboxes can reach the service for a maximum of 30 seconds. In require: the next sandbox sets the rules again or is refused. |
| Something removes the network and the engine makes it again with a new range | ensure sets the rules for the new range before the sandbox starts. |
| The network has no address range | ErrUnavailable: “the sandbox network has no subnet”. |
The engine is at a tcp address on a different machine |
The guard does not try. metadata_blocked is false. |
| The engine names no usable name server under gVisor | The helper answers NONE. Names do not resolve until the operator sets --dns. |
| The resolver file is a link or a device | The helper answers ERR. The daemon writes a warning. |
How to change it
Section titled “How to change it”Add a blocked address
Section titled “Add a blocked address”- Add the prefix to
BlockedorBlocked6in internal/hostfw/hostfw.go, with a comment that names the service. - Change
TestWhatIsBlockedin internal/hostfw/hostfw_test.go. - Run
go test ./internal/hostfw/.... - Add the address to the table on this page.
You do not need to remove old rules. The next pass of the guard replaces the full chain.
Change the rules of the chain
Section titled “Change the rules of the chain”- Change the text that
blockgives toiptables-restore. - Keep the three properties: one call,
--noflush, and the port 53 rules before the block rule. - Change the stand-in
fwin hostfw_test.go if it does not know the new rule text. - Make sure that
oursstill identifies the rule inDOCKER-USER, or old rules stay after an upgrade. - Run the tests of
internal/hostfwand ofcmd/boxfork. - Run the daemon as root on a Linux machine with Docker, and read the chain with
iptables -S BOXFORK-METADATA.
Change the name server logic
Section titled “Change the name server logic”- Change
fixResolvorusablein internal/guest/resolv.go. - Keep the first word of each answer line, because the daemon of a later build reads it.
- If you add an answer word, handle it in
Manager.setDNS. - Change the tests in internal/guest/resolv_test.go and internal/sandbox/dns_test.go.
- Run
make build, because the helper is embedded in the daemon.
Add a new network rule for a sandbox
Section titled “Add a new network rule for a sandbox”- Decide where the rule lives: in the engine (
CreateSpec), in the host firewall (hostfw), or in the helper. - For the engine, add a field to
CreateSpecand set it inDocker.Createand inFake. - For the host firewall, add the rule to the chain of boxfork. Do not add a second chain without a removal path in
Unblock. - Report the result in
api.Infoif the rule can fail, asnetwork_isolatedandmetadata_blockeddo. - Add the rule to the table “Summary” on this page.
- Check the rule on a real engine, and write what you checked in the section “What we checked”.
Where the tests are
Section titled “Where the tests are”| File | What it tests |
|---|---|
| internal/hostfw/hostfw_test.go | Block and Unblock against fw, a stand-in for iptables in memory: the rule order, a rule put back in front, an old range, IPv6, a kernel with no REJECT, failures. |
| cmd/boxfork/metadata_test.go | TestMetadataGuard: the three modes, the watch, the step before a sandbox, a remote engine, one pass at a time. TestNameServerFlag. |
| cmd/boxfork/main_test.go | TestIsLoopback, TestTLSConfig, TestServeOnWithoutACertificate, TestServeRefusesAWordThatIsNotAFlag. |
| internal/engine/docker_test.go | TestANetworkThatDoesNotIsolateIsReported, TestNetworkSubnets, TestListInspectCommitAndNetwork, and the dropped capabilities in TestCreateSendsLimitsHardeningAndGPUs. |
| internal/guest/resolv_test.go | fixResolv, usable and readResolv on files that the test makes. |
| internal/sandbox/dns_test.go | When the daemon calls the mode dns, and what it does with each answer. |
| internal/sandbox/manager_test.go | TestCheckNetwork. |
| internal/sandbox/review5_test.go | TestTheHostIsAskedBeforeASandboxJoinsTheNetwork: the order of NetworkReady and Create. |
All these tests use stand-ins.
No unit test runs iptables, creates a network, or sends a packet.
What we checked
Section titled “What we checked”| Environment | What the records say |
|---|---|
| Docker with gVisor on Ubuntu 24.04, arm64, in a nested virtual machine | boxfork ran here. The proof script conn.sh checks one network item: a sandbox with networking off runs commands. |
| Podman | boxfork ran here. The README says that Podman does not support the sandbox-to-sandbox block and that the daemon cannot set the metadata rule there. |
The tree has no proof script for the sandbox-to-sandbox block, the metadata-service block or the name servers.
The roadmap lists the metadata-service block as not proven for Docker with nftables.
If you check one of these rules on a real engine, add a script to scripts/proof and a row to this table.
Not proven
Section titled “Not proven”- Nothing ran on a real AWS account or a real Google Cloud project. No sandbox asked a real metadata service.
- Nothing ran on x86_64.
- The metadata-service block is not proven for Docker with nftables.
- The
ip6tablespath ran only against the stand-in. - The fallback from
REJECTtoDROPran only against the stand-in. - An engine at a
tcpaddress on a different machine: the code path forDocker.Remoteran only in unit tests.
Limits
Section titled “Limits”- A sandbox can reach each service that listens on the host on more than
127.0.0.1. To a sandbox, the host is one hop away. - That includes the API of the daemon when it listens for other machines. The API key is the only protection there.
- The firewall rules apply only to traffic that the machine forwards. A service that the machine itself runs at a link-local address is not included.
- Port 53 stays open to each blocked address.
- In
automode, a cleared firewall stays open for a maximum of 30 seconds. - The guard keeps its rule first in
DOCKER-USER. A rule of the operator that must come before it moves behind it at the next pass. - On Podman, with a remote engine, and when the daemon runs as a container, the daemon cannot set the metadata rule.
- On Podman, sandboxes with networking on can reach one another.
- A sandbox has networking on or off. There is no list of permitted destinations and no limit on outgoing traffic.
- The name servers are the same for all sandboxes of one daemon.