-
Notifications
You must be signed in to change notification settings - Fork 1
/
check-linux.sh
executable file
·439 lines (377 loc) · 10.2 KB
/
check-linux.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0
#
# Copyright © 2016-2024 Mickaël Salaün <mic@digikod.net>.
#
# Build the kernel, samples, tests and check everything for Landlock.
#
# usage: [ARCH=um] [CC=gcc] check-linux.sh <command>...
set -e -u -o pipefail
REF="${1:-$(git describe --all --abbrev=0 HEAD~)}"
if [[ -z "${REF}" ]]; then
echo "ERROR: Must be run in the Git repository of the Linux kernel" >&2
exit 1
fi
BASE_DIR="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"
if [[ -z "${ARCH:-}" ]]; then
export ARCH="um"
fi
if [[ -z "${CC:-}" ]]; then
export CC="gcc"
fi
if [[ -z "${O:-}" ]]; then
export O="./.out-landlock_local-${ARCH}-${CC}"
fi
# Required for a deterministic Linux kernel.
export KBUILD_BUILD_USER="root"
export KBUILD_BUILD_HOST="localhost"
export KBUILD_BUILD_TIMESTAMP="$(git log --no-walk --pretty=format:%aD)"
NPROC="$(nproc)"
make_cmd() {
make "-j${NPROC}" "ARCH=${ARCH}" "CC=${CC}" "O=${O}" "$@"
}
unpatch_item() {
# Should always succeed.
case "$1" in
kernel_kconfig)
git apply --reverse "${BASE_DIR}/kernels/0001-test-Landlock-with-UML.patch" || :
;;
samples_kconfig)
git apply --reverse "${BASE_DIR}/kernels/0002-build-sandboxer-with-UML.patch" || :
;;
kselftest)
sed -e '0,/^all:$/s//\0 khdr/' -i tools/testing/selftests/Makefile || :
;;
format)
git cat-file -p "HEAD:.clang-format" > .clang-format
;;
*)
return 1
;;
esac
}
PATCHES=()
unpatch_all() {
set -- "${PATCHES[@]}"
while [[ $# -ge 1 ]]; do
unpatch_item "$1"
shift
done
}
patch_kernel_kconfig() {
if [[ "${ARCH}" != "um" ]]; then
return
fi
if git apply "${BASE_DIR}/kernels/0001-test-Landlock-with-UML.patch" 2>/dev/null; then
PATCHES+=(kernel_kconfig)
trap unpatch_all QUIT INT TERM EXIT
echo "[+] Patched Landlock's Kconfig for UML support"
fi
}
patch_samples_kconfig() {
if [[ "${ARCH}" != "um" ]]; then
return
fi
# Requires headers to be installed.
if git apply "${BASE_DIR}/kernels/0002-build-sandboxer-with-UML.patch" 2>/dev/null; then
PATCHES+=(samples_kconfig)
trap unpatch_all QUIT INT TERM EXIT
echo "[+] Patched samples' Kconfig for UML support"
fi
}
# First argument may be:
# - light: Only build the kernel, not the sample.
# - check: Build the kernel with runtime checks.
create_config() {
local config_arch="${BASE_DIR}/kernels/config-mini-${ARCH}"
local config_landlock="tools/testing/selftests/landlock/config"
local config_all=(
"${config_arch}"
)
if [[ "${1:-}" = "check" ]]; then
config_all+=("${BASE_DIR}/kernels/config-check")
fi
if [[ ! -f "${config_arch}" ]]; then
echo "ERROR: Architecture not supported" >&2
exit 1
fi
if [[ -f "${config_landlock}" ]]; then
config_all+=("${config_landlock}")
fi
patch_kernel_kconfig
if [[ "${1:-}" != "light" ]]; then
patch_samples_kconfig
fi
echo "[+] Creating minimal configuration"
make_cmd \
KCONFIG_ALLCONFIG=<(sort -u -- "${config_all[@]}") \
allnoconfig
}
install_headers() {
if [[ "${ARCH}" = "um" ]]; then
# Headers not exportable for UML.
ARCH="x86_64"
make_cmd headers_install
ARCH="um"
else
make_cmd headers_install
fi
}
# First argument may be:
# - light: Only build the kernel, not the sample.
build_main() {
make_cmd
if [[ "${1:-}" != "light" ]] && [[ ! -f "${O}/samples/landlock/sandboxer" ]]; then
echo "ERROR: Failed to build the sample"
exit 1
fi
}
set_source_dir() {
SOURCE_DIR="$1/"
MAKE_ARGS=(W=1e KCFLAGS=-Werror HOSTCFLAGS=-Werror USERCFLAGS=-Werror)
if [[ "${SOURCE_DIR##tools/}" != "${SOURCE_DIR}" ]]; then
if ! grep -q USERCFLAGS tools/testing/selftests/lib.mk; then
# Hack to support -Werror without proper USERCFLAGS.
MAKE_ARGS+=(KHDR_INCLUDES="-isystem ../../../../usr/include -Werror")
fi
# make O=out TARGETS=landlock -C tools/testing/selftests
MAKE_ARGS+=(TARGETS="$(basename -- "${SOURCE_DIR}")" -C "$(dirname -- "${SOURCE_DIR}")")
else
MAKE_ARGS+=("${SOURCE_DIR}")
fi
}
make_clean() {
echo "[+] Cleaning: ${SOURCE_DIR}"
if [[ "${SOURCE_DIR##tools/}" != "${SOURCE_DIR}" ]]; then
# make O=out TARGETS=landlock -C tools/testing/selftests
make_cmd -C "${SOURCE_DIR}" clean
else
make_cmd "M=${SOURCE_DIR}" clean
fi
}
check_sparse() {
echo "[+] Checking with sparse: ${SOURCE_DIR}"
# Requires sparse with commit 0e1aae55e49c ("fix "unreplaced" warnings caused by using typeof() on inline functions")
make_cmd C=2 CF='-Wsparse-error -fdiagnostic-prefix -D__CHECK_ENDIAN__' "${MAKE_ARGS[@]}"
}
check_warning() {
echo "[+] Checking warnings: ${SOURCE_DIR}"
make_cmd W=1 "${MAKE_ARGS[@]}"
}
check_smatch() {
echo "[+] Checking with smatch: ${SOURCE_DIR}"
if ! command -v smatch &>/dev/null; then
echo "ERROR: Unable to find the \"smatch\" command" >&2
exit 1
fi
make_cmd CHECK="smatch -p=kernel" C=1 "${MAKE_ARGS[@]}"
}
check_format() {
if [[ -n "$(git --no-pager log --max-count=1 --grep '^landlock: Format with clang-format$' --pretty=format:%H v5.10..HEAD security/landlock)" ]]; then
echo "[+] Checking with clang-format: ${SOURCE_DIR}"
# Checks for commit 781121a7f6d1 ("clang-format: Fix space after for_each macros").
local clang_format_compat="781121a7f6d11d7cae44982f174ea82adeec7db0"
if ! git merge-base --is-ancestor "${clang_format_compat}" HEAD; then
PATCHES+=(format)
trap unpatch_all QUIT INT TERM EXIT
git cat-file -p "${clang_format_compat}:.clang-format" > .clang-format
fi
local last_version="18"
local first_version="14"
local clang_format=""
local version
for version in $(seq "${last_version}" -1 "${first_version}"); do
if clang-format --version | grep -qF " version ${version}."; then
clang_format="clang-format"
break
elif command -v "clang-format-${version}" &>/dev/null; then
clang_format="clang-format-${version}"
break
fi
done
if [[ -z "${clang_format}" ]]; then
echo "ERROR: No clang-format between ${first_version} and ${last_version} found." >&2
return 1
fi
echo "${clang_format}" --dry-run --Werror "${SOURCE_DIR}"/*.[ch]
"${clang_format}" --dry-run --Werror "${SOURCE_DIR}"/*.[ch]
else
echo "[-] Not checking with clang-format: ${SOURCE_DIR}"
fi
}
check_build() {
if [[ "${ARCH}" == "um" ]]; then
# Only Kselftest builds without warning.
if [[ "${SOURCE_DIR##tools/}" == "${SOURCE_DIR}" ]]; then
return
else
patch_kselftest
fi
fi
make_clean
check_sparse
# Put warning check in the middle to force the next C=1 build.
check_warning
check_smatch
}
check_source_dir() {
set_source_dir "$1"
check_build
check_format
}
patch_kselftest() {
# Fixed with commit a52540522c95 ("selftests/landlock: Fix out-of-tree builds").
if grep -qE '^all: khdr$' tools/testing/selftests/Makefile; then
PATCHES+=(kselftest)
trap unpatch_all QUIT INT TERM EXIT
sed -e '0,/^all: khdr$/s//all:/' -i tools/testing/selftests/Makefile
echo "[+] Patched Kselftest"
fi
}
build_kselftest() {
local static_build=()
# Makes sure tests are fresh and not containing unsupported ones.
rm -r -- "${O}/kselftest/kselftest_install/landlock" 2>/dev/null || :
rm -r -- "${O}/kselftest/landlock" 2>/dev/null || :
# Opportunistically build with a static library (e.g. on Debian).
if [[ -f /usr/lib/x86_64-linux-gnu/libcap.a ]]; then
if grep -q USERLDFLAGS tools/testing/selftests/lib.mk; then
# commit de3ee3f63400 ("selftests: Use optional USERCFLAGS and USERLDFLAGS")
static_build+=("USERLDFLAGS=-static")
else
static_build+=("LDFLAGS=-static")
fi
fi
set_source_dir tools/testing/selftests/landlock
make_cmd "${MAKE_ARGS[@]}" "${static_build[@]}" install
}
run_kselftest_uml() {
local timeout=20
# TODO: Use ./run_kselftest.sh --summary while catching test errors.
timeout --signal KILL "${timeout}" </dev/null 2>&1 "${BASE_DIR}/uml-run.sh" \
"${O}/linux" \
-- \
"${BASE_DIR}/guest/kselftest.sh" \
"${O}/kselftest/kselftest_install/landlock" \
| timeout "$((timeout + 1))" cat
}
run_kselftest() {
case "${ARCH}" in
um)
run_kselftest_uml
;;
*)
echo "ERROR: Architecture not supported" >&2
exit 1
;;
esac
}
run_kunit() {
if [[ -f security/landlock/.kunitconfig ]]; then
if [[ "$O" != "." ]]; then
echo "[+] Running KUnit tests"
# TODO: Reuse the common build directory?
./tools/testing/kunit/kunit.py \
run \
--kunitconfig security/landlock \
--arch "${ARCH}" \
--build_dir "${O}-kunit"
else
echo "WARNING: Cannot run KUnit tests" >&2
fi
else
echo "[*] No KUnit tests"
fi
}
check_doc_path() {
local path="$1"
local date="$(git log --no-walk '--date=format:%B %Y' --format=%ad HEAD -- "${path}")"
if [[ -z "${date}" ]]; then
return
fi
echo "[+] Checking date ${date} in ${path}"
if ! grep -q "^:Date: ${date}\$" -- "${path}"; then
echo "[-] Incorrect date"
return 1
fi
}
check_doc() {
check_doc_path Documentation/userspace-api/landlock.rst
check_doc_path Documentation/security/landlock.rst
./scripts/kernel-doc -Werror -none include/uapi/linux/landlock.h security/landlock/*.h
}
check_patch() {
./scripts/checkpatch.pl --strict --codespell --git HEAD
}
exit_usage() {
echo "usage: $(basename -- "${BASH_SOURCE[0]}") all|build|build_light|lint|kselftest|kunit|doc|patch..." >&2
exit 1
}
run() {
case "${1:-}" in
all)
run build
run lint
run kselftest
run kunit
run doc
run patch
;;
build)
create_config check
install_headers
build_main
;;
build_light)
# Required for a deterministic Linux kernel.
if [[ -e "${O}/.version" ]]; then
rm "${O}/.version"
fi
create_config light
install_headers
build_main light
if [[ "${ARCH}" = "um" ]]; then
strip "${O}/linux"
fi
;;
lint)
install_headers
# tools/testing/selftests must go first because of patch_kselftest()
check_source_dir tools/testing/selftests/landlock
check_source_dir security/landlock
check_source_dir samples/landlock
;;
kselftest)
install_headers
patch_kselftest
build_kselftest
run_kselftest
;;
kunit)
run_kunit
;;
doc)
check_doc
;;
patch)
check_patch
;;
*)
exit_usage
;;
esac
}
if [[ $# -lt 1 ]]; then
exit_usage
fi
echo "[*] Architecture: ${ARCH}"
echo "[*] Compiler: ${CC}"
echo "[*] Build directory: ${O}"
if ! command -v git &>/dev/null; then
echo "ERROR: Unable to find the \"git\" command" >&2
exit 1
fi
while [[ $# -ge 1 ]]; do
run "$1"
shift
done