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") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- Changes in v2: - add the missing kernel-doc entry for the new fe_release member, reported by Media CI. drivers/media/usb/dvb-usb-v2/dvb_usb.h | 12 +++ 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, 101 insertions(+), 8 deletions(-) diff --git a/drivers/media/usb/dvb-usb-v2/dvb_usb.h b/drivers/media/usb/dvb-usb-v2/dvb_usb.h index ecdc20d45132..35c24236387a 100644 --- a/drivers/media/usb/dvb-usb-v2/dvb_usb.h +++ b/drivers/media/usb/dvb-usb-v2/dvb_usb.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -325,6 +326,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 +352,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 +363,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 +386,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; 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..4a82590bd363 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 #include static int dvb_usbv2_disable_rc_polling; @@ -88,6 +89,34 @@ static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d) return 0; } +static void dvb_usbv2_device_release(struct kref *kref) +{ + struct dvb_usb_device *d = container_of(kref, struct dvb_usb_device, + kref); + + /* + * 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. + */ + dvb_usbv2_i2c_exit(d); + if (d->props->disconnect) + d->props->disconnect(d); + kfree(d->priv); + usb_put_dev(d->udev); + kfree(d); +} + +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 +585,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 (d->dead) + return -ENODEV; + dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id, fe->id); @@ -595,6 +628,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 (d->dead) + return -ENODEV; + dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id, fe->id); @@ -630,6 +667,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; @@ -661,9 +719,27 @@ static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap) adap->fe[i]->ops.init = dvb_usb_fe_init; adap->fe_sleep[i] = adap->fe[i]->ops.sleep; adap->fe[i]->ops.sleep = dvb_usb_fe_sleep; + adap->fe_release[i] = adap->fe[i]->ops.release; + + /* + * 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. + */ + 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) { + 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); @@ -840,9 +916,10 @@ static int dvb_usbv2_exit(struct dvb_usb_device *d) { dev_dbg(&d->udev->dev, "%s:\n", __func__); + d->dead = true; + dvb_usbv2_remote_exit(d); dvb_usbv2_adapter_exit(d); - dvb_usbv2_i2c_exit(d); return 0; } @@ -912,6 +989,9 @@ int dvb_usbv2_probe(struct usb_interface *intf, goto err; } + kref_init(&d->kref); + usb_get_dev(udev); + d->intf = intf; d->name = driver_info->name; d->rc_map = driver_info->rc_map; @@ -992,11 +1072,12 @@ 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_dev(d->udev); kfree(d); err: dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret); @@ -1018,11 +1099,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..c942a754aab8 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 (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); -- 2.34.1