Skip to contentSkip to conversation

media: imon: fix use-after-free in display_close via dev_dbg

PATCH In review Deepanshu Kartikey 2 participants 4 messages Sep 28 – Sep 30

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

PATCH v2 1/1 media: imon: fix use-after-free in display_close via dev_dbg

ictx->dev is a raw pointer to the usb_interface's embedded device, cached in imon_init_intf0() without taking a reference. On disconnect, usb_disconnect() can drop the last reference on the interface and free it while a userspace fd for /dev/lcd0 is still open. When that fd is later closed, display_close() dereferences the now-freed ictx->dev via dev_dbg(), causing a use-after-free.

Fix by pinning the device with get_device() when ictx->dev is assigned, and releasing it with put_device() when ictx is freed.

Also drop the dev_dbg() in free_imon_context(), which only printed a debug message and is no longer needed.

---
v2:
- Remove the dev_dbg() in free_imon_context(), as suggested by Sean Young.

---
 drivers/media/rc/imon.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/rc/imon.c +3 -2
index 049a73b5f882..562a70186db4 100644
@@ -501,7 +501,7 @@ static void free_imon_context(struct imon_context *ictx)
 	usb_free_urb(ictx->rx_urb_intf1);
 	kfree_rcu(ictx, rcu);
 
-	dev_dbg(dev, "%s: iMON context freed\n", __func__);
+	put_device(dev);
 }
 
 /*
@@ -2253,7 +2253,7 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf,
 
 	mutex_lock(&ictx->lock);
 
-	ictx->dev = dev;
+	ictx->dev = get_device(dev);
 	ictx->usbdev_intf0 = interface_to_usbdev(intf);
 	ictx->rx_urb_intf0 = rx_urb;
 	ictx->tx_urb = tx_urb;
@@ -2314,6 +2314,7 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf,
 find_endpoint_failed:
 	mutex_unlock(&ictx->lock);
 	usb_free_urb(tx_urb);
+	put_device(ictx->dev);
 tx_urb_alloc_failed:
 	usb_free_urb(rx_urb);
 rx_urb_alloc_failed:
Signature · 1 line
2.34.1
#1 lore raw
Quoted 58 lines · Deepanshu Kartikey

ictx->dev is a raw pointer to the usb_interface's embedded device, cached in imon_init_intf0() without taking a reference. On disconnect, usb_disconnect() can drop the last reference on the interface and free it while a userspace fd for /dev/lcd0 is still open. When that fd is later closed, display_close() dereferences the now-freed ictx->dev via dev_dbg(), causing a use-after-free.

Fix by pinning the device with get_device() when ictx->dev is assigned, and releasing it with put_device() when ictx is freed.

Also drop the dev_dbg() in free_imon_context(), which only printed a debug message and is no longer needed.

---
v2:
- Remove the dev_dbg() in free_imon_context(), as suggested by Sean Young.

---
 drivers/media/rc/imon.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/rc/imon.c +3 -3
index 049a73b5f882..562a70186db4 100644
@@ -501,7 +501,7 @@ static void free_imon_context(struct imon_context *ictx)
        usb_free_urb(ictx->rx_urb_intf1);
        kfree_rcu(ictx, rcu);
 
-       dev_dbg(dev, "%s: iMON context freed\n", __func__);
+       put_device(dev);
 }
 
 /*
@@ -2253,7 +2253,7 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf,
 
        mutex_lock(&ictx->lock);
 
-       ictx->dev = dev;
+       ictx->dev = get_device(dev);
        ictx->usbdev_intf0 = interface_to_usbdev(intf);
        ictx->rx_urb_intf0 = rx_urb;
        ictx->tx_urb = tx_urb;
@@ -2314,6 +2314,7 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf,
 find_endpoint_failed:
        mutex_unlock(&ictx->lock);
        usb_free_urb(tx_urb);
+       put_device(ictx->dev);
 tx_urb_alloc_failed:
        usb_free_urb(rx_urb);
 rx_urb_alloc_failed:
--
Quoted 2 lines

2.34.1

Please disregard this patch.

Thanks

Deepanshu

maintainer
#2 lore raw
Quoted 37 lines · Deepanshu Kartikey
Quoted 35 lines · Deepanshu Kartikey

ictx->dev is a raw pointer to the usb_interface's embedded device, cached in imon_init_intf0() without taking a reference. On disconnect, usb_disconnect() can drop the last reference on the interface and free it while a userspace fd for /dev/lcd0 is still open. When that fd is later closed, display_close() dereferences the now-freed ictx->dev via dev_dbg(), causing a use-after-free.

Fix by pinning the device with get_device() when ictx->dev is assigned, and releasing it with put_device() when ictx is freed.

Also drop the dev_dbg() in free_imon_context(), which only printed a debug message and is no longer needed.

---
v2:
- Remove the dev_dbg() in free_imon_context(), as suggested by Sean Young.

---
 drivers/media/rc/imon.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/rc/imon.c +0 -0
index 049a73b5f882..562a70186db4 100644
@@ -501,7 +501,7 @@ static void free_imon_context(struct imon_context *ictx)
        usb_free_urb(ictx->rx_urb_intf1);
        kfree_rcu(ictx, rcu);
 
Quoted 2 lines

drivers/media/rc/imon.c static void free_imon_context(struct imon_context *ictx) · line 504

Quoted 2 lines
drivers/media/rc/imon.c +1 -1
-       dev_dbg(dev, "%s: iMON context freed\n", __func__);
+       put_device(dev);

Why do you need put_device()?

Quoted 9 lines

drivers/media/rc/imon.c static struct imon_context *imon_init_intf0(struct usb_interface *intf, · line 2256

Quoted 9 lines
drivers/media/rc/imon.c +1 -1
 }
 
 /*
@@ -2253,7 +2253,7 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf,
 
        mutex_lock(&ictx->lock);
 
-       ictx->dev = dev;
+       ictx->dev = get_device(dev);

Why do you need this?

Sean

Quoted 20 lines
Quoted 12 lines
drivers/media/rc/imon.c +1 -1
        ictx->usbdev_intf0 = interface_to_usbdev(intf);
        ictx->rx_urb_intf0 = rx_urb;
        ictx->tx_urb = tx_urb;
@@ -2314,6 +2314,7 @@ static struct imon_context *imon_init_intf0(struct usb_interface *intf,
 find_endpoint_failed:
        mutex_unlock(&ictx->lock);
        usb_free_urb(tx_urb);
+       put_device(ictx->dev);
 tx_urb_alloc_failed:
        usb_free_urb(rx_urb);
 rx_urb_alloc_failed:
--
Quoted 2 lines

2.34.1

Please disregard this patch.

Thanks

Deepanshu

#3 lore raw
Quoted 39 lines · Sean Young
Quoted 37 lines · Deepanshu Kartikey
Quoted 35 lines · Deepanshu Kartikey

ictx->dev is a raw pointer to the usb_interface's embedded device, cached in imon_init_intf0() without taking a reference. On disconnect, usb_disconnect() can drop the last reference on the interface and free it while a userspace fd for /dev/lcd0 is still open. When that fd is later closed, display_close() dereferences the now-freed ictx->dev via dev_dbg(), causing a use-after-free.

Fix by pinning the device with get_device() when ictx->dev is assigned, and releasing it with put_device() when ictx is freed.

Also drop the dev_dbg() in free_imon_context(), which only printed a debug message and is no longer needed.

---
v2:
- Remove the dev_dbg() in free_imon_context(), as suggested by Sean Young.

---
 drivers/media/rc/imon.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/rc/imon.c +0 -0
index 049a73b5f882..562a70186db4 100644
@@ -501,7 +501,7 @@ static void free_imon_context(struct imon_context *ictx)
        usb_free_urb(ictx->rx_urb_intf1);
        kfree_rcu(ictx, rcu);
 
Quoted 5 lines
Quoted 2 lines

drivers/media/rc/imon.c static void free_imon_context(struct imon_context *ictx) · line 504

Quoted 2 lines
drivers/media/rc/imon.c +1 -1
-       dev_dbg(dev, "%s: iMON context freed\n", __func__);
+       put_device(dev);

Why do you need put_device()?

yes it is of no use. So, I have removed it and send patch v3.

Thanks

Deepanshu