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] [day] [month] [year] [list]
Message-ID: <CAJ-ks9mxRqFPz4J_L3KLa-VHX+dcbsobncc=n2f84iZ+ju2VaA@mail.gmail.com>
Date: Fri, 11 Apr 2025 10:34:00 -0400
From: Tamir Duberstein <tamird@...il.com>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: Benno Lossin <benno.lossin@...ton.me>, 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: retain pointer mut-ness in `container_of!`

On Fri, Apr 11, 2025 at 9:29 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
>
> On Fri, Apr 11, 2025 at 3:09 PM Tamir Duberstein <tamird@...il.com> wrote:
> >
> > On Fri, Apr 11, 2025 at 8:38 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
> > >
> > > On Fri, Apr 11, 2025 at 2:35 PM Benno Lossin <benno.lossin@...ton.me> wrote:
> > > >
> > > > On Fri Apr 11, 2025 at 2:25 PM CEST, Tamir Duberstein wrote:
> > > > > On Fri, Apr 11, 2025 at 5:02 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
> > > > >> On Wed, Apr 09, 2025 at 10:43:16AM -0400, Tamir Duberstein wrote:
> > > > >> > @@ -199,9 +199,8 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
> > > > >> >  #[macro_export]
> > > > >> >  macro_rules! container_of {
> > > > >> >      ($ptr:expr, $type:ty, $($f:tt)*) => {{
> > > > >> > -        let ptr = $ptr as *const _ as *const u8;
> > > > >> >          let offset: usize = ::core::mem::offset_of!($type, $($f)*);
> > > > >> > -        ptr.sub(offset) as *const $type
> > > > >> > +        $ptr.byte_sub(offset).cast::<$type>()
> > > > >> >      }}
> > > > >> >  }
> > > > >>
> > > > >> This implementation does not check the type of `ptr`. Would we not want
> > > > >> it to have the type of the field?
> > > > >
> > > > > I might be missing it but ISTM that the current implementation doesn't
> > > > > check that either.
> > > > >
> > > > > It's not obvious to me how you'd implement such a check; given `$ptr`
> > > > > and `$f`, how do you get your hands on the type of `$ptr->$($f)*`?
> > > >
> > > > I don't think it's possible with current rust, but maybe with field
> > > > projection (:
> > > >
> > > >     ($ptr:expr, $type:ty, $($f:tt)*) => {{
> > > >         // do not run this code, only use it for type-checking:
> > > >         let _ = || {
> > > >             let mut ptr = $ptr;
> > > >             ptr = $ptr.cast::<<field_of!($t, $($f)*) as Field>::Type>();
> > > >         };
> > > >         let offset: usize = ::core::mem::offset_of!($type, $($f)*);
> > > >         $ptr.byte_sub(offset).cast::<$type>()
> > > >     }}
> > >
> > > You can definitely implement the check with current Rust. You use
> > > addr_of! to create a raw pointer with the type of the field, and
> > > trigger a type error if `ptr` doesn't have the same type as that other
> > > pointer. Something along these lines would do it:
> > >
> > > let mut ptr = $ptr;
> > > let offset: usize = ::core::mem::offset_of!($type, $($f)*);
> > > let container = ptr.byte_sub(offset).cast::<$type>();
> > > if false {
> > >     ptr = ::core::ptr::addr_of!((*container).$($f)*).cast_mut();
> > > }
> > > container
> >
> > It's a nice idea. Wouldn't it require `$ptr` to be `*mut _` and not
> > work with `*const _`?
>
> As far as I can tell, it should work with const pointers too. The rhs
> of the last assignment gets downgraded back to a const pointer in that
> case.
>
> > In any case, I hope we agree that this can be
> > done separately.
>
> Yes, since the feature does not exist today, it can happen separately.

Sent as another patch:
https://lore.kernel.org/all/20250411-b4-container-of-type-check-v1-1-08262ef67c95@gmail.com/.

Thanks for the suggestion!
Tamir

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ