linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register()
@ 2022-09-07  7:37 Jianglei Nie
  2022-09-07 20:10 ` Jeff Johnson
  2022-09-10  6:27 ` Kalle Valo
  0 siblings, 2 replies; 5+ messages in thread
From: Jianglei Nie @ 2022-09-07  7:37 UTC (permalink / raw)
  To: kvalo, davem, edumazet, kuba, pabeni
  Cc: ath11k, linux-wireless, netdev, linux-kernel, Jianglei Nie

mhi_alloc_controller() allocates a memory space for mhi_ctrl. When gets
some error, mhi_ctrl should be freed with mhi_free_controller(). But
when ath11k_mhi_read_addr_from_dt() fails, the function returns without
calling mhi_free_controller(), which will lead to a memory leak.

We can fix it by calling mhi_free_controller() when
ath11k_mhi_read_addr_from_dt() fails.

Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
---
 drivers/net/wireless/ath/ath11k/mhi.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/net/wireless/ath/ath11k/mhi.c b/drivers/net/wireless/ath/ath11k/mhi.c
index c44df17719f6..86995e8dc913 100644
--- a/drivers/net/wireless/ath/ath11k/mhi.c
+++ b/drivers/net/wireless/ath/ath11k/mhi.c
@@ -402,8 +402,7 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
 	ret = ath11k_mhi_get_msi(ab_pci);
 	if (ret) {
 		ath11k_err(ab, "failed to get msi for mhi\n");
-		mhi_free_controller(mhi_ctrl);
-		return ret;
+		goto free_controller;
 	}
 
 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
@@ -412,7 +411,7 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
 	if (test_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags)) {
 		ret = ath11k_mhi_read_addr_from_dt(mhi_ctrl);
 		if (ret < 0)
-			return ret;
+			goto free_controller;
 	} else {
 		mhi_ctrl->iova_start = 0;
 		mhi_ctrl->iova_stop = 0xFFFFFFFF;
@@ -440,18 +439,22 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
 	default:
 		ath11k_err(ab, "failed assign mhi_config for unknown hw rev %d\n",
 			   ab->hw_rev);
-		mhi_free_controller(mhi_ctrl);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto free_controller;
 	}
 
 	ret = mhi_register_controller(mhi_ctrl, ath11k_mhi_config);
 	if (ret) {
 		ath11k_err(ab, "failed to register to mhi bus, err = %d\n", ret);
-		mhi_free_controller(mhi_ctrl);
-		return ret;
+		goto free_controller;
 	}
 
 	return 0;
+
+free_controller:
+	mhi_free_controller(mhi_ctrl);
+	ab_pci->mhi_ctrl = NULL;
+	return ret;
 }
 
 void ath11k_mhi_unregister(struct ath11k_pci *ab_pci)
-- 
2.25.1


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

* Re: [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register()
  2022-09-07  7:37 [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register() Jianglei Nie
@ 2022-09-07 20:10 ` Jeff Johnson
  2022-09-10  6:27 ` Kalle Valo
  1 sibling, 0 replies; 5+ messages in thread
From: Jeff Johnson @ 2022-09-07 20:10 UTC (permalink / raw)
  To: Jianglei Nie, kvalo, davem, edumazet, kuba, pabeni
  Cc: ath11k, linux-wireless, netdev, linux-kernel

On 9/7/2022 12:37 AM, Jianglei Nie wrote:
> mhi_alloc_controller() allocates a memory space for mhi_ctrl. When gets
> some error, mhi_ctrl should be freed with mhi_free_controller(). But
> when ath11k_mhi_read_addr_from_dt() fails, the function returns without
> calling mhi_free_controller(), which will lead to a memory leak.
> 
> We can fix it by calling mhi_free_controller() when
> ath11k_mhi_read_addr_from_dt() fails.
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>

I believe this should have been annotated as -v2 to the following:
<https://lore.kernel.org/ath11k/20220526100227.483609-1-niejianglei2021@163.com/>

Please properly annotate follow-up patches.

Also please add wifi: to the beginning of the subject prefix


> ---
>   drivers/net/wireless/ath/ath11k/mhi.c | 17 ++++++++++-------
>   1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/net/wireless/ath/ath11k/mhi.c b/drivers/net/wireless/ath/ath11k/mhi.c
> index c44df17719f6..86995e8dc913 100644
> --- a/drivers/net/wireless/ath/ath11k/mhi.c
> +++ b/drivers/net/wireless/ath/ath11k/mhi.c
> @@ -402,8 +402,7 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
>   	ret = ath11k_mhi_get_msi(ab_pci);
>   	if (ret) {
>   		ath11k_err(ab, "failed to get msi for mhi\n");
> -		mhi_free_controller(mhi_ctrl);
> -		return ret;
> +		goto free_controller;
>   	}
>   
>   	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
> @@ -412,7 +411,7 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
>   	if (test_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags)) {
>   		ret = ath11k_mhi_read_addr_from_dt(mhi_ctrl);
>   		if (ret < 0)
> -			return ret;
> +			goto free_controller;
>   	} else {
>   		mhi_ctrl->iova_start = 0;
>   		mhi_ctrl->iova_stop = 0xFFFFFFFF;
> @@ -440,18 +439,22 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
>   	default:
>   		ath11k_err(ab, "failed assign mhi_config for unknown hw rev %d\n",
>   			   ab->hw_rev);
> -		mhi_free_controller(mhi_ctrl);
> -		return -EINVAL;
> +		ret = -EINVAL;
> +		goto free_controller;
>   	}
>   
>   	ret = mhi_register_controller(mhi_ctrl, ath11k_mhi_config);
>   	if (ret) {
>   		ath11k_err(ab, "failed to register to mhi bus, err = %d\n", ret);
> -		mhi_free_controller(mhi_ctrl);
> -		return ret;
> +		goto free_controller;
>   	}
>   
>   	return 0;
> +
> +free_controller:
> +	mhi_free_controller(mhi_ctrl);
> +	ab_pci->mhi_ctrl = NULL;
> +	return ret;
>   }
>   
>   void ath11k_mhi_unregister(struct ath11k_pci *ab_pci)

Reviewed-by: Jeff Johnson <quic_jjohnson@quicinc.com>


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

* Re: [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register()
  2022-09-07  7:37 [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register() Jianglei Nie
  2022-09-07 20:10 ` Jeff Johnson
@ 2022-09-10  6:27 ` Kalle Valo
  1 sibling, 0 replies; 5+ messages in thread
From: Kalle Valo @ 2022-09-10  6:27 UTC (permalink / raw)
  To: Jianglei Nie
  Cc: davem, edumazet, kuba, pabeni, ath11k, linux-wireless, netdev,
	linux-kernel, Jianglei Nie

Jianglei Nie <niejianglei2021@163.com> wrote:

> mhi_alloc_controller() allocates a memory space for mhi_ctrl. When gets
> some error, mhi_ctrl should be freed with mhi_free_controller(). But
> when ath11k_mhi_read_addr_from_dt() fails, the function returns without
> calling mhi_free_controller(), which will lead to a memory leak.
> 
> We can fix it by calling mhi_free_controller() when
> ath11k_mhi_read_addr_from_dt() fails.
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> Reviewed-by: Jeff Johnson <quic_jjohnson@quicinc.com>
> Signed-off-by: Kalle Valo <quic_kvalo@quicinc.com>

Patch applied to ath-next branch of ath.git, thanks.

43e7c3505ec7 wifi: ath11k: mhi: fix potential memory leak in ath11k_mhi_register()

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/20220907073704.58806-1-niejianglei2021@163.com/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches


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

* Re: [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register()
  2022-05-26 10:02 Jianglei Nie
@ 2022-05-26 15:45 ` Jeff Johnson
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff Johnson @ 2022-05-26 15:45 UTC (permalink / raw)
  To: Jianglei Nie, kvalo, davem, edumazet, kuba, pabeni
  Cc: ath11k, linux-wireless, netdev, linux-kernel

On 5/26/2022 3:02 AM, Jianglei Nie wrote:
> mhi_alloc_controller() allocates a memory space for mhi_ctrl. When some
> errors occur, mhi_ctrl should be freed by mhi_free_controller(). But
> when ath11k_mhi_read_addr_from_dt() fails, the function returns without
> calling mhi_free_controller(), which will lead to a memory leak.
> 
> We can fix it by calling mhi_free_controller() when
> ath11k_mhi_read_addr_from_dt() fails.
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>

Reviewed-by: Jeff Johnson <quic_jjohnson@quicinc.com>

> ---
>   drivers/net/wireless/ath/ath11k/mhi.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/wireless/ath/ath11k/mhi.c b/drivers/net/wireless/ath/ath11k/mhi.c
> index fc3524e83e52..3318c7c2b32b 100644
> --- a/drivers/net/wireless/ath/ath11k/mhi.c
> +++ b/drivers/net/wireless/ath/ath11k/mhi.c
> @@ -376,8 +376,10 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
>   
>   	if (test_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags)) {
>   		ret = ath11k_mhi_read_addr_from_dt(mhi_ctrl);
> -		if (ret < 0)
> +		if (ret < 0) {
> +			mhi_free_controller(mhi_ctrl);
>   			return ret;
> +		}
>   	} else {
>   		mhi_ctrl->iova_start = 0;
>   		mhi_ctrl->iova_stop = 0xFFFFFFFF;

This patch looks good to me. However there is an additional issue that 
IMO should be addressed in this function with a separate patch.

Just after mhi_ctrl is allocated the pointer is saved:
	ab_pci->mhi_ctrl = mhi_ctrl;

When there is an error, mhi_ctrl is freed, but ab_pci->mhi_ctrl still 
has a dangling pointer to the freed memory. This has the potential to 
result in a subsequent use-after-free or double-free error.

So IMO we should do one of two things (I haven't looked at the code in 
detail to determine which is better):

1) don't set ab_pci->mhi_ctrl = mhi_ctrl until the end of the function 
after success from mhi_register_controller(). This requires that none of 
the functions called between the current assignment point and the 
proposed assignment point dereference ab_pci->mhi_ctrl

2) set ab_pci->mhi_ctrl = NULL in all of the places where we call 
mhi_free_controller()

2a) have a central error exit label and have all of the error conditions 
goto that label where there is a central code:
	mhi_free_controller(mhi_ctrl);
	ab_pci->mhi_ctrl = NULL;
	return ret;

	

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

* [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register()
@ 2022-05-26 10:02 Jianglei Nie
  2022-05-26 15:45 ` Jeff Johnson
  0 siblings, 1 reply; 5+ messages in thread
From: Jianglei Nie @ 2022-05-26 10:02 UTC (permalink / raw)
  To: kvalo, davem, edumazet, kuba, pabeni
  Cc: ath11k, linux-wireless, netdev, linux-kernel, Jianglei Nie

mhi_alloc_controller() allocates a memory space for mhi_ctrl. When some
errors occur, mhi_ctrl should be freed by mhi_free_controller(). But
when ath11k_mhi_read_addr_from_dt() fails, the function returns without
calling mhi_free_controller(), which will lead to a memory leak.

We can fix it by calling mhi_free_controller() when
ath11k_mhi_read_addr_from_dt() fails.

Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
---
 drivers/net/wireless/ath/ath11k/mhi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/ath/ath11k/mhi.c b/drivers/net/wireless/ath/ath11k/mhi.c
index fc3524e83e52..3318c7c2b32b 100644
--- a/drivers/net/wireless/ath/ath11k/mhi.c
+++ b/drivers/net/wireless/ath/ath11k/mhi.c
@@ -376,8 +376,10 @@ int ath11k_mhi_register(struct ath11k_pci *ab_pci)
 
 	if (test_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags)) {
 		ret = ath11k_mhi_read_addr_from_dt(mhi_ctrl);
-		if (ret < 0)
+		if (ret < 0) {
+			mhi_free_controller(mhi_ctrl);
 			return ret;
+		}
 	} else {
 		mhi_ctrl->iova_start = 0;
 		mhi_ctrl->iova_stop = 0xFFFFFFFF;
-- 
2.25.1


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

end of thread, other threads:[~2022-09-10  6:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07  7:37 [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register() Jianglei Nie
2022-09-07 20:10 ` Jeff Johnson
2022-09-10  6:27 ` Kalle Valo
  -- strict thread matches above, loose matches on Subject: below --
2022-05-26 10:02 Jianglei Nie
2022-05-26 15:45 ` Jeff Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).