diff mbox series

[next] net: dsa: sja1105: remove redundant re-assignment of pointer table

Message ID 20210722191529.11013-1-colin.king@canonical.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [next] net: dsa: sja1105: remove redundant re-assignment of pointer table | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Colin King July 22, 2021, 7:15 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

The pointer table is being re-assigned with a value that is never
read. The assignment is redundant and can be removed.

Addresses-Coverity: ("Unused value")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/net/dsa/sja1105/sja1105_main.c | 2 --
 1 file changed, 2 deletions(-)

Comments

Vladimir Oltean July 22, 2021, 8:05 p.m. UTC | #1
On Thu, Jul 22, 2021 at 08:15:29PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The pointer table is being re-assigned with a value that is never
> read. The assignment is redundant and can be removed.
> 
> Addresses-Coverity: ("Unused value")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/dsa/sja1105/sja1105_main.c | 2 --
>  1 file changed, 2 deletions(-)
> 
> diff --git a/drivers/net/dsa/sja1105/sja1105_main.c b/drivers/net/dsa/sja1105/sja1105_main.c
> index 6618abba23b3..c65dba3111d7 100644
> --- a/drivers/net/dsa/sja1105/sja1105_main.c
> +++ b/drivers/net/dsa/sja1105/sja1105_main.c
> @@ -2157,8 +2157,6 @@ static int sja1105_build_vlan_table(struct sja1105_private *priv)
>  	if (!new_vlan)
>  		return -ENOMEM;
>  
> -	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
> -
>  	for (i = 0; i < VLAN_N_VID; i++)
>  		new_vlan[i].vlanid = VLAN_N_VID;
>  
> -- 
> 2.31.1
> 

Oh my, what an interesting bug you uncovered.

That duplicate assignment was introduced in commit 3f01c91aab92 ("net:
dsa: sja1105: implement VLAN retagging for dsa_8021q sub-VLANs") and
used to read:

	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
	new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
				table->ops->unpacked_entry_size, GFP_KERNEL);

In retrospect, it should have been:

	table = &priv->static_config.tables[BLK_IDX_RETAGGING];

because we must allocate SJA1105_MAX_RETAGGING_COUNT elements of the
size of one VLAN retagging entry, and not of one VLAN lookup entry.

In commit 0fac6aa098ed ("net: dsa: sja1105: delete the
best_effort_vlan_filtering mode") I deleted everything that had to do
with VLAN retagging but left this behind because it didn't say RETAGGING
on it.

	[BLK_IDX_VLAN_LOOKUP] = {
		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
	},
	[BLK_IDX_RETAGGING] = {
		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
	},

and if you look at them, struct sja1105_retagging_entry has 7 u64
fields, while struct sja1105_vlan_lookup_entry has 6 elements (actually
since commit 3e77e59bf8cf ("net: dsa: sja1105: add support for the
SJA1110 switch family") it also has 7.

The point is, between commit 3f01c91aab92 and commit 3e77e59bf8cf, the
driver allocated 8 bytes too few per VLAN retagging entry, or multiplied
by 32 (the value of SJA1105_MAX_RETAGGING_COUNT), it only allocates
enough memory for 27.4 VLAN retagging entries. So any attempt to access
VLAN retagging entries 27-31 in this function would trigger an out of
bounds memory access.

Could you please also send a patch for the "net" tree with this, and the
explanation above?

-	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
+	table = &priv->static_config.tables[BLK_IDX_RETAGGING];

Thanks.
Vladimir Oltean July 23, 2021, 12:32 p.m. UTC | #2
On Thu, Jul 22, 2021 at 11:05:44PM +0300, Vladimir Oltean wrote:
> Could you please also send a patch for the "net" tree with this, and the
> explanation above?
> 
> -	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
> +	table = &priv->static_config.tables[BLK_IDX_RETAGGING];

I think it's best for me to just send that patch. I will send it towards
the "stable" trees directly, to avoid all sorts of conflicts with "net"
and "net-next" (as mentioned, the VLAN retagging code is going away).

For this patch towards net-next:

Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
diff mbox series

Patch

diff --git a/drivers/net/dsa/sja1105/sja1105_main.c b/drivers/net/dsa/sja1105/sja1105_main.c
index 6618abba23b3..c65dba3111d7 100644
--- a/drivers/net/dsa/sja1105/sja1105_main.c
+++ b/drivers/net/dsa/sja1105/sja1105_main.c
@@ -2157,8 +2157,6 @@  static int sja1105_build_vlan_table(struct sja1105_private *priv)
 	if (!new_vlan)
 		return -ENOMEM;
 
-	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
-
 	for (i = 0; i < VLAN_N_VID; i++)
 		new_vlan[i].vlanid = VLAN_N_VID;