| 1 |
package spec |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"path/filepath" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/google/go-tpm-tools/launcher/internal/logging" |
| 11 |
) |
| 12 |
|
| 13 |
// LaunchPolicy contains policies on starting the container. |
| 14 |
// The policy comes from the labels of the image. |
| 15 |
type LaunchPolicy struct { |
| 16 |
AllowedEnvOverride []string |
| 17 |
AllowedCmdOverride bool |
| 18 |
AllowedLogRedirect policy |
| 19 |
AllowedMountDestinations []string |
| 20 |
HardenedImageMonitoring MonitoringType |
| 21 |
DebugImageMonitoring MonitoringType |
| 22 |
PrivilegedCaps bool |
| 23 |
AllowCgroups bool |
| 24 |
} |
| 25 |
|
| 26 |
type policy int |
| 27 |
|
| 28 |
const ( |
| 29 |
debugOnly policy = iota |
| 30 |
always |
| 31 |
never |
| 32 |
) |
| 33 |
|
| 34 |
// MonitoringType represents the possible health monitoring presets for the client. |
| 35 |
type MonitoringType int |
| 36 |
|
| 37 |
const ( |
| 38 |
// None indicates no monitoring enabled. |
| 39 |
None MonitoringType = iota |
| 40 |
// MemoryOnly indicates only memory_bytes_used enabled. |
| 41 |
MemoryOnly |
| 42 |
// All indicates all supported metrics enabled. |
| 43 |
All |
| 44 |
) |
| 45 |
|
| 46 |
func (mt MonitoringType) String() string { |
| 47 |
switch mt { |
| 48 |
case None: |
| 49 |
return "none" |
| 50 |
case MemoryOnly: |
| 51 |
return "memoryOnly" |
| 52 |
case All: |
| 53 |
return "all" |
| 54 |
} |
| 55 |
|
| 56 |
return "" |
| 57 |
} |
| 58 |
|
| 59 |
func toMonitoringType(s string) (MonitoringType, error) { |
| 60 |
switch strings.ToLower(s) { |
| 61 |
case "none": |
| 62 |
return None, nil |
| 63 |
case "memoryonly": |
| 64 |
return MemoryOnly, nil |
| 65 |
case "all": |
| 66 |
return All, nil |
| 67 |
} |
| 68 |
|
| 69 |
return None, fmt.Errorf("invalid monitoring type %v", s) |
| 70 |
} |
| 71 |
|
| 72 |
// String returns LaunchPolicy details. |
| 73 |
func (p policy) String() string { |
| 74 |
switch p { |
| 75 |
case debugOnly: |
| 76 |
return "debugonly" |
| 77 |
case always: |
| 78 |
return "always" |
| 79 |
case never: |
| 80 |
return "never" |
| 81 |
default: |
| 82 |
return "unspecified launch policy" |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func toPolicy(policy, s string) (policy, error) { |
| 87 |
s = strings.ToLower(s) |
| 88 |
s = strings.TrimSpace(s) |
| 89 |
|
| 90 |
if s == "always" { |
| 91 |
return always, nil |
| 92 |
} |
| 93 |
if s == "never" { |
| 94 |
return never, nil |
| 95 |
} |
| 96 |
if s == "debugonly" { |
| 97 |
return debugOnly, nil |
| 98 |
} |
| 99 |
return 0, fmt.Errorf("not a valid %s %s (must be one of [always, never, debugonly])", policy, s) |
| 100 |
} |
| 101 |
|
| 102 |
const ( |
| 103 |
envOverride = "tee.launch_policy.allow_env_override" |
| 104 |
cmdOverride = "tee.launch_policy.allow_cmd_override" |
| 105 |
logRedirect = "tee.launch_policy.log_redirect" |
| 106 |
memoryMonitoring = "tee.launch_policy.monitoring_memory_allow" |
| 107 |
hardenedMonitoring = "tee.launch_policy.hardened_monitoring" |
| 108 |
debugMonitoring = "tee.launch_policy.debug_monitoring" |
| 109 |
// Values look like a PATH list, with ':' as a separator. |
| 110 |
// Empty paths will be ignored and relative paths will be interpreted as |
| 111 |
// relative to "/". |
| 112 |
// Paths will be cleaned using filepath.Clean. |
| 113 |
mountDestinations = "tee.launch_policy.allow_mount_destinations" |
| 114 |
privilegedCaps = "tee.launch_policy.allow_capabilities" |
| 115 |
allowCgroups = "tee.launch_policy.allow_cgroups" |
| 116 |
) |
| 117 |
|
| 118 |
func configureMonitoringPolicy(imageLabels map[string]string, launchPolicy *LaunchPolicy, logger logging.Logger) error { |
| 119 |
// Old policy. |
| 120 |
memVal, memOk := imageLabels[memoryMonitoring] |
| 121 |
// New policies. |
| 122 |
hardenedVal, hardenedOk := imageLabels[hardenedMonitoring] |
| 123 |
debugVal, debugOk := imageLabels[debugMonitoring] |
| 124 |
|
| 125 |
var err error |
| 126 |
|
| 127 |
// Return an error if old/new policies are both defined |
| 128 |
if memOk && (hardenedOk || debugOk) { |
| 129 |
return fmt.Errorf("use either %s or %s/%s in image labels,- not both", memoryMonitoring, hardenedMonitoring, debugMonitoring) |
| 130 |
} else if memOk { |
| 131 |
policy, err := toPolicy(memoryMonitoring, memVal) |
| 132 |
if err != nil { |
| 133 |
return fmt.Errorf("invalid image LABEL '%s'", memoryMonitoring) |
| 134 |
} |
| 135 |
|
| 136 |
logger.Info(fmt.Sprintf("%s will be deprecated, use %s and %s instead", memoryMonitoring, hardenedMonitoring, debugMonitoring)) |
| 137 |
|
| 138 |
switch policy { |
| 139 |
case always: |
| 140 |
logger.Info(fmt.Sprintf("%s=always will be treated as %s=memory_only and %s=memory_only", memoryMonitoring, hardenedMonitoring, debugMonitoring)) |
| 141 |
launchPolicy.HardenedImageMonitoring = MemoryOnly |
| 142 |
launchPolicy.DebugImageMonitoring = MemoryOnly |
| 143 |
case never: |
| 144 |
logger.Info(fmt.Sprintf("%s=never will be treated as %s=none and %s=none", memoryMonitoring, hardenedMonitoring, debugMonitoring)) |
| 145 |
logger.Info("memory monitoring not allowed by image") |
| 146 |
launchPolicy.HardenedImageMonitoring = None |
| 147 |
launchPolicy.DebugImageMonitoring = None |
| 148 |
case debugOnly: |
| 149 |
logger.Info(fmt.Sprintf("%s=debug_only will be treated as %s=none and %s=memory", memoryMonitoring, hardenedMonitoring, debugMonitoring)) |
| 150 |
logger.Info("memory monitoring only allowed on debug environment by image") |
| 151 |
launchPolicy.HardenedImageMonitoring = None |
| 152 |
launchPolicy.DebugImageMonitoring = MemoryOnly |
| 153 |
} |
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
if hardenedOk { |
| 158 |
launchPolicy.HardenedImageMonitoring, err = toMonitoringType(hardenedVal) |
| 159 |
if err != nil { |
| 160 |
return fmt.Errorf("invalid monitoring type for hardened image: %v", err) |
| 161 |
} |
| 162 |
logger.Info(fmt.Sprintf("'%s' monitoring allowed on hardened environment", launchPolicy.HardenedImageMonitoring)) |
| 163 |
} else { |
| 164 |
launchPolicy.HardenedImageMonitoring = None |
| 165 |
} |
| 166 |
|
| 167 |
if debugOk { |
| 168 |
launchPolicy.DebugImageMonitoring, err = toMonitoringType(debugVal) |
| 169 |
if err != nil { |
| 170 |
return fmt.Errorf("invalid monitoring type for debug image: %v", err) |
| 171 |
} |
| 172 |
logger.Info(fmt.Sprintf("'%s' monitoring allowed on debug environment", launchPolicy.DebugImageMonitoring)) |
| 173 |
} else { |
| 174 |
launchPolicy.DebugImageMonitoring = MemoryOnly |
| 175 |
} |
| 176 |
|
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
// GetLaunchPolicy takes in a map[string] string which should come from image labels, |
| 181 |
// and will try to parse it into a LaunchPolicy. Extra fields will be ignored. |
| 182 |
func GetLaunchPolicy(imageLabels map[string]string, logger logging.Logger) (LaunchPolicy, error) { |
| 183 |
var err error |
| 184 |
launchPolicy := LaunchPolicy{} |
| 185 |
if v, ok := imageLabels[envOverride]; ok { |
| 186 |
envs := strings.Split(v, ",") |
| 187 |
for _, env := range envs { |
| 188 |
// strip out empty env name |
| 189 |
if env != "" { |
| 190 |
launchPolicy.AllowedEnvOverride = append(launchPolicy.AllowedEnvOverride, env) |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if v, ok := imageLabels[cmdOverride]; ok { |
| 196 |
if launchPolicy.AllowedCmdOverride, err = strconv.ParseBool(v); err != nil { |
| 197 |
return LaunchPolicy{}, fmt.Errorf("invalid image LABEL '%s' (not a boolean)", cmdOverride) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// default is debug only for logRedirect |
| 202 |
if v, ok := imageLabels[logRedirect]; ok { |
| 203 |
launchPolicy.AllowedLogRedirect, err = toPolicy(logRedirect, v) |
| 204 |
if err != nil { |
| 205 |
return LaunchPolicy{}, fmt.Errorf("invalid image LABEL '%s'", logRedirect) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if err := configureMonitoringPolicy(imageLabels, &launchPolicy, logger); err != nil { |
| 210 |
return LaunchPolicy{}, err |
| 211 |
} |
| 212 |
|
| 213 |
if v, ok := imageLabels[mountDestinations]; ok { |
| 214 |
paths := filepath.SplitList(v) |
| 215 |
for _, path := range paths { |
| 216 |
// Strip out empty path name. |
| 217 |
if path != "" { |
| 218 |
path = filepath.Clean(path) |
| 219 |
launchPolicy.AllowedMountDestinations = append(launchPolicy.AllowedMountDestinations, path) |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if v, ok := imageLabels[privilegedCaps]; ok { |
| 225 |
if launchPolicy.PrivilegedCaps, err = strconv.ParseBool(v); err != nil { |
| 226 |
return LaunchPolicy{}, fmt.Errorf("invalid image LABEL '%s' (not a boolean)", privilegedCaps) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if v, ok := imageLabels[allowCgroups]; ok { |
| 231 |
if launchPolicy.AllowCgroups, err = strconv.ParseBool(v); err != nil { |
| 232 |
return LaunchPolicy{}, fmt.Errorf("invalid image LABEL '%s' (not a boolean)", allowCgroups) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return launchPolicy, nil |
| 237 |
} |
| 238 |
|
| 239 |
func verifyMonitoringConfig(policy MonitoringType, spec MonitoringType) error { |
| 240 |
switch policy { |
| 241 |
case All: |
| 242 |
// If policy is 'All', spec can be anything. |
| 243 |
return nil |
| 244 |
case MemoryOnly: |
| 245 |
// If policy is 'MemoryOnly', spec must be 'None' or 'MemoryOnly'. |
| 246 |
if spec == All { |
| 247 |
return fmt.Errorf("spec configured for all monitoring, policy only allows memory") |
| 248 |
} |
| 249 |
case None: |
| 250 |
// If policy is 'None', spec must also be 'None'. |
| 251 |
if spec != None { |
| 252 |
return fmt.Errorf("spec configured for %v but policy is none", spec) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return nil |
| 257 |
} |
| 258 |
|
| 259 |
// Verify will use the LaunchPolicy to verify the given LaunchSpec. If the verification passed, will return nil. |
| 260 |
// If there are multiple violations, the function will return the first error. |
| 261 |
func (p LaunchPolicy) Verify(ls LaunchSpec) error { |
| 262 |
for _, e := range ls.Envs { |
| 263 |
if !contains(p.AllowedEnvOverride, e.Name) { |
| 264 |
return fmt.Errorf("env var %s is not allowed to be overridden on this image; allowed envs to be overridden: %v", e, p.AllowedEnvOverride) |
| 265 |
} |
| 266 |
} |
| 267 |
if !p.AllowedCmdOverride && len(ls.Cmd) > 0 { |
| 268 |
return fmt.Errorf("CMD is not allowed to be overridden on this image") |
| 269 |
} |
| 270 |
|
| 271 |
if p.AllowedLogRedirect == never && ls.LogRedirect.enabled() { |
| 272 |
return fmt.Errorf("logging redirection not allowed by image") |
| 273 |
} |
| 274 |
|
| 275 |
if p.AllowedLogRedirect == debugOnly && ls.LogRedirect.enabled() && ls.Hardened { |
| 276 |
return fmt.Errorf("logging redirection only allowed on debug environment by image") |
| 277 |
} |
| 278 |
|
| 279 |
monitoringPolicy := p.DebugImageMonitoring |
| 280 |
if ls.Hardened { |
| 281 |
monitoringPolicy = p.HardenedImageMonitoring |
| 282 |
} |
| 283 |
|
| 284 |
if err := verifyMonitoringConfig(monitoringPolicy, ls.MonitoringEnabled); err != nil { |
| 285 |
return fmt.Errorf("error verifying monitoring config: %v", err) |
| 286 |
} |
| 287 |
|
| 288 |
var err error |
| 289 |
for _, mnt := range ls.Mounts { |
| 290 |
err = errors.Join(err, p.verifyMountDestination(mnt.Mountpoint())) |
| 291 |
} |
| 292 |
if err != nil { |
| 293 |
return fmt.Errorf("destination mount points are not allowed: %v", err) |
| 294 |
} |
| 295 |
|
| 296 |
if len(ls.AddedCapabilities) != 0 && !p.PrivilegedCaps { |
| 297 |
return errors.New("additional capabilities are not allowed") |
| 298 |
} |
| 299 |
|
| 300 |
if ls.CgroupNamespace && !p.AllowCgroups { |
| 301 |
return errors.New("cgroups usage is not allowed") |
| 302 |
} |
| 303 |
|
| 304 |
return nil |
| 305 |
} |
| 306 |
|
| 307 |
// verifyMountDestination assumes AllowedMountDestinations contains |
| 308 |
// `filepath.Clean`ed paths. |
| 309 |
func (p LaunchPolicy) verifyMountDestination(dstPath string) error { |
| 310 |
if !filepath.IsAbs(dstPath) { |
| 311 |
return fmt.Errorf("received a non-absolute destination path: %v", dstPath) |
| 312 |
} |
| 313 |
dstPath = filepath.Clean(dstPath) |
| 314 |
for _, allowDst := range p.AllowedMountDestinations { |
| 315 |
if !filepath.IsAbs(allowDst) { |
| 316 |
return fmt.Errorf("received a non-absolute allowed destination path: %v", allowDst) |
| 317 |
} |
| 318 |
rel, err := filepath.Rel(allowDst, dstPath) |
| 319 |
if err != nil { |
| 320 |
return err |
| 321 |
} |
| 322 |
|
| 323 |
// If dest is not the parent dir relative to the allowed mountpoint |
| 324 |
// or dest is not relative from the allowed's parent directory, then |
| 325 |
// dest must be a child (or the exact same directory). |
| 326 |
if rel != ".." && !strings.HasPrefix(rel, "../") { |
| 327 |
return nil |
| 328 |
} |
| 329 |
} |
| 330 |
return fmt.Errorf("destination mount point \"%v\" is invalid: policy only allows mounts in the following paths: %v", dstPath, p.AllowedMountDestinations) |
| 331 |
} |
| 332 |
|
| 333 |
func contains(strs []string, target string) bool { |
| 334 |
for _, s := range strs { |
| 335 |
if s == target { |
| 336 |
return true |
| 337 |
} |
| 338 |
} |
| 339 |
return false |
| 340 |
} |