amt: fix use-after-free in amt_source_work
Subsystems (3)
PATCH 1/1 amt: fix use-after-free in amt_source_work
amt_source_work() dereferences gnode->amt and gnode->tunnel_list before acquiring rcu_read_lock() or tunnel->lock. A concurrent teardown (amt_clear_groups -> amt_del_group) can free gnode via kfree_rcu between the pointer read and dereference because cancel_delayed_work() in amt_destroy_source() does not wait for already-running work.
Move rcu_read_lock() before the gnode dereferences so kfree_rcu cannot reclaim the memory while the work handler is accessing it. After acquiring tunnel->lock, verify the source node is still on gnode's hash via amt_lookup_src() to handle the case where teardown already processed this source.
--- drivers/net/amt.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/net/amt.c +10 -4
@@ -291,13 +291,18 @@ static void amt_source_work(struct work_struct *work)struct amt_source_node *snode =container_of (to_delayed_work (work), struct amt_source_node, source_timer); -struct amt_group_node *gnode = snode->gnode; -struct amt_dev *amt = gnode->amt; +struct amt_group_node *gnode; +struct amt_dev *amt;struct amt_tunnel_list *tunnel; +rcu_read_lock (); + gnode = snode->gnode; + amt = gnode->amt; tunnel = gnode->tunnel_list;spin_lock_bh (&tunnel->lock); -rcu_read_lock (); +if (!amt_lookup_src (tunnel, gnode,AMT_FILTER_ALL , + &snode->source_addr)) + goto unlock;if (gnode->filter_mode ==MCAST_INCLUDE ) {amt_destroy_source (snode);if (!gnode->nr_sources)
@@ -308,8 +313,9 @@ static void amt_source_work(struct work_struct *work)*/ snode->status =AMT_SOURCE_STATUS_D_FWD ; } -rcu_read_unlock (); +unlock:spin_unlock_bh (&tunnel->lock); +rcu_read_unlock (); } static void amt_act_src(struct amt_tunnel_list *tunnel,
Signature · 1 line
2.50.1 (Apple Git-155)
Simon Horman
reviewerFrom: 'Simon Horman' <[email protected]>
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
amt: fix use-after-free in amt_source_work
This patch tries to fix a UAF in amt_source_work() by moving rcu_read_lock() before the gnode dereferences and adding an amt_lookup_src() check after taking tunnel->lock to verify the source node is still present.
Quoted 7 lines
drivers/net/amt.c +0
-0@@ -291,13 +291,18 @@ static void amt_source_work(struct work_struct *work) struct amt_source_node *snode = container_of(to_delayed_work(work), struct amt_source_node,
drivers/net/amt.c static void amt_source_work(struct work_struct *work) · line 300
Quoted 10 lines
drivers/net/amt.c +5
-2source_timer); -struct amt_group_node *gnode = snode->gnode; -struct amt_dev *amt = gnode->amt; +struct amt_group_node *gnode; +struct amt_dev *amt;struct amt_tunnel_list *tunnel; +rcu_read_lock (); + gnode = snode->gnode; + amt = gnode->amt;
[High]
Is snode itself guaranteed to still be allocated by the time rcu_read_lock() runs here?
amt_destroy_source() calls cancel_delayed_work() (not the _sync() variant) and then hands snode off to source_gc_list, where __amt_source_gc_work() eventually does kfree_rcu(snode):
drivers/net/amt.c:amt_destroy_source() {
...
cancel_delayed_work(&snode->source_timer);
hlist_del_init_rcu(&snode->node);
...
spin_lock_bh(&source_gc_lock);
hlist_add_head_rcu(&snode->node, &source_gc_list);
spin_unlock_bh(&source_gc_lock);
}Once process_one_work() has already cleared WORK_STRUCT_PENDING but not yet entered amt_source_work(), cancel_delayed_work() returns 0 without waiting. If the worker is preempted at that point and amt_destroy_source(snode) plus the subsequent gc kfree_rcu(snode) run, an entire RCU grace period can elapse before the worker reaches rcu_read_lock().
Wouldn't the subsequent reads of snode->gnode, gnode->amt, gnode->tunnel_list, and snode->source_addr then be operating on freed memory?
This window appears to predate this patch, but since the patch's stated intent is "rcu_read_lock cannot reclaim the memory while the work handler is accessing it", is it worth noting that an already-dispatched work is not protected by rcu_read_lock taken inside the work itself?
drivers/net/amt.c static void amt_source_work(struct work_struct *work) · line 305
Quoted 6 lines
drivers/net/amt.c +3
-1tunnel = gnode->tunnel_list;spin_lock_bh (&tunnel->lock); -rcu_read_lock (); +if (!amt_lookup_src (tunnel, gnode,AMT_FILTER_ALL , + &snode->source_addr)) + goto unlock;
[Critical]
Does amt_lookup_src() actually verify that the original snode is still present? It matches by source_addr, not by snode pointer identity:
if (memcmp(&snode->source_addr, src, sizeof(union amt_addr)))
continue;
return snode;Consider this sequence after the worker picks up snode_A's timer but before it acquires tunnel->lock:
IGMP/MLD report path (under tunnel->lock):
amt_cleanup_srcs() -> amt_destroy_source(snode_A)
cancel_delayed_work(&snode_A->source_timer);
hlist_del_init_rcu(&snode_A->node);
hlist_add_head_rcu(&snode_A->node, &source_gc_list);
amt_add_srcs() allocates snode_B with the same source_addr in
gnode->sources. amt_source_work() now acquires tunnel->lock and calls:
amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL, &snode->source_addr)
Won't this find snode_B and return non-NULL, so the check passes and the function then proceeds to amt_destroy_source(snode) on the original snode_A pointer obtained via container_of(work)?
If so, several follow-on problems seem to arise:
amt_destroy_source(snode_A) calls hlist_del_init_rcu(&snode_A->node) without holding source_gc_lock, while __amt_source_gc_work() serializes source_gc_list manipulation under that lock — can this corrupt source_gc_list?
It also performs tunnel->nr_sources-- and gnode->nr_sources-- a second time, since snode_B's prior insertion incremented them — does this underflow accounting on still-live state?
If gnode->filter_mode == MCAST_INCLUDE and the bogus decrement reaches zero, amt_del_group(amt, gnode) runs on a still-populated group, destroying snode_B and kfree_rcu'ing gnode prematurely — is that intended?
Would comparing pointer identity instead, e.g.
if (amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL,
&snode->source_addr) != snode)
goto unlock;or checking hlist_unhashed(&snode->node) under tunnel->lock, more faithfully express "this snode is still on gnode's hash"?
Quoted 12 lines
drivers/net/amt.c +2
-1if (gnode->filter_mode ==MCAST_INCLUDE ) {amt_destroy_source (snode);if (!gnode->nr_sources)@@ -308,8 +313,9 @@ static void amt_source_work(struct work_struct *work)*/ snode->status =AMT_SOURCE_STATUS_D_FWD ; } -rcu_read_unlock (); +unlock:spin_unlock_bh (&tunnel->lock); +rcu_read_unlock (); }