From: sashiko-bot@kernel.org
To: "Danilo Krummrich" <dakr@kernel.org>
Cc: ojeda@kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v3 16/27] rust: types: add `ForLt` trait for higher-ranked lifetime support
Date: Sun, 17 May 2026 00:23:32 +0000 [thread overview]
Message-ID: <20260517002333.62343C19425@smtp.kernel.org> (raw)
In-Reply-To: <20260517000149.3226762-17-dakr@kernel.org>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Safe Rust code can directly name and use `UnsafeForLtImpl` to bypass covariance proofs, leading to use-after-free.
- [High] The `ForLt!` macro fails to check covariance for types wrapped in macro invocations.
- [Low] The `cast_ref` function is incorrectly annotated with `#[inline(always)]`.
--
commit 2088fc9ea2c7348deada4ab3dfcc55a6783dcf27
Author: Gary Guo <gary@garyguo.net>
rust: types: add ForLt trait for higher-ranked lifetime support
This commit adds a ForLt trait and macro to support types that are generic
over a lifetime, ensuring covariance for safe lifetime shortening. It
provides a way to represent types that are themselves generic over
lifetimes, which is useful for cross-referencing data.
Link: https://docs.rs/higher-kinded-types/ [1]
> diff --git a/rust/kernel/types/for_lt.rs b/rust/kernel/types/for_lt.rs
> new file mode 100644
> index 0000000000000..22b4518a115b3
> --- /dev/null
> +++ b/rust/kernel/types/for_lt.rs
[ ... ]
> + /// Cast a reference to a shorter lifetime.
> + #[inline(always)]
> + fn cast_ref<'r, 'short: 'r, 'long: 'short>(long: &'r Self::Of<'long>) -> &'r Self::Of<'short> {
> + // SAFETY: This is sound as this trait guarantees covariance.
> + unsafe { core::mem::transmute(long) }
> + }
> +}
Should this function use #[inline] instead of #[inline(always)]?
According to the Rust coding guidelines, #[inline(always)] is reserved for
functions using build_assert!() that depend on function parameters. For small
forwarding abstractions like this, #[inline] should be used.
[ ... ]
> +/// `N` is to provide the macro a place to emit arbitrary items, in case it needs to prove
> +/// additional properties.
> +#[doc(hidden)]
> +pub struct UnsafeForLtImpl<T: ?Sized, WF, const N: usize>(PhantomData<(WF, T)>);
> +
> +// This is a helper trait for implementation `ForLt` to be able to use HRTB.
> +#[doc(hidden)]
> +pub trait WithLt<'a> {
> + type Of: 'a;
> +}
> +
> +// SAFETY: In `ForLt!` macro, a covariance proof is generated when naming `UnsafeForLtImpl`
> +// and it will fail to evaluate if the type is not covariant.
> +unsafe impl<T: ?Sized + for<'a> WithLt<'a>, WF> ForLt for UnsafeForLtImpl<T, WF, 0> {
Does this allow safe Rust code to bypass covariance proofs?
Because UnsafeForLtImpl is a public type, it appears safe code can name it
directly to safely obtain a ForLt implementation for a contravariant or
invariant type. For example:
UnsafeForLtImpl<dyn for<'a> WithLt<'a, Of = fn(&'a ())>, fn(&'static ()), 0>
This would bypass the compiler variance checks generated by the macro's
anonymous const block, potentially enabling use-after-free vulnerabilities
from safe code via cast_ref.
> diff --git a/rust/macros/for_lt.rs b/rust/macros/for_lt.rs
> new file mode 100644
> index 0000000000000..df2027789713a
> --- /dev/null
> +++ b/rust/macros/for_lt.rs
[ ... ]
> + // `&[mut] T` is covariant over lifetime.
> + // In case we have `&[mut] NoLifetime`, we don't need to do additional checks.
> + Type::Reference(ty) if !ty.elem.has_lifetime(self.0) => (),
> +
> + // No mention of lifetime at all, no need to perform compiler check.
> + ty if !ty.has_lifetime(self.0) => (),
Can this logic fail to check covariance for types wrapped in macro invocations?
The syntactic prover uses has_lifetime to determine if a type depends on the
generic lifetime binder. However, the underlying syn visitor does not traverse
into the unparsed TokenStream of a macro invocation.
If a user provides a type wrapped in a macro, such as
ForLt!(for<'a> SomeMacro!(fn(&'a u32))), has_lifetime will return false.
This would cause the macro to silently skip generating the prove_covariant
safety checks, which might allow unsound lifetime downcasting on contravariant
types.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260517000149.3226762-1-dakr@kernel.org?part=16
next prev parent reply other threads:[~2026-05-17 0:23 UTC|newest]
Thread overview: 98+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-17 0:00 [PATCH v3 00/27] rust: device: Higher-Ranked Lifetime Types for device drivers Danilo Krummrich
2026-05-17 0:00 ` [PATCH v3 01/27] rust: alloc: remove `'static` bound on `ForeignOwnable` Danilo Krummrich
2026-05-18 14:42 ` Alexandre Courbot
2026-05-17 0:00 ` [PATCH v3 02/27] rust: driver: move 'static bounds to constructor Danilo Krummrich
2026-05-18 14:42 ` Alexandre Courbot
2026-05-17 0:00 ` [PATCH v3 03/27] rust: driver: decouple driver private data from driver type Danilo Krummrich
2026-05-17 0:19 ` sashiko-bot
2026-05-17 14:32 ` Danilo Krummrich
2026-05-19 12:47 ` Gary Guo
2026-05-18 14:43 ` Alexandre Courbot
2026-05-17 0:00 ` [PATCH v3 04/27] rust: driver core: drop drvdata before devres release Danilo Krummrich
2026-05-17 0:37 ` sashiko-bot
2026-05-18 14:45 ` Alexandre Courbot
2026-05-19 12:47 ` Gary Guo
2026-05-17 0:00 ` [PATCH v3 05/27] rust: pci: implement Sync for Device<Bound> Danilo Krummrich
2026-05-17 0:40 ` sashiko-bot
2026-05-18 14:46 ` Alexandre Courbot
2026-05-19 13:01 ` Gary Guo
2026-05-17 0:00 ` [PATCH v3 06/27] rust: platform: " Danilo Krummrich
2026-05-18 14:46 ` Alexandre Courbot
2026-05-19 13:01 ` Gary Guo
2026-05-17 0:00 ` [PATCH v3 07/27] rust: auxiliary: " Danilo Krummrich
2026-05-17 0:36 ` sashiko-bot
2026-05-18 14:47 ` Alexandre Courbot
2026-05-19 13:02 ` Gary Guo
2026-05-17 0:00 ` [PATCH v3 08/27] rust: usb: " Danilo Krummrich
2026-05-17 0:33 ` sashiko-bot
2026-05-18 14:47 ` Alexandre Courbot
2026-05-19 13:02 ` Gary Guo
2026-05-17 0:00 ` [PATCH v3 09/27] rust: device: " Danilo Krummrich
2026-05-17 0:25 ` sashiko-bot
2026-05-18 14:48 ` Alexandre Courbot
2026-05-19 13:02 ` Gary Guo
2026-05-17 0:00 ` [PATCH v3 10/27] rust: pci: make Driver trait lifetime-parameterized Danilo Krummrich
2026-05-17 0:29 ` sashiko-bot
2026-05-18 14:53 ` Alexandre Courbot
2026-05-18 15:36 ` Gary Guo
2026-05-18 16:10 ` Danilo Krummrich
2026-05-19 4:52 ` Eliot Courtney
2026-05-19 10:39 ` Danilo Krummrich
2026-05-19 11:48 ` Gary Guo
2026-05-19 12:36 ` Danilo Krummrich
2026-05-20 6:14 ` Eliot Courtney
2026-05-17 0:00 ` [PATCH v3 11/27] rust: platform: " Danilo Krummrich
2026-05-18 14:55 ` Alexandre Courbot
2026-05-17 0:01 ` [PATCH v3 12/27] rust: auxiliary: " Danilo Krummrich
2026-05-18 15:39 ` Alexandre Courbot
2026-05-17 0:01 ` [PATCH v3 13/27] rust: usb: " Danilo Krummrich
2026-05-17 0:25 ` sashiko-bot
2026-05-18 15:40 ` Alexandre Courbot
2026-05-17 0:01 ` [PATCH v3 14/27] rust: i2c: " Danilo Krummrich
2026-05-17 0:39 ` sashiko-bot
2026-05-18 15:41 ` Alexandre Courbot
2026-05-17 0:01 ` [PATCH v3 15/27] rust: driver: update module documentation for GAT-based Data type Danilo Krummrich
2026-05-18 15:46 ` Alexandre Courbot
2026-05-17 0:01 ` [PATCH v3 16/27] rust: types: add `ForLt` trait for higher-ranked lifetime support Danilo Krummrich
2026-05-17 0:23 ` sashiko-bot [this message]
2026-05-19 6:02 ` Eliot Courtney
2026-05-19 11:23 ` Gary Guo
2026-05-19 11:07 ` Alexandre Courbot
2026-05-19 11:39 ` Gary Guo
2026-05-19 13:03 ` Danilo Krummrich
2026-05-19 13:34 ` Miguel Ojeda
2026-05-17 0:01 ` [PATCH v3 17/27] rust: auxiliary: generalize Registration over ForLt Danilo Krummrich
2026-05-17 0:31 ` sashiko-bot
2026-05-19 7:56 ` Eliot Courtney
2026-05-19 10:39 ` Danilo Krummrich
2026-05-19 11:20 ` Gary Guo
2026-05-19 16:45 ` Gary Guo
2026-05-20 0:33 ` Danilo Krummrich
2026-05-20 9:34 ` Gary Guo
2026-05-17 0:01 ` [PATCH v3 18/27] samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data Danilo Krummrich
2026-05-19 6:52 ` Eliot Courtney
2026-05-19 15:48 ` Gary Guo
2026-05-17 0:01 ` [PATCH v3 19/27] rust: pci: make Bar lifetime-parameterized Danilo Krummrich
2026-05-17 0:57 ` sashiko-bot
2026-05-19 6:36 ` Eliot Courtney
2026-05-19 16:24 ` Gary Guo
2026-05-19 17:27 ` Danilo Krummrich
2026-05-17 0:01 ` [PATCH v3 20/27] rust: io: make IoMem and ExclusiveIoMem lifetime-parameterized Danilo Krummrich
2026-05-17 1:31 ` sashiko-bot
2026-05-19 6:39 ` Eliot Courtney
2026-05-17 0:01 ` [PATCH v3 21/27] samples: rust: rust_driver_pci: use HRT lifetime for Bar Danilo Krummrich
2026-05-17 0:57 ` sashiko-bot
2026-05-19 6:41 ` Eliot Courtney
2026-05-17 0:01 ` [PATCH v3 22/27] rust: driver-core: rename 'a lifetime to 'bound Danilo Krummrich
2026-05-17 0:31 ` sashiko-bot
2026-05-19 6:42 ` Eliot Courtney
2026-05-19 16:56 ` Gary Guo
2026-05-19 17:23 ` Danilo Krummrich
2026-05-17 0:01 ` [PATCH REF v3 23/27] gpu: nova-core: " Danilo Krummrich
2026-05-17 0:01 ` [PATCH REF v3 24/27] gpu: nova-core: use lifetime for Bar Danilo Krummrich
2026-05-17 0:58 ` sashiko-bot
2026-05-17 0:01 ` [PATCH REF v3 25/27] gpu: nova-core: unregister sysmem flush page from Drop Danilo Krummrich
2026-05-17 0:50 ` sashiko-bot
2026-05-17 0:01 ` [PATCH REF v3 26/27] gpu: nova-core: replace ARef<Device> with &'bound Device in SysmemFlush Danilo Krummrich
2026-05-17 0:01 ` [PATCH REF v3 27/27] gpu: drm: tyr: use lifetime for IoMem Danilo Krummrich
2026-05-17 0:47 ` 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=20260517002333.62343C19425@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dakr@kernel.org \
--cc=linux-pci@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox