From patchwork Sat Aug 22 20:50:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 11731291 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 A9252913 for ; Sat, 22 Aug 2020 20:51:51 +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 81B342072D for ; Sat, 22 Aug 2020 20:51:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Ii7KrNd2"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="Hhab7Toa" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 81B342072D 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=LWGhJfkeBdvkdHa3PAvjOkcRY8sYeqa+DiBMaBGoKIs=; b=Ii7KrNd2HxeCqGU7dO6CWKMKE1 qKnT917xxKVZ3daCGewrQodxcY9HAzZv7wZuo3bBb23VQaTNXplQ+9mTaB0sJ2OfjsAM8vegH9nKD uc3z2Qf3n7oyj7uNCyNY+3URt7OR+iit1KjwF0Hu8P2t2gy49EBsf9BqjBwV3zWQ4pu4KGkcNTz60 1RlEmGxDBKSR8s7IuwY5pgTMN1tddvOq6WsxxHAELuufsILCDd8PwlGRam1X/UeRJVbCqdzOplXDa XL0jJVjWRO+AV1d2yP/g7Ur4sFZZUeFbfUBVE87H1I76K2enX6qX/+4xL2sp2CBykt5uaFqXU3SNm ISBoMQHg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k9aT1-0003vf-Ff; Sat, 22 Aug 2020 20:50:23 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k9aSw-0003uF-JS for linux-arm-kernel@lists.infradead.org; Sat, 22 Aug 2020 20:50:19 +0000 Received: by mail-wr1-x444.google.com with SMTP id o4so991100wrn.0 for ; Sat, 22 Aug 2020 13:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id; bh=awS83gVg339WvkVehjyxkJ0tva7kYJAQiAAlkyDZdCY=; b=Hhab7Toa7JqQg6Fs3xCaGrLLkK7u5Ls3HVTVp62/yPtGHAdmEI+CGJqcQx+Zsr5uRs LRusOTPsSwoHzk5sO6LFtSmubjbUWgXURg4s1njMDIWMXOSBWtYjhv09oxEi7rt0zIXY 7RAQ6GQsDp2PGMoc2o20aAl7QqTa+UhprHOak= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=awS83gVg339WvkVehjyxkJ0tva7kYJAQiAAlkyDZdCY=; b=kzKRkcSWTHiWhgBnijuARft2AjwvM7bgS40CuPII3V9AnL+hqL96Llk63AUclYUOdh FmcDm10I+vIDaSOYS9r7cYM4FgfGvFN+dzA2m3TqRjP2iipvNt40BvAcRKMPfJbEkURY UkUeEpkzX3GdsEP6Y2LoROAqE60ko9L4PDKkA3LX/7S+dt+j0SqUxnH5l22GCyB/nrdb DopB0U/kHHTo1UJdeQCJg7tb7wqJlCZOpsl/D9Fb/AfFycLCmlVSoRfPT17Wv79d2/7W 0sK99jMJ3zJXqV31p0xJdGlL/5Bk758L+srv4YpjZXcGiVFH9wYP3oAb1m2I/6A2UVC9 VL3Q== X-Gm-Message-State: AOAM5323TIWg8uOZLdSZ8AU3AxC4BDS98/8mhIaJYYKCwO/EcZdxlvCk DpQ60AJUykQHwjd7T4eyOOE2SA== X-Google-Smtp-Source: ABdhPJxynv1Nm0KoX+Ru6/XDn803cx9lbNeDxDE59Kp8xjg81nYzHC9GGVXUlo/Af2VHftP5h/3wtg== X-Received: by 2002:a5d:544f:: with SMTP id w15mr8363433wrv.208.1598129416035; Sat, 22 Aug 2020 13:50:16 -0700 (PDT) Received: from lbrmn-mmayer.ric.broadcom.net ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id n18sm13041935wrp.58.2020.08.22.13.50.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Aug 2020 13:50:15 -0700 (PDT) Received: by lbrmn-mmayer.ric.broadcom.net (Postfix, from userid 1000) id E4FF6212A928; Sat, 22 Aug 2020 13:50:12 -0700 (PDT) From: Markus Mayer To: Florian Fainelli , Colin Ian King , Krzysztof Kozlowski Subject: [PATCH v3] memory: brcmstb_dpfe: fix array index out of bounds Date: Sat, 22 Aug 2020 13:50:00 -0700 Message-Id: <20200822205000.15841-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-20200822_165018_743499_D33E2423 X-CRM114-Status: GOOD ( 18.42 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:444 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_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -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. This patch was prepared in response to https://lkml.org/lkml/2020/8/18/505. Signed-off-by: Markus Mayer Acked-by: Florian Fainelli Fixes: 2f330caff577 ("memory: brcmstb: Add driver for DPFE") Reported-by: Colin Ian King --- Changes since v2: - Added "Fixes:" and "Reported-by:" - Fixed "From" address - No changes to the code Changes since v1: - Added link of the coverity report to the commit message. - Added Florian's ack. - Removed second "const" from get_error_text() return type (thanks to the kernel test robot). 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 e08528b12cbd..dcf50bb8dd69 100644 --- a/drivers/memory/brcmstb_dpfe.c +++ b/drivers/memory/brcmstb_dpfe.c @@ -188,11 +188,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 *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; @@ -445,7 +454,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; }