All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21  1:03 ` Markus Mayer
  0 siblings, 0 replies; 11+ messages in thread
From: Markus Mayer @ 2020-08-21  1:03 UTC (permalink / raw)
  To: Florian Fainelli, Colin Ian King, Krzysztof Kozlowski
  Cc: Markus Mayer, BCM Kernel Feedback, Linux ARM Kernel, Linux Kernel

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)
+{
+	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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21  1:03 ` Markus Mayer
  0 siblings, 0 replies; 11+ messages in thread
From: Markus Mayer @ 2020-08-21  1:03 UTC (permalink / raw)
  To: Florian Fainelli, Colin Ian King, Krzysztof Kozlowski
  Cc: BCM Kernel Feedback, Linux ARM Kernel, Markus Mayer, Linux Kernel

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)
+{
+	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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
  2020-08-21  1:03 ` Markus Mayer
@ 2020-08-21  3:15   ` Florian Fainelli
  -1 siblings, 0 replies; 11+ messages in thread
From: Florian Fainelli @ 2020-08-21  3:15 UTC (permalink / raw)
  To: Markus Mayer, Colin Ian King, Krzysztof Kozlowski
  Cc: BCM Kernel Feedback, Linux ARM Kernel, Linux Kernel



On 8/20/2020 6:03 PM, 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>

Acked-by: Florian Fainelli <f.fainelli@gmail.com>
Fixes: 2f330caff577 ("memory: brcmstb: Add driver for DPFE")
Reported-by: Colin Ian King <colin.king@canonical.com>

(Colin, was there a specific coverity ID you wanted to use?)
-- 
Florian

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21  3:15   ` Florian Fainelli
  0 siblings, 0 replies; 11+ messages in thread
From: Florian Fainelli @ 2020-08-21  3:15 UTC (permalink / raw)
  To: Markus Mayer, Colin Ian King, Krzysztof Kozlowski
  Cc: BCM Kernel Feedback, Linux Kernel, Linux ARM Kernel



On 8/20/2020 6:03 PM, 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>

Acked-by: Florian Fainelli <f.fainelli@gmail.com>
Fixes: 2f330caff577 ("memory: brcmstb: Add driver for DPFE")
Reported-by: Colin Ian King <colin.king@canonical.com>

(Colin, was there a specific coverity ID you wanted to use?)
-- 
Florian

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
  2020-08-21  1:03 ` Markus Mayer
  (?)
@ 2020-08-21  4:42   ` kernel test robot
  -1 siblings, 0 replies; 11+ messages in thread
From: kernel test robot @ 2020-08-21  4:42 UTC (permalink / raw)
  To: Markus Mayer, Florian Fainelli, Colin Ian King, Krzysztof Kozlowski
  Cc: kbuild-all, Markus Mayer, BCM Kernel Feedback, Linux ARM Kernel,
	Linux Kernel

[-- Attachment #1: Type: text/plain, Size: 2279 bytes --]

Hi Markus,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.9-rc1 next-20200820]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Markus-Mayer/memory-brcmstb_dpfe-fix-array-index-out-of-bounds/20200821-090533
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git da2968ff879b9e74688cdc658f646971991d2c56
config: arm-defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/memory/brcmstb_dpfe.c:305:8: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     305 | static const char * const get_error_text(unsigned int i)
         |        ^~~~~

# https://github.com/0day-ci/linux/commit/a2de88715f98369b7e4478457a6455c3e2c72725
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Markus-Mayer/memory-brcmstb_dpfe-fix-array-index-out-of-bounds/20200821-090533
git checkout a2de88715f98369b7e4478457a6455c3e2c72725
vim +305 drivers/memory/brcmstb_dpfe.c

   304	
 > 305	static const char * const get_error_text(unsigned int i)
   306	{
   307		static const char * const error_text[] = {
   308			"Success", "Header code incorrect",
   309			"Unknown command or argument", "Incorrect checksum",
   310			"Malformed command", "Timed out", "Unknown error",
   311		};
   312	
   313		if (unlikely(i >= ARRAY_SIZE(error_text)))
   314			i = ARRAY_SIZE(error_text) - 1;
   315	
   316		return error_text[i];
   317	}
   318	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 53061 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21  4:42   ` kernel test robot
  0 siblings, 0 replies; 11+ messages in thread
From: kernel test robot @ 2020-08-21  4:42 UTC (permalink / raw)
  To: Markus Mayer, Florian Fainelli, Colin Ian King, Krzysztof Kozlowski
  Cc: BCM Kernel Feedback, kbuild-all, Linux ARM Kernel, Markus Mayer,
	Linux Kernel

[-- Attachment #1: Type: text/plain, Size: 2279 bytes --]

Hi Markus,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.9-rc1 next-20200820]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Markus-Mayer/memory-brcmstb_dpfe-fix-array-index-out-of-bounds/20200821-090533
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git da2968ff879b9e74688cdc658f646971991d2c56
config: arm-defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/memory/brcmstb_dpfe.c:305:8: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     305 | static const char * const get_error_text(unsigned int i)
         |        ^~~~~

# https://github.com/0day-ci/linux/commit/a2de88715f98369b7e4478457a6455c3e2c72725
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Markus-Mayer/memory-brcmstb_dpfe-fix-array-index-out-of-bounds/20200821-090533
git checkout a2de88715f98369b7e4478457a6455c3e2c72725
vim +305 drivers/memory/brcmstb_dpfe.c

   304	
 > 305	static const char * const get_error_text(unsigned int i)
   306	{
   307		static const char * const error_text[] = {
   308			"Success", "Header code incorrect",
   309			"Unknown command or argument", "Incorrect checksum",
   310			"Malformed command", "Timed out", "Unknown error",
   311		};
   312	
   313		if (unlikely(i >= ARRAY_SIZE(error_text)))
   314			i = ARRAY_SIZE(error_text) - 1;
   315	
   316		return error_text[i];
   317	}
   318	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 53061 bytes --]

[-- Attachment #3: Type: text/plain, Size: 176 bytes --]

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21  4:42   ` kernel test robot
  0 siblings, 0 replies; 11+ messages in thread
From: kernel test robot @ 2020-08-21  4:42 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 2335 bytes --]

Hi Markus,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.9-rc1 next-20200820]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Markus-Mayer/memory-brcmstb_dpfe-fix-array-index-out-of-bounds/20200821-090533
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git da2968ff879b9e74688cdc658f646971991d2c56
config: arm-defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/memory/brcmstb_dpfe.c:305:8: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     305 | static const char * const get_error_text(unsigned int i)
         |        ^~~~~

# https://github.com/0day-ci/linux/commit/a2de88715f98369b7e4478457a6455c3e2c72725
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Markus-Mayer/memory-brcmstb_dpfe-fix-array-index-out-of-bounds/20200821-090533
git checkout a2de88715f98369b7e4478457a6455c3e2c72725
vim +305 drivers/memory/brcmstb_dpfe.c

   304	
 > 305	static const char * const get_error_text(unsigned int i)
   306	{
   307		static const char * const error_text[] = {
   308			"Success", "Header code incorrect",
   309			"Unknown command or argument", "Incorrect checksum",
   310			"Malformed command", "Timed out", "Unknown error",
   311		};
   312	
   313		if (unlikely(i >= ARRAY_SIZE(error_text)))
   314			i = ARRAY_SIZE(error_text) - 1;
   315	
   316		return error_text[i];
   317	}
   318	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 53061 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
  2020-08-21  1:03 ` Markus Mayer
@ 2020-08-21  5:40   ` Krzysztof Kozlowski
  -1 siblings, 0 replies; 11+ messages in thread
From: Krzysztof Kozlowski @ 2020-08-21  5:40 UTC (permalink / raw)
  To: Markus Mayer
  Cc: Florian Fainelli, Colin Ian King, BCM Kernel Feedback,
	Linux ARM Kernel, Linux Kernel

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
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21  5:40   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 11+ messages in thread
From: Krzysztof Kozlowski @ 2020-08-21  5:40 UTC (permalink / raw)
  To: Markus Mayer
  Cc: Colin Ian King, Florian Fainelli, BCM Kernel Feedback,
	Linux Kernel, Linux ARM Kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
  2020-08-21  5:40   ` Krzysztof Kozlowski
@ 2020-08-21 16:49     ` Markus Mayer
  -1 siblings, 0 replies; 11+ messages in thread
From: Markus Mayer @ 2020-08-21 16:49 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Florian Fainelli, Colin Ian King, BCM Kernel Feedback,
	Linux ARM Kernel, Linux Kernel

On Thu, 20 Aug 2020 at 22:40, Krzysztof Kozlowski <krzk@kernel.org> wrote:
>
> 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*).

v2 is on the way.

Regards,
-Markus

> > +{
> > +     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
> >

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] memory: brcmstb_dpfe: fix array index out of bounds
@ 2020-08-21 16:49     ` Markus Mayer
  0 siblings, 0 replies; 11+ messages in thread
From: Markus Mayer @ 2020-08-21 16:49 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Colin Ian King, Florian Fainelli, BCM Kernel Feedback,
	Linux Kernel, Linux ARM Kernel

On Thu, 20 Aug 2020 at 22:40, Krzysztof Kozlowski <krzk@kernel.org> wrote:
>
> 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*).

v2 is on the way.

Regards,
-Markus

> > +{
> > +     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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-08-21 16:52 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-21  1:03 [PATCH] memory: brcmstb_dpfe: fix array index out of bounds Markus Mayer
2020-08-21  1:03 ` Markus Mayer
2020-08-21  3:15 ` Florian Fainelli
2020-08-21  3:15   ` Florian Fainelli
2020-08-21  4:42 ` kernel test robot
2020-08-21  4:42   ` kernel test robot
2020-08-21  4:42   ` kernel test robot
2020-08-21  5:40 ` Krzysztof Kozlowski
2020-08-21  5:40   ` Krzysztof Kozlowski
2020-08-21 16:49   ` Markus Mayer
2020-08-21 16:49     ` Markus Mayer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.