Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net/sched: cls_u32: fix hnode UAF in u32_change
@ 2026-12-01  7:50 zhuorao yang
  2026-12-04  7:50 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: zhuorao yang @ 2026-12-01  7:50 UTC (permalink / raw)
  To: Jamal Hadi Salim, Jiri Pirko, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn
  Cc: netdev

u32_change() looks up a non-root hash node and keeps only a bare
pointer while it allocates a filter handle in that node.  The subsequent
action initialization can drop RTNL to request an unregistered action
module.  A concurrent RTM_DELTFILTER can then delete and eventually free
the hash node.

When action initialization fails, the error cleanup removes the handle
from the hash node's IDR through the stale pointer, causing a
use-after-free read and write.

Hold a reference on the hash node from before action initialization until
all uses of the pointer on both the success and error paths are complete.
This makes a concurrent deletion return -EBUSY.

Fixes: e7614370d6f0 ("net_sched: use idr to allocate u32 filter handles")
Signed-off-by: zhuorao yang <alex000young@gmail.com>
---

Vulnerability report

Affected versions: Linux 7.4.0-rc5 at
  fe6ff8d5d675c01793b9f051d16055dd4aa75244; the defect dates to v4.15
  and applies to later kernels with CONFIG_NET_CLS_U32.
Trigger conditions: CONFIG_NET_CLS_U32 and CONFIG_NET_CLS_ACT, with
  CAP_NET_ADMIN in the target network namespace. An RTM_NEWTFILTER
  request selects a non-root hash node and references an unregistered tc
  action, so request_module() drops RTNL. A concurrent RTM_DELTFILTER
  can delete the hash node during that window.
Reproducer status: A QEMU test with a test-only modprobe delay showed
  that the unpatched kernel deletes the hash node and later runs
  idr_remove() through the stale pointer; kprobes recorded the destroyed
  IDR and the later access. With the patch, concurrent deletion returns
  -EBUSY and the error path only accesses the live hash node. Reproducer
  material is available to maintainers on request.
Reproducer material is intentionally withheld from this public message.

AI assistance: DeepSeek identified the issue and drafted the patch.
The submitter reviewed the code and certified the Developer Certificate
of Origin.

 net/sched/cls_u32.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c
index 91f4e6458785..506640323335 100644
--- a/net/sched/cls_u32.c
+++ b/net/sched/cls_u32.c
@@ -1188,6 +1188,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
 	}
 #endif
 
+	refcount_inc(&ht->refcnt);
 	err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE],
 			    flags, n->flags, extack);
 
@@ -1216,6 +1217,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
 		rcu_assign_pointer(*ins, n);
 		tp_c->knodes++;
 		*arg = n;
+		refcount_dec(&ht->refcnt);
 		return 0;
 	}
 
@@ -1235,6 +1237,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
 	kfree(n);
 erridr:
 	idr_remove(&ht->handle_idr, handle);
+	refcount_dec(&ht->refcnt);
 	return err;
 }
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] net/sched: cls_u32: fix hnode UAF in u32_change
  2026-12-01  7:50 [PATCH net] net/sched: cls_u32: fix hnode UAF in u32_change zhuorao yang
@ 2026-12-04  7:50 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04  7:50 UTC (permalink / raw)
  To: alex000young; +Cc: jhs, jiri, davem, edumazet, kuba, pabeni, willemb, netdev

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 1 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Critical] In u32_change() (net/sched/cls_u32.c), the patch adds an
  unconditional `refcount_dec(&ht->refcnt)` under the shared `erridr`…
- [Medium] The patch releases the pinned hash node with a plain
  `refcount_dec()` (u32_change() lines 1220 and 1240).

--

net/sched: cls_u32: fix hnode UAF in u32_change

This patch takes a reference on the hash node in u32_change() before
u32_set_parms() and drops it on both the success and error paths. The goal
is to keep a concurrent RTM_DELTFILTER from freeing the hnode while RTNL is
dropped to load an action module.

> diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c
> index 91f4e64587852..5066403233350 100644
> --- a/net/sched/cls_u32.c
> +++ b/net/sched/cls_u32.c
> @@ -1188,6 +1188,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
>  	}
>  #endif
>  
> +	refcount_inc(&ht->refcnt);
>  	err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE],
>  			    flags, n->flags, extack);
>  
> @@ -1216,6 +1217,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
>  		rcu_assign_pointer(*ins, n);
>  		tp_c->knodes++;
>  		*arg = n;
> +		refcount_dec(&ht->refcnt);

[Severity: Medium]
Can this plain refcount_dec(), or the one under erridr, drop the last
reference to ht?

u32_init() adds every tcf_proto's root_ht to the shared tp_c->hlist. In
u32_change(), a TCA_U32_HASH value that is not TC_U32_ROOT goes through
this lookup:

    ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));

There is no is_root or ownership check. The lookup can return the root_ht
of another u32 tcf_proto (a different prio) that shares the same
tc_u_common and has refcnt 1. This patch then raises it to 2.

Suppose action initialization drops RTNL for request_module(), and a
concurrent RTM_DELTFILTER removes the other tcf_proto. u32_destroy() then
runs:

    if (root_ht && refcount_dec_and_test(&root_ht->refcnt))
        u32_destroy_hnode(tp, root_ht, extack);

The count goes from 2 to 1 and u32_destroy_hnode() is skipped. The root ht
is left orphaned on tp_c->hlist.

The refcount_dec() here or at erridr then takes it from 1 to 0. That warns
with REFCOUNT_DEC_LEAK and saturates the counter.

After that, u32_delete() refuses the orphaned ht because is_root is set. The
teardown loop's refcount_dec_and_test() in u32_destroy() never reaches zero
either, so the hnode is leaked. On the success path, the leaked hnode also
holds the new knode.

The commit message says:

    This makes a concurrent deletion return -EBUSY.

In this case the deletion of the other tcf_proto goes ahead instead of
returning -EBUSY.

Could the final release handle being the last reference, for example with
refcount_dec_and_test() followed by destroying the hnode? Or should
u32_change() avoid pinning another tcf_proto's root this way?

>  		return 0;
>  	}
>  
> @@ -1235,6 +1237,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
>  	kfree(n);
>  erridr:
>  	idr_remove(&ht->handle_idr, handle);
> +	refcount_dec(&ht->refcnt);

[Severity: Critical]
Is this refcount_dec() balanced on every path that reaches erridr?

The matching refcount_inc() is only taken right before u32_set_parms().
Several earlier exits in u32_change() reach erridr after the handle has
been allocated but before that increment. The first one is:

    if (tb[TCA_U32_SEL] == NULL) {
        NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
        err = -EINVAL;
        goto erridr;
    }

These exits do the same:

  - the short TCA_U32_SEL length check
  - the offshift >= 16 check
  - the kzalloc_flex() failure
  - the n->pf percpu allocation failure (via errfree)
  - the tcf_exts_init() failure (via errout)
  - the pcpu_success allocation failure (via errout)

So a single RTM_NEWTFILTER that names an existing hash node (the root
included) and omits TCA_U32_SEL drops a reference that was never taken. No
race is needed.

If ht->refcnt is 1 (a fresh divisor table, or tp->root), it goes from 1 to
0. refcount_dec() warns with REFCOUNT_DEC_LEAK and saturates the counter,
and after that u32_delete() and u32_destroy() can never free the hnode.

If a knode links to the hnode through TCA_U32_LINK, refcnt is 2 and drops
to 1. A later RTM_DELTFILTER on that hnode then passes this check in
u32_delete():

    if (refcount_dec_if_one(&ht->refcnt)) {
        u32_destroy_hnode(tp, ht, extack);

u32_destroy_hnode() calls kfree_rcu() on the hnode while the linking
knode's ht_down still points at it. u32_classify() would then read freed
memory, and __u32_destroy_key() would run this on the freed hnode:

    if (ht && refcount_dec_and_test(&ht->refcnt))
        kfree(ht);

Would it work to take the reference right after the handle is allocated,
before the first goto erridr? Alternatively, the early exits could use a
label that skips the decrement.

>  	return err;
>  }
>

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201075058.64049-1-alex000young%40gmail.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04  7:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  7:50 [PATCH net] net/sched: cls_u32: fix hnode UAF in u32_change zhuorao yang
2026-12-04  7:50 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox