archive_unix_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //go:build !windows
  2. // +build !windows
  3. package chrootarchive
  4. import (
  5. gotar "archive/tar"
  6. "bytes"
  7. "io"
  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 := t.TempDir()
  25. root := filepath.Join(dir, "root")
  26. err := os.MkdirAll(root, 0o755)
  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 = os.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 = os.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 := os.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 = os.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. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  72. dir, err := os.MkdirTemp("", t.Name())
  73. assert.NilError(t, err)
  74. // defer os.RemoveAll(dir)
  75. t.Log(dir)
  76. root := filepath.Join(dir, "root")
  77. err = os.MkdirAll(root, 0755)
  78. assert.NilError(t, err)
  79. hostFileData := []byte("I am a host file")
  80. // Add a file into a directory above root
  81. // Ensure that we can't access this file while tarring.
  82. err = os.WriteFile(filepath.Join(dir, "host-file"), hostFileData, 0644)
  83. assert.NilError(t, err)
  84. safe := filepath.Join(root, "safe")
  85. err = unix.Symlink(dir, safe)
  86. assert.NilError(t, err)
  87. data := filepath.Join(dir, "data")
  88. err = os.MkdirAll(data, 0755)
  89. assert.NilError(t, err)
  90. type testCase struct {
  91. p string
  92. includes []string
  93. }
  94. cases := []testCase{
  95. {p: safe, includes: []string{"host-file"}},
  96. {p: safe + "/", includes: []string{"host-file"}},
  97. {p: safe, includes: nil},
  98. {p: safe + "/", includes: nil},
  99. {p: root, includes: []string{"safe/host-file"}},
  100. {p: root, includes: []string{"/safe/host-file"}},
  101. {p: root, includes: nil},
  102. }
  103. maxBytes := len(hostFileData)
  104. for _, tc := range cases {
  105. t.Run(path.Join(tc.p+"_"+strings.Join(tc.includes, "_")), func(t *testing.T) {
  106. // Here if we use archive.TarWithOptions directly or change the "root" parameter
  107. // to be the same as "safe", data from the host will be leaked into the archive
  108. var opts *archive.TarOptions
  109. if tc.includes != nil {
  110. opts = &archive.TarOptions{
  111. IncludeFiles: tc.includes,
  112. }
  113. }
  114. rdr, err := Tar(tc.p, opts, root)
  115. assert.NilError(t, err)
  116. defer rdr.Close()
  117. tr := gotar.NewReader(rdr)
  118. assert.Assert(t, !isDataInTar(t, tr, hostFileData, int64(maxBytes)), "host data leaked to archive")
  119. })
  120. }
  121. }
  122. func isDataInTar(t *testing.T, tr *gotar.Reader, compare []byte, maxBytes int64) bool {
  123. for {
  124. h, err := tr.Next()
  125. if err == io.EOF {
  126. break
  127. }
  128. assert.NilError(t, err)
  129. if h.Size == 0 {
  130. continue
  131. }
  132. assert.Assert(t, h.Size <= maxBytes, "%s: file size exceeds max expected size %d: %d", h.Name, maxBytes, h.Size)
  133. data := make([]byte, int(h.Size))
  134. _, err = io.ReadFull(tr, data)
  135. assert.NilError(t, err)
  136. if bytes.Contains(data, compare) {
  137. return true
  138. }
  139. }
  140. return false
  141. }