@@ -36,7 +36,6 @@ import (
3636 "github.com/opencontainers/selinux/go-selinux/label"
3737 runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
3838
39- "github.com/containerd/containerd/pkg/cri/annotations"
4039 "github.com/containerd/containerd/pkg/cri/config"
4140 customopts "github.com/containerd/containerd/pkg/cri/opts"
4241)
@@ -126,51 +125,7 @@ func (c *criService) platformSpec(
126125 extraMounts []* runtime.Mount ,
127126 ociRuntime config.Runtime ,
128127) (_ []oci.SpecOpts , retErr error ) {
129- specOpts := []oci.SpecOpts {
130- oci .WithoutRunMount ,
131- }
132- // only clear the default security settings if the runtime does not have a custom
133- // base runtime spec spec. Admins can use this functionality to define
134- // default ulimits, seccomp, or other default settings.
135- if ociRuntime .BaseRuntimeSpec == "" {
136- specOpts = append (specOpts , customopts .WithoutDefaultSecuritySettings )
137- }
138- specOpts = append (specOpts ,
139- customopts .WithRelativeRoot (relativeRootfsPath ),
140- customopts .WithProcessArgs (config , imageConfig ),
141- oci .WithDefaultPathEnv ,
142- // this will be set based on the security context below
143- oci .WithNewPrivileges ,
144- )
145- if config .GetWorkingDir () != "" {
146- specOpts = append (specOpts , oci .WithProcessCwd (config .GetWorkingDir ()))
147- } else if imageConfig .WorkingDir != "" {
148- specOpts = append (specOpts , oci .WithProcessCwd (imageConfig .WorkingDir ))
149- }
150-
151- if config .GetTty () {
152- specOpts = append (specOpts , oci .WithTTY )
153- }
154-
155- // Add HOSTNAME env.
156- var (
157- err error
158- hostname = sandboxConfig .GetHostname ()
159- )
160- if hostname == "" {
161- if hostname , err = c .os .Hostname (); err != nil {
162- return nil , err
163- }
164- }
165- specOpts = append (specOpts , oci .WithEnv ([]string {hostnameEnv + "=" + hostname }))
166-
167- // Apply envs from image config first, so that envs from container config
168- // can override them.
169- env := append ([]string {}, imageConfig .Env ... )
170- for _ , e := range config .GetEnvs () {
171- env = append (env , e .GetKey ()+ "=" + e .GetValue ())
172- }
173- specOpts = append (specOpts , oci .WithEnv (env ))
128+ specOpts := []oci.SpecOpts {}
174129
175130 securityContext := config .GetLinux ().GetSecurityContext ()
176131 labelOptions , err := toLabel (securityContext .GetSelinuxOptions ())
@@ -197,61 +152,13 @@ func (c *criService) platformSpec(
197152 }
198153 }()
199154
200- specOpts = append (specOpts , customopts .WithMounts (c .os , config , extraMounts , mountLabel ))
201-
202- if ! c .config .DisableProcMount {
203- // Change the default masked/readonly paths to empty slices
204- // See https://github.com/containerd/containerd/issues/5029
205- // TODO: Provide an option to set default paths to the ones in oci.populateDefaultUnixSpec()
206- specOpts = append (specOpts , oci .WithMaskedPaths ([]string {}), oci .WithReadonlyPaths ([]string {}))
207-
208- // Apply masked paths if specified.
209- // If the container is privileged, this will be cleared later on.
210- if maskedPaths := securityContext .GetMaskedPaths (); maskedPaths != nil {
211- specOpts = append (specOpts , oci .WithMaskedPaths (maskedPaths ))
212- }
213-
214- // Apply readonly paths if specified.
215- // If the container is privileged, this will be cleared later on.
216- if readonlyPaths := securityContext .GetReadonlyPaths (); readonlyPaths != nil {
217- specOpts = append (specOpts , oci .WithReadonlyPaths (readonlyPaths ))
218- }
219- }
220-
221- specOpts = append (specOpts , customopts .WithDevices (c .os , config , c .config .DeviceOwnershipFromSecurityContext ),
222- customopts .WithCapabilities (securityContext , c .allCaps ))
223-
224- if securityContext .GetPrivileged () {
225- if ! sandboxConfig .GetLinux ().GetSecurityContext ().GetPrivileged () {
226- return nil , errors .New ("no privileged container allowed in sandbox" )
227- }
228- specOpts = append (specOpts , oci .WithPrivileged )
229- if ! ociRuntime .PrivilegedWithoutHostDevices {
230- specOpts = append (specOpts , oci .WithHostDevices , oci .WithAllDevicesAllowed )
231- } else if ociRuntime .PrivilegedWithoutHostDevicesAllDevicesAllowed {
232- // allow rwm on all devices for the container
233- specOpts = append (specOpts , oci .WithAllDevicesAllowed )
234- }
235- }
236-
237- // Clear all ambient capabilities. The implication of non-root + caps
238- // is not clearly defined in Kubernetes.
239- // See https://github.com/kubernetes/kubernetes/issues/56374
240- // Keep docker's behavior for now.
241155 specOpts = append (specOpts ,
242- customopts .WithoutAmbientCaps ,
243156 customopts .WithSelinuxLabels (processLabel , mountLabel ),
157+ customopts .WithMounts (c .os , config , extraMounts , mountLabel ),
158+ customopts .WithDevices (c .os , config , c .config .DeviceOwnershipFromSecurityContext ),
159+ customopts .WithCapabilities (securityContext , c .allCaps ),
244160 )
245161
246- // TODO: Figure out whether we should set no new privilege for sandbox container by default
247- if securityContext .GetNoNewPrivs () {
248- specOpts = append (specOpts , oci .WithNoNewPrivileges )
249- }
250- // TODO(random-liu): [P1] Set selinux options (privileged or not).
251- if securityContext .GetReadonlyRootfs () {
252- specOpts = append (specOpts , oci .WithRootFSReadonly ())
253- }
254-
255162 if c .config .DisableCgroup {
256163 specOpts = append (specOpts , customopts .WithDisabledCgroups )
257164 } else {
@@ -262,8 +169,6 @@ func (c *criService) platformSpec(
262169 }
263170 }
264171
265- supplementalGroups := securityContext .GetSupplementalGroups ()
266-
267172 // Get blockio class
268173 blockIOClass , err := c .blockIOClassFromAnnotations (config .GetMetadata ().GetName (), config .Annotations , sandboxConfig .Annotations )
269174 if err != nil {
@@ -286,53 +191,14 @@ func (c *criService) platformSpec(
286191 specOpts = append (specOpts , oci .WithRdt (rdtClass , "" , "" ))
287192 }
288193
289- for pKey , pValue := range getPassthroughAnnotations (sandboxConfig .Annotations ,
290- ociRuntime .PodAnnotations ) {
291- specOpts = append (specOpts , customopts .WithAnnotation (pKey , pValue ))
292- }
293-
294- for pKey , pValue := range getPassthroughAnnotations (config .Annotations ,
295- ociRuntime .ContainerAnnotations ) {
296- specOpts = append (specOpts , customopts .WithAnnotation (pKey , pValue ))
297- }
298-
299- // Default target PID namespace is the sandbox PID.
300- targetPid := sandboxPid
301- // If the container targets another container's PID namespace,
302- // set targetPid to the PID of that container.
303- nsOpts := securityContext .GetNamespaceOptions ()
304- if nsOpts .GetPid () == runtime .NamespaceMode_TARGET {
305- targetContainer , err := c .validateTargetContainer (sandboxID , nsOpts .TargetId )
306- if err != nil {
307- return nil , fmt .Errorf ("invalid target container: %w" , err )
308- }
309-
310- status := targetContainer .Status .Get ()
311- targetPid = status .Pid
312- }
194+ specOpts = append (specOpts , customopts .WithOOMScoreAdj (config , c .config .RestrictOOMScoreAdj ))
313195
314- specOpts = append (specOpts ,
315- customopts .WithOOMScoreAdj (config , c .config .RestrictOOMScoreAdj ),
316- // TODO: This is a hack to make this compile. We should move userns support to sbserver.
317- customopts .WithPodNamespaces (securityContext , sandboxPid , targetPid , nil , nil ),
318- customopts .WithSupplementalGroups (supplementalGroups ),
319- customopts .WithAnnotation (annotations .ContainerType , annotations .ContainerTypeContainer ),
320- customopts .WithAnnotation (annotations .SandboxID , sandboxID ),
321- customopts .WithAnnotation (annotations .SandboxNamespace , sandboxConfig .GetMetadata ().GetNamespace ()),
322- customopts .WithAnnotation (annotations .SandboxUID , sandboxConfig .GetMetadata ().GetUid ()),
323- customopts .WithAnnotation (annotations .SandboxName , sandboxConfig .GetMetadata ().GetName ()),
324- customopts .WithAnnotation (annotations .ContainerName , containerName ),
325- customopts .WithAnnotation (annotations .ImageName , imageName ),
326- )
327196 // cgroupns is used for hiding /sys/fs/cgroup from containers.
328197 // For compatibility, cgroupns is not used when running in cgroup v1 mode or in privileged.
329198 // https://github.com/containers/libpod/issues/4363
330199 // https://github.com/kubernetes/enhancements/blob/0e409b47497e398b369c281074485c8de129694f/keps/sig-node/20191118-cgroups-v2.md#cgroup-namespace
331200 if cgroups .Mode () == cgroups .Unified && ! securityContext .GetPrivileged () {
332- specOpts = append (specOpts , oci .WithLinuxNamespace (
333- runtimespec.LinuxNamespace {
334- Type : runtimespec .CgroupNamespace ,
335- }))
201+ specOpts = append (specOpts , oci .WithLinuxNamespace (runtimespec.LinuxNamespace {Type : runtimespec .CgroupNamespace }))
336202 }
337203
338204 return specOpts , nil
0 commit comments