From patchwork Wed Jun 15 16:16:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Pandit X-Patchwork-Id: 9178931 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EAE5C60776 for ; Wed, 15 Jun 2016 16:21:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC9E727D5D for ; Wed, 15 Jun 2016 16:21:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D145C27DCE; Wed, 15 Jun 2016 16:21:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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 mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CA5A527D5D for ; Wed, 15 Jun 2016 16:21:19 +0000 (UTC) Received: from localhost ([::1]:43411 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDDZ8-0002Hm-Rt for patchwork-qemu-devel@patchwork.kernel.org; Wed, 15 Jun 2016 12:21:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34014) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDDUV-0006bB-Q0 for qemu-devel@nongnu.org; Wed, 15 Jun 2016 12:16:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDDUQ-000458-OU for qemu-devel@nongnu.org; Wed, 15 Jun 2016 12:16:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39994) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDDUQ-000452-IJ for qemu-devel@nongnu.org; Wed, 15 Jun 2016 12:16:26 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F195F10569; Wed, 15 Jun 2016 16:16:25 +0000 (UTC) Received: from javelin.localdomain (vpn1-5-93.sin2.redhat.com [10.67.5.93]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5FGGHVA013273 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 15 Jun 2016 12:16:19 -0400 From: P J P To: Qemu Developers Date: Wed, 15 Jun 2016 21:46:16 +0530 Message-Id: <1466007376-27333-1-git-send-email-ppandit@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 15 Jun 2016 16:16:26 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3] scsi: esp: check length before dma read 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: Paolo Bonzini , Li Qiang , Prasad J Pandit , Laszlo Ersek Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Prasad J Pandit While doing DMA read into ESP command buffer 's->cmdbuf', the length parameter could exceed the buffer size. Add check to avoid OOB access. Also increase the command buffer size to 32, which is maximum when 's->do_cmd' is set. Reported-by: Li Qiang Signed-off-by: Prasad J Pandit --- hw/scsi/esp.c | 7 +++++-- include/hw/scsi/esp.h | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) Update as per -> https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg04194.html diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c index 4b94bbc..1208a63 100644 --- a/hw/scsi/esp.c +++ b/hw/scsi/esp.c @@ -249,6 +249,9 @@ static void esp_do_dma(ESPState *s) len = s->dma_left; if (s->do_cmd) { trace_esp_do_dma(s->cmdlen, len); + if (len > sizeof(s->cmdbuf) - s->cmdlen) { + return; + } s->dma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len); s->ti_size = 0; s->cmdlen = 0; @@ -348,7 +351,7 @@ static void handle_ti(ESPState *s) s->dma_counter = dmalen; if (s->do_cmd) - minlen = (dmalen < 32) ? dmalen : 32; + minlen = (dmalen < ESP_CMDBUF_SZ) ? dmalen : ESP_CMDBUF_SZ; else if (s->ti_size < 0) minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size; else @@ -452,7 +455,7 @@ void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val) break; case ESP_FIFO: if (s->do_cmd) { - if (s->cmdlen < TI_BUFSZ) { + if (s->cmdlen < ESP_CMDBUF_SZ) { s->cmdbuf[s->cmdlen++] = val & 0xff; } else { trace_esp_error_fifo_overrun(); diff --git a/include/hw/scsi/esp.h b/include/hw/scsi/esp.h index 6c79527..d2c4886 100644 --- a/include/hw/scsi/esp.h +++ b/include/hw/scsi/esp.h @@ -14,6 +14,7 @@ void esp_init(hwaddr espaddr, int it_shift, #define ESP_REGS 16 #define TI_BUFSZ 16 +#define ESP_CMDBUF_SZ 32 typedef struct ESPState ESPState; @@ -31,7 +32,7 @@ struct ESPState { SCSIBus bus; SCSIDevice *current_dev; SCSIRequest *current_req; - uint8_t cmdbuf[TI_BUFSZ]; + uint8_t cmdbuf[ESP_CMDBUF_SZ]; uint32_t cmdlen; uint32_t do_cmd;