From: sashiko-bot@kernel.org
To: "Markus Probst" <markus.probst@posteo.de>
Cc: linux-serial@vger.kernel.org, ojeda@kernel.org
Subject: Re: [PATCH v2 1/2] rust: serdev: Fix race condition on driver unbind
Date: Sat, 05 Sep 2026 13:44:58 +0000 [thread overview]
Message-ID: <20260905134458.85BDE1F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260905-rust_serdev_fix-v2-1-35dfcd06ef2e@posteo.de>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The function `drvdata_drop` introduces a Use-After-Free window and mutable aliasing vulnerability by dropping the driver data before clearing `dev->driver_data`.
- [Low] The new helper function `drvdata_drop` is missing the `#[inline]` attribute.
--
commit 26ab6ef2b4c35bdc9d899d45004d0c1a7f3fca80
Author: Markus Probst <markus.probst@posteo.de>
rust: serdev: Fix race condition on driver unbind
This commit attempts to fix a race condition where the driver data pointer is
NULL while the serdev device is still open. It does this by introducing a new
function drvdata_drop() that leaves the pointer valid until the memory drop
is complete.
Closes: https://lore.kernel.org/linux-serial/20260905000836.C8FC91F00A3D@smtp.kernel.org/
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index 2291d85b6849e..3886cc713c28b 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
[ ... ]
> @@ -236,6 +237,32 @@ pub(crate) unsafe fn drvdata_obtain<T>(&self) -> Option<Pin<KBox<T>>> {
> // in `into_foreign()`.
> Some(unsafe { Pin::<KBox<T>>::from_foreign(ptr.cast()) })
> }
> +
> + /// Drop the private data stored in this [`Device`].
> + ///
> + /// The pointer to the private data remains valid until the drop is complete.
> + ///
> + /// # Safety
> + ///
> + /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
> + /// [`Device::set_drvdata`].
> + pub(crate) unsafe fn drvdata_drop<T>(&self) {
[Severity: Low]
This isn't a bug, but should this abstraction function be annotated with
`#[inline]`?
The Rust subsystem coding guidelines mandate that small functions forwarding
to C bindings or performing small abstraction tasks should use `#[inline]`.
> + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> + let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
> +
> + if ptr.is_null() {
> + return;
> + }
> +
> + // SAFETY:
> + // - If `ptr` is not NULL, it comes from a previous call to `into_foreign()`.
> + // - `dev_get_drvdata()` guarantees to return the same pointer given to `dev_set_drvdata()`
> + // in `into_foreign()`.
> + drop(unsafe { Pin::<KBox<T>>::from_foreign(ptr.cast()) });
> +
> + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> + unsafe { bindings::dev_set_drvdata(self.as_raw(), core::ptr::null_mut()) };
[Severity: High]
Does this create a Use-After-Free window?
By dropping the driver data before clearing `dev->driver_data`, the pointer
remains valid while and after the memory is freed.
If a concurrent context, such as a hardware interrupt triggering
receive_buf_callback() in rust/kernel/serdev.rs, runs at this exact
moment, it will borrow a dangling pointer:
rust/kernel/serdev.rs:receive_buf_callback() {
...
let private_data = unsafe { sdev.as_ref().drvdata_borrow::<PrivateData<'_, T>>() };
...
}
Could this lead to a memory corruption vulnerability during the unbind path?
> + }
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260905-rust_serdev_fix-v2-0-35dfcd06ef2e@posteo.de?part=1
next prev parent reply other threads:[~2026-09-05 13:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-05 13:30 [PATCH v2 0/2] rust: serdev: Mitigate race conditions Markus Probst
2026-09-05 13:30 ` [PATCH v2 1/2] rust: serdev: Fix race condition on driver unbind Markus Probst
2026-09-05 13:44 ` sashiko-bot [this message]
2026-09-05 13:47 ` Markus Probst
2026-09-05 14:16 ` Gary Guo
2026-09-05 17:44 ` Markus Probst
2026-09-05 13:30 ` [PATCH v2 2/2] rust: serdev: Fix race condition on driver probe Markus Probst
2026-09-05 13:49 ` sashiko-bot
2026-09-05 13:57 ` Markus Probst
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=20260905134458.85BDE1F00A3D@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=markus.probst@posteo.de \
--cc=ojeda@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.