archive_unix_test.go 5.1 KB

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