archive_unix_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //go:build !windows
  2. package chrootarchive
  3. import (
  4. gotar "archive/tar"
  5. "bytes"
  6. "io"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "github.com/docker/docker/pkg/archive"
  13. "golang.org/x/sys/unix"
  14. "gotest.tools/v3/assert"
  15. "gotest.tools/v3/skip"
  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. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  23. dir := t.TempDir()
  24. root := filepath.Join(dir, "root")
  25. err := os.Mkdir(root, 0o755)
  26. assert.NilError(t, err)
  27. // Add a file into a directory above root
  28. // Ensure that we can't access this file while tarring.
  29. err = os.WriteFile(filepath.Join(dir, "host-file"), []byte("I am a host file"), 0o644)
  30. assert.NilError(t, err)
  31. // Create some data to copy into the "container" root into
  32. // the symlinked path.
  33. // Before this change, the copy would overwrite the "host" content.
  34. // With this change it should not.
  35. data := filepath.Join(dir, "data")
  36. err = os.Mkdir(data, 0o755)
  37. assert.NilError(t, err)
  38. err = os.WriteFile(filepath.Join(data, "local-file"), []byte("pwn3d"), 0o644)
  39. assert.NilError(t, err)
  40. safe := filepath.Join(root, "safe")
  41. err = unix.Symlink(dir, safe)
  42. assert.NilError(t, err)
  43. rdr, err := archive.TarWithOptions(data, &archive.TarOptions{IncludeFiles: []string{"local-file"}, RebaseNames: map[string]string{"local-file": "host-file"}})
  44. assert.NilError(t, err)
  45. // Use tee to test both the good case and the bad case w/o recreating the archive
  46. bufRdr := bytes.NewBuffer(nil)
  47. tee := io.TeeReader(rdr, bufRdr)
  48. err = UntarWithRoot(tee, safe, nil, root)
  49. assert.Assert(t, err != nil)
  50. assert.ErrorContains(t, err, "open /safe/host-file: no such file or directory")
  51. // Make sure the "host" file is still in tact
  52. // Before the fix the host file would be overwritten
  53. hostData, err := os.ReadFile(filepath.Join(dir, "host-file"))
  54. assert.NilError(t, err)
  55. assert.Equal(t, string(hostData), "I am a host file")
  56. // Now test by chrooting to an attacker controlled path
  57. // This should succeed as is and overwrite a "host" file
  58. // Note that this would be a mis-use of this function.
  59. err = UntarWithRoot(bufRdr, safe, nil, safe)
  60. assert.NilError(t, err)
  61. hostData, err = os.ReadFile(filepath.Join(dir, "host-file"))
  62. assert.NilError(t, err)
  63. assert.Equal(t, string(hostData), "pwn3d")
  64. }
  65. // Test for CVE-2018-15664
  66. // Assures that in the case where an "attacker" controlled path is a symlink to
  67. // some path outside of a container's rootfs that we do not unwittingly leak
  68. // host data into the archive.
  69. func TestTarWithMaliciousSymlinks(t *testing.T) {
  70. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  71. dir, err := os.MkdirTemp("", 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.Mkdir(root, 0o755)
  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 = os.WriteFile(filepath.Join(dir, "host-file"), hostFileData, 0o644)
  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.Mkdir(data, 0o755)
  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. }