From patchwork Mon Mar 10 22:44:07 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 328856 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 2A4042C00A1 for ; Tue, 11 Mar 2014 09:44:51 +1100 (EST) Received: from localhost ([::1]:51494 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WN8wD-0002cW-28 for incoming@patchwork.ozlabs.org; Mon, 10 Mar 2014 18:44:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48983) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WN8vm-0002c5-QF for qemu-devel@nongnu.org; Mon, 10 Mar 2014 18:44:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WN8vg-0005ij-86 for qemu-devel@nongnu.org; Mon, 10 Mar 2014 18:44:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:16064) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WN8vf-0005hQ-Vy for qemu-devel@nongnu.org; Mon, 10 Mar 2014 18:44:16 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2AMiFOL009958 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 10 Mar 2014 18:44:15 -0400 Received: from localhost (ovpn-116-21.ams2.redhat.com [10.36.116.21]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s2AMiDJ2027261 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Mon, 10 Mar 2014 18:44:14 -0400 From: Max Reitz To: qemu-devel@nongnu.org Date: Mon, 10 Mar 2014 23:44:07 +0100 Message-Id: <1394491449-10897-2-git-send-email-mreitz@redhat.com> In-Reply-To: <1394491449-10897-1-git-send-email-mreitz@redhat.com> References: <1394491449-10897-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH 1/3] qcow2: Check bs->drv in copy_sectors() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Before dereferencing bs->drv for a call to its member bdrv_co_readv(), copy_sectors() should check whether that pointer is indeed valid, since it may have been set to NULL by e.g. a concurrent write triggering the corruption prevention mechanism. Signed-off-by: Max Reitz --- To be precise, this still is a race condition. If bs->drv is set to NULL after the check and before the call to bdrv_co_readv(), QEMU will obviously still crash. However, in order to circumvent this behavior, we would probably have to re-lock s->lock, check bs->drv, take the function pointer to bdrv_co_readv() and then unlock s->lock before the function is called. I found this rather ugly and therefore this still has a very small chance of running into a race condition. Therefore, I'm asking for your opinion on this, whether we can really take this chance or should rather "do it right". In fact, if I were a reviewer, I'd probably reject this patch and request the solution with the function pointer (if there is no better solution), but I was afraid to send such an ugly patch. --- block/qcow2-cluster.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 36c1bed..9499df9 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -380,6 +380,10 @@ static int coroutine_fn copy_sectors(BlockDriverState *bs, BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); + if (!bs->drv) { + return -ENOMEDIUM; + } + /* Call .bdrv_co_readv() directly instead of using the public block-layer * interface. This avoids double I/O throttling and request tracking, * which can lead to deadlock when block layer copy-on-read is enabled.