Skip to content

ExportContainer: do not panic - #36586

Merged
thaJeztah merged 2 commits into
moby:masterfrom
kolyshkin:do-not-panic
Mar 14, 2018
Merged

ExportContainer: do not panic#36586
thaJeztah merged 2 commits into
moby:masterfrom
kolyshkin:do-not-panic

Conversation

@kolyshkin

Copy link
Copy Markdown
Contributor

This is proposed replacement for #36563, fixing #36561 and a few more potential nil deref cases. Please see more detailed description in commit messages.

@vdemeester @tonistiigi @thaJeztah PTAL

In case ContainerExport() is called for an unmounted container, it leads
to a daemon panic as container.BaseFS, which is dereferenced here, is
nil.

To fix, do not rely on container.BaseFS; use the one returned from
rwlayer.Mount().

Fixes: 7a7357d ("LCOW: Implemented support for docker cp + build")
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Commit 7a7357d ("LCOW: Implemented support for docker cp + build")
changed `container.BaseFS` from being a string (that could be empty but
can't lead to nil pointer dereference) to containerfs.ContainerFS,
which could be be `nil` and so nil dereference is at least theoretically
possible, which leads to panic (i.e. engine crashes).

Such a panic can be avoided by carefully analysing the source code in all
the places that dereference a variable, to make the variable can't be nil.
Practically, this analisys are impossible as code is constantly
evolving.

Still, we need to avoid panics and crashes. A good way to do so is to
explicitly check that a variable is non-nil, returning an error
otherwise. Even in case such a check looks absolutely redundant,
further changes to the code might make it useful, and having an
extra check is not a big price to pay to avoid a panic.

This commit adds such checks for all the places where it is not obvious
that container.BaseFS is not nil (which in this case means we do not
call daemon.Mount() a few lines earlier).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

@vdemeester vdemeester 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 🐯

@lowenna lowenna 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

Comment thread container/archive.go
// an error if the path points to outside the container's rootfs.
func (container *Container) ResolvePath(path string) (resolvedPath, absPath string, err error) {
if container.BaseFS == nil {
return "", "", errors.New("ResolvePath: BaseFS of container " + container.ID + " is unexpectedly nil")

@thaJeztah thaJeztah Mar 14, 2018

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.

Wonder if we should prefix all of these with the function-name (as we don't do it in other places)

@kolyshkin kolyshkin Mar 15, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The chance of such error is quite low (i.e. it should not happen with the current code base, as far as I was able to see), so normally a user won't even see it. If they do see it, though, it is kind of an internal error (as in "this software is buggy"), and having some (internal) context would be great to ease the debugging. Ideally, I would even add a stack trace here...

@thaJeztah

Copy link
Copy Markdown
Member

Failure on z/s390x is a flaky test, and being addressed in #36551

@andrewhsu

Copy link
Copy Markdown
Contributor

@kolyshkin if there isn't any test that covers the codebase affected by this PR, can we get unit tests for this in a separate PR?

@kolyshkin

Copy link
Copy Markdown
Contributor Author

can we get unit tests for this in a separate PR?

OK, I will see if I can come up with a unit test, imitating a condition described in #36561

@kolyshkin
kolyshkin deleted the do-not-panic branch March 15, 2018 06:55
@kolyshkin

Copy link
Copy Markdown
Contributor Author

OK, I will see if I can come up with a unit test, imitating a condition described in #36561

Having said that, a unit test makes little sense here, it is much better to have an integration test. Here: #36606

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants