Skip to content

Commit fcf2666

Browse files
onikombroz
authored andcommitted
Add validation tests for json area in non compact version.
The non compact json area may contiain whitespace characters in between json object key and value (e.g.: {"key": "the_value"}). For LUKS2 write optimization we need to check and do regression testing for the case where LUKS2 metadata would contain valid LUKS2 json area in non compact format. The test is meant to verify if the write optimization does not leave invalid characters beyond valid and properly terminated LUKS2 json area.
1 parent cb0f568 commit fcf2666

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
. lib.sh
4+
5+
#
6+
# *** Description ***
7+
#
8+
# generate LUKS2 header with non compact (valid!)
9+
# json and additional token with id 0 with json
10+
# format aligned to 4K boundary.
11+
#
12+
# The image is tested for correct LUKS2 write optimization
13+
# where non compact json trailing bytes must not remain in LUKS2 json
14+
# area after write of shorter (e.g. compact) json.
15+
16+
# $1 full target dir
17+
# $2 full source luks2 image
18+
19+
generate()
20+
{
21+
# add empty token
22+
json_str=$(jq -c '.tokens."0" = {"type":"a", "keyslots":[]}' $TMPDIR/json0)
23+
json_len_orig=${#json_str}
24+
test $json_len_orig -lt $((LUKS2_JSON_SIZE*512)) || exit 2
25+
26+
# align to 4k and full 4K of whitespace if already aligned
27+
json_fill_len=$((4096-(json_len_orig%4096)))
28+
fill=$(repeat_str " " $json_fill_len)
29+
json_str_new=$(echo -e $json_str | sed -e "s/\(\"type\":\)\(\"luks2\"\)/\1""$fill""\2/")
30+
json_len_new=${#json_str_new}
31+
32+
test $((json_len_new%4096)) -eq 0 || exit 2
33+
test $json_len_new -lt $((LUKS2_JSON_SIZE*512)) || exit 2
34+
test $json_len_new -gt $json_len_orig || exit 2
35+
36+
printf '%s' "$json_str_new" | _dd of=$TMPDIR/json0 bs=4K conv=notrunc
37+
printf '%s' "$json_str_new" | _dd of=$TMPDIR/json1 bs=4K conv=notrunc
38+
39+
lib_mangle_json_hdr0
40+
lib_mangle_json_hdr1
41+
}
42+
43+
check()
44+
{
45+
lib_hdr0_checksum || exit 2
46+
lib_hdr1_checksum || exit 2
47+
48+
read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
49+
50+
read -r json_str_res < $TMPDIR/json_res0
51+
test $((${#json_str_res}%4096)) -eq 0 || exit 2
52+
test ${#json_str_res} -gt $json_len_orig || exit 2
53+
}
54+
55+
lib_prepare $@
56+
generate
57+
check
58+
lib_cleanup
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
. lib.sh
4+
5+
#
6+
# *** Description ***
7+
#
8+
# generate LUKS2 header with non compact (valid!)
9+
# json and additional token with id 0.
10+
#
11+
# The image is tested for correct LUKS2 write optimization
12+
# where non compact json trailing bytes must not remain in LUKS2 json
13+
# area after write of shorter (e.g. compact) json.
14+
15+
# $1 full target dir
16+
# $2 full source luks2 image
17+
18+
generate()
19+
{
20+
# add empty token
21+
json_str=$(jq -c '.tokens."0" = {"type":"a", "keyslots":[]}' $TMPDIR/json0)
22+
json_len_orig=${#json_str}
23+
test $json_len_orig -lt $((LUKS2_JSON_SIZE*512)) || exit 2
24+
25+
json_str_new=$(echo -n $json_str | sed -e 's/\(\"type\":\)\(\"luks2\"\)/\1 \2/')
26+
json_len_new=${#json_str_new}
27+
28+
test $json_len_new -lt $((LUKS2_JSON_SIZE*512)) || exit 2
29+
test $json_len_new -gt $json_len_orig || exit 2
30+
31+
printf '%s' "$json_str_new" | _dd of=$TMPDIR/json0 bs=1 conv=notrunc
32+
printf '%s' "$json_str_new" | _dd of=$TMPDIR/json1 bs=1 conv=notrunc
33+
34+
lib_mangle_json_hdr0
35+
lib_mangle_json_hdr1
36+
}
37+
38+
check()
39+
{
40+
lib_hdr0_checksum || exit 2
41+
lib_hdr1_checksum || exit 2
42+
43+
read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
44+
45+
read -r json_str_res < $TMPDIR/json_res0
46+
test ${#json_str_res} -gt $json_len_orig || exit 2
47+
}
48+
49+
lib_prepare $@
50+
generate
51+
check
52+
lib_cleanup

tests/luks2-validation-test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ test_load()
7373
test -z "$_DEBUG" || _debug="--debug"
7474

7575
case "$1" in
76+
T)
77+
if [ -n "$_debug" ]; then
78+
$CRYPTSETUP token remove --token-id 0 $_debug $IMG
79+
else
80+
$CRYPTSETUP token remove --token-id 0 $IMG > /dev/null 2>&1
81+
fi
82+
test $? -eq 0 || return 1
83+
if [ -n "$_debug" ]; then
84+
$CRYPTSETUP luksDump $_debug $IMG
85+
else
86+
$CRYPTSETUP luksDump $IMG > /dev/null 2>&1
87+
fi
88+
test $? -eq 0 || return 1
89+
;;
7690
R)
7791
if [ -n "$_debug" ]; then
7892
$CRYPTSETUP luksDump $_debug $IMG
@@ -255,6 +269,10 @@ RUN luks2-keyslot-invalid-area-size.img "F" "Invalid keyslot area size that ca
255269
RUN luks2-keyslot-invalid-objects.img "F" "Invalid keyslot objects not rejected"
256270
RUN luks2-keyslot-invalid-af.img "F" "Invalid keyslot objects types not rejected"
257271

272+
echo "[8] Test non compact json does not break write optimization"
273+
RUN luks2-non-compact-json-token-0.img "T" "Non compact json area corrupted after write"
274+
RUN luks2-non-compact-json-4k-token-0.img "T" "Non compact 4K aligned json area corrupted after write"
275+
258276
remove_mapping
259277

260278
test $FAILS -eq 0 || fail "($FAILS wrong result(s) in total)"

0 commit comments

Comments
 (0)