From 2d6c4af15ee37f323a12da45955fd9087b145a77 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 9 Nov 2015 10:19:53 +0800 Subject: [PATCH] Prevent connecting to host and prevent disconnecting from host Container has private network namespace can not to connect to host and container with host network can not be disconnected from host. Signed-off-by: Lei Jitang (cherry picked from commit a2d8c93fc690c2244a3bed32a2bbeb8dae449f90) --- daemon/container_unix.go | 8 ++++++++ .../docker_cli_network_unix_test.go | 18 ++++++++++++++++++ runconfig/parse.go | 2 ++ 3 files changed, 28 insertions(+) diff --git a/daemon/container_unix.go b/daemon/container_unix.go index 35e3879d8..8491b6e34 100644 --- a/daemon/container_unix.go +++ b/daemon/container_unix.go @@ -723,6 +723,10 @@ func (container *Container) updateNetworkSettings(n libnetwork.Network) error { container.NetworkSettings = &network.Settings{Networks: make(map[string]*network.EndpointSettings)} } + if !container.hostConfig.NetworkMode.IsHost() && runconfig.NetworkMode(n.Type()).IsHost() { + return runconfig.ErrConflictHostNetwork + } + for s := range container.NetworkSettings.Networks { sn, err := container.daemon.FindNetwork(s) if err != nil { @@ -1201,6 +1205,10 @@ func (container *Container) DisconnectFromNetwork(n libnetwork.Network) error { return derr.ErrorCodeNotRunning.WithArgs(container.ID) } + if container.hostConfig.NetworkMode.IsHost() && runconfig.NetworkMode(n.Type()).IsHost() { + return runconfig.ErrConflictHostNetwork + } + if err := container.disconnectFromNetwork(n); err != nil { return err } diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index 2868c3a5f..b4a5d74a5 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -15,6 +15,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/versions/v1p20" "github.com/docker/docker/pkg/integration/checker" + "github.com/docker/docker/runconfig" "github.com/docker/libnetwork/driverapi" remoteapi "github.com/docker/libnetwork/drivers/remote/api" "github.com/docker/libnetwork/ipamapi" @@ -730,3 +731,20 @@ func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) { out, _ := dockerCmd(c, "network", "create", "one") dockerCmd(c, "run", "-d", "--net", strings.TrimSpace(out), "busybox", "top") } + +func (s *DockerNetworkSuite) TestDockerNetworkConnectToHostFromOtherNetwork(c *check.C) { + dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top") + c.Assert(waitRun("container1"), check.IsNil) + dockerCmd(c, "network", "disconnect", "bridge", "container1") + out, _, err := dockerCmdWithError("network", "connect", "host", "container1") + c.Assert(err, checker.NotNil, check.Commentf(out)) + c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error()) +} + +func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *check.C) { + dockerCmd(c, "run", "-d", "--name", "container1", "--net=host", "busybox", "top") + c.Assert(waitRun("container1"), check.IsNil) + out, _, err := dockerCmdWithError("network", "disconnect", "host", "container1") + c.Assert(err, checker.NotNil, check.Commentf("Should err out disconnect from host")) + c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error()) +} diff --git a/runconfig/parse.go b/runconfig/parse.go index 9577b7ff6..d0b73c75b 100644 --- a/runconfig/parse.go +++ b/runconfig/parse.go @@ -21,6 +21,8 @@ var ( ErrConflictUserDefinedNetworkAndLinks = fmt.Errorf("Conflicting options: --net= can't be used with links. This would result in undefined behavior") // ErrConflictSharedNetwork conflict between private and other networks ErrConflictSharedNetwork = fmt.Errorf("Container sharing network namespace with another container or host cannot be connected to any other network") + // ErrConflictHostNetwork conflict from being disconnected from host network or connected to host network. + ErrConflictHostNetwork = fmt.Errorf("Container cannot be disconnected from host network or connected to host network") // ErrConflictNoNetwork conflict between private and other networks ErrConflictNoNetwork = fmt.Errorf("Container cannot be connected to multiple networks with one of the networks in --none mode") // ErrConflictNetworkAndDNS conflict between --dns and the network mode