Skip to content

Commit d4c557b

Browse files
committed
return usernsmode=private for created containers with userns=auto
Signed-off-by: Riccardo Paolo Bestetti <pbl@bestov.io>
1 parent 6e73fc1 commit d4c557b

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

libpod/container_inspect_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC
292292
}
293293
}
294294
}
295+
296+
// If userns=auto, setting up the namespace is deferred until the container
297+
// is created. If the container is configured, check if it is going to have a
298+
// private userns and return accordingly
299+
if c.state.State == define.ContainerStateConfigured && c.config.IDMappings.AutoUserNs {
300+
usernsMode = "private"
301+
}
295302
}
296303
hostConfig.UsernsMode = usernsMode
297304
if c.config.IDMappings.UIDMap != nil && c.config.IDMappings.GIDMap != nil {

libpod/container_internal_common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
683683
}
684684
}
685685

686-
// Add shared namespaces from other containers
686+
// Add shared namespaces from other containers. Also handles userns=auto
687687
if err := c.addSharedNamespaces(&g); err != nil {
688688
return nil, nil, err
689689
}

test/apiv2/20-containers.at

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,24 @@ for runtime in "${oci_runtimes[@]}"; do
948948
t DELETE containers/$cid 204
949949
done
950950

951+
# 27998: make sure a created (and not started) container with userns=auto shows
952+
# UsernsMode = private before being started
953+
954+
t POST libpod/containers/create \
955+
image=$IMAGE \
956+
UserNS='{"NSMode":"auto"}' \
957+
IDMappings='{"AutoUserNs":true,"AutoUserNsOpts":{"AdditionalUIDMappings":[],"AdditionalGIDMappings":[],"PasswdFile":"","GroupFile":"","InitialSize":0,"Size":0}}' \
958+
201
959+
cid=$(jq -r '.Id' <<<"$output")
960+
961+
t GET libpod/containers/$cid/json \
962+
200 \
963+
.HostConfig.UsernsMode='private'
964+
965+
t DELETE libpod/containers/$cid 200 .[0].Id=$cid
966+
967+
968+
# clean up
951969
podman rmi -f $IMAGE
952970

953971
# Test health status in /containers/json (GH #27786)

0 commit comments

Comments
 (0)