Skip to contentSkip to conversation

media: i2c: mt9p031: Fix Use-After-Free in mt9p031_parse_properties()

PATCH Superseded Biren Pandya 2 participants 2 messages Jun 19 – Jul 30

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 media: i2c: mt9p031: Fix Use-After-Free in mt9p031_parse_properties()

The mt9p031_parse_properties() function calls fwnode_handle_put(np) to release the fwnode handle. However, immediately after this call, np is used in fwnode_property_read_u32(), leading to a Use-After-Free bug.

Use the __free(fwnode_handle) attribute for the np pointer to automate cleanup. This allows us to remove the manual fwnode_handle_put(np) call, ensuring the handle is only dropped when the function returns, thus preventing the Use-After-Free.

Fixes 8d4da37c3006 ("[media] media: i2c: mt9p031: add OF support")

---
Changes in v3:
- Fixed the incorrect commit hash in the Fixes tag that caused checkpatch to fail.

Changes in v2:
- Utilized __free(fwnode_handle) to automate cleanup and safely fix the UAF as suggested by reviewers.

---
 drivers/media/i2c/mt9p031.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/i2c/mt9p031.c +2 -3
index ea5d43d925ffa..3b3714b0ef8f3 100644
@@ -1067,15 +1067,14 @@ static int mt9p031_parse_properties(struct mt9p031 *mt9p031, struct device *dev)
 	struct v4l2_fwnode_endpoint endpoint = {
 		.bus_type = V4L2_MBUS_PARALLEL
 	};
-	struct fwnode_handle *np;
+	struct fwnode_handle *np __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
 	int ret;
 
-	np = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
 	if (!np)
 		return dev_err_probe(dev, -EINVAL, "endpoint node not found\n");
 
 	ret = v4l2_fwnode_endpoint_parse(np, &endpoint);
-	fwnode_handle_put(np);
 	if (ret)
 		return dev_err_probe(dev, -EINVAL, "could not parse endpoint\n");
 
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw

Hi Biren,

Quoted 11 lines · Biren Pandya

The mt9p031_parse_properties() function calls fwnode_handle_put(np) to release the fwnode handle. However, immediately after this call, np is used in fwnode_property_read_u32(), leading to a Use-After-Free bug.

Use the __free(fwnode_handle) attribute for the np pointer to automate cleanup. This allows us to remove the manual fwnode_handle_put(np) call, ensuring the handle is only dropped when the function returns, thus preventing the Use-After-Free.

Fixes 8d4da37c3006 ("[media] media: i2c: mt9p031: add OF support")

This should be cc'd to stable.

Quoted 16 lines

---
Changes in v3:
- Fixed the incorrect commit hash in the Fixes tag that caused checkpatch to fail.

Changes in v2:
- Utilized __free(fwnode_handle) to automate cleanup and safely fix the UAF as suggested by reviewers.

---
 drivers/media/i2c/mt9p031.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/i2c/mt9p031.c +0 -0
index ea5d43d925ffa..3b3714b0ef8f3 100644

drivers/media/i2c/mt9p031.c static int mt9p031_parse_properties(struct mt9p031 *mt9p031, struct device *dev) · line 1071

Quoted 6 lines
drivers/media/i2c/mt9p031.c +2 -1
@@ -1067,15 +1067,14 @@ static int mt9p031_parse_properties(struct mt9p031 *mt9p031, struct device *dev)
 	struct v4l2_fwnode_endpoint endpoint = {
 		.bus_type = V4L2_MBUS_PARALLEL
 	};
-	struct fwnode_handle *np;
+	struct fwnode_handle *np __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);

Can you call fwnode_graph_get_next_endpoint() where it used to be called, also declaring np there?

Quoted 11 lines
drivers/media/i2c/mt9p031.c +0 -2
 	int ret;
 
-	np = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
 	if (!np)
 		return dev_err_probe(dev, -EINVAL, "endpoint node not found\n");
 
 	ret = v4l2_fwnode_endpoint_parse(np, &endpoint);
-	fwnode_handle_put(np);
 	if (ret)
 		return dev_err_probe(dev, -EINVAL, "could not parse endpoint\n");
 
Signature · 3 lines
Regards,

Sakari Ailus