|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// These are copied from container_create_linux.go and should be consolidated later. |
| 18 | + |
| 19 | +package podsandbox |
| 20 | + |
| 21 | +import ( |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/containerd/containerd/contrib/seccomp" |
| 28 | + "github.com/containerd/containerd/oci" |
| 29 | + runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + // profileNamePrefix is the prefix for loading profiles on a localhost. Eg. AppArmor localhost/profileName. |
| 34 | + profileNamePrefix = "localhost/" // TODO (mikebrow): get localhost/ & runtime/default from CRI kubernetes/kubernetes#51747 |
| 35 | + // runtimeDefault indicates that we should use or create a runtime default profile. |
| 36 | + runtimeDefault = "runtime/default" |
| 37 | + // dockerDefault indicates that we should use or create a docker default profile. |
| 38 | + dockerDefault = "docker/default" |
| 39 | + // unconfinedProfile is a string indicating one should run a pod/containerd without a security profile |
| 40 | + unconfinedProfile = "unconfined" |
| 41 | +) |
| 42 | + |
| 43 | +// generateSeccompSpecOpts generates containerd SpecOpts for seccomp. |
| 44 | +func (c *Controller) generateSeccompSpecOpts(sp *runtime.SecurityProfile, privileged, seccompEnabled bool) (oci.SpecOpts, error) { |
| 45 | + if privileged { |
| 46 | + // Do not set seccomp profile when container is privileged |
| 47 | + return nil, nil |
| 48 | + } |
| 49 | + if !seccompEnabled { |
| 50 | + if sp != nil { |
| 51 | + if sp.ProfileType != runtime.SecurityProfile_Unconfined { |
| 52 | + return nil, errors.New("seccomp is not supported") |
| 53 | + } |
| 54 | + } |
| 55 | + return nil, nil |
| 56 | + } |
| 57 | + |
| 58 | + if sp == nil { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + |
| 62 | + if sp.ProfileType != runtime.SecurityProfile_Localhost && sp.LocalhostRef != "" { |
| 63 | + return nil, errors.New("seccomp config invalid LocalhostRef must only be set if ProfileType is Localhost") |
| 64 | + } |
| 65 | + switch sp.ProfileType { |
| 66 | + case runtime.SecurityProfile_Unconfined: |
| 67 | + // Do not set seccomp profile. |
| 68 | + return nil, nil |
| 69 | + case runtime.SecurityProfile_RuntimeDefault: |
| 70 | + return seccomp.WithDefaultProfile(), nil |
| 71 | + case runtime.SecurityProfile_Localhost: |
| 72 | + // trimming the localhost/ prefix just in case even though it should not |
| 73 | + // be necessary with the new SecurityProfile struct |
| 74 | + return seccomp.WithProfile(strings.TrimPrefix(sp.LocalhostRef, profileNamePrefix)), nil |
| 75 | + default: |
| 76 | + return nil, errors.New("seccomp unknown ProfileType") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func generateSeccompSecurityProfile(profilePath string, unsetProfilePath string) (*runtime.SecurityProfile, error) { |
| 81 | + if profilePath != "" { |
| 82 | + return generateSecurityProfile(profilePath) |
| 83 | + } |
| 84 | + if unsetProfilePath != "" { |
| 85 | + return generateSecurityProfile(unsetProfilePath) |
| 86 | + } |
| 87 | + return nil, nil |
| 88 | +} |
| 89 | + |
| 90 | +func generateSecurityProfile(profilePath string) (*runtime.SecurityProfile, error) { |
| 91 | + switch profilePath { |
| 92 | + case runtimeDefault, dockerDefault, "": |
| 93 | + return &runtime.SecurityProfile{ |
| 94 | + ProfileType: runtime.SecurityProfile_RuntimeDefault, |
| 95 | + }, nil |
| 96 | + case unconfinedProfile: |
| 97 | + return &runtime.SecurityProfile{ |
| 98 | + ProfileType: runtime.SecurityProfile_Unconfined, |
| 99 | + }, nil |
| 100 | + default: |
| 101 | + // Require and Trim default profile name prefix |
| 102 | + if !strings.HasPrefix(profilePath, profileNamePrefix) { |
| 103 | + return nil, fmt.Errorf("invalid profile %q", profilePath) |
| 104 | + } |
| 105 | + return &runtime.SecurityProfile{ |
| 106 | + ProfileType: runtime.SecurityProfile_Localhost, |
| 107 | + LocalhostRef: strings.TrimPrefix(profilePath, profileNamePrefix), |
| 108 | + }, nil |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// generateUserString generates valid user string based on OCI Image Spec |
| 113 | +// v1.0.0. |
| 114 | +// |
| 115 | +// CRI defines that the following combinations are valid: |
| 116 | +// |
| 117 | +// (none) -> "" |
| 118 | +// username -> username |
| 119 | +// username, uid -> username |
| 120 | +// username, uid, gid -> username:gid |
| 121 | +// username, gid -> username:gid |
| 122 | +// uid -> uid |
| 123 | +// uid, gid -> uid:gid |
| 124 | +// gid -> error |
| 125 | +// |
| 126 | +// TODO(random-liu): Add group name support in CRI. |
| 127 | +func generateUserString(username string, uid, gid *runtime.Int64Value) (string, error) { |
| 128 | + var userstr, groupstr string |
| 129 | + if uid != nil { |
| 130 | + userstr = strconv.FormatInt(uid.GetValue(), 10) |
| 131 | + } |
| 132 | + if username != "" { |
| 133 | + userstr = username |
| 134 | + } |
| 135 | + if gid != nil { |
| 136 | + groupstr = strconv.FormatInt(gid.GetValue(), 10) |
| 137 | + } |
| 138 | + if userstr == "" { |
| 139 | + if groupstr != "" { |
| 140 | + return "", fmt.Errorf("user group %q is specified without user", groupstr) |
| 141 | + } |
| 142 | + return "", nil |
| 143 | + } |
| 144 | + if groupstr != "" { |
| 145 | + userstr = userstr + ":" + groupstr |
| 146 | + } |
| 147 | + return userstr, nil |
| 148 | +} |
0 commit comments