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.
This commit is contained in:
4 files changed
+73
-2
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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...)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user