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