From: Jeff Johnson <quic_jjohnson@quicinc.com>
To: Jianglei Nie <niejianglei2021@163.com>, <kvalo@kernel.org>,
<davem@davemloft.net>, <edumazet@google.com>, <kuba@kernel.org>,
<pabeni@redhat.com>
Cc: <ath11k@lists.infradead.org>, <linux-wireless@vger.kernel.org>,
<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] ath11k: mhi: fix potential memory leak in ath11k_mhi_register()
Date: Thu, 26 May 2022 08:45:29 -0700 [thread overview]
Message-ID: <25540c69-c58c-0dc5-29b3-4914a656a06f@quicinc.com> (raw)
In-Reply-To: <20220526100227.483609-1-niejianglei2021@163.com>
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;
next prev parent reply other threads:[~2022-05-26 15:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-26 10:02 Jianglei Nie
2022-05-26 15:45 ` Jeff Johnson [this message]
2022-09-07 7:37 Jianglei Nie
2022-09-07 20:10 ` Jeff Johnson
2022-09-10 6:27 ` Kalle Valo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=25540c69-c58c-0dc5-29b3-4914a656a06f@quicinc.com \
--to=quic_jjohnson@quicinc.com \
--cc=ath11k@lists.infradead.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=kvalo@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=niejianglei2021@163.com \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®