Skip to content

Mac: KIRI_HOST_LOOPBACK=off keeps containers away from the Mac's loopback - #1008

Open
masihmoloodian wants to merge 1 commit into
libracoder:mainfrom
masihmoloodian:feat/no-host-loopback
Open

masihmoloodian wants to merge 1 commit into
libracoder:mainfrom
masihmoloodian:feat/no-host-loopback

Conversation

@masihmoloodian

Copy link
Copy Markdown
Contributor

What this changes and why

On a Mac, host.docker.internal reaches every service on the Mac's 127.0.0.1 and ::1 (#258), as on Docker Desktop and OrbStack. That's right for development. For untrusted workloads, such as CI jobs run by a Kiri under its own macOS user, there was no way to turn it off (#995):

$ python3 -m http.server 18999 --bind 127.0.0.1 &
$ docker run --rm alpine wget -qO- http://host.docker.internal:18999/ | head -1
<!DOCTYPE HTML>

The engine itself opens that connection on the Mac (ports.rs, host_bound), and pf can't stop it, because macOS skips lo0 while a VM runs.

This adds KIRI_HOST_LOOPBACK=off (or 0, false, no) to the daemon's environment:

  • Each container connection to the Mac's loopback is refused: host.docker.internal, host.kiri.internal, --add-host x:host-gateway, and the same from --network host. The names still resolve, which the issue allows. The connection is closed, as from a host where nothing listens on the port.
  • Kiri's own image mirror stays reachable. BuildKit pulls base images and pushes the image it built through it, at 198.19.248.254:<mirror port>, so builds would break without it. Reading images from it needs its secret token.
  • The daemon logs that it's off when the VM starts, and logs the first connection it refuses.
  • Unset, or any other value, nothing changes. A typo can't turn it off.

It's an environment variable, like KIRI_TLS_ROOTS, because the use in #995 is a kiri daemon under a LaunchDaemon, whose plist sets it. There's no Settings switch.

Linux hosts are unchanged. There host.docker.internal is the bridge's gateway and never reached 127.0.0.1. Windows (WSL) is unchanged too, since it has its own relay (hostrelay.rs).

Containers can still reach the Mac's own network addresses, for services listening on every address. The issue is about loopback, and those connections don't go through this relay.

Fixes #995

How you tested it

On this branch, with KIRI_HOST_LOOPBACK=off kiri daemon and the same server on the Mac's 127.0.0.1:

$ docker run --rm alpine sh -c 'wget -qO- -T 5 http://host.docker.internal:18999/; getent hosts host.docker.internal'
wget: error getting response: Invalid argument
198.19.248.254    host.docker.internal  host.docker.internal

The daemon log:

INFO  containers don't reach this Mac's loopback through host.docker.internal (KIRI_HOST_LOOPBACK=off)
INFO  refused a container's connection to this Mac's loopback: KIRI_HOST_LOOPBACK is off (logged once) port=18999

A docker build (FROM busybox:1.36, RUN …) with it off works. BuildKit pushed its image to Kiri through the mirror at 198.19.248.254:47391. Without the variable, host-internal, host-gateway and host-internal-hostnet pass as before.

Tests:

  • cargo test -p kiri: 1054 passed. New test: host_loopback_off_refuses_all_but_the_mirror. It covers the values that turn it off and the ones that don't, every port with it on, and only the mirror's port with it off.

  • tests/e2e.sh: 3 new checks, run when the suite runs with KIRI_HOST_LOOPBACK=off against a daemon started with it. They pass:

    • host-loopback-off
    • host-loopback-off-gateway
    • host-loopback-off-hostnet

    The full suite against a daemon started without it: 938 passed, 19 failed, 1 skipped.

    • 18 failures are checks that fail on main on this Mac too:
      • Ten are DNS checks, and this Mac's VPN answers every DNS query: default-bridge-no-names, dns-failed-start-gone, disconnect-dns-gone, network-recreate-same-subnet-old-name-gone, dns-user-net-three-silent-servfail-in-time and the five rename-dns-* checks.
      • The other eight are about bind mounts, the overlay's birth times, loop-mounted images, network disconnect and Rosetta's vDSO.
    • The 19th, guest-kernel-nfs-dmcrypt, is my older guest kernel.
  • cargo clippy: no warnings in the changed lines.

Tested on: macOS 27.2.1, Apple M4 Pro. The change only acts on a Mac. Linux and Windows hosts have no such relay to the host's loopback.

How you found it

  • I use Kiri and ran into this myself
  • I found it by reading the code or comparing with Docker
  • Someone else reported it (link the issue above)

AI tools used to write this change:

Checklist

  • I have at most 3 pull requests open on Kiri, this one included
  • cargo fmt, cargo clippy and the tests pass
  • It works on macOS, Windows and Linux, or the change only touches one of them

…back

On a Mac, host.docker.internal reaches every service on the Mac's
127.0.0.1 and ::1 (libracoder#258), as on Docker Desktop and OrbStack. For
untrusted workloads, such as CI jobs in a Kiri that runs as its own macOS
user, there was no way to turn that off (libracoder#995). The engine itself opens
the connection, and pf can't stop it, since macOS skips lo0 while a VM
runs.

- `KIRI_HOST_LOOPBACK=off` (or 0, false, no) in the daemon's environment
  refuses each container connection to the Mac's loopback:
  host.docker.internal, host.kiri.internal and --add-host host-gateway.
  The names still resolve, and the connection is closed, as from a host
  where nothing listens. Unset or anything else, nothing changes.
- Kiri's own image mirror stays reachable there: builds pull through it,
  and it checks its token.
- The daemon logs that it is off when the VM starts, and the first
  connection it refuses.

Fixes libracoder#995
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting to turn off host loopback access (host.docker.internal) for untrusted workloads such as CI

1 participant