Skip to contentSkip to conversation

media: v4l2-core: fix a use-after-free bug of sd->devnode

PATCH In review Dafna Hirschfeld 1 participant 2 messages Feb 19, 2020 – Feb 26, 2020

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

PATCH v4 1/1 media: v4l2-core: fix a use-after-free bug of sd->devnode

sd->devnode is released after calling
v4l2_subdev_release. Therefore it should be set
to NULL so that the subdev won't hold a pointer
to a released object. This fixes a reference
after free bug in function
v4l2_device_unregister_subdev

Fixes 0e43734d4c46e ("media: v4l2-subdev: add release() internal op")

---
changes since v2:
- since this is a regresion fix, I added Fixes and Cc to stable tags,
- change the commit title and log to be more clear.

changes since v3:
move the sd->devnode = NULL; line below the call to the release cb so that it can still use it.

 drivers/media/v4l2-core/v4l2-device.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/v4l2-core/v4l2-device.c +1 -0
index 63d6b147b21e..41da73ce2e98 100644
@@ -179,6 +179,7 @@ static void v4l2_subdev_release(struct v4l2_subdev *sd)
 
 	if (sd->internal_ops && sd->internal_ops->release)
 		sd->internal_ops->release(sd);
+	sd->devnode = NULL;
 	module_put(owner);
 }
 
Signature · 1 line
2.17.1
#1 lore raw

Hi,
When a subdevice is registered with v4l2_async_register_subdev_sensor_common and the main driver is unloaded and reloaded, it does not register a node for the subdevice since the devonde is not set to null.
in v4l2_device_register_subdev_nodes the sd is skipped if sd->devnode.

This patch solves this issue as well.

Dafna

Quoted 36 lines · Dafna Hirschfeld

sd->devnode is released after calling
v4l2_subdev_release. Therefore it should be set
to NULL so that the subdev won't hold a pointer
to a released object. This fixes a reference
after free bug in function
v4l2_device_unregister_subdev

Fixes 0e43734d4c46e ("media: v4l2-subdev: add release() internal op")

---
changes since v2:
- since this is a regresion fix, I added Fixes and Cc to stable tags,
- change the commit title and log to be more clear.

changes since v3:
move the sd->devnode = NULL; line below the call to the release cb so that it can still use it.

  drivers/media/v4l2-core/v4l2-device.c | 1 +
  1 file changed, 1 insertion(+)
drivers/media/v4l2-core/v4l2-device.c +1 -0
index 63d6b147b21e..41da73ce2e98 100644
@@ -179,6 +179,7 @@ static void v4l2_subdev_release(struct v4l2_subdev *sd)
 
  	if (sd->internal_ops && sd->internal_ops->release)
  		sd->internal_ops->release(sd);
+	sd->devnode = NULL;
  	module_put(owner);
  }