github.com/moby/docker@v26.1.3+incompatible/internal/rootless/mountopts/mountopts_linux.go (about) 1 package mountopts 2 3 import ( 4 "golang.org/x/sys/unix" 5 ) 6 7 // UnprivilegedMountFlags gets the set of mount flags that are set on the mount that contains the given 8 // path and are locked by CL_UNPRIVILEGED. This is necessary to ensure that 9 // bind-mounting "with options" will not fail with user namespaces, due to 10 // kernel restrictions that require user namespace mounts to preserve 11 // CL_UNPRIVILEGED locked flags. 12 // 13 // TODO: Move to github.com/moby/sys/mount, and update BuildKit copy of this code as well (https://github.com/moby/buildkit/blob/v0.13.0/util/rootless/mountopts/mountopts_linux.go#L11-L18) 14 func UnprivilegedMountFlags(path string) ([]string, error) { 15 var statfs unix.Statfs_t 16 if err := unix.Statfs(path, &statfs); err != nil { 17 return nil, err 18 } 19 20 // The set of keys come from https://github.com/torvalds/linux/blob/v4.13/fs/namespace.c#L1034-L1048. 21 unprivilegedFlags := map[uint64]string{ 22 unix.MS_RDONLY: "ro", 23 unix.MS_NODEV: "nodev", 24 unix.MS_NOEXEC: "noexec", 25 unix.MS_NOSUID: "nosuid", 26 unix.MS_NOATIME: "noatime", 27 unix.MS_RELATIME: "relatime", 28 unix.MS_NODIRATIME: "nodiratime", 29 } 30 31 var flags []string 32 for mask, flag := range unprivilegedFlags { 33 if uint64(statfs.Flags)&mask == mask { 34 flags = append(flags, flag) 35 } 36 } 37 38 return flags, nil 39 }