Debian Codegraph @

tests/keyring-trusted-test

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # Encrypted and trusted keyring test; trusted keys can be emulated by TPM2.
4 # DO NOT use this in combination with a real TPM data, it can destroy them.
5 #
6 # Note that if trusted or tpm kernel module is built-in, emulated TPM2 cannot
7 # provide trusted keys (only encrypted keys will be tested then).
8
9 [scip-shell system . . `[`. -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
10 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
11
12 IMG=keyring_trusted_test.img
13 MAP="trusted_test"
14 KEYNAME=testxxx
15
16 bin_checkscip-shell shell shell-project 0.0.0 bin_check().()
17 {
18 command -v $1 >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "WARNING: test require $1 binary, test skipped."
19 }
20
21 cleanupscip-shell shell shell-project 0.0.0 cleanup().() {
22 clear_keysscip-shell shell shell-project 0.0.0 clear_keys().
23
24 [scip-shell system . . `[`. -S $SWTPM_STATE_DIR/ctrl.sock ] && {
25 # shutdown TPM via control socket
26 swtpm_ioctl -s --unix $SWTPM_STATE_DIR/ctrl.sock
27 sleepscip-shell system . . sleep. 1
28 }
29
30 # if graceful shutdown was successful, pidfile should be deleted
31 # if it is still present, we forcefully kill the process
32 [scip-shell system . . `[`. -f "$SWTPM_PIDFILE" ] && {
33 killscip-shell system . . kill. -9 $(catscip-shell system . . cat. $SWTPM_PIDFILE) >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
34 }
35
36 [scip-shell system . . `[`. -b /dev/mapper/$MAP ] && dmsetup remove --retry $MAP
37
38 [scip-shell system . . `[`. -n "$SWTPM_PIDFILE" ] && rmscip-shell system . . rm. -f $SWTPM_PIDFILE >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
39 [scip-shell system . . `[`. -n "$SWTPM_STATE_DIR" ] && rmscip-shell system . . rm. -rf $SWTPM_STATE_DIR >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
40 rmscip-shell system . . rm. -f $IMG >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
41
42 cleanup_modulesscip-shell shell shell-project 0.0.0 cleanup_modules().
43 }
44
45 cleanup_modulesscip-shell shell shell-project 0.0.0 cleanup_modules().()
46 {
47 [scip-shell system . . `[`. -n "$SKIP_MODULE_UNLOAD" ] && return
48
49 # This is a hack to avoid linking trusted key to another TPM.
50 # Without clean load tests do not work reliably.
51 modprobe -r dm-crypt 2>/dev/nullscip-shell filesystem . . `/dev/null`.
52 modprobe -r encrypted-keys 2>/dev/nullscip-shell filesystem . . `/dev/null`.
53 modprobe -r trusted 2>/dev/nullscip-shell filesystem . . `/dev/null`.
54 modprobe -r tpm_vtpm_proxy 2>/dev/nullscip-shell filesystem . . `/dev/null`.
55 modprobe -r tpm 2>/dev/nullscip-shell filesystem . . `/dev/null`.
56 }
57
58 failscip-shell shell shell-project 0.0.0 fail().()
59 {
60 echoscip-shell system . . echo. "[FAILED]"
61 [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1"
62 echoscip-shell system . . echo. "FAILED backtrace:"
63 while caller $frame; do ((frame++)); done
64 cleanupscip-shell shell shell-project 0.0.0 cleanup().
65 exit 2
66 }
67
68 _sigchldscip-shell shell shell-project 0.0.0 _sigchld().() { local c=$?; [scip-shell system . . `[`. $c -eq 139 ] && failscip-shell shell shell-project 0.0.0 fail(). "Segfault"; [scip-shell system . . `[`. $c -eq 134 ] && failscip-shell shell shell-project 0.0.0 fail(). "Aborted"; }
69 trap _sigchld CHLD
70
71 skipscip-shell shell shell-project 0.0.0 skip().()
72 {
73 [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1"
74 cleanupscip-shell shell shell-project 0.0.0 cleanup().
75 exit 77
76 }
77
78 prepare_tpmscip-shell shell shell-project 0.0.0 prepare_tpm().()
79 {
80 SWTPM_STATE_DIR=$(mktempscip-shell system . . mktemp. -d /tmp/systemd_swtpm_state.XXXXXXscip-shell filesystem . . `/tmp/systemd_swtpm_state.XXXXXX`.)
81 SWTPM_SEAL_KEY="$SWTPM_STATE_DIR/sealkey.ctxt"
82 SWTPM_PERSISTENT_HANDLE=0x81000001
83
84 if [scip-shell system . . `[`. -z "$TPM_PATH" ]; then
85 bin_checkscip-shell shell shell-project 0.0.0 bin_check(). swtpm
86 bin_checkscip-shell shell shell-project 0.0.0 bin_check(). swtpm_ioctl
87
88 SWTPM_PIDFILE=$(mktempscip-shell system . . mktemp. /tmp/systemd_swtpm_pid.XXXXXXscip-shell filesystem . . `/tmp/systemd_swtpm_pid.XXXXXX`.)
89
90 cleanup_modulesscip-shell shell shell-project 0.0.0 cleanup_modules().
91 modprobe tpm_vtpm_proxy || failscip-shell shell shell-project 0.0.0 fail(). "Failed to load tpm_vtpm_proxy kernel module, required for emulated TPM."
92
93 SWTPM_LOG=$(swtpm chardev --vtpm-proxy --tpm2 --tpmstate dir=$SWTPM_STATE_DIR -d --pid file=$SWTPM_PIDFILE --ctrl type=unixio,path=$SWTPM_STATE_DIR/ctrl.sock)
94 TPM_PATH=$(echoscip-shell system . . echo. $SWTPM_LOG | grepscip-shell system . . grep. -Eo /dev/tpm\([0-9]\)+ | sedscip-shell system . . sed. s/tpm/tpmrm/)
95
96 [scip-shell system . . `[`. -z "$TPM_PATH" ] && failscip-shell shell shell-project 0.0.0 fail(). "No TPM_PATH set and swtpm failed, test skipped."
97
98 echoscip-shell system . . echo. "Virtual TPM set up at $TPM_PATH"
99
100 # Trusted module needs to see TPM above.
101 sleepscip-shell system . . sleep. 1
102 modprobe trusted 2>/dev/nullscip-shell filesystem . . `/dev/null`. || echoscip-shell system . . echo. "Failed to load trusted keys kernel module."
103 else
104 SKIP_MODULE_UNLOAD=1
105 echoscip-shell system . . echo. "Preconfigured TPM set up at $TPM_PATH"
106 fi
107 # Create persistent store
108 tpm2_createprimary -Q -T device:$TPM_PATH --hierarchy o -G rsa2048 -c $SWTPM_SEAL_KEY || failscip-shell shell shell-project 0.0.0 fail().
109 tpm2_evictcontrol -Q -T device:$TPM_PATH -c $SWTPM_SEAL_KEY $SWTPM_PERSISTENT_HANDLE || failscip-shell shell shell-project 0.0.0 fail().
110 }
111
112 prepare_vk_keyringscip-shell shell shell-project 0.0.0 prepare_vk_keyring().()
113 {
114 local s_desc=$(keyctl rdescribe @s | cutscip-shell system . . cut. -d';' -f5)
115 local us_desc=$(keyctl rdescribe @us | cutscip-shell system . . cut. -d';' -f5)
116
117 if [scip-shell system . . `[`. "$s_desc" = "$us_desc" -a -n "$s_desc" ]; then
118 echoscip-shell system . . echo. "Session keyring is missing, initializing new one."
119 keyctl new_session > /dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail().
120 fi
121 }
122
123 clear_keysscip-shell shell shell-project 0.0.0 clear_keys().()
124 {
125 keyctl revoke %encrypted:$KEYNAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.
126 keyctl revoke %user:$KEYNAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.
127
128 if [scip-shell system . . `[`. -n "$TRUSTED_SUPPORTED" ]; then
129 keyctl revoke %trusted:$KEYNAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.
130 tpm2_evictcontrol -Q -T device:$TPM_PATH -C o -c $SWTPM_PERSISTENT_HANDLE 2>/dev/nullscip-shell filesystem . . `/dev/null`.
131 fi
132 }
133
134 [scip-shell system . . `[`. $(idscip-shell system . . id. -u) != 0 ] && skipscip-shell shell shell-project 0.0.0 skip(). "WARNING: You must be root to run this test, test skipped."
135 bin_checkscip-shell shell shell-project 0.0.0 bin_check(). keyctl
136
137 prepare_vk_keyringscip-shell shell shell-project 0.0.0 prepare_vk_keyring().
138
139 # Prevent using TPM by default
140 if [scip-shell system . . `[`. -n "$RUN_KEYRING_TRUSTED_TEST" ]; then
141 prepare_tpmscip-shell shell shell-project 0.0.0 prepare_tpm().
142 # Trusted keyring key
143 keyctl add trusted $KEYNAME "new 32 keyhandle=$SWTPM_PERSISTENT_HANDLE" @s >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 && TRUSTED_SUPPORTED=1
144 if [scip-shell system . . `[`. -n "$TRUSTED_SUPPORTED" ]; then
145 keyctl print %trusted:$KEYNAME >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot read trusted key blob."
146 fi
147 else
148 echoscip-shell system . . echo. "WARNING: Variable RUN_KEYRING_TRUSTED_TEST must be defined, TPM trusted test skipped."
149 TRUSTED_SUPPORTED=
150 fi
151
152 # Prepare encrypted key
153 modprobe encrypted-keys 2>/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Failed to load encrypted keys kernel module."
154
155 # Encrypted keyring key (encrypted with user key)
156 # User key needed for encrypted key
157 keyctl add -x user $KEYNAME bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a @s >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || skipscip-shell shell shell-project 0.0.0 skip(). "User key load failed, test skipped."
158 keyctl print %user:$KEYNAME >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot read user key."
159
160 keyctl add encrypted $KEYNAME "new user:$KEYNAME 32" @s >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Encrypted keys are not supported."
161 keyctl print %encrypted:$KEYNAME >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot read encrypted key blob."
162
163 ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of=$IMG bs=1M count=32 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
164
165 echoscip-shell system . . echo. -n "Plain with trusted volume key "
166 if [scip-shell system . . `[`. -n "$TRUSTED_SUPPORTED" ]; then
167 $CRYPTSETUP open --type plain $IMG $MAP --key-size=256 --cipher=aes-xts-plain64 --volume-key-keyring=%trusted:$KEYNAME || failscip-shell shell shell-project 0.0.0 fail().
168 dmsetup table --showkeys $MAP | grepscip-shell system . . grep. -q ":32:trusted:$KEYNAME" || failscip-shell shell shell-project 0.0.0 fail().
169 $CRYPTSETUP status $MAP | grepscip-shell system . . grep. -q "key location: keyring" || failscip-shell shell shell-project 0.0.0 fail().
170 $CRYPTSETUP close $MAP || failscip-shell shell shell-project 0.0.0 fail().
171 echoscip-shell system . . echo. "[OK]"
172 else
173 echoscip-shell system . . echo. "[N/A]"
174 fi
175
176 echoscip-shell system . . echo. -n "Plain with encrypted volume key "
177 $CRYPTSETUP open --type plain $IMG $MAP --key-size=256 --cipher=aes-xts-plain64 --volume-key-keyring=%encrypted:$KEYNAME ||failscip-shell shell shell-project 0.0.0 fail().
178 dmsetup table --showkeys $MAP | grepscip-shell system . . grep. -q ":32:encrypted:$KEYNAME" || failscip-shell shell shell-project 0.0.0 fail().
179 $CRYPTSETUP status $MAP | grepscip-shell system . . grep. -q "key location: keyring" || failscip-shell shell shell-project 0.0.0 fail().
180 $CRYPTSETUP resize $MAP --size 100 || failscip-shell shell shell-project 0.0.0 fail().
181 $CRYPTSETUP status $MAP | grepscip-shell system . . grep. "size:" | grepscip-shell system . . grep. -q "100 \[512-byte units\]" || failscip-shell shell shell-project 0.0.0 fail().
182 $CRYPTSETUP resize $MAP || failscip-shell shell shell-project 0.0.0 fail().
183 $CRYPTSETUP close $MAP || failscip-shell shell shell-project 0.0.0 fail().
184 echoscip-shell system . . echo. "[OK]"
185
186 cleanupscip-shell shell shell-project 0.0.0 cleanup().
187 exit 0