Skip to content

remote API container create nil pointer dereference if HostConfig is not specified #14915

@srust

Description

@srust

It appears that the commit for #13218 changed the behavior of the GetHostConfig() wrapper in runconfig/config.go and broke the remote API for container/create

The change was part of this:

diff --git a/runconfig/config.go b/runconfig/config.go
index 74e8bd6..8c578ee 100644
--- a/runconfig/config.go
+++ b/runconfig/config.go
[snip]

-func (c ContainerConfigWrapper) HostConfig() *HostConfig {
-       if c.hostConfigWrapper == nil {
-               return new(HostConfig)
+func (w *ContainerConfigWrapper) GetHostConfig() *HostConfig {
+       hc := w.HostConfig

[snip]

This function used to return a new(HostConfig) when nil was passed.

This change ended up breaking the remote API. If a client does not specify the HostConfig object in the API request for /container/create, the server linux code ends up dereferencing the HostConfig pointer it thought was non-nil.

api/server/server.go

      config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
      if err != nil {
          return err
      }
      adjustCpuShares(version, hostConfig)

api/server/server_linux.go
  func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
      if version.LessThan("1.19") {                                          
          if hostConfig.CpuShares > 0 {

This causes the following backtrace:

2015/07/23 15:09:29 http: panic serving @: runtime error: invalid memory address or nil pointer dereference
goroutine 158 [running]:
net/http.func·011()
/usr/local/go/src/net/http/server.go:1130 +0xbb
github.com/docker/docker/api/server.adjustCpuShares(0xc20abea097, 0x4, 0x0)
/go/src/github.com/docker/docker/api/server/server_linux.go:112 +0x61
github.com/docker/docker/api/server.(_Server).postContainersCreate(0xc2080bb580, 0xc20abea097, 0x4, 0x7f276d88e7d8, 0xc2089f5680, 0xc2085f4ea0, 0xc20abea270, 0x0, 0x0)
/go/src/github.com/docker/docker/api/server/server.go:926 +0x21a
github.com/docker/docker/api/server._Server.(github.com/docker/docker/api/server.postContainersCreate)·fm(0xc20abea097, 0x4, 0x7f276d88e7d8, 0xc2089f5680, 0xc2085f4ea0, 0xc20abea270, 0x0, 0x0)
/go/src/github.com/docker/docker/api/server/server.go:1569 +0x7b
github.com/docker/docker/api/server.func·008(0x7f276d88e7d8, 0xc2089f5680, 0xc2085f4ea0)
/go/src/github.com/docker/docker/api/server/server.go:1525 +0xc8f
net/http.HandlerFunc.ServeHTTP(0xc20818af80, 0x7f276d88e7d8, 0xc2089f5680, 0xc2085f4ea0)
/usr/local/go/src/net/http/server.go:1265 +0x41
github.com/gorilla/mux.(_Router).ServeHTTP(0xc2080a3ae0, 0x7f276d88e7d8, 0xc2089f5680, 0xc2085f4ea0)
/go/src/github.com/docker/docker/vendor/src/github.com/gorilla/mux/mux.go:98 +0x297
net/http.serverHandler.ServeHTTP(0xc2081ba120, 0x7f276d88e7d8, 0xc2089f5680, 0xc2085f4ea0)
/usr/local/go/src/net/http/server.go:1703 +0x19a
net/http.(_conn).serve(0xc208a720a0)
/usr/local/go/src/net/http/server.go:1204 +0xb57
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:1751 +0x35e

$ bundles/latest/dynbinary/docker version
Client:
Version: 1.8.0-dev
API version: 1.20
Go version: go1.4.2
Git commit: 899c3e9-dirty
Built: Thu Jul 23 19:22:21 UTC 2015
OS/Arch: linux/amd64
Experimental: true

Reproduces every time if a remote API client specifies a /container/create with no HostConfig object specified in the request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugBugs are bugs. The cause may or may not be known at triage time so debugging may be needed.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions