Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/testing/selftests/exec/check-exec-tests.sh
26285 views
1
#!/usr/bin/env bash
2
# SPDX-License-Identifier: GPL-2.0
3
#
4
# Test the "inc" interpreter.
5
#
6
# See include/uapi/linux/securebits.h, include/uapi/linux/fcntl.h and
7
# samples/check-exec/inc.c
8
#
9
# Copyright © 2024 Microsoft Corporation
10
11
set -u -e -o pipefail
12
13
EXPECTED_OUTPUT="1"
14
exec 2>/dev/null
15
16
DIR="$(dirname $(readlink -f "$0"))"
17
source "${DIR}"/../kselftest/ktap_helpers.sh
18
19
exec_direct() {
20
local expect="$1"
21
local script="$2"
22
shift 2
23
local ret=0
24
local out
25
26
# Updates PATH for `env` to execute the `inc` interpreter.
27
out="$(PATH="." "$@" "${script}")" || ret=$?
28
29
if [[ ${ret} -ne ${expect} ]]; then
30
echo "ERROR: Wrong expectation for direct file execution: ${ret}"
31
return 1
32
fi
33
if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
34
echo "ERROR: Wrong output for direct file execution: ${out}"
35
return 1
36
fi
37
}
38
39
exec_indirect() {
40
local expect="$1"
41
local script="$2"
42
shift 2
43
local ret=0
44
local out
45
46
# Script passed as argument.
47
out="$("$@" ./inc "${script}")" || ret=$?
48
49
if [[ ${ret} -ne ${expect} ]]; then
50
echo "ERROR: Wrong expectation for indirect file execution: ${ret}"
51
return 1
52
fi
53
if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
54
echo "ERROR: Wrong output for indirect file execution: ${out}"
55
return 1
56
fi
57
}
58
59
exec_stdin_reg() {
60
local expect="$1"
61
local script="$2"
62
shift 2
63
local ret=0
64
local out
65
66
# Executing stdin must be allowed if the related file is executable.
67
out="$("$@" ./inc -i < "${script}")" || ret=$?
68
69
if [[ ${ret} -ne ${expect} ]]; then
70
echo "ERROR: Wrong expectation for stdin regular file execution: ${ret}"
71
return 1
72
fi
73
if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
74
echo "ERROR: Wrong output for stdin regular file execution: ${out}"
75
return 1
76
fi
77
}
78
79
exec_stdin_pipe() {
80
local expect="$1"
81
shift
82
local ret=0
83
local out
84
85
# A pipe is not executable.
86
out="$(cat script-exec.inc | "$@" ./inc -i)" || ret=$?
87
88
if [[ ${ret} -ne ${expect} ]]; then
89
echo "ERROR: Wrong expectation for stdin pipe execution: ${ret}"
90
return 1
91
fi
92
}
93
94
exec_argument() {
95
local expect="$1"
96
local ret=0
97
shift
98
local out
99
100
# Script not coming from a file must not be executed.
101
out="$("$@" ./inc -c "$(< script-exec.inc)")" || ret=$?
102
103
if [[ ${ret} -ne ${expect} ]]; then
104
echo "ERROR: Wrong expectation for arbitrary argument execution: ${ret}"
105
return 1
106
fi
107
if [[ ${ret} -eq 0 && "${out}" != "${EXPECTED_OUTPUT}" ]]; then
108
echo "ERROR: Wrong output for arbitrary argument execution: ${out}"
109
return 1
110
fi
111
}
112
113
exec_interactive() {
114
exec_stdin_pipe "$@"
115
exec_argument "$@"
116
}
117
118
ktap_test() {
119
ktap_test_result "$*" "$@"
120
}
121
122
ktap_print_header
123
ktap_set_plan 28
124
125
# Without secbit configuration, nothing is changed.
126
127
ktap_print_msg "By default, executable scripts are allowed to be interpreted and executed."
128
ktap_test exec_direct 0 script-exec.inc
129
ktap_test exec_indirect 0 script-exec.inc
130
131
ktap_print_msg "By default, executable stdin is allowed to be interpreted."
132
ktap_test exec_stdin_reg 0 script-exec.inc
133
134
ktap_print_msg "By default, non-executable scripts are allowed to be interpreted, but not directly executed."
135
# We get 126 because of direct execution by Bash.
136
ktap_test exec_direct 126 script-noexec.inc
137
ktap_test exec_indirect 0 script-noexec.inc
138
139
ktap_print_msg "By default, non-executable stdin is allowed to be interpreted."
140
ktap_test exec_stdin_reg 0 script-noexec.inc
141
142
ktap_print_msg "By default, interactive commands are allowed to be interpreted."
143
ktap_test exec_interactive 0
144
145
# With only file restriction: protect non-malicious users from inadvertent errors (e.g. python ~/Downloads/*.py).
146
147
ktap_print_msg "With -f, executable scripts are allowed to be interpreted and executed."
148
ktap_test exec_direct 0 script-exec.inc ./set-exec -f --
149
ktap_test exec_indirect 0 script-exec.inc ./set-exec -f --
150
151
ktap_print_msg "With -f, executable stdin is allowed to be interpreted."
152
ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -f --
153
154
ktap_print_msg "With -f, non-executable scripts are not allowed to be executed nor interpreted."
155
# Direct execution of non-executable script is alwayse denied by the kernel.
156
ktap_test exec_direct 1 script-noexec.inc ./set-exec -f --
157
ktap_test exec_indirect 1 script-noexec.inc ./set-exec -f --
158
159
ktap_print_msg "With -f, non-executable stdin is allowed to be interpreted."
160
ktap_test exec_stdin_reg 0 script-noexec.inc ./set-exec -f --
161
162
ktap_print_msg "With -f, interactive commands are allowed to be interpreted."
163
ktap_test exec_interactive 0 ./set-exec -f --
164
165
# With only denied interactive commands: check or monitor script content (e.g. with LSM).
166
167
ktap_print_msg "With -i, executable scripts are allowed to be interpreted and executed."
168
ktap_test exec_direct 0 script-exec.inc ./set-exec -i --
169
ktap_test exec_indirect 0 script-exec.inc ./set-exec -i --
170
171
ktap_print_msg "With -i, executable stdin is allowed to be interpreted."
172
ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -i --
173
174
ktap_print_msg "With -i, non-executable scripts are allowed to be interpreted, but not directly executed."
175
# Direct execution of non-executable script is alwayse denied by the kernel.
176
ktap_test exec_direct 1 script-noexec.inc ./set-exec -i --
177
ktap_test exec_indirect 0 script-noexec.inc ./set-exec -i --
178
179
ktap_print_msg "With -i, non-executable stdin is not allowed to be interpreted."
180
ktap_test exec_stdin_reg 1 script-noexec.inc ./set-exec -i --
181
182
ktap_print_msg "With -i, interactive commands are not allowed to be interpreted."
183
ktap_test exec_interactive 1 ./set-exec -i --
184
185
# With both file restriction and denied interactive commands: only allow executable scripts.
186
187
ktap_print_msg "With -fi, executable scripts are allowed to be interpreted and executed."
188
ktap_test exec_direct 0 script-exec.inc ./set-exec -fi --
189
ktap_test exec_indirect 0 script-exec.inc ./set-exec -fi --
190
191
ktap_print_msg "With -fi, executable stdin is allowed to be interpreted."
192
ktap_test exec_stdin_reg 0 script-exec.inc ./set-exec -fi --
193
194
ktap_print_msg "With -fi, non-executable scripts are not allowed to be interpreted nor executed."
195
# Direct execution of non-executable script is alwayse denied by the kernel.
196
ktap_test exec_direct 1 script-noexec.inc ./set-exec -fi --
197
ktap_test exec_indirect 1 script-noexec.inc ./set-exec -fi --
198
199
ktap_print_msg "With -fi, non-executable stdin is not allowed to be interpreted."
200
ktap_test exec_stdin_reg 1 script-noexec.inc ./set-exec -fi --
201
202
ktap_print_msg "With -fi, interactive commands are not allowed to be interpreted."
203
ktap_test exec_interactive 1 ./set-exec -fi --
204
205
ktap_finished
206
207