2022-09-26 07:40:08

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH v3] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv()

From: Xiaoke Wang <[email protected]>

In _r8712_init_xmit_priv(), if `pxmitbuf->pallocated_buf` is allocated in
failure or `r8712_xmit_resource_alloc(padapter, pxmitbuf)` fails, then it
just returns -ENOMEM but not releases the previous allocated resources,
which can lead to various memory leaks.

To fix them, this patch unifies the error handler in the above function.
Note that if `r8712_xmit_resource_alloc(padapter, pxmitbuf)` fails, we
first call `kfree(pxmitbuf->pallocated_buf);` before goto the error
section so that we do not need to concern the failed item.

As there is no proper device to test with, no runtime testing was
performed.

Signed-off-by: Xiaoke Wang <[email protected]>
---
ChangeLog:
v1->v2 update the description.
v2->v3 update the description.
drivers/staging/rtl8712/rtl871x_xmit.c | 27 ++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/rtl8712/rtl871x_xmit.c b/drivers/staging/rtl8712/rtl871x_xmit.c
index 090345b..dcf3f76 100644
--- a/drivers/staging/rtl8712/rtl871x_xmit.c
+++ b/drivers/staging/rtl8712/rtl871x_xmit.c
@@ -117,11 +117,8 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
_init_queue(&pxmitpriv->pending_xmitbuf_queue);
pxmitpriv->pallocated_xmitbuf =
kmalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4, GFP_ATOMIC);
- if (!pxmitpriv->pallocated_xmitbuf) {
- kfree(pxmitpriv->pallocated_frame_buf);
- pxmitpriv->pallocated_frame_buf = NULL;
- return -ENOMEM;
- }
+ if (!pxmitpriv->pallocated_xmitbuf)
+ goto free_frame_buf;
pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 -
((addr_t)(pxmitpriv->pallocated_xmitbuf) & 3);
pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
@@ -130,12 +127,14 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
pxmitbuf->pallocated_buf =
kmalloc(MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ, GFP_ATOMIC);
if (!pxmitbuf->pallocated_buf)
- return -ENOMEM;
+ goto free_xmitbuf;
pxmitbuf->pbuf = pxmitbuf->pallocated_buf + XMITBUF_ALIGN_SZ -
((addr_t) (pxmitbuf->pallocated_buf) &
(XMITBUF_ALIGN_SZ - 1));
- if (r8712_xmit_resource_alloc(padapter, pxmitbuf))
- return -ENOMEM;
+ if (r8712_xmit_resource_alloc(padapter, pxmitbuf)) {
+ kfree(pxmitbuf->pallocated_buf);
+ goto free_xmitbuf;
+ }
list_add_tail(&pxmitbuf->list,
&(pxmitpriv->free_xmitbuf_queue.queue));
pxmitbuf++;
@@ -146,6 +145,18 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
tasklet_setup(&pxmitpriv->xmit_tasklet, r8712_xmit_bh);
return 0;
+
+free_xmitbuf:
+ pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
+ while (i--) {
+ r8712_xmit_resource_free(padapter, pxmitbuf);
+ kfree(pxmitbuf->pallocated_buf);
+ pxmitbuf++;
+ }
+ kfree(pxmitpriv->pallocated_xmitbuf);
+free_frame_buf:
+ kfree(pxmitpriv->pallocated_frame_buf);
+ return -ENOMEM;
}

void _free_xmit_priv(struct xmit_priv *pxmitpriv)
--


2022-09-27 08:15:55

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v3] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv()

On Mon, Sep 26, 2022 at 03:06:05PM +0800, [email protected] wrote:
> diff --git a/drivers/staging/rtl8712/rtl871x_xmit.c b/drivers/staging/rtl8712/rtl871x_xmit.c
> index 090345b..dcf3f76 100644
> --- a/drivers/staging/rtl8712/rtl871x_xmit.c
> +++ b/drivers/staging/rtl8712/rtl871x_xmit.c
> @@ -117,11 +117,8 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
> _init_queue(&pxmitpriv->pending_xmitbuf_queue);
> pxmitpriv->pallocated_xmitbuf =
> kmalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4, GFP_ATOMIC);
> - if (!pxmitpriv->pallocated_xmitbuf) {
> - kfree(pxmitpriv->pallocated_frame_buf);
> - pxmitpriv->pallocated_frame_buf = NULL;
> - return -ENOMEM;
> - }
> + if (!pxmitpriv->pallocated_xmitbuf)
> + goto free_frame_buf;
> pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 -
> ((addr_t)(pxmitpriv->pallocated_xmitbuf) & 3);
> pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
> @@ -130,12 +127,14 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
> pxmitbuf->pallocated_buf =
> kmalloc(MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ, GFP_ATOMIC);
> if (!pxmitbuf->pallocated_buf)
> - return -ENOMEM;
> + goto free_xmitbuf;
> pxmitbuf->pbuf = pxmitbuf->pallocated_buf + XMITBUF_ALIGN_SZ -
> ((addr_t) (pxmitbuf->pallocated_buf) &
> (XMITBUF_ALIGN_SZ - 1));
> - if (r8712_xmit_resource_alloc(padapter, pxmitbuf))
> - return -ENOMEM;
> + if (r8712_xmit_resource_alloc(padapter, pxmitbuf)) {
> + kfree(pxmitbuf->pallocated_buf);
> + goto free_xmitbuf;
> + }
> list_add_tail(&pxmitbuf->list,
> &(pxmitpriv->free_xmitbuf_queue.queue));


pxmitbuf points to somewhere in the middle of pxmitpriv->pallocated_xmitbuf.
We add it to the list here.

> pxmitbuf++;
> @@ -146,6 +145,18 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
> init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
> tasklet_setup(&pxmitpriv->xmit_tasklet, r8712_xmit_bh);
> return 0;
> +
> +free_xmitbuf:
> + pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
> + while (i--) {
> + r8712_xmit_resource_free(padapter, pxmitbuf);
> + kfree(pxmitbuf->pallocated_buf);
> + pxmitbuf++;
> + }
> + kfree(pxmitpriv->pallocated_xmitbuf);

But then we free pxmitpriv->pallocated_xmitbuf here but it the memory
is still on the list. So that means there will be a use after free
eventually.

regards,
dan carpenter

2022-09-27 13:54:24

by Xiaoke Wang

[permalink] [raw]
Subject: Re: [PATCH v3] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv()

On Tue, Sep 27, 2022 03:57 PM, [email protected] wrote:
&gt; On Mon, Sep 26, 2022 at 03:06:05PM +0800, [email protected] wrote:
&gt;&gt; diff --git a/drivers/staging/rtl8712/rtl871x_xmit.c b/drivers/staging/rtl8712/rtl871x_xmit.c
&gt;&gt; index 090345b..dcf3f76 100644
&gt;&gt; --- a/drivers/staging/rtl8712/rtl871x_xmit.c
&gt;&gt; +++ b/drivers/staging/rtl8712/rtl871x_xmit.c
&gt;&gt; @@ -117,11 +117,8 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
&gt;&gt; _init_queue(&amp;pxmitpriv-&gt;pending_xmitbuf_queue);
&gt;&gt; pxmitpriv-&gt;pallocated_xmitbuf =
&gt;&gt; kmalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4, GFP_ATOMIC);
&gt;&gt; - if (!pxmitpriv-&gt;pallocated_xmitbuf) {
&gt;&gt; -kfree(pxmitpriv-&gt;pallocated_frame_buf);
&gt;&gt; -pxmitpriv-&gt;pallocated_frame_buf = NULL;
&gt;&gt; -return -ENOMEM;
&gt;&gt; - }
&gt;&gt; + if (!pxmitpriv-&gt;pallocated_xmitbuf)
&gt;&gt; +goto free_frame_buf;

Note here: may have the same concern.

&gt;&gt; pxmitpriv-&gt;pxmitbuf = pxmitpriv-&gt;pallocated_xmitbuf + 4 -
&gt;&gt; ((addr_t)(pxmitpriv-&gt;pallocated_xmitbuf) &amp; 3);
&gt;&gt; pxmitbuf = (struct xmit_buf *)pxmitpriv-&gt;pxmitbuf;
&gt;&gt; @@ -130,12 +127,14 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
&gt;&gt; pxmitbuf-&gt;pallocated_buf =
&gt;&gt; kmalloc(MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ, GFP_ATOMIC);
&gt;&gt; if (!pxmitbuf-&gt;pallocated_buf)
&gt;&gt; - return -ENOMEM;
&gt;&gt; + goto free_xmitbuf;
&gt;&gt; pxmitbuf-&gt;pbuf = pxmitbuf-&gt;pallocated_buf + XMITBUF_ALIGN_SZ -
&gt;&gt; ((addr_t) (pxmitbuf-&gt;pallocated_buf) &amp;
&gt;&gt; (XMITBUF_ALIGN_SZ - 1));
&gt;&gt; -if (r8712_xmit_resource_alloc(padapter, pxmitbuf))
&gt;&gt; - return -ENOMEM;
&gt;&gt; +if (r8712_xmit_resource_alloc(padapter, pxmitbuf)) {
&gt;&gt; + kfree(pxmitbuf-&gt;pallocated_buf);
&gt;&gt; + goto free_xmitbuf;
&gt;&gt; +}
&gt;&gt; list_add_tail(&amp;pxmitbuf-&gt;list,
&gt;&gt; &amp;(pxmitpriv-&gt;free_xmitbuf_queue.queue));
&gt;
&gt;
&gt; pxmitbuf points to somewhere in the middle of pxmitpriv-&gt;pallocated_xmitbuf.
&gt; We add it to the list here.
&gt;
&gt;&gt; pxmitbuf++;
&gt;&gt; @@ -146,6 +145,18 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
&gt;&gt; init_hwxmits(pxmitpriv-&gt;hwxmits, pxmitpriv-&gt;hwxmit_entry);
&gt;&gt; tasklet_setup(&amp;pxmitpriv-&gt;xmit_tasklet, r8712_xmit_bh);
&gt;&gt; return 0;
&gt;&gt; +
&gt;&gt; +free_xmitbuf:
&gt;&gt; + pxmitbuf = (struct xmit_buf *)pxmitpriv-&gt;pxmitbuf;
&gt;&gt; + while (i--) {
&gt;&gt; +r8712_xmit_resource_free(padapter, pxmitbuf);
&gt;&gt; +kfree(pxmitbuf-&gt;pallocated_buf);
&gt;&gt; +pxmitbuf++;
&gt;&gt; + }
&gt;&gt; + kfree(pxmitpriv-&gt;pallocated_xmitbuf);
&gt;
&gt; But then we free pxmitpriv-&gt;pallocated_xmitbuf here but it the memory
&gt; is still on the list. So that means there will be a use after free
&gt; eventually.

Yes, the memory address is still on the list, and at the above position of
`Note`, the address of `pxmitpriv-&gt;pallocated_frame_buf` is also left on a
list named `pxmitpriv-&gt;free_xmit_queue`.
However, these lists are in `pxmitpriv` and this function is for
initializing `pxmitpriv`. When this function fails, the probe function of
this driver will finally fail. So I guess the list in `pxmitpriv` will not
be accessed.

Please let me know if you still hold such concerns, I am glad to find a
time (in 2 weeks I guess) to add `list_del_init()` on the error paths
to clear all the improper pointing records.

Regards,
Xiaoke Wang

2022-10-04 13:47:23

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v3] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv()

On Tue, Sep 27, 2022 at 08:55:08PM +0800, Xiaoke Wang wrote:
>
> Yes, the memory address is still on the list, and at the above position of
> `Note`, the address of `pxmitpriv-&gt;pallocated_frame_buf` is also left on a
> list named `pxmitpriv-&gt;free_xmit_queue`.
> However, these lists are in `pxmitpriv` and this function is for
> initializing `pxmitpriv`. When this function fails, the probe function of
> this driver will finally fail. So I guess the list in `pxmitpriv` will not
> be accessed.

Sorry for the delayed response. I think you are right. This patch is
fine then.

Reviewed-by: Dan Carpenter <[email protected]>

regards,
dan carpenter