Debian Codegraph @

test/units/TEST-90-RESTRICT-FSACCESS.dm-verity-keyring.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 #
4 # Exercise the dedicated .dm-verity keyring trust path (kernel commit
5 # 033724b1c627, v7.0+): boot with linux-noinitrd so .platform stays empty,
6 # provision the mkosi cert into .dm-verity via keyctl, then verify a signed
7 # verity image still loads and execs under the BPF policy.
8 set -eux
9 set -o pipefail
10
11 # shellcheck source=test/units/util.sh
12 . "$(dirnamescip-shell system . . dirname. "$0")"/util.sh
13
14 if systemctl --version | grepscip-shell system . . grep. -F -- "-BPF_FRAMEWORK" >/dev/nullscip-shell filesystem . . `/dev/null`.; then
15 echoscip-shell system . . echo. "BPF framework not compiled in, skipping"
16 exit 0
17 fi
18
19 if ! kernel_supports_lsm bpf; then
20 echoscip-shell system . . echo. "BPF LSM not available in kernel, skipping"
21 exit 0
22 fi
23
24 if command -v bpftool >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
25 if ! bpftool btf dump file /sys/kernel/btf/vmlinuxscip-shell filesystem . . `/sys/kernel/btf/vmlinux`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. 'bpf_lsm_bdev_setintegrity' >/dev/nullscip-shell filesystem . . `/dev/null`.; then
26 echoscip-shell system . . echo. "Kernel lacks bdev_setintegrity LSM hook, skipping"
27 exit 0
28 fi
29 fi
30
31 if [[ -v ASAN_OPTIONS ]]; then
32 echoscip-shell system . . echo. "Skipping under sanitizers"
33 exit 0
34 fi
35
36 HELPER="/usr/lib/systemd/tests/unit-tests/manual/test-bpf-restrict-fsaccessscip-shell filesystem . . `/usr/lib/systemd/tests/unit-tests/manual/test-bpf-restrict-fsaccess`."
37 if [[ ! -x "$HELPER" ]]; then
38 echoscip-shell system . . echo. "ERROR: test-bpf-restrict-fsaccess helper not found at $HELPER" >&2
39 exit 1
40 fi
41
42 # Helper exits 77 when systemd was built with bpf-framework=enabled but no
43 # vmlinux.h (HAVE_LSM_INTEGRITY_TYPE=0), so the BPF program isn't compiled in.
44 rc=0
45 "$HELPER" check >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || rc=$?
46 if [[ "$rc" -eq 77 ]]; then
47 echoscip-shell system . . echo. "test-bpf-restrict-fsaccess built without BPF attach support, skipping"
48 exit 0
49 fi
50
51 if [[ ! -e /sys/module/dm_verity/parameters/require_signatures ]]; then
52 modprobe dm_verity 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.
53 fi
54 val="$(catscip-shell system . . cat. /sys/module/dm_verity/parameters/require_signaturesscip-shell filesystem . . `/sys/module/dm_verity/parameters/require_signatures`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. || echoscip-shell system . . echo.)"
55 if [[ "$val" != "Y" && "$val" != "1" ]]; then
56 echoscip-shell system . . echo. "require_signatures not enabled, skipping"
57 exit 0
58 fi
59
60 # Provision the .dm-verity keyring. Empty description lets the kernel derive
61 # one from the X.509 subject so machine_supports_verity_keyring finds the CN.
62 keyid=$(opensslscip-shell system . . openssl. x509 -in /usr/share/mkosi.crtscip-shell filesystem . . `/usr/share/mkosi.crt`. -outform DER |
63 keyctl padd asymmetric '' %:.dm-verity 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || keyid=""
64 if [[ -z "$keyid" ]]; then
65 echoscip-shell system . . echo. ".dm-verity keyring not provisionable (kernel < v7.0?), skipping"
66 exit 0
67 fi
68 if ! keyctl restrict_keyring %:.dm-verity; then
69 keyctl unlink "$keyid" %:.dm-verity 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.
70 echoscip-shell system . . echo. "ERROR: keyctl restrict_keyring failed" >&2
71 exit 1
72 fi
73 echoscip-shell system . . echo. "Provisioned .dm-verity keyring with mkosi.crt"
74
75 at_exitscip-shell shell systemd 261~rc4-1 at_exit().() {
76 set +e
77 [[ -n "${HELPER_PID:-}" ]] && killscip-shell system . . kill. "$HELPER_PID" 2>/dev/nullscip-shell filesystem . . `/dev/null`. && wait "$HELPER_PID" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.
78 rmscip-shell system . . rm. -rf /tmp/restrict-fsaccess-dvk-attach.outscip-shell filesystem . . `/tmp/restrict-fsaccess-dvk-attach.out`.
79 }
80 trap at_exit EXIT
81
82 HELPER_PID=
83 exec 3< <(exec "$HELPER" attach)
84 HELPER_PID=$!
85 while IFS= read -r -t 60 line <&3; do
86 echoscip-shell system . . echo. "$line"
87 [[ "$line" == LINK_IDS=* ]] && break
88 done > /tmp/restrict-fsaccess-dvk-attach.outscip-shell filesystem . . `/tmp/restrict-fsaccess-dvk-attach.out`.
89
90 # Fail closed if helper died before printing the full handshake: an unattached
91 # program would let the subsequent verity exec test pass trivially.
92 if ! killscip-shell system . . kill. -0 "$HELPER_PID" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
93 echoscip-shell system . . echo. "ERROR: helper exited before BPF programs were attached" >&2
94 exit 1
95 fi
96 grepscip-shell system . . grep. -E '^LINK_IDS="[^"]+"' /tmp/restrict-fsaccess-dvk-attach.outscip-shell filesystem . . `/tmp/restrict-fsaccess-dvk-attach.out`. >/dev/nullscip-shell filesystem . . `/dev/null`. || {
97 echoscip-shell system . . echo. "ERROR: helper did not report LINK_IDS, BPF programs not attached" >&2
98 exit 1
99 }
100
101 # Run a binary off the signed minimal_0 verity image. Trust path is exclusively
102 # the .dm-verity keyring we just provisioned; .platform is empty under
103 # linux-noinitrd.
104 systemd-run --pipe --wait \
105 --property RootImage=/usr/share/minimal_0.rawscip-shell filesystem . . `/usr/share/minimal_0.raw`. \
106 bash --version >/dev/nullscip-shell filesystem . . `/dev/null`.
107 echoscip-shell system . . echo. "Execution from signed dm-verity device (via .dm-verity keyring): OK"