github.com/containerd/Containerd@v1.4.13/snapshots/overlay/check.go (about) 1 // +build linux 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package overlay 20 21 import ( 22 "fmt" 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 27 "github.com/containerd/containerd/log" 28 "github.com/containerd/containerd/mount" 29 "github.com/containerd/containerd/sys" 30 "github.com/containerd/continuity/fs" 31 "github.com/pkg/errors" 32 ) 33 34 // supportsMultipleLowerDir checks if the system supports multiple lowerdirs, 35 // which is required for the overlay snapshotter. On 4.x kernels, multiple lowerdirs 36 // are always available (so this check isn't needed), and backported to RHEL and 37 // CentOS 3.x kernels (3.10.0-693.el7.x86_64 and up). This function is to detect 38 // support on those kernels, without doing a kernel version compare. 39 // 40 // Ported from moby overlay2. 41 func supportsMultipleLowerDir(d string) error { 42 td, err := ioutil.TempDir(d, "multiple-lowerdir-check") 43 if err != nil { 44 return err 45 } 46 defer func() { 47 if err := os.RemoveAll(td); err != nil { 48 log.L.WithError(err).Warnf("Failed to remove check directory %v", td) 49 } 50 }() 51 52 for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} { 53 if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil { 54 return err 55 } 56 } 57 58 opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", filepath.Join(td, "lower2"), filepath.Join(td, "lower1"), filepath.Join(td, "upper"), filepath.Join(td, "work")) 59 m := mount.Mount{ 60 Type: "overlay", 61 Source: "overlay", 62 Options: []string{opts}, 63 } 64 dest := filepath.Join(td, "merged") 65 if err := m.Mount(dest); err != nil { 66 return errors.Wrap(err, "failed to mount overlay") 67 } 68 if err := mount.UnmountAll(dest, 0); err != nil { 69 log.L.WithError(err).Warnf("Failed to unmount check directory %v", dest) 70 } 71 return nil 72 } 73 74 // Supported returns nil when the overlayfs is functional on the system with the root directory. 75 // Supported is not called during plugin initialization, but exposed for downstream projects which uses 76 // this snapshotter as a library. 77 func Supported(root string) error { 78 if err := os.MkdirAll(root, 0700); err != nil { 79 return err 80 } 81 supportsDType, err := fs.SupportsDType(root) 82 if err != nil { 83 return err 84 } 85 if !supportsDType { 86 return fmt.Errorf("%s does not support d_type. If the backing filesystem is xfs, please reformat with ftype=1 to enable d_type support", root) 87 } 88 return supportsMultipleLowerDir(root) 89 } 90 91 // NeedsUserXAttr returns whether overlayfs should be mounted with the "userxattr" mount option. 92 // 93 // The "userxattr" option is needed for mounting overlayfs inside a user namespace with kernel >= 5.11. 94 // 95 // The "userxattr" option is NOT needed for the initial user namespace (aka "the host"). 96 // 97 // Also, Ubuntu (since circa 2015) and Debian (since 10) with kernel < 5.11 can mount 98 // the overlayfs in a user namespace without the "userxattr" option. 99 // 100 // The corresponding kernel commit: https://github.com/torvalds/linux/commit/2d2f2d7322ff43e0fe92bf8cccdc0b09449bf2e1 101 // > ovl: user xattr 102 // > 103 // > Optionally allow using "user.overlay." namespace instead of "trusted.overlay." 104 // > ... 105 // > Disable redirect_dir and metacopy options, because these would allow privilege escalation through direct manipulation of the 106 // > "user.overlay.redirect" or "user.overlay.metacopy" xattrs. 107 // > ... 108 // 109 // The "userxattr" support is not exposed in "/sys/module/overlay/parameters". 110 func NeedsUserXAttr(d string) (bool, error) { 111 if !sys.RunningInUserNS() { 112 // we are the real root (i.e., the root in the initial user NS), 113 // so we do never need "userxattr" opt. 114 return false, nil 115 } 116 117 // TODO: add fast path for kernel >= 5.11 . 118 // 119 // Keep in mind that distro vendors might be going to backport the patch to older kernels. 120 // So we can't completely remove the check. 121 122 tdRoot := filepath.Join(d, "userxattr-check") 123 if err := os.RemoveAll(tdRoot); err != nil { 124 log.L.WithError(err).Warnf("Failed to remove check directory %v", tdRoot) 125 } 126 127 if err := os.MkdirAll(tdRoot, 0700); err != nil { 128 return false, err 129 } 130 131 defer func() { 132 if err := os.RemoveAll(tdRoot); err != nil { 133 log.L.WithError(err).Warnf("Failed to remove check directory %v", tdRoot) 134 } 135 }() 136 137 td, err := ioutil.TempDir(tdRoot, "") 138 if err != nil { 139 return false, err 140 } 141 142 for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} { 143 if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil { 144 return false, err 145 } 146 } 147 148 opts := []string{ 149 fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", filepath.Join(td, "lower2"), filepath.Join(td, "lower1"), filepath.Join(td, "upper"), filepath.Join(td, "work")), 150 "userxattr", 151 } 152 153 m := mount.Mount{ 154 Type: "overlay", 155 Source: "overlay", 156 Options: opts, 157 } 158 159 dest := filepath.Join(td, "merged") 160 if err := m.Mount(dest); err != nil { 161 // Probably the host is running Ubuntu/Debian kernel (< 5.11) with the userns patch but without the userxattr patch. 162 // Return false without error. 163 log.L.WithError(err).Debugf("cannot mount overlay with \"userxattr\", probably the kernel does not support userxattr") 164 return false, nil 165 } 166 if err := mount.UnmountAll(dest, 0); err != nil { 167 log.L.WithError(err).Warnf("Failed to unmount check directory %v", dest) 168 } 169 return true, nil 170 }