summaryrefslogtreecommitdiff
path: root/libpod/container_internal.go
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2021-10-29 10:28:38 +0200
committerMatthew Heon <matthew.heon@pm.me>2021-11-12 11:08:25 -0500
commitc8b7ca2ba2ef9a57103b5c05e6e29b54b0c6fa48 (patch)
tree2fd5d0ee15434453863127715e4c9d26cbdba010 /libpod/container_internal.go
parent2dc8db77307a050cab9d030149be34e636f04657 (diff)
downloadpodman-c8b7ca2ba2ef9a57103b5c05e6e29b54b0c6fa48.tar.gz
podman-c8b7ca2ba2ef9a57103b5c05e6e29b54b0c6fa48.tar.bz2
podman-c8b7ca2ba2ef9a57103b5c05e6e29b54b0c6fa48.zip
pod/container create: resolve conflicts of generated names
Address the TOCTOU when generating random names by having at most 10 attempts to assign a random name when creating a pod or container. [NO TESTS NEEDED] since I do not know a way to force a conflict with randomly generated names in a reasonable time frame. Fixes: #11735 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r--libpod/container_internal.go24
1 files changed, 21 insertions, 3 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index d71179017..54d6b1303 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -479,9 +479,27 @@ func (c *Container) setupStorage(ctx context.Context) error {
c.setupStorageMapping(&options.IDMappingOptions, &c.config.IDMappings)
- containerInfo, err := c.runtime.storageService.CreateContainerStorage(ctx, c.runtime.imageContext, c.config.RootfsImageName, c.config.RootfsImageID, c.config.Name, c.config.ID, options)
- if err != nil {
- return errors.Wrapf(err, "error creating container storage")
+ // Unless the user has specified a name, use a randomly generated one.
+ // Note that name conflicts may occur (see #11735), so we need to loop.
+ generateName := c.config.Name == ""
+ var containerInfo ContainerInfo
+ var containerInfoErr error
+ for {
+ if generateName {
+ name, err := c.runtime.generateName()
+ if err != nil {
+ return err
+ }
+ c.config.Name = name
+ }
+ containerInfo, containerInfoErr = c.runtime.storageService.CreateContainerStorage(ctx, c.runtime.imageContext, c.config.RootfsImageName, c.config.RootfsImageID, c.config.Name, c.config.ID, options)
+
+ if !generateName || errors.Cause(containerInfoErr) != storage.ErrDuplicateName {
+ break
+ }
+ }
+ if containerInfoErr != nil {
+ return errors.Wrapf(containerInfoErr, "error creating container storage")
}
c.config.IDMappings.UIDMap = containerInfo.UIDMap