From 137788a98a3c26825d9a709e80f90e8ae38ea40e Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Wed, 19 Apr 2017 23:52:26 +0100 Subject: [PATCH] Groot: Do not umount directories that are not mountpoint --- bin/groot | 2 +- tests/unit-tests/test-groot.sh | 46 +++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/bin/groot b/bin/groot index b2cc2ab..fd438b4 100755 --- a/bin/groot +++ b/bin/groot @@ -43,7 +43,7 @@ function chroot_teardown() { local final_res=0 for mp in $($CAT $MOUNTS_FILE | $CUT -f2 -d' ' | $SORT -r | $UNIQ) do - if [[ $mp =~ ^${normalized_chrootdir}.* ]] + if [[ $mp =~ ^${normalized_chrootdir}.* ]] && $MOUNTPOINT -q "$mp" then $UMOUNT $mp || final_res=$? fi diff --git a/tests/unit-tests/test-groot.sh b/tests/unit-tests/test-groot.sh index 784452d..ca4df91 100755 --- a/tests/unit-tests/test-groot.sh +++ b/tests/unit-tests/test-groot.sh @@ -41,6 +41,9 @@ function init_mocks() { # As default suppose the mountpoint does not exist return 1 } + function mountpoint_mock() { + echo "mountpoint($@)" + } function mount() { echo "mount($@)" } @@ -74,9 +77,6 @@ function test_groot_no_directory(){ assertCommandFailOnStatus $NOT_EXISTING_FILE main no-directory } function test_groot_mountpoint_exist(){ - mountpoint_mock() { - echo "mountpoint($@)" - } MOUNTPOINT=mountpoint_mock assertCommandSuccess main chrootdir assertEquals "$(echo -e "check_and_trap(chroot_teardown QUIT EXIT ABRT KILL TERM INT)\nmountpoint(-q chrootdir)\nchroot(chrootdir)")" "$(cat $STDOUTF)" @@ -138,17 +138,23 @@ function test_groot_with_bind_no_umount(){ assertEquals "$(echo -e "mountpoint(-q chrootdir)\nmount(--bind chrootdir chrootdir)\nchroot(chrootdir)")" "$(cat $STDOUTF)" } function test_groot_with_chroot_teardown(){ + MOUNTPOINT=mountpoint_mock echo -e "1 /home/mychroot/dev\n1 /home/mychroot/proc/fs1\n1 /home/mychroot\n1 /home/mychroot-no/dev\n1 /home/mychroot/dev/shm\n1 /home/mychroot/proc\n" > ./mounts MOUNTS_FILE=./mounts CHROOTDIR=/home/mychroot assertCommandSuccess chroot_teardown - assertEquals "$(echo -e "umount(/home/mychroot/proc/fs1) + assertEquals "$(echo -e "mountpoint(-q /home/mychroot/proc/fs1) +umount(/home/mychroot/proc/fs1) +mountpoint(-q /home/mychroot/proc) umount(/home/mychroot/proc) +mountpoint(-q /home/mychroot/dev/shm) umount(/home/mychroot/dev/shm) +mountpoint(-q /home/mychroot/dev) umount(/home/mychroot/dev) umount(/home/mychroot)")" "$(cat $STDOUTF)" } function test_groot_with_chroot_teardown_umount_failure(){ + MOUNTPOINT=mountpoint_mock function umount() { echo "umount($@)" [[ "$1" == "/home/mychroot/dev/shm" ]] && return 128 @@ -158,19 +164,47 @@ function test_groot_with_chroot_teardown_umount_failure(){ echo -e "1 /home/mychroot/dev\n1 /home/mychroot/proc/fs1\n1 /home/mychroot\n1 /home/mychroot-no/dev\n1 /home/mychroot/dev/shm\n1 /home/mychroot/proc\n" > ./mounts MOUNTS_FILE=./mounts CHROOTDIR=/home/mychroot assertCommandFailOnStatus 128 chroot_teardown - assertEquals "$(echo -e "umount(/home/mychroot/proc/fs1) + assertEquals "$(echo -e "mountpoint(-q /home/mychroot/proc/fs1) +umount(/home/mychroot/proc/fs1) +mountpoint(-q /home/mychroot/proc) umount(/home/mychroot/proc) +mountpoint(-q /home/mychroot/dev/shm) umount(/home/mychroot/dev/shm) +mountpoint(-q /home/mychroot/dev) umount(/home/mychroot/dev) umount(/home/mychroot)")" "$(cat $STDOUTF)" } function test_groot_with_chroot_teardown_with_trailing_slash(){ + MOUNTPOINT=mountpoint_mock echo -e "1 /home/mychroot/dev\n1 /home/mychroot/proc/fs1\n1 /home/mychroot\n1 /home/mychroot-no/dev\n1 /home/mychroot/dev/shm\n1 /home/mychroot/proc\n" > ./mounts MOUNTS_FILE=./mounts CHROOTDIR=/home/mychroot assertCommandSuccess chroot_teardown - assertEquals "$(echo -e "umount(/home/mychroot/proc/fs1) + assertEquals "$(echo -e "mountpoint(-q /home/mychroot/proc/fs1) +umount(/home/mychroot/proc/fs1) +mountpoint(-q /home/mychroot/proc) umount(/home/mychroot/proc) +mountpoint(-q /home/mychroot/dev/shm) umount(/home/mychroot/dev/shm) +mountpoint(-q /home/mychroot/dev) +umount(/home/mychroot/dev) +umount(/home/mychroot)")" "$(cat $STDOUTF)" +} +function test_groot_with_chroot_teardown_mountpoint_failure(){ + mountpoint_mock() { + echo "mountpoint($@)" + [[ $2 == "/home/mychroot/dev/shm" ]] && return 128 + return 0 + } + MOUNTPOINT=mountpoint_mock + echo -e "1 /home/mychroot/dev\n1 /home/mychroot/proc/fs1\n1 /home/mychroot\n1 /home/mychroot-no/dev\n1 /home/mychroot/dev/shm\n1 /home/mychroot/proc\n" > ./mounts + MOUNTS_FILE=./mounts + CHROOTDIR=/home/mychroot assertCommandSuccess chroot_teardown + assertEquals "$(echo -e "mountpoint(-q /home/mychroot/proc/fs1) +umount(/home/mychroot/proc/fs1) +mountpoint(-q /home/mychroot/proc) +umount(/home/mychroot/proc) +mountpoint(-q /home/mychroot/dev/shm) +mountpoint(-q /home/mychroot/dev) umount(/home/mychroot/dev) umount(/home/mychroot)")" "$(cat $STDOUTF)" }