From nobody Wed Dec 17 05:39:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520601743343706.368951894595; Fri, 9 Mar 2018 05:22:23 -0800 (PST) Received: from localhost ([::1]:45243 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euHyX-0002Ni-Q1 for importer@patchew.org; Fri, 09 Mar 2018 08:22:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45317) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euHwN-000105-Ri for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:20:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euHwM-0007OM-FK for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:20:07 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48112 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euHwD-0007L1-Ke; Fri, 09 Mar 2018 08:19:57 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 57C1B8D6FB; Fri, 9 Mar 2018 13:19:55 +0000 (UTC) Received: from localhost (ovpn-116-226.ams2.redhat.com [10.36.116.226]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9D49202322A; Fri, 9 Mar 2018 13:19:54 +0000 (UTC) From: Stefan Hajnoczi To: Date: Fri, 9 Mar 2018 13:19:43 +0000 Message-Id: <20180309131949.18640-2-stefanha@redhat.com> In-Reply-To: <20180309131949.18640-1-stefanha@redhat.com> References: <20180309131949.18640-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 09 Mar 2018 13:19:55 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 09 Mar 2018 13:19:55 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'stefanha@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 1/7] block: Fix qemu crash when using scsi-block X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peter Maydell , Fam Zheng , qemu-block@nongnu.org, Peter Crosthwaite , "Michael S. Tsirkin" , Max Reitz , Deepa Srinivasan , Stefan Hajnoczi , Paolo Bonzini , Konrad Rzeszutek Wilk , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Deepa Srinivasan Starting qemu with the following arguments causes qemu to segfault: ... -device lsi,id=3Dlsi0 -drive file=3Discsi:<...>,format=3Draw,if=3Dnone,= node-name=3D iscsi1 -device scsi-block,bus=3Dlsi0.0,id=3D<...>,drive=3Discsi1 This patch fixes blk_aio_ioctl() so it does not pass stack addresses to blk_aio_ioctl_entry() which may be invoked after blk_aio_ioctl() returns. M= ore details about the bug follow. blk_aio_ioctl() invokes blk_aio_prwv() with blk_aio_ioctl_entry as the coroutine parameter. blk_aio_prwv() ultimately calls aio_co_enter(). When blk_aio_ioctl() is executed from within a coroutine context (e.g. iscsi_bh_cb()), aio_co_enter() adds the coroutine (blk_aio_ioctl_entry) to the current coroutine's wakeup queue. blk_aio_ioctl() then returns. When blk_aio_ioctl_entry() executes later, it accesses an invalid pointer: .... BlkRwCo *rwco =3D &acb->rwco; rwco->ret =3D blk_co_ioctl(rwco->blk, rwco->offset, rwco->qiov->iov[0].iov_base); <--- qiov is invalid he= re ... In the case when blk_aio_ioctl() is called from a non-coroutine context, blk_aio_ioctl_entry() executes immediately. But if bdrv_co_ioctl() calls qemu_coroutine_yield(), blk_aio_ioctl() will return. When the coroutine execution is complete, control returns to blk_aio_ioctl_entry() after the c= all to blk_co_ioctl(). There is no invalid reference after this point, but the function is still holding on to invalid pointers. The fix is to change blk_aio_prwv() to accept a void pointer for the IO buf= fer rather than a QEMUIOVector. blk_aio_prwv() passes this through in BlkRwCo a= nd the coroutine function casts it to QEMUIOVector or uses the void pointer direct= ly. Signed-off-by: Deepa Srinivasan Signed-off-by: Konrad Rzeszutek Wilk Reviewed-by: Mark Kanda Reviewed-by: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- block/block-backend.c | 51 +++++++++++++++++++++++++----------------------= ---- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index b3c790e2bd..f2e0a855ff 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1150,7 +1150,7 @@ int coroutine_fn blk_co_pwritev(BlockBackend *blk, in= t64_t offset, typedef struct BlkRwCo { BlockBackend *blk; int64_t offset; - QEMUIOVector *qiov; + void *iobuf; int ret; BdrvRequestFlags flags; } BlkRwCo; @@ -1158,17 +1158,19 @@ typedef struct BlkRwCo { static void blk_read_entry(void *opaque) { BlkRwCo *rwco =3D opaque; + QEMUIOVector *qiov =3D rwco->iobuf; =20 - rwco->ret =3D blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size, - rwco->qiov, rwco->flags); + rwco->ret =3D blk_co_preadv(rwco->blk, rwco->offset, qiov->size, + qiov, rwco->flags); } =20 static void blk_write_entry(void *opaque) { BlkRwCo *rwco =3D opaque; + QEMUIOVector *qiov =3D rwco->iobuf; =20 - rwco->ret =3D blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size, - rwco->qiov, rwco->flags); + rwco->ret =3D blk_co_pwritev(rwco->blk, rwco->offset, qiov->size, + qiov, rwco->flags); } =20 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf, @@ -1188,7 +1190,7 @@ static int blk_prw(BlockBackend *blk, int64_t offset,= uint8_t *buf, rwco =3D (BlkRwCo) { .blk =3D blk, .offset =3D offset, - .qiov =3D &qiov, + .iobuf =3D &qiov, .flags =3D flags, .ret =3D NOT_DONE, }; @@ -1296,7 +1298,7 @@ static void blk_aio_complete_bh(void *opaque) } =20 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int byt= es, - QEMUIOVector *qiov, CoroutineEntry co_entr= y, + void *iobuf, CoroutineEntry co_entry, BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque) { @@ -1308,7 +1310,7 @@ static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, in= t64_t offset, int bytes, acb->rwco =3D (BlkRwCo) { .blk =3D blk, .offset =3D offset, - .qiov =3D qiov, + .iobuf =3D iobuf, .flags =3D flags, .ret =3D NOT_DONE, }; @@ -1331,10 +1333,11 @@ static void blk_aio_read_entry(void *opaque) { BlkAioEmAIOCB *acb =3D opaque; BlkRwCo *rwco =3D &acb->rwco; + QEMUIOVector *qiov =3D rwco->iobuf; =20 - assert(rwco->qiov->size =3D=3D acb->bytes); + assert(qiov->size =3D=3D acb->bytes); rwco->ret =3D blk_co_preadv(rwco->blk, rwco->offset, acb->bytes, - rwco->qiov, rwco->flags); + qiov, rwco->flags); blk_aio_complete(acb); } =20 @@ -1342,10 +1345,11 @@ static void blk_aio_write_entry(void *opaque) { BlkAioEmAIOCB *acb =3D opaque; BlkRwCo *rwco =3D &acb->rwco; + QEMUIOVector *qiov =3D rwco->iobuf; =20 - assert(!rwco->qiov || rwco->qiov->size =3D=3D acb->bytes); + assert(!qiov || qiov->size =3D=3D acb->bytes); rwco->ret =3D blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes, - rwco->qiov, rwco->flags); + qiov, rwco->flags); blk_aio_complete(acb); } =20 @@ -1474,8 +1478,10 @@ int blk_co_ioctl(BlockBackend *blk, unsigned long in= t req, void *buf) static void blk_ioctl_entry(void *opaque) { BlkRwCo *rwco =3D opaque; + QEMUIOVector *qiov =3D rwco->iobuf; + rwco->ret =3D blk_co_ioctl(rwco->blk, rwco->offset, - rwco->qiov->iov[0].iov_base); + qiov->iov[0].iov_base); } =20 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) @@ -1488,24 +1494,15 @@ static void blk_aio_ioctl_entry(void *opaque) BlkAioEmAIOCB *acb =3D opaque; BlkRwCo *rwco =3D &acb->rwco; =20 - rwco->ret =3D blk_co_ioctl(rwco->blk, rwco->offset, - rwco->qiov->iov[0].iov_base); + rwco->ret =3D blk_co_ioctl(rwco->blk, rwco->offset, rwco->iobuf); + blk_aio_complete(acb); } =20 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *= buf, BlockCompletionFunc *cb, void *opaque) { - QEMUIOVector qiov; - struct iovec iov; - - iov =3D (struct iovec) { - .iov_base =3D buf, - .iov_len =3D 0, - }; - qemu_iovec_init_external(&qiov, &iov, 1); - - return blk_aio_prwv(blk, req, 0, &qiov, blk_aio_ioctl_entry, 0, cb, op= aque); + return blk_aio_prwv(blk, req, 0, buf, blk_aio_ioctl_entry, 0, cb, opaq= ue); } =20 int blk_co_pdiscard(BlockBackend *blk, int64_t offset, int bytes) @@ -1949,7 +1946,9 @@ int blk_truncate(BlockBackend *blk, int64_t offset, P= reallocMode prealloc, static void blk_pdiscard_entry(void *opaque) { BlkRwCo *rwco =3D opaque; - rwco->ret =3D blk_co_pdiscard(rwco->blk, rwco->offset, rwco->qiov->siz= e); + QEMUIOVector *qiov =3D rwco->iobuf; + + rwco->ret =3D blk_co_pdiscard(rwco->blk, rwco->offset, qiov->size); } =20 int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes) --=20 2.14.3