public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup
@ 2024-12-17  4:26 Joe Hattori
  2024-12-17 10:55 ` Krzysztof Kozlowski
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Hattori @ 2024-12-17  4:26 UTC (permalink / raw)
  To: mchehab+huawei, sboyd; +Cc: linux-kernel, Joe Hattori

spmi_controller_probe() increments the refcount of an OF node, but does
not release it. Instead, call of_node_get() in spmi_controller_alloc()
and release it in spmi_ctrl_release() to avoid the reference leak.

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

Fixes: e562cf3aea3e ("spmi: hisi-spmi-controller: move driver from staging")
Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
---
 drivers/spmi/hisi-spmi-controller.c | 1 -
 drivers/spmi/spmi.c                 | 3 ++-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/spmi/hisi-spmi-controller.c b/drivers/spmi/hisi-spmi-controller.c
index 3cafdf22c909..dd21c5d1ca83 100644
--- a/drivers/spmi/hisi-spmi-controller.c
+++ b/drivers/spmi/hisi-spmi-controller.c
@@ -301,7 +301,6 @@ static int spmi_controller_probe(struct platform_device *pdev)
 	spin_lock_init(&spmi_controller->lock);
 
 	ctrl->dev.parent = pdev->dev.parent;
-	ctrl->dev.of_node = of_node_get(pdev->dev.of_node);
 
 	/* Callbacks */
 	ctrl->read_cmd = spmi_read_cmd;
diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
index fb0101da1485..e662a7a78df2 100644
--- a/drivers/spmi/spmi.c
+++ b/drivers/spmi/spmi.c
@@ -36,6 +36,7 @@ static void spmi_ctrl_release(struct device *dev)
 	struct spmi_controller *ctrl = to_spmi_controller(dev);
 
 	ida_free(&ctrl_ida, ctrl->nr);
+	of_node_put(dev->of_node);
 	kfree(ctrl);
 }
 
@@ -458,7 +459,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
 	ctrl->dev.type = &spmi_ctrl_type;
 	ctrl->dev.bus = &spmi_bus_type;
 	ctrl->dev.parent = parent;
-	ctrl->dev.of_node = parent->of_node;
+	ctrl->dev.of_node = of_node_get(parent->of_node);
 	spmi_controller_set_drvdata(ctrl, &ctrl[1]);
 
 	id = ida_alloc(&ctrl_ida, GFP_KERNEL);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup
  2024-12-17  4:26 [PATCH] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup Joe Hattori
@ 2024-12-17 10:55 ` Krzysztof Kozlowski
  2024-12-18  3:42   ` Joe Hattori
  0 siblings, 1 reply; 3+ messages in thread
From: Krzysztof Kozlowski @ 2024-12-17 10:55 UTC (permalink / raw)
  To: Joe Hattori, mchehab+huawei, sboyd; +Cc: linux-kernel

On 17/12/2024 05:26, Joe Hattori wrote:
> spmi_controller_probe() increments the refcount of an OF node, but does
> not release it. Instead, call of_node_get() in spmi_controller_alloc()
> and release it in spmi_ctrl_release() to avoid the reference leak.
> 
> This bug was found by an experimental static analysis tool that I am
> developing.
> 
> Fixes: e562cf3aea3e ("spmi: hisi-spmi-controller: move driver from staging")
> Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
> ---
>  drivers/spmi/hisi-spmi-controller.c | 1 -
>  drivers/spmi/spmi.c                 | 3 ++-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/spmi/hisi-spmi-controller.c b/drivers/spmi/hisi-spmi-controller.c
> index 3cafdf22c909..dd21c5d1ca83 100644
> --- a/drivers/spmi/hisi-spmi-controller.c
> +++ b/drivers/spmi/hisi-spmi-controller.c
> @@ -301,7 +301,6 @@ static int spmi_controller_probe(struct platform_device *pdev)
>  	spin_lock_init(&spmi_controller->lock);
>  
>  	ctrl->dev.parent = pdev->dev.parent;
> -	ctrl->dev.of_node = of_node_get(pdev->dev.of_node);
>  
>  	/* Callbacks */
>  	ctrl->read_cmd = spmi_read_cmd;
> diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
> index fb0101da1485..e662a7a78df2 100644
> --- a/drivers/spmi/spmi.c
> +++ b/drivers/spmi/spmi.c
> @@ -36,6 +36,7 @@ static void spmi_ctrl_release(struct device *dev)
>  	struct spmi_controller *ctrl = to_spmi_controller(dev);
>  
>  	ida_free(&ctrl_ida, ctrl->nr);
> +	of_node_put(dev->of_node);


Aren't other drivers like PMIC arbiter overwriting this with different
value, thus basically you are dropping reference which was not obtained
and causing actual use-after-free?


Best regards,
Krzysztof

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup
  2024-12-17 10:55 ` Krzysztof Kozlowski
@ 2024-12-18  3:42   ` Joe Hattori
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Hattori @ 2024-12-18  3:42 UTC (permalink / raw)
  To: Krzysztof Kozlowski, mchehab+huawei, sboyd; +Cc: linux-kernel

Thank you for your review.

On 12/17/24 19:55, Krzysztof Kozlowski wrote:
> On 17/12/2024 05:26, Joe Hattori wrote:
>> spmi_controller_probe() increments the refcount of an OF node, but does
>> not release it. Instead, call of_node_get() in spmi_controller_alloc()
>> and release it in spmi_ctrl_release() to avoid the reference leak.
>>
>> This bug was found by an experimental static analysis tool that I am
>> developing.
>>
>> Fixes: e562cf3aea3e ("spmi: hisi-spmi-controller: move driver from staging")
>> Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
>> ---
>>   drivers/spmi/hisi-spmi-controller.c | 1 -
>>   drivers/spmi/spmi.c                 | 3 ++-
>>   2 files changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/spmi/hisi-spmi-controller.c b/drivers/spmi/hisi-spmi-controller.c
>> index 3cafdf22c909..dd21c5d1ca83 100644
>> --- a/drivers/spmi/hisi-spmi-controller.c
>> +++ b/drivers/spmi/hisi-spmi-controller.c
>> @@ -301,7 +301,6 @@ static int spmi_controller_probe(struct platform_device *pdev)
>>   	spin_lock_init(&spmi_controller->lock);
>>   
>>   	ctrl->dev.parent = pdev->dev.parent;
>> -	ctrl->dev.of_node = of_node_get(pdev->dev.of_node);
>>   
>>   	/* Callbacks */
>>   	ctrl->read_cmd = spmi_read_cmd;
>> diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
>> index fb0101da1485..e662a7a78df2 100644
>> --- a/drivers/spmi/spmi.c
>> +++ b/drivers/spmi/spmi.c
>> @@ -36,6 +36,7 @@ static void spmi_ctrl_release(struct device *dev)
>>   	struct spmi_controller *ctrl = to_spmi_controller(dev);
>>   
>>   	ida_free(&ctrl_ida, ctrl->nr);
>> +	of_node_put(dev->of_node);
> 
> 
> Aren't other drivers like PMIC arbiter overwriting this with different
> value, thus basically you are dropping reference which was not obtained
> and causing actual use-after-free?

Should have caught this one. It is actually storing the same OF node 
pointer so it doesn't cause UAF, but is very confusing anyway. Addressed 
in the v2 patch.

> 
> 
> Best regards,
> Krzysztof

Best,
Joe

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-18  3:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-17  4:26 [PATCH] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup Joe Hattori
2024-12-17 10:55 ` Krzysztof Kozlowski
2024-12-18  3:42   ` Joe Hattori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox