124342f2cSKevin Wolf#!/bin/bash 224342f2cSKevin Wolf# 324342f2cSKevin Wolf# qcow2 format input validation tests 424342f2cSKevin Wolf# 524342f2cSKevin Wolf# Copyright (C) 2013 Red Hat, Inc. 624342f2cSKevin Wolf# 724342f2cSKevin Wolf# This program is free software; you can redistribute it and/or modify 824342f2cSKevin Wolf# it under the terms of the GNU General Public License as published by 924342f2cSKevin Wolf# the Free Software Foundation; either version 2 of the License, or 1024342f2cSKevin Wolf# (at your option) any later version. 1124342f2cSKevin Wolf# 1224342f2cSKevin Wolf# This program is distributed in the hope that it will be useful, 1324342f2cSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 1424342f2cSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1524342f2cSKevin Wolf# GNU General Public License for more details. 1624342f2cSKevin Wolf# 1724342f2cSKevin Wolf# You should have received a copy of the GNU General Public License 1824342f2cSKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 1924342f2cSKevin Wolf# 2024342f2cSKevin Wolf 2124342f2cSKevin Wolf# creator 2224342f2cSKevin Wolfowner=kwolf@redhat.com 2324342f2cSKevin Wolf 2424342f2cSKevin Wolfseq=`basename $0` 2524342f2cSKevin Wolfecho "QA output created by $seq" 2624342f2cSKevin Wolf 2724342f2cSKevin Wolfhere=`pwd` 2824342f2cSKevin Wolftmp=/tmp/$$ 2924342f2cSKevin Wolfstatus=1 # failure is the default! 3024342f2cSKevin Wolf 3124342f2cSKevin Wolf_cleanup() 3224342f2cSKevin Wolf{ 336a83f8b5SKevin Wolf rm -f $TEST_IMG.snap 3424342f2cSKevin Wolf _cleanup_test_img 3524342f2cSKevin Wolf} 3624342f2cSKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 3724342f2cSKevin Wolf 3824342f2cSKevin Wolf# get standard environment, filters and checks 3924342f2cSKevin Wolf. ./common.rc 4024342f2cSKevin Wolf. ./common.filter 4124342f2cSKevin Wolf 4224342f2cSKevin Wolf_supported_fmt qcow2 43c5f7c0afSPeter Lieven_supported_proto file 4424342f2cSKevin Wolf_supported_os Linux 4524342f2cSKevin Wolf 4624342f2cSKevin Wolfheader_size=104 47a1b3955cSKevin Wolf 48a1b3955cSKevin Wolfoffset_backing_file_offset=8 496d33e8e7SKevin Wolfoffset_backing_file_size=16 502d51c32cSKevin Wolfoffset_l1_size=36 512d51c32cSKevin Wolfoffset_l1_table_offset=40 528c7de283SKevin Wolfoffset_refcount_table_offset=48 535dab2fadSKevin Wolfoffset_refcount_table_clusters=56 54ce48f2f4SKevin Wolfoffset_nb_snapshots=60 55ce48f2f4SKevin Wolfoffset_snapshots_offset=64 5624342f2cSKevin Wolfoffset_header_size=100 5724342f2cSKevin Wolfoffset_ext_magic=$header_size 5824342f2cSKevin Wolfoffset_ext_size=$((header_size + 4)) 5924342f2cSKevin Wolf 60b106ad91SKevin Wolfoffset_l2_table_0=$((0x40000)) 61b106ad91SKevin Wolf 626a83f8b5SKevin Wolfoffset_snap1=$((0x70000)) 636a83f8b5SKevin Wolfoffset_snap1_l1_offset=$((offset_snap1 + 0)) 646a83f8b5SKevin Wolfoffset_snap1_l1_size=$((offset_snap1 + 8)) 656a83f8b5SKevin Wolf 6624342f2cSKevin Wolfecho 6724342f2cSKevin Wolfecho "== Huge header size ==" 6824342f2cSKevin Wolf_make_test_img 64M 6924342f2cSKevin Wolfpoke_file "$TEST_IMG" "$offset_header_size" "\xff\xff\xff\xff" 7024342f2cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 7124342f2cSKevin Wolfpoke_file "$TEST_IMG" "$offset_header_size" "\x7f\xff\xff\xff" 7224342f2cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 7324342f2cSKevin Wolf 74a1b3955cSKevin Wolfecho 75a1b3955cSKevin Wolfecho "== Huge unknown header extension ==" 76a1b3955cSKevin Wolf_make_test_img 64M 77a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\xff\xff\xff\xff\xff\xff\xff\xff" 78a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_ext_magic" "\x12\x34\x56\x78" 79a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_ext_size" "\x7f\xff\xff\xff" 80a1b3955cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 81*2ebafc85SKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x$(printf %x $offset_ext_size)" 82*2ebafc85SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 83a1b3955cSKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x00" 84a1b3955cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 85a1b3955cSKevin Wolf 865dab2fadSKevin Wolfecho 875dab2fadSKevin Wolfecho "== Huge refcount table size ==" 885dab2fadSKevin Wolf_make_test_img 64M 895dab2fadSKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\xff\xff\xff\xff" 905dab2fadSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 915dab2fadSKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x02\x00\x01" 925dab2fadSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 935dab2fadSKevin Wolf 948c7de283SKevin Wolfecho 958c7de283SKevin Wolfecho "== Misaligned refcount table ==" 968c7de283SKevin Wolf_make_test_img 64M 978c7de283SKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef" 988c7de283SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 998c7de283SKevin Wolf 1008c7de283SKevin Wolfecho 1018c7de283SKevin Wolfecho "== Huge refcount offset ==" 1028c7de283SKevin Wolf_make_test_img 64M 1038c7de283SKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_offset" "\xff\xff\xff\xff\xff\xff\x00\x00" 1048c7de283SKevin Wolfpoke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x00\x00\x7f" 1058c7de283SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1065dab2fadSKevin Wolf 107ce48f2f4SKevin Wolfecho 108ce48f2f4SKevin Wolfecho "== Invalid snapshot table ==" 109ce48f2f4SKevin Wolf_make_test_img 64M 110ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\xff\xff\xff\xff" 111ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 112ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x7f\xff\xff\xff" 113ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 114ce48f2f4SKevin Wolf 115ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_snapshots_offset" "\xff\xff\xff\xff\xff\xff\x00\x00" 116ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\xff\xff" 117ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 118ce48f2f4SKevin Wolf 119ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_snapshots_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef" 120ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\x00\x00" 121ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 122ce48f2f4SKevin Wolf 123ce48f2f4SKevin Wolfecho 124ce48f2f4SKevin Wolfecho "== Hitting snapshot table size limit ==" 125ce48f2f4SKevin Wolf_make_test_img 64M 126ce48f2f4SKevin Wolf# Put the refcount table in a more or less safe place (16 MB) 127ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_snapshots_offset" "\x00\x00\x00\x00\x01\x00\x00\x00" 128ce48f2f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x01\x00\x00" 129ce48f2f4SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir 130ce48f2f4SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 131ce48f2f4SKevin Wolf 1322d51c32cSKevin Wolfecho 1332d51c32cSKevin Wolfecho "== Invalid L1 table ==" 1342d51c32cSKevin Wolf_make_test_img 64M 1352d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\xff\xff\xff\xff" 1362d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1372d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x7f\xff\xff\xff" 1382d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1392d51c32cSKevin Wolf 1402d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_table_offset" "\x7f\xff\xff\xff\xff\xff\x00\x00" 1412d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\xff\xff" 1422d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1432d51c32cSKevin Wolf 1442d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef" 1452d51c32cSKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x01" 1462d51c32cSKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1472d51c32cSKevin Wolf 1486d33e8e7SKevin Wolfecho 14911b128f4SKevin Wolfecho "== Invalid L1 table (with internal snapshot in the image) ==" 15011b128f4SKevin Wolf_make_test_img 64M 15111b128f4SKevin Wolf{ $QEMU_IMG snapshot -c foo $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 15211b128f4SKevin Wolfpoke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x00" 15311b128f4SKevin Wolf_img_info 15411b128f4SKevin Wolf 15511b128f4SKevin Wolfecho 1566d33e8e7SKevin Wolfecho "== Invalid backing file size ==" 1576d33e8e7SKevin Wolf_make_test_img 64M 1586d33e8e7SKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x10\x00" 1596d33e8e7SKevin Wolfpoke_file "$TEST_IMG" "$offset_backing_file_size" "\xff\xff\xff\xff" 1606d33e8e7SKevin Wolf{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1616d33e8e7SKevin Wolf 162b106ad91SKevin Wolfecho 163b106ad91SKevin Wolfecho "== Invalid L2 entry (huge physical offset) ==" 164b106ad91SKevin Wolf_make_test_img 64M 165b106ad91SKevin Wolf{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 166b106ad91SKevin Wolfpoke_file "$TEST_IMG" "$offset_l2_table_0" "\xbf\xff\xff\xff\xff\xff\x00\x00" 167b106ad91SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 168b106ad91SKevin Wolfpoke_file "$TEST_IMG" "$offset_l2_table_0" "\x80\x00\x00\xff\xff\xff\x00\x00" 169b106ad91SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 170b106ad91SKevin Wolf 1716a83f8b5SKevin Wolfecho 1726a83f8b5SKevin Wolfecho "== Invalid snapshot L1 table ==" 1736a83f8b5SKevin Wolf_make_test_img 64M 1746a83f8b5SKevin Wolf{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir 1756a83f8b5SKevin Wolf{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir 1766a83f8b5SKevin Wolfpoke_file "$TEST_IMG" "$offset_snap1_l1_size" "\x10\x00\x00\x00" 1776a83f8b5SKevin Wolf{ $QEMU_IMG convert -s test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_testdir 1786a83f8b5SKevin Wolf 17924342f2cSKevin Wolf# success, all done 18024342f2cSKevin Wolfecho "*** done" 18124342f2cSKevin Wolfrm -f $seq.full 18224342f2cSKevin Wolfstatus=0 183