media: hackrf: fix to not free memory after the device is registered in hackrf_probe()
Subsystems (3)
PATCH RESEND v2 1/1 media: hackrf: fix to not free memory after the device is registered in hackrf_probe()
In hackrf driver, the following race condition occurs:
```
CPU0 CPU1
hackrf_probe()
kzalloc(); // alloc hackrf_dev
....
v4l2_device_register();
....
open("/path/to/dev"); // open hackrf dev
....
v4l2_device_unregister();
....
kfree(); // free hackrf_dev
....
ioctl(fd, ...);
v4l2_ioctl();
video_is_registered() // UAF!!
....
close(fd);
v4l2_release() // UAF!!
hackrf_video_release()
kfree(); // DFB!!
```When a V4L2 or video device is unregistered, the device node is removed so new open() calls are blocked.
However, file descriptors that are already open-and any in-flight I/O-do not terminate immediately; they remain valid until the last reference is dropped and the driver's release() is invoked.
Therefore, freeing device memory on the error path after hackrf_probe() has registered dev it will lead to a race to use-after-free vuln, since those already-open handles haven't been released yet.
And since release() free memory too, race to use-after-free and double-free vuln occur.
To prevent this, if device is registered from probe(), it should be modified to free memory only through release() rather than calling kfree() directly.
---
v2: Fix incorrect patch description style and CC stable mailing list
- Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/media/usb/hackrf/hackrf.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/media/usb/hackrf/hackrf.c +2 -0
@@ -1515,6 +1515,8 @@ static int hackrf_probe(struct usb_interface *intf,video_unregister_device (&dev->rx_vdev); err_v4l2_device_unregister:v4l2_device_unregister (&dev->v4l2_dev); +dev_dbg (&intf->dev,"failed=%d\n" , ret); +return ret; err_v4l2_ctrl_handler_free_tx:v4l2_ctrl_handler_free (&dev->tx_ctrl_handler); err_v4l2_ctrl_handler_free_rx:
--
Hi Jeongjun Park,
Quoted 61 lines · Jeongjun Park
In hackrf driver, the following race condition occurs:
``` CPU0 CPU1 hackrf_probe() kzalloc(); // alloc hackrf_dev .... v4l2_device_register(); .... open("/path/to/dev"); // open hackrf dev .... v4l2_device_unregister(); .... kfree(); // free hackrf_dev .... ioctl(fd, ...); v4l2_ioctl(); video_is_registered() // UAF!! .... close(fd); v4l2_release() // UAF!! hackrf_video_release() kfree(); // DFB!! ```When a V4L2 or video device is unregistered, the device node is removed so new
open()calls are blocked.However, file descriptors that are already open-and any in-flight I/O-do not terminate immediately; they remain valid until the last reference is dropped and the driver's
release()is invoked.Therefore, freeing device memory on the error path after
hackrf_probe()has registered dev it will lead to a race to use-after-free vuln, since those already-open handles haven't been released yet.And since
release()free memory too, race to use-after-free and double-free vuln occur.To prevent this, if device is registered from
probe(), it should be modified to free memory only throughrelease()rather than callingkfree()directly.Cc <[email protected]>Reported-by [email protected]Reported-by [email protected]Fixes 8bc4a9ed8504 ("[media] hackrf: add support for transmitter")Signed-off-by Jeongjun Park <[email protected]>---
v2: Fix incorrect patch description style and CC stable mailing list
- Link to v1: https://lore.kernel.org/all/[email protected]/--- drivers/media/usb/hackrf/hackrf.c | 2 ++ 1 file changed, 2 insertions(+)drivers/media/usb/hackrf/hackrf.c +0
-0
drivers/media/usb/hackrf/hackrf.c static int hackrf_probe(struct usb_interface *intf, · line 1517
Quoted 3 lines
drivers/media/usb/hackrf/hackrf.c +0
-0@@ -1515,6 +1515,8 @@ static int hackrf_probe(struct usb_interface *intf,video_unregister_device (&dev->rx_vdev); err_v4l2_device_unregister:v4l2_device_unregister (&dev->v4l2_dev);
Instead of v4l2_device_unregister() this should call v4l2_device_put(). Otherwise the memory will never be freed since the v4l2_device refcount will never reach 0.
drivers/media/usb/hackrf/hackrf.c static int hackrf_probe(struct usb_interface *intf, · line 1518
Quoted 1 line
drivers/media/usb/hackrf/hackrf.c +1
-0+dev_dbg (&intf->dev,"failed=%d\n" , ret);
Drop this line, it doesn't add anything.
Regards,
Hans
Quoted 6 lines
drivers/media/usb/hackrf/hackrf.c +1
-1+return ret; err_v4l2_ctrl_handler_free_tx:v4l2_ctrl_handler_free (&dev->tx_ctrl_handler); err_v4l2_ctrl_handler_free_rx: --
Hi Hans,
Quoted 60 lines · Hans Verkuil
Hi Jeongjun Park,
Quoted 56 lines · Jeongjun Park
In hackrf driver, the following race condition occurs:
``` CPU0 CPU1 hackrf_probe() kzalloc(); // alloc hackrf_dev .... v4l2_device_register(); .... open("/path/to/dev"); // open hackrf dev .... v4l2_device_unregister(); .... kfree(); // free hackrf_dev .... ioctl(fd, ...); v4l2_ioctl(); video_is_registered() // UAF!! .... close(fd); v4l2_release() // UAF!! hackrf_video_release() kfree(); // DFB!! ```When a V4L2 or video device is unregistered, the device node is removed so new
open()calls are blocked.However, file descriptors that are already open-and any in-flight I/O-do not terminate immediately; they remain valid until the last reference is dropped and the driver's
release()is invoked.Therefore, freeing device memory on the error path after
hackrf_probe()has registered dev it will lead to a race to use-after-free vuln, since those already-open handles haven't been released yet.And since
release()free memory too, race to use-after-free and double-free vuln occur.To prevent this, if device is registered from
probe(), it should be modified to free memory only throughrelease()rather than callingkfree()directly.Cc <[email protected]>Reported-by [email protected]Reported-by [email protected]Fixes 8bc4a9ed8504 ("[media] hackrf: add support for transmitter")Signed-off-by Jeongjun Park <[email protected]>---
v2: Fix incorrect patch description style and CC stable mailing list
- Link to v1: https://lore.kernel.org/all/[email protected]/--- drivers/media/usb/hackrf/hackrf.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 12 lines
drivers/media/usb/hackrf/hackrf.c static int hackrf_probe(struct usb_interface *intf, · line 1517
Quoted 8 lines
drivers/media/usb/hackrf/hackrf.c +0
-0@@ -1515,6 +1515,8 @@ static int hackrf_probe(struct usb_interface *intf,video_unregister_device (&dev->rx_vdev); err_v4l2_device_unregister:v4l2_device_unregister (&dev->v4l2_dev);Instead of
v4l2_device_unregister()this should callv4l2_device_put(). Otherwise the memory will never be freed since thev4l2_devicerefcount will never reach 0.
Oh, you're right.
Calling v4l2_device_unregister() instead of v4l2_device_put() causes a memory leak here.
I'll write a v3 patch for hackrf and as102 and send it to you right away.
Quoted 16 lines
drivers/media/usb/hackrf/hackrf.c static int hackrf_probe(struct usb_interface *intf, · line 1518
Quoted 2 lines
drivers/media/usb/hackrf/hackrf.c +1
-0+dev_dbg (&intf->dev,"failed=%d\n" , ret);Drop this line, it doesn't add anything.
Regards,
Hans
Quoted 6 lines
drivers/media/usb/hackrf/hackrf.c +1
-1+return ret; err_v4l2_ctrl_handler_free_tx:v4l2_ctrl_handler_free (&dev->tx_ctrl_handler); err_v4l2_ctrl_handler_free_rx: --
Regards,
Jeongjun Park