From patchwork Fri May 20 08:45:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 9129003 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 DDD8860467 for ; Fri, 20 May 2016 08:50:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF5C420265 for ; Fri, 20 May 2016 08:50:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C203F279C4; Fri, 20 May 2016 08:50:36 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 5CBC320265 for ; Fri, 20 May 2016 08:50:36 +0000 (UTC) Received: from localhost ([::1]:53846 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3g8g-00088C-Ng for patchwork-qemu-devel@patchwork.kernel.org; Fri, 20 May 2016 04:50:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53625) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3g4I-0004cZ-Ge for qemu-devel@nongnu.org; Fri, 20 May 2016 04:46:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3g4E-0006We-BL for qemu-devel@nongnu.org; Fri, 20 May 2016 04:46:01 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:34600) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3g4E-0006WZ-0K for qemu-devel@nongnu.org; Fri, 20 May 2016 04:45:58 -0400 Received: by mail-wm0-x241.google.com with SMTP id n129so27304065wmn.1 for ; Fri, 20 May 2016 01:45:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:subject:date:message-id; bh=ZMpbeCeDMQG7kFoCNTvOwNIAedmRWTY6XmUEbw3WDA8=; b=KIjFQevBXYr1VI8zahefhEj8riiitrxCW62WaoTzqJDcVYMOW+Ty7oy8Pj/pxe1qeP nHR9RvjD0tJvtBibV9aOIcf4sXS3n44loTiJcvmBU3QWqiBiXG/1nV3r/5AZSL3g8iMl aWvDeZtrk4OHhhOUTC9izNBmil4Ljus4zib/C+xMvqda8WqTgbEiJeYbPxjqaUBCPa43 m2EkfYEi+w8pel2+kJ7mbI8RF/OfxzjfYfwve12C5Xmdbpgedk9Yrroke9c8lgVtOWEM 1oYBmLfvLiUfmIyf8A9/g+w1OP7k1zGBRpD3GRirQfgMUB9KhnOKLTBrsNXxvopwLnnS g8qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:subject:date:message-id; bh=ZMpbeCeDMQG7kFoCNTvOwNIAedmRWTY6XmUEbw3WDA8=; b=NPkQnxUCYxRDRPuTMuDBm0gJ4M+XUvgaeYU12XDxZrbj03mdwpdXjYiL1qRg1b+vRO qWrMmHqeQWBssemzWYTuSipn3/Pf10T5JmCvhflF6QVcFynU9uNMeWOLb+WjggZQvcWK CxYfnT/AfbYGHDUOVhEU+qsGLwdngSuwxfLcnVRbEMYeirVlI/2BzhrrUWx9QP4N68WK UGMicYlAezckgiYGMJ/Xw9wer46/lmLwtjpK5CI9iiD3o+mxgoPyRZGAjOppEI7jyZvx vQZmIIk48yjWktkIccjuhQ6tJnIzPdp9gTh5Ai4OjlBko+AYlfCMtthnrZDYBeDtWB1f Ep2w== X-Gm-Message-State: AOPr4FUtd+fjitv7Cs/hpmav/sISGY1UEF8AThuIABi2hd65nxtAY9dWa3QHmEHtjU3WEA== X-Received: by 10.194.24.38 with SMTP id r6mr1943435wjf.88.1463733957195; Fri, 20 May 2016 01:45:57 -0700 (PDT) Received: from donizetti.lan (dynamic-adsl-78-12-252-58.clienti.tiscali.it. [78.12.252.58]) by smtp.gmail.com with ESMTPSA id u4sm18650194wjz.4.2016.05.20.01.45.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 May 2016 01:45:55 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 20 May 2016 10:45:53 +0200 Message-Id: <1463733953-23850-1-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.5.5 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH] bt: rewrite csrhci_write to avoid out-of-bounds writes 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: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The usage of INT_MAX in this function confuses Coverity. I think the defect is bogus, however there is no protection against getting more than sizeof(s->inpkt) bytes from the character device backend. Rewrite the function to only fill in as much data as needed from buf into s->inpkt. The plen variable is replaced by a simple state machine and there is no need anymore to shift contents to the beginning of s->inpkt. Signed-off-by: Paolo Bonzini --- hw/bt/hci-csr.c | 67 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/hw/bt/hci-csr.c b/hw/bt/hci-csr.c index e6b8998..d688372 100644 --- a/hw/bt/hci-csr.c +++ b/hw/bt/hci-csr.c @@ -39,9 +39,14 @@ struct csrhci_s { int out_size; uint8_t outfifo[FIFO_LEN * 2]; uint8_t inpkt[FIFO_LEN]; + enum { + CSR_HDR_LEN, + CSR_DATA_LEN, + CSR_DATA + } in_state; int in_len; int in_hdr; - int in_data; + int in_needed; QEMUTimer *out_tm; int64_t baud_delay; @@ -296,38 +301,60 @@ static int csrhci_data_len(const uint8_t *pkt) exit(-1); } +static void csrhci_ready_for_next_inpkt(struct csrhci_s *s) +{ + s->in_state = CSR_HDR_LEN; + s->in_len = 0; + s->in_needed = 2; + s->in_hdr = INT_MAX; +} + static int csrhci_write(struct CharDriverState *chr, const uint8_t *buf, int len) { struct csrhci_s *s = (struct csrhci_s *) chr->opaque; - int plen = s->in_len; + int total = 0; if (!s->enable) return 0; - s->in_len += len; - memcpy(s->inpkt + plen, buf, len); + for (;;) { + int cnt = MIN(len, s->in_needed - s->in_len); + if (cnt) { + memcpy(s->inpkt + s->in_len, buf, cnt); + s->in_len += cnt; + buf += cnt; + len -= cnt; + total += cnt; + } + + if (s->in_len < s->in_needed) { + break; + } - while (1) { - if (s->in_len >= 2 && plen < 2) + if (s->in_state == CSR_HDR_LEN) { s->in_hdr = csrhci_header_len(s->inpkt) + 1; + assert(s->in_hdr >= s->in_needed); + s->in_needed = s->in_hdr; + s->in_state = CSR_DATA_LEN; + continue; + } - if (s->in_len >= s->in_hdr && plen < s->in_hdr) - s->in_data = csrhci_data_len(s->inpkt) + s->in_hdr; + if (s->in_state == CSR_DATA_LEN) { + s->in_needed += csrhci_data_len(s->inpkt); + /* hci_acl_hdr could specify more than 4096 bytes, so assert. */ + assert(s->in_needed <= sizeof(s->inpkt)); + s->in_state = CSR_DATA; + continue; + } - if (s->in_len >= s->in_data) { + if (s->in_state == CSR_DATA) { csrhci_in_packet(s, s->inpkt); - - memmove(s->inpkt, s->inpkt + s->in_len, s->in_len - s->in_data); - s->in_len -= s->in_data; - s->in_hdr = INT_MAX; - s->in_data = INT_MAX; - plen = 0; - } else - break; + csrhci_ready_for_next_inpkt(s); + } } - return len; + return total; } static void csrhci_out_hci_packet_event(void *opaque, @@ -389,11 +416,9 @@ static void csrhci_reset(struct csrhci_s *s) { s->out_len = 0; s->out_size = FIFO_LEN; - s->in_len = 0; + csrhci_ready_for_next_inpkt(s); s->baud_delay = NANOSECONDS_PER_SECOND; s->enable = 0; - s->in_hdr = INT_MAX; - s->in_data = INT_MAX; s->modem_state = 0; /* After a while... (but sooner than 10ms) */