"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/generators/generate-luks2-metadata-size-invalid-secondary.img.sh" (13 Jan 2022, 2641 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/bash
    2 
    3 . lib.sh
    4 
    5 #
    6 # *** Description ***
    7 #
    8 # generate primary with predefined json_size. There's only limited
    9 # set of values allowed as json size in config section of LUKS2
   10 # metadata
   11 #
   12 # secondary header is corrupted on purpose as well
   13 #
   14 
   15 # $1 full target dir
   16 # $2 full source luks2 image
   17 
   18 function prepare()
   19 {
   20     cp $SRC_IMG $TGT_IMG
   21     test -d $TMPDIR || mkdir $TMPDIR
   22     read_luks2_json0 $TGT_IMG $TMPDIR/json0
   23     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
   24     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
   25 }
   26 
   27 function generate()
   28 {
   29     TEST_MDA_SIZE=$LUKS2_HDR_SIZE_1M
   30 
   31     TEST_MDA_SIZE_BYTES=$((TEST_MDA_SIZE*512))
   32     TEST_MDA_SIZE_BOGUS_BYTES=$((TEST_MDA_SIZE*512*2*1024))
   33     TEST_JSN_SIZE=$((TEST_MDA_SIZE-LUKS2_BIN_HDR_SIZE))
   34     KEYSLOTS_OFFSET=$((TEST_MDA_SIZE*1024))
   35     JSON_DIFF=$(((TEST_MDA_SIZE-LUKS2_HDR_SIZE)*1024))
   36     JSON_SIZE=$((TEST_JSN_SIZE*512))
   37     DATA_OFFSET=16777216
   38 
   39     json_str=$(jq -c --arg jdiff $JSON_DIFF --arg jsize $JSON_SIZE --arg off $DATA_OFFSET \
   40            '.keyslots[].area.offset |= ( . | tonumber + ($jdiff | tonumber) | tostring) |
   41             .config.json_size = $jsize |
   42             .segments."0".offset = $off' $TMPDIR/json0)
   43     test -n "$json_str" || exit 2
   44     test ${#json_str} -lt $((LUKS2_JSON_SIZE*512)) || exit 2
   45 
   46     write_luks2_json "$json_str" $TMPDIR/json0 $TEST_JSN_SIZE
   47 
   48     write_bin_hdr_size $TMPDIR/hdr0 $TEST_MDA_SIZE_BYTES
   49     write_bin_hdr_size $TMPDIR/hdr1 $TEST_MDA_SIZE_BOGUS_BYTES
   50 
   51     write_bin_hdr_offset $TMPDIR/hdr1 $TEST_MDA_SIZE_BYTES
   52 
   53     merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0 $TEST_JSN_SIZE
   54     merge_bin_hdr_with_json $TMPDIR/hdr1 $TMPDIR/json0 $TMPDIR/area1 $TEST_JSN_SIZE
   55 
   56     erase_checksum $TMPDIR/area0
   57     chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
   58     write_checksum $chks0 $TMPDIR/area0
   59 
   60     erase_checksum $TMPDIR/area1
   61     chks0=$(calc_sha256_checksum_file $TMPDIR/area1)
   62     write_checksum $chks0 $TMPDIR/area1
   63 
   64     kill_bin_hdr $TMPDIR/area0
   65 
   66     write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG $TEST_MDA_SIZE
   67     write_luks2_hdr1 $TMPDIR/area1 $TGT_IMG $TEST_MDA_SIZE
   68 }
   69 
   70 function check()
   71 {
   72     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr_res0 $TEST_MDA_SIZE
   73     local str_res0=$(head -c 6 $TMPDIR/hdr_res0)
   74     test "$str_res0" = "VACUUM" || exit 2
   75     read_luks2_json1 $TGT_IMG $TMPDIR/json_res1 $TEST_JSN_SIZE
   76     jq -c --arg koff $KEYSLOTS_OFFSET --arg jsize $JSON_SIZE \
   77         'if ([.keyslots[].area.offset] | map(tonumber) | min | tostring != $koff) or
   78             (.config.json_size != $jsize)
   79         then error("Unexpected value in result json") else empty end' $TMPDIR/json_res1 || exit 5
   80 }
   81 
   82 function cleanup()
   83 {
   84     rm -f $TMPDIR/*
   85     rm -fd $TMPDIR
   86 }
   87 
   88 test $# -eq 2 || exit 1
   89 
   90 TGT_IMG=$1/$(test_img_name $0)
   91 SRC_IMG=$2
   92 
   93 prepare
   94 generate
   95 check
   96 cleanup