vz: add networkOpts.hostLoopback to block guest access to host loopback #5581
AI review: https://jandubois.github.io/lima/20261006-184956-pr-5581.html
Also commits should be squashed
Thanks for the review. All the Important items and the suggestion are addressed, and the commits are squashed into one, and rebased on current master.
On external drivers failing open: agreed. I'd rather handle that as a follow-up, for example with a driver feature flag, than grow this PR. I also changed Fixes #5574 to Refs #5574, so the issue stays open for user-v2, krunkit and QEMU.
Thanks, LGTM,
I would still like to see the tests added that are suggested by I1 in the updated review: https://jandubois.github.io/lima/20261007-102123-pr-5581.html
I would ignore the new suggestions, but take a look if you feel you want to take any of them.
Added the I1 tests (netstack NAT config and the krunkit refusal), plus the S1 doc wording. Squashed to one commit as well
Two jobs (BATS, QEMU test-misc) timed out on this run. All their tests passed, but every VM boot in the run was 2–5× slower than the previous run (e.g. the BATS snapshot test took 517 s vs ~104 s on master today). @jandubois Could you please help re-run the failed jobs? (Seems like I can't trigger re-run myself)
Can't we support it in user-v2 (via networks.yaml maybe), as they share the code with vz's default network?
Should we introduce struct NetworkOpts or something to groupify other potential network options?
This includes services that listen only on the host's `127.0.0.1`. To block this, for example for a guest that runs untrusted code, set `hostLoopback: false` (available since Lima v2.3) in `lima.yaml`:
Yes, we can. However, scope will increase a bit and I would propose it as a follow up PR, as also mentioned in our discussion on the issue thread(https://github.com/lima-vm/lima/issues/5574#issuecomment-6018078418).
Let me know if that's ok with you.
Sounds good.
Going to use networkOpts.hostLoopback unless you prefer different name. Will update the PR shortly.
Thanks for the suggestion. Will fold it in the next PR update.
@AkihiroSuda FYI: Done addressing your comments
Thanks
Added hack/bats/extras/vz-host-loopback.bats and wired it into the vz job;
The only red check in CI is the release job, which timed out (21 min vs its 20-min limit.
@AkihiroSuda Could you re-run it?
Also, the latest test push also dismissed your approval, a re-approval would be appreciated.
Branch needs to be rebased to resolve merge conflict
@jandubois Rebased on current master.
Also, a re-approval would be appreciated.
Thanks
Thanks, LGTM
AI review: https://jandubois.github.io/lima/20261015-112802-pr-5581.html
Only contains suggestions; none of which are needed to merge the PR.
AkihiroSuda
ranacseruet
Refs #5574
Adds
networkOpts.hostLoopbacktolima.yaml, in a newnetworkOptsgroup for network options. It defaults totrue, so nothing changes unless it is set tofalse. Whenfalse, the in-process gateway mapshost.lima.internal(and0.0.0.0) to0.0.0.1instead of127.0.0.1. The host has no route to0.0.0.1, so the guest's connections are refused at once instead of reaching the host's loopback.Scope. This is narrower than I proposed in the issue, which said vz and krunkit:
vmType: vzonly.qemu,krunkit,wsl2andhcsrejectnetworkOpts.hostLoopback: falsewith an error instead of ignoring it. krunkit's default host resolver sends DNS throughhost.lima.internal, so it needs separate work, and I could not test krunkit.user-v2network is rejected too, because it is a shared daemon and a per-instance setting cannot apply to it. Anetworks.yamlsetting for it can be a follow-up.disable_host_loopbackis not implemented here.env, orpropagateProxyEnv) becomes unreachable from the guest. Lima logs a warning and the docs say so.Tested on macOS 26.6 with a vz VM, with and without the option, and with the option plus
vzNAT: true. Host servers on127.0.0.1:18081and0.0.0.0:18082:| From the guest | default |
networkOpts.hostLoopback: false||---|---|---|
|
host.lima.internal:18081(host127.0.0.1only) | reachable | refused at once ||
host.lima.internal:18082(host) | reachable | refused at once || raw UDP from a root guest to
0.0.0.0(host127.0.0.1only) | (reachable viahost.lima.internalanyway) | blocked || Mac LAN IP
:18081| closed | closed || Mac LAN IP
:18082(host) | reachable | reachable || DNS, HTTPS | ok | ok |
|
limactl shell(SSH) | ok | ok |With
vzNAT: true, connections tohost.lima.internalare refused at once as well. The vzNAT gateway only reaches host services bound to*, like the LAN address.hack/bats/extras/vz-host-loopback.bats, added to the vz CI job, checks the firsthost.lima.internalrow and DNS, then switches the instance totrueand checks that the service is reachable. It also checks that auser-v2network is rejected. It fails if the gateway mapping change is reverted.go test ./...,go vetand golangci-lint pass.Assisted-by: Claude Code