From 95ce309b3aea3beec89eb76610a22c4d587b596e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 27 Nov 2026 20:06:45 +1100 Subject: [PATCH] fix(connectors): leave a package with no declared egress on the host's network Confining it was pointless: the relay the namespace exists for is only started when egress is non-empty, so the package got loopback and a proxy address nothing served. Any client honouring the proxy variables then reported a dial refused at the relay rather than reaching the host it was configured for. Fixes #2152. --- .../connectors/internal/host/confine_linux.go | 11 ++++ .../connectors/internal/host/confine_other.go | 5 ++ modules/connectors/internal/host/host.go | 5 +- modules/connectors/internal/host/host_test.go | 54 +++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 modules/connectors/internal/host/host_test.go diff --git a/modules/connectors/internal/host/confine_linux.go b/modules/connectors/internal/host/confine_linux.go index 75700b550..173c38c6f 100644 --- a/modules/connectors/internal/host/confine_linux.go +++ b/modules/connectors/internal/host/confine_linux.go @@ -58,6 +58,17 @@ func confinedCommand(ctx context.Context, binary *os.File, args []string, socket return cmd, nil } +// openCommand runs the verified binary by its open descriptor with the host's +// network, for a package that declares no egress hosts: a connector that +// reaches hosts an operator configured, not hosts a manifest lists. +func openCommand(ctx context.Context, binary *os.File, args []string) *exec.Cmd { + path := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), binary.Fd()) + //nolint:gosec // the binary is this one, named by descriptor; the arguments are the accepted package's own. + cmd := exec.CommandContext(ctx, path, args...) + cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} + return cmd +} + // launch runs inside the namespace: loopback up, relay listening, then the // child from descriptor 3 with every capability gone. func launch(socket string, args []string) int { diff --git a/modules/connectors/internal/host/confine_other.go b/modules/connectors/internal/host/confine_other.go index a1f7eeee0..821c38351 100644 --- a/modules/connectors/internal/host/confine_other.go +++ b/modules/connectors/internal/host/confine_other.go @@ -14,3 +14,8 @@ import ( func confinedCommand(context.Context, *os.File, []string, string) (*exec.Cmd, error) { return nil, errors.New("extensions need Linux network namespaces to confine their egress") } + +func openCommand(ctx context.Context, binary *os.File, args []string) *exec.Cmd { + //nolint:gosec // the binary is the host-verified one, named by its path. + return exec.CommandContext(ctx, binary.Name(), args...) +} diff --git a/modules/connectors/internal/host/host.go b/modules/connectors/internal/host/host.go index 68ddb9a80..ec302848d 100644 --- a/modules/connectors/internal/host/host.go +++ b/modules/connectors/internal/host/host.go @@ -72,8 +72,9 @@ func (p *proc) kill() { func command(ctx context.Context, binary *os.File, egress []string) (*exec.Cmd, *gate, error) { if len(egress) == 0 { - cmd, err := confinedCommand(ctx, binary, nil, "") - return cmd, nil, err + // Nothing to allow, so there is no relay to reach through: confining + // this would give it loopback and a proxy address that nothing serves. + return openCommand(ctx, binary, nil), nil, nil } g, err := openGate(ctx, egress) if err != nil { diff --git a/modules/connectors/internal/host/host_test.go b/modules/connectors/internal/host/host_test.go new file mode 100644 index 000000000..726599e57 --- /dev/null +++ b/modules/connectors/internal/host/host_test.go @@ -0,0 +1,54 @@ +//go:build linux + +package host + +import ( + "context" + "os" + "syscall" + "testing" +) + +// TestCommandConfinesOnlyWithEgress guards the regression where a package that +// declares no egress was still confined to a namespace holding loopback and +// handed a proxy address nothing served, so it reached nothing: the host +// connector could not open its SSH connection and reported a refused dial to +// the relay instead. +func TestCommandConfinesOnlyWithEgress(t *testing.T) { + binary, err := os.Open("/proc/self/exe") + if err != nil { + t.Fatalf("open the binary: %v", err) + } + t.Cleanup(func() { _ = binary.Close() }) + ctx := context.Background() + + t.Run("no egress runs on the host's network with no relay", func(t *testing.T) { + cmd, gate, err := command(ctx, binary, nil) + if err != nil { + t.Fatalf("command: %v", err) + } + if gate != nil { + t.Error("a package with no egress has nothing to relay") + } + if cmd.SysProcAttr != nil && cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWNET != 0 { + t.Error("a package with no egress was confined to its own network namespace") + } + if cmd.Env != nil { + t.Error("a package with no egress was handed a proxy environment") + } + }) + + t.Run("egress is confined behind a relay", func(t *testing.T) { + cmd, gate, err := command(ctx, binary, []string{"example.com:443"}) + if err != nil { + t.Fatalf("command: %v", err) + } + if gate == nil { + t.Fatal("a package with egress needs a gate to reach it through") + } + t.Cleanup(gate.close) + if cmd.SysProcAttr == nil || cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWNET == 0 { + t.Error("a package with egress was not confined to its own network namespace") + } + }) +}