diff mbox series

net: mdiobus: fix an OF node reference leak

Message ID 20241214081546.183159-1-joe@pf.is.s.u-tokyo.ac.jp (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: mdiobus: fix an OF node reference leak | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers fail 1 blamed authors not CCed: ioana.ciornei@nxp.com; 1 maintainers not CCed: ioana.ciornei@nxp.com
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 7 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-12-15--09-00 (tests: 795)

Commit Message

Joe Hattori Dec. 14, 2024, 8:15 a.m. UTC
fwnode_find_mii_timestamper() calls of_parse_phandle_with_fixed_args()
but does not decrement the refcount of the obtained OF node. Add an
of_node_put() call before returning from the function.

This bug was detected by an experimental static analysis tool that I am
developing.

Fixes: bc1bee3b87ee ("net: mdiobus: Introduce fwnode_mdiobus_register_phy()")
Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
---
 drivers/net/mdio/fwnode_mdio.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Andrew Lunn Dec. 15, 2024, 11:03 a.m. UTC | #1
On Sat, Dec 14, 2024 at 05:15:46PM +0900, Joe Hattori wrote:
> fwnode_find_mii_timestamper() calls of_parse_phandle_with_fixed_args()
> but does not decrement the refcount of the obtained OF node. Add an
> of_node_put() call before returning from the function.
> 
> This bug was detected by an experimental static analysis tool that I am
> developing.
> 
> Fixes: bc1bee3b87ee ("net: mdiobus: Introduce fwnode_mdiobus_register_phy()")
> Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
> ---
>  drivers/net/mdio/fwnode_mdio.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
> index b156493d7084..83c8bd333117 100644
> --- a/drivers/net/mdio/fwnode_mdio.c
> +++ b/drivers/net/mdio/fwnode_mdio.c
> @@ -56,6 +56,7 @@ fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
>  	if (arg.args_count != 1)
>  		return ERR_PTR(-EINVAL);
>  
> +	of_node_put(arg.np);
>  	return register_mii_timestamper(arg.np, arg.args[0]);

This looks wrong to me. If you do a put on an object, it can
disappear, because it is not being used. You then pass it to
register_mii_timestamper() and it gets to use something which no
longer exists.

Please think about what get/put are used for, and what that means for
ordering.

Maybe you can extend your tool to look for potential use after free
bugs.

    Andrew

---
pw-bot: cr
Joe Hattori Dec. 15, 2024, 11:27 a.m. UTC | #2
Hi Andrew,

Thank you for your review.

On 12/15/24 20:03, Andrew Lunn wrote:
> On Sat, Dec 14, 2024 at 05:15:46PM +0900, Joe Hattori wrote:
>> fwnode_find_mii_timestamper() calls of_parse_phandle_with_fixed_args()
>> but does not decrement the refcount of the obtained OF node. Add an
>> of_node_put() call before returning from the function.
>>
>> This bug was detected by an experimental static analysis tool that I am
>> developing.
>>
>> Fixes: bc1bee3b87ee ("net: mdiobus: Introduce fwnode_mdiobus_register_phy()")
>> Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
>> ---
>>   drivers/net/mdio/fwnode_mdio.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
>> index b156493d7084..83c8bd333117 100644
>> --- a/drivers/net/mdio/fwnode_mdio.c
>> +++ b/drivers/net/mdio/fwnode_mdio.c
>> @@ -56,6 +56,7 @@ fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
>>   	if (arg.args_count != 1)
>>   		return ERR_PTR(-EINVAL);
>>   
>> +	of_node_put(arg.np);
>>   	return register_mii_timestamper(arg.np, arg.args[0]);
> 
> This looks wrong to me. If you do a put on an object, it can
> disappear, because it is not being used. You then pass it to
> register_mii_timestamper() and it gets to use something which no
> longer exists.

Totally. Should have realized that. It is fixed in the v2 patch as I 
moved the of_node_put() call to the very end of the function.

> 
> Please think about what get/put are used for, and what that means for
> ordering.
> 
> Maybe you can extend your tool to look for potential use after free
> bugs.
> 
>      Andrew
> 
> ---
> pw-bot: cr

Best,
Joe
diff mbox series

Patch

diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index b156493d7084..83c8bd333117 100644
--- a/drivers/net/mdio/fwnode_mdio.c
+++ b/drivers/net/mdio/fwnode_mdio.c
@@ -56,6 +56,7 @@  fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
 	if (arg.args_count != 1)
 		return ERR_PTR(-EINVAL);
 
+	of_node_put(arg.np);
 	return register_mii_timestamper(arg.np, arg.args[0]);
 }