Skip to content

Commit f99010d

Browse files
lifubangcyphar
authored andcommitted
integration: add some tests for bind mount through dangling symlinks
We intentionally broke this in commit d40b343 ("rootfs: switch to fd-based handling of mountpoint targets") under the assumption that most users do not need this feature. Sadly it turns out they do, and so commit 3f92552 ("rootfs: re-allow dangling symlinks in mount targets") added a hotfix to re-add this functionality. This patch adds some much-needed tests for this behaviour, since it seems we are going to need to keep this for compatibility reasons (at least until runc v2...). Co-developed-by: lifubang <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 33d4af2 commit f99010d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/integration/mounts.bats

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,42 @@ function test_mount_order() {
127127
[[ "$output" == *"a/x"* ]] # the final "file" was from a/x.
128128
}
129129

130+
# This needs to be placed at the top of the bats file to work around
131+
# a shellcheck bug. See <https://github.com/koalaman/shellcheck/issues/2873>.
132+
test_mount_target() {
133+
src="$1"
134+
dst="$2"
135+
real_dst="${3:-$dst}"
136+
137+
echo "== $src -> $dst (=> $real_dst) =="
138+
139+
old_config="$(mktemp ./config.json.bak.XXXXXX)"
140+
cp ./config.json "$old_config"
141+
142+
update_config '.mounts += [{
143+
source: "'"$src"'",
144+
destination: "'"$dst"'",
145+
options: ["bind"]
146+
}]'
147+
148+
# Make sure the target path is at the right spot and is actually a
149+
# bind-mount of the correct inode.
150+
update_config '.process.args = ["stat", "-c", "%n %d:%i", "--", "'"$real_dst"'"]'
151+
runc run test_busybox
152+
[ "$status" -eq 0 ]
153+
[[ "$output" == "$real_dst $(stat -c "%d:%i" -- "$src")" ]]
154+
155+
# Make sure there is a mount entry for the target path.
156+
# shellcheck disable=SC2016
157+
update_config '.process.args = ["awk", "-F", "PATH='"$real_dst"'", "$2 == PATH", "/proc/self/mounts"]'
158+
runc run test_busybox
159+
[ "$status" -eq 0 ]
160+
[[ "$output" == *"$real_dst"* ]]
161+
162+
# Switch back the old config so this function can be called multiple times.
163+
mv "$old_config" "./config.json"
164+
}
165+
130166
# https://github.com/opencontainers/runc/issues/3991
131167
@test "runc run [tmpcopyup]" {
132168
mkdir -p rootfs/dir1/dir2
@@ -343,3 +379,25 @@ function test_mount_order() {
343379
@test "runc run [mount order, container idmap source] (userns)" {
344380
test_mount_order userns,idmap
345381
}
382+
383+
@test "runc run [bind mount through a dangling symlink component]" {
384+
rm -rf rootfs/etc/hosts
385+
ln -s /tmp/foo/bar rootfs/jump
386+
ln -s /jump/baz/hosts rootfs/etc/hosts
387+
388+
rm -rf rootfs/tmp/foo
389+
test_mount_target ./config.json /etc/hosts /tmp/foo/bar/baz/hosts
390+
}
391+
392+
@test "runc run [bind mount through a trailing dangling symlink]" {
393+
rm -rf rootfs/etc/hosts
394+
ln -s /tmp/hosts rootfs/etc/hosts
395+
396+
# File.
397+
rm -rf rootfs/tmp/hosts
398+
test_mount_target ./config.json /etc/hosts /tmp/hosts
399+
400+
# Directory.
401+
rm -rf rootfs/tmp/hosts
402+
test_mount_target . /etc/hosts /tmp/hosts
403+
}

0 commit comments

Comments
 (0)