@@ -21,6 +21,23 @@ import (
2121
2222const host = "host"
2323
24+ // userNSConflictsWithPod returns an error if the user namespace mode
25+ // conflicts with pod namespace sharing requirements.
26+ // Containers in a pod must use the same user namespace to avoid ownership and
27+ // capability issues with shared resources.
28+ func userNSConflictsWithPod (pod * libpod.Pod , mode specgen.NamespaceMode ) error {
29+ if pod != nil && pod .HasInfraContainer () {
30+ // Allow modes that don't create a new user namespace
31+ switch mode {
32+ case specgen .FromPod , specgen .Default , specgen .Host , specgen .FromContainer :
33+ return nil
34+ default :
35+ return fmt .Errorf ("cannot set user namespace mode when joining pod with infra container: %w" , define .ErrInvalidArg )
36+ }
37+ }
38+ return nil
39+ }
40+
2441// Get the default namespace mode for any given namespace type.
2542func GetDefaultNamespaceMode (nsType string , cfg * config.Config , pod * libpod.Pod ) (specgen.Namespace , error ) {
2643 // The default for most is private
@@ -211,7 +228,11 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
211228 }
212229 }
213230
214- // User
231+ // Validate that user namespace mode is compatible with pod.
232+ if err := userNSConflictsWithPod (pod , s .UserNS .NSMode ); err != nil {
233+ return nil , err
234+ }
235+
215236 switch s .UserNS .NSMode {
216237 case specgen .KeepID :
217238 opts , err := namespaces .UsernsMode (s .UserNS .String ()).GetKeepIDOptions ()
@@ -247,6 +268,10 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
247268 return nil , fmt .Errorf ("looking up container to share user namespace with: %w" , err )
248269 }
249270 toReturn = append (toReturn , libpod .WithUserNSFrom (userCtr ))
271+ case specgen .Private :
272+ case specgen .Auto :
273+ case specgen .NoMap :
274+ case specgen .Path :
250275 }
251276
252277 // This wipes the UserNS settings that get set from the infra container
@@ -255,8 +280,6 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
255280 if s .IDMappings != nil {
256281 if pod == nil {
257282 toReturn = append (toReturn , libpod .WithIDMappings (* s .IDMappings ))
258- } else if pod .HasInfraContainer () && (len (s .IDMappings .UIDMap ) > 0 || len (s .IDMappings .GIDMap ) > 0 ) {
259- return nil , fmt .Errorf ("cannot specify a new uid/gid map when entering a pod with an infra container: %w" , define .ErrInvalidArg )
260283 }
261284 }
262285 if s .User != "" {
0 commit comments