Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.2] cp: do not allow dir->file copying #10775

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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