Skip to content

Commit

Permalink
libpod: fix lookup for subpath in volumes
Browse files Browse the repository at this point in the history
a subdirectory that is below a mount destination is detected as a
subpath.

Closes: containers#15789

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Sep 14, 2022
1 parent 92dc61d commit 14e5d1c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
26 changes: 22 additions & 4 deletions libpod/container_path_resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,29 @@ func findVolume(c *Container, containerPath string) (*Volume, error) {
return nil, nil
}

// isSubDir checks whether path is a subdirectory of root.
func isSubDir(path, root string) bool {
// check if the specified container path is below a bind mount.
rel, err := filepath.Rel(root, path)
if err != nil {
return false
}
return rel != ".." && !strings.HasPrefix(rel, "../")
}

// isPathOnVolume returns true if the specified containerPath is a subdir of any
// Volume's destination.
func isPathOnVolume(c *Container, containerPath string) bool {
cleanedContainerPath := filepath.Clean(containerPath)
for _, vol := range c.config.NamedVolumes {
if cleanedContainerPath == filepath.Clean(vol.Dest) {
cleanedDestination := filepath.Clean(vol.Dest)
if cleanedContainerPath == cleanedDestination {
return true
}
for dest := vol.Dest; dest != "/" && dest != "."; dest = filepath.Dir(dest) {
if isSubDir(cleanedContainerPath, cleanedDestination) {
return true
}
for dest := cleanedDestination; dest != "/" && dest != "."; dest = filepath.Dir(dest) {
if cleanedContainerPath == dest {
return true
}
Expand Down Expand Up @@ -157,10 +171,14 @@ func findBindMount(c *Container, containerPath string) *specs.Mount {
func isPathOnMount(c *Container, containerPath string) bool {
cleanedContainerPath := filepath.Clean(containerPath)
for _, m := range c.config.Spec.Mounts {
if cleanedContainerPath == filepath.Clean(m.Destination) {
cleanedDestination := filepath.Clean(m.Destination)
if cleanedContainerPath == cleanedDestination {
return true
}
if isSubDir(cleanedContainerPath, cleanedDestination) {
return true
}
for dest := m.Destination; dest != "/" && dest != "."; dest = filepath.Dir(dest) {
for dest := cleanedDestination; dest != "/" && dest != "."; dest = filepath.Dir(dest) {
if cleanedContainerPath == dest {
return true
}
Expand Down
28 changes: 28 additions & 0 deletions libpod/container_path_resolution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package libpod

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsSubDir(t *testing.T) {
assert.True(t, isSubDir("/foo", "/foo"))
assert.True(t, isSubDir("/foo/bar", "/foo"))
assert.True(t, isSubDir("/foo/bar", "/foo/"))
assert.True(t, isSubDir("/foo/bar", "/foo//"))
assert.True(t, isSubDir("/foo/bar/", "/foo"))
assert.True(t, isSubDir("/foo/bar/baz/", "/foo"))
assert.True(t, isSubDir("/foo/bar/baz/", "/foo/bar"))
assert.True(t, isSubDir("/foo/bar/baz/", "/foo/bar/"))
assert.False(t, isSubDir("/foo/bar/baz/", "/foobar/"))
assert.False(t, isSubDir("/foo/bar/baz/../../", "/foobar/"))
assert.False(t, isSubDir("/foo/bar/baz/", "../foo/bar"))
assert.False(t, isSubDir("/foo/bar/baz/", "../foo/"))
assert.False(t, isSubDir("/foo/bar/baz/", "../foo"))
assert.False(t, isSubDir("/", ".."))
assert.False(t, isSubDir("//", ".."))
assert.False(t, isSubDir("//", "../"))
assert.False(t, isSubDir("//", "..//"))
assert.True(t, isSubDir("/foo/bar/baz/../../", "/foo/"))
}
9 changes: 9 additions & 0 deletions test/e2e/run_working_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ var _ = Describe("Podman run", func() {
Expect(session).Should(Exit(126))
})

It("podman run a container using a --workdir under a bind mount", func() {
volume, err := CreateTempDirInTempDir()
Expect(err).To(BeNil())

session := podmanTest.Podman([]string{"run", "--volume", fmt.Sprintf("%s:/var_ovl/:O", volume), "--workdir", "/var_ovl/log", ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
})

It("podman run a container on an image with a workdir", func() {
dockerfile := fmt.Sprintf(`FROM %s
RUN mkdir -p /home/foobar /etc/foobar; chown bin:bin /etc/foobar
Expand Down

0 comments on commit 14e5d1c

Please sign in to comment.