media: v4l2-core: Fix Use-After-Free in v4l2_subdev_get_fwnode_pad_1_to_1
PATCH 1/1 media: v4l2-core: Fix Use-After-Free in v4l2_subdev_get_fwnode_pad_1_to_1
v4l2_subdev_get_fwnode_pad_1_to_1() drops the fwnode reference via fwnode_handle_put() before passing it to device_match_fwnode(). This creates a Use-After-Free vulnerability. If the freed memory is instantly reallocated by SLUB, the pointer comparison could accidentally match the wrong pad or trigger a KASAN panic.
Fix this by using the __free(fwnode_handle) scoped guard. This safely binds the fwnode lifecycle to the function scope, holding the reference during the match and releasing it automatically upon return.
--- drivers/media/v4l2-core/v4l2-subdev.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -3
@@ -7,7 +7,7 @@* Contact: Laurent Pinchart <[email protected]> * Sakari Ailus <[email protected]> */ - +#include <linux/cleanup.h> #include <linux/export.h> #include <linux/ioctl.h> #include <linux/leds.h>
@@ -1243,7 +1243,7 @@ const struct v4l2_file_operations v4l2_subdev_fops = {int v4l2_subdev_get_fwnode_pad_1_to_1 (struct media_entity *entity,struct fwnode_endpoint *endpoint) { -struct fwnode_handle *fwnode; + struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;struct v4l2_subdev *sd;if (!is_media_entity_v4l2_subdev (entity))
@@ -1252,7 +1252,6 @@ int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, sd =media_entity_to_v4l2_subdev (entity); fwnode =fwnode_graph_get_port_parent (endpoint->local_fwnode); -fwnode_handle_put (fwnode);if (device_match_fwnode (sd->dev, fwnode))return endpoint->port;
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 5 lines · Biren Pandya
v4l2_subdev_get_fwnode_pad_1_to_1()drops the fwnode reference viafwnode_handle_put()before passing it todevice_match_fwnode(). This creates a Use-After-Free vulnerability. If the freed memory is instantly reallocated by SLUB, the pointer comparison could accidentally match the wrong pad or trigger a KASAN panic.
There's no vulnerability in practice. The code change is still worth it in my opinion, but the commit message needs an update.
Quoted 32 lines
Fix this by using the
__free(fwnode_handle)scoped guard. This safely binds the fwnode lifecycle to the function scope, holding the reference during the match and releasing it automatically upon return.Signed-off-by Biren Pandya <[email protected]>--- drivers/media/v4l2-core/v4l2-subdev.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-subdev.c +2
-2@@ -7,7 +7,7 @@* Contact: Laurent Pinchart <[email protected]> * Sakari Ailus <[email protected]> */ - +#include <linux/cleanup.h> #include <linux/export.h> #include <linux/ioctl.h> #include <linux/leds.h> @@ -1243,7 +1243,7 @@ const struct v4l2_file_operations v4l2_subdev_fops = {int v4l2_subdev_get_fwnode_pad_1_to_1 (struct media_entity *entity,struct fwnode_endpoint *endpoint) { -struct fwnode_handle *fwnode; + struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;struct v4l2_subdev *sd;if (!is_media_entity_v4l2_subdev (entity))
drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, · line 1254
Quoted 3 lines
drivers/media/v4l2-core/v4l2-subdev.c +0
-0@@ -1252,7 +1252,6 @@ int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, sd =media_entity_to_v4l2_subdev (entity); fwnode =fwnode_graph_get_port_parent (endpoint->local_fwnode);
The recommended usage is to declare the variable where initialized:
struct fwnode_handle *fwnode __free(fwnode_handle) = fwnode_graph_get_port_parent(endpoint->local_fwnode);
Quoted 4 lines
drivers/media/v4l2-core/v4l2-subdev.c +0
-1-fwnode_handle_put (fwnode);if (device_match_fwnode (sd->dev, fwnode))return endpoint->port;
Signature · 3 lines
Regards, Laurent Pinchart
PATCH v2 1/1 media: v4l2-core: Drop manual fwnode_handle_put() via scope-based cleanup
In reply to Biren Pandyav4l2_subdev_get_fwnode_pad_1_to_1() acquires a fwnode reference and drops it via fwnode_handle_put() before calling device_match_fwnode(). This requires manual lifecycle management.
Simplify the function by converting the local fwnode variable to use the __free(fwnode_handle) scope-based cleanup macro.
---
Changes in v2:
- Updated the commit message to remove the "Use-After-Free" language,
as there is no vulnerability in practice.
- Adjusted the implementation to declare struct fwnode_handle *fwnode directly at the point of initialization, rather than at the top of the block, conforming to recommended usage.
- Renamed the patch subject to better reflect the true nature of the change.
- Link to v1: https://lore.kernel.org/all/[email protected]/
Hi Laurent,
My sincere apologies for the mailing list noise earlier. That duplicate submission was an accidental misfire while I was adjusting my git send-email scripts, and I deeply apologize for the lack of versioning and threading. I am still getting up to speed with the subsystem's submission workflow! Here is the correctly formatted v2 addressing Sakari's feedback.
drivers/media/v4l2-core/v4l2-subdev.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -3
@@ -1243,7 +1243,8 @@ const struct v4l2_file_operations v4l2_subdev_fops = {int v4l2_subdev_get_fwnode_pad_1_to_1 (struct media_entity *entity,struct fwnode_endpoint *endpoint) { - struct fwnode_handle *fwnode __free(fwnode_handle) = NULL; + struct fwnode_handle *fwnode __free(fwnode_handle) = +fwnode_graph_get_port_parent (endpoint->local_fwnode);struct v4l2_subdev *sd;if (!is_media_entity_v4l2_subdev (entity))
@@ -1251,8 +1252,6 @@ int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, sd =media_entity_to_v4l2_subdev (entity); - fwnode =fwnode_graph_get_port_parent (endpoint->local_fwnode); -if (device_match_fwnode (sd->dev, fwnode))return endpoint->port;
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 1/1 media: v4l2-core: Drop manual fwnode_handle_put() via scope-based cleanup
In reply to Biren PandyaSimplify v4l2_subdev_get_fwnode_pad_1_to_1() by converting the local fwnode variable to use the __free(fwnode_handle) scope-based cleanup macro.
This removes the need for manual fwnode_handle_put() calls and naturally ensures the fwnode reference is held during the device_match_fwnode() comparison.
---
Changes in v2:
- Updated the commit message to remove the "Use-After-Free" language,
as there is no vulnerability in practice.
- Adjusted the implementation to declare struct fwnode_handle *fwnode directly at the point of initialization, rather than at the top of the block, conforming to recommended usage.
- Renamed the patch subject to better reflect the true nature of the change.
- Link to v1: https://lore.kernel.org/all/[email protected]/
drivers/media/v4l2-core/v4l2-subdev.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -3
@@ -1243,15 +1243,14 @@ const struct v4l2_file_operations v4l2_subdev_fops = {int v4l2_subdev_get_fwnode_pad_1_to_1 (struct media_entity *entity,struct fwnode_endpoint *endpoint) { -struct fwnode_handle *fwnode; + struct fwnode_handle *fwnode __free(fwnode_handle) = +fwnode_graph_get_port_parent (endpoint->local_fwnode);struct v4l2_subdev *sd;if (!is_media_entity_v4l2_subdev (entity))return -EINVAL ; sd =media_entity_to_v4l2_subdev (entity); - fwnode =fwnode_graph_get_port_parent (endpoint->local_fwnode); -fwnode_handle_put (fwnode);if (device_match_fwnode (sd->dev, fwnode))
return endpoint->port;
Signature · 1 line
2.50.1 (Apple Git-155)