Skip to content

Agent Mesh

nano-init: refuse to run where the sandbox is not one

Antonio Ojea589e481be094
diff --git a/cmd/nano-init/isolation.go b/cmd/nano-init/isolation.go
new file mode 100644
index 00000000..0ba365d9
--- /dev/null
+++ b/cmd/nano-init/isolation.go
@@ -0,0 +1,73 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/vishvananda/netlink"
+)
+
+// This binary builds the only route out of a sandbox. It does not build the
+// sandbox: `docker run --network none` and a microVM's own kernel each hand it
+// a network namespace with nowhere to go, and it fills in the way out.
+//
+// That assumption is worth checking rather than trusting, because when it does
+// not hold nothing complains. Started in a namespace that already has an
+// interface -- a Kubernetes pod, where every container shares one, or a plain
+// `docker run` where somebody forgot the flag -- it would add tun0 alongside
+// the existing device, add a second default route, and hand the agent a
+// sandbox that is not one. The agent would be confined to a network it can
+// route around, and the run would look like every successful run.
+//
+// So the precondition is enforced here, once, whatever created the namespace.
+
+// assertIsolated reports whether this network namespace is a sandbox.
+func assertIsolated() error {
+ links, err := netlink.LinkList()
+ if err != nil {
+ return fmt.Errorf("list interfaces: %w", err)
+ }
+ names := make([]string, 0, len(links))
+ for _, l := range links {
+ names = append(names, l.Attrs().Name)
+ }
+ return isolationError(names)
+}
+
+// isolationError names the interfaces that mean this is not a sandbox.
+//
+// Loopback is expected and carries no traffic off the namespace. tun0 is our
+// own, which matters because the device outlives the process that made it, so
+// a second run in the same namespace must read as "already set up" rather than
+// as "not isolated".
+func isolationError(links []string) error {
+ var foreign []string
+ for _, name := range links {
+ if name == "lo" || name == tunName {
+ continue
+ }
+ foreign = append(foreign, name)
+ }
+ if len(foreign) == 0 {
+ return nil
+ }
+ return fmt.Errorf(
+ "this network namespace has %s, so it is not a sandbox: the agent could route around the boundary. "+
+ "Give it a namespace of its own -- `docker run --network none`, or a microVM with no network device",
+ strings.Join(foreign, ", "),
+ )
+}
diff --git a/cmd/nano-init/isolation_test.go b/cmd/nano-init/isolation_test.go
new file mode 100644
index 00000000..86ca96c2
--- /dev/null
+++ b/cmd/nano-init/isolation_test.go
@@ -0,0 +1,86 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestIsolationError(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ links []string
+ wantErr bool
+ // names is what the message must mention, so an operator is told which
+ // interface is the problem rather than only that there is one.
+ names []string
+ }{
+ {
+ name: "a microVM with no network device",
+ links: []string{"lo"},
+ },
+ {
+ name: "a container run with --network none",
+ links: []string{"lo"},
+ },
+ {
+ name: "our own tun, from an earlier run in this namespace",
+ links: []string{"lo", tunName},
+ },
+ {
+ name: "a Kubernetes pod, where the namespace is shared",
+ links: []string{"lo", "eth0"},
+ wantErr: true,
+ names: []string{"eth0"},
+ },
+ {
+ name: "a docker run that forgot --network none",
+ links: []string{"lo", "eth0", tunName},
+ wantErr: true,
+ names: []string{"eth0"},
+ },
+ {
+ name: "several ways out are all reported",
+ links: []string{"lo", "eth0", "vlan7"},
+ wantErr: true,
+ names: []string{"eth0", "vlan7"},
+ },
+ {
+ // A namespace with nothing at all is not one we built, but it has
+ // no way out either, which is the only property being asserted.
+ name: "an empty namespace",
+ links: nil,
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ err := isolationError(tc.links)
+ if tc.wantErr && err == nil {
+ t.Fatalf("isolationError(%q) = nil, want an error: an agent here is not confined", tc.links)
+ }
+ if !tc.wantErr {
+ if err != nil {
+ t.Fatalf("isolationError(%q) = %v, want nil", tc.links, err)
+ }
+ return
+ }
+ for _, name := range tc.names {
+ if !strings.Contains(err.Error(), name) {
+ t.Errorf("error does not name %q, so it does not say what to fix: %v", name, err)
+ }
+ }
+ })
+ }
+}
diff --git a/cmd/nano-init/main.go b/cmd/nano-init/main.go
index f7cd037d..0cf4886c 100644
--- a/cmd/nano-init/main.go
+++ b/cmd/nano-init/main.go
@@ -115,6 +115,14 @@ func run(boundarySocket, cmdName string, cmdArgs []string) {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
+ // First, and before anything is built: if this namespace is not a sandbox
+ // then the boundary is beside the point, and saying so in that order is
+ // the difference between "you forgot --network none" and a puzzling
+ // complaint about a socket.
+ if err := assertIsolated(); err != nil {
+ log.Fatalf("refusing to start: %v", err)
+ }
+
if err := checkBoundary(boundarySocket); err != nil {
log.Fatalf("this sandbox has no way out: %v", err)
}