[PATCH master 4/7] net: gianfar: fix out of bounds read of local variable

Sascha Hauer sha at pengutronix.de
Tue Jun 6 02:33:44 PDT 2023


On Mon, Jun 05, 2023 at 08:29:36AM +0200, Ahmad Fatoum wrote:
> The MAC address will be written to two 32-bit registers. Because
> MAC_ADDR_LEN == 6, this meant two bytes out-of-bounds where written to
> the hardware register. Fix this by having them be in-bound and always
> initialized to zero.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
>  drivers/net/gianfar.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/gianfar.c b/drivers/net/gianfar.c
> index 4b374b4a50de..10a95324920b 100644
> --- a/drivers/net/gianfar.c
> +++ b/drivers/net/gianfar.c
> @@ -234,7 +234,7 @@ static int gfar_set_ethaddr(struct eth_device *edev, const unsigned char *mac)
>  {
>  	struct gfar_private *priv = edev->priv;
>  	void __iomem *regs = priv->regs;
> -	char tmpbuf[MAC_ADDR_LEN];
> +	char tmpbuf[8] = {};

I would prefer to adopt the Linux commit fixing this code which apart
from the out-of-bounds access also has endianess fixes and makes the
code simpler:

---------------------------8<-----------------------------------

>From 35cc52aacd65886a2ae46e68f727cadd09a3e8f2 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <s.hauer at pengutronix.de>
Date: Tue, 6 Jun 2023 11:29:51 +0200
Subject: [PATCH] net: gianfar: make MAC addr setup endian safe, cleanup

This is an adoption of Linux commit:

| commit 83bfc3c4765c35ef0dfff8a3d6dedab88f3f50ea
| Author: Claudiu Manoil <claudiu.manoil at freescale.com>
| Date:   Tue Oct 7 10:44:33 2014 +0300
|
|     gianfar: Make MAC addr setup endian safe, cleanup
|
|     Fix the 32-bit memory access that is not endian safe,
|     i.e. not giving the desired byte layout for a LE CPU:
|     tempval = *((u32 *) (tmpbuf + 4)), where 'char tmpbuf[]'.
|
|     Get rid of rendundant local vars (tmpbuf[] and idx) and
|     forced casts.  Cleanup comments.
|
|     Signed-off-by: Claudiu Manoil <claudiu.manoil at freescale.com>
|     Signed-off-by: David S. Miller <davem at davemloft.net>

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/net/gianfar.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/net/gianfar.c b/drivers/net/gianfar.c
index 4b374b4a50..1a07059db4 100644
--- a/drivers/net/gianfar.c
+++ b/drivers/net/gianfar.c
@@ -234,19 +234,13 @@ static int gfar_set_ethaddr(struct eth_device *edev, const unsigned char *mac)
 {
 	struct gfar_private *priv = edev->priv;
 	void __iomem *regs = priv->regs;
-	char tmpbuf[MAC_ADDR_LEN];
 	uint tempval;
-	int ix;
-
-	for (ix = 0; ix < MAC_ADDR_LEN; ix++)
-		tmpbuf[MAC_ADDR_LEN - 1 - ix] = mac[ix];
 
-	tempval = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) |
-		  tmpbuf[3];
+	tempval = (mac[5] << 24) | (mac[4] << 16) | (mac[3] << 8)  |  mac[2];
 
 	out_be32(regs + GFAR_MACSTRADDR1_OFFSET, tempval);
 
-	tempval = *((uint *)(tmpbuf + 4));
+	tempval = (mac[1] << 24) | (mac[0] << 16);
 
 	out_be32(regs + GFAR_MACSTRADDR2_OFFSET, tempval);
 
-- 
2.39.2

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list