block: Fix parameter checking in bdrv_co_copy_range_internal

src may be NULL if BDRV_REQ_ZERO_WRITE flag is set, in this case only
check dst and dst->bs. This bug was introduced when moving in the
request tracking code from bdrv_co_copy_range, in 37aec7d75e.

This especially fixes the possible segfault when initializing src_bs
with a NULL src.

Signed-off-by: Fam Zheng <famz@redhat.com>
Message-id: 20180703023758.14422-2-famz@redhat.com
Reviewed-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Jeff Cody <jcody@redhat.com>
This commit is contained in:
Fam Zheng 2018-07-03 10:37:56 +08:00 committed by Jeff Cody
parent ab08440a4e
commit d4d3e5a0d5
1 changed files with 15 additions and 14 deletions

View File

@ -2897,18 +2897,11 @@ static int coroutine_fn bdrv_co_copy_range_internal(BdrvChild *src,
bool recurse_src) bool recurse_src)
{ {
BdrvTrackedRequest src_req, dst_req; BdrvTrackedRequest src_req, dst_req;
BlockDriverState *src_bs = src->bs;
BlockDriverState *dst_bs = dst->bs;
int ret; int ret;
if (!src || !dst || !src->bs || !dst->bs) { if (!dst || !dst->bs) {
return -ENOMEDIUM; return -ENOMEDIUM;
} }
ret = bdrv_check_byte_request(src->bs, src_offset, bytes);
if (ret) {
return ret;
}
ret = bdrv_check_byte_request(dst->bs, dst_offset, bytes); ret = bdrv_check_byte_request(dst->bs, dst_offset, bytes);
if (ret) { if (ret) {
return ret; return ret;
@ -2917,16 +2910,24 @@ static int coroutine_fn bdrv_co_copy_range_internal(BdrvChild *src,
return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, flags); return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, flags);
} }
if (!src || !src->bs) {
return -ENOMEDIUM;
}
ret = bdrv_check_byte_request(src->bs, src_offset, bytes);
if (ret) {
return ret;
}
if (!src->bs->drv->bdrv_co_copy_range_from if (!src->bs->drv->bdrv_co_copy_range_from
|| !dst->bs->drv->bdrv_co_copy_range_to || !dst->bs->drv->bdrv_co_copy_range_to
|| src->bs->encrypted || dst->bs->encrypted) { || src->bs->encrypted || dst->bs->encrypted) {
return -ENOTSUP; return -ENOTSUP;
} }
bdrv_inc_in_flight(src_bs); bdrv_inc_in_flight(src->bs);
bdrv_inc_in_flight(dst_bs); bdrv_inc_in_flight(dst->bs);
tracked_request_begin(&src_req, src_bs, src_offset, tracked_request_begin(&src_req, src->bs, src_offset,
bytes, BDRV_TRACKED_READ); bytes, BDRV_TRACKED_READ);
tracked_request_begin(&dst_req, dst_bs, dst_offset, tracked_request_begin(&dst_req, dst->bs, dst_offset,
bytes, BDRV_TRACKED_WRITE); bytes, BDRV_TRACKED_WRITE);
wait_serialising_requests(&src_req); wait_serialising_requests(&src_req);
@ -2944,8 +2945,8 @@ static int coroutine_fn bdrv_co_copy_range_internal(BdrvChild *src,
} }
tracked_request_end(&src_req); tracked_request_end(&src_req);
tracked_request_end(&dst_req); tracked_request_end(&dst_req);
bdrv_dec_in_flight(src_bs); bdrv_dec_in_flight(src->bs);
bdrv_dec_in_flight(dst_bs); bdrv_dec_in_flight(dst->bs);
return ret; return ret;
} }