From: Dan Williams <dan.j.williams@intel.com>
To: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>,
Dan Williams <dan.j.williams@intel.com>,
<gregkh@linuxfoundation.org>
Cc: <syzbot+4762dd74e32532cda5ff@syzkaller.appspotmail.com>,
<stable@vger.kernel.org>, Ashish Sangwan <a.sangwan@samsung.com>,
Namjae Jeon <namjae.jeon@samsung.com>,
Dirk Behme <dirk.behme@de.bosch.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-cxl@vger.kernel.org>
Subject: Re: [PATCH] driver core: Fix uevent_show() vs driver detach race
Date: Sat, 13 Jul 2024 10:22:33 -0700 [thread overview]
Message-ID: <6692b7d912e3e_8f74d2945a@dwillia2-xfh.jf.intel.com.notmuch> (raw)
In-Reply-To: <0b34da9e-c13f-4fab-a67d-244b0ebba394@I-love.SAKURA.ne.jp>
Tetsuo Handa wrote:
> On 2024/07/13 8:49, Dan Williams wrote:
> >>> + /* Synchronize with dev_uevent() */
> >>> + synchronize_rcu();
> >>> +
> >>
> >> this synchronize_rcu(), in order to make sure that
> >> READ_ONCE(dev->driver) in dev_uevent() observes NULL?
> >
> > No, this synchronize_rcu() is to make sure that if dev_uevent() wins the
> > race and observes that dev->driver is not NULL that it is still safe to
> > dereference that result because the 'struct device_driver' object is
> > still live.
>
> I can't catch what the pair of rcu_read_lock()/rcu_read_unlock() in dev_uevent()
> and synchronize_rcu() in module_remove_driver() is for.
It is to extend the lifetime of @driver if dev_uevent() observes
non-NULL @dev->driver.
> I think that the below race is possible.
> Please explain how "/* Synchronize with module_remove_driver() */" works.
It is for this race:
Thread1: Thread2:
dev_uevent(...) delete_module()
driver = dev->driver; mod->exit()
if (driver) driver_unregister()
driver_detach() // <-- @dev->driver marked NULL
module_remove_driver()
free_module() // <-- @driver object destroyed
add_uevent_var(env, "DRIVER=%s", driver->name); // <-- use after free of @driver
If driver_detach() happens before Thread1 reads dev->driver then there
is no use after free risk.
The previous attempt to fix this held the device_lock() over
dev_uevent() which prevents driver_detach() from even starting, but that
causes lockdep issues and is even more heavy-handed than the
synchronize_rcu() delay. RCU makes sure that @driver stays alive between
reading @dev->driver and reading @driver->name.
next prev parent reply other threads:[~2024-07-13 17:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-12 19:42 Dan Williams
2024-07-12 22:18 ` Tetsuo Handa
2024-07-12 23:49 ` Dan Williams
2024-07-13 6:05 ` Tetsuo Handa
2024-07-13 17:22 ` Dan Williams [this message]
2024-07-14 7:17 ` Greg KH
2024-07-14 16:49 ` Dan Williams
2024-08-06 9:00 ` Dirk Behme
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=6692b7d912e3e_8f74d2945a@dwillia2-xfh.jf.intel.com.notmuch \
--to=dan.j.williams@intel.com \
--cc=a.sangwan@samsung.com \
--cc=dirk.behme@de.bosch.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=namjae.jeon@samsung.com \
--cc=penguin-kernel@i-love.sakura.ne.jp \
--cc=rafael@kernel.org \
--cc=stable@vger.kernel.org \
--cc=syzbot+4762dd74e32532cda5ff@syzkaller.appspotmail.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®