check.go 6.8 KB

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