Skip to content

Commit c9c44d4

Browse files
committed
libpod: do not cover idmapped mountpoint
Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent faf8574 commit c9c44d4

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

Diff for: libpod/oci_conmon_linux.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ func (r *ConmonOCIRuntime) createRootlessContainer(ctr *Container, restoreOption
8989
}
9090
}
9191

92-
// do not propagate the bind mount on the parent mount namespace
93-
if err := unix.Mount("", parentMount, "", unix.MS_SLAVE, ""); err != nil {
94-
return 0, fmt.Errorf("failed to make %s slave: %w", parentMount, err)
95-
}
96-
9792
// bind mount the containers' mount path to the path where the OCI runtime expects it to be
98-
if err := unix.Mount(ctr.state.Mountpoint, rootPath, "", unix.MS_BIND, ""); err != nil {
99-
return 0, fmt.Errorf("failed to bind mount %s to %s: %w", ctr.state.Mountpoint, rootPath, err)
93+
// if the container is already mounted at the expected path, do not cover the mountpoint.
94+
if filepath.Clean(ctr.state.Mountpoint) != filepath.Clean(rootPath) {
95+
// do not propagate the bind mount on the parent mount namespace
96+
if err := unix.Mount("", parentMount, "", unix.MS_SLAVE, ""); err != nil {
97+
return 0, fmt.Errorf("failed to make %s slave: %w", parentMount, err)
98+
}
99+
if err := unix.Mount(ctr.state.Mountpoint, rootPath, "", unix.MS_BIND, ""); err != nil {
100+
return 0, fmt.Errorf("failed to bind mount %s to %s: %w", ctr.state.Mountpoint, rootPath, err)
101+
}
100102
}
101103

102104
if isShared {

Diff for: test/system/030-run.bats

+8-1
Original file line numberDiff line numberDiff line change
@@ -1419,17 +1419,23 @@ EOF
14191419
# Any other error is fatal
14201420
die "Cannot create idmap mount: $output"
14211421
fi
1422+
ensure_no_mountpoint "$romount"
14221423

1423-
run_podman run --security-opt label=disable --rm --uidmap=0:1000:10000 --rootfs $romount:idmap stat -c %u:%g /bin
1424+
mkdir -p $PODMAN_TMPDIR/shared-volume
1425+
# test that there are no mount leaks also when a shared volume is used (with a shared volume the rootfs propagation is set to shared).
1426+
run_podman run --security-opt label=disable --rm --uidmap=0:1000:10000 -v $PODMAN_TMPDIR/shared-volume:/a-shared-volume:shared --rootfs $romount:idmap stat -c %u:%g /bin
14241427
is "$output" "0:0"
1428+
ensure_no_mountpoint "$romount"
14251429

14261430
run_podman run --security-opt label=disable --uidmap=0:1000:10000 --rm --rootfs "$romount:idmap=uids=0-1001-10000;gids=0-1002-10000" stat -c %u:%g /bin
14271431
is "$output" "1:2"
1432+
ensure_no_mountpoint "$romount"
14281433

14291434
touch $romount/testfile
14301435
chown 2000:2000 $romount/testfile
14311436
run_podman run --security-opt label=disable --uidmap=0:1000:200 --rm --rootfs "$romount:idmap=uids=@2000-1-1;gids=@2000-1-1" stat -c %u:%g /testfile
14321437
is "$output" "1:1"
1438+
ensure_no_mountpoint "$romount"
14331439

14341440
# verify that copyup with an empty idmap volume maintains the original ownership with different mappings and --rootfs
14351441
myvolume=my-volume-$(safename)
@@ -1439,6 +1445,7 @@ EOF
14391445
for FROM in 1000 2000; do
14401446
run_podman run --security-opt label=disable --rm --uidmap=0:$FROM:10000 -v $myvolume:/volume:idmap --rootfs $romount stat -c %u:%g /volume
14411447
is "$output" "0:0"
1448+
ensure_no_mountpoint "$romount"
14421449
done
14431450
run_podman volume rm $myvolume
14441451

Diff for: test/system/060-mount.bats

+1-3
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,7 @@ EOF
309309

310310
# umount, and make sure mountpoint no longer exists
311311
run_podman umount $external_cname
312-
if findmnt "$mount_path" >/dev/null ; then
313-
die "'podman umount' did not umount $mount_path"
314-
fi
312+
ensure_no_mountpoint "$mount_path"
315313
buildah rm $external_cname
316314
}
317315

Diff for: test/system/helpers.bash

+11
Original file line numberDiff line numberDiff line change
@@ -1361,5 +1361,16 @@ function make_random_file() {
13611361
dd if=/dev/urandom of="$1" bs=1 count=${2:-$((${RANDOM} % 8192 + 1024))} status=none
13621362
}
13631363

1364+
###########################
1365+
# ensure there is no mount point at the specified path
1366+
###########################
1367+
function ensure_no_mountpoint() {
1368+
local path="$1"
1369+
if findmnt "$path"; then
1370+
die "there is a mountpoint at $path"
1371+
fi
1372+
}
1373+
1374+
13641375
# END miscellaneous tools
13651376
###############################################################################

0 commit comments

Comments
 (0)