linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: dw: Avoid stack content exposure
@ 2021-02-11 20:37 Kees Cook
  2021-02-11 21:49 ` Serge Semin
  2021-02-12 14:01 ` Mark Brown
  0 siblings, 2 replies; 3+ messages in thread
From: Kees Cook @ 2021-02-11 20:37 UTC (permalink / raw)
  To: linux-kernel, Mark Brown
  Cc: Kees Cook, Serge Semin, Ramil Zaripov, Gustavo A. R. Silva,
	linux-spi, linux-hardening

Since "data" is u32, &data is a "u32 *" type, which means pointer math
will move in u32-sized steps. This was meant to be a byte offset, so
cast &data to "char *" to aim the copy into the correct location.

Seen with -Warray-bounds (and found by Coverity):

In file included from ./include/linux/string.h:269,
                 from ./arch/powerpc/include/asm/paca.h:15,
                 from ./arch/powerpc/include/asm/current.h:13,
                 from ./include/linux/mutex.h:14,
                 from ./include/linux/notifier.h:14,
                 from ./include/linux/clk.h:14,
                 from drivers/spi/spi-dw-bt1.c:12:
In function 'memcpy',
    inlined from 'dw_spi_bt1_dirmap_copy_from_map' at drivers/spi/spi-dw-bt1.c:87:3:
./include/linux/fortify-string.h:20:29: warning: '__builtin_memcpy' offset 4 is out of the bounds [0, 4] of object 'data' with type 'u32' {aka 'unsigned int'} [-Warray-bounds]
   20 | #define __underlying_memcpy __builtin_memcpy
      |                             ^
./include/linux/fortify-string.h:191:9: note: in expansion of macro '__underlying_memcpy'
  191 |  return __underlying_memcpy(p, q, size);
      |         ^~~~~~~~~~~~~~~~~~~
drivers/spi/spi-dw-bt1.c: In function 'dw_spi_bt1_dirmap_copy_from_map':
drivers/spi/spi-dw-bt1.c:77:6: note: 'data' declared here
   77 |  u32 data;
      |      ^~~~

Addresses-Coverity: CID 1497771 Out-of-bounds access
Fixes: abf00907538e ("spi: dw: Add Baikal-T1 SPI Controller glue driver")
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/spi/spi-dw-bt1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/spi/spi-dw-bt1.c b/drivers/spi/spi-dw-bt1.c
index 4aa8596fb1f2..5be6b7b80c21 100644
--- a/drivers/spi/spi-dw-bt1.c
+++ b/drivers/spi/spi-dw-bt1.c
@@ -84,7 +84,7 @@ static void dw_spi_bt1_dirmap_copy_from_map(void *to, void __iomem *from, size_t
 	if (shift) {
 		chunk = min_t(size_t, 4 - shift, len);
 		data = readl_relaxed(from - shift);
-		memcpy(to, &data + shift, chunk);
+		memcpy(to, (char *)&data + shift, chunk);
 		from += chunk;
 		to += chunk;
 		len -= chunk;
-- 
2.25.1


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

* Re: [PATCH] spi: dw: Avoid stack content exposure
  2021-02-11 20:37 [PATCH] spi: dw: Avoid stack content exposure Kees Cook
@ 2021-02-11 21:49 ` Serge Semin
  2021-02-12 14:01 ` Mark Brown
  1 sibling, 0 replies; 3+ messages in thread
From: Serge Semin @ 2021-02-11 21:49 UTC (permalink / raw)
  To: Kees Cook
  Cc: Serge Semin, linux-kernel, Mark Brown, Ramil Zaripov,
	Gustavo A. R. Silva, linux-spi, linux-hardening

On Thu, Feb 11, 2021 at 12:37:14PM -0800, Kees Cook wrote:
> Since "data" is u32, &data is a "u32 *" type, which means pointer math
> will move in u32-sized steps. This was meant to be a byte offset, so
> cast &data to "char *" to aim the copy into the correct location.
> 
> Seen with -Warray-bounds (and found by Coverity):
> 
> In file included from ./include/linux/string.h:269,
>                  from ./arch/powerpc/include/asm/paca.h:15,
>                  from ./arch/powerpc/include/asm/current.h:13,
>                  from ./include/linux/mutex.h:14,
>                  from ./include/linux/notifier.h:14,
>                  from ./include/linux/clk.h:14,
>                  from drivers/spi/spi-dw-bt1.c:12:
> In function 'memcpy',
>     inlined from 'dw_spi_bt1_dirmap_copy_from_map' at drivers/spi/spi-dw-bt1.c:87:3:
> ./include/linux/fortify-string.h:20:29: warning: '__builtin_memcpy' offset 4 is out of the bounds [0, 4] of object 'data' with type 'u32' {aka 'unsigned int'} [-Warray-bounds]
>    20 | #define __underlying_memcpy __builtin_memcpy
>       |                             ^
> ./include/linux/fortify-string.h:191:9: note: in expansion of macro '__underlying_memcpy'
>   191 |  return __underlying_memcpy(p, q, size);
>       |         ^~~~~~~~~~~~~~~~~~~
> drivers/spi/spi-dw-bt1.c: In function 'dw_spi_bt1_dirmap_copy_from_map':
> drivers/spi/spi-dw-bt1.c:77:6: note: 'data' declared here
>    77 |  u32 data;
>       |      ^~~~

Can't believe I missed that. Thanks!
Acked-by: Serge Semin <fancer.lancer@gmail.com>

> 
> Addresses-Coverity: CID 1497771 Out-of-bounds access
> Fixes: abf00907538e ("spi: dw: Add Baikal-T1 SPI Controller glue driver")
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  drivers/spi/spi-dw-bt1.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/spi/spi-dw-bt1.c b/drivers/spi/spi-dw-bt1.c
> index 4aa8596fb1f2..5be6b7b80c21 100644
> --- a/drivers/spi/spi-dw-bt1.c
> +++ b/drivers/spi/spi-dw-bt1.c
> @@ -84,7 +84,7 @@ static void dw_spi_bt1_dirmap_copy_from_map(void *to, void __iomem *from, size_t
>  	if (shift) {
>  		chunk = min_t(size_t, 4 - shift, len);
>  		data = readl_relaxed(from - shift);
> -		memcpy(to, &data + shift, chunk);
> +		memcpy(to, (char *)&data + shift, chunk);
>  		from += chunk;
>  		to += chunk;
>  		len -= chunk;
> -- 
> 2.25.1
> 

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

* Re: [PATCH] spi: dw: Avoid stack content exposure
  2021-02-11 20:37 [PATCH] spi: dw: Avoid stack content exposure Kees Cook
  2021-02-11 21:49 ` Serge Semin
@ 2021-02-12 14:01 ` Mark Brown
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Brown @ 2021-02-12 14:01 UTC (permalink / raw)
  To: Kees Cook, linux-kernel
  Cc: Ramil Zaripov, Gustavo A. R. Silva, Serge Semin, linux-spi,
	linux-hardening

On Thu, 11 Feb 2021 12:37:14 -0800, Kees Cook wrote:
> Since "data" is u32, &data is a "u32 *" type, which means pointer math
> will move in u32-sized steps. This was meant to be a byte offset, so
> cast &data to "char *" to aim the copy into the correct location.
> 
> Seen with -Warray-bounds (and found by Coverity):
> 
> In file included from ./include/linux/string.h:269,
>                  from ./arch/powerpc/include/asm/paca.h:15,
>                  from ./arch/powerpc/include/asm/current.h:13,
>                  from ./include/linux/mutex.h:14,
>                  from ./include/linux/notifier.h:14,
>                  from ./include/linux/clk.h:14,
>                  from drivers/spi/spi-dw-bt1.c:12:
> In function 'memcpy',
>     inlined from 'dw_spi_bt1_dirmap_copy_from_map' at drivers/spi/spi-dw-bt1.c:87:3:
> ./include/linux/fortify-string.h:20:29: warning: '__builtin_memcpy' offset 4 is out of the bounds [0, 4] of object 'data' with type 'u32' {aka 'unsigned int'} [-Warray-bounds]
>    20 | #define __underlying_memcpy __builtin_memcpy
>       |                             ^
> ./include/linux/fortify-string.h:191:9: note: in expansion of macro '__underlying_memcpy'
>   191 |  return __underlying_memcpy(p, q, size);
>       |         ^~~~~~~~~~~~~~~~~~~
> drivers/spi/spi-dw-bt1.c: In function 'dw_spi_bt1_dirmap_copy_from_map':
> drivers/spi/spi-dw-bt1.c:77:6: note: 'data' declared here
>    77 |  u32 data;
>       |      ^~~~

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/1] spi: dw: Avoid stack content exposure
      commit: 386f771aad15dd535f2368b4adc9958c0160edd4

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

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

end of thread, other threads:[~2021-02-12 14:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-11 20:37 [PATCH] spi: dw: Avoid stack content exposure Kees Cook
2021-02-11 21:49 ` Serge Semin
2021-02-12 14:01 ` Mark Brown

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).