From patchwork Fri Nov 27 03:12:21 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 549297 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DD27D1401B5 for ; Fri, 27 Nov 2015 14:13:52 +1100 (AEDT) Received: from localhost ([::1]:54427 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a29Tq-0006CL-6u for incoming@patchwork.ozlabs.org; Thu, 26 Nov 2015 22:13:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60138) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a29Sd-0003to-EW for qemu-devel@nongnu.org; Thu, 26 Nov 2015 22:12:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a29Sc-0002tu-GI for qemu-devel@nongnu.org; Thu, 26 Nov 2015 22:12:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39903) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a29Sc-0002to-Bq for qemu-devel@nongnu.org; Thu, 26 Nov 2015 22:12:34 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id EDC948E224; Fri, 27 Nov 2015 03:12:33 +0000 (UTC) Received: from jason-ThinkPad-T430s.nay.redhat.com (dhcp-14-168.nay.redhat.com [10.66.14.168]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAR3CORC015404; Thu, 26 Nov 2015 22:12:30 -0500 From: Jason Wang To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Fri, 27 Nov 2015 11:12:21 +0800 Message-Id: <1448593943-6415-2-git-send-email-jasowang@redhat.com> In-Reply-To: <1448593943-6415-1-git-send-email-jasowang@redhat.com> References: <1448593943-6415-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Stefan Weil , Jason Wang Subject: [Qemu-devel] [PULL for-2.5 1/3] eepro100: Prevent two endless loops X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Stefan Weil http://lists.nongnu.org/archive/html/qemu-devel/2015-11/msg04592.html shows an example how an endless loop in function action_command can be achieved. During my code review, I noticed a 2nd case which can result in an endless loop. Reported-by: Qinghao Tang Signed-off-by: Stefan Weil Signed-off-by: Jason Wang --- hw/net/eepro100.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/hw/net/eepro100.c b/hw/net/eepro100.c index 60333b7..685a478 100644 --- a/hw/net/eepro100.c +++ b/hw/net/eepro100.c @@ -774,6 +774,11 @@ static void tx_command(EEPRO100State *s) #if 0 uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, tbd_address + 6); #endif + if (tx_buffer_size == 0) { + /* Prevent an endless loop. */ + logout("loop in %s:%u\n", __FILE__, __LINE__); + break; + } tbd_address += 8; TRACE(RXTX, logout ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n", @@ -855,6 +860,10 @@ static void set_multicast_list(EEPRO100State *s) static void action_command(EEPRO100State *s) { + /* The loop below won't stop if it gets special handcrafted data. + Therefore we limit the number of iterations. */ + unsigned max_loop_count = 16; + for (;;) { bool bit_el; bool bit_s; @@ -870,6 +879,13 @@ static void action_command(EEPRO100State *s) #if 0 bool bit_sf = ((s->tx.command & COMMAND_SF) != 0); #endif + + if (max_loop_count-- == 0) { + /* Prevent an endless loop. */ + logout("loop in %s:%u\n", __FILE__, __LINE__); + break; + } + s->cu_offset = s->tx.link; TRACE(OTHER, logout("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",