linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Krzysztof Kozlowski <krzk@kernel.org>
To: Markus Mayer <markus.mayer@broadcom.com>
Cc: Colin Ian King <colin.king@canonical.com>,
	Florian Fainelli <f.fainelli@gmail.com>,
	BCM Kernel Feedback <bcm-kernel-feedback-list@broadcom.com>,
	Linux Kernel <linux-kernel@vger.kernel.org>,
	Linux ARM Kernel <linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
Date: Fri, 21 Aug 2020 07:40:23 +0200	[thread overview]
Message-ID: <20200821054023.GA3906@kozik-lap> (raw)
In-Reply-To: <20200821010333.20436-1-mmayer@broadcom.com>

On Thu, Aug 20, 2020 at 06:03:33PM -0700, Markus Mayer wrote:
> 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 <mmayer@broadcom.com>
> ---
> 
> 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)

The pointer itself is returned by value and you cannot return a const
value. I mean, you can but it does not have an effect. Only pointed
memory should be const (const const char*).

Best regards,
Krzysztof


> +{
> +	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;
>  }
> -- 
> 2.17.1
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2020-08-21  5:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-21  1:03 [PATCH] memory: brcmstb_dpfe: fix array index out of bounds Markus Mayer
2020-08-21  3:15 ` Florian Fainelli
2020-08-21  4:42 ` kernel test robot
2020-08-21  5:40 ` Krzysztof Kozlowski [this message]
2020-08-21 16:49   ` Markus Mayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200821054023.GA3906@kozik-lap \
    --to=krzk@kernel.org \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=colin.king@canonical.com \
    --cc=f.fainelli@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markus.mayer@broadcom.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).