diff --git a/xCAT-test/bats/helpers/doxcat_sandbox.bash b/xCAT-test/bats/helpers/doxcat_sandbox.bash index b909bad2ef..9a6123983c 100644 --- a/xCAT-test/bats/helpers/doxcat_sandbox.bash +++ b/xCAT-test/bats/helpers/doxcat_sandbox.bash @@ -4,7 +4,7 @@ load 'helpers/genesis_sandbox' setup_doxcat() { - setup_genesis_sandbox doxcat + setup_genesis_sandbox doxcat || return ib_address=80:00:00:48:fe:80:00:00:00:00:00:00:00:02:00:bb:cc:dd:ee:ff local utility fips library fips=$(repo_path xCAT-genesis-scripts/usr/lib/xcat/fips.sh) @@ -13,13 +13,8 @@ setup_doxcat() # chmod is in this list because doxcat runs it: the sandbox has no /usr/bin on PATH, so a # command that is missing here is a command that silently does nothing under test. doxcat's # chmod of the sshd privilege separation directory was not exercised until it was added. - for utility in bash sh cat cp awk grep sed sort tr tail rm wc cut head uname \ - timeout env ls mkdir touch readlink basename sleep chmod; do - cp -L "$(PATH=/usr/bin:/bin type -P "$utility")" "$fixture/bin/$utility" - done - if [ -x /usr/bin/coreutils ]; then - cp -L /usr/bin/coreutils "$fixture/bin/coreutils" - fi + stage_sandbox_tools bash sh cat cp awk grep sed sort tr tail rm wc cut head uname \ + timeout env ls mkdir touch readlink basename sleep chmod || return mv "$fixture/bin/sleep" "$fixture/real/sleep" printf '#!/bin/sh\nexit 0\n' >"$fixture/bin/quiet" for utility in sleep rpcbind rpc.statd ssh-keygen lldpad chronyd; do @@ -96,19 +91,10 @@ SH : >"$fixture/modules" : >"$fixture/dhcp" printf '0\n' >"$fixture/fips_enabled" - sandbox+=(--ro-bind "$fixture/bin" /usr/bin - --tmpfs /proc/sys + restrict_sandbox_tools + sandbox+=(--tmpfs /proc/sys --ro-bind "$fixture/fips_enabled" /proc/sys/crypto/fips_enabled --tmpfs /usr/lib) - if [ ! -L /usr/sbin ]; then - sandbox+=(--ro-bind "$fixture/bin" /usr/sbin) - fi - if [ ! -L /bin ]; then - sandbox+=(--ro-bind "$fixture/bin" /bin) - fi - if [ ! -L /sbin ]; then - sandbox+=(--ro-bind "$fixture/bin" /sbin) - fi for library in /usr/lib/*; do [ "${library##*/}" = xcat ] && continue sandbox+=(--ro-bind "$library" "$library") diff --git a/xCAT-test/bats/helpers/genesis_sandbox.bash b/xCAT-test/bats/helpers/genesis_sandbox.bash index e2e7769700..3536e839f7 100644 --- a/xCAT-test/bats/helpers/genesis_sandbox.bash +++ b/xCAT-test/bats/helpers/genesis_sandbox.bash @@ -1,15 +1,11 @@ #!/usr/bin/env bash -load 'helpers/shell_source' +load 'helpers/script_sandbox' setup_genesis_sandbox() { - [ "$(uname -s)" = Linux ] || skip 'Genesis filesystem isolation requires Linux' - local bwrap utility source - bwrap=$(PATH=/usr/bin:/bin type -P bwrap) || { - echo 'Install bubblewrap to run the Genesis script tests' >&2 - return 1 - } + setup_filesystem_sandbox || return + local utility source for utility in bash cat cp awk grep sed sort tr tail rm wc cut head uname timeout; do PATH=/usr/bin:/bin type -P "$utility" >/dev/null || { echo "Required utility is unavailable: $utility" >&2 @@ -21,15 +17,10 @@ setup_genesis_sandbox() echo "Required Genesis script is unreadable: $source" >&2 return 1 } - fixture="${BATS_TEST_TMPDIR:?bats-core 1.4 or newer is required}/fixture" - mkdir -p "$fixture"/{bin,etc/xcat,sys/class/net/lo,tmp,var/lib/dhclient} - : >"$fixture/cmdline" - sandbox=(env -i PATH=/usr/bin:/bin LC_ALL=C "$bwrap" - --unshare-all --die-with-parent --new-session - --ro-bind / / --tmpfs /run --proc /proc --dev /dev - --bind "$fixture" /run/fixture --bind "$fixture/tmp" /tmp - --bind "$fixture/etc" /etc --ro-bind "$fixture/sys" /sys - --bind "$fixture/var" /var + mkdir -p "$fixture"/{etc/xcat,sys/class/net/lo,var/lib/dhclient} || return + : >"$fixture/cmdline" || return + sandbox+=(--tmpfs /run --bind "$fixture" /run/fixture + --ro-bind "$fixture/sys" /sys --ro-bind "$fixture/cmdline" /proc/cmdline --ro-bind "$source" /run/script --chdir /tmp --setenv PATH /run/fixture/bin:/usr/bin:/bin --setenv LC_ALL C) diff --git a/xCAT-test/bats/sandbox_helpers.bats b/xCAT-test/bats/sandbox_helpers.bats new file mode 100644 index 0000000000..a6207a9e05 --- /dev/null +++ b/xCAT-test/bats/sandbox_helpers.bats @@ -0,0 +1,53 @@ +#!/usr/bin/env bats + +load 'helpers/genesis_sandbox' + +@test "script sandbox defaults to a private working directory" { + setup_script_sandbox pwd + run "${sandbox[@]}" /bin/pwd + [ "$status" -eq 0 ] + [ "$output" = /tmp ] +} + +@test "script sandbox lets adapters choose the working directory" { + setup_script_sandbox --no-chdir pwd + run "${sandbox[@]}" --chdir /run/fixture /bin/pwd + [ "$status" -eq 0 ] + [ "$output" = /run/fixture ] +} + +@test "script sandbox exposes only staged tools and private writable directories" { + setup_script_sandbox cat + printf 'private\n' >"$fixture/etc/marker" + run "${sandbox[@]}" /bin/sh -c ' + test ! -e /etc/os-release && test ! -x /usr/bin/id && + test -x /bin/sh && test -x /usr/bin/cat && + cat /etc/marker && + printf tmp > /tmp/marker && printf var > /var/marker && + printf run > /run/marker + ' + [ "$status" -eq 0 ] + [ "$output" = private ] + [ "$(cat "$fixture/tmp/marker")" = tmp ] + [ "$(cat "$fixture/var/marker")" = var ] + [ "$(cat "$fixture/run/marker")" = run ] +} + +@test "tool staging rejects an unavailable command" { + setup_script_sandbox + run stage_sandbox_tools xcat_fixture_missing_command + [ "$status" -ne 0 ] + [[ "$output" = *'Required utility is unavailable: xcat_fixture_missing_command'* ]] +} + +@test "Genesis retains host tools and synthetic kernel inputs" { + setup_genesis_sandbox doxcat + printf 'fixture command line\n' >"$fixture/cmdline" + run "${sandbox[@]}" /bin/sh -c ' + test -x /usr/bin/id && test -r /run/script && + test -d /sys/class/net/lo && test ! -e /sys/kernel && + test ! -e /etc/os-release && cat /proc/cmdline + ' + [ "$status" -eq 0 ] + [ "$output" = 'fixture command line' ] +}