Skip to contentSkip to conversation

media: dvb-usb-v2: keep dvb_usb_device alive until frontend release

PATCH New YuanZhe Liu 1 participant 1 message Dec 4

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 media: dvb-usb-v2: keep dvb_usb_device alive until frontend release

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")

---
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(-)
drivers/media/usb/dvb-usb-v2/au6610.c +8 -0
index c20a9469f564..e9bd6e034f29 100644
@@ -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)) {
drivers/media/usb/dvb-usb-v2/az6007.c +8 -0
index 65ef045b74ca..d5889b1bd0df 100644
@@ -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)
drivers/media/usb/dvb-usb-v2/ce6230.c +8 -0
index 7ebaf3ee4491..9957f28a6806 100644
@@ -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 ==
drivers/media/usb/dvb-usb-v2/dvb_usb.h +18 -0
index ecdc20d45132..c78877bc6371 100644
@@ -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;
 
drivers/media/usb/dvb-usb-v2/dvb_usb_core.c +145 -9

235 lines not on this page · raw message

drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c +3 -0
index 7e817ea506c6..a2bc756fe001 100644
@@ -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);
drivers/media/usb/dvb-usb-v2/ec168.c +8 -0
index 973b32356b17..f6b68c109b9e 100644
@@ -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) {
drivers/media/usb/dvb-usb-v2/gl861.c +8 -0
index 0538170ccf29..fe684b857255 100644
@@ -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 */
drivers/media/usb/dvb-usb-v2/rtl28xxu.c +9 -0
index d2957e4bf017..d8a0af93cf85 100644
@@ -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 = {};
Signature · 1 line
2.34.1