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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package specutils
import (
"fmt"
"math/bits"
"path"
"slices"
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/log"
)
type mapping struct {
set bool
val uint32
}
// optionsMap maps mount propagation-related OCI filesystem options to mount(2)
// syscall flags.
var optionsMap = map[string]mapping{
"acl": {set: true, val: unix.MS_POSIXACL},
"async": {set: false, val: unix.MS_SYNCHRONOUS},
"atime": {set: false, val: unix.MS_NOATIME},
"bind": {set: true, val: unix.MS_BIND},
"defaults": {set: true, val: 0},
"dev": {set: false, val: unix.MS_NODEV},
"diratime": {set: false, val: unix.MS_NODIRATIME},
"dirsync": {set: true, val: unix.MS_DIRSYNC},
"exec": {set: false, val: unix.MS_NOEXEC},
"noexec": {set: true, val: unix.MS_NOEXEC},
"iversion": {set: true, val: unix.MS_I_VERSION},
"loud": {set: false, val: unix.MS_SILENT},
"mand": {set: true, val: unix.MS_MANDLOCK},
"noacl": {set: false, val: unix.MS_POSIXACL},
"noatime": {set: true, val: unix.MS_NOATIME},
"nodev": {set: true, val: unix.MS_NODEV},
"nodiratime": {set: true, val: unix.MS_NODIRATIME},
"noiversion": {set: false, val: unix.MS_I_VERSION},
"nomand": {set: false, val: unix.MS_MANDLOCK},
"norelatime": {set: false, val: unix.MS_RELATIME},
"nostrictatime": {set: false, val: unix.MS_STRICTATIME},
"nosuid": {set: true, val: unix.MS_NOSUID},
"rbind": {set: true, val: unix.MS_BIND | unix.MS_REC},
"relatime": {set: true, val: unix.MS_RELATIME},
"remount": {set: true, val: unix.MS_REMOUNT},
"ro": {set: true, val: unix.MS_RDONLY},
"rw": {set: false, val: unix.MS_RDONLY},
"silent": {set: true, val: unix.MS_SILENT},
"strictatime": {set: true, val: unix.MS_STRICTATIME},
"suid": {set: false, val: unix.MS_NOSUID},
"sync": {set: true, val: unix.MS_SYNCHRONOUS},
}
// propOptionsMap is similar to optionsMap, but it lists propagation options
// that cannot be used together with other flags.
var propOptionsMap = map[string]mapping{
"private": {set: true, val: unix.MS_PRIVATE},
"rprivate": {set: true, val: unix.MS_PRIVATE | unix.MS_REC},
"slave": {set: true, val: unix.MS_SLAVE},
"rslave": {set: true, val: unix.MS_SLAVE | unix.MS_REC},
"unbindable": {set: true, val: unix.MS_UNBINDABLE},
"runbindable": {set: true, val: unix.MS_UNBINDABLE | unix.MS_REC},
}
// invalidOptions list options not allowed.
// - shared: sandbox must be isolated from the host. Propagating mount changes
// from the sandbox to the host breaks the isolation. The sandbox's mount
// table is maintained in sentry memory. Mount operations from the application
// are not propagated to the host.
var invalidOptions = []string{"shared", "rshared"}
// OptionsToFlags converts mount options to syscall flags.
func OptionsToFlags(opts []string) uint32 {
return optionsToFlags(opts, optionsMap)
}
// PropOptionsToFlags converts propagation mount options to syscall flags.
// Propagation options cannot be set other with other options and must be
// handled separately.
func PropOptionsToFlags(opts []string) uint32 {
return optionsToFlags(opts, propOptionsMap)
}
func optionsToFlags(opts []string, source map[string]mapping) uint32 {
var rv uint32
for _, opt := range opts {
if m, ok := source[opt]; ok {
if m.set {
rv |= m.val
} else {
rv ^= m.val
}
}
}
return rv
}
// IsReadonlyMount returns true if the mount options has read only option.
func IsReadonlyMount(opts []string) bool {
for _, o := range opts {
if o == "ro" {
return true
}
}
return false
}
// validateMount validates that spec mounts are correct.
func validateMount(mnt *specs.Mount) error {
if !path.IsAbs(mnt.Destination) {
return fmt.Errorf("Mount.Destination must be an absolute path: %v", mnt)
}
if IsGoferMount(*mnt) {
return ValidateMountOptions(mnt.Options)
}
return nil
}
// FilterMountOptions filters out all invalid mount options.
func FilterMountOptions(opts []string) []string {
out := make([]string, 0, len(opts))
for _, o := range opts {
if err := validateMountOption(o); err == nil {
out = append(out, o)
} else {
log.Warningf("mount option skipped %q: %v", o, err)
}
}
return out
}
// ValidateMountOptions validates that mount options are correct.
func ValidateMountOptions(opts []string) error {
for _, o := range opts {
if err := validateMountOption(o); err != nil {
return err
}
}
return nil
}
func validateMountOption(o string) error {
if slices.Contains(invalidOptions, o) {
return fmt.Errorf("mount option %q is not supported", o)
}
return validatePropagation(o)
}
// ValidateRootfsPropagation validates that rootfs propagation options are
// correct.
func validateRootfsPropagation(opt string) error {
flags := PropOptionsToFlags([]string{opt})
if flags&(unix.MS_SLAVE|unix.MS_PRIVATE) == 0 {
return fmt.Errorf("root mount propagation option must specify private or slave: %q", opt)
}
return validatePropagation(opt)
}
func validatePropagation(opt string) error {
flags := PropOptionsToFlags([]string{opt})
exclusive := flags & (unix.MS_SLAVE | unix.MS_PRIVATE | unix.MS_SHARED | unix.MS_UNBINDABLE)
if bits.OnesCount32(exclusive) > 1 {
return fmt.Errorf("mount propagation options are mutually exclusive: %q", opt)
}
return nil
}
|