net/sched: cls_route: fix fastmap use-after-free on filter
Subsystems (4)
Jamal Hadi Salim
maintainerPATCH 1/1 net/sched: cls_route: fix fastmap use-after-free on filter
A case of partially rcu'ed structs.
The route4 classifier maintains a 16-slot fastmap cache that stores raw struct route4_filter pointers indexed by (id, iif). The reader (route4_classify) populates this cache via route4_set_fastmap() for every classified packet that hits a filter. The writer (route4_delete, route4_change) clears the cache via route4_reset_fastmap() before RCU-deferred kfree of the filter.
This creates a UAF race:
1. Reader walks the RCU-protected bucket chain, finds filter f
2. Writer unlinks f, calls route4_reset_fastmap(), then tcf_queue_work()
3. Reader calls route4_set_fastmap() and writes f into the cache *after* the writer's reset, caching a pointer about to be freed
4. After the RCU grace period, kfree(f) executes
5. Next classified packet on the same (id, iif) tuple hits the stale
fastmap entry and reads f->res from freed memory
Reproduced with an mdelay(100) accelerator in route4_set_fastmap() and a concurrent add/delete stress test (provided by both zdi and Santosh) deletes the last filter on a tp. Both triggered KASAN slab-use-after-free reports in the route4 fastmap paths.
Fix:
Move the fastmap flush to the writer side under RTNL, ordered after synchronize_rcu(). This ensures stale fastmap entries written by in-flight readers are visible (readers have finished) and no new readers can find the unlinked filter, so the subsequent route4_reset_fastmap(head) flushes them safely while head is valid under RTNL.
Note: This fix will slow down deletes and filter add/replace, since synchronize_rcu() is called under RTNL in route4_delete() and route4_change(). An alternative fix would have been to introduce refcnt to the head struct but that is a much bigger patch. We justify this as a reasonable fix.
--- net/sched/cls_route.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-)
net/sched/cls_route.c +35 -7
@@ -297,16 +297,29 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, next =rtnl_dereference (f->next);RCU_INIT_POINTER (b->ht[h2], next);tcf_unbind_filter (tp, &f->res); -if (tcf_exts_get_net (&f->exts)) -route4_queue_work (f); -else -__route4_delete_filter (f); +/* Always defer the free. The direct +* __route4_delete_filter() path has no +* grace period and races with readers +* that cached f in the fastmap. +*/ +tcf_exts_get_net (&f->exts); +route4_queue_work (f); } }RCU_INIT_POINTER (head->table[h1],NULL );kfree_rcu (b, rcu); } } + +/* All filters are unlinked; no new reader can find them on the +* chain. Wait for in-flight readers that may still hold a filter +* pointer and have published it into the fastmap after we unlinked. +* Then flush the stale entries while head is still valid under +* RTNL, matching the route4_delete() pattern. +*/ +synchronize_rcu (); +route4_reset_fastmap (head); +kfree_rcu (head, rcu); }
@@ -334,10 +347,16 @@ static int route4_delete(struct tcf_proto *tp, void *arg, bool *last,/* unlink it */ RCU_INIT_POINTER (*fp,rtnl_dereference (f->next)); -/* Remove any fastmap lookups that might ref filter -* notice we unlink'd the filter so we can't get it -* back in the fastmap. +/* This code path assumes we have the RTNL lock. +* We wait for in-flight readers that may still hold +* the filter pointer and publish it into the fastmap +* after we unlinked it. +* Once done, no new reader can find the filter on the +* chain, so the only stale fastmap entries are the +* ones those readers just wrote. Flush them now +* while head is still valid. */ +synchronize_rcu ();route4_reset_fastmap (head);/* Delete it */
@@ -558,6 +577,15 @@ static int route4_change(struct net *net, struct sk_buff *in_skb, } } +/* Assuming RTNL lock. Wait for in-flight readers that may still +* hold a filter pointer and publish it into the fastmap after we +* inserted the new filter (or unlinked fold when replacing). +* Once they are done, flush the stale entries while head is still +* valid. Needed on both the creation and replace paths: on +* creation, readers may have cached a ROUTE4_FAILURE entry for the +* (id, iif) tuple that the new filter now matches. +*/ +synchronize_rcu ();route4_reset_fastmap (head); *arg = f;if (fold) {
Signature · 1 line
2.34.1
Paolo Abeni
maintainerQuoted 9 lines · Jamal Hadi Salim
net/sched/cls_route.c +0
-1@@ -297,16 +297,29 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, next =rtnl_dereference (f->next);RCU_INIT_POINTER (b->ht[h2], next);tcf_unbind_filter (tp, &f->res); -if (tcf_exts_get_net (&f->exts))
net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 306
Quoted 10 lines
net/sched/cls_route.c +7
-3-route4_queue_work (f); - else -__route4_delete_filter (f ); +/* Always defer the free. The direct +* __route4_delete_filter() path has no +* grace period and races with readers +* that cached f in the fastmap. +*/ +tcf_exts_get_net (&f->exts); +route4_queue_work (f);
Sashiko fears the above would be race prone:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260723105210.817079-1-jhs%40mojatatu.com
Quoted 5 lines
net/sched/cls_route.c +0
-0} }RCU_INIT_POINTER (head->table[h1],NULL );kfree_rcu (b, rcu); }
net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 321
Quoted 10 lines
net/sched/cls_route.c +9
-0} + +/* All filters are unlinked; no new reader can find them on the +* chain. Wait for in-flight readers that may still hold a filter +* pointer and have published it into the fastmap after we unlinked. +* Then flush the stale entries while head is still valid under +* RTNL, matching the route4_delete() pattern. +*/ +synchronize_rcu (); +route4_reset_fastmap (head);
This really looks like quite a big hammer. Since there is already a synchronization point for both reader and write while acquiring the fastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):
---
net/sched/cls_route.c +11 -6
@@ -52,6 +52,7 @@ struct route4_filter {struct tcf_result res;struct tcf_exts exts;u32 handle; +bool dying;struct route4_bucket *bkt;struct tcf_proto *tp;struct rcu_work rwork;
@@ -66,9 +67,11 @@ static inline int route4_fastmap_hash(u32 id, int iif)static DEFINE_SPINLOCK (fastmap_lock);static void -route4_reset_fastmap (struct route4_head *head) +route4_reset_fastmap (struct route4_head *head,struct route4_filter *f) {spin_lock_bh (&fastmap_lock); +if (f) + f->dying = true;memset (head->fastmap,0 ,sizeof (head->fastmap));spin_unlock_bh (&fastmap_lock); }
@@ -81,9 +84,11 @@ route4_set_fastmap(struct route4_head *head, u32 id, int iif,/* fastmap updates must look atomic to aling id, iff, filter */ spin_lock_bh (&fastmap_lock); - head->fastmap[h].id = id; - head->fastmap[h].iif = iif; - head->fastmap[h].filter = f; +if (!f->dying) { + head->fastmap[h].id = id; + head->fastmap[h].iif = iif; + head->fastmap[h].filter = f; + }spin_unlock_bh (&fastmap_lock); }
@@ -338,7 +343,7 @@ static int route4_delete(struct tcf_proto *tp, void *arg, bool *last,* notice we unlink'd the filter so we can't get it * back in the fastmap. */ -route4_reset_fastmap (head); +route4_reset_fastmap (head, f);/* Delete it */ tcf_unbind_filter (tp, &f->res);
@@ -558,7 +563,7 @@ static int route4_change(struct net *net, struct sk_buff *in_skb, } } -route4_reset_fastmap (head); +route4_reset_fastmap (head, fold); *arg = f;if (fold) {tcf_unbind_filter (tp, &fold->res);
Jamal Hadi Salim
maintainerQuoted 24 lines · Paolo Abeni
Quoted 9 lines · Jamal Hadi Salim
net/sched/cls_route.c +0
-1@@ -297,16 +297,29 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, next =rtnl_dereference (f->next);RCU_INIT_POINTER (b->ht[h2], next);tcf_unbind_filter (tp, &f->res); -if (tcf_exts_get_net (&f->exts))net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 306
Quoted 10 lines
net/sched/cls_route.c +7
-3-route4_queue_work (f); - else -__route4_delete_filter (f ); +/* Always defer the free. The direct +* __route4_delete_filter() path has no +* grace period and races with readers +* that cached f in the fastmap. +*/ +tcf_exts_get_net (&f->exts); +route4_queue_work (f);Sashiko fears the above would be race prone:
Quoted 2 lines
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260723105210.817079-1-jhs%40mojatatu.com
That was fixed in v2. Is there something i can do next time (message patchwork?) to not waste your time looking at an older version?
Quoted 18 lines
Quoted 7 lines
net/sched/cls_route.c +0
-0} }RCU_INIT_POINTER (head->table[h1],NULL );kfree_rcu (b, rcu); }net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 321
Quoted 10 lines
net/sched/cls_route.c +9
-0} + +/* All filters are unlinked; no new reader can find them on the +* chain. Wait for in-flight readers that may still hold a filter +* pointer and have published it into the fastmap after we unlinked. +* Then flush the stale entries while head is still valid under +* RTNL, matching the route4_delete() pattern. +*/ +synchronize_rcu (); +route4_reset_fastmap (head);
Quoted 1 line
This really looks like quite a big hammer.
It is.
I viewed it as bug fix to original intent of 1109c00547fc - that commit even though had the intent of protecting the fast map as as well (based on the wording of comment i saw).
If performance is a big concern: another approach that maybe worth considering is to totally remove the fastpath. It maybe more performant than imposing the rcus.
Quoted 5 lines
Since there is already a
synchronization point for both reader and write while acquiring thefastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):
Your approach has some holes, example misses the most dangerous part - destroy(), etc
I will mull over it and see if it can be fixed to handle all the issues. I think it's possible i just need to put time to validate/test.
cheers,
jamal
Quoted 60 lines
---
net/sched/cls_route.c +11
-6@@ -52,6 +52,7 @@ struct route4_filter {struct tcf_result res;struct tcf_exts exts;u32 handle; +bool dying;struct route4_bucket *bkt;struct tcf_proto *tp;struct rcu_work rwork;@@ -66,9 +67,11 @@ static inline int route4_fastmap_hash(u32 id, int iif)static DEFINE_SPINLOCK (fastmap_lock);static void -route4_reset_fastmap (struct route4_head *head) +route4_reset_fastmap (struct route4_head *head,struct route4_filter *f) {spin_lock_bh (&fastmap_lock); +if (f) + f->dying = true;memset (head->fastmap,0 ,sizeof (head->fastmap));spin_unlock_bh (&fastmap_lock); }@@ -81,9 +84,11 @@ route4_set_fastmap(struct route4_head *head, u32 id, int iif,/* fastmap updates must look atomic to aling id, iff, filter */ spin_lock_bh (&fastmap_lock); - head->fastmap[h].id = id; - head->fastmap[h].iif = iif; - head->fastmap[h].filter = f; +if (!f->dying) { + head->fastmap[h].id = id; + head->fastmap[h].iif = iif; + head->fastmap[h].filter = f; + }spin_unlock_bh (&fastmap_lock); }@@ -338,7 +343,7 @@ static int route4_delete(struct tcf_proto *tp, void *arg, bool *last,* notice we unlink'd the filter so we can't get it * back in the fastmap. */ -route4_reset_fastmap (head); +route4_reset_fastmap (head, f);/* Delete it */ tcf_unbind_filter (tp, &f->res);@@ -558,7 +563,7 @@ static int route4_change(struct net *net, struct sk_buff *in_skb, } } -route4_reset_fastmap (head); +route4_reset_fastmap (head, fold); *arg = f;if (fold) {tcf_unbind_filter (tp, &fold->res);
Paolo Abeni
maintainerQuoted 25 lines · Jamal Hadi Salim
Quoted 24 lines · Paolo Abeni
Quoted 9 lines · Jamal Hadi Salim
net/sched/cls_route.c +0
-1@@ -297,16 +297,29 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, next =rtnl_dereference (f->next);RCU_INIT_POINTER (b->ht[h2], next);tcf_unbind_filter (tp, &f->res); -if (tcf_exts_get_net (&f->exts))net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 306
Quoted 10 lines
net/sched/cls_route.c +7
-3-route4_queue_work (f); - else -__route4_delete_filter (f ); +/* Always defer the free. The direct +* __route4_delete_filter() path has no +* grace period and races with readers +* that cached f in the fastmap. +*/ +tcf_exts_get_net (&f->exts); +route4_queue_work (f);Sashiko fears the above would be race prone:
Quoted 5 lines
Quoted 2 lines
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260723105210.817079-1-jhs%40mojatatu.com
That was fixed in v2. Is there something i can do next time (message patchwork?) to not waste your time looking at an older version?
Usually the tool properly updates the old revision status, but sometimes it fails do to that.
Double checking that the old revision is marked as superseded in PW after posting the new one could help.
Quoted 18 lines
Quoted 16 lines
Quoted 5 lines
net/sched/cls_route.c +0
-0} }RCU_INIT_POINTER (head->table[h1],NULL );kfree_rcu (b, rcu); }net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 321
Quoted 10 lines
net/sched/cls_route.c +9
-0} + +/* All filters are unlinked; no new reader can find them on the +* chain. Wait for in-flight readers that may still hold a filter +* pointer and have published it into the fastmap after we unlinked. +* Then flush the stale entries while head is still valid under +* RTNL, matching the route4_delete() pattern. +*/ +synchronize_rcu (); +route4_reset_fastmap (head);Quoted 1 line
This really looks like quite a big hammer.
Quoted 7 lines
It is.
I viewed it as bug fix to original intent of1109c00547fc- that commit even though had the intent of protecting the fast map as as well (based on the wording of comment i saw).
If performance is a big concern: another approach that maybe worth considering is to totally remove the fastpath. It maybe more performant than imposing the rcus.
Given the amount of effort involved in rtnl lock contention reduction, I think it would be great if we could avoid the synchronize_rcu(), if it does not block the fix for an unreasonable amount of time.
Quoted 11 lines
Quoted 5 lines
Since there is already a
synchronization point for both reader and write while acquiring thefastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):Your approach has some holes, example misses the most dangerous part -
destroy(), etc
I will mull over it and see if it can be fixed to handle all the issues. I think it's possible i just need to put time to validate/test.
I would appreciate that, thanks!
Paolo
Jamal Hadi Salim
maintainerQuoted 75 lines · Paolo Abeni
Quoted 25 lines · Jamal Hadi Salim
Quoted 24 lines · Paolo Abeni
Quoted 9 lines · Jamal Hadi Salim
net/sched/cls_route.c +0
-1@@ -297,16 +297,29 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, next =rtnl_dereference (f->next);RCU_INIT_POINTER (b->ht[h2], next);tcf_unbind_filter (tp, &f->res); -if (tcf_exts_get_net (&f->exts))net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 306
Quoted 10 lines
net/sched/cls_route.c +7
-3-route4_queue_work (f); - else -__route4_delete_filter (f ); +/* Always defer the free. The direct +* __route4_delete_filter() path has no +* grace period and races with readers +* that cached f in the fastmap. +*/ +tcf_exts_get_net (&f->exts); +route4_queue_work (f);Sashiko fears the above would be race prone:
Quoted 5 lines
Quoted 2 lines
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260723105210.817079-1-jhs%40mojatatu.com
That was fixed in v2. Is there something i can do next time (message patchwork?) to not waste your time looking at an older version?
Usually the tool properly updates the old revision status, but sometimes it fails do to that.
Double checking that the old revision is marked as superseded in PW after posting the new one could help.
Quoted 18 lines
Quoted 16 lines
Quoted 5 lines
net/sched/cls_route.c +0
-0} }RCU_INIT_POINTER (head->table[h1],NULL );kfree_rcu (b, rcu); }net/sched/cls_route.c static void route4_destroy(struct tcf_proto *tp, bool rtnl_held, · line 321
Quoted 10 lines
net/sched/cls_route.c +9
-0} + +/* All filters are unlinked; no new reader can find them on the +* chain. Wait for in-flight readers that may still hold a filter +* pointer and have published it into the fastmap after we unlinked. +* Then flush the stale entries while head is still valid under +* RTNL, matching the route4_delete() pattern. +*/ +synchronize_rcu (); +route4_reset_fastmap (head);Quoted 1 line
This really looks like quite a big hammer.
Quoted 7 lines
It is.
I viewed it as bug fix to original intent of1109c00547fc- that commit even though had the intent of protecting the fast map as as well (based on the wording of comment i saw).
If performance is a big concern: another approach that maybe worth considering is to totally remove the fastpath. It maybe more performant than imposing the rcus.Given the amount of effort involved in rtnl lock contention reduction, I think it would be great if we could avoid the
synchronize_rcu(), if it does not block the fix for an unreasonable amount of time.Quoted 6 lines
Quoted 5 lines
Since there is already a
synchronization point for both reader and write while acquiring thefastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):
Quoted 6 lines
Quoted 5 lines
Your approach has some holes, example misses the most dangerous part -
destroy(), etc
I will mull over it and see if it can be fixed to handle all the issues. I think it's possible i just need to put time to validate/test.I would appreciate that, thanks!
See attached. Untested (compiles) - addresses my destroy() concern eaerlier _i think_ (unless something pops up in testing)
cheers,
jamal
Quoted 2 lines
Paolo
Paolo Abeni
maintainerQuoted 13 lines · Jamal Hadi Salim
Quoted 12 lines · Paolo Abeni
Quoted 11 lines · Jamal Hadi Salim
Quoted 4 lines · Paolo Abeni
Since there is already a
synchronization point for both reader and write while acquiring thefastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):Your approach has some holes, example misses the most dangerous part -
destroy(), etc
I will mull over it and see if it can be fixed to handle all the issues. I think it's possible i just need to put time to validate/test.
Quoted 4 lines
Quoted 1 line
I would appreciate that, thanks!
See attached. Untested (compiles) - addresses my
destroy()concern eaerlier _ithink_(unless something pops up in testing)
FWIW, the changes LGTM, thanks!
Paolo
Jamal Hadi Salim
maintainerQuoted 15 lines · Paolo Abeni
Quoted 13 lines · Jamal Hadi Salim
Quoted 12 lines · Paolo Abeni
Quoted 11 lines · Jamal Hadi Salim
Quoted 4 lines · Paolo Abeni
Since there is already a
synchronization point for both reader and write while acquiring thefastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):Your approach has some holes, example misses the most dangerous part -
destroy(), etc
I will mull over it and see if it can be fixed to handle all the issues. I think it's possible i just need to put time to validate/test.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I would appreciate that, thanks!
See attached. Untested (compiles) - addresses my
destroy()concern eaerlier _ithink_(unless something pops up in testing)FWIW, the changes LGTM, thanks!
Tests on all PoCs worked.
I will give Santosh and Victor a chance to test and will post the patch tomorrow.
cheers,
jamal
Quoted 2 lines
Paolo
Hi Jamal,
I tested the patch-paolo-mod after correcting the hunk count locally, and did not find any issues.
The attached patch does not apply as sent: hunk 5's declared line count does not match its body (16 vs 11). Could you resend a regenerated patch?
Thanks,
Santosh Kalluri
Quoted 33 lines · Jamal Hadi Salim
Quoted 15 lines · Paolo Abeni
Quoted 13 lines · Jamal Hadi Salim
Quoted 12 lines · Paolo Abeni
Quoted 11 lines · Jamal Hadi Salim
Quoted 4 lines · Paolo Abeni
Since there is already a
synchronization point for both reader and write while acquiring thefastmap_lock, I'm wondering if something alike the following could work ?!? (completely untested!!!):Your approach has some holes, example misses the most dangerous part -
destroy(), etc
I will mull over it and see if it can be fixed to handle all the issues. I think it's possible i just need to put time to validate/test.Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I would appreciate that, thanks!
See attached. Untested (compiles) - addresses my
destroy()concern eaerlier _ithink_(unless something pops up in testing)FWIW, the changes LGTM, thanks!
Tests on all PoCs worked.
I will give Santosh and Victor a chance to test and will post the patch tomorrow.cheers,
jamalQuoted 2 lines
Paolo
Quoted 8 lines · Santosh Kalluri
Hi Jamal,
I tested the patch-paolo-mod after correcting the hunk count locally, and did not find any issues.
The attached patch does not apply as sent: hunk 5's declared line count does not match its body (16 vs 11). Could you resend a regenerated patch?
Just change this :
@@ -307,6 +319,16 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held,
with this in the patch (11 instead of 16):
@@ -307,6 +319,11 @@ static void route4_destroy(struct tcf_proto *tp, bool rtnl_held,
then it applies fine for me.
Quoted 2 lines
Thanks,
Santosh Kalluri
thanks,
Willy