Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,6 @@ func CreateInit(c *cobra.Command, vals entities.ContainerCreateOptions, isInfra
if c.Flag("cgroups").Changed && vals.CgroupsMode == "split" && registry.IsRemote() {
return vals, fmt.Errorf("the option --cgroups=%q is not supported in remote mode", vals.CgroupsMode)
}

if c.Flag("pod").Changed && !strings.HasPrefix(c.Flag("pod").Value.String(), "new:") && c.Flag("userns").Changed {
return vals, errors.New("--userns and --pod cannot be set together")
}
}
if c.Flag("shm-size").Changed {
vals.ShmSize = c.Flag("shm-size").Value.String()
Expand Down
29 changes: 26 additions & 3 deletions pkg/specgen/generate/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ import (

const host = "host"

// userNSConflictsWithPod returns an error if the user namespace mode
// conflicts with pod namespace sharing requirements.
// Containers in a pod must use the same user namespace to avoid ownership and
// capability issues with shared resources.
func userNSConflictsWithPod(pod *libpod.Pod, mode specgen.NamespaceMode) error {
if pod != nil && pod.HasInfraContainer() {
// Allow modes that don't create a new user namespace
switch mode {
case specgen.FromPod, specgen.Default, specgen.Host, specgen.FromContainer:
return nil
default:
return fmt.Errorf("cannot set user namespace mode when joining pod with infra container: %w", define.ErrInvalidArg)
}
}
return nil
}

// Get the default namespace mode for any given namespace type.
func GetDefaultNamespaceMode(nsType string, cfg *config.Config, pod *libpod.Pod) (specgen.Namespace, error) {
// The default for most is private
Expand Down Expand Up @@ -211,7 +228,11 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
}
}

// User
// Validate that user namespace mode is compatible with pod.
if err := userNSConflictsWithPod(pod, s.UserNS.NSMode); err != nil {
return nil, err
}

switch s.UserNS.NSMode {
case specgen.KeepID:
opts, err := namespaces.UsernsMode(s.UserNS.String()).GetKeepIDOptions()
Expand Down Expand Up @@ -247,6 +268,10 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
return nil, fmt.Errorf("looking up container to share user namespace with: %w", err)
}
toReturn = append(toReturn, libpod.WithUserNSFrom(userCtr))
case specgen.Private:
case specgen.Auto:
case specgen.NoMap:
case specgen.Path:
}

// This wipes the UserNS settings that get set from the infra container
Expand All @@ -255,8 +280,6 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
if s.IDMappings != nil {
if pod == nil {
toReturn = append(toReturn, libpod.WithIDMappings(*s.IDMappings))
} else if pod.HasInfraContainer() && (len(s.IDMappings.UIDMap) > 0 || len(s.IDMappings.GIDMap) > 0) {
return nil, fmt.Errorf("cannot specify a new uid/gid map when entering a pod with an infra container: %w", define.ErrInvalidArg)
}
}
if s.User != "" {
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,14 @@ var _ = Describe("Podman create", func() {
create := podmanTest.Podman([]string{"create", "--uidmap", "0:1000:1000", "--pod", "new:testing123", ALPINE})
create.WaitWithDefaultTimeout()
Expect(create).ShouldNot(ExitCleanly())
Expect(create.ErrorToString()).To(ContainSubstring("cannot specify a new uid/gid map when entering a pod with an infra container"))
Expect(create.ErrorToString()).To(ContainSubstring("cannot set user namespace mode when joining pod with infra container"))

podmanTest.PodmanExitCleanly("pod", "rm", "-f", "testing123")

create = podmanTest.Podman([]string{"create", "--gidmap", "0:1000:1000", "--pod", "new:testing1234", ALPINE})
create.WaitWithDefaultTimeout()
Expect(create).ShouldNot(ExitCleanly())
Expect(create.ErrorToString()).To(ContainSubstring("cannot specify a new uid/gid map when entering a pod with an infra container"))

Expect(create.ErrorToString()).To(ContainSubstring("cannot set user namespace mode when joining pod with infra container"))
})

It("podman create --chrootdirs inspection test", func() {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/pod_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ ENTRYPOINT ["sleep","99999"]
// fail if --pod and --userns set together
session = podmanTest.Podman([]string{"run", "--pod", podName, "--userns", "keep-id", ALPINE, "id", "-u"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitWithError(125, "--userns and --pod cannot be set together"))
Expect(session).Should(ExitWithError(125, "cannot set user namespace mode when joining pod with infra container"))
})

It("podman pod create with --userns=keep-id can add users", func() {
Expand Down
9 changes: 8 additions & 1 deletion test/system/620-option-conflicts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ load helpers
create,run | --cpu-period=1 | --cpus=2 | $IMAGE
create,run | --cpu-quota=1 | --cpus=2 | $IMAGE
create,run | --no-hosts | --add-host=foo:1.1.1.1 | $IMAGE
create,run | --userns=bar | --pod=foo | $IMAGE
container cleanup | --all | --exec=foo
container cleanup | --exec=foo | --rmi | foo
"
Expand Down Expand Up @@ -48,6 +47,14 @@ container cleanup | --exec=foo | --rmi | foo
"podman $cmd --platform + --$opt"
done
done

# --userns and --pod have a different error message format
podname=p-$(safename)
run_podman pod create --name $podname
run_podman 125 run --uidmap=0:1000:1000 --pod=$podname $IMAGE true
is "$output" "Error: cannot set user namespace mode when joining pod with infra container: invalid argument" \
"podman run --uidmap + --pod"
run_podman pod rm -f $podname
}


Expand Down