summaryrefslogtreecommitdiffstats
path: root/tests/align-test2
blob: 33126a4393bd8443244f829cdf9ddbbd45740945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/bash

[ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
DEV=""
DEV_STACKED="luks0xbabe"
DEV_NAME="dummyalign"
HDR="test_luks2_hdr"
MNT_DIR="./mnt_luks"
PWD1="93R4P4pIqAH8"
PWD2="mymJeD8ivEhE"
FAST_PBKDF="--pbkdf pbkdf2 --pbkdf-force-iterations 1000"

CRYPTSETUP_VALGRIND=../.libs/cryptsetup
CRYPTSETUP_LIB_VALGRIND=../.libs

cleanup() {
	udevadm settle >/dev/null 2>&1
	if [ -d "$MNT_DIR" ] ; then
		umount -f $MNT_DIR 2>/dev/null
		rmdir $MNT_DIR 2>/dev/null
	fi
	[ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
	[ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME >/dev/null 2>&1
	# FIXME scsi_debug sometimes in-use here
	sleep 1
	rmmod scsi_debug >/dev/null 2>&1
	sleep 1
	rm -f $HDR 2>/dev/null
}

fail()
{
	if [ -n "$1" ] ; then echo "FAIL $1" ; fi
	echo "FAILED backtrace:"
	while caller $frame; do ((frame++)); done
	cleanup
	exit 100
}

skip()
{
	echo "TEST SKIPPED: $1"
	cleanup
	exit 77
}

function valgrind_setup()
{
	command -v valgrind >/dev/null || fail "Cannot find valgrind."
	[ ! -f $CRYPTSETUP_VALGRIND ] && fail "Unable to get location of cryptsetup executable."
	export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
}

function valgrind_run()
{
	INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
}

function dm_crypt_features()
{
	VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
	[ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."

	VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
	VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
	VER_PTC=$(echo $VER_STR | cut -f 3 -d.)

	[ $VER_MAJ -lt 1 ] && return
	[ $VER_MAJ -gt 1 ] && {
		DM_PERF_CPU=1
		DM_SECTOR_SIZE=1
		return
	}

	[ $VER_MIN -lt 14 ] && return
	DM_PERF_CPU=1
	if [ $VER_MIN -ge 17 ]; then
		DM_SECTOR_SIZE=1
	fi
}

add_device() {
	modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
	if [ $? -ne 0 ] ; then
		echo "This kernel seems to not support proper scsi_debug module, test skipped."
		exit 77
	fi

	sleep 2
	DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)

	if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
		echo "This kernel seems to not support topology info, test skipped."
		cleanup
		exit 77
	fi

	DEV="/dev/$DEV"
	[ -b $DEV ] || fail "Cannot find $DEV."
}

format() # expected [forced] [encryption_sector_size]
{
	local _sec_size=512

	local _exp=$1

	if [ "${2:0:1}" = "s" ]; then
		_sec_size=${2:1}
		shift
	fi

	test "${3:0:1}" = "s" && _sec_size=${3:1}

	test $_sec_size -eq 512 || local _smsg=" (encryption sector size $_sec_size)"

	if [ -z "$2" ] ; then
		echo -n "Formatting using topology info$_smsg..."
		echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 || fail
	else
		echo -n "Formatting using forced sector alignment $2$_smsg..."
		echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null || fail
	fi

	# check the device can be activated
	if [ -n "$DM_SECTOR_SIZE" ] ; then
		echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
		$CRYPTSETUP close $DEV_NAME || fail
	fi

	ALIGN=$($CRYPTSETUP luksDump $DEV | grep -A1 "0: crypt" | grep "offset:" | cut -d ' ' -f2)
	# echo "ALIGN = $ALIGN"

	[ -z "$ALIGN" ] && fail
	ALIGN=$((ALIGN/512))
	[ $ALIGN -ne $_exp ] && fail "Expected alignment differs: expected $_exp != detected $ALIGN"

	# test some operation, just in case
	echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV $FAST_PBKDF --new-key-slot 1
	[ $? -ne 0 ] && fail "Keyslot add failed."

	$CRYPTSETUP -q luksKillSlot $DEV 1
	[ $? -ne 0 ] && fail "Keyslot removal failed."

	echo "PASSED"
}

format_fail() # expected [forced] [encryption_sector_size]
{
	local _sec_size=512

	local _exp=$1

	if [ "${2:0:1}" = "s" ]; then
		_sec_size=${2:1}
		shift
	fi

	test "${3:0:1}" = "s" && _sec_size=${3:1}

	test $_sec_size -eq 512 || local _smsg=" (encryption sector size $_sec_size)"

	if [ -z "$2" ] ; then
		echo -n "Formatting using topology info$_smsg (must fail)..."
		echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 && fail
	else
		echo -n "Formatting using forced sector alignment $2$_smsg (must fail)..."
		echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null 2>&1 && fail
	fi

	echo "PASSED"
}

auto_sector() # expected device header
{
	local _exp=$1
	local _dev=$2
	local _hdr=$2
	local _hdrstr=""
	local _hdrmsg=""

	if [ -n "$3" ]; then
		_hdrstr="--header $3"
		_hdr=$3
		_hdrmsg=" detached header"
	fi

	echo -n "Formatting$_hdrmsg using optimal encryption sector size (expecting $_exp)..."

	if [ -z "$DM_SECTOR_SIZE" -a $_exp -ne 512 ]; then
		echo "SKIPPED"
		return
	fi

	echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $_hdrstr $_dev -q >/dev/null 2>&1 || fail

	# check the device can be activated
	echo $PWD1 | $CRYPTSETUP luksOpen $_hdrstr $_dev $DEV_NAME || fail
	$CRYPTSETUP close $DEV_NAME || fail

	SECTOR=$($CRYPTSETUP luksDump $_hdr | grep -A4 "0: crypt" | grep "sector:" | cut -d ' ' -f2)

	[ -z "$SECTOR" ] && fail
	[ $SECTOR -ne $_exp ] && fail "Expected sector size differs: expected $_exp != detected $SECTOR"

	echo "PASSED"
}

[ ! -x "$CRYPTSETUP" ] && skip "Cannot find $CRYPTSETUP, test skipped."
[ -n "$VALG" ] && valgrind_setup && CRYPTSETUP=valgrind_run
if [ $(id -u) != 0 ]; then
	echo "WARNING: You must be root to run this test, test skipped."
	exit 77
fi

dm_crypt_features
modprobe --dry-run scsi_debug >/dev/null 2>&1 || skip "This kernel seems to not support proper scsi_debug module, test skipped."
cleanup
if [ -d /sys/module/scsi_debug ] ; then
	echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
	exit 77
fi

add_device dev_size_mb=32
echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q  >/dev/null || fail
EXPCT=$($CRYPTSETUP luksDump $DEV | grep "offset: " | cut -f 2 -d ' ')
test "$EXPCT" -gt 512 || fail
EXPCT=$((EXPCT/512))
echo "Default alignment detected: $EXPCT sectors"
cleanup

echo "# Create desktop-class 4K drive"
echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1
format $EXPCT
format $EXPCT s1024
format $EXPCT s2048
format $EXPCT s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create desktop-class 4K drive with misaligned opt-io (some bad USB enclosures)"
echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0, opt-io=1025)"
add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1 opt_blks=1025
format $EXPCT
format $EXPCT s1024
format $EXPCT s2048
format $EXPCT s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create drive with misaligned opt-io to page-size (some bad USB enclosures)"
echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0, opt-io=33553920)"
add_device dev_size_mb=32 sector_size=512 num_tgts=1 opt_blks=65535
format $EXPCT
format $EXPCT s1024
format $EXPCT s2048
format $EXPCT s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create desktop-class 4K drive w/ 1-sector shift (original bug report)"
echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=512)"
add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=1 num_tgts=1
format $((EXPCT+1))
format_fail $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
format $((EXPCT+7))
format_fail $((EXPCT+7)) s1024
format_fail $((EXPCT+7)) s2048
format_fail $((EXPCT+7)) s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create enterprise-class 4K drive"
echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
add_device dev_size_mb=32 sector_size=4096 num_tgts=1 opt_blks=64
format $EXPCT
format $EXPCT s1024
format $EXPCT s2048
format $EXPCT s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
#FIXME: kernel limits issue?
##format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create classic 512B drive and stack dm-linear"
echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
add_device dev_size_mb=32 sector_size=512 num_tgts=1
DEV2=$DEV
DEV=/dev/mapper/$DEV_STACKED
dmsetup create $DEV_STACKED --table "0 65536 linear $DEV2 0"
format $EXPCT
format $EXPCT s1024
format $EXPCT s2048
format $EXPCT s4096
format $EXPCT 1
format $EXPCT 1 s1024
format $EXPCT 1 s2048
format $EXPCT 1 s4096
format $EXPCT 8
format $EXPCT 8 s1024
format $EXPCT 8 s2048
format $EXPCT 8 s4096
format $((EXPCT+1)) $((EXPCT+1))
format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
format $EXPCT $EXPCT
format $EXPCT $EXPCT s1024
format $EXPCT $EXPCT s2048
format $EXPCT $EXPCT s4096
cleanup

echo "# Create enterprise-class 4K drive with fs and LUKS images."
# loop device here presents 512 block but images have 4k block
# cryptsetup should properly use 4k block on direct-io
add_device dev_size_mb=32 sector_size=4096 physblk_exp=0 num_tgts=1 opt_blks=64
for file in $(ls img_fs_*.img.xz) ; do
	echo "Format using fs image $file."
	xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
	[ ! -d $MNT_DIR ] && mkdir $MNT_DIR
	mount $DEV $MNT_DIR || skip "Mounting image is not available."
	echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $MNT_DIR/luks.img --offset 8192 || fail
	echo $PWD2 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail
	umount $MNT_DIR
done
cleanup

echo "# Create classic 512B drive"
echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
add_device dev_size_mb=32 sector_size=512 num_tgts=1
auto_sector 512 $DEV
auto_sector 512 $DEV $HDR
cleanup
echo "# Create desktop-class 4K drive"
echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1
auto_sector 4096 $DEV
auto_sector 4096 $DEV $HDR
DEV2=$DEV
DEV=/dev/mapper/$DEV_STACKED
dmsetup create $DEV_STACKED --table "0 $((`blockdev --getsz $DEV2`-1)) linear $DEV2 0"
auto_sector 512 $DEV
auto_sector 512 $DEV $HDR
cleanup
echo "# Create enterprise-class 4K drive"
echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
add_device dev_size_mb=32 sector_size=4096 num_tgts=1 opt_blks=64
auto_sector 4096 $DEV
auto_sector 4096 $DEV $HDR
cleanup