From patchwork Wed Jun 15 12:29:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 9178471 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 A2A4C60573 for ; Wed, 15 Jun 2016 12:30:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 913EF27D45 for ; Wed, 15 Jun 2016 12:30:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 83C4027DA4; Wed, 15 Jun 2016 12:30:10 +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 541DA27D45 for ; Wed, 15 Jun 2016 12:30:09 +0000 (UTC) Received: from localhost ([::1]:41860 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bD9xQ-0000mV-2o for patchwork-qemu-devel@patchwork.kernel.org; Wed, 15 Jun 2016 08:30:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38417) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bD9wx-0000jr-U7 for qemu-devel@nongnu.org; Wed, 15 Jun 2016 08:29:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bD9wt-0005XB-N2 for qemu-devel@nongnu.org; Wed, 15 Jun 2016 08:29:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54840) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bD9wt-0005X4-Gw for qemu-devel@nongnu.org; Wed, 15 Jun 2016 08:29:35 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (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 553A3C05681C; Wed, 15 Jun 2016 12:29:33 +0000 (UTC) Received: from [10.36.112.54] (ovpn-112-54.ams2.redhat.com [10.36.112.54]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5FCTTwM003500 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 15 Jun 2016 08:29:31 -0400 To: Laszlo Ersek , P J P , Qemu Developers References: <1465982948-14639-1-git-send-email-ppandit@redhat.com> <7cdb718e-6551-bf3a-a431-575aa29ac767@redhat.com> From: Paolo Bonzini Message-ID: <13f61979-7f38-4b33-9160-ea35dfa6bc7f@redhat.com> Date: Wed, 15 Jun 2016 14:29:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <7cdb718e-6551-bf3a-a431-575aa29ac767@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 15 Jun 2016 12:29:35 +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: Re: [Qemu-devel] [PATCH] 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: Li Qiang , Prasad J Pandit Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP On 15/06/2016 14:11, Laszlo Ersek wrote: > (1) In my opinion, this check is not sufficient. All of the following > objects: > > - the "len" local variable > - the "ESPState.dma_left" field > - the "ESPState.cmdlen" field > > have type "uint32_t" (that is, "unsigned int"). Therefore the addition > on the LHS is performed in "unsigned int", resulting in (well-defined, > but still harmful) wrapping at 2^32. True, on the other hand if s->do_cmd is 1 then dma_left is at most 32 (see handle_ti). So a better fix is to change cmdbuf[] to 32 bytes in include/hw/scsi/esp.h, and define a constant ESP_CMDBUF_SZ equal to 32 that can be used in handle_ti and in the definition of cmdbuf. In addition, there is some useless code duplication between esp_do_dma and handle_ti that can be fixed like this: > (2) I think the check may be off-by-one. If (s->cmdlen + len) equal > sizeof(s->cmdbuf), that should be allowed, shouldn't it? Then the > dma_memory_read() function just after will access the cmd buffer right > to its end, but not after. Also correct. Paolo diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c index 3f08598..43d10f6 100644 --- a/hw/scsi/esp.c +++ b/hw/scsi/esp.c @@ -245,21 +245,17 @@ static void esp_do_dma(ESPState *s) uint32_t len; int to_device; - to_device = (s->ti_size < 0); len = s->dma_left; if (s->do_cmd) { trace_esp_do_dma(s->cmdlen, len); s->dma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len); - s->ti_size = 0; - s->cmdlen = 0; - s->do_cmd = 0; - do_cmd(s, s->cmdbuf); return; } if (s->async_len == 0) { /* Defer until data is available. */ return; } + to_device = (s->ti_size < 0); if (len > s->async_len) { len = s->async_len; } @@ -358,13 +354,13 @@ static void handle_ti(ESPState *s) s->dma_left = minlen; s->rregs[ESP_RSTAT] &= ~STAT_TC; esp_do_dma(s); - } else if (s->do_cmd) { + } + if (s->do_cmd) { trace_esp_handle_ti_cmd(s->cmdlen); s->ti_size = 0; s->cmdlen = 0; s->do_cmd = 0; do_cmd(s, s->cmdbuf); - return; } }