Skip to contentSkip to conversation

net: shaper: fix VALID confusion even more

PATCH Applied Jakub Kicinski 3 participants 6 messages May 15 – May 21

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover net: shaper: fix VALID confusion even more

Sashiko reported another pre-exising issue in the previous batch of fixes:
https://sashiko.dev/#/patchset/[email protected]

Turns out I over-esitmated the guarantees of the XArray flags. Stop using them completely.

Jakub Kicinski (2):
net: shaper: annotate the data races
net: shaper: rework the VALID marking (again)

 include/net/net_shaper.h |  1 +
 net/shaper/shaper.c      | 98 +++++++++++++++++++++++-----------------
 2 files changed, 57 insertions(+), 42 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 1/2 net: shaper: annotate the data races

As previously discussed we don't care about making the shaper state fully RCU-compliant because the hierarchy itself can't be dumped in one go over Netlink. Let's annotate the reads and writes to make that clear.

The field-by-field assignments will also be useful for the next commit which adds explicit "valid" field (which we don't want to override with the current full struct assignment).

---
 net/shaper/shaper.c | 53 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 15 deletions(-)
net/shaper/shaper.c +38 -15
index b1c65110f04d..520cefdc3d90 100644
@@ -138,35 +138,58 @@ static int net_shaper_fill_handle(struct sk_buff *msg,
 	return -EMSGSIZE;
 }
 
+static void net_shaper_copy(struct net_shaper *dst,
+			    const struct net_shaper *src)
+{
+	WRITE_ONCE(dst->parent.scope, READ_ONCE(src->parent.scope));
+	WRITE_ONCE(dst->parent.id, READ_ONCE(src->parent.id));
+	WRITE_ONCE(dst->handle.scope, READ_ONCE(src->handle.scope));
+	WRITE_ONCE(dst->handle.id, READ_ONCE(src->handle.id));
+
+	WRITE_ONCE(dst->metric, READ_ONCE(src->metric));
+	WRITE_ONCE(dst->bw_min, READ_ONCE(src->bw_min));
+	WRITE_ONCE(dst->bw_max, READ_ONCE(src->bw_max));
+	WRITE_ONCE(dst->burst, READ_ONCE(src->burst));
+	WRITE_ONCE(dst->priority, READ_ONCE(src->priority));
+	WRITE_ONCE(dst->weight, READ_ONCE(src->weight));
+
+	/* private fields are only used on the write path under the lock */
+	data_race(dst->leaves = src->leaves);
+}
+
 static int
 net_shaper_fill_one(struct sk_buff *msg,
 		    const struct net_shaper_binding *binding,
 		    const struct net_shaper *shaper,
 		    const struct genl_info *info)
 {
+	struct net_shaper cur;
 	void *hdr;
 
 	hdr = genlmsg_iput(msg, info);
 	if (!hdr)
 		return -EMSGSIZE;
 
+	/* Make a copy to avoid data races */
+	net_shaper_copy(&cur, shaper);
+
 	if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_IFINDEX) ||
-	    net_shaper_fill_handle(msg, &shaper->parent,
+	    net_shaper_fill_handle(msg, &cur.parent,
 				   NET_SHAPER_A_PARENT) ||
-	    net_shaper_fill_handle(msg, &shaper->handle,
+	    net_shaper_fill_handle(msg, &cur.handle,
 				   NET_SHAPER_A_HANDLE) ||
-	    ((shaper->bw_min || shaper->bw_max || shaper->burst) &&
-	     nla_put_u32(msg, NET_SHAPER_A_METRIC, shaper->metric)) ||
-	    (shaper->bw_min &&
-	     nla_put_uint(msg, NET_SHAPER_A_BW_MIN, shaper->bw_min)) ||
-	    (shaper->bw_max &&
-	     nla_put_uint(msg, NET_SHAPER_A_BW_MAX, shaper->bw_max)) ||
-	    (shaper->burst &&
-	     nla_put_uint(msg, NET_SHAPER_A_BURST, shaper->burst)) ||
-	    (shaper->priority &&
-	     nla_put_u32(msg, NET_SHAPER_A_PRIORITY, shaper->priority)) ||
-	    (shaper->weight &&
-	     nla_put_u32(msg, NET_SHAPER_A_WEIGHT, shaper->weight)))
+	    ((cur.bw_min || cur.bw_max || cur.burst) &&
+	     nla_put_u32(msg, NET_SHAPER_A_METRIC, cur.metric)) ||
+	    (cur.bw_min &&
+	     nla_put_uint(msg, NET_SHAPER_A_BW_MIN, cur.bw_min)) ||
+	    (cur.bw_max &&
+	     nla_put_uint(msg, NET_SHAPER_A_BW_MAX, cur.bw_max)) ||
+	    (cur.burst &&
+	     nla_put_uint(msg, NET_SHAPER_A_BURST, cur.burst)) ||
+	    (cur.priority &&
+	     nla_put_u32(msg, NET_SHAPER_A_PRIORITY, cur.priority)) ||
+	    (cur.weight &&
+	     nla_put_u32(msg, NET_SHAPER_A_WEIGHT, cur.weight)))
 		goto nla_put_failure;
 
 	genlmsg_end(msg, hdr);
@@ -424,7 +447,7 @@ static void net_shaper_commit(struct net_shaper_binding *binding,
 		/* Successful update: drop the tentative mark
 		 * and update the hierarchy container.
 		 */
-		*cur = shapers[i];
+		net_shaper_copy(cur, &shapers[i]);
 		smp_wmb();
 		__xa_set_mark(&hierarchy->shapers, index, NET_SHAPER_VALID);
 	}
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

PATCH 2/2 net: shaper: rework the VALID marking (again)

In reply to Jakub Kicinski

Recent commit changed the semantics from NOT_VALID to VALID. I didn't realize that the flags are not stored atomically with the entry in XArray. There's still a race of reader observing a VALID mark for a slot, getting interrupted, writer replacing the entry with a different one, reader continuing, fetching the entry which is now a different pointer than the pointer for which VALID was meant.

The biggest consequence of this is that we may see a UAF since net_shaper_rollback() assumed that entries without VALID can be freed without observing RCU.

Looks like the XArray marks are buying us nothing at this point. Let's convert the code to an explicit valid field. The smp_load_acquire() / smp_store_release() barriers are marginally cleaner.

Fixes 93954b40f6a4 ("net-shapers: implement NL set and delete operations")
---
 include/net/net_shaper.h |  1 +
 net/shaper/shaper.c      | 45 ++++++++++++++++------------------------
 2 files changed, 19 insertions(+), 27 deletions(-)
include/net/net_shaper.h +1 -0
index 5c3f49b52fe9..3939b816b001 100644
@@ -53,6 +53,7 @@ struct net_shaper {
 
 	/* private: */
 	u32 leaves; /* accounted only for NODE scope */
+	bool valid;
 	struct rcu_head rcu;
 };
 
net/shaper/shaper.c +18 -27

83 lines not on this page · raw message

Signature · 1 line
2.54.0
reviewer Reviewed-by on the v1 series
#3 lore raw
In reply to Jakub Kicinski
Quoted 10 lines · Jakub Kicinski

Sashiko reported another pre-exising issue in the previous batch of fixes:
https://sashiko.dev/#/patchset/[email protected]

Turns out I over-esitmated the guarantees of the XArray flags. Stop using them completely.

Jakub Kicinski (2):
net: shaper: annotate the data races
net: shaper: rework the VALID marking (again)

Sashiko suggests updating the wording of the VALID flag comment in net_shaper_pre_insert as part of the second patch. But, TBH, I don't feel strongly about that.

For the series:

author maintainer
#4 lore raw
Quoted 11 lines · On Wed, 20 May 2026 20:54:28 +0100 Simon Horman wrote:
Quoted 6 lines · Jakub Kicinski

Sashiko reported another pre-exising issue in the previous batch of fixes:
https://sashiko.dev/#/patchset/[email protected]

Turns out I over-esitmated the guarantees of the XArray flags. Stop using them completely.

Sashiko suggests updating the wording of the VALID flag comment in net_shaper_pre_insert as part of the second patch. But, TBH, I don't feel strongly about that.

It's very picky about these comments.
IMHO the word "mark" is not reserved to exclusively mean xa_mark in case no xa_mark is used by the code any more.

Quoted 2 lines

For the series:

Thanks!

#5 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Fri, 15 May 2026 15:13:23 -0700 you wrote:

Sashiko reported another pre-exising issue in the previous batch of fixes:
https://sashiko.dev/#/patchset/[email protected]

Turns out I over-esitmated the guarantees of the XArray flags. Stop using them completely.

[...]

Here is the summary with links:
- [net,1/2] net: shaper: annotate the data races

    https://git.kernel.org/netdev/net/c/a3442936dd05
  - [net,2/2] net: shaper: rework the VALID marking (again)
    https://git.kernel.org/netdev/net/c/b8d7519352ba

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html