[PATCH v3] media: dvb-usb-v2: keep dvb_usb_device alive until frontend release

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 03:42:56 EST


A frontend character device that is still open when the USB device goes
away is released after dvb_usbv2_disconnect() has freed the
struct dvb_usb_device that hosts its dvb_adapter. dvb_frontend_release()
then takes fe->dvb->mdev_lock and writes into freed memory:

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)

The frontend and its struct dvb_device survive, because the open file
holds a reference to both, but nothing keeps the parent dvb_usb_device
alive. The same happens when probe fails after the frontend has been
registered: the error path frees the device while a file descriptor may
already be open.

Add a reference count to struct dvb_usb_device. Every registered
frontend takes one reference and drops it from a wrapper around
fe->ops.release, which the DVB core calls when the frontend is finally
freed, after dvb_frontend_release() has returned. Disconnect and the
probe error path mark the device dead and drop the framework reference.

The last reference can be dropped by close(), so the final teardown
(I2C adapter removal, the driver's disconnect callback and the private
data) runs from a workqueue. i2c_del_adapter() waits for every
i2c_get_adapter() reference, including the one held by an open
/dev/i2c-N descriptor, and waiting for it in close() would deadlock.

Reference the USB interface with usb_get_intf(): holding the usb_device
alone does not keep the interface alive after the disconnect callback
returns, and the deferred callback and later transfers still use it.
The transfer helpers, the frontend init/sleep callbacks and the drivers
that send their own control messages check ->dead and return -ENODEV
instead of using a device that is gone. ->dead is set after the
adapters have been torn down, so the frontend thread still runs the
driver's sleep callback that releases its locks.

Drivers that release the demodulator, and with it an embedded
struct dvb_frontend, from ->frontend_detach() free the frontend before
the last user closes it. That lifetime is a separate, pre-existing
bug; those frontends deliberately do not take this reference, because
the release wrapper would never run and the reference would leak.

The same signature was seen with four different dvb-usb-v2 drivers
(gl861, az6007, lmedm04 and au6610), so the lifetime has to be handled
by the framework rather than by a single driver.

Fixes: c79b339f9292 ("[media] dvb_usb_v2: copy current dvb_usb as a starting point")
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>
---
Changes in v3:
- reference the USB interface for the deferred teardown and later
transfers
- run the final teardown from a workqueue, as i2c_del_adapter() waits
for open i2c-dev descriptors and would deadlock close()
- set ->dead only after the adapters are torn down and use
READ_ONCE()/WRITE_ONCE()
- check ->dead in the drivers that submit their own control messages
- do not take the frontend reference for drivers that free the
frontend from ->frontend_detach()
- add the missing kernel-doc entry for fe_release (Media CI)

drivers/media/usb/dvb-usb-v2/au6610.c | 3 +
drivers/media/usb/dvb-usb-v2/az6007.c | 3 +
drivers/media/usb/dvb-usb-v2/ce6230.c | 3 +
drivers/media/usb/dvb-usb-v2/dvb_usb.h | 15 +++
drivers/media/usb/dvb-usb-v2/dvb_usb_core.c | 130 ++++++++++++++++++--
drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c | 3 +
drivers/media/usb/dvb-usb-v2/ec168.c | 3 +
drivers/media/usb/dvb-usb-v2/gl861.c | 3 +
drivers/media/usb/dvb-usb-v2/rtl28xxu.c | 3 +
9 files changed, 157 insertions(+), 9 deletions(-)

diff --git a/drivers/media/usb/dvb-usb-v2/au6610.c b/drivers/media/usb/dvb-usb-v2/au6610.c
index c20a9469f564..b103fd10d72f 100644
--- a/drivers/media/usb/dvb-usb-v2/au6610.c
+++ b/drivers/media/usb/dvb-usb-v2/au6610.c
@@ -86,6 +86,9 @@ static int au6610_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
struct dvb_usb_device *d = i2c_get_adapdata(adap);
int i;

+ if (!d || READ_ONCE(d->dead))
+ return -ENODEV;
+
if (num > 2)
return -EINVAL;

diff --git a/drivers/media/usb/dvb-usb-v2/az6007.c b/drivers/media/usb/dvb-usb-v2/az6007.c
index 65ef045b74ca..ba2f5db34006 100644
--- a/drivers/media/usb/dvb-usb-v2/az6007.c
+++ b/drivers/media/usb/dvb-usb-v2/az6007.c
@@ -752,6 +752,9 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
int length;
u8 req, addr;

+ if (!d || READ_ONCE(d->dead))
+ return -ENODEV;
+
if (mutex_lock_interruptible(&st->mutex) < 0)
return -EAGAIN;

diff --git a/drivers/media/usb/dvb-usb-v2/ce6230.c b/drivers/media/usb/dvb-usb-v2/ce6230.c
index 7ebaf3ee4491..7e113a48ab49 100644
--- a/drivers/media/usb/dvb-usb-v2/ce6230.c
+++ b/drivers/media/usb/dvb-usb-v2/ce6230.c
@@ -89,6 +89,9 @@ static int ce6230_i2c_master_xfer(struct i2c_adapter *adap,
int ret = 0, i = 0;
struct usb_req req;

+ if (!d || READ_ONCE(d->dead))
+ return -ENODEV;
+
if (num > 2)
return -EOPNOTSUPP;

diff --git a/drivers/media/usb/dvb-usb-v2/dvb_usb.h b/drivers/media/usb/dvb-usb-v2/dvb_usb.h
index ecdc20d45132..4213260f9989 100644
--- a/drivers/media/usb/dvb-usb-v2/dvb_usb.h
+++ b/drivers/media/usb/dvb-usb-v2/dvb_usb.h
@@ -11,6 +11,8 @@

#include <linux/usb/input.h>
#include <linux/firmware.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
#include <media/rc-core.h>
#include <media/media-device.h>

@@ -325,6 +327,7 @@ struct usb_data_stream {
* @fe: adapter frontends
* @fe_init: rerouted frontend-init function
* @fe_sleep: rerouted frontend-sleep function
+ * @fe_release: saved frontend-release callback
*/
struct dvb_usb_adapter {
const struct dvb_usb_adapter_properties *props;
@@ -350,6 +353,7 @@ struct dvb_usb_adapter {
struct dvb_frontend *fe[MAX_NO_OF_FE_PER_ADAP];
int (*fe_init[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
int (*fe_sleep[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
+ void (*fe_release[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
};

/**
@@ -360,6 +364,11 @@ struct dvb_usb_adapter {
* @rc_polling_active: set when RC polling is active
* @intf: pointer to the device's struct usb_interface
* @udev: pointer to the device's struct usb_device
+ * @kref: reference count; the object is freed once the last user that
+ * can outlive USB disconnect (a frontend file descriptor) drops its
+ * reference
+ * @free_work: work that runs the final teardown outside the last put
+ * @dead: set when the USB device is gone; transfers must not touch it
* @rc: remote controller configuration
* @powered: indicated whether the device is power or not
* @usb_mutex: mutex for usb control messages
@@ -379,6 +388,12 @@ struct dvb_usb_device {
bool rc_polling_active;
struct usb_interface *intf;
struct usb_device *udev;
+
+ /* lifetime */
+ struct kref kref;
+ struct work_struct free_work;
+ bool dead;
+
struct dvb_usb_rc rc;
int powered;

diff --git a/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c b/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
index bd86d250433d..09501b7293d7 100644
--- a/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
+++ b/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
@@ -7,6 +7,7 @@
*/

#include "dvb_usb_common.h"
+#include <linux/module.h>
#include <media/media-device.h>

static int dvb_usbv2_disable_rc_polling;
@@ -88,6 +89,56 @@ static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
return 0;
}

+static void dvb_usbv2_device_free_work(struct work_struct *work)
+{
+ struct dvb_usb_device *d = container_of(work, struct dvb_usb_device,
+ free_work);
+
+ /*
+ * Last reference: no frontend file descriptor can start a callback
+ * or an I2C transfer any more. Release what the disconnect path
+ * had to keep around for those callbacks.
+ *
+ * This runs from a workqueue, not from the last put(): the last put()
+ * can happen in close(), and i2c_del_adapter() waits for every
+ * i2c_get_adapter() reference, including the one held by an open
+ * /dev/i2c-N descriptor. Waiting there would deadlock a process
+ * that closes the frontend but still holds the I2C descriptor.
+ */
+ dvb_usbv2_i2c_exit(d);
+ if (d->props->disconnect)
+ d->props->disconnect(d);
+ usb_put_intf(d->intf);
+ kfree(d->priv);
+ usb_put_dev(d->udev);
+ module_put(THIS_MODULE);
+ kfree(d);
+}
+
+static void dvb_usbv2_device_release(struct kref *kref)
+{
+ struct dvb_usb_device *d = container_of(kref, struct dvb_usb_device,
+ kref);
+
+ /*
+ * The work handler may run after the caller that dropped the last
+ * reference has returned, so pin the module until it has finished.
+ */
+ __module_get(THIS_MODULE);
+ if (!schedule_work(&d->free_work))
+ module_put(THIS_MODULE);
+}
+
+static void dvb_usbv2_device_get(struct dvb_usb_device *d)
+{
+ kref_get(&d->kref);
+}
+
+static void dvb_usbv2_device_put(struct dvb_usb_device *d)
+{
+ kref_put(&d->kref, dvb_usbv2_device_release);
+}
+
#if IS_ENABLED(CONFIG_RC_CORE)
static void dvb_usb_read_remote_control(struct work_struct *work)
{
@@ -556,6 +607,10 @@ static int dvb_usb_fe_init(struct dvb_frontend *fe)
int ret;
struct dvb_usb_adapter *adap = fe->dvb->priv;
struct dvb_usb_device *d = adap_to_d(adap);
+
+ if (READ_ONCE(d->dead))
+ return -ENODEV;
+
dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
fe->id);

@@ -595,6 +650,10 @@ static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
int ret;
struct dvb_usb_adapter *adap = fe->dvb->priv;
struct dvb_usb_device *d = adap_to_d(adap);
+
+ if (READ_ONCE(d->dead))
+ return -ENODEV;
+
dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
fe->id);

@@ -630,6 +689,27 @@ static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
return ret;
}

+static void dvb_usbv2_fe_release(struct dvb_frontend *fe)
+{
+ struct dvb_usb_adapter *adap = fe->dvb->priv;
+ struct dvb_usb_device *d = adap_to_d(adap);
+ void (*release)(struct dvb_frontend *) = adap->fe_release[fe->id];
+
+ if (release) {
+ release(fe);
+#ifdef CONFIG_MEDIA_ATTACH
+ /*
+ * __dvb_frontend_free() detaches fe->ops.release, which is
+ * this wrapper now, so the driver callback is left attached.
+ * Drop the reference dvb_attach() took instead.
+ */
+ dvb_detach(release);
+#endif
+ }
+
+ dvb_usbv2_device_put(d);
+}
+
static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
{
int ret, i, count_registered = 0;
@@ -662,8 +742,36 @@ static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;

+ /*
+ * A frontend file descriptor can outlive USB disconnect. Hold
+ * a reference for the whole frontend lifetime, so the release
+ * callback the core invokes when the frontend is finally freed
+ * still runs against a live dvb_usb_device.
+ *
+ * Drivers that release the demodulator, and with it an embedded
+ * struct dvb_frontend, from ->frontend_detach() free the
+ * frontend before the last user closes it. That lifetime is
+ * broken independently of this device reference, and taking it
+ * would only leak it because the release wrapper never runs.
+ */
+ if (!d->props->frontend_detach) {
+ adap->fe_release[i] = adap->fe[i]->ops.release;
+ dvb_usbv2_device_get(d);
+#ifdef CONFIG_MEDIA_ATTACH
+ __module_get(THIS_MODULE);
+#endif
+ adap->fe[i]->ops.release = dvb_usbv2_fe_release;
+ }
+
ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
if (ret < 0) {
+ if (!d->props->frontend_detach) {
+ adap->fe[i]->ops.release = adap->fe_release[i];
+#ifdef CONFIG_MEDIA_ATTACH
+ module_put(THIS_MODULE);
+#endif
+ dvb_usbv2_device_put(d);
+ }
dev_err(&d->udev->dev,
"%s: frontend%d registration failed\n",
KBUILD_MODNAME, i);
@@ -842,7 +950,8 @@ static int dvb_usbv2_exit(struct dvb_usb_device *d)

dvb_usbv2_remote_exit(d);
dvb_usbv2_adapter_exit(d);
- dvb_usbv2_i2c_exit(d);
+
+ WRITE_ONCE(d->dead, true);

return 0;
}
@@ -912,7 +1021,11 @@ int dvb_usbv2_probe(struct usb_interface *intf,
goto err;
}

- d->intf = intf;
+ kref_init(&d->kref);
+ INIT_WORK(&d->free_work, dvb_usbv2_device_free_work);
+ usb_get_dev(udev);
+
+ d->intf = usb_get_intf(intf);
d->name = driver_info->name;
d->rc_map = driver_info->rc_map;
d->udev = udev;
@@ -992,11 +1105,13 @@ int dvb_usbv2_probe(struct usb_interface *intf,
return 0;
err_free_all:
dvb_usbv2_exit(d);
- if (d->props->disconnect)
- d->props->disconnect(d);
+ dvb_usbv2_device_put(d);
+ goto err;
err_kfree_priv:
kfree(d->priv);
err_kfree_d:
+ usb_put_intf(d->intf);
+ usb_put_dev(d->udev);
kfree(d);
err:
dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
@@ -1018,11 +1133,8 @@ void dvb_usbv2_disconnect(struct usb_interface *intf)

dvb_usbv2_exit(d);

- if (d->props->disconnect)
- d->props->disconnect(d);
-
- kfree(d->priv);
- kfree(d);
+ usb_set_intfdata(intf, NULL);
+ dvb_usbv2_device_put(d);

pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
KBUILD_MODNAME, drvname, devname);
diff --git a/drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c b/drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c
index 7e817ea506c6..a2bc756fe001 100644
--- a/drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c
+++ b/drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c
@@ -13,6 +13,9 @@ static int dvb_usb_v2_generic_io(struct dvb_usb_device *d,
{
int ret, actual_length;

+ if (READ_ONCE(d->dead))
+ return -ENODEV;
+
if (!wbuf || !wlen || !d->props->generic_bulk_ctrl_endpoint ||
!d->props->generic_bulk_ctrl_endpoint_response) {
dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, -EINVAL);
diff --git a/drivers/media/usb/dvb-usb-v2/ec168.c b/drivers/media/usb/dvb-usb-v2/ec168.c
index 973b32356b17..785dcd60d44f 100644
--- a/drivers/media/usb/dvb-usb-v2/ec168.c
+++ b/drivers/media/usb/dvb-usb-v2/ec168.c
@@ -106,6 +106,9 @@ static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
int i = 0;
int ret;

+ if (!d || READ_ONCE(d->dead))
+ return -ENODEV;
+
if (num > 2)
return -EINVAL;

diff --git a/drivers/media/usb/dvb-usb-v2/gl861.c b/drivers/media/usb/dvb-usb-v2/gl861.c
index 0538170ccf29..d04fd59ebe6c 100644
--- a/drivers/media/usb/dvb-usb-v2/gl861.c
+++ b/drivers/media/usb/dvb-usb-v2/gl861.c
@@ -94,6 +94,9 @@ static int gl861_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
u8 request, *data;
u16 value, index, size;

+ if (!d || READ_ONCE(d->dead))
+ return -ENODEV;
+
/* XXX: I2C adapter maximum data lengths are not tested */
if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
/* I2C write */
diff --git a/drivers/media/usb/dvb-usb-v2/rtl28xxu.c b/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
index d2957e4bf017..4cadaab6dcd5 100644
--- a/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
+++ b/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
@@ -144,6 +144,9 @@ static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
struct rtl28xxu_dev *dev = d->priv;
struct rtl28xxu_req req;

+ if (!d || READ_ONCE(d->dead))
+ return -ENODEV;
+
/*
* It is not known which are real I2C bus xfer limits, but testing
* with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
--
2.34.1