check.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //go:build linux
  2. // +build linux
  3. package overlay2 // import "github.com/docker/docker/daemon/graphdriver/overlay2"
  4. import (
  5. "fmt"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "syscall"
  10. "github.com/containerd/containerd/mount"
  11. "github.com/containerd/containerd/pkg/userns"
  12. "github.com/docker/docker/daemon/graphdriver/overlayutils"
  13. "github.com/docker/docker/pkg/system"
  14. "github.com/pkg/errors"
  15. "golang.org/x/sys/unix"
  16. )
  17. // doesSupportNativeDiff checks whether the filesystem has a bug
  18. // which copies up the opaque flag when copying up an opaque
  19. // directory or the kernel enable CONFIG_OVERLAY_FS_REDIRECT_DIR.
  20. // When these exist naive diff should be used.
  21. //
  22. // When running in a user namespace, returns errRunningInUserNS
  23. // immediately.
  24. func doesSupportNativeDiff(d string) error {
  25. if userns.RunningInUserNS() {
  26. return errors.New("running in a user namespace")
  27. }
  28. td, err := os.MkdirTemp(d, "opaque-bug-check")
  29. if err != nil {
  30. return err
  31. }
  32. defer func() {
  33. if err := os.RemoveAll(td); err != nil {
  34. logger.Warnf("Failed to remove check directory %v: %v", td, err)
  35. }
  36. }()
  37. // Make directories l1/d, l1/d1, l2/d, l3, work, merged
  38. if err := os.MkdirAll(filepath.Join(td, "l1", "d"), 0755); err != nil {
  39. return err
  40. }
  41. if err := os.MkdirAll(filepath.Join(td, "l1", "d1"), 0755); err != nil {
  42. return err
  43. }
  44. if err := os.MkdirAll(filepath.Join(td, "l2", "d"), 0755); err != nil {
  45. return err
  46. }
  47. if err := os.Mkdir(filepath.Join(td, "l3"), 0755); err != nil {
  48. return err
  49. }
  50. if err := os.Mkdir(filepath.Join(td, workDirName), 0755); err != nil {
  51. return err
  52. }
  53. if err := os.Mkdir(filepath.Join(td, mergedDirName), 0755); err != nil {
  54. return err
  55. }
  56. // Mark l2/d as opaque
  57. if err := system.Lsetxattr(filepath.Join(td, "l2", "d"), "trusted.overlay.opaque", []byte("y"), 0); err != nil {
  58. return errors.Wrap(err, "failed to set opaque flag on middle layer")
  59. }
  60. opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", path.Join(td, "l2"), path.Join(td, "l1"), path.Join(td, "l3"), path.Join(td, workDirName))
  61. if err := unix.Mount("overlay", filepath.Join(td, mergedDirName), "overlay", 0, opts); err != nil {
  62. return errors.Wrap(err, "failed to mount overlay")
  63. }
  64. defer func() {
  65. if err := unix.Unmount(filepath.Join(td, mergedDirName), 0); err != nil {
  66. logger.Warnf("Failed to unmount check directory %v: %v", filepath.Join(td, mergedDirName), err)
  67. }
  68. }()
  69. // Touch file in d to force copy up of opaque directory "d" from "l2" to "l3"
  70. if err := os.WriteFile(filepath.Join(td, mergedDirName, "d", "f"), []byte{}, 0644); err != nil {
  71. return errors.Wrap(err, "failed to write to merged directory")
  72. }
  73. // Check l3/d does not have opaque flag
  74. xattrOpaque, err := system.Lgetxattr(filepath.Join(td, "l3", "d"), "trusted.overlay.opaque")
  75. if err != nil {
  76. return errors.Wrap(err, "failed to read opaque flag on upper layer")
  77. }
  78. if string(xattrOpaque) == "y" {
  79. return errors.New("opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix")
  80. }
  81. // rename "d1" to "d2"
  82. if err := os.Rename(filepath.Join(td, mergedDirName, "d1"), filepath.Join(td, mergedDirName, "d2")); err != nil {
  83. // if rename failed with syscall.EXDEV, the kernel doesn't have CONFIG_OVERLAY_FS_REDIRECT_DIR enabled
  84. if err.(*os.LinkError).Err == syscall.EXDEV {
  85. return nil
  86. }
  87. return errors.Wrap(err, "failed to rename dir in merged directory")
  88. }
  89. // get the xattr of "d2"
  90. xattrRedirect, err := system.Lgetxattr(filepath.Join(td, "l3", "d2"), "trusted.overlay.redirect")
  91. if err != nil {
  92. return errors.Wrap(err, "failed to read redirect flag on upper layer")
  93. }
  94. if string(xattrRedirect) == "d1" {
  95. return errors.New("kernel has CONFIG_OVERLAY_FS_REDIRECT_DIR enabled")
  96. }
  97. return nil
  98. }
  99. // Forked from https://github.com/containers/storage/blob/05c69f1b2a5871d170c07dc8d2eec69c681e143b/drivers/overlay/check.go
  100. //
  101. // usingMetacopy checks if overlayfs's metacopy feature is active. When active,
  102. // overlayfs will only copy up metadata (as opposed to the whole file) when a
  103. // metadata-only operation is performed. Affected inodes will be marked with
  104. // the "(trusted|user).overlay.metacopy" xattr.
  105. //
  106. // The CONFIG_OVERLAY_FS_METACOPY option, the overlay.metacopy parameter, or
  107. // the metacopy mount option can all enable metacopy mode. For more details on
  108. // this feature, see filesystems/overlayfs.txt in the kernel documentation
  109. // tree.
  110. //
  111. // Note that the mount option should never be relevant should never come up the
  112. // daemon has control over all of its own mounts and presently does not request
  113. // metacopy. Nonetheless, a user or kernel distributor may enable metacopy, so
  114. // we should report in the daemon whether or not we detect its use.
  115. func usingMetacopy(d string) (bool, error) {
  116. userxattr := false
  117. if userns.RunningInUserNS() {
  118. needed, err := overlayutils.NeedsUserXAttr(d)
  119. if err != nil {
  120. return false, err
  121. }
  122. if needed {
  123. userxattr = true
  124. }
  125. }
  126. td, err := os.MkdirTemp(d, "metacopy-check")
  127. if err != nil {
  128. return false, err
  129. }
  130. defer func() {
  131. if err := os.RemoveAll(td); err != nil {
  132. logger.WithError(err).Warnf("failed to remove check directory %v", td)
  133. }
  134. }()
  135. l1, l2, work, merged := filepath.Join(td, "l1"), filepath.Join(td, "l2"), filepath.Join(td, "work"), filepath.Join(td, "merged")
  136. for _, dir := range []string{l1, l2, work, merged} {
  137. if err := os.Mkdir(dir, 0755); err != nil {
  138. return false, err
  139. }
  140. }
  141. // Create empty file in l1 with 0700 permissions for metacopy test
  142. if err := os.WriteFile(filepath.Join(l1, "f"), []byte{}, 0700); err != nil {
  143. return false, err
  144. }
  145. opts := []string{fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", l1, l2, work)}
  146. if userxattr {
  147. opts = append(opts, "userxattr")
  148. }
  149. m := mount.Mount{
  150. Type: "overlay",
  151. Source: "overlay",
  152. Options: opts,
  153. }
  154. if err := m.Mount(merged); err != nil {
  155. return false, errors.Wrap(err, "failed to mount overlay for metacopy check")
  156. }
  157. defer func() {
  158. if err := mount.UnmountAll(merged, 0); err != nil {
  159. logger.WithError(err).Warnf("failed to unmount check directory %v", merged)
  160. }
  161. }()
  162. // Make a change that only impacts the inode, in the upperdir
  163. if err := os.Chmod(filepath.Join(merged, "f"), 0600); err != nil {
  164. return false, errors.Wrap(err, "error changing permissions on file for metacopy check")
  165. }
  166. // ...and check if the pulled-up copy is marked as metadata-only
  167. xattr, err := system.Lgetxattr(filepath.Join(l2, "f"), overlayutils.GetOverlayXattr("metacopy"))
  168. if err != nil {
  169. // ENOTSUP signifies the FS does not support either xattrs or metacopy. In either case,
  170. // it is not a fatal error, and we should report metacopy as unused.
  171. if errors.Is(err, unix.ENOTSUP) {
  172. return false, nil
  173. }
  174. return false, errors.Wrap(err, "metacopy flag was not set on file in the upperdir")
  175. }
  176. usingMetacopy := xattr != nil
  177. logger.WithField("usingMetacopy", usingMetacopy).Debug("successfully detected metacopy status")
  178. return usingMetacopy, nil
  179. }