Skip to content

[20.10 backport] replace json.Unmarshal with NewFromJSON in Create#41976

Merged
tiborvass merged 1 commit into
moby:20.10from
thaJeztah:20.10_backport_reuse
Feb 18, 2021
Merged

[20.10 backport] replace json.Unmarshal with NewFromJSON in Create#41976
tiborvass merged 1 commit into
moby:20.10from
thaJeztah:20.10_backport_reuse

Conversation

@thaJeztah

Copy link
Copy Markdown
Member

backport of #41701

fixes #41683 /image/store.go Empty configuration causes runtime panic

- What I did

Reuse NewFromJSON to handle no rootfs key situation in image/store.go to solve the issue in #41683

- How I did it

Simply replace the json.Unmarshal function with NewFromJSON in image/image.go

- How to verify it

Running this (mainly modified from #41683) will show invalid image JSON, no RootFS:

package main

import (
    "github.com/docker/docker/image"
    "os"
    "io/ioutil"
    "runtime"
    "github.com/docker/docker/layer"
    "fmt"
)


type mockLayerGetReleaser struct{}

func (ls *mockLayerGetReleaser) Get(layer.ChainID) (layer.Layer, error) {
        return nil, nil
}

func (ls *mockLayerGetReleaser) Release(layer.Layer) ([]layer.Metadata, error) {
        return nil, nil
}

func main(){
    tmpdir, err := ioutil.TempDir("", "images-fs-store")
    defer os.RemoveAll(tmpdir)
    if err != nil {
        fmt.Print(err)
        os.Exit(1)
    }
    fsBackend, err := image.NewFSStoreBackend(tmpdir)
    if err != nil {
        fmt.Print(err)
        os.Exit(1)
    }
    mlgrMap := make(map[string]image.LayerGetReleaser)
    mlgrMap[runtime.GOOS] = &mockLayerGetReleaser{}
    store, err := image.NewImageStore(fsBackend, mlgrMap)
    if err != nil {
        fmt.Print(err)
        os.Exit(1)
    }
    _, err = store.Create([]byte(`{}`))
    if err != nil {
        fmt.Print(err)
        os.Exit(1)
    }
}

- Description for the changelog

Prevent a panic when handling an image json with no `rootfs` key present.

@thaJeztah thaJeztah added status/2-code-review area/images Image Service kind/bugfix PR's that fix bugs labels Feb 3, 2021
@thaJeztah thaJeztah added this to the 20.10.4 milestone Feb 3, 2021
Signed-off-by: Jim Lin <[email protected]>
(cherry picked from commit c9ec21e)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
thaJeztah force-pushed the 20.10_backport_reuse branch from 0a1037e to 34446d0 Compare February 17, 2021 20:18
@thaJeztah

Copy link
Copy Markdown
Member Author

rebased to trigger CI with test-fixes that were merged

@cpuguy83 cpuguy83 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tiborvass
tiborvass merged commit b55d9e1 into moby:20.10 Feb 18, 2021
@thaJeztah
thaJeztah deleted the 20.10_backport_reuse branch February 18, 2021 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants