From: tarunsahu@google.com
To: sashiko-reviews@lists.linux.dev, David Jeffery <djeffery@redhat.com>
Cc: linux-scsi@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH 2/9] driver core: prevent device_add() during system shutdown
Date: Fri, 28 Aug 2026 16:01:43 +0000 [thread overview]
Message-ID: <9huzh5kez120.fsf@tarunix.c.googlers.com> (raw)
In-Reply-To: <20260821144335.1BE1F1F000E9@smtp.kernel.org>
sashiko-bot@kernel.org writes:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] Race condition in device_add() abort path during system shutdown leading to Use-After-Free.
> --
>
> --- Patch [2]: [PATCH 2/9] driver core: prevent device_add() during system shutdown ---
> commit 17748fb46ccfdb0adadd634adbfe70a9fd601b9a
> Author: Tarun Sahu <tarunsahu@google.com>
>
> driver core: prevent device_add() during system shutdown
>
> This commit adds system state checks to device_add() to prevent new
> devices from being added while the system is halting, powering off, or
> restarting. It re-checks the state under devices_kset->list_lock to
> avoid a TOCTOU race window with device_shutdown().
>
>> diff --git a/drivers/base/core.c b/drivers/base/core.c
>> --- a/drivers/base/core.c
>> +++ b/drivers/base/core.c
> [ ... ]
>> @@ -3699,6 +3720,18 @@ int device_add(struct device *dev)
>> goto Error;
>> }
>>
>> + /*
>> + * Check system_state again under list_lock to prevent a TOCTOU race
>> + * where device_shutdown() runs concurrently and misses this device.
>> + */
>> + spin_lock(&devices_kset->list_lock);
>> + if (unlikely(system_is_shutting_down())) {
>> + spin_unlock(&devices_kset->list_lock);
>> + error = -ESHUTDOWN;
>> + goto ShutdownError;
>
> [Severity: High]
> Does this error path introduce a use-after-free race with device_shutdown()?
>
> If device_add() successfully links the device to devices_kset->list via
> kobject_add(), device_shutdown() could concurrently run and pull the
> newly added device from the list:
>
> device_shutdown() {
> ...
> spin_lock(&devices_kset->list_lock);
> ...
> dev = list_entry(devices_kset->list.prev, struct device, kobj.entry);
> ...
> list_del_init(&dev->kobj.entry);
> spin_unlock(&devices_kset->list_lock);
> ...
> }
>
> If device_add() then resumes, acquires the list lock, and sees
> system_is_shutting_down(), it will jump to ShutdownError and return
> -ESHUTDOWN.
>
> The caller of device_add() will typically treat this as a failure and
> free the device and its associated private data. Meanwhile,
> device_shutdown() is already outside the lock and actively executing the
> shutdown callbacks on that exact same device:
>
> device_shutdown() {
> ...
> if (dev->bus && dev->bus->shutdown) {
> ...
> dev->bus->shutdown(dev);
> }
> ...
> }
>
> Can this lead to device_shutdown() accessing freed device memory?
We can make device_add and device_shutdown synchronise like
+device_block_adding();
+wait_for_device_add();
calling these two in device_shutdown similiarly we have for probing.
~Tarun
>
>> + }
>> + spin_unlock(&devices_kset->list_lock);
>> +
>> /* notify platform of device entry */
>> device_platform_notify(dev);
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20260821142414.150892-1-djeffery@redhat.com?part=2
next prev parent reply other threads:[~2026-08-28 16:01 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-21 14:24 [PATCH v20 0/9] shut down devices asynchronously David Jeffery
2026-08-21 14:24 ` [PATCH 1/9] driver core: rely on put_device to free dev->p David Jeffery
2026-08-21 14:32 ` sashiko-bot
2026-08-21 14:24 ` [PATCH 2/9] driver core: prevent device_add() during system shutdown David Jeffery
2026-08-21 14:43 ` sashiko-bot
2026-08-28 16:01 ` tarunsahu [this message]
2026-08-21 14:24 ` [PATCH 3/9] driver core: warn should device_move try to move a need_parent_lock device David Jeffery
2026-08-21 14:34 ` sashiko-bot
2026-08-28 15:50 ` tarunsahu
2026-08-21 14:24 ` [PATCH 4/9] driver core: separate function to shutdown one device David Jeffery
2026-08-21 14:29 ` sashiko-bot
2026-08-21 14:24 ` [PATCH 5/9] driver core: do not always lock parent in shutdown David Jeffery
2026-08-21 14:33 ` sashiko-bot
2026-08-21 14:24 ` [PATCH 6/9] driver core: async device shutdown infrastructure David Jeffery
2026-08-21 14:38 ` sashiko-bot
2026-08-21 14:24 ` [PATCH 7/9] PCI: Link a virtual function to its physical function David Jeffery
2026-08-21 14:34 ` sashiko-bot
2026-08-21 14:24 ` [PATCH 8/9] PCI: Enable async shutdown support David Jeffery
2026-08-21 14:39 ` sashiko-bot
2026-08-21 14:24 ` [PATCH 9/9] scsi: " David Jeffery
2026-08-21 14:38 ` sashiko-bot
-- strict thread matches above, loose matches on Subject: below --
2026-09-02 17:07 [PATCH v21 0/9] shut down devices asynchronously David Jeffery
2026-09-02 17:07 ` [PATCH 2/9] driver core: prevent device_add() during system shutdown David Jeffery
2026-09-02 17:15 ` sashiko-bot
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=9huzh5kez120.fsf@tarunix.c.googlers.com \
--to=tarunsahu@google.com \
--cc=djeffery@redhat.com \
--cc=linux-pci@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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