Path: blob/master/tools/testing/selftests/exec/check-exec-tests.sh
26285 views
#!/usr/bin/env bash1# SPDX-License-Identifier: GPL-2.02#3# Test the "inc" interpreter.4#5# See include/uapi/linux/securebits.h, include/uapi/linux/fcntl.h and6# samples/check-exec/inc.c7#8# Copyright © 2024 Microsoft Corporation910set -u -e -o pipefail1112EXPECTED_OUTPUT="1"13exec 2>/dev/null1415DIR="$(dirname $(readlink -f "$0"))"16source "${DIR}"/../kselftest/ktap_helpers.sh1718exec_direct() {19local expect="$1"20local script="$2"21shift 222local ret=023local out2425# Updates PATH for `env` to execute the `inc` interpreter.26out="$(PATH="." "$@" "${script}")" || ret=$?2728if [[ ${ret} -ne ${expect} ]]; then29echo "ERROR: Wrong expectation for direct file execution: ${ret}"30return 131fi32if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then33echo "ERROR: Wrong output for direct file execution: ${out}"34return 135fi36}3738exec_indirect() {39local expect="$1"40local script="$2"41shift 242local ret=043local out4445# Script passed as argument.46out="$("$@" ./inc "${script}")" || ret=$?4748if [[ ${ret} -ne ${expect} ]]; then49echo "ERROR: Wrong expectation for indirect file execution: ${ret}"50return 151fi52if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then53echo "ERROR: Wrong output for indirect file execution: ${out}"54return 155fi56}5758exec_stdin_reg() {59local expect="$1"60local script="$2"61shift 262local ret=063local out6465# Executing stdin must be allowed if the related file is executable.66out="$("$@" ./inc -i < "${script}")" || ret=$?6768if [[ ${ret} -ne ${expect} ]]; then69echo "ERROR: Wrong expectation for stdin regular file execution: ${ret}"70return 171fi72if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then73echo "ERROR: Wrong output for stdin regular file execution: ${out}"74return 175fi76}7778exec_stdin_pipe() {79local expect="$1"80shift81local ret=082local out8384# A pipe is not executable.85out="$(cat script-exec.inc | "$@" ./inc -i)" || ret=$?8687if [[ ${ret} -ne ${expect} ]]; then88echo "ERROR: Wrong expectation for stdin pipe execution: ${ret}"89return 190fi91}9293exec_argument() {94local expect="$1"95local ret=096shift97local out9899# Script not coming from a file must not be executed.100out="$("$@" ./inc -c "$(< script-exec.inc)")" || ret=$?101102if [[ ${ret} -ne ${expect} ]]; then103echo "ERROR: Wrong expectation for arbitrary argument execution: ${ret}"104return 1105fi106if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then107echo "ERROR: Wrong output for arbitrary argument execution: ${out}"108return 1109fi110}111112exec_interactive() {113exec_stdin_pipe "$@"114exec_argument "$@"115}116117ktap_test() {118ktap_test_result "$*" "$@"119}120121ktap_print_header122ktap_set_plan 28123124# Without secbit configuration, nothing is changed.125126ktap_print_msg "By default, executable scripts are allowed to be interpreted and executed."127ktap_test exec_direct 0 script-exec.inc128ktap_test exec_indirect 0 script-exec.inc129130ktap_print_msg "By default, executable stdin is allowed to be interpreted."131ktap_test exec_stdin_reg 0 script-exec.inc132133ktap_print_msg "By default, non-executable scripts are allowed to be interpreted, but not directly executed."134# We get 126 because of direct execution by Bash.135ktap_test exec_direct 126 script-noexec.inc136ktap_test exec_indirect 0 script-noexec.inc137138ktap_print_msg "By default, non-executable stdin is allowed to be interpreted."139ktap_test exec_stdin_reg 0 script-noexec.inc140141ktap_print_msg "By default, interactive commands are allowed to be interpreted."142ktap_test exec_interactive 0143144# With only file restriction: protect non-malicious users from inadvertent errors (e.g. python ~/Downloads/*.py).145146ktap_print_msg "With -f, executable scripts are allowed to be interpreted and executed."147ktap_test exec_direct 0 script-exec.inc ./set-exec -f --148ktap_test exec_indirect 0 script-exec.inc ./set-exec -f --149150ktap_print_msg "With -f, executable stdin is allowed to be interpreted."151ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -f --152153ktap_print_msg "With -f, non-executable scripts are not allowed to be executed nor interpreted."154# Direct execution of non-executable script is alwayse denied by the kernel.155ktap_test exec_direct 1 script-noexec.inc ./set-exec -f --156ktap_test exec_indirect 1 script-noexec.inc ./set-exec -f --157158ktap_print_msg "With -f, non-executable stdin is allowed to be interpreted."159ktap_test exec_stdin_reg 0 script-noexec.inc ./set-exec -f --160161ktap_print_msg "With -f, interactive commands are allowed to be interpreted."162ktap_test exec_interactive 0 ./set-exec -f --163164# With only denied interactive commands: check or monitor script content (e.g. with LSM).165166ktap_print_msg "With -i, executable scripts are allowed to be interpreted and executed."167ktap_test exec_direct 0 script-exec.inc ./set-exec -i --168ktap_test exec_indirect 0 script-exec.inc ./set-exec -i --169170ktap_print_msg "With -i, executable stdin is allowed to be interpreted."171ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -i --172173ktap_print_msg "With -i, non-executable scripts are allowed to be interpreted, but not directly executed."174# Direct execution of non-executable script is alwayse denied by the kernel.175ktap_test exec_direct 1 script-noexec.inc ./set-exec -i --176ktap_test exec_indirect 0 script-noexec.inc ./set-exec -i --177178ktap_print_msg "With -i, non-executable stdin is not allowed to be interpreted."179ktap_test exec_stdin_reg 1 script-noexec.inc ./set-exec -i --180181ktap_print_msg "With -i, interactive commands are not allowed to be interpreted."182ktap_test exec_interactive 1 ./set-exec -i --183184# With both file restriction and denied interactive commands: only allow executable scripts.185186ktap_print_msg "With -fi, executable scripts are allowed to be interpreted and executed."187ktap_test exec_direct 0 script-exec.inc ./set-exec -fi --188ktap_test exec_indirect 0 script-exec.inc ./set-exec -fi --189190ktap_print_msg "With -fi, executable stdin is allowed to be interpreted."191ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -fi --192193ktap_print_msg "With -fi, non-executable scripts are not allowed to be interpreted nor executed."194# Direct execution of non-executable script is alwayse denied by the kernel.195ktap_test exec_direct 1 script-noexec.inc ./set-exec -fi --196ktap_test exec_indirect 1 script-noexec.inc ./set-exec -fi --197198ktap_print_msg "With -fi, non-executable stdin is not allowed to be interpreted."199ktap_test exec_stdin_reg 1 script-noexec.inc ./set-exec -fi --200201ktap_print_msg "With -fi, interactive commands are not allowed to be interpreted."202ktap_test exec_interactive 1 ./set-exec -fi --203204ktap_finished205206207