Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1673663pxa; Thu, 20 Aug 2020 18:09:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwq5/ipq5oLpdBLmriavE7PTXkFkPH47GwLYAFLDdz21TWk1b2onbKkDvGrQvMveuqP+vrr X-Received: by 2002:a05:6402:30a5:: with SMTP id df5mr577120edb.18.1597972197694; Thu, 20 Aug 2020 18:09:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597972197; cv=none; d=google.com; s=arc-20160816; b=L5el2MjfTxFKPlnsCl11toQGlahKoeHhZIdehZy/dU5WtebkxrY8FnM9WGjl5TMEGZ FwF/2mqt0K33MCVKNd8+Bn3MrWh+Ll5gsBeX3tnYFEclt+hfarx1BXEbin+CWWxXWxWl 3RJllyrm8p+JXCwEeAo1hHW4MON3zCsZ0jOpUtLXXLW4QeByoT8HSaG0JZPMHhmsaXol 7nVncF6hJQqkCnlL7ectG5FkOwDnxBUmGycbD6CAnRnAitfIqu8BIFaMUYBlWuPwOW1Q AHmIeqKoeJq9iYTSppZG4iLYbdtZtiDFColPCIlIYicVbTUnuz3VSM0mxe4Cl5OgMb86 9r5Q== 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=JSXxxVxBZt5qj3H+1yv/ZiFeWKO2vqSUz4tnuOVfh9U=; b=yGIDNs+TwBTN91+mtbs0BhQ9bhAjp9tTsPexYWFZQlssNKVk+pTZial72C2eu0b8oe ENUrxjBhEXCVWaZlwWH7J54+K2zuSaCQ8HN2gN9hsRHztoqSZu56Y3s7XJL0F9gr5/dL BdLoNy0jOiGETpTECvSoxlCkN9nWv/b9WBM2Wt9xefud/b1mWPALbgRyqgI0PsVVA0O1 OaiaVo1N46dJn3gGb7tdLamfv2NTO2b06NjD1B5lL7s0cbmUv2xDUeLityffDiTvs7PB WKYOeHLHQLWXUUrdd9TZNA3rnaq7ukrSAT/+FMtYhhEOFKzoPiJCSDPa+Hcw0mZETZ/S UyXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=Ox6lZUEy; 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 dn2si171658edb.378.2020.08.20.18.09.31; Thu, 20 Aug 2020 18:09:57 -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=Ox6lZUEy; 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 S1726841AbgHUBFJ (ORCPT + 99 others); Thu, 20 Aug 2020 21:05:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726309AbgHUBFF (ORCPT ); Thu, 20 Aug 2020 21:05:05 -0400 Received: from mail-ed1-x544.google.com (mail-ed1-x544.google.com [IPv6:2a00:1450:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A5FAC061385 for ; Thu, 20 Aug 2020 18:05:05 -0700 (PDT) Received: by mail-ed1-x544.google.com with SMTP id b2so130415edw.5 for ; Thu, 20 Aug 2020 18:05:05 -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=JSXxxVxBZt5qj3H+1yv/ZiFeWKO2vqSUz4tnuOVfh9U=; b=Ox6lZUEybubjhfdPiOKWRZG4qfFEecYwQvufT8kMPiwpTg8/545YywiMSwSKdQaFYQ 8yTK33s5XJHGaIh16ow88NuFxVBZGDqNfHwhECe1FeiLkw1V9pzS7KCBEv4JMM/Dl6t+ RmyCaLAagpkumwFoeHNEK99TcKaEkoCH8sEBo= 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=JSXxxVxBZt5qj3H+1yv/ZiFeWKO2vqSUz4tnuOVfh9U=; b=rXcpnNkulatF43oprH8qW/IYH6cBL4vdkGxhP/hHxnFj4w6200vlU5zyqoSeIiSlLq y5JR0urLM8C7HHkv1phCvKUToYgesChJcnfqYj3aqX2Y8wjcT54Ppjq5mz/fEUu0tT8l fWv8HmMiCGnguAzn/Bj+Fu8SYP0dx/LECBjwFapsyphjrkFSjlPuWVmByRFicJSJy7W6 +T7Gn/V8MzhQ4Ba6BXkifT0XmavnETAmca9v1qktJ2Z0YR4sMdzaxA7aB8keUFu3jY8K Uhw2QSItxiVcuPEgbkjqMetaK61kWa50V/+c200sB3phY8/1Th3lek92xbshhXq1w2XA d/9Q== X-Gm-Message-State: AOAM531BmW3YNcwXwczXvXXpl+TJL6eQrrtpsNs0R9alQecvZJgkRJIk dmqI6znyCdxJ5Yg6LkdBs3scZoesaQVB83RhkcYkeA== X-Received: by 2002:a05:6402:297:: with SMTP id l23mr528464edv.145.1597971903431; Thu, 20 Aug 2020 18:05:03 -0700 (PDT) MIME-Version: 1.0 References: <38d00022-730c-948a-917c-d86382df8cb9@canonical.com> <4acc34c4-9c17-41f7-2e39-7eda16792572@gmail.com> In-Reply-To: From: Markus Mayer Date: Thu, 20 Aug 2020 18:04:52 -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 Thu, 20 Aug 2020 at 10:23, Markus Mayer wrote: > > 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. I submitted a patch for review. See https://lkml.org/lkml/2020/8/20/2291. Regards, -Markus