111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw auto 324342f2cSKevin Wolf# 424342f2cSKevin Wolf# qcow2 format input validation tests 524342f2cSKevin Wolf# 624342f2cSKevin Wolf# Copyright (C) 2013 Red Hat, Inc. 724342f2cSKevin Wolf# 824342f2cSKevin Wolf# This program is free software; you can redistribute it and/or modify 924342f2cSKevin Wolf# it under the terms of the GNU General Public License as published by 1024342f2cSKevin Wolf# the Free Software Foundation; either version 2 of the License, or 1124342f2cSKevin Wolf# (at your option) any later version. 1224342f2cSKevin Wolf# 1324342f2cSKevin Wolf# This program is distributed in the hope that it will be useful, 1424342f2cSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 1524342f2cSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1624342f2cSKevin Wolf# GNU General Public License for more details. 1724342f2cSKevin Wolf# 1824342f2cSKevin Wolf# You should have received a copy of the GNU General Public License 1924342f2cSKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 2024342f2cSKevin Wolf# 2124342f2cSKevin Wolf 2224342f2cSKevin Wolf# creator 2324342f2cSKevin Wolfowner=kwolf@redhat.com 2424342f2cSKevin Wolf 2524342f2cSKevin Wolfseq=`basename $0` 2624342f2cSKevin Wolfecho "QA output created by $seq" 2724342f2cSKevin Wolf 2824342f2cSKevin Wolfstatus=1 # failure is the default! 2924342f2cSKevin Wolf 3024342f2cSKevin Wolf_cleanup() 3124342f2cSKevin Wolf{ 32f91ecbd7SMax Reitz _rm_test_img "$TEST_IMG.snap" 3324342f2cSKevin Wolf _cleanup_test_img 3424342f2cSKevin Wolf} 3524342f2cSKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 3624342f2cSKevin Wolf 3724342f2cSKevin Wolf# get standard environment, filters and checks 3824342f2cSKevin Wolf. ./common.rc 3924342f2cSKevin Wolf. ./common.filter 4024342f2cSKevin Wolf 4124342f2cSKevin Wolf_supported_fmt qcow2 4257284d2aSMax Reitz_supported_proto file fuse 4324342f2cSKevin Wolf_supported_os Linux 443be2024aSMax Reitz# - Internal snapshots are (currently) impossible with refcount_bits=1, 453be2024aSMax Reitz# and generally impossible with external data files 4694254c9bSMax Reitz# - This is generally a test for compat=1.1 images 473be2024aSMax Reitz_unsupported_imgopts 'refcount_bits=1[^0-9]' data_file 'compat=0.10' 4824342f2cSKevin Wolf 49572ad978SDenis Plotnikovheader_size=112 50a1b3955cSKevin Wolf 51a1b3955cSKevin Wolfoffset_backing_file_offset=8 526d33e8e7SKevin Wolfoffset_backing_file_size=16 532d51c32cSKevin Wolfoffset_l1_size=36 542d51c32cSKevin Wolfoffset_l1_table_offset=40 558c7de283SKevin Wolfoffset_refcount_table_offset=48 565dab2fadSKevin Wolfoffset_refcount_table_clusters=56 57ce48f2f4SKevin Wolfoffset_nb_snapshots=60 58ce48f2f4SKevin Wolfoffset_snapshots_offset=64 5924342f2cSKevin Wolfoffset_header_size=100 6024342f2cSKevin Wolfoffset_ext_magic=$header_size 6124342f2cSKevin Wolfoffset_ext_size=$((header_size + 4)) 6224342f2cSKevin Wolf 63b106ad91SKevin Wolfoffset_l2_table_0=$((0x40000)) 64b106ad91SKevin Wolf 656a83f8b5SKevin Wolfoffset_snap1=$((0x70000)) 666a83f8b5SKevin Wolfoffset_snap1_l1_offset=$((offset_snap1 + 0)) 676a83f8b5SKevin Wolfoffset_snap1_l1_size=$((offset_snap1 + 8)) 686a83f8b5SKevin Wolf 6924342f2cSKevin Wolfecho 7024342f2cSKevin Wolfecho "== Huge header size ==" 7124342f2cSKevin Wolf_make_test_img 64M 7224342f2cSKevin Wolfpoke_file "$TEST_IMG" "$offset_header_size" "\xff\xff\xff\xff" 7324342f2cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 7424342f2cSKevin Wolfpoke_file "$TEST_IMG" "$offset_header_size" "\x7f\xff\xff\xff" 7524342f2cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 7624342f2cSKevin Wolf 77a1b3955cSKevin Wolfecho 78a1b3955cSKevin Wolfecho "== Huge unknown header extension ==" 79a1b3955cSKevin Wolf_make_test_img 64M 80a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\xff\xff\xff\xff\xff\xff\xff\xff" 81a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_ext_magic" "\x12\x34\x56\x78" 82a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_ext_size" "\x7f\xff\xff\xff" 83a1b3955cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 842ebafc85SKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x$(printf %x $offset_ext_size)" 852ebafc85SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 86a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x00" 87a1b3955cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 88a1b3955cSKevin Wolf 895dab2fadSKevin Wolfecho 905dab2fadSKevin Wolfecho "== Huge refcount table size ==" 915dab2fadSKevin Wolf_make_test_img 64M 925dab2fadSKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\xff\xff\xff\xff" 935dab2fadSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 945dab2fadSKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x02\x00\x01" 955dab2fadSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 965dab2fadSKevin Wolf 978c7de283SKevin Wolfecho 988c7de283SKevin Wolfecho "== Misaligned refcount table ==" 998c7de283SKevin Wolf_make_test_img 64M 1008c7de283SKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef" 1018c7de283SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1028c7de283SKevin Wolf 1038c7de283SKevin Wolfecho 1048c7de283SKevin Wolfecho "== Huge refcount offset ==" 1058c7de283SKevin Wolf_make_test_img 64M 1068c7de283SKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_offset" "\xff\xff\xff\xff\xff\xff\x00\x00" 1078c7de283SKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x00\x00\x7f" 1088c7de283SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1095dab2fadSKevin Wolf 110ce48f2f4SKevin Wolfecho 111ce48f2f4SKevin Wolfecho "== Invalid snapshot table ==" 112ce48f2f4SKevin Wolf_make_test_img 64M 113ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\xff\xff\xff\xff" 114ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 115ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x7f\xff\xff\xff" 116ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 117ce48f2f4SKevin Wolf 118ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_snapshots_offset" "\xff\xff\xff\xff\xff\xff\x00\x00" 119ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\xff\xff" 120ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 121ce48f2f4SKevin Wolf 122ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_snapshots_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef" 123ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\x00\x00" 124ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 125ce48f2f4SKevin Wolf 126ce48f2f4SKevin Wolfecho 127ce48f2f4SKevin Wolfecho "== Hitting snapshot table size limit ==" 128ce48f2f4SKevin Wolf_make_test_img 64M 129ce48f2f4SKevin Wolf# Put the refcount table in a more or less safe place (16 MB) 130ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_snapshots_offset" "\x00\x00\x00\x00\x01\x00\x00\x00" 131ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x01\x00\x00" 132ce48f2f4SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir 133ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 134ce48f2f4SKevin Wolf 1352d51c32cSKevin Wolfecho 1362d51c32cSKevin Wolfecho "== Invalid L1 table ==" 1372d51c32cSKevin Wolf_make_test_img 64M 1382d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\xff\xff\xff\xff" 1392d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1402d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x7f\xff\xff\xff" 1412d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1422d51c32cSKevin Wolf 1432d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_table_offset" "\x7f\xff\xff\xff\xff\xff\x00\x00" 1442d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\xff\xff" 1452d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1462d51c32cSKevin Wolf 1472d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef" 1482d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x01" 1492d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1502d51c32cSKevin Wolf 1516d33e8e7SKevin Wolfecho 15211b128f4SKevin Wolfecho "== Invalid L1 table (with internal snapshot in the image) ==" 15311b128f4SKevin Wolf_make_test_img 64M 15411b128f4SKevin Wolf{ $QEMU_IMG snapshot -c foo $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 15511b128f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x00" 15611b128f4SKevin Wolf_img_info 15711b128f4SKevin Wolf 15811b128f4SKevin Wolfecho 1596d33e8e7SKevin Wolfecho "== Invalid backing file size ==" 1606d33e8e7SKevin Wolf_make_test_img 64M 1616d33e8e7SKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x10\x00" 1626d33e8e7SKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_size" "\xff\xff\xff\xff" 1636d33e8e7SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1646d33e8e7SKevin Wolf 165b106ad91SKevin Wolfecho 166b106ad91SKevin Wolfecho "== Invalid L2 entry (huge physical offset) ==" 167b106ad91SKevin Wolf_make_test_img 64M 168b106ad91SKevin Wolf{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 169b106ad91SKevin Wolfpoke_file "$TEST_IMG" "$offset_l2_table_0" "\xbf\xff\xff\xff\xff\xff\x00\x00" 170b106ad91SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 171b106ad91SKevin Wolfpoke_file "$TEST_IMG" "$offset_l2_table_0" "\x80\x00\x00\xff\xff\xff\x00\x00" 172b106ad91SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 173b106ad91SKevin Wolf 1746a83f8b5SKevin Wolfecho 175314e8d39SAlberto Garciaecho "== Invalid snapshot L1 table offset ==" 176314e8d39SAlberto Garcia_make_test_img 64M 177314e8d39SAlberto Garcia{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 178314e8d39SAlberto Garcia{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir 179314e8d39SAlberto Garciapoke_file "$TEST_IMG" "$offset_snap1_l1_offset" "\x00\x00\x00\x00\x00\x40\x02\x00" 18046e8d272SThomas Huth{ $QEMU_IMG convert -l test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_testdir 181c9a442e4SAlberto Garcia{ $QEMU_IMG amend -o compat=0.10 $TEST_IMG; } 2>&1 | _filter_testdir 182c7a9d81dSAlberto Garcia{ $QEMU_IO -c "open -o overlap-check.inactive-l2=on $TEST_IMG" \ 183c7a9d81dSAlberto Garcia -c 'write 0 4k'; } 2>&1 | _filter_qemu_io | _filter_testdir 184a8475d75SAlberto Garcia{ $QEMU_IMG snapshot -a test $TEST_IMG; } 2>&1 | _filter_testdir 185db5794f1SAlberto Garcia{ $QEMU_IMG snapshot -d test $TEST_IMG; } 2>&1 | _filter_testdir 1860c2ada81SAlberto Garcia_check_test_img 187314e8d39SAlberto Garcia 188314e8d39SAlberto Garciaecho 189314e8d39SAlberto Garciaecho "== Invalid snapshot L1 table size ==" 1906a83f8b5SKevin Wolf_make_test_img 64M 1916a83f8b5SKevin Wolf{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1926a83f8b5SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir 1936a83f8b5SKevin Wolfpoke_file "$TEST_IMG" "$offset_snap1_l1_size" "\x10\x00\x00\x00" 19446e8d272SThomas Huth{ $QEMU_IMG convert -l test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_testdir 195c9a442e4SAlberto Garcia{ $QEMU_IMG amend -o compat=0.10 $TEST_IMG; } 2>&1 | _filter_testdir 196c7a9d81dSAlberto Garcia{ $QEMU_IO -c "open -o overlap-check.inactive-l2=on $TEST_IMG" \ 197c7a9d81dSAlberto Garcia -c 'write 0 4k'; } 2>&1 | _filter_qemu_io | _filter_testdir 198a8475d75SAlberto Garcia{ $QEMU_IMG snapshot -a test $TEST_IMG; } 2>&1 | _filter_testdir 199db5794f1SAlberto Garcia{ $QEMU_IMG snapshot -d test $TEST_IMG; } 2>&1 | _filter_testdir 2000c2ada81SAlberto Garcia_check_test_img 2016a83f8b5SKevin Wolf 20224342f2cSKevin Wolf# success, all done 20324342f2cSKevin Wolfecho "*** done" 20424342f2cSKevin Wolfrm -f $seq.full 20524342f2cSKevin Wolfstatus=0 206