Debian Codegraph @

tests/align-test

raw symbols SCIP
1 #!/bin/bash
2
3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
5 DEV=""
6 DEV_STACKED="luks0xbabe"
7 DEV_NAME="dummyalign"
8 MNT_DIR="./mnt_luks"
9 PWD1="93R4P4pIqAH8"
10 PWD2="mymJeD8ivEhE"
11 FAST_PBKDF="--pbkdf-force-iterations 1000"
12
13 FIPS_MODE=$(cat /proc/sys/crypto/fips_enabled 2>/dev/null)
14
15 if [ -n "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then
16 CRYPTSETUP_VALGRIND=$CRYPTSETUP
17 else
18 CRYPTSETUP_VALGRIND=../.libs/cryptsetup
19 CRYPTSETUP_LIB_VALGRIND=../.libs
20 fi
21
22
23 fips_mode()
24 {
25 [ -n "$FIPS_MODE" ] && [ "$FIPS_MODE" -gt 0 ]
26 }
27
28 cleanup() {
29 udevadm settle >/dev/null 2>&1
30 if [ -d "$MNT_DIR" ] ; then
31 umount -f $MNT_DIR 2>/dev/null
32 rmdir $MNT_DIR 2>/dev/null
33 fi
34 [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
35 [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME >/dev/null 2>&1
36 # FIXME scsi_debug sometimes in-use here
37 sleep 1
38 rmmod scsi_debug >/dev/null 2>&1
39 sleep 1
40 }
41
42 fail()
43 {
44 if [ -n "$1" ] ; then echo "FAIL $1" ; fi
45 echo "FAILED backtrace:"
46 while caller $frame; do ((frame++)); done
47 cleanup
48 exit 100
49 }
50
51 _sigchld() { local c=$?; [ $c -eq 139 ] && fail "Segfault"; [ $c -eq 134 ] && fail "Aborted"; }
52 trap _sigchld CHLD
53
54 skip()
55 {
56 echo "TEST SKIPPED: $1"
57 cleanup
58 exit 77
59 }
60
61 valgrind_setup()
62 {
63 command -v valgrind >/dev/null || fail "Cannot find valgrind."
64 [ ! -f $CRYPTSETUP_VALGRIND ] && fail "Unable to get location of cryptsetup executable."
65 [ ! -f valg.sh ] && fail "Unable to get location of valg runner script."
66 if [ -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then
67 export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
68 fi
69 }
70
71 valgrind_run()
72 {
73 INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
74 }
75
76 dm_crypt_features()
77 {
78 VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
79 [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
80
81 VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
82 VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
83 VER_PTC=$(echo $VER_STR | cut -f 3 -d.)
84
85 [ $VER_MAJ -lt 1 ] && return
86 [ $VER_MAJ -gt 1 ] && {
87 DM_PERF_CPU=1
88 DM_SECTOR_SIZE=1
89 return
90 }
91
92 [ $VER_MIN -lt 14 ] && return
93 DM_PERF_CPU=1
94 if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
95 DM_SECTOR_SIZE=1
96 fi
97 }
98
99 add_device() {
100 modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
101 if [ $? -ne 0 ] ; then
102 echo "This kernel seems to not support proper scsi_debug module, test skipped."
103 exit 77
104 fi
105
106 sleep 1
107 DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
108
109 if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
110 echo "This kernel seems to not support topology info, test skipped."
111 cleanup
112 exit 77
113 fi
114
115 DEV="/dev/$DEV"
116 [ -b $DEV ] || fail "Cannot find $DEV."
117 }
118
119 format() # key_bits expected [forced]
120 {
121 if [ -z "$3" ] ; then
122 echo -n "Formatting using topology info ($1 bits key)..."
123 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c aes-cbc-essiv:sha256 -s $1 || fail
124 else
125 echo -n "Formatting using forced sector alignment $3 ($1 bits key)..."
126 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -s $1 -c aes-cbc-essiv:sha256 --align-payload=$3 ||fail
127 fi
128
129 # check the device can be activated
130 echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
131 $CRYPTSETUP close $DEV_NAME || fail
132
133 ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
134 #echo "ALIGN = $ALIGN"
135
136 [ -z "$ALIGN" ] && fail
137 [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
138
139 # test some operation, just in case
140 echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV $FAST_PBKDF --new-key-slot 1
141 [ $? -ne 0 ] && fail "Keyslot add failed."
142
143 $CRYPTSETUP -q luksKillSlot $DEV 1
144 [ $? -ne 0 ] && fail "Keyslot removal failed."
145
146 echo "PASSED"
147 }
148
149 get_offsets()
150 {
151 $CRYPTSETUP luksDump $DEV | grep "$1" | cut -s -d ':' -f 2 | sed -e 's/\s//g' -e :a -e N -e 's/\n/:/g' -e 's/\s//g' -e ta
152 }
153
154 format_null()
155 {
156 if [ $3 -eq 0 ] ; then
157 echo -n "Formatting using topology info ($1 bits key) [slot 0"
158 echo | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c null -s $1 || fail
159 else
160 echo -n "Formatting using forced sector alignment $3 ($1 bits key) [slot 0"
161 echo | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c null -s $1 --align-payload=$3 || fail
162 fi
163
164 # check the device can be activated
165 echo | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
166 $CRYPTSETUP close $DEV_NAME || fail
167
168 POFF=$(get_offsets "Payload offset")
169 [ -z "$POFF" ] && fail
170 [ $POFF != $2 ] && fail "Expected data offset differs: expected $2 != detected $POFF"
171 if [ -n "$4" ] ; then
172 for j in 1 2 3 4 5 6 7 ; do
173 echo -e "\n" | $CRYPTSETUP luksAddKey $DEV -q $FAST_PBKDF --new-key-slot $j -c null $PARAMS
174 echo -n $j
175 [ $? -ne 0 ] && fail
176 done
177
178 KOFF=$(get_offsets "Key material offset")
179 [ -z "$KOFF" ] && fail
180 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
181 fi
182
183 echo "]...PASSED"
184 }
185
186 format_plain() # sector size
187 {
188 echo -n "Formatting plain device (sector size $1)..."
189 if [ -n "$DM_SECTOR_SIZE" ] ; then
190 echo $PWD1 | $CRYPTSETUP open --type plain --cipher aes-cbc-essiv:sha256 --key-size 256 --hash sha256 --sector-size $1 $DEV $DEV_NAME || fail
191 $CRYPTSETUP close $DEV_NAME || fail
192 echo "PASSED"
193 else
194 echo "N/A"
195 fi
196 }
197
198 format_plain_fail() # sector size
199 {
200 echo -n "Formatting plain device (sector size $1, must fail)..."
201 if [ -n "$DM_SECTOR_SIZE" ] ; then
202 echo $PWD1 | $CRYPTSETUP open --type plain --hash sha256 --sector-size $1 $DEV $DEV_NAME >/dev/null 2>&1 && fail
203 echo "PASSED"
204 else
205 echo "N/A"
206 fi
207 }
208
209 [ ! -x "$CRYPTSETUP" ] && skip "Cannot find $CRYPTSETUP, test skipped."
210 [ -n "$VALG" ] && valgrind_setup && CRYPTSETUP=valgrind_run
211 if [ $(id -u) != 0 ]; then
212 echo "WARNING: You must be root to run this test, test skipped."
213 exit 77
214 fi
215
216 dm_crypt_features
217 modprobe --dry-run scsi_debug >/dev/null 2>&1 || skip "This kernel seems to not support proper scsi_debug module, test skipped."
218 cleanup
219 if [ -d /sys/module/scsi_debug ] ; then
220 echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
221 exit 77
222 fi
223
224 echo "# Create desktop-class 4K drive"
225 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
226 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
227 format 256 4096
228 format 256 2056 8
229 format 128 2048
230 format 128 1032 8
231 format 256 8192 8192
232 format 128 8192 8192
233 cleanup
234
235 echo "# Create desktop-class 4K drive with misaligned opt-io (some bad USB enclosures)"
236 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0, opt-io=1025)"
237 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1 opt_blks=1025
238 format 256 4096
239 format 256 2056 8
240 format 128 2048
241 format 128 1032 8
242 format 256 8192 8192
243 format 128 8192 8192
244 cleanup
245
246 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
247 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
248 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
249 format 256 4103
250 format 256 2056 8
251 format 128 2055
252 format 128 1032 8
253 cleanup
254
255 echo "# Create enterprise-class 4K drive"
256 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
257 add_device dev_size_mb=16 sector_size=4096 num_tgts=1 opt_blks=64
258 format 256 4096
259 format 256 2056 8
260 format 128 2048
261 format 128 1032 8
262 cleanup
263
264 echo "# Create classic 512B drive and stack dm-linear"
265 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
266 add_device dev_size_mb=16 sector_size=512 num_tgts=1
267 DEV2=$DEV
268 DEV=/dev/mapper/$DEV_STACKED
269 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
270 format 256 4096
271 format 256 2056 8
272 format 128 2048
273 format 128 1032 8
274 format 128 8192 8192
275 cleanup
276
277 echo "# Create classic 512B drive and stack dm-linear (plain mode)"
278 add_device dev_size_mb=16 sector_size=512 num_tgts=1
279 DEV2=$DEV
280 DEV=/dev/mapper/$DEV_STACKED
281 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
282 format_plain 512
283 format_plain 1024
284 format_plain 2048
285 format_plain 4096
286 format_plain_fail 1111
287 format_plain_fail 8192
288 echo "# Create classic 512B drive, unaligned to 4096 and stack dm-linear (plain mode)"
289 dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
290 dmsetup create $DEV_STACKED --table "0 32762 linear $DEV2 0"
291 format_plain 512
292 format_plain 1024
293 format_plain_fail 2048
294 format_plain_fail 4096
295 cleanup
296
297 # skip tests using empty passphrase (LUKS1 cipher_null)
298 if ! fips_mode; then
299 echo "# Offset check: 512B sector drive"
300 add_device dev_size_mb=16 sector_size=512 num_tgts=1
301 # |k| expO reqO expected slot offsets
302 format_null 128 2048 0 8:136:264:392:520:648:776:904
303 format_null 128 1032 1
304 format_null 128 1032 8
305 format_null 128 1152 128
306 format_null 128 2048 2048
307 format_null 256 4096 0 8:264:520:776:1032:1288:1544:1800
308 format_null 256 2056 1
309 format_null 256 2056 8
310 format_null 256 2176 128
311 format_null 256 4096 2048
312 format_null 512 4096 0 8:512:1016:1520:2024:2528:3032:3536
313 format_null 512 4040 1
314 format_null 512 4040 8
315 format_null 512 4096 128
316 format_null 512 4096 2048
317 cleanup
318
319 echo "# Offset check: 4096B sector drive"
320 add_device dev_size_mb=16 sector_size=4096 num_tgts=1 opt_blks=64
321 format_null 128 2048 0 8:136:264:392:520:648:776:904
322 format_null 128 1032 1
323 format_null 128 1032 8
324 format_null 128 1152 128
325 format_null 128 2048 2048
326 format_null 256 4096 0 8:264:520:776:1032:1288:1544:1800
327 format_null 256 2056 1
328 format_null 256 2056 8
329 format_null 256 2176 128
330 format_null 256 4096 2048
331 format_null 512 4096 0 8:512:1016:1520:2024:2528:3032:3536
332 format_null 512 4040 1
333 format_null 512 4040 8
334 format_null 512 4096 128
335 format_null 512 4096 2048
336 cleanup
337 fi
338
339 echo "# Create enterprise-class 4K drive with fs and LUKS images."
340 # loop device here presents 512 block but images have 4k block
341 # cryptsetup should properly use 4k block on direct-io
342 add_device dev_size_mb=32 sector_size=4096 physblk_exp=0 num_tgts=1 opt_blks=64
343 for file in $(ls img_fs_*.img.xz) ; do
344 echo -n "Format using fs image $file "
345 xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
346 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
347 mount $DEV $MNT_DIR 2>/dev/null
348 if [ $? -ne 0 ] ; then
349 echo "[N/A]"
350 continue
351 fi
352 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 --key-size 256 $FAST_PBKDF $MNT_DIR/luks.img || fail
353 echo $PWD2 | $CRYPTSETUP luksFormat --type luks1 --key-size 256 $FAST_PBKDF $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail
354 umount $MNT_DIR || fail
355 echo "[OK]"
356 done
357 cleanup
358
359 exit 0