Skip to content

cniprovider: keep proxy dials in CNI netns - #6863

Merged
tonistiigi merged 2 commits into
moby:masterfrom
tonistiigi:exec-proxy-cni-dial-update
Jun 11, 2026
Merged

tonistiigi merged 2 commits into
moby:masterfrom
tonistiigi:exec-proxy-cni-dial-update

Conversation

@tonistiigi

Copy link
Copy Markdown
Member

depends on #6862

Dialing from a CNI namespace with the standard net.Dialer can escape the
namespace through happy-eyeballs connection attempts or resolver goroutines.

Use a serial dialer and route Go resolver sockets through the target
namespace, while preserving host loopback DNS stubs such as systemd-resolved
and Docker embedded DNS.

Comment thread util/network/cniprovider/cni_linux.go Outdated
Comment on lines +122 to +124
// loopback resolvers (systemd-resolved, Docker embedded
// DNS) are only reachable in the host namespace
return d.DialContext(ctx, networkName, address)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this bypass the target CNI namespace which could be wrong in RootlessKit detached-netns cases?

// withDetachedNetNSIfAny executes fn in $ROOTLESSKIT_STATE_DIR/netns if it exists.
// Otherwise it executes fn in the current netns.
//
// $ROOTLESSKIT_STATE_DIR/netns exists when we are running with RootlessKit >= 2.0 with --detach-netns.
// Since we are left in the host netns, we have to join the "detached" netns that is associated with slirp
// to create CNI namespaces inside it.
// https://github.com/rootless-containers/rootlesskit/pull/379
// https://github.com/containerd/nerdctl/pull/2723
func withDetachedNetNSIfAny(ctx context.Context, fn func(context.Context) error) error {
if stateDir := os.Getenv("ROOTLESSKIT_STATE_DIR"); stateDir != "" {
root, err := os.OpenRoot(stateDir)
if err != nil {
return err
}
defer root.Close()
if _, err := root.Lstat("netns"); err == nil {
detachedNetNS := filepath.Join(stateDir, "netns")
return netns.WithNetNSPath(detachedNetNS, func(_ netns.NetNS) error {
ctx := context.WithValue(ctx, contextKeyDetachedNetNS, detachedNetNS)
bklog.G(ctx).Debugf("Entering RootlessKit's detached netns %q", detachedNetNS)
err2 := fn(ctx)
bklog.G(ctx).WithError(err2).Debugf("Leaving RootlessKit's detached netns %q", detachedNetNS)
return err2
})
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
}
return fn(ctx)
}

cc @AkihiroSuda

@crazy-max crazy-max Jun 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a follow-up to make the loopback DNS path explicit.

Now it captures the namespace that called DialContext before entering the CNI namespace, then uses that namespace for loopback DNS dials. Non-loopback DNS and final connection dials still run inside the target CNI namespace.

Also added a regression test that invokes the resolver hook while already inside the target namespace, so the old ambient-namespace behavior would fail instead of passing by accident.

Let me know if that looks good @tonistiigi

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

tonistiigi and others added 2 commits June 11, 2026 11:27
Dialing from a CNI namespace with the standard net.Dialer can escape the
namespace through happy-eyeballs connection attempts or resolver goroutines.

Use a serial dialer and route Go resolver sockets through the target
namespace, while preserving host loopback DNS stubs such as systemd-resolved
and Docker embedded DNS.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@tonistiigi
tonistiigi force-pushed the exec-proxy-cni-dial-update branch from 78df309 to bac24d3 Compare June 11, 2026 18:27
@tonistiigi
tonistiigi merged commit 128c322 into moby:master Jun 11, 2026
192 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants