[PATCH] media: dvb-core: do not touch a removed adapter at frontend release

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 11:45:14 EST


A frontend file descriptor that stays open across USB disconnect is
released after the driver has freed the adapter. dvb_frontend_release()
then dereferences fe->dvb unconditionally:

KASAN: slab-use-after-free in mutex_lock
Write of size 8 at addr ffff88800aa30690
...
dvb_frontend_release (dvb_frontend.c:2942)
__fput (fs/file_table.c:512)
...
kfree (mm/slub.c:6802)
dvb_usbv2_disconnect (dvb_usb_core.c:1025)

dvb_frontend_stop() already records that the frontend was removed in
fe->exit before the driver releases the adapter. Serialize the release
path with frontend_mutex and skip the media source teardown when
fe->exit is set: the media device has been unregistered by then, and
fe->dvb must not be dereferenced.

The debug statement at the top of the function is the other
unconditional user of fe->dvb; remove it, as this path can run after
the adapter is gone.

This replaces the earlier attempt to keep the whole dvb_usb_device and
its I2C adapter alive past disconnect; the release path only needs to
stop touching the adapter.

Fixes: 68ff5767ddc4 ("[media] media: dvb-frontend invoke enable/disable_source handlers")
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>
---
drivers/media/dvb-core/dvb_frontend.c | 26 +++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/drivers/media/dvb-core/dvb_frontend.c b/drivers/media/dvb-core/dvb_frontend.c
index 0286da57f382..bdedf6cc8a25 100644
--- a/drivers/media/dvb-core/dvb_frontend.c
+++ b/drivers/media/dvb-core/dvb_frontend.c
@@ -2927,8 +2927,6 @@ static int dvb_frontend_release(struct inode *inode, struct file *file)
struct dvb_frontend_private *fepriv = fe->frontend_priv;
int ret;

- dev_dbg(fe->dvb->device, "%s:\n", __func__);
-
if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
fepriv->release_jiffies = jiffies;
mb();
@@ -2939,14 +2937,24 @@ static int dvb_frontend_release(struct inode *inode, struct file *file)
if (dvbdev->users == -1) {
wake_up(&fepriv->wait_queue);
#ifdef CONFIG_MEDIA_CONTROLLER_DVB
- mutex_lock(&fe->dvb->mdev_lock);
- if (fe->dvb->mdev) {
- mutex_lock(&fe->dvb->mdev->graph_mutex);
- if (fe->dvb->mdev->disable_source)
- fe->dvb->mdev->disable_source(dvbdev->entity);
- mutex_unlock(&fe->dvb->mdev->graph_mutex);
+ /*
+ * dvb_frontend_stop() sets fe->exit before the driver frees
+ * the adapter. If the frontend was removed while this file
+ * was open, the media device has been unregistered already
+ * and fe->dvb is gone; do not touch it.
+ */
+ mutex_lock(&frontend_mutex);
+ if (fe->exit == DVB_FE_NO_EXIT) {
+ mutex_lock(&fe->dvb->mdev_lock);
+ if (fe->dvb->mdev) {
+ mutex_lock(&fe->dvb->mdev->graph_mutex);
+ if (fe->dvb->mdev->disable_source)
+ fe->dvb->mdev->disable_source(dvbdev->entity);
+ mutex_unlock(&fe->dvb->mdev->graph_mutex);
+ }
+ mutex_unlock(&fe->dvb->mdev_lock);
}
- mutex_unlock(&fe->dvb->mdev_lock);
+ mutex_unlock(&frontend_mutex);
#endif
if (fe->exit != DVB_FE_NO_EXIT)
wake_up(&dvbdev->wait_queue);
--
2.34.1