mfd: core: fix UAF while using device of node

Message ID 20221116074116.1022139-1-yangyingliang@huawei.com
State New
Headers
Series mfd: core: fix UAF while using device of node |

Commit Message

Yang Yingliang Nov. 16, 2022, 7:41 a.m. UTC
  I got the following UAF report:

  refcount_t: underflow; use-after-free.
  WARNING: CPU: 1 PID: 270 at lib/refcount.c:29 refcount_warn_saturate+0x121/0x180
  ...
  OF: ERROR: memory leak, expected refcount 1 instead of -1073741824,
  of_node_get()/of_node_put() unbalanced - destroy cset entry:
  attach overlay node /i2c/pmic@62/powerkey

The of_node of device assigned in mfd_match_of_node_to_dev() need be
get, and it will be put in platform_device_release().

Fixes: 002be8114007 ("mfd: core: Add missing of_node_put for loop iteration")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/mfd/mfd-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Yang Yingliang Dec. 8, 2022, 1:53 p.m. UTC | #1
Hi,

On 2022/11/16 15:41, Yang Yingliang wrote:
> I got the following UAF report:
>
>    refcount_t: underflow; use-after-free.
>    WARNING: CPU: 1 PID: 270 at lib/refcount.c:29 refcount_warn_saturate+0x121/0x180
>    ...
>    OF: ERROR: memory leak, expected refcount 1 instead of -1073741824,
>    of_node_get()/of_node_put() unbalanced - destroy cset entry:
>    attach overlay node /i2c/pmic@62/powerkey
>
> The of_node of device assigned in mfd_match_of_node_to_dev() need be
> get, and it will be put in platform_device_release().
>
> Fixes: 002be8114007 ("mfd: core: Add missing of_node_put for loop iteration")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Is this patch good or do you have any suggestion ?

Thanks,
Yang
> ---
>   drivers/mfd/mfd-core.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c
> index 16d1861e9682..8e57f67719cf 100644
> --- a/drivers/mfd/mfd-core.c
> +++ b/drivers/mfd/mfd-core.c
> @@ -161,7 +161,7 @@ static int mfd_match_of_node_to_dev(struct platform_device *pdev,
>   	of_entry->np = np;
>   	list_add_tail(&of_entry->list, &mfd_of_node_list);
>   
> -	pdev->dev.of_node = np;
> +	pdev->dev.of_node = of_node_get(np);
>   	pdev->dev.fwnode = &np->fwnode;
>   #endif
>   	return 0;
  
Lee Jones Dec. 8, 2022, 3:06 p.m. UTC | #2
On Thu, 08 Dec 2022, Yang Yingliang wrote:

> Hi,
> 
> On 2022/11/16 15:41, Yang Yingliang wrote:
> > I got the following UAF report:
> > 
> >    refcount_t: underflow; use-after-free.
> >    WARNING: CPU: 1 PID: 270 at lib/refcount.c:29 refcount_warn_saturate+0x121/0x180
> >    ...
> >    OF: ERROR: memory leak, expected refcount 1 instead of -1073741824,
> >    of_node_get()/of_node_put() unbalanced - destroy cset entry:
> >    attach overlay node /i2c/pmic@62/powerkey
> > 
> > The of_node of device assigned in mfd_match_of_node_to_dev() need be
> > get, and it will be put in platform_device_release().
> > 
> > Fixes: 002be8114007 ("mfd: core: Add missing of_node_put for loop iteration")
> > Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> Is this patch good or do you have any suggestion ?

Looks okay, but I'm not applying it this late in the cycle.

Please wait until the next merge-window closes.
  

Patch

diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c
index 16d1861e9682..8e57f67719cf 100644
--- a/drivers/mfd/mfd-core.c
+++ b/drivers/mfd/mfd-core.c
@@ -161,7 +161,7 @@  static int mfd_match_of_node_to_dev(struct platform_device *pdev,
 	of_entry->np = np;
 	list_add_tail(&of_entry->list, &mfd_of_node_list);
 
-	pdev->dev.of_node = np;
+	pdev->dev.of_node = of_node_get(np);
 	pdev->dev.fwnode = &np->fwnode;
 #endif
 	return 0;