|
| 1 | +package mountopts |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/containerd/containerd/mount" |
| 5 | + "github.com/moby/buildkit/util/strutil" |
| 6 | + specs "github.com/opencontainers/runtime-spec/specs-go" |
| 7 | + "github.com/pkg/errors" |
| 8 | + "golang.org/x/sys/unix" |
| 9 | +) |
| 10 | + |
| 11 | +// UnprivilegedMountFlags gets the set of mount flags that are set on the mount that contains the given |
| 12 | +// path and are locked by CL_UNPRIVILEGED. This is necessary to ensure that |
| 13 | +// bind-mounting "with options" will not fail with user namespaces, due to |
| 14 | +// kernel restrictions that require user namespace mounts to preserve |
| 15 | +// CL_UNPRIVILEGED locked flags. |
| 16 | +// |
| 17 | +// From https://github.com/moby/moby/blob/v23.0.1/daemon/oci_linux.go#L430-L460 |
| 18 | +func UnprivilegedMountFlags(path string) ([]string, error) { |
| 19 | + var statfs unix.Statfs_t |
| 20 | + if err := unix.Statfs(path, &statfs); err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + |
| 24 | + // The set of keys come from https://github.com/torvalds/linux/blob/v4.13/fs/namespace.c#L1034-L1048. |
| 25 | + unprivilegedFlags := map[uint64]string{ |
| 26 | + unix.MS_RDONLY: "ro", |
| 27 | + unix.MS_NODEV: "nodev", |
| 28 | + unix.MS_NOEXEC: "noexec", |
| 29 | + unix.MS_NOSUID: "nosuid", |
| 30 | + unix.MS_NOATIME: "noatime", |
| 31 | + unix.MS_RELATIME: "relatime", |
| 32 | + unix.MS_NODIRATIME: "nodiratime", |
| 33 | + } |
| 34 | + |
| 35 | + var flags []string |
| 36 | + for mask, flag := range unprivilegedFlags { |
| 37 | + if uint64(statfs.Flags)&mask == mask { |
| 38 | + flags = append(flags, flag) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return flags, nil |
| 43 | +} |
| 44 | + |
| 45 | +// FixUp is for https://github.com/moby/buildkit/issues/3098 |
| 46 | +func FixUp(mounts []mount.Mount) ([]mount.Mount, error) { |
| 47 | + for i, m := range mounts { |
| 48 | + var isBind bool |
| 49 | + for _, o := range m.Options { |
| 50 | + switch o { |
| 51 | + case "bind", "rbind": |
| 52 | + isBind = true |
| 53 | + } |
| 54 | + } |
| 55 | + if !isBind { |
| 56 | + continue |
| 57 | + } |
| 58 | + unpriv, err := UnprivilegedMountFlags(m.Source) |
| 59 | + if err != nil { |
| 60 | + return nil, errors.Wrapf(err, "failed to get unprivileged mount flags for %+v", m) |
| 61 | + } |
| 62 | + m.Options = strutil.DedupeSlice(append(m.Options, unpriv...)) |
| 63 | + mounts[i] = m |
| 64 | + } |
| 65 | + return mounts, nil |
| 66 | +} |
| 67 | + |
| 68 | +func FixUpOCI(mounts []specs.Mount) ([]specs.Mount, error) { |
| 69 | + for i, m := range mounts { |
| 70 | + var isBind bool |
| 71 | + for _, o := range m.Options { |
| 72 | + switch o { |
| 73 | + case "bind", "rbind": |
| 74 | + isBind = true |
| 75 | + } |
| 76 | + } |
| 77 | + if !isBind { |
| 78 | + continue |
| 79 | + } |
| 80 | + unpriv, err := UnprivilegedMountFlags(m.Source) |
| 81 | + if err != nil { |
| 82 | + return nil, errors.Wrapf(err, "failed to get unprivileged mount flags for %+v", m) |
| 83 | + } |
| 84 | + m.Options = strutil.DedupeSlice(append(m.Options, unpriv...)) |
| 85 | + mounts[i] = m |
| 86 | + } |
| 87 | + return mounts, nil |
| 88 | +} |
0 commit comments