Skip to content

Agent Mesh

ingress: refuse to serve rather than dial the gateway's own namespace

Antonio Ojeaa845f055c83d
diff --git a/cmd/sam-box/main.go b/cmd/sam-box/main.go
index e53aa6d7..3d90c222 100644
--- a/cmd/sam-box/main.go
+++ b/cmd/sam-box/main.go
@@ -234,11 +234,14 @@ func resolveIngress(bundlePath, sidecarSocket, agentIngressSocket string) (*samb
return nil, nil
}
if agentIngressSocket == "" {
- // Not fatal: an agent sharing this process's network namespace is
- // reachable without one. That is no sandbox, so it is worth saying.
- logger.Warnf("Agent %s may serve the mesh, but no --agent-ingress-socket was given: "+
- "delivery will dial the agent directly, which only reaches an agent sharing this "+
- "network namespace and therefore not a sandboxed one", bundle.Agent.ID)
+ // Refused rather than degraded. Without a channel into the sandbox the
+ // only address left is one in this process's network namespace, which
+ // is the pod's: the node's API and every sidecar are on that loopback,
+ // and the port would be the agent's to choose.
+ return nil, fmt.Errorf("agent %s may serve %d mesh service(s), but --agent-ingress-socket is not set. "+
+ "Point it at the path nano-init --ingress-socket serves; without it there is no way into the "+
+ "sandbox, and delivering to this process's own network namespace would reach the gateway's "+
+ "neighbours instead of the agent", bundle.Agent.ID, len(bundle.Ingress))
}
logger.Infof("Agent %s may serve %d mesh service(s) once it announces them", bundle.Agent.ID, len(bundle.Ingress))
return &sambox.IngressManager{
diff --git a/internal/sambox/ingress.go b/internal/sambox/ingress.go
index 70e3611a..045b2ee3 100644
--- a/internal/sambox/ingress.go
+++ b/internal/sambox/ingress.go
@@ -127,6 +127,9 @@ func (m *IngressManager) Announce(ctx context.Context, decl ingressDeclaration)
if !m.permits(decl) {
return fmt.Errorf("this agent was not granted %s://%s", decl.Type, decl.Name)
}
+ if err := m.reachable(); err != nil {
+ return err
+ }
addr, err := m.ensureListening()
if err != nil {
@@ -212,6 +215,9 @@ func (m *IngressManager) forwarder() http.Handler {
return proxy
}
+// agentAddr names where the agent is, for a transport that knows how to get
+// there. The port is the agent's own choice, so this must never become an
+// address in this process's network namespace: see reachable.
func (m *IngressManager) agentAddr(port int) string {
if m.AgentAddr != nil {
return m.AgentAddr(port)
@@ -219,6 +225,27 @@ func (m *IngressManager) agentAddr(port int) string {
return net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
}
+// reachable reports whether this manager can deliver into the sandbox at all.
+//
+// There used to be a fallback here: with no reverse channel, dial
+// 127.0.0.1:<port> and hope the agent shares this network namespace. That is a
+// hole rather than a degraded mode. The port is chosen by the agent, and this
+// process's loopback is the pod's -- where sam-node's API, other sidecars and
+// every other boundary are listening. An agent could therefore announce a
+// service whose backend is the node that vouches for it, and the mesh would
+// route to it.
+//
+// So an agent that may serve needs a channel into its sandbox, and without one
+// nothing is registered.
+func (m *IngressManager) reachable() error {
+ if m.AgentSocket != "" || m.AgentAddr != nil {
+ return nil
+ }
+ return fmt.Errorf("no way into the sandbox: set --agent-ingress-socket to the path " +
+ "nano-init --ingress-socket serves, because delivering to an address in this " +
+ "process's network namespace would reach the gateway's neighbours rather than the agent")
+}
+
// AgentTransport reaches the sandbox over its reverse channel when there is
// one, and returns nil when the agent can be dialled directly.
//
diff --git a/internal/sambox/ingress_test.go b/internal/sambox/ingress_test.go
index f9f15660..9103d21b 100644
--- a/internal/sambox/ingress_test.go
+++ b/internal/sambox/ingress_test.go
@@ -17,9 +17,13 @@ package sambox
import (
"context"
"encoding/json"
+ "fmt"
"io"
+ "net"
"net/http"
"net/http/httptest"
+ "path/filepath"
+ "strconv"
"strings"
"testing"
@@ -73,6 +77,9 @@ func TestIngressRegistersWhatTheAgentAnnounces(t *testing.T) {
manager := &IngressManager{
SidecarSocket: socket,
Allowed: []BundleIngress{{Name: "code-reviewer", Type: "mcp"}},
+ // Registration never dials it, but a manager with no way into the
+ // sandbox now refuses to register at all.
+ AgentSocket: filepath.Join(t.TempDir(), "ingress.sock"),
}
t.Cleanup(func() { manager.Close(context.Background()) })
@@ -104,6 +111,9 @@ func TestIngressRefusesNamesTheAgentWasNotGranted(t *testing.T) {
manager := &IngressManager{
SidecarSocket: socket,
Allowed: []BundleIngress{{Name: "code-reviewer", Type: "mcp"}},
+ // Registration never dials it, but a manager with no way into the
+ // sandbox now refuses to register at all.
+ AgentSocket: filepath.Join(t.TempDir(), "ingress.sock"),
}
t.Cleanup(func() { manager.Close(context.Background()) })
@@ -135,6 +145,57 @@ func TestIngressRefusesNamesTheAgentWasNotGranted(t *testing.T) {
}
}
+// TestIngressWithNoWayInRefusesRatherThanDiallingItsOwnNamespace is a
+// vulnerability regression.
+//
+// There used to be a fallback: with no reverse channel, deliver to
+// 127.0.0.1:<port>. That address is in the gateway's network namespace, which
+// in a pod is the pod's -- sam-node's API, the other sidecars, every other
+// boundary. The port comes from the agent. So an agent could announce a
+// service whose backend was the node that vouches for it, and the mesh would
+// route to it.
+//
+// Nothing is registered without a way into the sandbox, so there is no address
+// for the mesh to reach and nothing for the agent to aim.
+func TestIngressWithNoWayInRefusesRatherThanDiallingItsOwnNamespace(t *testing.T) {
+ // Stands in for whatever else shares the gateway's namespace: the node's
+ // API, a metrics endpoint, another agent's boundary.
+ neighbour := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ t.Error("the gateway dialled a neighbour in its own network namespace")
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer neighbour.Close()
+
+ _, portStr, err := net.SplitHostPort(strings.TrimPrefix(neighbour.URL, "http://"))
+ if err != nil {
+ t.Fatalf("SplitHostPort: %v", err)
+ }
+ port, err := strconv.Atoi(portStr)
+ if err != nil {
+ t.Fatalf("Atoi: %v", err)
+ }
+
+ registrations := make(chan *api.RegisterServiceRequest, 1)
+ manager := &IngressManager{
+ SidecarSocket: registrationRecorder(t, registrations),
+ // Granted the name, so only the missing channel can refuse it.
+ Allowed: []BundleIngress{{Name: "code-reviewer", Type: "mcp"}},
+ }
+ t.Cleanup(func() { manager.Close(context.Background()) })
+
+ body := fmt.Sprintf(`{"name":"code-reviewer","type":"mcp","port":%d}`, port)
+ rec := announce(t, manager.Handler(), body)
+ if rec.Code == http.StatusNoContent {
+ t.Fatalf("the agent announced a service aimed at the gateway's own namespace and was accepted")
+ }
+
+ select {
+ case got := <-registrations:
+ t.Fatalf("registered %q despite having no way into the sandbox", got.GetService().GetName())
+ default:
+ }
+}
+
func TestIngressRefusesEverythingWhenNothingWasGranted(t *testing.T) {
registrations := make(chan *api.RegisterServiceRequest, 1)
manager := &IngressManager{SidecarSocket: registrationRecorder(t, registrations)}
@@ -242,6 +303,9 @@ func TestIngressWithdrawsOnClose(t *testing.T) {
manager := &IngressManager{
SidecarSocket: socket,
Allowed: []BundleIngress{{Name: "code-reviewer", Type: "mcp"}},
+ // Registration never dials it, but a manager with no way into the
+ // sandbox now refuses to register at all.
+ AgentSocket: filepath.Join(t.TempDir(), "ingress.sock"),
}
if rec := announce(t, manager.Handler(), `{"name":"code-reviewer","type":"mcp","port":8080}`); rec.Code != http.StatusNoContent {
t.Fatalf("announce: %d", rec.Code)
diff --git a/site/content/docs/user/running-agents.md b/site/content/docs/user/running-agents.md
index 48bf7335..0953cc27 100644
--- a/site/content/docs/user/running-agents.md
+++ b/site/content/docs/user/running-agents.md
@@ -433,9 +433,12 @@ args:
- --bundle=/etc/sam/bundle.yaml
```
-Without `--agent-ingress-socket`, `sam-box` falls back to dialling the agent
-directly and warns that this only reaches an agent sharing its network
-namespace — which is to say, one that is not sandboxed.
+`--agent-ingress-socket` is required whenever the bundle grants ingress, and
+`sam-box` refuses to start without it. There is no fallback on purpose: the only
+other address available is one in the gateway's own network namespace, which in
+a pod is the pod's — where `sam-node`'s API and every sidecar are listening —
+and the port would be the agent's to choose. An agent could otherwise announce
+a service whose backend was the node that vouches for it.
What the agent may serve is still the bundle's decision, not the agent's. The
agent chooses the port, because that is the part only it knows.
diff --git a/site/content/docs/user/secure-gateway.md b/site/content/docs/user/secure-gateway.md
index 52a3ced5..5bc5c18a 100644
--- a/site/content/docs/user/secure-gateway.md
+++ b/site/content/docs/user/secure-gateway.md
@@ -142,9 +142,9 @@ Reaching back into the sandbox cannot mean dialling the agent: a sandbox has a
network namespace of its own, so the gateway's `127.0.0.1` is its own loopback
and not the agent's. Delivery therefore goes over a second Unix socket, served
from inside the sandbox by `nano-init --ingress-socket` and dialled by
-`sam-box --agent-ingress-socket`. Without that flag the gateway falls back to
-dialling the agent directly and warns, because that only reaches an agent
-sharing its network namespace — one that is not sandboxed.
+`sam-box --agent-ingress-socket`. That flag is required whenever a bundle grants
+ingress: the gateway refuses to start without it, because the only other address
+available is one in its own network namespace, on a port the agent chooses.
---