Skip to content

Commit

Permalink
Fix panic in container create compat api
Browse files Browse the repository at this point in the history
The bind and tmpfs options can be nil, we have to check that before we
try to use it.

Fixes containers#11961

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Oct 14, 2021
1 parent 1c156f2 commit 8cae297
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 8 additions & 5 deletions cmd/podman/common/create_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,18 @@ func ContainerCreateToContainerCLIOpts(cc handlers.CreateContainerConfig, rtc *c
addField(&builder, "target", m.Target)
addField(&builder, "ro", strconv.FormatBool(m.ReadOnly))
addField(&builder, "consistency", string(m.Consistency))

// Map any specialized mount options that intersect between *Options and cli options
switch m.Type {
case mount.TypeBind:
addField(&builder, "bind-propagation", string(m.BindOptions.Propagation))
addField(&builder, "bind-nonrecursive", strconv.FormatBool(m.BindOptions.NonRecursive))
if m.BindOptions != nil {
addField(&builder, "bind-propagation", string(m.BindOptions.Propagation))
addField(&builder, "bind-nonrecursive", strconv.FormatBool(m.BindOptions.NonRecursive))
}
case mount.TypeTmpfs:
addField(&builder, "tmpfs-size", strconv.FormatInt(m.TmpfsOptions.SizeBytes, 10))
addField(&builder, "tmpfs-mode", strconv.FormatUint(uint64(m.TmpfsOptions.Mode), 10))
if m.TmpfsOptions != nil {
addField(&builder, "tmpfs-size", strconv.FormatInt(m.TmpfsOptions.SizeBytes, 10))
addField(&builder, "tmpfs-mode", strconv.FormatUint(uint64(m.TmpfsOptions.Mode), 10))
}
case mount.TypeVolume:
// All current VolumeOpts are handled above
// See vendor/github.com/containers/common/pkg/parse/parse.go:ValidateVolumeOpts()
Expand Down
18 changes: 18 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,21 @@ t GET containers/$cid/json 200 \
.HostConfig.Tmpfs['"/mnt/scratch"']~.*mode=755.*

t DELETE containers/$cid?v=true 204

# compat api: tmpfs without mount options
payload='{"Mounts":[{"Type":"tmpfs","Target":"/mnt/scratch"}]}'
t POST containers/create Image=$IMAGE HostConfig="$payload" 201 .Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")
t GET containers/$cid/json 200 \
.HostConfig.Tmpfs['"/mnt/scratch"']~.*tmpcopyup.* \

t DELETE containers/$cid?v=true 204

# compat api: bind mount without mount options
payload='{"Mounts":[{"Type":"bind","Source":"/tmp","Target":"/mnt"}]}'
t POST containers/create Image=$IMAGE HostConfig="$payload" 201 .Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")
t GET containers/$cid/json 200 \
.HostConfig.Binds[0]~/tmp:/mnt:.* \

t DELETE containers/$cid?v=true 204

0 comments on commit 8cae297

Please sign in to comment.