Skip to content

Modern Docker Management, Designed for Everyone

fix: validate cgroup-derived container ID during self-detection under network_mode service (#3710)

Kyle Mendellc24013107eff
diff --git a/backend/pkg/dockerutil/mount_utils.go b/backend/pkg/dockerutil/mount_utils.go
index d13ecc583..96f558d16 100644
--- a/backend/pkg/dockerutil/mount_utils.go
+++ b/backend/pkg/dockerutil/mount_utils.go
@@ -2,7 +2,6 @@ package docker
import (
"context"
- "os"
"strings"
"github.com/moby/moby/api/types/container"
@@ -10,28 +9,23 @@ import (
"github.com/moby/moby/client"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"
- "go.getarcane.app/sys/cgroup"
)
// MountForCurrentContainerSubpath inspects the current container, finds the
// existing mount whose destination covers containerPath, and returns a Mount
// suitable for use in another container creation that exposes the same data
-// at target. Returns nil + no error if Arcane isn't running inside a
-// container or no suitable mount is found — callers can fall back to a
-// plain bind on containerPath in that case.
+// at target. Returns nil + no error when no suitable mount is found, and an
+// error when the current container can't be detected — callers can fall back
+// to a plain bind on containerPath in either case.
func MountForCurrentContainerSubpath(ctx context.Context, dockerCli *client.Client, containerPath, target string) (*mount.Mount, error) {
if dockerCli == nil {
return nil, nil
}
- inspectTarget, _, err := libarcane.CurrentContainerInspectTarget(cgroup.CurrentContainerID, os.Hostname)
+ inspect, err := libarcane.InspectCurrentArcaneContainer(ctx, dockerCli)
if err != nil {
return nil, err
}
- inspect, err := libarcane.ContainerInspectWithCompatibility(ctx, dockerCli, inspectTarget, client.ContainerInspectOptions{})
- if err != nil {
- return nil, err
- }
- return MountForSubpath(inspect.Container.Mounts, containerPath, target), nil
+ return MountForSubpath(inspect.Mounts, containerPath, target), nil
}
// MountForSubpath returns a Mount that exposes a subpath of one of the
diff --git a/backend/pkg/libarcane/internal_resource.go b/backend/pkg/libarcane/internal_resource.go
index 564250783..0f47a49a4 100644
--- a/backend/pkg/libarcane/internal_resource.go
+++ b/backend/pkg/libarcane/internal_resource.go
@@ -77,18 +77,17 @@ func InspectCurrentArcaneContainer(ctx context.Context, dockerClient *client.Cli
return nil, errors.New("docker client is not available")
}
- // A cgroup-derived container ID is definitively the current container, so
- // that inspect is trusted even without the Arcane label (custom images).
- // With network_mode: service:<sidecar> the hostname belongs to the sidecar
- // container, so a hostname-derived inspect is only trusted directly when it
- // carries the Arcane label (#3544); an unlabeled hostname hit is kept as a
- // last resort so the label lookup can't hijack detection either way.
+ // With network_mode: service:<sidecar> both the hostname and the cgroup
+ // mountinfo fallback resolve the netns-owning sidecar — the container's
+ // /etc/hostname, /etc/hosts and /etc/resolv.conf bind-mounts come from the
+ // sidecar's container directory — so no derived target is authoritative. A
+ // derived inspect is only trusted directly when it carries the Arcane label
+ // (#3544, #3693); an unlabeled hit is kept as a last resort so the label
+ // lookup can't hijack detection either way. Custom images without the label
+ // behind a sidecar stay undetectable and must carry the label themselves.
var unlabeled *container.InspectResponse
- if target, fromContainerID, err := CurrentContainerInspectTarget(cgroup.CurrentContainerID, os.Hostname); err == nil && target != "" {
+ if target, err := CurrentContainerInspectTarget(cgroup.CurrentContainerID, os.Hostname); err == nil && target != "" {
if inspect, inspectErr := ContainerInspectWithCompatibility(ctx, dockerClient, target, client.ContainerInspectOptions{}); inspectErr == nil {
- if fromContainerID {
- return &inspect.Container, nil
- }
if inspect.Container.Config != nil && strings.EqualFold(strings.TrimSpace(inspect.Container.Config.Labels[labels.LabelArcane]), "true") {
return &inspect.Container, nil
}
@@ -101,10 +100,10 @@ func InspectCurrentArcaneContainer(ctx context.Context, dockerClient *client.Cli
if err != nil {
return nil, errors.WrapIf(err, "inspect Arcane container")
}
- // An unlabeled hostname hit is only overridden by the labeled match when
- // that match shares the hostname container's network namespace — i.e. the
- // hostname really belonged to its sidecar (#3544). Otherwise the labeled
- // container is a different Arcane instance and the hostname hit is us.
+ // An unlabeled derived hit is only overridden by the labeled match when
+ // that match shares the derived container's network namespace — i.e. the
+ // derived target really was its sidecar (#3544, #3693). Otherwise the
+ // labeled container is a different Arcane instance and the hit is us.
if unlabeled == nil || sharesNetworkNamespaceInternal(&inspect.Container, unlabeled) {
return &inspect.Container, nil
}
@@ -133,13 +132,15 @@ func sharesNetworkNamespaceInternal(candidate, owner *container.InspectResponse)
}
// CurrentContainerInspectTarget resolves the identifier used to inspect Arcane's
-// current container. The second return reports whether the target came from the
-// cgroup container ID (authoritative) rather than the hostname fallback.
-func CurrentContainerInspectTarget(currentContainerID func() (string, error), hostname func() (string, error)) (string, bool, error) {
+// current container: the detected container ID when available, the hostname
+// otherwise. Neither source is authoritative — under network_mode:
+// container:<sidecar> both can resolve the netns-owning sidecar — so callers
+// must validate the inspected container (see InspectCurrentArcaneContainer).
+func CurrentContainerInspectTarget(currentContainerID func() (string, error), hostname func() (string, error)) (string, error) {
if currentContainerID != nil {
if containerID, err := currentContainerID(); err == nil {
if containerID = strings.TrimSpace(containerID); containerID != "" {
- return containerID, true, nil
+ return containerID, nil
}
}
}
@@ -150,8 +151,8 @@ func CurrentContainerInspectTarget(currentContainerID func() (string, error), ho
value, err := hostname()
if err != nil {
- return "", false, err
+ return "", err
}
- return strings.TrimSpace(value), false, nil
+ return strings.TrimSpace(value), nil
}
diff --git a/backend/pkg/libarcane/internal_resource_test.go b/backend/pkg/libarcane/internal_resource_test.go
index 9aaece853..1421a53a8 100644
--- a/backend/pkg/libarcane/internal_resource_test.go
+++ b/backend/pkg/libarcane/internal_resource_test.go
@@ -13,40 +13,37 @@ import (
func TestCurrentContainerInspectTarget(t *testing.T) {
t.Run("prefers detected container id over hostname", func(t *testing.T) {
- target, fromContainerID, err := CurrentContainerInspectTarget(
+ target, err := CurrentContainerInspectTarget(
func() (string, error) { return "0123456789ab", nil },
func() (string, error) { return "rpi4", nil },
)
require.NoError(t, err)
require.Equal(t, "0123456789ab", target)
- require.True(t, fromContainerID)
})
t.Run("falls back to hostname when container id unavailable", func(t *testing.T) {
- target, fromContainerID, err := CurrentContainerInspectTarget(
+ target, err := CurrentContainerInspectTarget(
func() (string, error) { return "", errors.New("no container id found") },
func() (string, error) { return "rpi4", nil },
)
require.NoError(t, err)
require.Equal(t, "rpi4", target)
- require.False(t, fromContainerID)
})
t.Run("trims whitespace from detected container id", func(t *testing.T) {
- target, fromContainerID, err := CurrentContainerInspectTarget(
+ target, err := CurrentContainerInspectTarget(
func() (string, error) { return " 0123456789ab ", nil },
func() (string, error) { return "rpi4", nil },
)
require.NoError(t, err)
require.Equal(t, "0123456789ab", target)
- require.True(t, fromContainerID)
})
t.Run("returns hostname error when fallback fails", func(t *testing.T) {
- target, fromContainerID, err := CurrentContainerInspectTarget(
+ target, err := CurrentContainerInspectTarget(
func() (string, error) { return "", errors.New("no container id found") },
func() (string, error) { return "", errors.New("hostname unavailable") },
)
@@ -54,14 +51,14 @@ func TestCurrentContainerInspectTarget(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "hostname unavailable")
require.Empty(t, target)
- require.False(t, fromContainerID)
})
}
func TestInspectCurrentArcaneContainer_FallsBackToLabelWhenHostnameIsSidecar(t *testing.T) {
- // With network_mode: service:<sidecar> the hostname inspect returns the
- // sidecar container (no Arcane label); detection must fall back to the
- // label lookup instead of trusting it (#3544).
+ // With network_mode: service:<sidecar> both the hostname and the cgroup
+ // mountinfo fallback resolve the sidecar container (no Arcane label);
+ // detection must fall back to the label lookup instead of trusting the
+ // derived inspect (#3544, #3693).
dockerClient := newTestDockerClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {