Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1406376pxa; Thu, 20 Aug 2020 10:25:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuIaRfM5fqhO4w8YxxNucf6D4kyN6VYSdwDyRdpUE/Gnu4tlMIHiL3vpdMeBa4MCyRSVCF X-Received: by 2002:a17:906:3655:: with SMTP id r21mr4472980ejb.248.1597944335495; Thu, 20 Aug 2020 10:25:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597944335; cv=none; d=google.com; s=arc-20160816; b=j0VDjdwjs3UM3R0QVA60e4JnZcdS4ambdUHLFkuoEU0p9B5j0s/pDBoJdkxm69jTCR JzoZhOHq5lClnGT45QuWMZ5htQSjefeIdE6QaLrxNQ/ao0E3bLiHiL9bhf+XL3iiN5v5 gZadY3cJ5O0/q5wfwnrKw85RXx9od+gJrJIcf6JFPslxhd0budvatCXfHzMD04mJy4HZ dhRrI1UtWL2Hyz6Bh/cm6kYABGAWl+Q6AyJ8wa7Nmv/Vr/wKQ8gYf0bE6qIIzlet2rRZ TPlVAf9UlErWQvlcpkBEHzE7YBSXfDb+O4Gjsp0+ROhNZYldSdPfZJn//wVFOqVkOFmu SZOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=FSEoi89dHfhzP32ddR5j968nwd71ucC9yk+h6uBOV28=; b=i2LzrakjPZN+TjeyGkkyKmSlg55WSQBmO1kl/pE7T6LSf4iZSk9YkI4hYIZsHAtRUq 7ZOQxFXBquXEHi0LJbOq4mMxCh0TAXt6jqyPhfOfRR8PLP+LG3zklXlrsDUkT9WmTis3 1REX+8GQYdXjfWT8JtBGWx5LroqTpvV0+8iZ6xVaqqdkuJX1tXpuEq81fKJHcdc7EMLs 21y7sinP2esm2rBLjpCwj4/x5QqcZEkmWD9WoPjOglfubRZMmcgdeKFJz1hS8S/Uiv/q KoswizUYhR6R49d/c3gJgk2CkzCtdEVI31H3Ao8bA2D00cgZCSS3i4oFfKtJitRXiWvu flkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=FxtAaQHp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u18si1584839ejr.700.2020.08.20.10.25.11; Thu, 20 Aug 2020 10:25:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=FxtAaQHp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730191AbgHTRYA (ORCPT + 99 others); Thu, 20 Aug 2020 13:24:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728132AbgHTRX7 (ORCPT ); Thu, 20 Aug 2020 13:23:59 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB5D7C061385 for ; Thu, 20 Aug 2020 10:23:58 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id kq25so3454708ejb.3 for ; Thu, 20 Aug 2020 10:23:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=FSEoi89dHfhzP32ddR5j968nwd71ucC9yk+h6uBOV28=; b=FxtAaQHphv14xbyM7ngVPajpiRbWySCAkBgXGB3jsIyE2OrnpIDSuLLNPHbwWREaNi viUeJr9a0BpFBvV8kooP7S7PxLqua3ElF/xNUAqGdzEzycHzxJtqeUS0EHuN9jEvUUkL 7WDFeQ72BQkrRIhIwqay7/HZQ81jZDjZQn+sc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FSEoi89dHfhzP32ddR5j968nwd71ucC9yk+h6uBOV28=; b=NFEBwh5ApFA8DOc7n0yfMDI7tsONOZ7uEsoea8RymIpS/Oq64yEswGljPlAhNkX2WD BCLh1t2c7pew2Q8VFFe3Xw8RAGcpwewAFbREOXbcpuGX7o5qT2nTAPuKFrdyCNrouUIe DuD0I00J3r8LR1TXmlkzwV3K5YXwsJRHKUavdKsWLuQ2wVgZFuSoUXGyLfE0PGO2wNrl Ky+OEz2bTL7iqKaF49XocFAUspApH6FKxDZVCY04+o5fLnk5uGlESH9jVm/3UJogbH+8 KMhLIAtXfvAtgidkc1/WTxp3DeCZPN24Mzwh4uyvDYaOVDd8NgT8RAdIbnV8au9htkwP hxKg== X-Gm-Message-State: AOAM531TZuHsGD1gpXvOpzVkuZgLmmWPy6Cbt5+E8kGXkbwSXInPIB2M ZgLgOFhLcKyLawYacr5EKGBimsUMO5R2xQWYb18qCQ== X-Received: by 2002:a17:906:d187:: with SMTP id c7mr4444516ejz.196.1597944236714; Thu, 20 Aug 2020 10:23:56 -0700 (PDT) MIME-Version: 1.0 References: <38d00022-730c-948a-917c-d86382df8cb9@canonical.com> <4acc34c4-9c17-41f7-2e39-7eda16792572@gmail.com> In-Reply-To: <4acc34c4-9c17-41f7-2e39-7eda16792572@gmail.com> From: Markus Mayer Date: Thu, 20 Aug 2020 10:23:45 -0700 Message-ID: Subject: Re: out of bounds access on array error_text[] because of -ETIMEDOUT return from __send_command() To: Florian Fainelli Cc: Colin Ian King , BCM Kernel Feedback , Krzysztof Kozlowski , Linux ARM Kernel , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 19 Aug 2020 at 11:34, Florian Fainelli wrote: > > On 8/18/20 5:21 AM, Colin Ian King wrote: > > Hi, > > > > static analysis with coverity has found a buffer overflow issue with the > > brcmstb driver, I believe it may have been introduced with the following > > commit: > > > > commit a7c25759d8d84b64c437a78f05df7314b02934e5 > > Author: Markus Mayer > > Date: Tue Apr 2 16:01:00 2019 -0700 > > > > memory: brcmstb: dpfe: wait for DCPU to be ready > > > > The static analysis is as follows for the source file > > /drivers/memory/brcmstb_dpfe.c : > > > > 684 static ssize_t generic_show(unsigned int command, u32 response[], > > 685 struct brcmstb_dpfe_priv *priv, char *buf) > > 686 { > > 687 int ret; > > 688 > > 1. Condition !priv, taking false branch. > > > > 689 if (!priv) > > 690 return sprintf(buf, "ERROR: driver private data not > > set\n"); > > 691 > > 2. return_constant: Function call __send_command(priv, command, > > response) may return -110. > > 3. assignment: Assigning: ret = __send_command(priv, command, > > response). The value of ret is now -110. > > > > 692 ret = __send_command(priv, command, response); > > 4. Condition ret < 0, taking true branch. > > > > 693 if (ret < 0) > > > > Out-of-bounds read (OVERRUN) > > 5. overrun-local: Overrunning array error_text of 6 8-byte elements > > at element index 110 (byte offset 887) using index -ret (which evaluates > > to 110). > > 694 return sprintf(buf, "ERROR: %s\n", error_text[-ret]); > > 695 > > 696 return 0; > > 697 } > > > > > > Function __send_command() can return -ETIMEDOUT and this causes an > > out-of-bounds access on error_text[]. > > Markus, what do you think of this: > > diff --git a/drivers/memory/brcmstb_dpfe.c b/drivers/memory/brcmstb_dpfe.c > index 60e8633b1175..b41c6251ddc3 100644 > --- a/drivers/memory/brcmstb_dpfe.c > +++ b/drivers/memory/brcmstb_dpfe.c > @@ -445,7 +445,7 @@ static int __send_command(struct brcmstb_dpfe_priv > *priv, unsigned int cmd, > } > if (resp != 0) { > mutex_unlock(&priv->lock); > - return -ETIMEDOUT; > + return -ffs(DCPU_RET_ERR_TIMEDOUT); > } > > /* Compute checksum over the message */ > > That way we only return DCPU-style error code from __send_command and we > de-reference error_text accordingly? Or we could just introduce a proper > lookup with a function instead of a direct array de-reference. Let me do some experiments. What you are proposing should work and is in line with the current code. A lookup function might be cleaner, though. Thanks, -Markus > -- > Florian