9ae3a8
From 9dc2684d2d9c9f9e62b5e4260fcd0c254f58bb6d Mon Sep 17 00:00:00 2001
9ae3a8
From: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Date: Wed, 14 May 2014 08:32:12 +0200
9ae3a8
Subject: [PATCH 14/31] virtio-scsi: fix buffer overrun on invalid state load
9ae3a8
MIME-Version: 1.0
9ae3a8
Content-Type: text/plain; charset=UTF-8
9ae3a8
Content-Transfer-Encoding: 8bit
9ae3a8
9ae3a8
RH-Author: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Message-id: <1400056285-6688-11-git-send-email-mst@redhat.com>
9ae3a8
Patchwork-id: 58860
9ae3a8
O-Subject: [PATCH qemu-kvm RHEL7.1] virtio-scsi: fix buffer overrun on invalid state load
9ae3a8
Bugzilla: 1095742
9ae3a8
RH-Acked-by: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
9ae3a8
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
9ae3a8
RH-Acked-by: Amos Kong <akong@redhat.com>
9ae3a8
9ae3a8
CVE-2013-4542
9ae3a8
9ae3a8
hw/scsi/scsi-bus.c invokes load_request.
9ae3a8
9ae3a8
 virtio_scsi_load_request does:
9ae3a8
    qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
9ae3a8
9ae3a8
this probably can make elem invalid, for example,
9ae3a8
make in_num or out_num huge, then:
9ae3a8
9ae3a8
    virtio_scsi_parse_req(s, vs->cmd_vqs[n], req);
9ae3a8
9ae3a8
will do:
9ae3a8
9ae3a8
    if (req->elem.out_num > 1) {
9ae3a8
        qemu_sgl_init_external(req, &req->elem.out_sg[1],
9ae3a8
                               &req->elem.out_addr[1],
9ae3a8
                               req->elem.out_num - 1);
9ae3a8
    } else {
9ae3a8
        qemu_sgl_init_external(req, &req->elem.in_sg[1],
9ae3a8
                               &req->elem.in_addr[1],
9ae3a8
                               req->elem.in_num - 1);
9ae3a8
    }
9ae3a8
9ae3a8
and this will access out of array bounds.
9ae3a8
9ae3a8
Note: this adds security checks within assert calls since
9ae3a8
SCSIBusInfo's load_request cannot fail.
9ae3a8
For now simply disable builds with NDEBUG - there seems
9ae3a8
to be little value in supporting these.
9ae3a8
9ae3a8
Cc: Andreas Färber <afaerber@suse.de>
9ae3a8
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Signed-off-by: Juan Quintela <quintela@redhat.com>
9ae3a8
(cherry picked from commit 3c3ce981423e0d6c18af82ee62f1850c2cda5976)
9ae3a8
9ae3a8
Tested: lightly on developer's box
9ae3a8
Brew build: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=7452039
9ae3a8
Bugzilla:1095742
9ae3a8
---
9ae3a8
 hw/scsi/virtio-scsi.c | 9 +++++++++
9ae3a8
 1 file changed, 9 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 hw/scsi/virtio-scsi.c |    9 +++++++++
9ae3a8
 1 files changed, 9 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c
9ae3a8
index 57541b4..7cf3e4b 100644
9ae3a8
--- a/hw/scsi/virtio-scsi.c
9ae3a8
+++ b/hw/scsi/virtio-scsi.c
9ae3a8
@@ -145,6 +145,15 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
9ae3a8
     qemu_get_be32s(f, &n);
9ae3a8
     assert(n < vs->conf.num_queues);
9ae3a8
     qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
9ae3a8
+    /* TODO: add a way for SCSIBusInfo's load_request to fail,
9ae3a8
+     * and fail migration instead of asserting here.
9ae3a8
+     * When we do, we might be able to re-enable NDEBUG below.
9ae3a8
+     */
9ae3a8
+#ifdef NDEBUG
9ae3a8
+#error building with NDEBUG is not supported
9ae3a8
+#endif
9ae3a8
+    assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg));
9ae3a8
+    assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg));
9ae3a8
     virtio_scsi_parse_req(s, vs->cmd_vqs[n], req);
9ae3a8
 
9ae3a8
     scsi_req_ref(sreq);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8