From 4ce817796e639391f6bc3e338f5a88985daacaca Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Sat, 6 Jun 2015 18:41:42 +0200 Subject: [PATCH] Avoid nil pointer dereference while creating a container with an empty Config Signed-off-by: Antonio Murdaca --- api/server/server.go | 4 ---- builder/job.go | 4 ++++ daemon/create.go | 4 ++++ integration-cli/docker_api_containers_test.go | 13 +++++++++++++ runconfig/config.go | 4 ++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/api/server/server.go b/api/server/server.go index 90378ba4d9..3f5cc27c0b 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -658,10 +658,6 @@ func (s *Server) postCommit(version version.Version, w http.ResponseWriter, r *h return err } - if c == nil { - c = &runconfig.Config{} - } - containerCommitConfig := &daemon.ContainerCommitConfig{ Pause: pause, Repo: r.Form.Get("repo"), diff --git a/builder/job.go b/builder/job.go index c081dbe9f8..b24d8e16df 100644 --- a/builder/job.go +++ b/builder/job.go @@ -221,6 +221,10 @@ func Commit(d *daemon.Daemon, name string, c *daemon.ContainerCommitConfig) (str return "", err } + if c.Config == nil { + c.Config = &runconfig.Config{} + } + newConfig, err := BuildFromConfig(d, c.Config, c.Changes) if err != nil { return "", err diff --git a/daemon/create.go b/daemon/create.go index 16b6b48b92..031f337a94 100644 --- a/daemon/create.go +++ b/daemon/create.go @@ -15,6 +15,10 @@ import ( ) func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) { + if config == nil { + return "", nil, fmt.Errorf("Config cannot be empty in order to create a container") + } + warnings, err := daemon.verifyHostConfig(hostConfig) if err != nil { return "", warnings, err diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 025206514a..ae69dfd91a 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -836,6 +836,19 @@ func (s *DockerSuite) TestContainerApiCreate(c *check.C) { } } +func (s *DockerSuite) TestContainerApiCreateEmptyConfig(c *check.C) { + config := map[string]interface{}{} + + status, b, err := sockRequest("POST", "/containers/create", config) + c.Assert(err, check.IsNil) + c.Assert(status, check.Equals, http.StatusInternalServerError) + + expected := "Config cannot be empty in order to create a container\n" + if body := string(b); body != expected { + c.Fatalf("Expected to get %q, got %q", expected, body) + } +} + func (s *DockerSuite) TestContainerApiCreateWithHostName(c *check.C) { hostName := "test-host" config := map[string]interface{}{ diff --git a/runconfig/config.go b/runconfig/config.go index e698f3078c..74e8bd62a0 100644 --- a/runconfig/config.go +++ b/runconfig/config.go @@ -143,6 +143,10 @@ func (c ContainerConfigWrapper) HostConfig() *HostConfig { return c.hostConfigWrapper.GetHostConfig() } +// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper +// struct and returns both a Config and an HostConfig struct +// Be aware this function is not checking whether the resulted structs are nil, +// it's your business to do so func DecodeContainerConfig(src io.Reader) (*Config, *HostConfig, error) { decoder := json.NewDecoder(src)