Blame 0002-scsi-pvscsi-limit-loop-to-fetch-SG-list.patch

3a13dd
From: Prasad J Pandit <pjp@fedoraproject.org>
3a13dd
Date: Tue, 6 Sep 2016 02:20:43 +0530
3a13dd
Subject: [PATCH] scsi: pvscsi: limit loop to fetch SG list
3a13dd
3a13dd
In PVSCSI paravirtual SCSI bus, pvscsi_convert_sglist can take a very
3a13dd
long time or go into an infinite loop due to two different bugs:
3a13dd
3a13dd
1) the request descriptor data length is defined to be 64 bit. While
3a13dd
building SG list from a request descriptor, it gets truncated to 32bit
3a13dd
in routine 'pvscsi_convert_sglist'. This could lead to an infinite loop
3a13dd
situation large 'dataLen' values when data_length is cast to uint32_t and
3a13dd
chunk_size becomes always zero.  Fix this by removing the incorrect cast.
3a13dd
3a13dd
2) pvscsi_get_next_sg_elem can be called arbitrarily many times if the
3a13dd
element has a zero length.  Get out of the loop early when this happens,
3a13dd
by introducing an upper limit on the number of SG list elements.
3a13dd
3a13dd
Reported-by: Li Qiang <liqiang6-s@360.cn>
3a13dd
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
3a13dd
Message-Id: <1473108643-12983-1-git-send-email-ppandit@redhat.com>
3a13dd
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
3a13dd
(cherry picked from commit 49adc5d3f8c6bb75e55ebfeab109c5c37dea65e8)
3a13dd
---
3a13dd
 hw/scsi/vmw_pvscsi.c | 11 ++++++-----
3a13dd
 1 file changed, 6 insertions(+), 5 deletions(-)
3a13dd
3a13dd
diff --git a/hw/scsi/vmw_pvscsi.c b/hw/scsi/vmw_pvscsi.c
3a13dd
index 4245c15..babac5a 100644
3a13dd
--- a/hw/scsi/vmw_pvscsi.c
3a13dd
+++ b/hw/scsi/vmw_pvscsi.c
3a13dd
@@ -40,6 +40,8 @@
3a13dd
 #define PVSCSI_MAX_DEVS                   (64)
3a13dd
 #define PVSCSI_MSIX_NUM_VECTORS           (1)
3a13dd
 
3a13dd
+#define PVSCSI_MAX_SG_ELEM                2048
3a13dd
+
3a13dd
 #define PVSCSI_MAX_CMD_DATA_WORDS \
3a13dd
     (sizeof(PVSCSICmdDescSetupRings)/sizeof(uint32_t))
3a13dd
 
3a13dd
@@ -628,17 +630,16 @@ pvscsi_queue_pending_descriptor(PVSCSIState *s, SCSIDevice **d,
3a13dd
 static void
3a13dd
 pvscsi_convert_sglist(PVSCSIRequest *r)
3a13dd
 {
3a13dd
-    int chunk_size;
3a13dd
+    uint32_t chunk_size, elmcnt = 0;
3a13dd
     uint64_t data_length = r->req.dataLen;
3a13dd
     PVSCSISGState sg = r->sg;
3a13dd
-    while (data_length) {
3a13dd
-        while (!sg.resid) {
3a13dd
+    while (data_length && elmcnt < PVSCSI_MAX_SG_ELEM) {
3a13dd
+        while (!sg.resid && elmcnt++ < PVSCSI_MAX_SG_ELEM) {
3a13dd
             pvscsi_get_next_sg_elem(&sg;;
3a13dd
             trace_pvscsi_convert_sglist(r->req.context, r->sg.dataAddr,
3a13dd
                                         r->sg.resid);
3a13dd
         }
3a13dd
-        assert(data_length > 0);
3a13dd
-        chunk_size = MIN((unsigned) data_length, sg.resid);
3a13dd
+        chunk_size = MIN(data_length, sg.resid);
3a13dd
         if (chunk_size) {
3a13dd
             qemu_sglist_add(&r->sgl, sg.dataAddr, chunk_size);
3a13dd
         }