archive_unix_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // +build !windows
  2. package chrootarchive
  3. import (
  4. gotar "archive/tar"
  5. "bytes"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/docker/docker/pkg/archive"
  14. "golang.org/x/sys/unix"
  15. "gotest.tools/assert"
  16. )
  17. // Test for CVE-2018-15664
  18. // Assures that in the case where an "attacker" controlled path is a symlink to
  19. // some path outside of a container's rootfs that we do not copy data to a
  20. // container path that will actually overwrite data on the host
  21. func TestUntarWithMaliciousSymlinks(t *testing.T) {
  22. dir, err := ioutil.TempDir("", t.Name())
  23. assert.NilError(t, err)
  24. defer os.RemoveAll(dir)
  25. root := filepath.Join(dir, "root")
  26. err = os.MkdirAll(root, 0755)
  27. assert.NilError(t, err)
  28. // Add a file into a directory above root
  29. // Ensure that we can't access this file while tarring.
  30. err = ioutil.WriteFile(filepath.Join(dir, "host-file"), []byte("I am a host file"), 0644)
  31. assert.NilError(t, err)
  32. // Create some data which which will be copied into the "container" root into
  33. // the symlinked path.
  34. // Before this change, the copy would overwrite the "host" content.
  35. // With this change it should not.
  36. data := filepath.Join(dir, "data")
  37. err = os.MkdirAll(data, 0755)
  38. assert.NilError(t, err)
  39. err = ioutil.WriteFile(filepath.Join(data, "local-file"), []byte("pwn3d"), 0644)
  40. assert.NilError(t, err)
  41. safe := filepath.Join(root, "safe")
  42. err = unix.Symlink(dir, safe)
  43. assert.NilError(t, err)
  44. rdr, err := archive.TarWithOptions(data, &archive.TarOptions{IncludeFiles: []string{"local-file"}, RebaseNames: map[string]string{"local-file": "host-file"}})
  45. assert.NilError(t, err)
  46. // Use tee to test both the good case and the bad case w/o recreating the archive
  47. bufRdr := bytes.NewBuffer(nil)
  48. tee := io.TeeReader(rdr, bufRdr)
  49. err = UntarWithRoot(tee, safe, nil, root)
  50. assert.Assert(t, err != nil)
  51. assert.ErrorContains(t, err, "open /safe/host-file: no such file or directory")
  52. // Make sure the "host" file is still in tact
  53. // Before the fix the host file would be overwritten
  54. hostData, err := ioutil.ReadFile(filepath.Join(dir, "host-file"))
  55. assert.NilError(t, err)
  56. assert.Equal(t, string(hostData), "I am a host file")
  57. // Now test by chrooting to an attacker controlled path
  58. // This should succeed as is and overwrite a "host" file
  59. // Note that this would be a mis-use of this function.
  60. err = UntarWithRoot(bufRdr, safe, nil, safe)
  61. assert.NilError(t, err)
  62. hostData, err = ioutil.ReadFile(filepath.Join(dir, "host-file"))
  63. assert.NilError(t, err)
  64. assert.Equal(t, string(hostData), "pwn3d")
  65. }
  66. // Test for CVE-2018-15664
  67. // Assures that in the case where an "attacker" controlled path is a symlink to
  68. // some path outside of a container's rootfs that we do not unwittingly leak
  69. // host data into the archive.
  70. func TestTarWithMaliciousSymlinks(t *testing.T) {
  71. dir, err := ioutil.TempDir("", t.Name())
  72. assert.NilError(t, err)
  73. // defer os.RemoveAll(dir)
  74. t.Log(dir)
  75. root := filepath.Join(dir, "root")
  76. err = os.MkdirAll(root, 0755)
  77. assert.NilError(t, err)
  78. hostFileData := []byte("I am a host file")
  79. // Add a file into a directory above root
  80. // Ensure that we can't access this file while tarring.
  81. err = ioutil.WriteFile(filepath.Join(dir, "host-file"), hostFileData, 0644)
  82. assert.NilError(t, err)
  83. safe := filepath.Join(root, "safe")
  84. err = unix.Symlink(dir, safe)
  85. assert.NilError(t, err)
  86. data := filepath.Join(dir, "data")
  87. err = os.MkdirAll(data, 0755)
  88. assert.NilError(t, err)
  89. type testCase struct {
  90. p string
  91. includes []string
  92. }
  93. cases := []testCase{
  94. {p: safe, includes: []string{"host-file"}},
  95. {p: safe + "/", includes: []string{"host-file"}},
  96. {p: safe, includes: nil},
  97. {p: safe + "/", includes: nil},
  98. {p: root, includes: []string{"safe/host-file"}},
  99. {p: root, includes: []string{"/safe/host-file"}},
  100. {p: root, includes: nil},
  101. }
  102. maxBytes := len(hostFileData)
  103. for _, tc := range cases {
  104. t.Run(path.Join(tc.p+"_"+strings.Join(tc.includes, "_")), func(t *testing.T) {
  105. // Here if we use archive.TarWithOptions directly or change the "root" parameter
  106. // to be the same as "safe", data from the host will be leaked into the archive
  107. var opts *archive.TarOptions
  108. if tc.includes != nil {
  109. opts = &archive.TarOptions{
  110. IncludeFiles: tc.includes,
  111. }
  112. }
  113. rdr, err := Tar(tc.p, opts, root)
  114. assert.NilError(t, err)
  115. defer rdr.Close()
  116. tr := gotar.NewReader(rdr)
  117. assert.Assert(t, !isDataInTar(t, tr, hostFileData, int64(maxBytes)), "host data leaked to archive")
  118. })
  119. }
  120. }
  121. func isDataInTar(t *testing.T, tr *gotar.Reader, compare []byte, maxBytes int64) bool {
  122. for {
  123. h, err := tr.Next()
  124. if err == io.EOF {
  125. break
  126. }
  127. assert.NilError(t, err)
  128. if h.Size == 0 {
  129. continue
  130. }
  131. assert.Assert(t, h.Size <= maxBytes, "%s: file size exceeds max expected size %d: %d", h.Name, maxBytes, h.Size)
  132. data := make([]byte, int(h.Size))
  133. _, err = io.ReadFull(tr, data)
  134. assert.NilError(t, err)
  135. if bytes.Contains(data, compare) {
  136. return true
  137. }
  138. }
  139. return false
  140. }