linux-rtc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: Jiri Kosina <jikos@kernel.org>
Cc: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Ales Novak" <alnovak@suse.cz>,
	a.zummo@towertech.it, akpm@linux-foundation.org,
	rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org,
	linux-rtc@vger.kernel.org
Subject: Re: rtc: fix chardev initialization races
Date: Wed, 5 Sep 2018 00:02:07 +0200	[thread overview]
Message-ID: <20180904220207.GK13888@piout.net> (raw)
In-Reply-To: <nycvar.YFH.7.76.1805221726300.27054@cbobk.fhfr.pm>

On 22/05/2018 17:33:40+0200, Jiri Kosina wrote:
> On Tue, 22 May 2018, Uwe Kleine-König wrote:
> > [adding linux-rtc ML and Alexandre to Cc:]
> > 
> > Hello,
> > 
> > On Tue, May 22, 2018 at 02:09:36PM +0200, Jiri Kosina wrote:
> > > On Mon, 21 May 2018, Uwe Kleine-König wrote:
> > > 
> > > > > The race looks like that (thanks Jiri):
> > > > > 
> > > > > CPU0:                                CPU1:
> > > > > sys_load_module()
> > > > >  do_init_module()
> > > > >   do_one_initcall()
> > > > >    cmos_do_probe()
> > > > >     rtc_device_register()
> > > > >      __register_chrdev()
> > > > >      cdev->owner = struct module*
> > > > >                                      open("/dev/rtc0")
> > > > >     rtc_device_unregister()
> > > > >   module_put()
> > > > >   free_module()
> > > > >    module_free(mod->module_core)
> > > > >    /* struct module *module is now
> > > > >       freed */
> > > > >                                       chrdev_open()
> > > > >                                        spin_lock(cdev_lock)
> > > > >                                        cdev_get()
> > > > >                                         try_module_get()
> > > > >                                          module_is_live()
> > > > >                                          /* dereferences already
> > > > >                                             freed struct module* */
> > > > 
> > > > [Context: For a patch to rtc-pcf2127.c Alexandre Belloni asked not to
> > > > fail after rtc_device_register successfully finished and pointed to this
> > > > reasoning as explaination.]
> > > > 
> > > > If there is really such a race then (I hope) there is
> > > > something in the cdev code that needs fixing. According to my
> > > > understanding, when rtc_device_unregister returned, the cdev is gone and
> > > > so chrdev_open is supposed to fail.
> > > 
> > > Oh wow, hello back to 4 years ago!
> > 
> > :-)
> > 
> 
> > > Looking at the current code, I don't think there is no such race any more, 
> > > as the last thing cmos_do_probe() -> __rtc_register_device() does that can 
> > > potentially fail is the chardev creation itself.
> > 
> > OK, so you agree that it's also save to do something in a driver's probe
> > after rtc_device_register() and call rtc_device_unregister() in the error
> > path, right? 
> 
> Hmm, not really; that's what the code apparently did 4 years ago (judging 
> from the scenario in the old mail, I of course forgot all the details), 
> but doesn't do it any more.
> 
> Looking at the current code, if you call rtc_device_unregister() in the 
> probe path, where is the guarantee that cdev_get() will not derefernce 
> already freed struct module*?
> 

I've also tried to produce it and couldn't. The whole magic happens in
kobj_lookup. Freeing the module before ends up with the application
opening the char dev getting -ENXIO. Freeing the module after
kobj_lookup is not possible and ends up with -EBUSY.

-- 
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

      reply	other threads:[~2018-09-04 22:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1393410793-7315-1-git-send-email-alnovak@suse.cz>
2018-05-21 12:25 ` rtc: fix chardev initialization races Uwe Kleine-König
     [not found]   ` <nycvar.YFH.7.76.1805221407100.27054@cbobk.fhfr.pm>
2018-05-22 13:39     ` Uwe Kleine-König
2018-05-22 15:33       ` Jiri Kosina
2018-09-04 22:02         ` Alexandre Belloni [this message]

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=20180904220207.GK13888@piout.net \
    --to=alexandre.belloni@bootlin.com \
    --cc=a.zummo@towertech.it \
    --cc=akpm@linux-foundation.org \
    --cc=alnovak@suse.cz \
    --cc=jikos@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=rtc-linux@googlegroups.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /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).