Skip to contentSkip to conversation

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

PATCH Superseded YuanZhe Liu 1 participant 1 message Dec 4

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 I2C adapter, the driver's disconnect callback and the private data are released with the last reference. The transfer helper and the frontend init/sleep callbacks check ->dead and return -ENODEV instead of using a device that is gone.

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")
---
 drivers/media/usb/dvb-usb-v2/dvb_usb.h      | 11 +++
 drivers/media/usb/dvb-usb-v2/dvb_usb_core.c | 94 +++++++++++++++++++--
 drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c  |  3 +
 3 files changed, 100 insertions(+), 8 deletions(-)
drivers/media/usb/dvb-usb-v2/dvb_usb.h +11 -0
index ecdc20d45132..7580c464086e 100644
@@ -11,6 +11,7 @@
 
 #include <linux/usb/input.h>
 #include <linux/firmware.h>
+#include <linux/kref.h>
 #include <media/rc-core.h>
 #include <media/media-device.h>
 
@@ -350,6 +351,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 +362,10 @@ 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
+ * @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 +385,11 @@ struct dvb_usb_device {
 	bool rc_polling_active;
 	struct usb_interface *intf;
 	struct usb_device *udev;
+
+	/* lifetime */
+	struct kref kref;
+	bool dead;
+
 	struct dvb_usb_rc rc;
 	int powered;
 
drivers/media/usb/dvb-usb-v2/dvb_usb_core.c +86 -8

162 lines not on this page · raw message

drivers/media/usb/dvb-usb-v2/dvb_usb_urb.c +3 -0
index 7e817ea506c6..c942a754aab8 100644
@@ -13,6 +13,9 @@ static int dvb_usb_v2_generic_io(struct dvb_usb_device *d,
 {
 	int ret, actual_length;
 
+	if (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);
Signature · 1 line
2.34.1