Skip to content

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.

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"]
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.

All sandboxes with networking on share one bridge network. Its name is the constant NetworkName, boxfork.

Docker.EnsureNetwork does this work:

  1. It reads the network with GET /networks/boxfork.
  2. If the network is not there, it creates a network with the driver bridge.
  3. It sets the option com.docker.network.bridge.enable_icc to false and the label boxfork.managed=true.
  4. 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 set

The firewall step comes before Create. Thus no code of the client runs on a network that has no rules.

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/info gives the value as network_isolated, and boxfork info shows 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.

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 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

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:

  1. It lists the chain DOCKER-USER. If that fails, it gives ErrUnavailable.
  2. It makes sure that the chain FORWARD sends traffic to DOCKER-USER. If not, it gives ErrUnavailable.
  3. It writes the chain BOXFORK-METADATA with one call of iptables-restore --noflush.
  4. For each range, it puts the rule -s <range> -j BOXFORK-METADATA first in DOCKER-USER.
  5. 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 REJECT

These 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.
  • REJECT first, DROP second. With REJECT the sender gets an answer immediately. If the kernel cannot do REJECT, block writes the chain again with DROP.
  • 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.

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.Remote is 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 successful

metadataGuard.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.

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.

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 --dns addresses.

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.

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:

  • readResolv accepts only a regular file. It opens it with O_NOFOLLOW and O_NONBLOCK and reads a maximum of maxResolv, 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.
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.

--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 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 require does 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:

  • tlsConfig reads 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.
  • serve refuses an argument that is not a flag. The flag parser drops all flags after such a word, and --tls-cert can be one of them.
  • If the daemon serves plain HTTP on an address that is not a loopback address, it writes a warning.
  • isLoopback accepts localhost and each loopback IP address.

Each request needs the API key, also a request from a sandbox.

A change must keep these rules.

  • The daemon does not connect to a sandbox through the network.
  • A container gets the network boxfork only when its record has Network true. The default in boot is none.
  • Config.NetworkReady runs after EnsureNetwork and before Create.
  • The report network_isolated comes from a read of the engine, not from the create request.
  • hostfw.Block is safe to call again. It leaves one rule for each range, first in DOCKER-USER.
  • hostfw changes only its own chain and its own rules in DOCKER-USER.
  • The chain always lets port 53 pass for a blocked address.
  • Only one pass of the guard runs at a time.
  • In require mode, 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 setDNS does not fail the create request.
  • A daemon with a TLS flag does not start on plain HTTP.
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.
  1. Add the prefix to Blocked or Blocked6 in internal/hostfw/hostfw.go, with a comment that names the service.
  2. Change TestWhatIsBlocked in internal/hostfw/hostfw_test.go.
  3. Run go test ./internal/hostfw/....
  4. 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.

  1. Change the text that block gives to iptables-restore.
  2. Keep the three properties: one call, --noflush, and the port 53 rules before the block rule.
  3. Change the stand-in fw in hostfw_test.go if it does not know the new rule text.
  4. Make sure that ours still identifies the rule in DOCKER-USER, or old rules stay after an upgrade.
  5. Run the tests of internal/hostfw and of cmd/boxfork.
  6. Run the daemon as root on a Linux machine with Docker, and read the chain with iptables -S BOXFORK-METADATA.
  1. Change fixResolv or usable in internal/guest/resolv.go.
  2. Keep the first word of each answer line, because the daemon of a later build reads it.
  3. If you add an answer word, handle it in Manager.setDNS.
  4. Change the tests in internal/guest/resolv_test.go and internal/sandbox/dns_test.go.
  5. Run make build, because the helper is embedded in the daemon.
  1. Decide where the rule lives: in the engine (CreateSpec), in the host firewall (hostfw), or in the helper.
  2. For the engine, add a field to CreateSpec and set it in Docker.Create and in Fake.
  3. For the host firewall, add the rule to the chain of boxfork. Do not add a second chain without a removal path in Unblock.
  4. Report the result in api.Info if the rule can fail, as network_isolated and metadata_blocked do.
  5. Add the rule to the table “Summary” on this page.
  6. Check the rule on a real engine, and write what you checked in the section “What we checked”.
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.

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.

  • 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 ip6tables path ran only against the stand-in.
  • The fallback from REJECT to DROP ran only against the stand-in.
  • An engine at a tcp address on a different machine: the code path for Docker.Remote ran only in unit tests.
  • 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 auto mode, 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.