Debian Codegraph @

tests/luks2-validation-test

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 #turn on debug mode by following env. variable _DEBUG=1
4
5 PS4='$LINENO:'
6 [scip-shell system . . `[`. -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
7 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
8
9 if [scip-shell system . . `[`. -n "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then
10 CRYPTSETUP_VALGRIND=$CRYPTSETUP
11 else
12 CRYPTSETUP_VALGRIND=../.libs/cryptsetup
13 CRYPTSETUP_LIB_VALGRIND=../.libs
14 fi
15
16 START_DIR=$(pwdscip-shell system . . pwd.)
17
18 IMG=luks2-backend.img
19 ORIG_IMG=luks2_valid_hdr.img
20 TST_IMGS=$START_DIR/luks2-images
21
22 GEN_DIR=generators
23
24 FAILS=0
25
26 [scip-shell system . . `[`. -z "$srcdir" ] && srcdir="."
27
28 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().()
29 {
30 rmscip-shell system . . rm. -rf $IMG $TST_IMGS >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
31 }
32
33 failscip-shell shell shell-project 0.0.0 fail().()
34 {
35 [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1"
36 echoscip-shell system . . echo. "FAILED backtrace:"
37 while caller $frame; do ((frame++)); done
38 cd $START_DIR
39 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().
40 exit 2
41 }
42
43 _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"; }
44 trap _sigchld CHLD
45
46 fail_countscip-shell shell shell-project 0.0.0 fail_count().()
47 {
48 echoscip-shell system . . echo. "$1"
49 FAILS=$((FAILS+1))
50 }
51
52 skipscip-shell shell shell-project 0.0.0 skip().()
53 {
54 [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1"
55 exit 77
56 }
57
58 preparescip-shell shell shell-project 0.0.0 prepare().() # $1 dev1_size
59 {
60 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().
61
62 testscip-shell system . . test. -d $TST_IMGS || mkdirscip-shell system . . mkdir. $TST_IMGS
63
64 testscip-shell system . . test. -e $ORIG_IMG || xzscip-shell system . . xz. -dkc $srcdir/$ORIG_IMG.xz >$ORIG_IMG
65 cpscip-shell system . . cp. $ORIG_IMG $TST_IMGS
66 cpscip-shell system . . cp. $ORIG_IMG $IMG
67 }
68
69 test_loadscip-shell shell shell-project 0.0.0 test_load().()
70 {
71 local _debug=
72
73 testscip-shell system . . test. -z "$_DEBUG" || _debug="--debug"
74
75 case "$1" in
76 T)
77 if [scip-shell system . . `[`. -n "$_debug" ]; then
78 $CRYPTSETUP token remove --token-id 0 $_debug $IMG
79 else
80 $CRYPTSETUP token remove --token-id 0 $IMG > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
81 fi
82 testscip-shell system . . test. $? -eq 0 || return 1
83 if [scip-shell system . . `[`. -n "$_debug" ]; then
84 $CRYPTSETUP luksDump $_debug $IMG
85 else
86 $CRYPTSETUP luksDump $IMG > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
87 fi
88 testscip-shell system . . test. $? -eq 0 || return 1
89 ;;
90 R)
91 if [scip-shell system . . `[`. -n "$_debug" ]; then
92 $CRYPTSETUP luksDump $_debug $IMG
93 else
94 $CRYPTSETUP luksDump $_debug $IMG > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
95 fi
96 testscip-shell system . . test. $? -eq 0 || return 1
97 ;;
98 F)
99 if [scip-shell system . . `[`. -n "$_debug" ]; then
100 $CRYPTSETUP luksDump $_debug $IMG
101 else
102 $CRYPTSETUP luksDump $_debug $IMG > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
103 fi
104 ret=$?
105 testscip-shell system . . test. $ret -ne 0 || return 1
106 testscip-shell system . . test. $ret -ne 139 || return 1
107 ;;
108 *)
109 failscip-shell shell shell-project 0.0.0 fail(). "Internal test error"
110 ;;
111 esac
112 }
113
114 RUNscip-shell shell shell-project 0.0.0 RUN().()
115 {
116 echoscip-shell system . . echo. -n "Test image: $1..."
117 cpscip-shell system . . cp. $TST_IMGS/$1 $IMG || failscip-shell shell shell-project 0.0.0 fail(). "Missing test image"
118 test_loadscip-shell shell shell-project 0.0.0 test_load(). $2 "$3"
119 if [scip-shell system . . `[`. $? -ne 0 ]; then
120 fail_countscip-shell shell shell-project 0.0.0 fail_count(). "$3"
121 else
122 echoscip-shell system . . echo. "OK"
123 fi
124 }
125
126 valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup().()
127 {
128 command -v valgrind >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot find valgrind."
129 [scip-shell system . . `[`. ! -f $CRYPTSETUP_VALGRIND ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of cryptsetup executable."
130 [scip-shell system . . `[`. ! -f valg.sh ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of valg runner script."
131 if [scip-shell system . . `[`. -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then
132 export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
133 fi
134 }
135
136 valgrind_runscip-shell shell shell-project 0.0.0 valgrind_run().()
137 {
138 INFOSTRING="$(basenamescip-shell system . . basename. ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
139 }
140
141 [scip-shell system . . `[`. ! -x "$CRYPTSETUP" ] && skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find $CRYPTSETUP, test skipped."
142 [scip-shell system . . `[`. -n "$VALG" ] && valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup(). && CRYPTSETUP=valgrind_run
143
144 command -v jq >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find jq, test skipped."
145 command -v xxd >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find xxd, test skipped."
146
147 preparescip-shell shell shell-project 0.0.0 prepare().
148
149 echoscip-shell system . . echo. "[0] Generating test headers"
150 cd $srcdir/$GEN_DIR
151 for scr in ./generate-*.sh; do
152 echoscip-shell system . . echo. -n "$(basenamescip-shell system . . basename. $scr)..."
153 $scr $TST_IMGS $TST_IMGS/$ORIG_IMG || failscip-shell shell shell-project 0.0.0 fail(). "Header generator $scr failed: '$?'"
154 echoscip-shell system . . echo. "done"
155 done
156 cd $START_DIR
157
158 echoscip-shell system . . echo. "[1] Test basic auto-recovery"
159 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-checksum-hdr0.img "R" "Failed to recover from trivial header corruption at offset 0"
160 # TODO: check epoch is increased after recovery
161 # TODO: check only sectors related to corrupted hdr at offset 0 are written (dmstats tool/differ.c)
162
163 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-checksum-hdr1.img "R" "Failed to recover from trivial header corruption at offset 16384"
164 # TODO: check epoch is increased after recovery
165 # TODO: check only sectors related to corrupted hdr at offset 16384 are written (dmstats tool/differ.c)
166
167 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-checksum-both-hdrs.img "F" "Failed to recognise corrupted header beyond repair"
168
169 echoscip-shell system . . echo. "[2] Test ability to auto-correct mallformed json area"
170 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-corrupted-hdr0-with-correct-chks.img "R" "Failed to auto correct malformed json area at offset 512"
171 # TODO: check epoch is increased after recovery
172 # TODO: check only sectors related to corrupted hdr at offset 0 are written (dmstats tool/differ.c)
173
174 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-corrupted-hdr1-with-correct-chks.img "R" "Failed to auto correct malformed json area at offset 16896"
175 # TODO: check epoch is increased after recovery
176 # TODO: check only sectors related to corrupted hdr at offset 16384 are written (dmstats tool/differ.c)
177
178 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-correct-full-json0.img "R" "Failed to parse full and correct json area"
179 # TODO: detect noop (norecovery, epoch untouched)
180 # TODO: check epoch is NOT increased after recovery of secondary header
181
182 # these tests auto-correct json in-memory only. It'll get fixed on-disk after write operation
183 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-argon2-leftover-params.img "R" "Failed to repair keyslot with old argon2 parameters."
184 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-pbkdf2-leftover-params-0.img "R" "Failed to repair keyslot with old pbkdf2 parameters."
185 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-pbkdf2-leftover-params-1.img "R" "Failed to repair keyslot with old pbkdf2 parameters."
186
187 # Secondary header is always broken in following tests
188 echoscip-shell system . . echo. "[3] Test LUKS2 json area restrictions"
189 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-non-null-byte-beyond-json0.img "F" "Failed to detect illegal data right beyond json data string"
190 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-non-null-bytes-beyond-json0.img "F" "Failed to detect illegal data in json area"
191 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-missing-trailing-null-byte-json0.img "F" "Failed to detect missing terminal null byte"
192 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-opening-char-json0.img "F" "Failed to detect invalid opening character in json area"
193 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-object-type-json0.img "F" "Failed to detect invalid json object type"
194 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-overlapping-areas-c0-json0.img "F" "Failed to detect two exactly same area specifications"
195 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-overlapping-areas-c1-json0.img "F" "Failed to detect two intersecting area specifications"
196 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-overlapping-areas-c2-json0.img "F" "Failed to detect two slightly intersecting area specifications"
197 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-area-in-json-hdr-space-json0.img "F" "Failed to detect area referencing LUKS2 header space"
198 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-missing-keyslot-referenced-in-digest.img "F" "Failed to detect missing keyslot referenced in digest"
199 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-missing-segment-referenced-in-digest.img "F" "Failed to detect missing segment referenced in digest"
200 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-missing-keyslot-referenced-in-token.img "F" "Failed to detect missing keyslots referenced in token"
201 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-keyslot-missing-digest.img "F" "Failed to detect missing keyslot digest."
202 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-keyslot-too-many-digests.img "F" "Failed to detect keyslot has too many digests."
203
204 echoscip-shell system . . echo. "[4] Test integers value limits"
205 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-uint64-max-segment-size.img "R" "Validation rejected correct value"
206 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-uint64-overflow-segment-size.img "F" "Failed to detect uint64_t overflow"
207 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-uint64-signed-segment-size.img "F" "Failed to detect negative value"
208
209 echoscip-shell system . . echo. "[5] Test segments validation"
210 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-missing-type.img "F" "Failed to detect missing type field"
211 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-type.img "F" "Failed to detect invalid type field"
212 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-missing-offset.img "F" "Failed to detect missing offset field"
213 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-offset.img "F" "Failed to detect invalid offset field"
214 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-missing-size.img "F" "Failed to detect missing size field"
215 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-size-0.img "F" "Failed to detect invalid size field"
216 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-size-1.img "F" "Failed to detect invalid size field"
217 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-size-2.img "F" "Failed to detect invalid size field"
218 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-missing-encryption.img "F" "Failed to detect missing encryption field"
219 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-wrong-encryption.img "F" "Failed to detect invalid encryption field"
220 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-missing-ivoffset.img "F" "Failed to detect missing iv_tweak field"
221 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-wrong-ivoffset.img "F" "Failed to detect invalid iv_tweak field"
222 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-missing-sectorsize.img "F" "Failed to detect missing sector_size field"
223 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-wrong-sectorsize-0.img "F" "Failed to detect invalid sector_size field"
224 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-wrong-sectorsize-1.img "F" "Failed to detect invalid sector_size field"
225 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-wrong-sectorsize-2.img "F" "Failed to detect invalid sector_size field"
226 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-unknown-type.img "R" "Validation rejected segment with all mandatory fields correct"
227 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-two.img "R" "Validation rejected two valid segments"
228 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-flags.img "F" "Failed to detect invalid flags field"
229 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-flags-element.img "F" "Failed to detect invalid flags content"
230 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-backup-key-0.img "F" "Failed to detect gap in backup segments"
231 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-wrong-backup-key-1.img "F" "Failed to detect gap in backup segments"
232 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-segment-crypt-empty-encryption.img "F" "Failed to detect empty encryption field"
233
234 echoscip-shell system . . echo. "[6] Test metadata size and keyslots size (config section)"
235 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-keyslots-size-c0.img "F" "Failed to detect too large keyslots_size in config section"
236 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-keyslots-size-c1.img "F" "Failed to detect unaligned keyslots_size in config section"
237 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-keyslots-size-c2.img "F" "Failed to detect too small keyslots_size config section"
238 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-json-size-c0.img "F" "Failed to detect invalid json_size config section"
239 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-json-size-c1.img "F" "Failed to detect invalid json_size config section"
240 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-json-size-c2.img "F" "Failed to detect mismatching json size in config and binary hdr"
241 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-32k.img "R" "Valid 32KiB metadata size failed to validate"
242 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-64k.img "R" "Valid 64KiB metadata size failed to validate"
243 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-64k-inv-area-c0.img "F" "Failed to detect keyslot area trespassing in json area"
244 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-64k-inv-area-c1.img "F" "Failed to detect keyslot area overflowing keyslots area"
245 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-64k-inv-keyslots-size-c0.img "F" "Failed to detect keyslots size overflowing in data area"
246 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-128k.img "R" "Valid 128KiB metadata size failed to validate"
247 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-256k.img "R" "Valid 256KiB metadata size failed to validate"
248 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-512k.img "R" "Valid 512KiB metadata size failed to validate"
249 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-1m.img "R" "Valid 1MiB metadata size failed to validate"
250 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-2m.img "R" "Valid 2MiB metadata size failed to validate"
251 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-4m.img "R" "Valid 4MiB metadata size failed to validate"
252 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-16k-secondary.img "R" "Valid 16KiB metadata size in secondary hdr failed to validate"
253 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-32k-secondary.img "R" "Valid 32KiB metadata size in secondary hdr failed to validate"
254 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-64k-secondary.img "R" "Valid 64KiB metadata size in secondary hdr failed to validate"
255 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-128k-secondary.img "R" "Valid 128KiB metadata size in secondary hdr failed to validate"
256 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-256k-secondary.img "R" "Valid 256KiB metadata size in secondary hdr failed to validate"
257 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-512k-secondary.img "R" "Valid 512KiB metadata size in secondary hdr failed to validate"
258 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-1m-secondary.img "R" "Valid 1MiB metadata size in secondary hdr failed to validate"
259 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-2m-secondary.img "R" "Valid 2MiB metadata size in secondary hdr failed to validate"
260 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-4m-secondary.img "R" "Valid 4MiB metadata size in secondary hdr failed to validate"
261 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-invalid.img "F" "Invalid metadata size in secondary hdr not rejected"
262 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-metadata-size-invalid-secondary.img "F" "Invalid metadata size in secondary hdr not rejected"
263
264 echoscip-shell system . . echo. "[7] Test invalid metadata object property"
265 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-tokens.img "F" "Invalid tokens objects not rejected"
266 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-invalid-top-objects.img "F" "Invalid top-level objects not rejected"
267 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-keyslot-invalid-area.img "F" "Invalid keyslot area object not rejected"
268 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-keyslot-invalid-area-size.img "F" "Invalid keyslot area size that can overflow not rejected"
269 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-keyslot-invalid-objects.img "F" "Invalid keyslot objects not rejected"
270 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-keyslot-invalid-af.img "F" "Invalid keyslot objects types not rejected"
271
272 echoscip-shell system . . echo. "[8] Test non compact json does not break write optimization"
273 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-non-compact-json-token-0.img "T" "Non compact json area corrupted after write"
274 RUNscip-shell shell shell-project 0.0.0 RUN(). luks2-non-compact-json-4k-token-0.img "T" "Non compact 4K aligned json area corrupted after write"
275
276 remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().
277
278 testscip-shell system . . test. $FAILS -eq 0 || failscip-shell shell shell-project 0.0.0 fail(). "($FAILS wrong result(s) in total)"