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

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 04:26:40 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 dedicated 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.
The workqueue is drained in module_exit(), so the handler cannot
outlive this module. The subdriver module is pinned from probe until
the handler has run, and its owner is also set as the I2C adapter
owner, so the callbacks, the I2C algorithm and the driver data stay
valid while the teardown and the frontend release run.

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; the driver transfer callbacks
check again after taking their transfer lock. ->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. Such a frontend must not take this reference,
because the release wrapper would never run and the reference would
leak. A driver that has a frontend which ->frontend_detach() does not
release sets fe_survives_detach for it; rtl28xxu does so for the
cxd2837er frontend that is released by its own ops.release.

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 v4:
- pin the subdriver module (props->owner) from probe until the work
runs, and use it as the I2C adapter owner
- run the teardown on a dedicated WQ_UNBOUND workqueue drained in
module_exit(), instead of system_wq plus a per-work core module
reference
- per-frontend ownership: keep the device reference for frontends that
->frontend_detach() does not release (rtl28xxu cxd2837er)
- re-check ->dead after taking the driver transfer locks
- drop the redundant NULL check that ran after the first dereference

drivers/media/usb/dvb-usb-v2/au6610.c | 8 +
drivers/media/usb/dvb-usb-v2/az6007.c | 8 +
drivers/media/usb/dvb-usb-v2/ce6230.c | 8 +
drivers/media/usb/dvb-usb-v2/dvb_usb.h | 18 +++
drivers/media/usb/dvb-usb-v2/dvb_usb_core.c | 154 ++++++++++++++++++--
drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c | 3 +
drivers/media/usb/dvb-usb-v2/ec168.c | 8 +
drivers/media/usb/dvb-usb-v2/gl861.c | 8 +
drivers/media/usb/dvb-usb-v2/rtl28xxu.c | 9 ++
9 files changed, 215 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..e9bd6e034f29 100644
--- a/drivers/media/usb/dvb-usb-v2/au6610.c
+++ b/drivers/media/usb/dvb-usb-v2/au6610.c
@@ -86,12 +86,20 @@ 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 (READ_ONCE(d->dead))
+ return -ENODEV;
+
if (num > 2)
return -EINVAL;

if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
return -EAGAIN;

+ if (READ_ONCE(d->dead)) {
+ mutex_unlock(&d->i2c_mutex);
+ return -ENODEV;
+ }
+
for (i = 0; i < num; i++) {
/* write/read request */
if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
diff --git a/drivers/media/usb/dvb-usb-v2/az6007.c b/drivers/media/usb/dvb-usb-v2/az6007.c
index 65ef045b74ca..d5889b1bd0df 100644
--- a/drivers/media/usb/dvb-usb-v2/az6007.c
+++ b/drivers/media/usb/dvb-usb-v2/az6007.c
@@ -752,9 +752,17 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
int length;
u8 req, addr;

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

+ if (READ_ONCE(d->dead)) {
+ mutex_unlock(&st->mutex);
+ return -ENODEV;
+ }
+
for (i = 0; i < num; i++) {
addr = msgs[i].addr << 1;
if (((i + 1) < num)
diff --git a/drivers/media/usb/dvb-usb-v2/ce6230.c b/drivers/media/usb/dvb-usb-v2/ce6230.c
index 7ebaf3ee4491..9957f28a6806 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 (READ_ONCE(d->dead))
+ return -ENODEV;
+
if (num > 2)
return -EOPNOTSUPP;

@@ -97,6 +100,11 @@ static int ce6230_i2c_master_xfer(struct i2c_adapter *adap,
if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
return -EAGAIN;

+ if (READ_ONCE(d->dead)) {
+ mutex_unlock(&d->i2c_mutex);
+ return -ENODEV;
+ }
+
while (i < num) {
if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
if (msg[i].addr ==
diff --git a/drivers/media/usb/dvb-usb-v2/dvb_usb.h b/drivers/media/usb/dvb-usb-v2/dvb_usb.h
index ecdc20d45132..c78877bc6371 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,9 @@ 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
+ * @fe_survives_detach: frontend is not released by ->frontend_detach()
+ * and keeps the device reference until its own release callback runs
*/
struct dvb_usb_adapter {
const struct dvb_usb_adapter_properties *props;
@@ -350,6 +355,8 @@ 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 *);
+ bool fe_survives_detach[MAX_NO_OF_FE_PER_ADAP];
};

/**
@@ -360,6 +367,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 +391,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..1e3608430473 100644
--- a/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
+++ b/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c
@@ -7,8 +7,11 @@
*/

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

+static struct workqueue_struct *dvb_usbv2_wq;
+
static int dvb_usbv2_disable_rc_polling;
module_param_named(disable_rc_polling, dvb_usbv2_disable_rc_polling, int, 0644);
MODULE_PARM_DESC(disable_rc_polling,
@@ -63,6 +66,7 @@ static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)

strscpy(d->i2c_adap.name, d->name, sizeof(d->i2c_adap.name));
d->i2c_adap.algo = d->props->i2c_algo;
+ d->i2c_adap.owner = d->props->owner;
d->i2c_adap.dev.parent = &d->udev->dev;
i2c_set_adapdata(&d->i2c_adap, d);

@@ -88,6 +92,54 @@ 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(d->props->owner);
+ 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 workqueue is drained in module_exit(), so the handler cannot
+ * outlive this module; the subdriver is pinned until it has run.
+ */
+ queue_work(dvb_usbv2_wq, &d->free_work);
+}
+
+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 +608,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 +651,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 +690,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 +743,38 @@ 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. Such a frontend
+ * must not take this reference, because the release wrapper
+ * would never run and the reference would leak. A driver that
+ * has a frontend which ->frontend_detach() does not release
+ * sets fe_survives_detach for it.
+ */
+ if (!d->props->frontend_detach || adap->fe_survives_detach[i]) {
+ 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_survives_detach[i]) {
+ 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 +953,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,11 +1024,16 @@ 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;
d->props = driver_info->props;
+ __module_get(d->props->owner);

if (intf->cur_altsetting->desc.bInterfaceNumber !=
d->props->bInterfaceNumber) {
@@ -992,11 +1109,14 @@ 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:
+ module_put(d->props->owner);
+ 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 +1138,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);
@@ -1124,4 +1241,23 @@ MODULE_VERSION("2.0");
MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@xxxxxxxxx>");
MODULE_AUTHOR("Antti Palosaari <crope@xxxxxx>");
MODULE_DESCRIPTION("DVB USB common");
+
+static int __init dvb_usbv2_module_init(void)
+{
+ dvb_usbv2_wq = alloc_workqueue("dvb_usbv2",
+ WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
+ if (!dvb_usbv2_wq)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static void __exit dvb_usbv2_module_exit(void)
+{
+ destroy_workqueue(dvb_usbv2_wq);
+}
+
+module_init(dvb_usbv2_module_init);
+module_exit(dvb_usbv2_module_exit);
+
MODULE_LICENSE("GPL");
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..f6b68c109b9e 100644
--- a/drivers/media/usb/dvb-usb-v2/ec168.c
+++ b/drivers/media/usb/dvb-usb-v2/ec168.c
@@ -106,12 +106,20 @@ static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
int i = 0;
int ret;

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

if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
return -EAGAIN;

+ if (READ_ONCE(d->dead)) {
+ mutex_unlock(&d->i2c_mutex);
+ return -ENODEV;
+ }
+
while (i < num) {
if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
if (msg[i].addr == ec168_ec100_config.demod_address) {
diff --git a/drivers/media/usb/dvb-usb-v2/gl861.c b/drivers/media/usb/dvb-usb-v2/gl861.c
index 0538170ccf29..fe684b857255 100644
--- a/drivers/media/usb/dvb-usb-v2/gl861.c
+++ b/drivers/media/usb/dvb-usb-v2/gl861.c
@@ -38,6 +38,11 @@ static int gl861_ctrl_msg(struct dvb_usb_device *d, u8 request, u16 value,

mutex_lock(&d->usb_mutex);

+ if (READ_ONCE(d->dead)) {
+ ret = -ENODEV;
+ goto err_mutex_unlock;
+ }
+
switch (request) {
case CMD_WRITE:
memcpy(ctx->buf, data, size);
@@ -94,6 +99,9 @@ static int gl861_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
u8 request, *data;
u16 value, index, size;

+ if (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..d8a0af93cf85 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 (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.
@@ -169,6 +172,11 @@ static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
return -EAGAIN;

+ if (READ_ONCE(d->dead)) {
+ mutex_unlock(&d->i2c_mutex);
+ return -ENODEV;
+ }
+
if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
(msg[1].flags & I2C_M_RD)) {
if (msg[0].len > 24 || msg[1].len > 24) {
@@ -1029,6 +1037,7 @@ static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
if (!adap->fe[1])
goto err_slave_demod_failed;
adap->fe[1]->id = 1;
+ adap->fe_survives_detach[1] = true;
dev->i2c_client_slave_demod = NULL;
} else {
struct si2168_config si2168_config = {};
--
2.34.1