utils_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package archive
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "time"
  11. )
  12. var testUntarFns = map[string]func(string, io.Reader) error{
  13. "untar": func(dest string, r io.Reader) error {
  14. return Untar(r, dest, nil)
  15. },
  16. "applylayer": func(dest string, r io.Reader) error {
  17. _, err := ApplyLayer(dest, r)
  18. return err
  19. },
  20. }
  21. // testBreakout is a helper function that, within the provided `tmpdir` directory,
  22. // creates a `victim` folder with a generated `hello` file in it.
  23. // `untar` extracts to a directory named `dest`, the tar file created from `headers`.
  24. //
  25. // Here are the tested scenarios:
  26. // - removed `victim` folder (write)
  27. // - removed files from `victim` folder (write)
  28. // - new files in `victim` folder (write)
  29. // - modified files in `victim` folder (write)
  30. // - file in `dest` with same content as `victim/hello` (read)
  31. //
  32. // When using testBreakout make sure you cover one of the scenarios listed above.
  33. func testBreakout(untarFn string, tmpdir string, headers []*tar.Header) error {
  34. tmpdir, err := ioutil.TempDir("", tmpdir)
  35. if err != nil {
  36. return err
  37. }
  38. defer os.RemoveAll(tmpdir)
  39. dest := filepath.Join(tmpdir, "dest")
  40. if err := os.Mkdir(dest, 0755); err != nil {
  41. return err
  42. }
  43. victim := filepath.Join(tmpdir, "victim")
  44. if err := os.Mkdir(victim, 0755); err != nil {
  45. return err
  46. }
  47. hello := filepath.Join(victim, "hello")
  48. helloData, err := time.Now().MarshalText()
  49. if err != nil {
  50. return err
  51. }
  52. if err := ioutil.WriteFile(hello, helloData, 0644); err != nil {
  53. return err
  54. }
  55. helloStat, err := os.Stat(hello)
  56. if err != nil {
  57. return err
  58. }
  59. reader, writer := io.Pipe()
  60. go func() {
  61. t := tar.NewWriter(writer)
  62. for _, hdr := range headers {
  63. t.WriteHeader(hdr)
  64. }
  65. t.Close()
  66. }()
  67. untar := testUntarFns[untarFn]
  68. if untar == nil {
  69. return fmt.Errorf("could not find untar function %q in testUntarFns", untarFn)
  70. }
  71. if err := untar(dest, reader); err != nil {
  72. if _, ok := err.(breakoutError); !ok {
  73. // If untar returns an error unrelated to an archive breakout,
  74. // then consider this an unexpected error and abort.
  75. return err
  76. }
  77. // Here, untar detected the breakout.
  78. // Let's move on verifying that indeed there was no breakout.
  79. fmt.Printf("breakoutError: %v\n", err)
  80. }
  81. // Check victim folder
  82. f, err := os.Open(victim)
  83. if err != nil {
  84. // codepath taken if victim folder was removed
  85. return fmt.Errorf("archive breakout: error reading %q: %v", victim, err)
  86. }
  87. defer f.Close()
  88. // Check contents of victim folder
  89. //
  90. // We are only interested in getting 2 files from the victim folder, because if all is well
  91. // we expect only one result, the `hello` file. If there is a second result, it cannot
  92. // hold the same name `hello` and we assume that a new file got created in the victim folder.
  93. // That is enough to detect an archive breakout.
  94. names, err := f.Readdirnames(2)
  95. if err != nil {
  96. // codepath taken if victim is not a folder
  97. return fmt.Errorf("archive breakout: error reading directory content of %q: %v", victim, err)
  98. }
  99. for _, name := range names {
  100. if name != "hello" {
  101. // codepath taken if new file was created in victim folder
  102. return fmt.Errorf("archive breakout: new file %q", name)
  103. }
  104. }
  105. // Check victim/hello
  106. f, err = os.Open(hello)
  107. if err != nil {
  108. // codepath taken if read permissions were removed
  109. return fmt.Errorf("archive breakout: could not lstat %q: %v", hello, err)
  110. }
  111. defer f.Close()
  112. b, err := ioutil.ReadAll(f)
  113. if err != nil {
  114. return err
  115. }
  116. fi, err := f.Stat()
  117. if err != nil {
  118. return err
  119. }
  120. if helloStat.IsDir() != fi.IsDir() ||
  121. // TODO: cannot check for fi.ModTime() change
  122. helloStat.Mode() != fi.Mode() ||
  123. helloStat.Size() != fi.Size() ||
  124. !bytes.Equal(helloData, b) {
  125. // codepath taken if hello has been modified
  126. return fmt.Errorf("archive breakout: file %q has been modified. Contents: expected=%q, got=%q. FileInfo: expected=%#v, got=%#v", hello, helloData, b, helloStat, fi)
  127. }
  128. // Check that nothing in dest/ has the same content as victim/hello.
  129. // Since victim/hello was generated with time.Now(), it is safe to assume
  130. // that any file whose content matches exactly victim/hello, managed somehow
  131. // to access victim/hello.
  132. return filepath.Walk(dest, func(path string, info os.FileInfo, err error) error {
  133. if info.IsDir() {
  134. if err != nil {
  135. // skip directory if error
  136. return filepath.SkipDir
  137. }
  138. // enter directory
  139. return nil
  140. }
  141. if err != nil {
  142. // skip file if error
  143. return nil
  144. }
  145. b, err := ioutil.ReadFile(path)
  146. if err != nil {
  147. // Houston, we have a problem. Aborting (space)walk.
  148. return err
  149. }
  150. if bytes.Equal(helloData, b) {
  151. return fmt.Errorf("archive breakout: file %q has been accessed via %q", hello, path)
  152. }
  153. return nil
  154. })
  155. }