From patchwork Tue Sep 13 17:16:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 669558 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3sYXKH0NCVz9s8x for ; Wed, 14 Sep 2016 03:50:59 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=l1GpBSz7; dkim-atps=neutral Received: from localhost ([::1]:50649 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjrrE-0002lO-LX for incoming@patchwork.ozlabs.org; Tue, 13 Sep 2016 13:50:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33714) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjrLg-0008TL-SP for qemu-devel@nongnu.org; Tue, 13 Sep 2016 13:18:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bjrLb-0005r4-OQ for qemu-devel@nongnu.org; Tue, 13 Sep 2016 13:18:19 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:36096) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjrLb-0005qN-H0 for qemu-devel@nongnu.org; Tue, 13 Sep 2016 13:18:15 -0400 Received: by mail-wm0-f68.google.com with SMTP id b184so3337824wma.3 for ; Tue, 13 Sep 2016 10:18:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=686UewaFBjHnf/UD8XdAmlpIlJBGB4TJBeKxdmqZsuU=; b=l1GpBSz7NmFb7PQh4G/l+IqZ206XjkLOd25Kb/Lwgzn2snOcrOwtxRneshgpDJQUBD 1KahSzwLJqJW3CXH07JsxtXiRCCkeITJ5hm7WHvKwe5s233WwLwRHhLVu90Foa67B8tZ SHmW8BTxj01EiuilOYfJN0Fs2Pwv4Ttojumc0DVLjf4LvDM+qIcrzJOW3YwZhPFFeraS R8RI1ECdszwYzU+oVc+8xt6O2ml4l6iNBFrne4SaGS+z9jSszqPgBxn2+ozSBps9m7UX gjjk4O3UueOrHaRLAIBhsNnVbReGLapPhyaq0cdmsz2gX2RPM2gX5SFp67nCSOBDllJt cdig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=686UewaFBjHnf/UD8XdAmlpIlJBGB4TJBeKxdmqZsuU=; b=i8W2bZRRQgPHFPCYgHyexis6Bt8mMTguYhlpGIo5vj41+LA4wG4NnT6Ll1Fl3yRwvR KSuA9dyhipvkUOvF0giPqnEqbkKZbhr5f2yECWfbFw+gAHV2EUmGpgFbem5AoMjNAe5s d1CwroUFa93JumN/uPPGWt6G6MCQl91947oO/KTBM62AETwKy5VSk6+vSIxtjlS8h621 tVIMIClfFk3M4eg+UmTJcoKWfOR+IYsIkiWZPs4CtLoLYWQVGyklvfEVSmsMjwlqUkQ5 +OaMfwiUilD64MBFlU0mLfTGH/vHVyZX3iCNLT9xJKRZyUudXtmjZvEkLkhLXHfnqAKP bb3Q== X-Gm-Message-State: AE9vXwNNKAmYSmC763Y4QDsC2AUNTW3mJqBPiodHrloVXwduD3byz9WNBhPPgdts1wyMag== X-Received: by 10.194.234.69 with SMTP id uc5mr23989245wjc.76.1473787034985; Tue, 13 Sep 2016 10:17:14 -0700 (PDT) Received: from 640k.lan (94-39-180-254.adsl-ull.clienti.tiscali.it. [94.39.180.254]) by smtp.gmail.com with ESMTPSA id s184sm6331987wmb.11.2016.09.13.10.17.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Sep 2016 10:17:14 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 13 Sep 2016 19:16:04 +0200 Message-Id: <1473786989-54823-34-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1473786989-54823-1-git-send-email-pbonzini@redhat.com> References: <1473786989-54823-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 74.125.82.68 Subject: [Qemu-devel] [PULL 33/58] scsi: pvscsi: limit loop to fetch SG list 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: Prasad J Pandit Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Prasad J Pandit In PVSCSI paravirtual SCSI bus, pvscsi_convert_sglist can take a very long time or go into an infinite loop due to two different bugs: 1) the request descriptor data length is defined to be 64 bit. While building SG list from a request descriptor, it gets truncated to 32bit in routine 'pvscsi_convert_sglist'. This could lead to an infinite loop situation large 'dataLen' values when data_length is cast to uint32_t and chunk_size becomes always zero. Fix this by removing the incorrect cast. 2) pvscsi_get_next_sg_elem can be called arbitrarily many times if the element has a zero length. Get out of the loop early when this happens, by introducing an upper limit on the number of SG list elements. Reported-by: Li Qiang Signed-off-by: Prasad J Pandit Message-Id: <1473108643-12983-1-git-send-email-ppandit@redhat.com> Signed-off-by: Paolo Bonzini --- hw/scsi/vmw_pvscsi.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/hw/scsi/vmw_pvscsi.c b/hw/scsi/vmw_pvscsi.c index 4245c15..babac5a 100644 --- a/hw/scsi/vmw_pvscsi.c +++ b/hw/scsi/vmw_pvscsi.c @@ -40,6 +40,8 @@ #define PVSCSI_MAX_DEVS (64) #define PVSCSI_MSIX_NUM_VECTORS (1) +#define PVSCSI_MAX_SG_ELEM 2048 + #define PVSCSI_MAX_CMD_DATA_WORDS \ (sizeof(PVSCSICmdDescSetupRings)/sizeof(uint32_t)) @@ -628,17 +630,16 @@ pvscsi_queue_pending_descriptor(PVSCSIState *s, SCSIDevice **d, static void pvscsi_convert_sglist(PVSCSIRequest *r) { - int chunk_size; + uint32_t chunk_size, elmcnt = 0; uint64_t data_length = r->req.dataLen; PVSCSISGState sg = r->sg; - while (data_length) { - while (!sg.resid) { + while (data_length && elmcnt < PVSCSI_MAX_SG_ELEM) { + while (!sg.resid && elmcnt++ < PVSCSI_MAX_SG_ELEM) { pvscsi_get_next_sg_elem(&sg); trace_pvscsi_convert_sglist(r->req.context, r->sg.dataAddr, r->sg.resid); } - assert(data_length > 0); - chunk_size = MIN((unsigned) data_length, sg.resid); + chunk_size = MIN(data_length, sg.resid); if (chunk_size) { qemu_sglist_add(&r->sgl, sg.dataAddr, chunk_size); }