mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: Su Hui <suhui@nfschina.com>
Cc: stuyoder@gmail.com, laurentiu.tudor@nxp.com, nathan@kernel.org,
	ndesaulniers@google.com, morbo@google.com,
	justinstitt@google.com, gregkh@linuxfoundation.org,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] bus: fsl-mc:  Fix the double free in fsl_mc_device_add()
Date: Thu, 14 Nov 2024 11:57:47 +0300	[thread overview]
Message-ID: <b767348e-d89c-416e-acea-1ebbff3bea20@stanley.mountain> (raw)
In-Reply-To: <656ca826-cb81-4b46-8e15-ec0b1044db8d@stanley.mountain>

On Thu, Nov 14, 2024 at 11:41:25AM +0300, Dan Carpenter wrote:
> On Thu, Nov 14, 2024 at 04:27:52PM +0800, Su Hui wrote:
> > Clang static checker(scan-build) warning:
> > drivers/bus/fsl-mc/fsl-mc-bus.c: line 909, column 2
> > Attempt to free released memory.
> > 
> > When 'obj_desc->type' == "dprc" and begin to free 'mc_bus' and 'mc_dev',
> > there is a double free problem because of 'mc_dev = &mc_bus->mc_dev'.
> > Add a judgment to fix this problem.
> > 
> > Fixes: a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations")
> > Signed-off-by: Su Hui <suhui@nfschina.com>
> > ---
> >  drivers/bus/fsl-mc/fsl-mc-bus.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/bus/fsl-mc/fsl-mc-bus.c b/drivers/bus/fsl-mc/fsl-mc-bus.c
> > index 930d8a3ba722..8d2d5d3cc782 100644
> > --- a/drivers/bus/fsl-mc/fsl-mc-bus.c
> > +++ b/drivers/bus/fsl-mc/fsl-mc-bus.c
> > @@ -905,8 +905,10 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
> >  
> >  error_cleanup_dev:
> >  	kfree(mc_dev->regions);
> > -	kfree(mc_bus);
> > -	kfree(mc_dev);
> > +	if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
> 
> This works, but it would probably be nicer to write this as:
> 
> 	if (is_fsl_mc_bus_dprc(mc_dev))
> 		kfree(mc_bus);
> 	else
> 		kfree(mc_dev);
> 
> That way it would match the release function.

   820          mc_dev->dev.release = fsl_mc_device_release;

	[ snip ]

   891           * The device-specific probe callback will get invoked by device_add()
   892           */
   893          error = device_add(&mc_dev->dev);
   894          if (error < 0) {
   895                  dev_err(parent_dev,
   896                          "device_add() failed for device %s: %d\n",
   897                          dev_name(&mc_dev->dev), error);
   898                  goto error_cleanup_dev;

I don't think this goto is correct.  I think fsl_mc_device_release() will be
called automaticall on this path so the goto is a double free.

   899          }
   900  
   901          dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
   902  
   903          *new_mc_dev = mc_dev;
   904          return 0;
   905  
   906  error_cleanup_dev:
   907          kfree(mc_dev->regions);
   908          if (is_fsl_mc_bus_dprc(mc_dev))
   909                  kfree(mc_bus);
   910          else
   911                  kfree(mc_dev);
   912  
   913          return error;
   914  }

regards,
dan carpenter


  reply	other threads:[~2024-11-14  8:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-14  8:27 Su Hui
2024-11-14  8:41 ` Dan Carpenter
2024-11-14  8:57   ` Dan Carpenter [this message]
2024-11-14  9:34     ` Su Hui

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=b767348e-d89c-416e-acea-1ebbff3bea20@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=justinstitt@google.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=laurentiu.tudor@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=stuyoder@gmail.com \
    --cc=suhui@nfschina.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®