Skip to content

Commit f29f6aa

Browse files
Merge pull request containers#10772 from vrothberg/fix-cp
cp: do not allow dir->file copying
2 parents da33fc4 + b108269 commit f29f6aa

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

cmd/podman/containers/cp.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func copyFromContainer(container string, containerPath string, hostPath string)
177177
containerTarget = filepath.Dir(containerTarget)
178178
}
179179

180+
if !isStdout && containerInfo.IsDir && !hostInfo.IsDir {
181+
return errors.New("destination must be a directory when copying a directory")
182+
}
183+
180184
reader, writer := io.Pipe()
181185
hostCopy := func() error {
182186
defer reader.Close()
@@ -334,6 +338,10 @@ func copyToContainer(container string, containerPath string, hostPath string) er
334338
stdinFile = tmpFile.Name()
335339
}
336340

341+
if hostInfo.IsDir && !containerInfo.IsDir {
342+
return errors.New("destination must be a directory when copying a directory")
343+
}
344+
337345
reader, writer := io.Pipe()
338346
hostCopy := func() error {
339347
defer writer.Close()

test/system/065-cp.bats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ load helpers
272272
run_podman rm -f cpcontainer
273273
done < <(parse_table "$tests")
274274

275+
run_podman create --name cpcontainer --workdir=/srv $cpimage sleep infinity
276+
run_podman 125 cp $srcdir cpcontainer:/etc/os-release
277+
is "$output" "Error: destination must be a directory when copying a directory" "cannot copy directory to file"
278+
run_podman rm -f cpcontainer
279+
275280
run_podman rmi -f $cpimage
276281
}
277282

@@ -343,6 +348,10 @@ load helpers
343348
is "$(< $destdir$dest_fullname/containerfile1)" "${randomcontent[1]}" "$description"
344349
rm -rf $destdir/*
345350
done < <(parse_table "$tests")
351+
352+
touch $destdir/testfile
353+
run_podman 125 cp cpcontainer:/etc/ $destdir/testfile
354+
is "$output" "Error: destination must be a directory when copying a directory" "cannot copy directory to file"
346355
run_podman rm -f cpcontainer
347356

348357
run_podman rmi -f $cpimage

0 commit comments

Comments
 (0)