archive_unix_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // +build !windows
  2. package chrootarchive
  3. import (
  4. "bytes"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "github.com/docker/docker/pkg/archive"
  11. "golang.org/x/sys/unix"
  12. "gotest.tools/assert"
  13. )
  14. // Test for CVE-2018-15664
  15. // Assures that in the case where an "attacker" controlled path is a symlink to
  16. // some path outside of a container's rootfs that we do not copy data to a
  17. // container path that will actually overwrite data on the host
  18. func TestUntarWithMaliciousSymlinks(t *testing.T) {
  19. dir, err := ioutil.TempDir("", t.Name())
  20. assert.NilError(t, err)
  21. defer os.RemoveAll(dir)
  22. root := filepath.Join(dir, "root")
  23. err = os.MkdirAll(root, 0755)
  24. assert.NilError(t, err)
  25. // Add a file into a directory above root
  26. // Ensure that we can't access this file while tarring.
  27. err = ioutil.WriteFile(filepath.Join(dir, "host-file"), []byte("I am a host file"), 0644)
  28. assert.NilError(t, err)
  29. // Create some data which which will be copied into the "container" root into
  30. // the symlinked path.
  31. // Before this change, the copy would overwrite the "host" content.
  32. // With this change it should not.
  33. data := filepath.Join(dir, "data")
  34. err = os.MkdirAll(data, 0755)
  35. assert.NilError(t, err)
  36. err = ioutil.WriteFile(filepath.Join(data, "local-file"), []byte("pwn3d"), 0644)
  37. assert.NilError(t, err)
  38. safe := filepath.Join(root, "safe")
  39. err = unix.Symlink(dir, safe)
  40. assert.NilError(t, err)
  41. rdr, err := archive.TarWithOptions(data, &archive.TarOptions{IncludeFiles: []string{"local-file"}, RebaseNames: map[string]string{"local-file": "host-file"}})
  42. assert.NilError(t, err)
  43. // Use tee to test both the good case and the bad case w/o recreating the archive
  44. bufRdr := bytes.NewBuffer(nil)
  45. tee := io.TeeReader(rdr, bufRdr)
  46. err = UntarWithRoot(tee, safe, nil, root)
  47. assert.Assert(t, err != nil)
  48. assert.ErrorContains(t, err, "open /safe/host-file: no such file or directory")
  49. // Make sure the "host" file is still in tact
  50. // Before the fix the host file would be overwritten
  51. hostData, err := ioutil.ReadFile(filepath.Join(dir, "host-file"))
  52. assert.NilError(t, err)
  53. assert.Equal(t, string(hostData), "I am a host file")
  54. // Now test by chrooting to an attacker controlled path
  55. // This should succeed as is and overwrite a "host" file
  56. // Note that this would be a mis-use of this function.
  57. err = UntarWithRoot(bufRdr, safe, nil, safe)
  58. assert.NilError(t, err)
  59. hostData, err = ioutil.ReadFile(filepath.Join(dir, "host-file"))
  60. assert.NilError(t, err)
  61. assert.Equal(t, string(hostData), "pwn3d")
  62. }