Skip to content

Commit 5a7b73a

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 <neilohene@gmail.com>
1 parent 69b397a commit 5a7b73a

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
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()

libpod/container_validate.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ func (c *Container) validate() error {
187187
return fmt.Errorf("default rootfs-based infra container is set for non-infra container")
188188
}
189189

190+
// Validate user namespace configuration for containers in pods
191+
if err := c.validateUserNamespaceInPod(); err != nil {
192+
return err
193+
}
194+
190195
return nil
191196
}
192197

@@ -207,3 +212,51 @@ func validateAutoUpdateImageReference(imageName string) error {
207212
}
208213
return nil
209214
}
215+
216+
func (c *Container) validateUserNamespaceInPod() error {
217+
if c.config.Pod == "" {
218+
return nil
219+
}
220+
221+
if len(c.config.IDMappings.UIDMap) == 0 && len(c.config.IDMappings.GIDMap) == 0 {
222+
return nil
223+
}
224+
225+
pod, err := c.runtime.LookupPod(c.config.Pod)
226+
if err != nil {
227+
return fmt.Errorf("looking up pod for container: %w", err)
228+
}
229+
230+
if !pod.HasInfraContainer() {
231+
return nil
232+
}
233+
234+
infraContainer, err := pod.infraContainer()
235+
if err != nil {
236+
return fmt.Errorf("getting pod infra container: %w", err)
237+
}
238+
239+
podIDMappings := infraContainer.IDMappings()
240+
241+
// Check UID mappings
242+
if len(c.config.IDMappings.UIDMap) != len(podIDMappings.UIDMap) {
243+
return fmt.Errorf("cannot set user namespace mappings that differ from pod: %w", define.ErrInvalidArg)
244+
}
245+
for i := range c.config.IDMappings.UIDMap {
246+
if c.config.IDMappings.UIDMap[i] != podIDMappings.UIDMap[i] {
247+
return fmt.Errorf("cannot set user namespace mappings that differ from pod: %w", define.ErrInvalidArg)
248+
}
249+
}
250+
251+
// Check GID mappings
252+
if len(c.config.IDMappings.GIDMap) != len(podIDMappings.GIDMap) {
253+
return fmt.Errorf("cannot set user namespace mappings that differ from pod: %w", define.ErrInvalidArg)
254+
}
255+
for i := range c.config.IDMappings.GIDMap {
256+
if c.config.IDMappings.GIDMap[i] != podIDMappings.GIDMap[i] {
257+
return fmt.Errorf("cannot set user namespace mappings that differ from pod: %w", define.ErrInvalidArg)
258+
}
259+
}
260+
261+
return nil
262+
}

pkg/specgen/generate/namespaces.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,10 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
251251

252252
// This wipes the UserNS settings that get set from the infra container
253253
// when we are inheriting from the pod. So only apply this if the container
254-
// is not being created in a pod.
254+
// is not being created in a pod. Validation is handled in container_validate.go.
255255
if s.IDMappings != nil {
256256
if pod == nil {
257257
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)
260258
}
261259
}
262260
if s.User != "" {

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 --userns=keep-id --pod=userns-pod-test $IMAGE true
54+
is "$output" "Error: cannot set user namespace mappings that differ from pod: invalid argument" \
55+
"podman run --userns + --pod"
56+
run_podman pod rm -f userns-pod-test
5157
}
5258

5359

0 commit comments

Comments
 (0)