linux-nvdimm.lists.01.org archive mirror
 help / color / mirror / Atom feed
* [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
@ 2020-02-05 12:38 Dan Carpenter
  2020-02-05 17:47 ` Dan Williams
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2020-02-05 12:38 UTC (permalink / raw)
  To: dan.j.williams; +Cc: linux-nvdimm

Hello Dan Williams,

The patch 4d88a97aa9e8: "libnvdimm, nvdimm: dimm driver and base
libnvdimm device-driver infrastructure" from May 31, 2015, leads to
the following static checker warning:

	drivers/nvdimm/bus.c:511 nd_async_device_register()
	error: dereferencing freed memory 'dev'

drivers/nvdimm/bus.c
   502  static void nd_async_device_register(void *d, async_cookie_t cookie)
   503  {
   504          struct device *dev = d;
   505  
   506          if (device_add(dev) != 0) {
   507                  dev_err(dev, "%s: failed\n", __func__);
   508                  put_device(dev);
                        ^^^^^^^^^^^^^^^
   509          }
   510          put_device(dev);
                ^^^^^^^^^^^^^^
   511          if (dev->parent)
   512                  put_device(dev->parent);
   513  }

We call get_device() from __nd_device_register(), I guess.  It seems
buggy to call put device twice on error.

regards,
dan carpenter


regards,
dan carpenter
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 12:38 [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure Dan Carpenter
@ 2020-02-05 17:47 ` Dan Williams
  2020-02-05 18:10   ` Dan Carpenter
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Williams @ 2020-02-05 17:47 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-nvdimm

On Wed, Feb 5, 2020 at 4:38 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> Hello Dan Williams,
>
> The patch 4d88a97aa9e8: "libnvdimm, nvdimm: dimm driver and base
> libnvdimm device-driver infrastructure" from May 31, 2015, leads to
> the following static checker warning:
>
>         drivers/nvdimm/bus.c:511 nd_async_device_register()
>         error: dereferencing freed memory 'dev'
>
> drivers/nvdimm/bus.c
>    502  static void nd_async_device_register(void *d, async_cookie_t cookie)
>    503  {
>    504          struct device *dev = d;
>    505
>    506          if (device_add(dev) != 0) {
>    507                  dev_err(dev, "%s: failed\n", __func__);
>    508                  put_device(dev);
>                         ^^^^^^^^^^^^^^^
>    509          }
>    510          put_device(dev);
>                 ^^^^^^^^^^^^^^
>    511          if (dev->parent)
>    512                  put_device(dev->parent);
>    513  }
>
> We call get_device() from __nd_device_register(), I guess.  It seems
> buggy to call put device twice on error.

The registration path does:

        get_device(dev);

        async_schedule_dev_domain(nd_async_device_register, dev,
                                  &nd_async_domain);

...and device_add() does its own get_device(). I could add a comment
to clarify which put_device() is correlated to which put_device(), but
this seems a false positive to me.
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 17:47 ` Dan Williams
@ 2020-02-05 18:10   ` Dan Carpenter
  2020-02-05 18:23     ` Dan Williams
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2020-02-05 18:10 UTC (permalink / raw)
  To: Dan Williams; +Cc: linux-nvdimm

On Wed, Feb 05, 2020 at 09:47:01AM -0800, Dan Williams wrote:
> On Wed, Feb 5, 2020 at 4:38 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
> >
> > Hello Dan Williams,
> >
> > The patch 4d88a97aa9e8: "libnvdimm, nvdimm: dimm driver and base
> > libnvdimm device-driver infrastructure" from May 31, 2015, leads to
> > the following static checker warning:
> >
> >         drivers/nvdimm/bus.c:511 nd_async_device_register()
> >         error: dereferencing freed memory 'dev'
> >
> > drivers/nvdimm/bus.c
> >    502  static void nd_async_device_register(void *d, async_cookie_t cookie)
> >    503  {
> >    504          struct device *dev = d;
> >    505
> >    506          if (device_add(dev) != 0) {
> >    507                  dev_err(dev, "%s: failed\n", __func__);
> >    508                  put_device(dev);
> >                         ^^^^^^^^^^^^^^^
> >    509          }
> >    510          put_device(dev);
> >                 ^^^^^^^^^^^^^^
> >    511          if (dev->parent)
> >    512                  put_device(dev->parent);
> >    513  }
> >
> > We call get_device() from __nd_device_register(), I guess.  It seems
> > buggy to call put device twice on error.
> 
> The registration path does:
> 
>         get_device(dev);
> 
>         async_schedule_dev_domain(nd_async_device_register, dev,
>                                   &nd_async_domain);
> 
> ...and device_add() does its own get_device().

device_add() does its own put_device() at the end so it's a net zero.

regards,
dan carpenter
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 18:10   ` Dan Carpenter
@ 2020-02-05 18:23     ` Dan Williams
  2020-02-05 19:08       ` Dan Carpenter
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Williams @ 2020-02-05 18:23 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-nvdimm

On Wed, Feb 5, 2020 at 10:11 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> On Wed, Feb 05, 2020 at 09:47:01AM -0800, Dan Williams wrote:
> > On Wed, Feb 5, 2020 at 4:38 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
> > >
> > > Hello Dan Williams,
> > >
> > > The patch 4d88a97aa9e8: "libnvdimm, nvdimm: dimm driver and base
> > > libnvdimm device-driver infrastructure" from May 31, 2015, leads to
> > > the following static checker warning:
> > >
> > >         drivers/nvdimm/bus.c:511 nd_async_device_register()
> > >         error: dereferencing freed memory 'dev'
> > >
> > > drivers/nvdimm/bus.c
> > >    502  static void nd_async_device_register(void *d, async_cookie_t cookie)
> > >    503  {
> > >    504          struct device *dev = d;
> > >    505
> > >    506          if (device_add(dev) != 0) {
> > >    507                  dev_err(dev, "%s: failed\n", __func__);
> > >    508                  put_device(dev);
> > >                         ^^^^^^^^^^^^^^^
> > >    509          }
> > >    510          put_device(dev);
> > >                 ^^^^^^^^^^^^^^
> > >    511          if (dev->parent)
> > >    512                  put_device(dev->parent);
> > >    513  }
> > >
> > > We call get_device() from __nd_device_register(), I guess.  It seems
> > > buggy to call put device twice on error.
> >
> > The registration path does:
> >
> >         get_device(dev);
> >
> >         async_schedule_dev_domain(nd_async_device_register, dev,
> >                                   &nd_async_domain);
> >
> > ...and device_add() does its own get_device().
>
> device_add() does its own put_device() at the end so it's a net zero.
>

It does it's own, yes, but the put_device() after device_add() failure
is there to drop the reference taken by device_initialize().
Otherwise, device_add() has always documented:

 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up your
 * reference instead.

...so what am I missing?
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 18:23     ` Dan Williams
@ 2020-02-05 19:08       ` Dan Carpenter
  2020-02-05 19:16         ` Dan Williams
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2020-02-05 19:08 UTC (permalink / raw)
  To: Dan Williams; +Cc: linux-nvdimm

On Wed, Feb 05, 2020 at 10:23:00AM -0800, Dan Williams wrote:
> > > >    506          if (device_add(dev) != 0) {
> > > >    507                  dev_err(dev, "%s: failed\n", __func__);
> > > >    508                  put_device(dev);
> > > >                         ^^^^^^^^^^^^^^^
> > > >    509          }
> > > >    510          put_device(dev);
> > > >                 ^^^^^^^^^^^^^^
> > > >    511          if (dev->parent)
> > > >    512                  put_device(dev->parent);
> > > >    513  }
> > > >
> > > > We call get_device() from __nd_device_register(), I guess.  It seems
> > > > buggy to call put device twice on error.
> > >
> > > The registration path does:
> > >
> > >         get_device(dev);
> > >
> > >         async_schedule_dev_domain(nd_async_device_register, dev,
> > >                                   &nd_async_domain);
> > >
> > > ...and device_add() does its own get_device().
> >
> > device_add() does its own put_device() at the end so it's a net zero.
> >
> 
> It does it's own, yes, but the put_device() after device_add() failure
> is there to drop the reference taken by device_initialize().
> Otherwise, device_add() has always documented:
> 
>  * NOTE: _Never_ directly free @dev after calling this function, even
>  * if it returned an error! Always use put_device() to give up your
>  * reference instead.
> 
> ...so what am I missing?

The "never call kfree" is hopefully straight forward because the kobject
needs to do its own cleanup.

__nvdimm_create() allocates the dev.
nd_device_register() calls device_initialize() which call kobject_init()
   so the refcount is 1.
__nd_device_register() call get_device() so the refcount is now two.
nd_async_device_register() decrements the refcount once on success.

But if device_add() fails then it decrements it twice.  Now the refcount
is zero so we call nvdimm_release().  This leads to a use after free on
the next line:

	put_device(dev);
	if (dev->parent)

There is a trick here because depending on the debug options it
might free immediately or it might call nvdimm_release() after 4
seconds.  See kobject_release() for details.

Either way if device_add() fails we return back to __nvdimm_create()
and return the zero reference count "nvdimm" pointer, which is going
to be a problem.

regards,
dan carpenter
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 19:08       ` Dan Carpenter
@ 2020-02-05 19:16         ` Dan Williams
  2020-02-05 19:28           ` Dan Carpenter
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Williams @ 2020-02-05 19:16 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-nvdimm

On Wed, Feb 5, 2020 at 11:08 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> On Wed, Feb 05, 2020 at 10:23:00AM -0800, Dan Williams wrote:
> > > > >    506          if (device_add(dev) != 0) {
> > > > >    507                  dev_err(dev, "%s: failed\n", __func__);
> > > > >    508                  put_device(dev);
> > > > >                         ^^^^^^^^^^^^^^^
> > > > >    509          }
> > > > >    510          put_device(dev);
> > > > >                 ^^^^^^^^^^^^^^
> > > > >    511          if (dev->parent)
> > > > >    512                  put_device(dev->parent);
> > > > >    513  }
> > > > >
> > > > > We call get_device() from __nd_device_register(), I guess.  It seems
> > > > > buggy to call put device twice on error.
> > > >
> > > > The registration path does:
> > > >
> > > >         get_device(dev);
> > > >
> > > >         async_schedule_dev_domain(nd_async_device_register, dev,
> > > >                                   &nd_async_domain);
> > > >
> > > > ...and device_add() does its own get_device().
> > >
> > > device_add() does its own put_device() at the end so it's a net zero.
> > >
> >
> > It does it's own, yes, but the put_device() after device_add() failure
> > is there to drop the reference taken by device_initialize().
> > Otherwise, device_add() has always documented:
> >
> >  * NOTE: _Never_ directly free @dev after calling this function, even
> >  * if it returned an error! Always use put_device() to give up your
> >  * reference instead.
> >
> > ...so what am I missing?
>
> The "never call kfree" is hopefully straight forward because the kobject
> needs to do its own cleanup.
>
> __nvdimm_create() allocates the dev.
> nd_device_register() calls device_initialize() which call kobject_init()
>    so the refcount is 1.
> __nd_device_register() call get_device() so the refcount is now two.
> nd_async_device_register() decrements the refcount once on success.
>
> But if device_add() fails then it decrements it twice.  Now the refcount
> is zero so we call nvdimm_release().  This leads to a use after free on
> the next line:
>
>         put_device(dev);
>         if (dev->parent)
>
> There is a trick here because depending on the debug options it
> might free immediately or it might call nvdimm_release() after 4
> seconds.  See kobject_release() for details.
>
> Either way if device_add() fails we return back to __nvdimm_create()
> and return the zero reference count "nvdimm" pointer, which is going
> to be a problem.

Ugh, sorry I thought you were pointing out that there's too many
put_device() not the use after free. Yes, the use after free is a bug
that needs fixing.
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 19:16         ` Dan Williams
@ 2020-02-05 19:28           ` Dan Carpenter
  2020-02-05 20:04             ` Dan Williams
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2020-02-05 19:28 UTC (permalink / raw)
  To: Dan Williams; +Cc: linux-nvdimm

On Wed, Feb 05, 2020 at 11:16:22AM -0800, Dan Williams wrote:
> Ugh, sorry I thought you were pointing out that there's too many
> put_device() not the use after free. Yes, the use after free is a bug
> that needs fixing.

I am complaining about the device_puts...  If we call device_put()
twice then it cause a problem in __nvdimm_create()

drivers/nvdimm/dimm_devs.c
   506          nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
   507          nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
   508          nd_device_register(dev);
   509  
   510          return nvdimm;
                       ^^^^^^
If we call device_put() twice then we this pointer within 4 seconds.

   511  }

The fix is probably to make nd_device_register() return an error code so
we can do:

	ret = nd_device_register(dev);
	if (ret) {
		device_put(&nvdimm->dev);
		return NULL;
	}

	return nvdimm;

regards,
dan carpenter
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 19:28           ` Dan Carpenter
@ 2020-02-05 20:04             ` Dan Williams
  2020-02-05 20:18               ` Dan Carpenter
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Williams @ 2020-02-05 20:04 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-nvdimm

On Wed, Feb 5, 2020 at 11:28 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> On Wed, Feb 05, 2020 at 11:16:22AM -0800, Dan Williams wrote:
> > Ugh, sorry I thought you were pointing out that there's too many
> > put_device() not the use after free. Yes, the use after free is a bug
> > that needs fixing.
>
> I am complaining about the device_puts...  If we call device_put()
> twice then it cause a problem in __nvdimm_create()
>
> drivers/nvdimm/dimm_devs.c
>    506          nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
>    507          nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
>    508          nd_device_register(dev);
>    509
>    510          return nvdimm;
>                        ^^^^^^
> If we call device_put() twice then we this pointer within 4 seconds.

"we this pointer"? We "what" this pointer. 4 seconds is relative to a
runtime test case?

...but yes, point taken this looks like an obvious leak in isolation.

>
>    511  }
>
> The fix is probably to make nd_device_register() return an error code so
> we can do:
>
>         ret = nd_device_register(dev);
>         if (ret) {
>                 device_put(&nvdimm->dev);
>                 return NULL;
>         }
>
>         return nvdimm;

Ok, so this is meant to be mitigated by the fact that all consumers of
nvdimm_create() perform a nvdimm_bus_check_dimm_count() to validate
that device_add() did not fail. The reason for this organization is to
allow nvdimm initialization to happen in parallel.
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
  2020-02-05 20:04             ` Dan Williams
@ 2020-02-05 20:18               ` Dan Carpenter
  0 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2020-02-05 20:18 UTC (permalink / raw)
  To: Dan Williams; +Cc: linux-nvdimm

On Wed, Feb 05, 2020 at 12:04:15PM -0800, Dan Williams wrote:
> On Wed, Feb 5, 2020 at 11:28 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
> >
> > On Wed, Feb 05, 2020 at 11:16:22AM -0800, Dan Williams wrote:
> > > Ugh, sorry I thought you were pointing out that there's too many
> > > put_device() not the use after free. Yes, the use after free is a bug
> > > that needs fixing.
> >
> > I am complaining about the device_puts...  If we call device_put()
> > twice then it cause a problem in __nvdimm_create()
> >
> > drivers/nvdimm/dimm_devs.c
> >    506          nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
> >    507          nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
> >    508          nd_device_register(dev);
> >    509
> >    510          return nvdimm;
> >                        ^^^^^^
> > If we call device_put() twice then we this pointer within 4 seconds.
> 
> "we this pointer"? We "what" this pointer. 4 seconds is relative to a
> runtime test case?
> 

Sorry.  I meant we *free* it.  The second device_put() leads to a
nvdimm_release(dev) where dev is "&nvdimm->dev" within 0-4 seconds.

Most times it will free it immediately but if you have
CONFIG_DEBUG_KOBJECT_RELEASE enabled then it will wait between 1-4
seconds and then free nvdimm.  It's a config option, not a runtime
thing.

regards,
dan carpenter
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-02-05 20:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-05 12:38 [bug report] libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure Dan Carpenter
2020-02-05 17:47 ` Dan Williams
2020-02-05 18:10   ` Dan Carpenter
2020-02-05 18:23     ` Dan Williams
2020-02-05 19:08       ` Dan Carpenter
2020-02-05 19:16         ` Dan Williams
2020-02-05 19:28           ` Dan Carpenter
2020-02-05 20:04             ` Dan Williams
2020-02-05 20:18               ` Dan Carpenter

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).