From patchwork Fri Aug 21 01:03:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 11727233 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 99454138C for ; Fri, 21 Aug 2020 01:04:12 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 72993207DE for ; Fri, 21 Aug 2020 01:04:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="lh4DjUv3"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="HazYeEzK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 72993207DE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Owner; bh=gRKEA73OF+mK3SNJjeKfAo0iqV/zRZt4yf9ZrYrNigE=; b=lh4DjUv380O7yv+CRNsJOwKqNz H3GJlVtNlgC/yjUUkDnX7R0sROOkonPPlA6IqneBqL7qpZxx3bczqe9KIn+b6hGeLqqFDrlmYkbbQ +J4FFSwyWrEMogVNe/ph3j0qUYqjDHDkxw3JNoSEeXHmTfltbguK8lIFdbDwjHvTREW65MOoSn6wd cu1geMyK8c5ScmGfLBTBrKPSUWHBQU69YkrutUI04YuMnlGPlggaG1/fE3EtboVJ6nfAlz19x+TN4 PGo6R//OHoyhx128uSbYOUIB287rDspzQ7D7VMGyQF+OxFycyW/bdyoRn4Z4K2Q1Py2rsU+ma4tfG 4FdRw1PQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k8vTD-0003fl-OZ; Fri, 21 Aug 2020 01:03:51 +0000 Received: from rnd-relay.smtp.broadcom.com ([192.19.229.170]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k8vT9-0003ec-Qc for linux-arm-kernel@lists.infradead.org; Fri, 21 Aug 2020 01:03:49 +0000 Received: from mail-irv-17.broadcom.com (mail-irv-17.lvn.broadcom.net [10.75.242.48]) by rnd-relay.smtp.broadcom.com (Postfix) with ESMTP id 9C2BE30C016; Thu, 20 Aug 2020 18:01:04 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.10.3 rnd-relay.smtp.broadcom.com 9C2BE30C016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=broadcom.com; s=dkimrelay; t=1597971664; bh=gfV9uR728yHTYUxg7H5ULAzW8rEUVFV045F+OHaYPqQ=; h=From:To:Cc:Subject:Date:From; b=HazYeEzKIGHdcBMStV0QAJpJ20cR1oqFarwW88uBZ3id4PtnGXyXZvPmW/LWkaddu TPIzdRAx9W0Db4XCVEUFJH5SXrhH38bfDK+IJdOPr46EfJLrAbU/UsEAjAMnjIsnkM rtvGP7HrXL92HHD2EOXPxEYsMerocf4crc9/r/S4= Received: from lbrmn-mmayer.ric.broadcom.net (lbrmn-mmayer.ric.broadcom.net [10.136.28.150]) by mail-irv-17.broadcom.com (Postfix) with ESMTP id BA7B214008B; Thu, 20 Aug 2020 18:03:38 -0700 (PDT) From: Markus Mayer To: Florian Fainelli , Colin Ian King , Krzysztof Kozlowski Subject: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds Date: Thu, 20 Aug 2020 18:03:33 -0700 Message-Id: <20200821010333.20436-1-mmayer@broadcom.com> X-Mailer: git-send-email 2.17.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200820_210347_943047_F32E1471 X-CRM114-Status: GOOD ( 15.09 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [192.19.229.170 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 DKIMWL_WL_HIGH DKIMwl.org - Whitelisted High sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: BCM Kernel Feedback , Linux ARM Kernel , Markus Mayer , Linux Kernel MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org We would overrun the error_text array if we hit a TIMEOUT condition, because we were using the error code "ETIMEDOUT" (which is 110) as an array index. We fix the problem by correcting the array index and by providing a function to retrieve error messages rather than accessing the array directly. The function includes a bounds check that prevents the array from being overrun. Signed-off-by: Markus Mayer Acked-by: Florian Fainelli Reported-by: Colin Ian King Reported-by: kernel test robot --- This patch was prepared in response to https://lkml.org/lkml/2020/8/18/505. drivers/memory/brcmstb_dpfe.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/memory/brcmstb_dpfe.c b/drivers/memory/brcmstb_dpfe.c index 81abc4a98a27..a986a849f58e 100644 --- a/drivers/memory/brcmstb_dpfe.c +++ b/drivers/memory/brcmstb_dpfe.c @@ -190,11 +190,6 @@ struct brcmstb_dpfe_priv { struct mutex lock; }; -static const char * const error_text[] = { - "Success", "Header code incorrect", "Unknown command or argument", - "Incorrect checksum", "Malformed command", "Timed out", -}; - /* * Forward declaration of our sysfs attribute functions, so we can declare the * attribute data structures early. @@ -307,6 +302,20 @@ static const struct dpfe_api dpfe_api_v3 = { }, }; +static const char * const get_error_text(unsigned int i) +{ + static const char * const error_text[] = { + "Success", "Header code incorrect", + "Unknown command or argument", "Incorrect checksum", + "Malformed command", "Timed out", "Unknown error", + }; + + if (unlikely(i >= ARRAY_SIZE(error_text))) + i = ARRAY_SIZE(error_text) - 1; + + return error_text[i]; +} + static bool is_dcpu_enabled(struct brcmstb_dpfe_priv *priv) { u32 val; @@ -446,7 +455,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 */ @@ -695,7 +704,7 @@ static ssize_t generic_show(unsigned int command, u32 response[], ret = __send_command(priv, command, response); if (ret < 0) - return sprintf(buf, "ERROR: %s\n", error_text[-ret]); + return sprintf(buf, "ERROR: %s\n", get_error_text(-ret)); return 0; }