Skip to content

Commit

Permalink
Merge pull request #10775 from vrothberg/3.2-fix-cp
Browse files Browse the repository at this point in the history
[v3.2] cp: do not allow dir->file copying
  • Loading branch information
openshift-merge-robot authored Jun 24, 2021
2 parents 3ee967b + 879d66e commit f8a793a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/podman/containers/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func copyFromContainer(container string, containerPath string, hostPath string)
containerTarget = filepath.Dir(containerTarget)
}

if !isStdout && containerInfo.IsDir && !hostInfo.IsDir {
return errors.New("destination must be a directory when copying a directory")
}

reader, writer := io.Pipe()
hostCopy := func() error {
defer reader.Close()
Expand Down Expand Up @@ -336,6 +340,10 @@ func copyToContainer(container string, containerPath string, hostPath string) er
stdinFile = tmpFile.Name()
}

if hostInfo.IsDir && !containerInfo.IsDir {
return errors.New("destination must be a directory when copying a directory")
}

reader, writer := io.Pipe()
hostCopy := func() error {
defer writer.Close()
Expand Down
9 changes: 9 additions & 0 deletions test/system/065-cp.bats
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ load helpers
run_podman rm -f cpcontainer
done < <(parse_table "$tests")

run_podman create --name cpcontainer --workdir=/srv $cpimage sleep infinity
run_podman 125 cp $srcdir cpcontainer:/etc/os-release
is "$output" "Error: destination must be a directory when copying a directory" "cannot copy directory to file"
run_podman rm -f cpcontainer

run_podman rmi -f $cpimage
}

Expand Down Expand Up @@ -343,6 +348,10 @@ load helpers
is "$(< $destdir$dest_fullname/containerfile1)" "${randomcontent[1]}" "$description"
rm -rf $destdir/*
done < <(parse_table "$tests")

touch $destdir/testfile
run_podman 125 cp cpcontainer:/etc/ $destdir/testfile
is "$output" "Error: destination must be a directory when copying a directory" "cannot copy directory to file"
run_podman rm -f cpcontainer

run_podman rmi -f $cpimage
Expand Down

0 comments on commit f8a793a

Please sign in to comment.