| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #!/usr/bin/env bash
- # SPDX-License-Identifier: GPL-2.0
- #
- # Test the "inc" interpreter.
- #
- # See include/uapi/linux/securebits.h, include/uapi/linux/fcntl.h and
- # samples/check-exec/inc.c
- #
- # Copyright © 2024 Microsoft Corporation
- set -u -e -o pipefail
- EXPECTED_OUTPUT="1"
- exec 2>/dev/null
- DIR="$(dirname $(readlink -f "$0"))"
- source "${DIR}"/../kselftest/ktap_helpers.sh
- exec_direct() {
- local expect="$1"
- local script="$2"
- shift 2
- local ret=0
- local out
- # Updates PATH for `env` to execute the `inc` interpreter.
- out="$(PATH="." "$@" "${script}")" || ret=$?
- if [[ ${ret} -ne ${expect} ]]; then
- echo "ERROR: Wrong expectation for direct file execution: ${ret}"
- return 1
- fi
- if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
- echo "ERROR: Wrong output for direct file execution: ${out}"
- return 1
- fi
- }
- exec_indirect() {
- local expect="$1"
- local script="$2"
- shift 2
- local ret=0
- local out
- # Script passed as argument.
- out="$("$@" ./inc "${script}")" || ret=$?
- if [[ ${ret} -ne ${expect} ]]; then
- echo "ERROR: Wrong expectation for indirect file execution: ${ret}"
- return 1
- fi
- if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
- echo "ERROR: Wrong output for indirect file execution: ${out}"
- return 1
- fi
- }
- exec_stdin_reg() {
- local expect="$1"
- local script="$2"
- shift 2
- local ret=0
- local out
- # Executing stdin must be allowed if the related file is executable.
- out="$("$@" ./inc -i < "${script}")" || ret=$?
- if [[ ${ret} -ne ${expect} ]]; then
- echo "ERROR: Wrong expectation for stdin regular file execution: ${ret}"
- return 1
- fi
- if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
- echo "ERROR: Wrong output for stdin regular file execution: ${out}"
- return 1
- fi
- }
- exec_stdin_pipe() {
- local expect="$1"
- shift
- local ret=0
- local out
- # A pipe is not executable.
- out="$(cat script-exec.inc | "$@" ./inc -i)" || ret=$?
- if [[ ${ret} -ne ${expect} ]]; then
- echo "ERROR: Wrong expectation for stdin pipe execution: ${ret}"
- return 1
- fi
- }
- exec_argument() {
- local expect="$1"
- local ret=0
- shift
- local out
- # Script not coming from a file must not be executed.
- out="$("$@" ./inc -c "$(< script-exec.inc)")" || ret=$?
- if [[ ${ret} -ne ${expect} ]]; then
- echo "ERROR: Wrong expectation for arbitrary argument execution: ${ret}"
- return 1
- fi
- if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
- echo "ERROR: Wrong output for arbitrary argument execution: ${out}"
- return 1
- fi
- }
- exec_interactive() {
- exec_stdin_pipe "$@"
- exec_argument "$@"
- }
- ktap_test() {
- ktap_test_result "$*" "$@"
- }
- ktap_print_header
- ktap_set_plan 28
- # Without secbit configuration, nothing is changed.
- ktap_print_msg "By default, executable scripts are allowed to be interpreted and executed."
- ktap_test exec_direct 0 script-exec.inc
- ktap_test exec_indirect 0 script-exec.inc
- ktap_print_msg "By default, executable stdin is allowed to be interpreted."
- ktap_test exec_stdin_reg 0 script-exec.inc
- ktap_print_msg "By default, non-executable scripts are allowed to be interpreted, but not directly executed."
- # We get 126 because of direct execution by Bash.
- ktap_test exec_direct 126 script-noexec.inc
- ktap_test exec_indirect 0 script-noexec.inc
- ktap_print_msg "By default, non-executable stdin is allowed to be interpreted."
- ktap_test exec_stdin_reg 0 script-noexec.inc
- ktap_print_msg "By default, interactive commands are allowed to be interpreted."
- ktap_test exec_interactive 0
- # With only file restriction: protect non-malicious users from inadvertent errors (e.g. python ~/Downloads/*.py).
- ktap_print_msg "With -f, executable scripts are allowed to be interpreted and executed."
- ktap_test exec_direct 0 script-exec.inc ./set-exec -f --
- ktap_test exec_indirect 0 script-exec.inc ./set-exec -f --
- ktap_print_msg "With -f, executable stdin is allowed to be interpreted."
- ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -f --
- ktap_print_msg "With -f, non-executable scripts are not allowed to be executed nor interpreted."
- # Direct execution of non-executable script is alwayse denied by the kernel.
- ktap_test exec_direct 1 script-noexec.inc ./set-exec -f --
- ktap_test exec_indirect 1 script-noexec.inc ./set-exec -f --
- ktap_print_msg "With -f, non-executable stdin is allowed to be interpreted."
- ktap_test exec_stdin_reg 0 script-noexec.inc ./set-exec -f --
- ktap_print_msg "With -f, interactive commands are allowed to be interpreted."
- ktap_test exec_interactive 0 ./set-exec -f --
- # With only denied interactive commands: check or monitor script content (e.g. with LSM).
- ktap_print_msg "With -i, executable scripts are allowed to be interpreted and executed."
- ktap_test exec_direct 0 script-exec.inc ./set-exec -i --
- ktap_test exec_indirect 0 script-exec.inc ./set-exec -i --
- ktap_print_msg "With -i, executable stdin is allowed to be interpreted."
- ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -i --
- ktap_print_msg "With -i, non-executable scripts are allowed to be interpreted, but not directly executed."
- # Direct execution of non-executable script is alwayse denied by the kernel.
- ktap_test exec_direct 1 script-noexec.inc ./set-exec -i --
- ktap_test exec_indirect 0 script-noexec.inc ./set-exec -i --
- ktap_print_msg "With -i, non-executable stdin is not allowed to be interpreted."
- ktap_test exec_stdin_reg 1 script-noexec.inc ./set-exec -i --
- ktap_print_msg "With -i, interactive commands are not allowed to be interpreted."
- ktap_test exec_interactive 1 ./set-exec -i --
- # With both file restriction and denied interactive commands: only allow executable scripts.
- ktap_print_msg "With -fi, executable scripts are allowed to be interpreted and executed."
- ktap_test exec_direct 0 script-exec.inc ./set-exec -fi --
- ktap_test exec_indirect 0 script-exec.inc ./set-exec -fi --
- ktap_print_msg "With -fi, executable stdin is allowed to be interpreted."
- ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -fi --
- ktap_print_msg "With -fi, non-executable scripts are not allowed to be interpreted nor executed."
- # Direct execution of non-executable script is alwayse denied by the kernel.
- ktap_test exec_direct 1 script-noexec.inc ./set-exec -fi --
- ktap_test exec_indirect 1 script-noexec.inc ./set-exec -fi --
- ktap_print_msg "With -fi, non-executable stdin is not allowed to be interpreted."
- ktap_test exec_stdin_reg 1 script-noexec.inc ./set-exec -fi --
- ktap_print_msg "With -fi, interactive commands are not allowed to be interpreted."
- ktap_test exec_interactive 1 ./set-exec -fi --
- ktap_finished
|