mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Nelson, Shannon" <shannon.nelson@amd.com>
To: hyper <hyperlyzcs@gmail.com>,
	brett.creeley@amd.com, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	jitxie@tencent.com, huntazhang@tencent.com
Subject: Re: [PATCH] pds_core: Fix possible double free in error handling path
Date: Fri, 1 Mar 2024 09:55:40 -0800	[thread overview]
Message-ID: <333dca5e-fae7-4684-afa8-10b8fdd48bf6@amd.com> (raw)
In-Reply-To: <20240301022325.20861-1-hyperlyzcs@gmail.com>

On 2/29/2024 6:23 PM, hyper wrote:
> 

Please specify the networking tree in your patch subject, something like 
[PATCH net] in this case.

> 
> When auxiliary_device_add() returns error and then calls
> auxiliary_device_uninit(), Callback function pdsc_auxbus_dev_release
> calls kfree(padev) to free memory. We shouldn't call kfree(padev)
> again in the error handling path.
> 
> Fix this by returning error directly after calling
> auxiliary_device_uninit().
> 
> Fixes: 4569cce43bc6 ("pds_core: add auxiliary_bus devices")
> Signed-off-by: hyper <hyperlyzcs@gmail.com>
> ---
>   drivers/net/ethernet/amd/pds_core/auxbus.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/net/ethernet/amd/pds_core/auxbus.c b/drivers/net/ethernet/amd/pds_core/auxbus.c
> index 11c23a7f3172..d6eedd78d5cc 100644
> --- a/drivers/net/ethernet/amd/pds_core/auxbus.c
> +++ b/drivers/net/ethernet/amd/pds_core/auxbus.c
> @@ -174,6 +174,8 @@ static struct pds_auxiliary_dev *pdsc_auxbus_dev_register(struct pdsc *cf,
> 
>   err_out_uninit:
>          auxiliary_device_uninit(aux_dev);
> +       return ERR_PTR(err);
> +
>   err_out:
>          kfree(padev);
>          return ERR_PTR(err);
> --
> 2.36.1
> 

Yes, I think you've got the right idea here, and this is probably a 
reasonable solution.

However, usually the error handling exit code stacks on itself, but here 
it becomes two separate independent chunks - a slightly different 
pattern.  Since these are both very short bits I'd be tempted to 
"enhance" that independence by putting the error handling back to where 
the errors happened, something like

diff --git a/drivers/net/ethernet/amd/pds_core/auxbus.c 
b/drivers/net/ethernet/amd/pds_core/auxbus.c
index a3c79848a69a..2babea110991 100644
--- a/drivers/net/ethernet/amd/pds_core/auxbus.c
+++ b/drivers/net/ethernet/amd/pds_core/auxbus.c
@@ -160,23 +160,19 @@ static struct pds_auxiliary_dev 
*pdsc_auxbus_dev_register(struct pdsc *cf,
         if (err < 0) {
                 dev_warn(cf->dev, "auxiliary_device_init of %s failed: 
%pe\n",
                          name, ERR_PTR(err));
-               goto err_out;
+               kfree(padev);
+               return ERR_PTR(err);
         }

         err = auxiliary_device_add(aux_dev);
         if (err) {
                 dev_warn(cf->dev, "auxiliary_device_add of %s failed: 
%pe\n",
                          name, ERR_PTR(err));
-               goto err_out_uninit;
+               auxiliary_device_uninit(aux_dev);
+               return ERR_PTR(err);
         }

         return padev;
-
-err_out_uninit:
-       auxiliary_device_uninit(aux_dev);
-err_out:
-       kfree(padev);
-       return ERR_PTR(err);
  }

Some might disagree. I like this a little better, but I could go either way.

Thoughts?

sln

  reply	other threads:[~2024-03-01 17:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-01  2:23 hyper
2024-03-01 17:55 ` Nelson, Shannon [this message]
2024-03-03  8:49   ` [PATCH net V2] net: " hyper
2024-03-04 17:21     ` Nelson, Shannon
2024-03-04 20:26     ` Breno Leitao
2024-03-05 14:58     ` Paolo Abeni
2024-03-06 10:57       ` [PATCH net V3] " Yongzhi Liu
2024-03-06 13:44         ` Wojciech Drewek
2024-03-06 17:05         ` Nelson, Shannon
2024-03-07 11:10         ` patchwork-bot+netdevbpf

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=333dca5e-fae7-4684-afa8-10b8fdd48bf6@amd.com \
    --to=shannon.nelson@amd.com \
    --cc=brett.creeley@amd.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=huntazhang@tencent.com \
    --cc=hyperlyzcs@gmail.com \
    --cc=jitxie@tencent.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --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®