lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAJ-ks9kBpj1-_bwN_ncMc62indvviJv6zbpdJgd8JOf5o05a-g@mail.gmail.com>
Date: Fri, 11 Apr 2025 17:39:24 -0400
From: Tamir Duberstein <tamird@...il.com>
To: Benno Lossin <benno.lossin@...ton.me>
Cc: Alice Ryhl <aliceryhl@...gle.com>, Miguel Ojeda <ojeda@...nel.org>, 
	Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>, 
	Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, 
	Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>, 
	Danilo Krummrich <dakr@...nel.org>, rust-for-linux@...r.kernel.org, 
	linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: check type of `$ptr` in `container_of!`

On Fri, Apr 11, 2025 at 5:12 PM Benno Lossin <benno.lossin@...ton.me> wrote:
>
> On Fri Apr 11, 2025 at 5:41 PM CEST, Tamir Duberstein wrote:
> > On Fri, Apr 11, 2025 at 10:36 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
> >>
> >> On Fri, Apr 11, 2025 at 4:31 PM Tamir Duberstein <tamird@...il.com> wrote:
> >> >
> >> > Add a compile-time check that `*$ptr` is of the type of `$type->$($f)*`.
> >> >
> >> > Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
> >> > Link: https://lore.kernel.org/all/CAH5fLgh6gmqGBhPMi2SKn7mCmMWfOSiS0WP5wBuGPYh9ZTAiww@mail.gmail.com/
> >> > Signed-off-by: Tamir Duberstein <tamird@...il.com>
> >> > ---
> >> >  rust/kernel/lib.rs | 5 ++++-
> >> >  1 file changed, 4 insertions(+), 1 deletion(-)
> >> >
> >> > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> >> > index 1df11156302a..da9e36aa7967 100644
> >> > --- a/rust/kernel/lib.rs
> >> > +++ b/rust/kernel/lib.rs
> >> > @@ -200,7 +200,10 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
> >> >  macro_rules! container_of {
> >> >      ($ptr:expr, $type:ty, $($f:tt)*) => {{
> >> >          let offset: usize = ::core::mem::offset_of!($type, $($f)*);
> >> > -        $ptr.byte_sub(offset).cast::<$type>()
> >> > +        let container = $ptr.byte_sub(offset).cast::<$type>();
> >> > +        fn assert_same_type<T>(_: T, _: T) {}
> >> > +        assert_same_type($ptr, ::core::mem::addr_of!((*container).$($f)*).cast_mut());
> >
> > I noticed I accidentally sent `::core::mem::addr_of` instead of
> > `::core::ptr::addr_of`; will fix once we agree below.
> >
> >> Perhaps it would be better to wrap the type check in an `if false` to
> >> avoid evaluating the expressions at runtime?
> >
> > It's optimized out at O1: https://godbolt.org/z/44Go5xnWr. Is it worth it?
>
> Wrapping in `if false` definitely doesn't hurt, since we get better
> debug perf.

How's this?

if false { [$ptr, ::core::ptr::addr_of!((*container).$($f)*).cast_mut()]; }

Confirmed it's entirely gone from codegen: https://godbolt.org/z/fo3x13r5K.




Tamir

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ