blob: 7588c63b6c428ed7f4586bcf208930b925730879 [file] [log] [blame]
Philippe Mathieu-Daudé11a82d12019-03-07 15:58:38 +01001#!/usr/bin/env bash
Kevin Wolf24342f22014-03-26 13:05:41 +01002#
3# qcow2 format input validation tests
4#
5# Copyright (C) 2013 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20
21# creator
22owner=kwolf@redhat.com
23
24seq=`basename $0`
25echo "QA output created by $seq"
26
Kevin Wolf24342f22014-03-26 13:05:41 +010027status=1 # failure is the default!
28
29_cleanup()
30{
Max Reitzf91ecbd2019-11-07 17:37:01 +010031 _rm_test_img "$TEST_IMG.snap"
Kevin Wolf6a83f8b2014-03-26 13:06:06 +010032 _cleanup_test_img
Kevin Wolf24342f22014-03-26 13:05:41 +010033}
34trap "_cleanup; exit \$status" 0 1 2 3 15
35
36# get standard environment, filters and checks
37. ./common.rc
38. ./common.filter
39
40_supported_fmt qcow2
Peter Lievenc5f7c0a2014-10-20 13:47:11 +020041_supported_proto file
Kevin Wolf24342f22014-03-26 13:05:41 +010042_supported_os Linux
Max Reitz3be20242019-11-07 17:37:07 +010043# - Internal snapshots are (currently) impossible with refcount_bits=1,
44# and generally impossible with external data files
Max Reitz94254c92018-01-17 10:54:20 -060045# - This is generally a test for compat=1.1 images
Max Reitz3be20242019-11-07 17:37:07 +010046_unsupported_imgopts 'refcount_bits=1[^0-9]' data_file 'compat=0.10'
Kevin Wolf24342f22014-03-26 13:05:41 +010047
Denis Plotnikov572ad972020-05-07 11:25:18 +030048header_size=112
Kevin Wolfa1b3955c2014-03-26 13:05:42 +010049
50offset_backing_file_offset=8
Kevin Wolf6d33e8e2014-03-26 13:05:47 +010051offset_backing_file_size=16
Kevin Wolf2d51c322014-03-26 13:05:46 +010052offset_l1_size=36
53offset_l1_table_offset=40
Kevin Wolf8c7de282014-03-26 13:05:44 +010054offset_refcount_table_offset=48
Kevin Wolf5dab2fa2014-03-26 13:05:43 +010055offset_refcount_table_clusters=56
Kevin Wolfce48f2f42014-03-26 13:05:45 +010056offset_nb_snapshots=60
57offset_snapshots_offset=64
Kevin Wolf24342f22014-03-26 13:05:41 +010058offset_header_size=100
59offset_ext_magic=$header_size
60offset_ext_size=$((header_size + 4))
61
Kevin Wolfb106ad92014-03-28 18:06:31 +010062offset_l2_table_0=$((0x40000))
63
Kevin Wolf6a83f8b2014-03-26 13:06:06 +010064offset_snap1=$((0x70000))
65offset_snap1_l1_offset=$((offset_snap1 + 0))
66offset_snap1_l1_size=$((offset_snap1 + 8))
67
Kevin Wolf24342f22014-03-26 13:05:41 +010068echo
69echo "== Huge header size =="
70_make_test_img 64M
71poke_file "$TEST_IMG" "$offset_header_size" "\xff\xff\xff\xff"
72{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
73poke_file "$TEST_IMG" "$offset_header_size" "\x7f\xff\xff\xff"
74{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
75
Kevin Wolfa1b3955c2014-03-26 13:05:42 +010076echo
77echo "== Huge unknown header extension =="
78_make_test_img 64M
79poke_file "$TEST_IMG" "$offset_backing_file_offset" "\xff\xff\xff\xff\xff\xff\xff\xff"
80poke_file "$TEST_IMG" "$offset_ext_magic" "\x12\x34\x56\x78"
81poke_file "$TEST_IMG" "$offset_ext_size" "\x7f\xff\xff\xff"
82{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
Kevin Wolf2ebafc82014-11-25 18:12:40 +010083poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x$(printf %x $offset_ext_size)"
84{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
Kevin Wolfa1b3955c2014-03-26 13:05:42 +010085poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x00"
86{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
87
Kevin Wolf5dab2fa2014-03-26 13:05:43 +010088echo
89echo "== Huge refcount table size =="
90_make_test_img 64M
91poke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\xff\xff\xff\xff"
92{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
93poke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x02\x00\x01"
94{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
95
Kevin Wolf8c7de282014-03-26 13:05:44 +010096echo
97echo "== Misaligned refcount table =="
98_make_test_img 64M
99poke_file "$TEST_IMG" "$offset_refcount_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef"
100{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
101
102echo
103echo "== Huge refcount offset =="
104_make_test_img 64M
105poke_file "$TEST_IMG" "$offset_refcount_table_offset" "\xff\xff\xff\xff\xff\xff\x00\x00"
106poke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x00\x00\x7f"
107{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
Kevin Wolf5dab2fa2014-03-26 13:05:43 +0100108
Kevin Wolfce48f2f42014-03-26 13:05:45 +0100109echo
110echo "== Invalid snapshot table =="
111_make_test_img 64M
112poke_file "$TEST_IMG" "$offset_nb_snapshots" "\xff\xff\xff\xff"
113{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
114poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x7f\xff\xff\xff"
115{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
116
117poke_file "$TEST_IMG" "$offset_snapshots_offset" "\xff\xff\xff\xff\xff\xff\x00\x00"
118poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\xff\xff"
119{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
120
121poke_file "$TEST_IMG" "$offset_snapshots_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef"
122poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\x00\x00"
123{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
124
125echo
126echo "== Hitting snapshot table size limit =="
127_make_test_img 64M
128# Put the refcount table in a more or less safe place (16 MB)
129poke_file "$TEST_IMG" "$offset_snapshots_offset" "\x00\x00\x00\x00\x01\x00\x00\x00"
130poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x01\x00\x00"
131{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir
132{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
133
Kevin Wolf2d51c322014-03-26 13:05:46 +0100134echo
135echo "== Invalid L1 table =="
136_make_test_img 64M
137poke_file "$TEST_IMG" "$offset_l1_size" "\xff\xff\xff\xff"
138{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
139poke_file "$TEST_IMG" "$offset_l1_size" "\x7f\xff\xff\xff"
140{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
141
142poke_file "$TEST_IMG" "$offset_l1_table_offset" "\x7f\xff\xff\xff\xff\xff\x00\x00"
143poke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\xff\xff"
144{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
145
146poke_file "$TEST_IMG" "$offset_l1_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef"
147poke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x01"
148{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
149
Kevin Wolf6d33e8e2014-03-26 13:05:47 +0100150echo
Kevin Wolf11b128f2014-03-26 13:06:04 +0100151echo "== Invalid L1 table (with internal snapshot in the image) =="
152_make_test_img 64M
153{ $QEMU_IMG snapshot -c foo $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
154poke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x00"
155_img_info
156
157echo
Kevin Wolf6d33e8e2014-03-26 13:05:47 +0100158echo "== Invalid backing file size =="
159_make_test_img 64M
160poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x10\x00"
161poke_file "$TEST_IMG" "$offset_backing_file_size" "\xff\xff\xff\xff"
162{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
163
Kevin Wolfb106ad92014-03-28 18:06:31 +0100164echo
165echo "== Invalid L2 entry (huge physical offset) =="
166_make_test_img 64M
167{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
168poke_file "$TEST_IMG" "$offset_l2_table_0" "\xbf\xff\xff\xff\xff\xff\x00\x00"
169{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
170poke_file "$TEST_IMG" "$offset_l2_table_0" "\x80\x00\x00\xff\xff\xff\x00\x00"
171{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
172
Kevin Wolf6a83f8b2014-03-26 13:06:06 +0100173echo
Alberto Garcia314e8d32018-03-06 18:14:07 +0200174echo "== Invalid snapshot L1 table offset =="
175_make_test_img 64M
176{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
177{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir
178poke_file "$TEST_IMG" "$offset_snap1_l1_offset" "\x00\x00\x00\x00\x00\x40\x02\x00"
Thomas Huth46e8d272018-06-06 14:35:51 +0200179{ $QEMU_IMG convert -l test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_testdir
Alberto Garciac9a442e2018-03-06 18:14:08 +0200180{ $QEMU_IMG amend -o compat=0.10 $TEST_IMG; } 2>&1 | _filter_testdir
Alberto Garciac7a9d812018-03-06 18:14:09 +0200181{ $QEMU_IO -c "open -o overlap-check.inactive-l2=on $TEST_IMG" \
182 -c 'write 0 4k'; } 2>&1 | _filter_qemu_io | _filter_testdir
Alberto Garciaa8475d72018-03-06 18:14:10 +0200183{ $QEMU_IMG snapshot -a test $TEST_IMG; } 2>&1 | _filter_testdir
Alberto Garciadb5794f2018-03-06 18:14:11 +0200184{ $QEMU_IMG snapshot -d test $TEST_IMG; } 2>&1 | _filter_testdir
Alberto Garcia0c2ada82018-03-06 18:14:12 +0200185_check_test_img
Alberto Garcia314e8d32018-03-06 18:14:07 +0200186
187echo
188echo "== Invalid snapshot L1 table size =="
Kevin Wolf6a83f8b2014-03-26 13:06:06 +0100189_make_test_img 64M
190{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
191{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir
192poke_file "$TEST_IMG" "$offset_snap1_l1_size" "\x10\x00\x00\x00"
Thomas Huth46e8d272018-06-06 14:35:51 +0200193{ $QEMU_IMG convert -l test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_testdir
Alberto Garciac9a442e2018-03-06 18:14:08 +0200194{ $QEMU_IMG amend -o compat=0.10 $TEST_IMG; } 2>&1 | _filter_testdir
Alberto Garciac7a9d812018-03-06 18:14:09 +0200195{ $QEMU_IO -c "open -o overlap-check.inactive-l2=on $TEST_IMG" \
196 -c 'write 0 4k'; } 2>&1 | _filter_qemu_io | _filter_testdir
Alberto Garciaa8475d72018-03-06 18:14:10 +0200197{ $QEMU_IMG snapshot -a test $TEST_IMG; } 2>&1 | _filter_testdir
Alberto Garciadb5794f2018-03-06 18:14:11 +0200198{ $QEMU_IMG snapshot -d test $TEST_IMG; } 2>&1 | _filter_testdir
Alberto Garcia0c2ada82018-03-06 18:14:12 +0200199_check_test_img
Kevin Wolf6a83f8b2014-03-26 13:06:06 +0100200
Kevin Wolf24342f22014-03-26 13:05:41 +0100201# success, all done
202echo "*** done"
203rm -f $seq.full
204status=0