From nobody Mon Feb 9 20:32:11 2026 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 (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1545234488780822.9499059152898; Wed, 19 Dec 2018 07:48:08 -0800 (PST) Received: from localhost ([::1]:60766 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZdl2-00046z-4o for importer@patchew.org; Wed, 19 Dec 2018 10:27:36 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55679) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZddb-00061T-5E for qemu-devel@nongnu.org; Wed, 19 Dec 2018 10:19:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gZddZ-0004A2-4u for qemu-devel@nongnu.org; Wed, 19 Dec 2018 10:19:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48936) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gZddY-000490-Or for qemu-devel@nongnu.org; Wed, 19 Dec 2018 10:19:53 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 85AFEA08F0; Wed, 19 Dec 2018 15:19:51 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-76.ams2.redhat.com [10.36.112.76]) by smtp.corp.redhat.com (Postfix) with ESMTP id 125F868890; Wed, 19 Dec 2018 15:19:49 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 19 Dec 2018 16:18:56 +0100 Message-Id: <20181219151917.3874-15-pbonzini@redhat.com> In-Reply-To: <20181219151917.3874-1-pbonzini@redhat.com> References: <20181219151917.3874-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 19 Dec 2018 15:19:51 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 14/35] block/iscsi: fix ioctl cancel use-after-free 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: Sreejith Mohanan , Stefan Hajnoczi , Felipe Franciosi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Stefan Hajnoczi iscsi_aio_cancel() does not increment the request's reference count, causing a use-after-free when ABORT TASK finishes after the request has already completed. There are some additional issues with iscsi_aio_cancel(): 1. Several ABORT TASKs may be sent for the same task if iscsi_aio_cancel() is invoked multiple times. It's better to avoid this just in case the command identifier is reused. 2. The iscsilun->mutex protection is missing in iscsi_aio_cancel(). Reported-by: Felipe Franciosi Signed-off-by: Stefan Hajnoczi Message-Id: <20180203061621.7033-4-stefanha@redhat.com> Reviewed-by: Felipe Franciosi Tested-by: Sreejith Mohanan Signed-off-by: Paolo Bonzini --- block/iscsi.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/block/iscsi.c b/block/iscsi.c index 1924a2b58e..abb872d3d9 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -124,6 +124,7 @@ typedef struct IscsiAIOCB { #ifdef __linux__ sg_io_hdr_t *ioh; #endif + bool cancelled; } IscsiAIOCB; =20 /* libiscsi uses time_t so its enough to process events every second */ @@ -287,6 +288,7 @@ static void iscsi_co_init_iscsitask(IscsiLun *iscsilun,= struct IscsiTask *iTask) }; } =20 +/* Called (via iscsi_service) with QemuMutex held. */ static void iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command= _data, void *private_data) @@ -295,6 +297,7 @@ iscsi_abort_task_cb(struct iscsi_context *iscsi, int st= atus, void *command_data, =20 acb->status =3D -ECANCELED; iscsi_schedule_bh(acb); + qemu_aio_unref(acb); /* acquired in iscsi_aio_cancel() */ } =20 static void @@ -303,14 +306,25 @@ iscsi_aio_cancel(BlockAIOCB *blockacb) IscsiAIOCB *acb =3D (IscsiAIOCB *)blockacb; IscsiLun *iscsilun =3D acb->iscsilun; =20 - if (acb->status !=3D -EINPROGRESS) { + qemu_mutex_lock(&iscsilun->mutex); + + /* If it was cancelled or completed already, our work is done here */ + if (acb->cancelled || acb->status !=3D -EINPROGRESS) { + qemu_mutex_unlock(&iscsilun->mutex); return; } =20 + acb->cancelled =3D true; + + qemu_aio_ref(acb); /* released in iscsi_abort_task_cb() */ + /* send a task mgmt call to the target to cancel the task on the targe= t */ - iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, - iscsi_abort_task_cb, acb); + if (iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, + iscsi_abort_task_cb, acb) < 0) { + qemu_aio_unref(acb); /* since iscsi_abort_task_cb() won't be calle= d */ + } =20 + qemu_mutex_unlock(&iscsilun->mutex); } =20 static const AIOCBInfo iscsi_aiocb_info =3D { @@ -1008,6 +1022,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *= bs, acb->bh =3D NULL; acb->status =3D -EINPROGRESS; acb->ioh =3D buf; + acb->cancelled =3D false; =20 if (req !=3D SG_IO) { iscsi_ioctl_handle_emulated(acb, req, buf); --=20 2.20.1