File: mountopts_linux.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (88 lines) | stat: -rw-r--r-- 2,421 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package mountopts

import (
	"github.com/containerd/containerd/mount"
	"github.com/moby/buildkit/util/strutil"
	specs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"golang.org/x/sys/unix"
)

// UnprivilegedMountFlags gets the set of mount flags that are set on the mount that contains the given
// path and are locked by CL_UNPRIVILEGED. This is necessary to ensure that
// bind-mounting "with options" will not fail with user namespaces, due to
// kernel restrictions that require user namespace mounts to preserve
// CL_UNPRIVILEGED locked flags.
//
// From https://github.com/moby/moby/blob/v23.0.1/daemon/oci_linux.go#L430-L460
func UnprivilegedMountFlags(path string) ([]string, error) {
	var statfs unix.Statfs_t
	if err := unix.Statfs(path, &statfs); err != nil {
		return nil, err
	}

	// The set of keys come from https://github.com/torvalds/linux/blob/v4.13/fs/namespace.c#L1034-L1048.
	unprivilegedFlags := map[uint64]string{
		unix.MS_RDONLY:     "ro",
		unix.MS_NODEV:      "nodev",
		unix.MS_NOEXEC:     "noexec",
		unix.MS_NOSUID:     "nosuid",
		unix.MS_NOATIME:    "noatime",
		unix.MS_RELATIME:   "relatime",
		unix.MS_NODIRATIME: "nodiratime",
	}

	var flags []string
	for mask, flag := range unprivilegedFlags {
		if uint64(statfs.Flags)&mask == mask {
			flags = append(flags, flag)
		}
	}

	return flags, nil
}

// FixUp is for https://github.com/moby/buildkit/issues/3098
func FixUp(mounts []mount.Mount) ([]mount.Mount, error) {
	for i, m := range mounts {
		var isBind bool
		for _, o := range m.Options {
			switch o {
			case "bind", "rbind":
				isBind = true
			}
		}
		if !isBind {
			continue
		}
		unpriv, err := UnprivilegedMountFlags(m.Source)
		if err != nil {
			return nil, errors.Wrapf(err, "failed to get unprivileged mount flags for %+v", m)
		}
		m.Options = strutil.DedupeSlice(append(m.Options, unpriv...))
		mounts[i] = m
	}
	return mounts, nil
}

func FixUpOCI(mounts []specs.Mount) ([]specs.Mount, error) {
	for i, m := range mounts {
		var isBind bool
		for _, o := range m.Options {
			switch o {
			case "bind", "rbind":
				isBind = true
			}
		}
		if !isBind {
			continue
		}
		unpriv, err := UnprivilegedMountFlags(m.Source)
		if err != nil {
			return nil, errors.Wrapf(err, "failed to get unprivileged mount flags for %+v", m)
		}
		m.Options = strutil.DedupeSlice(append(m.Options, unpriv...))
		mounts[i] = m
	}
	return mounts, nil
}