Received: from eggs.gnu.org ([2001:4830:134:3::10]:59367) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c5F6q-0006JT-RU for qemu-devel@nongnu.org; Fri, 11 Nov 2016 11:55:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c5F6q-0002c0-5f for qemu-devel@nongnu.org; Fri, 11 Nov 2016 11:55:24 -0500 From: Kevin Wolf Date: Fri, 11 Nov 2016 17:55:03 +0100 Message-Id: <1478883311-24052-5-git-send-email-kwolf@redhat.com> In-Reply-To: <1478883311-24052-1-git-send-email-kwolf@redhat.com> References: <1478883311-24052-1-git-send-email-kwolf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 04/12] raw_bsd: move check to prevent overflow List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com, qemu-devel@nongnu.org From: Tom=C3=A1=C5=A1 Golembiovsk=C3=BD When only offset is specified but no size and the offset is greater than the real size of the containing device an overflow occurs when parsing the options. This overflow is harmless because we do check for this exact situation little bit later, but it leads to an error message with weird values. It is better to do the check is sooner and prevent the overflow. Signed-off-by: Tom=C3=A1=C5=A1 Golembiovsk=C3=BD Signed-off-by: Kevin Wolf --- block/raw_bsd.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/block/raw_bsd.c b/block/raw_bsd.c index 7c9bebb..cf7a560 100644 --- a/block/raw_bsd.c +++ b/block/raw_bsd.c @@ -91,6 +91,14 @@ static int raw_read_options(QDict *options, BlockDrive= rState *bs, } =20 s->offset =3D qemu_opt_get_size(opts, "offset", 0); + if (s->offset > real_size) { + error_setg(errp, "Offset (%" PRIu64 ") cannot be greater than " + "size of the containing file (%" PRId64 ")", + s->offset, real_size); + ret =3D -EINVAL; + goto end; + } + if (qemu_opt_find(opts, "size") !=3D NULL) { s->size =3D qemu_opt_get_size(opts, "size", 0); s->has_size =3D true; @@ -100,7 +108,7 @@ static int raw_read_options(QDict *options, BlockDriv= erState *bs, } =20 /* Check size and offset */ - if (real_size < s->offset || (real_size - s->offset) < s->size) { + if ((real_size - s->offset) < s->size) { error_setg(errp, "The sum of offset (%" PRIu64 ") and size " "(%" PRIu64 ") has to be smaller or equal to the " " actual size of the containing file (%" PRId64 ")", --=20 1.8.3.1