Skip to contentSkip to conversation

net: dsa: don't assign an error value to tag_ops

PATCH Applied George McCollister 4 participants 6 messages Mar 22, 2021 – Mar 23, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: dsa: don't assign an error value to tag_ops

Use a temporary variable to hold the return value from dsa_tag_driver_get() instead of assigning it to dst->tag_ops. Leaving an error value in dst->tag_ops can result in deferencing an invalid pointer when a deferred switch configuration happens later.

Fixes 357f203bb3b5 ("net: dsa: keep a copy of the tagging protocol in the DSA switch tree")
---
 net/dsa/dsa2.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/dsa/dsa2.c +7 -4
index eb709d988c54..8f9e35e1aa89 100644
@@ -1068,6 +1068,7 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
 {
 	struct dsa_switch *ds = dp->ds;
 	struct dsa_switch_tree *dst = ds->dst;
+	const struct dsa_device_ops *tag_ops;
 	enum dsa_tag_protocol tag_protocol;
 
 	tag_protocol = dsa_get_tag_protocol(dp, master);
@@ -1082,14 +1083,16 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
 		 * nothing to do here.
 		 */
 	} else {
-		dst->tag_ops = dsa_tag_driver_get(tag_protocol);
-		if (IS_ERR(dst->tag_ops)) {
-			if (PTR_ERR(dst->tag_ops) == -ENOPROTOOPT)
+		tag_ops = dsa_tag_driver_get(tag_protocol);
+		if (IS_ERR(tag_ops)) {
+			if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
 				return -EPROBE_DEFER;
 			dev_warn(ds->dev, "No tagger for this switch\n");
 			dp->master = NULL;
-			return PTR_ERR(dst->tag_ops);
+			return PTR_ERR(tag_ops);
 		}
+
+		dst->tag_ops = tag_ops;
 	}
 
 	dp->master = master;
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 9 lines · George McCollister

Use a temporary variable to hold the return value from dsa_tag_driver_get() instead of assigning it to dst->tag_ops. Leaving an error value in dst->tag_ops can result in deferencing an invalid pointer when a deferred switch configuration happens later.

Fixes 357f203bb3b5 ("net: dsa: keep a copy of the tagging protocol in the DSA switch tree")

---

Who dereferences the invalid pointer? dsa_tree_free I guess?

#2 lore raw
Quoted 7 lines · Vladimir Oltean
Quoted 5 lines · George McCollister

Use a temporary variable to hold the return value from dsa_tag_driver_get() instead of assigning it to dst->tag_ops. Leaving an error value in dst->tag_ops can result in deferencing an invalid pointer when a deferred switch configuration happens later.

Quoted 6 lines
Quoted 4 lines
Fixes 357f203bb3b5 ("net: dsa: keep a copy of the tagging protocol in the DSA switch tree")

---

Who dereferences the invalid pointer? dsa_tree_free I guess?

I saw it occur just above on the following line the next time dsa_port_parse_cpu() is called:
if (dst->tag_ops->proto != tag_protocol) {

-George

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to George McCollister
Quoted 9 lines · George McCollister

Use a temporary variable to hold the return value from dsa_tag_driver_get() instead of assigning it to dst->tag_ops. Leaving an error value in dst->tag_ops can result in deferencing an invalid pointer when a deferred switch configuration happens later.

Fixes 357f203bb3b5 ("net: dsa: keep a copy of the tagging protocol in the DSA switch tree")

---

Just FYI, new lines aren't typically added between the various tags.

Reviewed-by on v1 1/1
#4 lore raw
In reply to George McCollister
Quoted 8 lines · George McCollister

Use a temporary variable to hold the return value from dsa_tag_driver_get() instead of assigning it to dst->tag_ops. Leaving an error value in dst->tag_ops can result in deferencing an invalid pointer when a deferred switch configuration happens later.

Fixes 357f203bb3b5 ("net: dsa: keep a copy of the tagging protocol in the DSA switch tree")
Signature · 1 line
Florian
#5 lore raw
In reply to George McCollister
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 8 lines · On Mon, 22 Mar 2021 15:26:50 -0500 you wrote:

Use a temporary variable to hold the return value from dsa_tag_driver_get() instead of assigning it to dst->tag_ops. Leaving an error value in dst->tag_ops can result in deferencing an invalid pointer when a deferred switch configuration happens later.

Fixes 357f203bb3b5 ("net: dsa: keep a copy of the tagging protocol in the DSA switch tree")

[...]

Here is the summary with links:
- [net] net: dsa: don't assign an error value to tag_ops

    https://git.kernel.org/netdev/net/c/e0c755a45f6f

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html