Skip to content

Commit 181f6ca

Browse files
committed
Fix user namespace validation for containers in pods
Remove incomplete CLI validation that only checked --pod flag and missed --pod-id-file (used by quadlet). Move validation to libpod/container_validate.go to catch all cases where --userns is set with --pod. The new validation checks if container's ID mappings differ from the pod's infra container and returns a clearer error message: 'cannot set user namespace mappings that differ from pod' This addresses the issue request for a better error message that explains the kernel limitation more clearly. Fixes: #26848 Signed-off-by: 0xdvc <[email protected]>
1 parent 69b397a commit 181f6ca

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

cmd/podman/containers/create.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,8 @@ func CreateInit(c *cobra.Command, vals entities.ContainerCreateOptions, isInfra
309309
return vals, fmt.Errorf("the option --cgroups=%q is not supported in remote mode", vals.CgroupsMode)
310310
}
311311

312-
if c.Flag("pod").Changed && !strings.HasPrefix(c.Flag("pod").Value.String(), "new:") && c.Flag("userns").Changed {
313-
return vals, errors.New("--userns and --pod cannot be set together")
314-
}
312+
// Validation for --userns with --pod moved to libpod/container_validate.go
313+
// to catch all cases (--pod, --pod-id-file used by quadlet, etc.)
315314
}
316315
if c.Flag("shm-size").Changed {
317316
vals.ShmSize = c.Flag("shm-size").Value.String()

pkg/specgen/generate/namespaces.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ import (
2121

2222
const host = "host"
2323

24+
// userNSConflictsWithPod returns an error if the user namespace mode
25+
// conflicts with pod namespace sharing requirements.
26+
// Linux requires containers sharing network or IPC namespaces (like in a pod)
27+
// to use the same user namespace. Trying to use different ones will fail at the kernel level.
28+
func userNSConflictsWithPod(pod *libpod.Pod, mode specgen.NamespaceMode) error {
29+
if pod != nil && pod.HasInfraContainer() && (pod.SharesIPC() || pod.SharesNet()) {
30+
// FromPod mode is allowed - container inherits from pod
31+
if mode != specgen.FromPod {
32+
return fmt.Errorf("cannot set user namespace mappings that differ from pod: %w", define.ErrInvalidArg)
33+
}
34+
}
35+
return nil
36+
}
37+
2438
// Get the default namespace mode for any given namespace type.
2539
func GetDefaultNamespaceMode(nsType string, cfg *config.Config, pod *libpod.Pod) (specgen.Namespace, error) {
2640
// The default for most is private
@@ -214,6 +228,9 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
214228
// User
215229
switch s.UserNS.NSMode {
216230
case specgen.KeepID:
231+
if err := userNSConflictsWithPod(pod, s.UserNS.NSMode); err != nil {
232+
return nil, err
233+
}
217234
opts, err := namespaces.UsernsMode(s.UserNS.String()).GetKeepIDOptions()
218235
if err != nil {
219236
return nil, err
@@ -247,16 +264,32 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
247264
return nil, fmt.Errorf("looking up container to share user namespace with: %w", err)
248265
}
249266
toReturn = append(toReturn, libpod.WithUserNSFrom(userCtr))
267+
case specgen.Private:
268+
if err := userNSConflictsWithPod(pod, s.UserNS.NSMode); err != nil {
269+
return nil, err
270+
}
271+
case specgen.Auto:
272+
if err := userNSConflictsWithPod(pod, s.UserNS.NSMode); err != nil {
273+
return nil, err
274+
}
275+
case specgen.NoMap:
276+
if err := userNSConflictsWithPod(pod, s.UserNS.NSMode); err != nil {
277+
return nil, err
278+
}
279+
case specgen.Path:
280+
if err := userNSConflictsWithPod(pod, s.UserNS.NSMode); err != nil {
281+
return nil, err
282+
}
250283
}
251284

252285
// This wipes the UserNS settings that get set from the infra container
253286
// when we are inheriting from the pod. So only apply this if the container
254287
// is not being created in a pod.
255-
if s.IDMappings != nil {
288+
if s.IDMappings != nil && (len(s.IDMappings.UIDMap) > 0 || len(s.IDMappings.GIDMap) > 0) {
256289
if pod == nil {
257290
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)
291+
} else if pod.HasInfraContainer() && (pod.SharesIPC() || pod.SharesNet()) {
292+
return nil, fmt.Errorf("cannot set user namespace mappings that differ from pod: %w", define.ErrInvalidArg)
260293
}
261294
}
262295
if s.User != "" {

test/e2e/create_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,18 @@ var _ = Describe("Podman create", func() {
681681
create := podmanTest.Podman([]string{"create", "--uidmap", "0:1000:1000", "--pod", "new:testing123", ALPINE})
682682
create.WaitWithDefaultTimeout()
683683
Expect(create).ShouldNot(ExitCleanly())
684-
Expect(create.ErrorToString()).To(ContainSubstring("cannot specify a new uid/gid map when entering a pod with an infra container"))
684+
Expect(create.ErrorToString()).To(ContainSubstring("cannot set user namespace mappings that differ from pod"))
685+
686+
cleanup := podmanTest.Podman([]string{"pod", "rm", "-f", "testing123"})
687+
cleanup.WaitWithDefaultTimeout()
685688

686689
create = podmanTest.Podman([]string{"create", "--gidmap", "0:1000:1000", "--pod", "new:testing1234", ALPINE})
687690
create.WaitWithDefaultTimeout()
688691
Expect(create).ShouldNot(ExitCleanly())
689-
Expect(create.ErrorToString()).To(ContainSubstring("cannot specify a new uid/gid map when entering a pod with an infra container"))
692+
Expect(create.ErrorToString()).To(ContainSubstring("cannot set user namespace mappings that differ from pod"))
690693

694+
cleanup = podmanTest.Podman([]string{"pod", "rm", "-f", "testing1234"})
695+
cleanup.WaitWithDefaultTimeout()
691696
})
692697

693698
It("podman create --chrootdirs inspection test", func() {

test/e2e/pod_create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ ENTRYPOINT ["sleep","99999"]
804804
// fail if --pod and --userns set together
805805
session = podmanTest.Podman([]string{"run", "--pod", podName, "--userns", "keep-id", ALPINE, "id", "-u"})
806806
session.WaitWithDefaultTimeout()
807-
Expect(session).Should(ExitWithError(125, "--userns and --pod cannot be set together"))
807+
Expect(session).Should(ExitWithError(125, "cannot set user namespace mappings that differ from pod"))
808808
})
809809

810810
It("podman pod create with --userns=keep-id can add users", func() {

test/system/620-option-conflicts.bats

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ load helpers
1414
create,run | --cpu-period=1 | --cpus=2 | $IMAGE
1515
create,run | --cpu-quota=1 | --cpus=2 | $IMAGE
1616
create,run | --no-hosts | --add-host=foo:1.1.1.1 | $IMAGE
17-
create,run | --userns=bar | --pod=foo | $IMAGE
1817
container cleanup | --all | --exec=foo
1918
container cleanup | --exec=foo | --rmi | foo
2019
"
@@ -48,6 +47,13 @@ container cleanup | --exec=foo | --rmi | foo
4847
"podman $cmd --platform + --$opt"
4948
done
5049
done
50+
51+
# --userns and --pod have a different error message format
52+
run_podman pod create --name userns-pod-test
53+
run_podman 125 run --uidmap=0:1000:1000 --pod=userns-pod-test $IMAGE true
54+
is "$output" "Error: cannot set user namespace mappings that differ from pod: invalid argument" \
55+
"podman run --uidmap + --pod"
56+
run_podman pod rm -f userns-pod-test
5157
}
5258

5359

0 commit comments

Comments
 (0)