wifi: mac80211: fix use-after-free risk in sta debugfs removal
Subsystems (3)
PATCH 1/1 wifi: mac80211: fix use-after-free risk in sta debugfs removal
A NULL pointer dereference may occur in ieee80211_sta_debugfs_remove() when debugfs_remove_recursive() is called on a dentry whose inode has already been freed. This can happen due to a race between STA teardown and debugfs cleanup.
Fix this by checking that both sta->debugfs_dir and its d_inode are valid before invoking debugfs_remove_recursive().
This avoids the crash reported in syzbot bug:
wlan1: send auth to aa:09:b7:99:c0:d7 (try 2/3)
wlan1: send auth to aa:09:b7:99:c0:d7 (try 3/3)
wlan1: authentication with aa:09:b7:99:c0:d7 timed out
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000029: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000148-0x000000000000014f] CPU: 0 UID: 0 PID: 171 Comm: kworker/u4:4 Not tainted 6.16.0-rc7-syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2~bpo12+1 04/01/2014 Workqueue: events_unbound cfg80211_wiphy_work RIP: 0010:kasan_byte_accessible+0x12/0x30 mm/kasan/generic.c:199 Code: 0f 1f 84 00 00 00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 66 0f 1f 00 48 c1 ef 03 48 b8 00 00 00 00 00 fc ff df <0f> b6 04 07 3c 08 0f 92 c0 c3 cc cc cc cc cc 66 66 66 66 66 66 2e RSP: 0018:ffffc90001977400 EFLAGS: 00010202 RAX: dffffc0000000000 RBX: ffffffff8b713286 RCX: ca5c1933e35f3700 RDX: 0000000000000000 RSI: ffffffff8b713286 RDI: 0000000000000029 RBP: ffffffff824067f0 R08: 0000000000000001 R09: 0000000000000000 R10: dffffc0000000000 R11: ffffed10085cf24c R12: 0000000000000000 R13: 0000000000000148 R14: 0000000000000148 R15: 0000000000000001 FS: 0000000000000000(0000) GS:ffff88808d218000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000001b2f55ffff CR3: 000000005030a000 CR4: 0000000000352ef0 Call Trace: <TASK> __kasan_check_byte+0x12/0x40 mm/kasan/common.c:556 kasan_check_byte include/linux/kasan.h:399 [inline] lock_acquire+0x8d/0x360 kernel/locking/lockdep.c:5845 down_write+0x96/0x1f0 kernel/locking/rwsem.c:1577 inode_lock include/linux/fs.h:869 [inline] simple_recursive_removal+0x90/0x690 fs/libfs.c:616 debugfs_remove+0x5b/0x70 fs/debugfs/inode.c:805 ieee80211_sta_debugfs_remove+0x40/0x70 net/mac80211/debugfs_sta.c:1279 __sta_info_destroy_part2+0x352/0x450 net/mac80211/sta_info.c:1501 __sta_info_destroy net/mac80211/sta_info.c:1517 [inline] sta_info_destroy_addr+0xf5/0x140 net/mac80211/sta_info.c:1529 ieee80211_destroy_auth_data+0x12d/0x260 net/mac80211/mlme.c:4597 ieee80211_sta_work+0x11cf/0x3600 net/mac80211/mlme.c:8310 cfg80211_wiphy_work+0x2df/0x460 net/wireless/core.c:435 process_one_work kernel/workqueue.c:3238 [inline] process_scheduled_works+0xae1/0x17b0 kernel/workqueue.c:3321 worker_thread+0x8a0/0xda0 kernel/workqueue.c:3402 kthread+0x70e/0x8a0 kernel/kthread.c:464 ret_from_fork+0x3fc/0x770 arch/x86/kernel/process.c:148 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 </TASK>
--- net/mac80211/debugfs_sta.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/mac80211/debugfs_sta.c +2 -1
@@ -1276,7 +1276,8 @@ void ieee80211_sta_debugfs_add(struct sta_info *sta)void ieee80211_sta_debugfs_remove (struct sta_info *sta) { -debugfs_remove_recursive (sta->debugfs_dir); +if (sta->debugfs_dir && sta->debugfs_dir->d_inode) +debugfs_remove_recursive (sta->debugfs_dir); sta->debugfs_dir =NULL ; }
Signature · 1 line
2.43.0
Johannes Berg
maintainerQuoted 9 lines · Moon Hee Lee
A NULL pointer dereference may occur in
ieee80211_sta_debugfs_remove()whendebugfs_remove_recursive()is called on a dentry whose inode has already been freed. This can happen due to a race between STA teardown and debugfs cleanup.Fix this by checking that both
sta->debugfs_dirand itsd_inodeare valid before invokingdebugfs_remove_recursive().This avoids the crash reported in syzbot bug:
Maybe so, but it really looks like a bandaid?! Why are we trying to remove it twice in the first place?
johannes
Quoted 2 lines · Johannes Berg
Maybe so, but it really looks like a bandaid?! Why are we trying to remove it twice in the first place?
Thanks, Johannes. I agree this patch is only defensive and does not address the root cause.
I am currently tracing the sta_info_destroy flow to determine whether this is caused by a duplicate free or an unsynchronized access to sta->debugfs_dir. If the race is confirmed, I will follow up with a more complete fix that properly handles the lifecycle.