media: imon: fix use-after-free in display_close via dev_dbg
PATCH 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.
--- drivers/media/rc/imon.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/rc/imon.c +3 -1
@@ -502,6 +502,7 @@ static void free_imon_context(struct imon_context *ictx)kfree_rcu (ictx, rcu);dev_dbg (dev,"%s: iMON context freed\n" , __func__); +put_device (dev); }/*
@@ -2253,7 +2254,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 +2315,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
Sean Young
maintainerQuoted 23 lines · Deepanshu Kartikey
ictx->devis a raw pointer to the usb_interface's embedded device, cached inimon_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/lcd0is still open. When that fd is later closed,display_close()dereferences the now-freedictx->devviadev_dbg(), causing a use-after-free.Fix by pinning the device with
get_device()whenictx->devis assigned, and releasing it withput_device()when ictx is freed.Fixes 21677cfc562a ("V4L/DVB: ir-core: add imon driver")Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/media/rc/imon.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/media/rc/imon.c +0
-0
drivers/media/rc/imon.c static void free_imon_context(struct imon_context *ictx) · line 504
Quoted 3 lines
drivers/media/rc/imon.c +0
-0@@ -502,6 +502,7 @@ static void free_imon_context(struct imon_context *ictx)kfree_rcu (ictx, rcu);dev_dbg (dev,"%s: iMON context freed\n" , __func__);
The dev is only referenced in dev_dbg() here.
The debug statement is pretty pointless anyway so why don't we remove that instead.
Sean
Quoted 24 lines
drivers/media/rc/imon.c +3
-1+put_device (dev); }/* @@ -2253,7 +2254,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 +2315,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
Quoted 6 lines · Sean Young
The debug statement is pretty pointless anyway so why don't we remove that instead.
Sure, I will remove this and send patch v2
Thanks
Deepanshu
Quoted 31 lines · Sean Young
Quoted 23 lines · Deepanshu Kartikey
ictx->devis a raw pointer to the usb_interface's embedded device, cached inimon_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/lcd0is still open. When that fd is later closed,display_close()dereferences the now-freedictx->devviadev_dbg(), causing a use-after-free.Fix by pinning the device with
get_device()whenictx->devis assigned, and releasing it withput_device()when ictx is freed.Fixes 21677cfc562a ("V4L/DVB: ir-core: add imon driver")Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/media/rc/imon.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/media/rc/imon.c +0
-0drivers/media/rc/imon.c static void free_imon_context(struct imon_context *ictx) · line 504
Quoted 3 lines
drivers/media/rc/imon.c +0
-0@@ -502,6 +502,7 @@ static void free_imon_context(struct imon_context *ictx)kfree_rcu (ictx, rcu);dev_dbg (dev,"%s: iMON context freed\n" , __func__);The dev is only referenced in
dev_dbg()here.
Quoted 5 lines
The debug statement is pretty pointless anyway so why don't we remove that instead.
Sean
I misunderstood your last message.
The reported crash is in display_close(), andictx->dev is also passed to dev_dbg() in display_open() and lcd_write(), which can run after disconnect. Removing only the dev_dbg() in free_imon_context() would not fix the bug.
Do you want me to replace dev_dbg from all over the place ?
Thanks