qemu/tests/qemu-iotests/153

279 lines
8.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
#
# Test image locking
#
# Copyright 2016, 2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# creator
owner=famz@redhat.com
seq="$(basename $0)"
echo "QA output created by $seq"
tmp=/tmp/$$
status=1 # failure is the default!
_cleanup()
{
_cleanup_test_img
for img in "${TEST_IMG}".{base,overlay,convert,a,b,c,lnk}; do
_rm_test_img "$img"
done
}
trap "_cleanup; exit \$status" 0 1 2 3 15
# get standard environment, filters and checks
. ./common.rc
. ./common.filter
. ./common.qemu
size=32M
_check_ofd()
{
_make_test_img $size >/dev/null
if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 |
grep -q 'falling back to POSIX file'; then
return 1
else
return 0
fi
}
_check_ofd || _notrun "OFD lock not available"
_supported_fmt qcow2
_supported_proto file
_run_cmd()
{
echo
(echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir
}
_do_run_qemu()
{
(
if ! test -t 0; then
while read cmd; do
echo $cmd
done
fi
echo quit
) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null
}
_run_qemu_with_images()
{
_do_run_qemu \
$(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \
| _filter_testdir | _filter_qemu
}
echo "== readonly=off,force-share=on should be rejected =="
_run_qemu_with_images null-co://,readonly=off,force-share=on
for opts1 in "" "read-only=on" "read-only=on,force-share=on"; do
echo
echo "== Creating base image =="
TEST_IMG="${TEST_IMG}.base" _make_test_img $size
echo
echo "== Creating test image =="
iotests: Specify explicit backing format where sensible There are many existing qcow2 images that specify a backing file but no format. This has been the source of CVEs in the past, but has become more prominent of a problem now that libvirt has switched to -blockdev. With older -drive, at least the probing was always done by qemu (so the only risk of a changed format between successive boots of a guest was if qemu was upgraded and probed differently). But with newer -blockdev, libvirt must specify a format; if libvirt guesses raw where the image was formatted, this results in data corruption visible to the guest; conversely, if libvirt guesses qcow2 where qemu was using raw, this can result in potential security holes, so modern libvirt instead refuses to use images without explicit backing format. The change in libvirt to reject images without explicit backing format has pointed out that a number of tools have been far too reliant on probing in the past. It's time to set a better example in our own iotests of properly setting this parameter. iotest calls to create, rebase, and convert are all impacted to some degree. It's a bit annoying that we are inconsistent on command line - while all of those accept -o backing_file=...,backing_fmt=..., the shortcuts are different: create and rebase have -b and -F, while convert has -B but no -F. (amend has no shortcuts, but the previous patch just deprecated the use of amend to change backing chains). Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20200706203954.341758-9-eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
_make_test_img -b "${TEST_IMG}.base" -F $IMGFMT
echo
echo "== Launching QEMU, opts: '$opts1' =="
_launch_qemu -drive file="${TEST_IMG}",if=none,$opts1
h=$QEMU_HANDLE
for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do
echo
echo "== Launching another QEMU, opts: '$opts2' =="
echo "quit" | \
$QEMU -nographic -monitor stdio \
-drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \
_filter_testdir | _filter_qemu
done
for L in "" "-U"; do
echo
echo "== Running utility commands $L =="
_run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}"
_run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}"
_run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512"
_run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512"
_run_cmd $QEMU_IMG info $L "${TEST_IMG}"
_run_cmd $QEMU_IMG check $L "${TEST_IMG}"
_run_cmd $QEMU_IMG compare $L "${TEST_IMG}" "${TEST_IMG}"
_run_cmd $QEMU_IMG map $L "${TEST_IMG}"
_run_cmd $QEMU_IMG amend -o "size=$size" $L "${TEST_IMG}"
_run_cmd $QEMU_IMG commit $L "${TEST_IMG}"
_run_cmd $QEMU_IMG resize $L "${TEST_IMG}" $size
iotests: Specify explicit backing format where sensible There are many existing qcow2 images that specify a backing file but no format. This has been the source of CVEs in the past, but has become more prominent of a problem now that libvirt has switched to -blockdev. With older -drive, at least the probing was always done by qemu (so the only risk of a changed format between successive boots of a guest was if qemu was upgraded and probed differently). But with newer -blockdev, libvirt must specify a format; if libvirt guesses raw where the image was formatted, this results in data corruption visible to the guest; conversely, if libvirt guesses qcow2 where qemu was using raw, this can result in potential security holes, so modern libvirt instead refuses to use images without explicit backing format. The change in libvirt to reject images without explicit backing format has pointed out that a number of tools have been far too reliant on probing in the past. It's time to set a better example in our own iotests of properly setting this parameter. iotest calls to create, rebase, and convert are all impacted to some degree. It's a bit annoying that we are inconsistent on command line - while all of those accept -o backing_file=...,backing_fmt=..., the shortcuts are different: create and rebase have -b and -F, while convert has -B but no -F. (amend has no shortcuts, but the previous patch just deprecated the use of amend to change backing chains). Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20200706203954.341758-9-eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
_run_cmd $QEMU_IMG rebase $L "${TEST_IMG}" -b "${TEST_IMG}.base" -F $IMGFMT
_run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}"
_run_cmd $QEMU_IMG convert $L "${TEST_IMG}" "${TEST_IMG}.convert"
_run_cmd $QEMU_IMG dd $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1
_run_cmd $QEMU_IMG bench $L -c 1 "${TEST_IMG}"
_run_cmd $QEMU_IMG bench $L -w -c 1 "${TEST_IMG}"
# qemu-img create does not support -U
if [ -z "$L" ]; then
_run_cmd $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" \
iotests: Specify explicit backing format where sensible There are many existing qcow2 images that specify a backing file but no format. This has been the source of CVEs in the past, but has become more prominent of a problem now that libvirt has switched to -blockdev. With older -drive, at least the probing was always done by qemu (so the only risk of a changed format between successive boots of a guest was if qemu was upgraded and probed differently). But with newer -blockdev, libvirt must specify a format; if libvirt guesses raw where the image was formatted, this results in data corruption visible to the guest; conversely, if libvirt guesses qcow2 where qemu was using raw, this can result in potential security holes, so modern libvirt instead refuses to use images without explicit backing format. The change in libvirt to reject images without explicit backing format has pointed out that a number of tools have been far too reliant on probing in the past. It's time to set a better example in our own iotests of properly setting this parameter. iotest calls to create, rebase, and convert are all impacted to some degree. It's a bit annoying that we are inconsistent on command line - while all of those accept -o backing_file=...,backing_fmt=..., the shortcuts are different: create and rebase have -b and -F, while convert has -B but no -F. (amend has no shortcuts, but the previous patch just deprecated the use of amend to change backing chains). Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20200706203954.341758-9-eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
-b ${TEST_IMG}.base -F $IMGFMT
# Read the file format. It used to be the case that
# file-posix simply truncated the file, but the qcow2
# driver then failed to format it because it was unable
# to acquire the necessary WRITE permission. However, the
# truncation was already wrong, and the whole process
# resulted in the file being completely empty and thus its
# format would be detected to be raw.
# So we read it here to see that creation either completed
# successfully (thus the format is qcow2) or it aborted
# before the file was changed at all (thus the format stays
# qcow2).
_img_info -U | grep 'file format'
fi
done
_send_qemu_cmd $h "{ 'execute': 'quit' }" ''
echo
echo "Round done"
_cleanup_qemu
done
test_opts="read-only=off read-only=on read-only=on,force-share=on"
for opt1 in $test_opts; do
for opt2 in $test_opts; do
echo
echo "== Two devices with the same image ($opt1 - $opt2) =="
_run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2"
done
done
echo
echo "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir
iotests: Specify explicit backing format where sensible There are many existing qcow2 images that specify a backing file but no format. This has been the source of CVEs in the past, but has become more prominent of a problem now that libvirt has switched to -blockdev. With older -drive, at least the probing was always done by qemu (so the only risk of a changed format between successive boots of a guest was if qemu was upgraded and probed differently). But with newer -blockdev, libvirt must specify a format; if libvirt guesses raw where the image was formatted, this results in data corruption visible to the guest; conversely, if libvirt guesses qcow2 where qemu was using raw, this can result in potential security holes, so modern libvirt instead refuses to use images without explicit backing format. The change in libvirt to reject images without explicit backing format has pointed out that a number of tools have been far too reliant on probing in the past. It's time to set a better example in our own iotests of properly setting this parameter. iotest calls to create, rebase, and convert are all impacted to some degree. It's a bit annoying that we are inconsistent on command line - while all of those accept -o backing_file=...,backing_fmt=..., the shortcuts are different: create and rebase have -b and -F, while convert has -B but no -F. (amend has no shortcuts, but the previous patch just deprecated the use of amend to change backing chains). Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20200706203954.341758-9-eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
$QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}" -F $IMGFMT | _filter_img_create
$QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}" -F $IMGFMT | _filter_img_create
$QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b" -F $IMGFMT \
iotests: Make _filter_img_create more active Right now, _filter_img_create just filters out everything that looks format-dependent, and applies some filename filters. That means that we have to add another filter line every time some format gets a new creation option. This can be avoided by instead discarding everything and just keeping what we know is format-independent (format, size, backing file, encryption information[1], preallocation) or just interesting to have in the reference output (external data file path). Furthermore, we probably want to sort these options. Format drivers are not required to define them in any specific order, so the output is effectively random (although this has never bothered us until now). We need a specific order for our reference outputs, though. Unfortunately, just using a plain "sort" would change a lot of existing reference outputs, so we have to pre-filter the option keys to keep our existing order (fmt, size, backing*, data, encryption info, preallocation). Finally, this makes it difficult for _filter_img_create to automagically work for QMP output. Thus, this patch adds a separate _filter_img_create_for_qmp function that echos every line verbatim that does not start with "Formatting", and pipes those "Formatting" lines to _filter_img_create. [1] Actually, the only thing that is really important is whether encryption is enabled or not. A patch by Maxim thus removes all other "encrypt.*" options from the output: https://lists.nongnu.org/archive/html/qemu-block/2020-06/msg00339.html But that patch needs to come later so we can get away with changing as few reference outputs in this patch here as possible. Signed-off-by: Max Reitz <mreitz@redhat.com> Message-Id: <20200625125548.870061-2-mreitz@redhat.com> Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
2020-06-25 15:55:30 +03:00
| _filter_img_create
echo
echo "== Two devices sharing the same file in backing chain =="
_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b"
_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c"
echo
echo "== Backing image also as an active device =="
_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}"
echo
echo "== Backing image also as an active device (ro) =="
_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on"
echo
echo "== Symbolic link =="
rm -f "${TEST_IMG}.lnk" &>/dev/null
ln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link"
_run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}"
echo
echo "== Active commit to intermediate layer should work when base in use =="
_launch_qemu -drive format=$IMGFMT,file="${TEST_IMG}.a",id=drive0,if=none \
-device virtio-blk,drive=drive0
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'qmp_capabilities' }" \
'return'
_run_cmd $QEMU_IMG commit -b "${TEST_IMG}.b" "${TEST_IMG}.c"
_cleanup_qemu
_launch_qemu
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'qmp_capabilities' }" \
'return'
echo "Adding drive"
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'human-monitor-command',
'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \
'return'
_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
echo "Creating overlay with qemu-img when the guest is running should be allowed"
iotests: Specify explicit backing format where sensible There are many existing qcow2 images that specify a backing file but no format. This has been the source of CVEs in the past, but has become more prominent of a problem now that libvirt has switched to -blockdev. With older -drive, at least the probing was always done by qemu (so the only risk of a changed format between successive boots of a guest was if qemu was upgraded and probed differently). But with newer -blockdev, libvirt must specify a format; if libvirt guesses raw where the image was formatted, this results in data corruption visible to the guest; conversely, if libvirt guesses qcow2 where qemu was using raw, this can result in potential security holes, so modern libvirt instead refuses to use images without explicit backing format. The change in libvirt to reject images without explicit backing format has pointed out that a number of tools have been far too reliant on probing in the past. It's time to set a better example in our own iotests of properly setting this parameter. iotest calls to create, rebase, and convert are all impacted to some degree. It's a bit annoying that we are inconsistent on command line - while all of those accept -o backing_file=...,backing_fmt=..., the shortcuts are different: create and rebase have -b and -F, while convert has -B but no -F. (amend has no shortcuts, but the previous patch just deprecated the use of amend to change backing chains). Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20200706203954.341758-9-eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
_run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" -F $IMGFMT "${TEST_IMG}.overlay"
echo "== Closing an image should unlock it =="
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'human-monitor-command',
'arguments': { 'command-line': 'drive_del d0' } }" \
'return'
_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
echo "Adding two and closing one"
for d in d0 d1; do
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'human-monitor-command',
'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \
'return'
done
_run_cmd $QEMU_IMG info "${TEST_IMG}"
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'human-monitor-command',
'arguments': { 'command-line': 'drive_del d0' } }" \
'return'
_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
echo "Closing the other"
_send_qemu_cmd $QEMU_HANDLE \
"{ 'execute': 'human-monitor-command',
'arguments': { 'command-line': 'drive_del d1' } }" \
'return'
_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
_cleanup_qemu
echo
echo "== Detecting -U and force-share conflicts =="
echo
echo 'No conflict:'
$QEMU_IMG info -U --image-opts driver=null-co,force-share=on
echo
echo 'Conflict:'
$QEMU_IMG info -U --image-opts driver=null-co,force-share=off
echo
echo 'No conflict:'
$QEMU_IO -c 'open -r -U -o driver=null-co,force-share=on'
echo
echo 'Conflict:'
$QEMU_IO -c 'open -r -U -o driver=null-co,force-share=off'
# success, all done
echo "*** done"
rm -f $seq.full
status=0