Skip to contentSkip to conversation

media: v4l2-fwnode: Fix subdev owner overwritten in v4l2_async_register_subdev_sensor()

PATCH Superseded Mirela Rabulea 3 participants 5 messages May 21 – May 22

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: v4l2-fwnode: Fix subdev owner overwritten in v4l2_async_register_subdev_sensor()

The v4l2 helper v4l2_async_register_subdev_sensor() calls v4l2_async_register_subdev(), which is a macro that expands to __v4l2_async_register_subdev(sd,THIS_MODULE). Since the macro is expanded inside v4l2-fwnode.c, THIS_MODULE resolves to the v4l2-fwnode module rather than the sensor driver module that originally set sd->owner. When v4l2-fwnode is built-in, THIS_MODULE evaluates to NULL, which then overwrites the sensor driver's owner with NULL.

This causes the problem that the sensor module's reference count is never incremented during async registration, so the module can be removed while the subdevice is still in use by a notifier (e.g., a CSI-2 receiver bridge driver).

Fix this by calling __v4l2_async_register_subdev() directly with sd->owner, which preserves the module owner that the sensor driver set during probe via v4l2_i2c_subdev_init() or direct assignment.

Fixes aef69d54755d ("media: v4l: fwnode: Add a convenience function for registering sensors")
---
 drivers/media/v4l2-core/v4l2-fwnode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-fwnode.c +1 -1
index 77f3298821b5..57284b7adddf 100644
@@ -1282,7 +1282,7 @@ int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
 	if (ret < 0)
 		goto out_cleanup;
 
-	ret = v4l2_async_register_subdev(sd);
+	ret = __v4l2_async_register_subdev(sd, sd->owner);
 	if (ret < 0)
 		goto out_unregister;
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Preexisting Use-After-Free Vulnerability in Subdev Owner Registration --

commit fdcb49ff4268301dc4e675c5a0c14133a98c403e
Author: Mirela Rabulea <[email protected]>

media: v4l2-fwnode: Fix subdev owner overwritten in v4l2_async_register_subdev_sensor()

This commit fixes a reference counting issue during async subdevice registration by directly calling __v4l2_async_register_subdev() with the correct module owner. Previously, the macro expansion of v4l2_async_register_subdev() incorrectly assigned THIS_MODULE to the subdevice owner.

drivers/media/v4l2-core/v4l2-fwnode.c int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd) · line 1285

Quoted 10 lines
drivers/media/v4l2-core/v4l2-fwnode.c +1 -1
index 77f3298821b5c..57284b7adddfa 100644
@@ -1282,7 +1282,7 @@ int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
 	if (ret < 0)
 		goto out_cleanup;
 
-	ret = v4l2_async_register_subdev(sd);
+	ret = __v4l2_async_register_subdev(sd, sd->owner);

[Severity: High]
This is a pre-existing issue, but does this exact same module owner overwrite bug happen in v4l2-flash-led-class.c as well?

Looking at v4l2_flash_init() and v4l2_flash_indicator_init(), they also use the v4l2_async_register_subdev() macro to register a subdevice:

drivers/media/v4l2-core/v4l2-flash-led-class.c:v4l2_flash_init() {
    ...
    ret = v4l2_async_register_subdev(sd);
    if (ret < 0)
        ...
}

Since this macro expands to __v4l2_async_register_subdev(sd, THIS_MODULE) inside v4l2-flash-led-class.c, won't THIS_MODULE evaluate to the v4l2-flash-led-class module rather than the underlying LED hardware driver module (like leds-mt6360)?

If a userspace process opens the V4L2 subdevice node /dev/v4l-subdevX, it increments the refcount of v4l2-flash-led-class but leaves the LED hardware driver's module refcount at 0.

Could this allow the LED hardware driver module to be unloaded while a userspace process still holds the open file descriptor? If unloaded, the unbind path would free the v4l2_flash and v4l2_subdev structures, and a subsequent poll, ioctl, or close could trigger a use-after-free.

Should those functions also be updated to explicitly call __v4l2_async_register_subdev() with the correct owner, perhaps using dev->driver ? dev->driver->owner : NULL?

Quoted 2 lines
drivers/media/v4l2-core/v4l2-fwnode.c +0 -0
 	if (ret < 0)
 		goto out_unregister;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Mirela Rabulea
Quoted 7 lines · Mirela Rabulea

The v4l2 helper v4l2_async_register_subdev_sensor() calls v4l2_async_register_subdev(), which is a macro that expands to __v4l2_async_register_subdev(sd,THIS_MODULE). Since the macro is expanded inside v4l2-fwnode.c, THIS_MODULE resolves to the v4l2-fwnode module rather than the sensor driver module that originally set sd->owner. When v4l2-fwnode is built-in, THIS_MODULE evaluates to NULL, which then overwrites the sensor driver's owner with NULL.

This is problem. but this patch can't resolve problem 100% because many sensor driver have not init sd->owner before call v4l2_async_register_subdev_sensor()

suggest solution like what v4l2_async_register_subdev() did

rename v4l2_async_register_subdev_sensor() to __v4l2_async_register_subdev_sensor(truct v4l2_subdev *sd, module *this)

#define v4l2_async_register_subdev_sensor(sd)
__v4l2_async_register_subdev_sensor(sd, THIS_MODULE)

Frank

Quoted 33 lines

This causes the problem that the sensor module's reference count is never incremented during async registration, so the module can be removed while the subdevice is still in use by a notifier (e.g., a CSI-2 receiver bridge driver).

Fix this by calling __v4l2_async_register_subdev() directly with sd->owner, which preserves the module owner that the sensor driver set during probe via v4l2_i2c_subdev_init() or direct assignment.

Fixes aef69d54755d ("media: v4l: fwnode: Add a convenience function for registering sensors")
---
 drivers/media/v4l2-core/v4l2-fwnode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-fwnode.c +1 -2
index 77f3298821b5..57284b7adddf 100644
@@ -1282,7 +1282,7 @@ int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
 	if (ret < 0)
 		goto out_cleanup;
 
-	ret = v4l2_async_register_subdev(sd);
+	ret = __v4l2_async_register_subdev(sd, sd->owner);
 	if (ret < 0)
 		goto out_unregister;
 
--

2.43.0

#3 lore raw

Hi Frank,

Quoted 11 lines · Frank Li
Quoted 7 lines · Mirela Rabulea

The v4l2 helper v4l2_async_register_subdev_sensor() calls v4l2_async_register_subdev(), which is a macro that expands to __v4l2_async_register_subdev(sd,THIS_MODULE). Since the macro is expanded inside v4l2-fwnode.c, THIS_MODULE resolves to the v4l2-fwnode module rather than the sensor driver module that originally set sd->owner. When v4l2-fwnode is built-in, THIS_MODULE evaluates to NULL, which then overwrites the sensor driver's owner with NULL.

This is problem. but this patch can't resolve problem 100% because many sensor driver have not init sd->owner before call v4l2_async_register_subdev_sensor()

Thanks for the suggestion. From what I see, all sensor drivers that use v4l2_async_register_subdev_sensor() also use v4l2_i2c_subdev_init(), which

sets sd->owner. However, since your proposal looks more robust and there is already a similar solution accepted for v4l2_async_register_subdev(), I will send a v2 with that approach.

Thanks,

Mirela

Quoted 43 lines

suggest solution like what v4l2_async_register_subdev() did

rename v4l2_async_register_subdev_sensor() to __v4l2_async_register_subdev_sensor(truct v4l2_subdev *sd, module *this)

#define v4l2_async_register_subdev_sensor(sd)
__v4l2_async_register_subdev_sensor(sd, THIS_MODULE)

Frank

Quoted 32 lines

This causes the problem that the sensor module's reference count is never incremented during async registration, so the module can be removed while the subdevice is still in use by a notifier (e.g., a CSI-2 receiver bridge driver).

Fix this by calling __v4l2_async_register_subdev() directly with sd->owner, which preserves the module owner that the sensor driver set during probe via v4l2_i2c_subdev_init() or direct assignment.

Fixes aef69d54755d ("media: v4l: fwnode: Add a convenience function for registering sensors")
---
  drivers/media/v4l2-core/v4l2-fwnode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-fwnode.c +1 -2
index 77f3298821b5..57284b7adddf 100644
@@ -1282,7 +1282,7 @@ int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
  	if (ret < 0)
  		goto out_cleanup;
 
-	ret = v4l2_async_register_subdev(sd);
+	ret = __v4l2_async_register_subdev(sd, sd->owner);
  	if (ret < 0)
  		goto out_unregister;
 
--

2.43.0

maintainer
#4 lore raw
Quoted 14 lines · Mirela Rabulea

Hi Frank,

Quoted 11 lines · Frank Li
Quoted 7 lines · Mirela Rabulea

The v4l2 helper v4l2_async_register_subdev_sensor() calls v4l2_async_register_subdev(), which is a macro that expands to __v4l2_async_register_subdev(sd,THIS_MODULE). Since the macro is expanded inside v4l2-fwnode.c, THIS_MODULE resolves to the v4l2-fwnode module rather than the sensor driver module that originally set sd->owner. When v4l2-fwnode is built-in, THIS_MODULE evaluates to NULL, which then overwrites the sensor driver's owner with NULL.

This is problem. but this patch can't resolve problem 100% because many sensor driver have not init sd->owner before call v4l2_async_register_subdev_sensor()

Quoted 6 lines

Thanks for the suggestion. From what I see, all sensor drivers that use v4l2_async_register_subdev_sensor() also use v4l2_i2c_subdev_init(), which

sets sd->owner. However, since your proposal looks more robust and there is already a similar solution accepted for v4l2_async_register_subdev(), I will send a v2 with that approach.

Okay, that's also make sense. let's wait for maintainers' opinion.

Frank

Quoted 48 lines

Thanks,

Mirela

Quoted 43 lines

suggest solution like what v4l2_async_register_subdev() did

rename v4l2_async_register_subdev_sensor() to __v4l2_async_register_subdev_sensor(truct v4l2_subdev *sd, module *this)

#define v4l2_async_register_subdev_sensor(sd)
__v4l2_async_register_subdev_sensor(sd, THIS_MODULE)

Frank

Quoted 32 lines

This causes the problem that the sensor module's reference count is never incremented during async registration, so the module can be removed while the subdevice is still in use by a notifier (e.g., a CSI-2 receiver bridge driver).

Fix this by calling __v4l2_async_register_subdev() directly with sd->owner, which preserves the module owner that the sensor driver set during probe via v4l2_i2c_subdev_init() or direct assignment.

Fixes aef69d54755d ("media: v4l: fwnode: Add a convenience function for registering sensors")
---
  drivers/media/v4l2-core/v4l2-fwnode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-fwnode.c +1 -2
index 77f3298821b5..57284b7adddf 100644
@@ -1282,7 +1282,7 @@ int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
  	if (ret < 0)
  		goto out_cleanup;
 
-	ret = v4l2_async_register_subdev(sd);
+	ret = __v4l2_async_register_subdev(sd, sd->owner);
  	if (ret < 0)
  		goto out_unregister;
 
--

2.43.0