123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- package main
- import (
- "bytes"
- "fmt"
- "io/ioutil"
- "os"
- "os/exec"
- "path/filepath"
- "testing"
- )
- const (
- cpTestPathParent = "/some"
- cpTestPath = "/some/path"
- cpTestName = "test"
- cpFullPath = "/some/path/test"
- cpContainerContents = "holla, i am the container"
- cpHostContents = "hello, i am the host"
- )
- // Test for #5656
- // Check that garbage paths don't escape the container's rootfs
- func TestCpGarbagePath(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
- t.Fatal(err)
- }
- hostFile, err := os.Create(cpFullPath)
- if err != nil {
- t.Fatal(err)
- }
- defer hostFile.Close()
- defer os.RemoveAll(cpTestPathParent)
- fmt.Fprintf(hostFile, "%s", cpHostContents)
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- tmpname := filepath.Join(tmpdir, cpTestName)
- defer os.RemoveAll(tmpdir)
- path := filepath.Join("../../../../../../../../../../../../", cpFullPath)
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
- if err != nil {
- t.Fatalf("couldn't copy from garbage path: %s:%s %s", cleanedContainerID, path, err)
- }
- file, _ := os.Open(tmpname)
- defer file.Close()
- test, err := ioutil.ReadAll(file)
- if err != nil {
- t.Fatal(err)
- }
- if string(test) == cpHostContents {
- t.Errorf("output matched host file -- garbage path can escape container rootfs")
- }
- if string(test) != cpContainerContents {
- t.Errorf("output doesn't match the input for garbage path")
- }
- logDone("cp - garbage paths relative to container's rootfs")
- }
- // Check that relative paths are relative to the container's rootfs
- func TestCpRelativePath(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
- t.Fatal(err)
- }
- hostFile, err := os.Create(cpFullPath)
- if err != nil {
- t.Fatal(err)
- }
- defer hostFile.Close()
- defer os.RemoveAll(cpTestPathParent)
- fmt.Fprintf(hostFile, "%s", cpHostContents)
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- tmpname := filepath.Join(tmpdir, cpTestName)
- defer os.RemoveAll(tmpdir)
- path, _ := filepath.Rel("/", cpFullPath)
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
- if err != nil {
- t.Fatalf("couldn't copy from relative path: %s:%s %s", cleanedContainerID, path, err)
- }
- file, _ := os.Open(tmpname)
- defer file.Close()
- test, err := ioutil.ReadAll(file)
- if err != nil {
- t.Fatal(err)
- }
- if string(test) == cpHostContents {
- t.Errorf("output matched host file -- relative path can escape container rootfs")
- }
- if string(test) != cpContainerContents {
- t.Errorf("output doesn't match the input for relative path")
- }
- logDone("cp - relative paths relative to container's rootfs")
- }
- // Check that absolute paths are relative to the container's rootfs
- func TestCpAbsolutePath(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
- t.Fatal(err)
- }
- hostFile, err := os.Create(cpFullPath)
- if err != nil {
- t.Fatal(err)
- }
- defer hostFile.Close()
- defer os.RemoveAll(cpTestPathParent)
- fmt.Fprintf(hostFile, "%s", cpHostContents)
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- tmpname := filepath.Join(tmpdir, cpTestName)
- defer os.RemoveAll(tmpdir)
- path := cpFullPath
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
- if err != nil {
- t.Fatalf("couldn't copy from absolute path: %s:%s %s", cleanedContainerID, path, err)
- }
- file, _ := os.Open(tmpname)
- defer file.Close()
- test, err := ioutil.ReadAll(file)
- if err != nil {
- t.Fatal(err)
- }
- if string(test) == cpHostContents {
- t.Errorf("output matched host file -- absolute path can escape container rootfs")
- }
- if string(test) != cpContainerContents {
- t.Errorf("output doesn't match the input for absolute path")
- }
- logDone("cp - absolute paths relative to container's rootfs")
- }
- // Test for #5619
- // Check that absolute symlinks are still relative to the container's rootfs
- func TestCpAbsoluteSymlink(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
- t.Fatal(err)
- }
- hostFile, err := os.Create(cpFullPath)
- if err != nil {
- t.Fatal(err)
- }
- defer hostFile.Close()
- defer os.RemoveAll(cpTestPathParent)
- fmt.Fprintf(hostFile, "%s", cpHostContents)
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- tmpname := filepath.Join(tmpdir, cpTestName)
- defer os.RemoveAll(tmpdir)
- path := filepath.Join("/", "container_path")
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
- if err != nil {
- t.Fatalf("couldn't copy from absolute path: %s:%s %s", cleanedContainerID, path, err)
- }
- file, _ := os.Open(tmpname)
- defer file.Close()
- test, err := ioutil.ReadAll(file)
- if err != nil {
- t.Fatal(err)
- }
- if string(test) == cpHostContents {
- t.Errorf("output matched host file -- absolute symlink can escape container rootfs")
- }
- if string(test) != cpContainerContents {
- t.Errorf("output doesn't match the input for absolute symlink")
- }
- logDone("cp - absolute symlink relative to container's rootfs")
- }
- // Test for #5619
- // Check that symlinks which are part of the resource path are still relative to the container's rootfs
- func TestCpSymlinkComponent(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
- t.Fatal(err)
- }
- hostFile, err := os.Create(cpFullPath)
- if err != nil {
- t.Fatal(err)
- }
- defer hostFile.Close()
- defer os.RemoveAll(cpTestPathParent)
- fmt.Fprintf(hostFile, "%s", cpHostContents)
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- tmpname := filepath.Join(tmpdir, cpTestName)
- defer os.RemoveAll(tmpdir)
- path := filepath.Join("/", "container_path", cpTestName)
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
- if err != nil {
- t.Fatalf("couldn't copy from symlink path component: %s:%s %s", cleanedContainerID, path, err)
- }
- file, _ := os.Open(tmpname)
- defer file.Close()
- test, err := ioutil.ReadAll(file)
- if err != nil {
- t.Fatal(err)
- }
- if string(test) == cpHostContents {
- t.Errorf("output matched host file -- symlink path component can escape container rootfs")
- }
- if string(test) != cpContainerContents {
- t.Errorf("output doesn't match the input for symlink path component")
- }
- logDone("cp - symlink path components relative to container's rootfs")
- }
- // Check that cp with unprivileged user doesn't return any error
- func TestCpUnprivilegedUser(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
- if err = os.Chmod(tmpdir, 0777); err != nil {
- t.Fatal(err)
- }
- path := cpTestName
- _, _, err = runCommandWithOutput(exec.Command("su", "unprivilegeduser", "-c", dockerBinary+" cp "+cleanedContainerID+":"+path+" "+tmpdir))
- if err != nil {
- t.Fatalf("couldn't copy with unprivileged user: %s:%s %s", cleanedContainerID, path, err)
- }
- logDone("cp - unprivileged user")
- }
- func TestCpVolumePath(t *testing.T) {
- tmpDir, err := ioutil.TempDir("", "cp-test-volumepath")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
- outDir, err := ioutil.TempDir("", "cp-test-volumepath-out")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(outDir)
- _, err = os.Create(tmpDir + "/test")
- if err != nil {
- t.Fatal(err)
- }
- out, exitCode, err := dockerCmd(t, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- // Copy actual volume path
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/foo", outDir)
- if err != nil {
- t.Fatalf("couldn't copy from volume path: %s:%s %v", cleanedContainerID, "/foo", err)
- }
- stat, err := os.Stat(outDir + "/foo")
- if err != nil {
- t.Fatal(err)
- }
- if !stat.IsDir() {
- t.Fatal("expected copied content to be dir")
- }
- stat, err = os.Stat(outDir + "/foo/bar")
- if err != nil {
- t.Fatal(err)
- }
- if stat.IsDir() {
- t.Fatal("Expected file `bar` to be a file")
- }
- // Copy file nested in volume
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/foo/bar", outDir)
- if err != nil {
- t.Fatalf("couldn't copy from volume path: %s:%s %v", cleanedContainerID, "/foo", err)
- }
- stat, err = os.Stat(outDir + "/bar")
- if err != nil {
- t.Fatal(err)
- }
- if stat.IsDir() {
- t.Fatal("Expected file `bar` to be a file")
- }
- // Copy Bind-mounted dir
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/baz", outDir)
- if err != nil {
- t.Fatalf("couldn't copy from bind-mounted volume path: %s:%s %v", cleanedContainerID, "/baz", err)
- }
- stat, err = os.Stat(outDir + "/baz")
- if err != nil {
- t.Fatal(err)
- }
- if !stat.IsDir() {
- t.Fatal("Expected `baz` to be a dir")
- }
- // Copy file nested in bind-mounted dir
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/baz/test", outDir)
- fb, err := ioutil.ReadFile(outDir + "/baz/test")
- if err != nil {
- t.Fatal(err)
- }
- fb2, err := ioutil.ReadFile(tmpDir + "/test")
- if err != nil {
- t.Fatal(err)
- }
- if !bytes.Equal(fb, fb2) {
- t.Fatalf("Expected copied file to be duplicate of bind-mounted file")
- }
- // Copy bind-mounted file
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/test", outDir)
- fb, err = ioutil.ReadFile(outDir + "/test")
- if err != nil {
- t.Fatal(err)
- }
- fb2, err = ioutil.ReadFile(tmpDir + "/test")
- if err != nil {
- t.Fatal(err)
- }
- if !bytes.Equal(fb, fb2) {
- t.Fatalf("Expected copied file to be duplicate of bind-mounted file")
- }
- logDone("cp - volume path")
- }
- func TestCpToDot(t *testing.T) {
- out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
- if err != nil || exitCode != 0 {
- t.Fatal("failed to create a container", out, err)
- }
- cleanedContainerID := stripTrailingCharacters(out)
- defer deleteContainer(cleanedContainerID)
- out, _, err = dockerCmd(t, "wait", cleanedContainerID)
- if err != nil || stripTrailingCharacters(out) != "0" {
- t.Fatal("failed to set up container", out, err)
- }
- tmpdir, err := ioutil.TempDir("", "docker-integration")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
- cwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(cwd)
- if err := os.Chdir(tmpdir); err != nil {
- t.Fatal(err)
- }
- _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/test", ".")
- if err != nil {
- t.Fatalf("couldn't docker cp to \".\" path: %s", err)
- }
- content, err := ioutil.ReadFile("./test")
- if string(content) != "lololol\n" {
- t.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
- }
- logDone("cp - to dot path")
- }
|