In __pktgen_NN_threads(), pktgen_find_dev() looks up a pktgen_dev on t->if_list under rcu_read_lock(), drops rcu_read_unlock(), and returns the raw pkt_dev pointer without holding t->if_lock. When called from pktgen_mark_device() on NETDEV_UNREGISTER, __pktgen_NN_threads() holds pktgen_thread_lock and writes pkt_dev->removal_mark = 1 outside both RCU and t->if_lock. Concurrently, the kpktgend kernel thread in pktgen_thread_worker() does not acquire pktgen_thread_lock when processing T_REMDEVALL or T_REMDEV (e.g. via "rem_device_all" on /proc/net/pktgen/kpktgend_N). It calls pktgen_remove_device() -> _rem_dev_from_if_list() under t->if_lock and frees pkt_dev via kfree_rcu(pkt_dev, rcu). If an RCU grace period elapses before __pktgen_NN_threads() writes pkt_dev->removal_mark = 1, it performs a slab-use-after-free write. Hold if_lock(t) around pktgen_find_dev() and the removal_mark update in __pktgen_NN_threads(), matching the existing rtnl_lock -> pktgen_thread_lock -> if_lock(t) lock ordering used by pktgen_change_name() and add_dev_to_thread(). Since pktgen_find_dev() has no other callers, assert t->if_lock is held and traverse t->if_list directly under the mutex. Found by static call-chain analysis of RCU-borrowed pointers, followed by manual code inspection and QEMU/KASAN verification. Fixes: 8788370a1d4b ("pktgen: RCU-ify "if_list" to remove lock in next_to_run()") Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) --- Testing: - Compile-tested on x86_64 with W=1, KASAN, and PROVE_LOCKING/LOCKDEP (net/core/pktgen.o: 0 warnings, 0 errors). - Verified in 4-vCPU QEMU (KASAN + LOCKDEP + DEBUG_ATOMIC_SLEEP) by racing RTM_DELLINK on dummy0 against "rem_device_all" writes to /proc/net/pktgen/kpktgend_0: * With a test-only 50ms race-window delay inserted before pkt_dev->removal_mark = 1, the unpatched kernel reliably hits "BUG: KASAN: slab-use-after-free in pktgen_device_event" (4-byte write at offset 76 of kmalloc-4k struct pktgen_dev freed via kfree_rcu / rcu_core). * With this patch applied (both clean and with the identical 50ms delay), 20 concurrent race iterations complete with 0 KASAN, 0 lockdep, and 0 atomic-sleep warnings. KASAN report (baseline + 50ms race-window delay): BUG: KASAN: slab-use-after-free in pktgen_device_event+0x432/0x490 Write of size 4 at addr ffff88800a47a04c by task pktgen_race_tes/80 Call Trace: pktgen_device_event+0x432/0x490 notifier_call_chain+0x9a/0x2d0 unregister_netdevice_many_notify+0xbce/0x1960 rtnl_dellink+0x367/0xa80 Allocated by task 80: __kmalloc_cache_node_noprof+0x254/0x5f0 pktgen_thread_write+0x57d/0x1630 Freed by task 0: __kasan_slab_free+0x43/0x70 __rcu_free_sheaf_prepare+0x65/0x250 rcu_free_sheaf+0x1a/0x100 rcu_core+0x636/0x1550 The buggy address is located 76 bytes inside of freed 4096-byte region [ffff88800a47a000, ffff88800a47b000) net/core/pktgen.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/net/core/pktgen.c b/net/core/pktgen.c index d63eead..39362ce 100644 --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -2032,14 +2032,17 @@ static bool __pktgen_NN_threads(const struct pktgen_net *pn, bool exact = (remove == FIND); list_for_each_entry(t, &pn->pktgen_threads, th_list) { + if_lock(t); pkt_dev = pktgen_find_dev(t, ifname, exact); if (pkt_dev) { if (remove) { pkt_dev->removal_mark = 1; t->control |= T_REMDEV; } + if_unlock(t); return true; } + if_unlock(t); } return false; } @@ -3774,8 +3777,9 @@ static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, struct pktgen_dev *p, *pkt_dev = NULL; size_t len = strlen(ifname); - rcu_read_lock(); - list_for_each_entry_rcu(p, &t->if_list, list) + lockdep_assert_held(&t->if_lock); + + list_for_each_entry(p, &t->if_list, list) if (strncmp(p->odevname, ifname, len) == 0) { if (p->odevname[len]) { if (exact || p->odevname[len] != '@') @@ -3785,7 +3789,6 @@ static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, break; } - rcu_read_unlock(); pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev); return pkt_dev; } -- 2.43.0