Skip to contentSkip to conversation

fsl/fman: Validate cell-index value obtained from Device Tree

PATCH Superseded Aleksandr Mishin 4 participants 5 messages Jul 2, 2024

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsl/fman: Validate cell-index value obtained from Device Tree

Cell-index value is obtained from Device Tree and then used to calculate the index for accessing arrays port_mfl[], mac_mfl[] and intr_mng[]. In case of broken DT due to any error cell-index can contain any value and it is possible to go beyond the array boundaries which can lead at least to memory corruption.
Validate cell-index value obtained from Device Tree.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 414fd46e7762 ("fsl/fman: Add FMan support")
---
 drivers/net/ethernet/freescale/fman/fman.c | 7 +++++++
 drivers/net/ethernet/freescale/fman/fman.h | 2 ++
 drivers/net/ethernet/freescale/fman/mac.c  | 5 +++++
 3 files changed, 14 insertions(+)
drivers/net/ethernet/freescale/fman/fman.c +7 -0
index d96028f01770..6929bca3f768 100644
@@ -2933,3 +2933,10 @@ module_exit(fman_unload);
 
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_DESCRIPTION("Freescale DPAA Frame Manager driver");
+
+int check_mac_id(u32 mac_id)
+{
+	if (mac_id >= MAX_NUM_OF_MACS)
+		return -EINVAL;
+	return 0;
+}
drivers/net/ethernet/freescale/fman/fman.h +2 -0
index 2ea575a46675..3cedde4851e1 100644
@@ -372,6 +372,8 @@ u16 fman_get_max_frm(void);
 
 int fman_get_rx_extra_headroom(void);
 
+int check_mac_id(u32 mac_id);
+
 #ifdef CONFIG_DPAA_ERRATUM_A050385
 bool fman_has_errata_a050385(void);
 #endif
drivers/net/ethernet/freescale/fman/mac.c +5 -0
index 9767586b4eb3..7a67b4c887e2 100644
@@ -247,6 +247,11 @@ static int mac_probe(struct platform_device *_of_dev)
 		dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
 		return -EINVAL;
 	}
+	err = check_mac_id(val);
+	if (err) {
+		dev_err(dev, "cell-index value is out of range for %pOF\n", mac_node);
+		return err;
+	}
 	priv->cell_index = (u8)val;
 
 	/* Get the MAC address */
Signature · 1 line
2.30.2
reviewer
#1 lore raw
Quoted 50 lines · Aleksandr Mishin

Cell-index value is obtained from Device Tree and then used to calculate the index for accessing arrays port_mfl[], mac_mfl[] and intr_mng[]. In case of broken DT due to any error cell-index can contain any value and it is possible to go beyond the array boundaries which can lead at least to memory corruption.
Validate cell-index value obtained from Device Tree.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 414fd46e7762 ("fsl/fman: Add FMan support")
---
 drivers/net/ethernet/freescale/fman/fman.c | 7 +++++++
 drivers/net/ethernet/freescale/fman/fman.h | 2 ++
 drivers/net/ethernet/freescale/fman/mac.c  | 5 +++++
 3 files changed, 14 insertions(+)
drivers/net/ethernet/freescale/fman/fman.c +7 -0
index d96028f01770..6929bca3f768 100644
@@ -2933,3 +2933,10 @@ module_exit(fman_unload);
 
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_DESCRIPTION("Freescale DPAA Frame Manager driver");
+
+int check_mac_id(u32 mac_id)
+{
+	if (mac_id >= MAX_NUM_OF_MACS)
+		return -EINVAL;
+	return 0;
+}
drivers/net/ethernet/freescale/fman/fman.h +2 -0
index 2ea575a46675..3cedde4851e1 100644
@@ -372,6 +372,8 @@ u16 fman_get_max_frm(void);
 
 int fman_get_rx_extra_headroom(void);
 
+int check_mac_id(u32 mac_id);
+
 #ifdef CONFIG_DPAA_ERRATUM_A050385
 bool fman_has_errata_a050385(void);
 #endif
drivers/net/ethernet/freescale/fman/mac.c +0 -0
index 9767586b4eb3..7a67b4c887e2 100644

drivers/net/ethernet/freescale/fman/mac.c static int mac_probe(struct platform_device *_of_dev) · line 250

Quoted 4 lines
drivers/net/ethernet/freescale/fman/mac.c +1 -0
@@ -247,6 +247,11 @@ static int mac_probe(struct platform_device *_of_dev)
 		dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
 		return -EINVAL;
 	}
+	err = check_mac_id(val);

Hi Aleksandr, all,

It seems that this breaks linking with allmodconfig builds on x86_64, perhaps it is better to simply make check_mac_id a static function in mac.c ?

drivers/net/ethernet/freescale/fman/mac.c static int mac_probe(struct platform_device *_of_dev) · line 252

Quoted 2 lines
drivers/net/ethernet/freescale/fman/mac.c +2 -0
+	if (err) {
+		dev_err(dev, "cell-index value is out of range for %pOF\n", mac_node);

Although other instances exist in this function before this patch, it does look like this leaks a reference to of_dev taken by the call to of_find_device_by_node() on line 194.

Maybe it is intentional, I'm unsure.
Perhaps this can be investigated separately to the fix proposed by this patch?

Flagged by Coccinelle (this one is on line 253):

.../mac.c:238:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:242:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:248:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:253:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:267:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:273:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:282:3-9: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:320:2-8: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:333:1-7: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:337:1-7: ERROR: missing put_device; call of_find_device_by_node on line 194, but without a corresponding object release within this function. .../mac.c:282:3-9: ERROR: missing put_device; call of_find_device_by_node on line 285, but without a corresponding object release within this function. .../mac.c:320:2-8: ERROR: missing put_device; call of_find_device_by_node on line 285, but without a corresponding object release within this function. .../mac.c:333:1-7: ERROR: missing put_device; call of_find_device_by_node on line 285, but without a corresponding object release within this function. .../mac.c:337:1-7: ERROR: missing put_device; call of_find_device_by_node on line 285, but without a corresponding object release within this function.

Quoted 5 lines
drivers/net/ethernet/freescale/fman/mac.c +2 -0
+		return err;
+	}
 	priv->cell_index = (u8)val;
 
 	/* Get the MAC address */
Signature · 1 line
pw-bot: changes-requested
maintainer
#2 lore raw
Quoted 3 lines · On Tue, 2 Jul 2024 14:36:51 +0100 Simon Horman wrote:

Maybe it is intentional, I'm unsure.
Perhaps this can be investigated separately to the fix proposed by this patch?

+1, please fix this first

#3 lore raw

PATCH v2 1/1 fsl/fman: Validate cell-index value obtained from Device Tree

In reply to Aleksandr Mishin

Cell-index value is obtained from Device Tree and then used to calculate the index for accessing arrays port_mfl[], mac_mfl[] and intr_mng[]. In case of broken DT due to any error cell-index can contain any value and it is possible to go beyond the array boundaries which can lead at least to memory corruption.
Validate cell-index value obtained from Device Tree.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 414fd46e7762 ("fsl/fman: Add FMan support")

---
v1->v2: Move check to mac.c to avoid allmodconfig build errors and reference leaks

 drivers/net/ethernet/freescale/fman/fman.c | 1 -
 drivers/net/ethernet/freescale/fman/fman.h | 3 +++
 drivers/net/ethernet/freescale/fman/mac.c  | 4 ++++
 3 files changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/freescale/fman/fman.c +0 -1
index d96028f01770..fb416d60dcd7 100644
@@ -24,7 +24,6 @@
 
 /* General defines */
 #define FMAN_LIODN_TBL			64	/* size of LIODN table */
-#define MAX_NUM_OF_MACS			10
 #define FM_NUM_OF_FMAN_CTRL_EVENT_REGS	4
 #define BASE_RX_PORTID			0x08
 #define BASE_TX_PORTID			0x28
drivers/net/ethernet/freescale/fman/fman.h +3 -0
index 2ea575a46675..74eb62eba0d7 100644
@@ -74,6 +74,9 @@
 #define BM_MAX_NUM_OF_POOLS		64 /* Buffers pools */
 #define FMAN_PORT_MAX_EXT_POOLS_NUM	8  /* External BM pools per Rx port */
 
+/* General defines */
+#define MAX_NUM_OF_MACS			10
+
 struct fman; /* FMan data */
 
 /* Enum for defining port types */
drivers/net/ethernet/freescale/fman/mac.c +4 -0
index 9767586b4eb3..ac9ad5e67b44 100644
@@ -247,6 +247,10 @@ static int mac_probe(struct platform_device *_of_dev)
 		dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
 		return -EINVAL;
 	}
+	if (val >= MAX_NUM_OF_MACS) {
+		dev_err(dev, "cell-index value is too big for %pOF\n", mac_node);
+		return -EINVAL;
+	}
 	priv->cell_index = (u8)val;
 
 	/* Get the MAC address */
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#4 lore raw
Quoted 63 lines · Aleksandr Mishin

[You don't often get email from [email protected]. Learn why this is important at https://cas5-0-urlprotect.trendmicro.com:443/wis/clicktime/v1/query?url=https%3a%2f%2faka.ms%2fLearnAboutSenderIdentification&umid=ca782a5b-a3fd-4738-b50c-ffbd8e52e50f&auth=d807158c60b7d2502abde8a2fc01f40662980862-ac0deac714bbe85d389fd2711a17d0113034b3ca ]

Cell-index value is obtained from Device Tree and then used to calculate the index for accessing arrays port_mfl[], mac_mfl[] and intr_mng[]. In case of broken DT due to any error cell-index can contain any value and it is possible to go beyond the array boundaries which can lead at least to memory corruption.
Validate cell-index value obtained from Device Tree.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 414fd46e7762 ("fsl/fman: Add FMan support")

---
v1->v2: Move check to mac.c to avoid allmodconfig build errors and reference leaks

 drivers/net/ethernet/freescale/fman/fman.c | 1 -
 drivers/net/ethernet/freescale/fman/fman.h | 3 +++
 drivers/net/ethernet/freescale/fman/mac.c  | 4 ++++
 3 files changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/freescale/fman/fman.c +0 -1
index d96028f01770..fb416d60dcd7 100644
@@ -24,7 +24,6 @@
 
 /* General defines */
 #define FMAN_LIODN_TBL                 64      /* size of LIODN table */
-#define MAX_NUM_OF_MACS                        10
 #define FM_NUM_OF_FMAN_CTRL_EVENT_REGS 4
 #define BASE_RX_PORTID                 0x08
 #define BASE_TX_PORTID                 0x28
drivers/net/ethernet/freescale/fman/fman.h +3 -0
index 2ea575a46675..74eb62eba0d7 100644
@@ -74,6 +74,9 @@
 #define BM_MAX_NUM_OF_POOLS            64 /* Buffers pools */
 #define FMAN_PORT_MAX_EXT_POOLS_NUM    8  /* External BM pools per Rx port */
 
+/* General defines */
+#define MAX_NUM_OF_MACS                        10
+
 struct fman; /* FMan data */
 
 /* Enum for defining port types */
drivers/net/ethernet/freescale/fman/mac.c +4 -1
index 9767586b4eb3..ac9ad5e67b44 100644
@@ -247,6 +247,10 @@ static int mac_probe(struct platform_device *_of_dev)
                dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
                return -EINVAL;
        }
+       if (val >= MAX_NUM_OF_MACS) {
+               dev_err(dev, "cell-index value is too big for %pOF\n", mac_node);
+               return -EINVAL;
+       }
        priv->cell_index = (u8)val;
 
        /* Get the MAC address */
--
Quoted 2 lines

2.30.2

[StudioX, SECO SpA]<https://www.seco.com/applications/studiox>