linux-staging.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: "Xiaoke Wang" <xkernel.wang@foxmail.com>
To: "Dan Carpenter" <dan.carpenter@oracle.com>
Cc: "Larry.Finger" <Larry.Finger@lwfinger.net>,
	"florian.c.schilhabel" <florian.c.schilhabel@googlemail.com>,
	gregkh <gregkh@linuxfoundation.org>,
	linux-staging <linux-staging@lists.linux.dev>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v3] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv()
Date: Tue, 27 Sep 2022 20:55:08 +0800	[thread overview]
Message-ID: <tencent_FACCCAB91E1C6F141FE1CA4AC1FF2420DA08@qq.com> (raw)

On Tue, Sep 27, 2022 03:57 PM, dan.carpenter@oracle.com wrote:
&gt; On Mon, Sep 26, 2022 at 03:06:05PM +0800, xkernel.wang@foxmail.com 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

             reply	other threads:[~2022-09-27 12:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-27 12:55 Xiaoke Wang [this message]
2022-10-04 13:37 ` [PATCH v3] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv() Dan Carpenter
  -- strict thread matches above, loose matches on Subject: below --
2022-09-26  7:06 xkernel.wang
2022-09-27  7:57 ` Dan Carpenter

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=tencent_FACCCAB91E1C6F141FE1CA4AC1FF2420DA08@qq.com \
    --to=xkernel.wang@foxmail.com \
    --cc=Larry.Finger@lwfinger.net \
    --cc=dan.carpenter@oracle.com \
    --cc=florian.c.schilhabel@googlemail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    /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;
as well as URLs for NNTP newsgroup(s).