1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #36242 from kolyshkin/rwlayer-nil-deref

c.RWLayer: check for nil before use
This commit is contained in:
Sebastiaan van Stijn 2018-02-09 18:13:03 -08:00 committed by GitHub
commit ab3ea81376
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 3 deletions

View file

@ -22,6 +22,9 @@ func (daemon *Daemon) ContainerChanges(name string) ([]archive.Change, error) {
container.Lock()
defer container.Unlock()
if container.RWLayer == nil {
return nil, errors.New("RWLayer of container " + name + " is unexpectedly nil")
}
c, err := container.RWLayer.Changes()
if err != nil {
return nil, err

View file

@ -1051,6 +1051,9 @@ func (daemon *Daemon) Shutdown() error {
// Mount sets container.BaseFS
// (is it not set coming in? why is it unset?)
func (daemon *Daemon) Mount(container *container.Container) error {
if container.RWLayer == nil {
return errors.New("RWLayer of container " + container.ID + " is unexpectedly nil")
}
dir, err := container.RWLayer.Mount(container.GetMountLabel())
if err != nil {
return err
@ -1073,6 +1076,9 @@ func (daemon *Daemon) Mount(container *container.Container) error {
// Unmount unsets the container base filesystem
func (daemon *Daemon) Unmount(container *container.Container) error {
if container.RWLayer == nil {
return errors.New("RWLayer of container " + container.ID + " is unexpectedly nil")
}
if err := container.RWLayer.Unmount(); err != nil {
logrus.Errorf("Error unmounting container %s: %s", container.ID, err)
return err

View file

@ -1,6 +1,7 @@
package daemon // import "github.com/docker/docker/daemon"
import (
"errors"
"fmt"
"time"
@ -184,14 +185,24 @@ func (daemon *Daemon) getInspectData(container *container.Container) (*types.Con
contJSONBase.GraphDriver.Name = container.Driver
if container.RWLayer == nil {
if container.Dead {
return contJSONBase, nil
}
return nil, errdefs.System(errors.New("RWLayer of container " + container.ID + " is unexpectedly nil"))
}
graphDriverData, err := container.RWLayer.Metadata()
// If container is marked as Dead, the container's graphdriver metadata
// could have been removed, it will cause error if we try to get the metadata,
// we can ignore the error if the container is dead.
if err != nil && !container.Dead {
return nil, errdefs.System(err)
if err != nil {
if !container.Dead {
return nil, errdefs.System(err)
}
} else {
contJSONBase.GraphDriver.Data = graphDriverData
}
contJSONBase.GraphDriver.Data = graphDriverData
return contJSONBase, nil
}

33
daemon/inspect_test.go Normal file
View file

@ -0,0 +1,33 @@
package daemon // import "github.com/docker/docker/daemon"
import (
"testing"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/container"
"github.com/docker/docker/daemon/config"
"github.com/docker/docker/daemon/exec"
"github.com/stretchr/testify/assert"
)
func TestGetInspectData(t *testing.T) {
c := &container.Container{
ID: "inspect-me",
HostConfig: &containertypes.HostConfig{},
State: container.NewState(),
ExecCommands: exec.NewStore(),
}
d := &Daemon{
linkIndex: newLinkIndex(),
configStore: &config.Config{},
}
_, err := d.getInspectData(c)
assert.Error(t, err)
c.Dead = true
_, err = d.getInspectData(c)
assert.NoError(t, err)
}

View file

@ -1,6 +1,7 @@
package daemon // import "github.com/docker/docker/daemon"
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
@ -156,6 +157,9 @@ func (daemon *Daemon) createSpec(c *container.Container) (*specs.Spec, error) {
// Reverse order, expecting parent most first
s.Windows.LayerFolders = append([]string{layerPath}, s.Windows.LayerFolders...)
}
if c.RWLayer == nil {
return nil, errors.New("RWLayer of container " + c.ID + " is unexpectedly nil")
}
m, err := c.RWLayer.Metadata()
if err != nil {
return nil, fmt.Errorf("failed to get layer metadata - %s", err)