[PATCH] wifi: rt2x00: fix debugfs file release after device removal
From: YuanZhe Liu
Date: Fri Dec 04 2026 - 11:17:01 EST
rt2x00 keeps its per-device debugfs state in struct rt2x00debug_intf and
stores the pointer both in inode->i_private and, on open, in
file->private_data. rt2x00debug_deregister() removes the debugfs
directory and frees that structure, but file descriptors opened earlier
survive the removal.
Closing such a descriptor later calls the file release callback, which
debugfs invokes unconditionally after removal and which is not allowed to
assume that i_private is still valid. The release handler therefore
purges an skb queue that was already freed and then calls module_put()
through the freed structure:
BUG: KASAN: slab-use-after-free in skb_queue_purge_reason
Read of size 8 at addr ...
skb_queue_purge
rt2x00debug_release_queue_dump
full_proxy_release
__fput
The same happens on the suspend path, where rt2x00debug_deregister() runs
while the device stays around and a new instance is registered on resume.
Keep the structure alive until the last debugfs file is closed. Add a
kref to struct rt2x00debug_intf, take a reference on successful open and
drop it when the file is released; rt2x00debug_deregister() drops the
reference owned by the registration. Concurrent open is serialized
against removal by debugfs itself, so no reference can be taken after
deregister() has dropped its own.
Fixes: 181d6902b6ba ("rt2x00: Queue handling overhaul")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
.../net/wireless/ralink/rt2x00/rt2x00debug.c | 24 +++++++++++++++----
1 file changed, 19 insertions(+), 5 deletions(-)
diff --git a/drivers/net/wireless/ralink/rt2x00/rt2x00debug.c b/drivers/net/wireless/ralink/rt2x00/rt2x00debug.c
index e0bca06c7094..d762d835a750 100644
--- a/drivers/net/wireless/ralink/rt2x00/rt2x00debug.c
+++ b/drivers/net/wireless/ralink/rt2x00/rt2x00debug.c
@@ -12,6 +12,7 @@
#include <linux/debugfs.h>
#include <linux/kernel.h>
+#include <linux/kref.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/sched.h>
@@ -45,6 +46,8 @@ struct rt2x00debug_intf {
*/
const struct rt2x00debug *debug;
+ struct kref kref;
+
/*
* Debugfs entries for:
* - driver folder
@@ -193,19 +196,31 @@ static int rt2x00debug_file_open(struct inode *inode, struct file *file)
{
struct rt2x00debug_intf *intf = inode->i_private;
- file->private_data = inode->i_private;
-
if (!try_module_get(intf->debug->owner))
return -EBUSY;
+ kref_get(&intf->kref);
+ file->private_data = intf;
+
return 0;
}
+static void rt2x00debug_intf_release(struct kref *kref)
+{
+ struct rt2x00debug_intf *intf =
+ container_of(kref, struct rt2x00debug_intf, kref);
+
+ kfree(intf->chipset_blob.data);
+ kfree(intf->driver_blob.data);
+ kfree(intf);
+}
+
static int rt2x00debug_file_release(struct inode *inode, struct file *file)
{
struct rt2x00debug_intf *intf = file->private_data;
module_put(intf->debug->owner);
+ kref_put(&intf->kref, rt2x00debug_intf_release);
return 0;
}
@@ -647,6 +662,7 @@ void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
intf->debug = debug;
intf->rt2x00dev = rt2x00dev;
+ kref_init(&intf->kref);
rt2x00dev->debugfs_intf = intf;
intf->driver_folder =
@@ -715,9 +731,7 @@ void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
skb_queue_purge(&intf->frame_dump_skbqueue);
debugfs_remove_recursive(intf->driver_folder);
- kfree(intf->chipset_blob.data);
- kfree(intf->driver_blob.data);
- kfree(intf);
rt2x00dev->debugfs_intf = NULL;
+ kref_put(&intf->kref, rt2x00debug_intf_release);
}
--
2.34.1