Debian Codegraph @

tests/keyring-compat-test

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 CIPHER_XTS_PLAIN="aes-xts-plain64"
4 CIPHER_CBC_ESSIV="aes-cbc-essiv:sha256"
5 CIPHER_CBC_TCW="serpent-cbc-tcw"
6 # TODO: mode with LMK
7
8 TEST_KEYRING_NAME="keyringtest_keyring"
9
10 LOGON_KEY_16_OK="dmtst:lkey_16"
11 LOGON_KEY_32_OK="dmtst:lkey_32"
12 LOGON_KEY_64_OK="dmtst:lkey_64"
13
14 HEXKEY_16="be21aa8c733229347bd4e681891e213d";
15 HEXKEY_32="bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
16 HEXKEY_64="34f95b96abff946b64f1339ff8653cc77c38697c93b797a496f3786e86eed7781850d5112bbae17d209b8310a8f3a034f1cd297667bc0cd1438fad28d87ef6a1"
17
18 DEVSIZEMB=16
19 DEVSECTORS=$((DEVSIZEMB*1024*1024/512))
20 NAME=testcryptdev
21 CHKS_DMCRYPT=vk_in_dmcrypt.chk
22 CHKS_KEYRING=vk_in_keyring.chk
23
24 PWD="aaablabl"
25
26 [scip-shell system . . `[`. -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
27 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
28
29 if [scip-shell system . . `[`. -n "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then
30 CRYPTSETUP_VALGRIND=$CRYPTSETUP
31 else
32 CRYPTSETUP_VALGRIND=../.libs/cryptsetup
33 CRYPTSETUP_LIB_VALGRIND=../.libs
34 fi
35
36 FIPS_MODE=$(catscip-shell system . . cat. /proc/sys/crypto/fips_enabledscip-shell filesystem . . `/proc/sys/crypto/fips_enabled`. 2>/dev/nullscip-shell filesystem . . `/dev/null`.)
37
38 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().()
39 {
40 [scip-shell system . . `[`. -b /dev/mapper/$NAME ] && dmsetup remove --retry $NAME
41
42 # unlink whole test keyring
43 [scip-shell system . . `[`. -n "$TEST_KEYRING" ] && keyctl unlink $TEST_KEYRING "@u" >/dev/nullscip-shell filesystem . . `/dev/null`.
44
45 rmmod scsi_debug >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
46
47 rmscip-shell system . . rm. -f $CHKS_DMCRYPT $CHKS_KEYRING
48 }
49
50 skipscip-shell shell shell-project 0.0.0 skip().()
51 {
52 [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1"
53 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().
54 exit 77
55 }
56
57 valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup().()
58 {
59 command -v valgrind >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot find valgrind."
60 [scip-shell system . . `[`. ! -f $CRYPTSETUP_VALGRIND ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of cryptsetup executable."
61 [scip-shell system . . `[`. ! -f valg.sh ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of valg runner script."
62 if [scip-shell system . . `[`. -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then
63 export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
64 fi
65 }
66
67 valgrind_runscip-shell shell shell-project 0.0.0 valgrind_run().()
68 {
69 INFOSTRING="$(basenamescip-shell system . . basename. ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
70 }
71
72 failscip-shell shell shell-project 0.0.0 fail().()
73 {
74 [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1"
75 echoscip-shell system . . echo. "FAILED backtrace:"
76 while caller $frame; do ((frame++)); done
77 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().
78 exit 2
79 }
80
81 _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"; }
82 trap _sigchld CHLD
83
84 # $1 hexbyte key
85 # $2 type
86 # $3 description
87 # $4 keyring
88 load_keyscip-shell shell shell-project 0.0.0 load_key().()
89 {
90 local tmp="$1"
91 shift
92 echoscip-shell system . . echo. -n "$tmp" | xxd -r -p | keyctl padd $@ >/dev/nullscip-shell filesystem . . `/dev/null`.
93 }
94
95 dm_crypt_keyring_supportscip-shell shell shell-project 0.0.0 dm_crypt_keyring_support().()
96 {
97 VER_STR=$(dmsetup targets | grepscip-shell system . . grep. crypt | cutscip-shell system . . cut. -f2 -dv)
98 [scip-shell system . . `[`. -z "$VER_STR" ] && failscip-shell shell shell-project 0.0.0 fail(). "Failed to parse dm-crypt version."
99
100 VER_MAJ=$(echoscip-shell system . . echo. $VER_STR | cutscip-shell system . . cut. -f 1 -d.)
101 VER_MIN=$(echoscip-shell system . . echo. $VER_STR | cutscip-shell system . . cut. -f 2 -d.)
102
103 # run the test with dm-crypt v1.15.0+ on purpose
104 # the fix is in dm-crypt v1.18.1+
105 [scip-shell system . . `[`. $VER_MAJ -gt 1 ] && return 0
106 [scip-shell system . . `[`. $VER_MAJ -lt 1 ] && return 1
107 [scip-shell system . . `[`. $VER_MIN -ge 15 ]
108 }
109
110 test_and_prepare_keyringscip-shell shell shell-project 0.0.0 test_and_prepare_keyring().() {
111 keyctl list "@s" > /dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Current session keyring is unreachable, test skipped"
112 TEST_KEYRING=$(keyctl newring $TEST_KEYRING_NAME "@u" 2> /dev/nullscip-shell filesystem . . `/dev/null`.)
113 testscip-shell system . . test. -n "$TEST_KEYRING" || skipscip-shell shell shell-project 0.0.0 skip(). "Failed to create keyring in user keyring"
114 keyctl search "@s" keyring "$TEST_KEYRING" > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || keyctl link "@u" "@s" > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
115 load_keyscip-shell shell shell-project 0.0.0 load_key(). "$HEXKEY_16" user test_key "$TEST_KEYRING" || skipscip-shell shell shell-project 0.0.0 skip(). "Kernel keyring service is useless on this system, test skipped."
116 }
117
118 fips_modescip-shell shell shell-project 0.0.0 fips_mode().()
119 {
120 [scip-shell system . . `[`. -n "$FIPS_MODE" ] && [scip-shell system . . `[`. "$FIPS_MODE" -gt 0 ]
121 }
122
123 add_devicescip-shell shell shell-project 0.0.0 add_device().() {
124 rmmod scsi_debug >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
125 if [scip-shell system . . `[`. -d /sys/module/scsi_debugscip-shell filesystem . . `/sys/module/scsi_debug`. ] ; then
126 echoscip-shell system . . echo. "Cannot use scsi_debug module (in use or compiled-in), test skipped."
127 exit 77
128 fi
129
130 modprobe scsi_debug $@ delay=0 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
131 if [scip-shell system . . `[`. $? -ne 0 ] ; then
132 echoscip-shell system . . echo. "This kernel seems to not support proper scsi_debug module, test skipped."
133 exit 77
134 fi
135
136 sleepscip-shell system . . sleep. 1
137 DEV=$(grepscip-shell system . . grep. -l -e scsi_debug /sys/block/*/device/model | cutscip-shell system . . cut. -f4 -d /scip-shell filesystem . . `/`.)
138
139 DEV="/dev/$DEV"
140 [scip-shell system . . `[`. -b $DEV ] || failscip-shell shell shell-project 0.0.0 fail(). "Cannot find $DEV."
141 }
142
143 [scip-shell system . . `[`. ! -x "$CRYPTSETUP" ] && skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find $CRYPTSETUP, test skipped."
144 [scip-shell system . . `[`. -n "$VALG" ] && valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup(). && CRYPTSETUP=valgrind_run
145 [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."
146 command -v dmsetup >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find dmsetup, test skipped"
147 command -v keyctl >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find keyctl, test skipped"
148 command -v xxd >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find xxd, test skipped"
149 command -v sha256sum >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find sha256sum, test skipped"
150 modprobe dm-crypt >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). "dm-crypt failed to load"
151 dm_crypt_keyring_supportscip-shell shell shell-project 0.0.0 dm_crypt_keyring_support(). || skipscip-shell shell shell-project 0.0.0 skip(). "dm-crypt doesn't support kernel keyring, test skipped."
152
153 test_and_prepare_keyringscip-shell shell shell-project 0.0.0 test_and_prepare_keyring().
154
155 add_devicescip-shell shell shell-project 0.0.0 add_device(). dev_size_mb=$DEVSIZEMB
156
157 ddscip-shell system . . dd. if=/dev/urandomscip-shell filesystem . . `/dev/urandom`. of=$DEV bs=1M count=$DEVSIZEMB oflag=direct > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail().
158
159 #test aes cipher with xts mode, plain IV
160 echoscip-shell system . . echo. -n "Testing $CIPHER_XTS_PLAIN..."
161 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_XTS_PLAIN $HEXKEY_32 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
162 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
163 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
164 load_keyscip-shell shell shell-project 0.0.0 load_key(). "$HEXKEY_32" logon $LOGON_KEY_32_OK "$TEST_KEYRING" || failscip-shell shell shell-project 0.0.0 fail(). "Cannot load 32 byte logon key type"
165 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_XTS_PLAIN :32:logon:$LOGON_KEY_32_OK 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
166 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
167 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
168 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksums mismatch (corruption)"
169 # same test using message
170 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_XTS_PLAIN $HEXKEY_32 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
171 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
172 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
173 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_XTS_PLAIN $HEXKEY_32 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
174 dmsetup suspend $NAME || failscip-shell shell shell-project 0.0.0 fail().
175 dmsetup message $NAME 0 key wipe || failscip-shell shell shell-project 0.0.0 fail().
176 dmsetup message $NAME 0 "key set :32:logon:$LOGON_KEY_32_OK" || failscip-shell shell shell-project 0.0.0 fail().
177 dmsetup resume $NAME || failscip-shell shell shell-project 0.0.0 fail().
178 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
179 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
180 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksums mismatch (corruption)"
181 echoscip-shell system . . echo. "OK"
182
183 #test aes cipher, xts mode, essiv IV
184 echoscip-shell system . . echo. -n "Testing $CIPHER_CBC_ESSIV..."
185 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_ESSIV $HEXKEY_16 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
186 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
187 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
188 load_keyscip-shell shell shell-project 0.0.0 load_key(). "$HEXKEY_16" logon $LOGON_KEY_16_OK "$TEST_KEYRING" || failscip-shell shell shell-project 0.0.0 fail(). "Cannot load 16 byte logon key type"
189 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_ESSIV :16:logon:$LOGON_KEY_16_OK 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
190 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
191 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
192 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksums mismatch (corruption)"
193 # same test using message
194 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_ESSIV $HEXKEY_16 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
195 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
196 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
197 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_ESSIV $HEXKEY_16 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
198 dmsetup suspend $NAME || failscip-shell shell shell-project 0.0.0 fail().
199 dmsetup message $NAME 0 key wipe || failscip-shell shell shell-project 0.0.0 fail().
200 dmsetup message $NAME 0 "key set :16:logon:$LOGON_KEY_16_OK" || failscip-shell shell shell-project 0.0.0 fail().
201 dmsetup resume $NAME || failscip-shell shell shell-project 0.0.0 fail().
202 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
203 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
204 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksums mismatch (corruption)"
205 echoscip-shell system . . echo. "OK"
206
207 #test serpent cipher, cbc mode, tcw IV
208 fips_modescip-shell shell shell-project 0.0.0 fips_mode(). || {
209 echoscip-shell system . . echo. -n "Testing $CIPHER_CBC_TCW..."
210 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_TCW $HEXKEY_64 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
211 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
212 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
213 load_keyscip-shell shell shell-project 0.0.0 load_key(). "$HEXKEY_64" logon $LOGON_KEY_64_OK "$TEST_KEYRING" || failscip-shell shell shell-project 0.0.0 fail(). "Cannot load 16 byte logon key type"
214 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_TCW :64:logon:$LOGON_KEY_64_OK 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
215 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
216 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
217 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksum mismatch (corruption)"
218 # same test using message
219 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_TCW $HEXKEY_64 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
220 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
221 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
222 dmsetup create $NAME --table "0 $DEVSECTORS crypt $CIPHER_CBC_TCW $HEXKEY_64 0 $DEV 0" || failscip-shell shell shell-project 0.0.0 fail().
223 dmsetup suspend $NAME || failscip-shell shell shell-project 0.0.0 fail().
224 dmsetup message $NAME 0 key wipe || failscip-shell shell shell-project 0.0.0 fail().
225 dmsetup message $NAME 0 "key set :64:logon:$LOGON_KEY_64_OK" || failscip-shell shell shell-project 0.0.0 fail().
226 dmsetup resume $NAME || failscip-shell shell shell-project 0.0.0 fail().
227 sha256sumscip-shell system . . sha256sum. /dev/mapper/$NAME > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
228 dmsetup remove --retry $NAME || failscip-shell shell shell-project 0.0.0 fail().
229 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksums mismatch (corruption)"
230 echoscip-shell system . . echo. "OK"
231 }
232
233 echoscip-shell system . . echo. -n "Test LUKS2 key refresh..."
234 echoscip-shell system . . echo. $PWD | $CRYPTSETUP luksFormat --type luks2 --luks2-metadata-size 16k --luks2-keyslots-size 4064k --pbkdf pbkdf2 --pbkdf-force-iterations 1000 --force-password $DEV || failscip-shell shell shell-project 0.0.0 fail().
235 echoscip-shell system . . echo. $PWD | $CRYPTSETUP open $DEV $NAME || failscip-shell shell shell-project 0.0.0 fail().
236 $CRYPTSETUP status $NAME | grepscip-shell system . . grep. -q -i "location:.*keyring" || skipscip-shell shell shell-project 0.0.0 skip(). "LUKS2 can't use keyring. Test skipped."
237 ddscip-shell system . . dd. if=/dev/mapper/$NAME bs=1M iflag=direct status=none | sha256sumscip-shell system . . sha256sum. > $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail().
238 echoscip-shell system . . echo. $PWD | $CRYPTSETUP refresh $NAME --disable-keyring || failscip-shell shell shell-project 0.0.0 fail().
239 $CRYPTSETUP status $NAME | grepscip-shell system . . grep. -q -i "location:.*keyring" && failscip-shell shell shell-project 0.0.0 fail(). "Key is still in keyring"
240 ddscip-shell system . . dd. if=/dev/mapper/$NAME bs=1M iflag=direct status=none | sha256sumscip-shell system . . sha256sum. > $CHKS_DMCRYPT || failscip-shell shell shell-project 0.0.0 fail().
241 diffscip-shell system . . diff. $CHKS_DMCRYPT $CHKS_KEYRING || failscip-shell shell shell-project 0.0.0 fail(). "Plaintext checksum mismatch (corruption)"
242 echoscip-shell system . . echo. "OK"
243
244 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().