summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2021-06-24 13:53:36 +0200
committerValentin Rothberg <rothberg@redhat.com>2021-06-24 14:58:11 +0200
commit879d66e7db25d1bd1ce8c0906ceb069cdf713f62 (patch)
tree132449013e980eea8ea003fb75a0393db7283ad8
parent3ee967bd5da30ddbd3a8625b00bb82f903641800 (diff)
downloadpodman-879d66e7db25d1bd1ce8c0906ceb069cdf713f62.tar.gz
podman-879d66e7db25d1bd1ce8c0906ceb069cdf713f62.tar.bz2
podman-879d66e7db25d1bd1ce8c0906ceb069cdf713f62.zip
[v3.2] cp: do not allow dir->file copying
Fix a bug in `podman-cp` to forbid copying directories to files. Previously, the directory was copied to the parent directory of the file which is wrong. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
-rw-r--r--cmd/podman/containers/cp.go8
-rw-r--r--test/system/065-cp.bats9
2 files changed, 17 insertions, 0 deletions
diff --git a/cmd/podman/containers/cp.go b/cmd/podman/containers/cp.go
index 27aacc6e5..0be135c91 100644
--- a/cmd/podman/containers/cp.go
+++ b/cmd/podman/containers/cp.go
@@ -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()
@@ -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()
diff --git a/test/system/065-cp.bats b/test/system/065-cp.bats
index 24ac8118e..eda04611f 100644
--- a/test/system/065-cp.bats
+++ b/test/system/065-cp.bats
@@ -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
}
@@ -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