i2c: sun6i-p2wi: Fix device node reference leak in p2wi_probe
Subsystems (2)
PATCH v3 1/1 i2c: sun6i-p2wi: Fix device node reference leak in p2wi_probe
In p2wi_probe(), the device node reference obtained via of_get_next_available_child() is stored in childnp. This reference is never released, causing a device node reference leak.
Fix this by calling of_node_put(childnp) on both the error and success paths.
---
v3:
- Revert back to manual of_node_put() because the function uses goto-based error handling, and mixing the two styles is discouraged (reported by Sashiko-bot).
v2:
- Use __free(device_node) and include <linux/cleanup.h> to automate the device node reference cleanup instead of manually calling of_node_put() on error and success paths (suggested by Chen-Yu Tsai).
drivers/i2c/busses/i2c-sun6i-p2wi.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-sun6i-p2wi.c +6 -2
@@ -184,7 +184,6 @@ static int p2wi_probe(struct platform_device *pdev) {struct device *dev = &pdev->dev;struct device_node *np = dev->of_node; -struct device_node *childnp;unsigned long parent_clk_freq;u32 clk_freq =I2C_MAX_STANDARD_MODE_FREQ ;struct p2wi *p2wi;
@@ -217,14 +216,19 @@ static int p2wi_probe(struct platform_device *pdev)* In this case the target_addr is set to -1 and won't be checked when * launching a P2WI transfer. */ +struct device_node *childnp; + childnp =of_get_next_available_child (np,NULL );if (childnp) { ret =of_property_read_u32 (childnp,"reg" , &target_addr); -if (ret) +if (ret) { +of_node_put (childnp);return dev_err_probe (dev, -EINVAL ,"invalid target address on node %pOF\n" , childnp); + } p2wi->target_addr = target_addr; +of_node_put (childnp); } p2wi->regs =devm_platform_ioremap_resource (pdev,0 );
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] Calling of_node_put(childnp) before passing childnp to dev_err_probe() causes a use-after-free because the %pOF format specifier dereferences the freed node.
Pre-existing issues:
- [High] Hardware reset is asserted before the I2C adapter is deleted in p2wi_remove(), risking hangs and incomplete transfers.
- [High] The I2C transfer function p2wi_smbus_xfer() uses wait_for_completion() without a timeout, which can permanently hang the kernel task if the hardware fails. --
commit c8d2fd420d274dc476834daabf9f1fb97be750d0
Author: Uday Khare <[email protected]>
i2c: sun6i-p2wi: Fix device node reference leak in p2wi_probe
This commit addresses a device node reference leak in p2wi_probe() by calling of_node_put() on the child node reference obtained via of_get_next_available_child(). The fix is applied to both the success and error paths.
Quoted 4 lines
drivers/i2c/busses/i2c-sun6i-p2wi.c +0
-0
[ ... ]
Quoted 4 lines
drivers/i2c/busses/i2c-sun6i-p2wi.c +0
-0@@ -217,14 +216,19 @@ static int p2wi_probe(struct platform_device *pdev)* In this case the target_addr is set to -1 and won't be checked when * launching a P2WI transfer. */
drivers/i2c/busses/i2c-sun6i-p2wi.c static int p2wi_probe(struct platform_device *pdev) · line 227
Quoted 10 lines
drivers/i2c/busses/i2c-sun6i-p2wi.c +4
-1+struct device_node *childnp; + childnp =of_get_next_available_child (np,NULL );if (childnp) { ret =of_property_read_u32 (childnp,"reg" , &target_addr); -if (ret) +if (ret) { +of_node_put (childnp);return dev_err_probe (dev, -EINVAL ,"invalid target address on node %pOF\n" , childnp);
[Severity: High]
Does calling of_node_put() before dev_err_probe() cause a use-after-free?
The %pOF format specifier in dev_err_probe() will dereference childnp to read its full name. Since of_node_put() decrements the reference count and can potentially free the node, accessing it immediately afterwards might lead to a use-after-free.
Could the node be released after logging the error instead?
drivers/i2c/busses/i2c-sun6i-p2wi.c static int p2wi_probe(struct platform_device *pdev) · line 232
Quoted 5 lines
drivers/i2c/busses/i2c-sun6i-p2wi.c +2
-0+ } p2wi->target_addr = target_addr; +of_node_put (childnp); }
While reviewing the above, I also noticed a couple of pre-existing issues in the same file:
drivers/i2c/busses/i2c-sun6i-p2wi.c:p2wi_remove() {
...
reset_control_assert(p2wi->rstc);
i2c_del_adapter(&p2wi->adapter);
}[Severity: High]
This is a pre-existing issue, but could asserting the hardware reset before deleting the I2C adapter risk hanging the system?
If reset_control_assert() disables the hardware while i2c_del_adapter() is still waiting for ongoing transfers to finish or accepting new ones, any in-flight transfers might interact with a disabled controller. This could lead to the driver waiting indefinitely for an interrupt.
Should i2c_del_adapter() be called before asserting the hardware reset?
drivers/i2c/busses/i2c-sun6i-p2wi.c:p2wi_smbus_xfer() {
...
writel(P2WI_CTRL_START_TRANS | P2WI_CTRL_GLOBAL_INT_ENB,
p2wi->regs + P2WI_CTRL);
wait_for_completion(&p2wi->complete);
...
}[Severity: High]
This is a pre-existing issue, but can this wait_for_completion() hang the kernel task permanently?
If the hardware fails to generate an interrupt due to a bus lockup, a missed edge, or reset, the calling thread will block indefinitely in an uninterruptible sleep state because there is no timeout mechanism.
Would it be safer to use wait_for_completion_timeout() and handle the timeout condition by reporting an error and cleaning up?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1