Skip to contentSkip to conversation

media: adv748x: Fix endpoint reference leaks on probe failure

PATCH Superseded Ruoyu Wang 2 participants 2 messages Aug 14 – Sep 8

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 media: adv748x: Fix endpoint reference leaks on probe failure

adv748x_parse_dt() takes an extra reference for every endpoint saved in state->endpoints. If parsing later fails, adv748x_probe() skips adv748x_dt_cleanup(), so the saved references remain held. A CSI-2 lane parsing error also returns from for_each_endpoint_of_node() without dropping the iterator's reference.

Drop the iterator reference before returning a lane parsing error and route all parse failures through the endpoint cleanup path. This balances both reference classes while retaining the endpoints for a successful probe.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 6a18865da8e3 ("media: i2c: adv748x: store number of CSI-2 lanes described in device tree")
Fixes eccf442ce156 ("media: i2c: adv748x: Support probing a single output")
---
 drivers/media/i2c/adv748x/adv748x-core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/media/i2c/adv748x/adv748x-core.c +4 -3
index 3eb6d5e8f0826..010a4c55b4002 100644
@@ -689,8 +689,10 @@ static int adv748x_parse_dt(struct adv748x_state *state)
 
 		/* Store number of CSI-2 lanes used for TXA and TXB. */
 		ret = adv748x_parse_csi2_lanes(state, ep.port, ep_np);
-		if (ret)
+		if (ret) {
+			of_node_put(ep_np);
 			return ret;
+		}
 	}
 
 	return in_found && out_found ? 0 : -ENODEV;
@@ -739,7 +741,7 @@ static int adv748x_probe(struct i2c_client *client)
 	ret = adv748x_parse_dt(state);
 	if (ret) {
 		adv_err(state, "Failed to parse device tree");
-		goto err_free_mutex;
+		goto err_cleanup_dt;
 	}
 
 	/* Configure IO Regmap region */
@@ -809,7 +811,6 @@ static int adv748x_probe(struct i2c_client *client)
 	adv748x_unregister_clients(state);
 err_cleanup_dt:
 	adv748x_dt_cleanup(state);
-err_free_mutex:
 	mutex_destroy(&state->mutex);
 
 	return ret;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 26 lines · Ruoyu Wang

adv748x_parse_dt() takes an extra reference for every endpoint saved in state->endpoints. If parsing later fails, adv748x_probe() skips adv748x_dt_cleanup(), so the saved references remain held. A CSI-2 lane parsing error also returns from for_each_endpoint_of_node() without dropping the iterator's reference.

Drop the iterator reference before returning a lane parsing error and route all parse failures through the endpoint cleanup path. This balances both reference classes while retaining the endpoints for a successful probe.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 6a18865da8e3 ("media: i2c: adv748x: store number of CSI-2 lanes described in device tree")
Fixes eccf442ce156 ("media: i2c: adv748x: Support probing a single output")
---
 drivers/media/i2c/adv748x/adv748x-core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/media/i2c/adv748x/adv748x-core.c +0 -0
index 3eb6d5e8f0826..010a4c55b4002 100644

drivers/media/i2c/adv748x/adv748x-core.c static int adv748x_parse_dt(struct adv748x_state *state) · line 693

Quoted 6 lines
drivers/media/i2c/adv748x/adv748x-core.c +2 -1
@@ -689,8 +689,10 @@ static int adv748x_parse_dt(struct adv748x_state *state)
 
 		/* Store number of CSI-2 lanes used for TXA and TXB. */
 		ret = adv748x_parse_csi2_lanes(state, ep.port, ep_np);
-		if (ret)
+		if (ret) {
+			of_node_put(ep_np);

Shouldn't this set state->endpoints[ep.port] to NULL as well? Otherwise it will be put again in adv748x_dt_cleanup(). Or better, wait with setting state->endpoints[ep.port] until after this check.

Regards,

Hans

Quoted 22 lines
drivers/media/i2c/adv748x/adv748x-core.c +2 -2
 			return ret;
+		}
 	}
 
 	return in_found && out_found ? 0 : -ENODEV;
@@ -739,7 +741,7 @@ static int adv748x_probe(struct i2c_client *client)
 	ret = adv748x_parse_dt(state);
 	if (ret) {
 		adv_err(state, "Failed to parse device tree");
-		goto err_free_mutex;
+		goto err_cleanup_dt;
 	}
 
 	/* Configure IO Regmap region */
@@ -809,7 +811,6 @@ static int adv748x_probe(struct i2c_client *client)
 	adv748x_unregister_clients(state);
 err_cleanup_dt:
 	adv748x_dt_cleanup(state);
-err_free_mutex:
 	mutex_destroy(&state->mutex);
 
 	return ret;