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: <ZtH0nK8ytZCiZs-_@Boquns-Mac-mini.local>
Date: Fri, 30 Aug 2024 09:34:36 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Gary Guo <gary@...yguo.net>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
	Miguel Ojeda <ojeda@...nel.org>,
	Alex Gaynor <alex.gaynor@...il.com>,
	Wedson Almeida Filho <wedsonaf@...il.com>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <benno.lossin@...ton.me>,
	Andreas Hindborg <a.hindborg@...sung.com>,
	Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>
Subject: Re: [RFC PATCH] rust: Provide correct provenance when constructing
 THIS_MODULE

On Fri, Aug 30, 2024 at 04:05:32PM +0100, Gary Guo wrote:
> On Wed, 28 Aug 2024 11:01:29 -0700
> Boqun Feng <boqun.feng@...il.com> wrote:
> 
> > Currently while defining `THIS_MODULE` symbol in `module!()`, the
> > pointer used to contruct `ThisModule` is derived from an immutable
> > reference of `__this_module`, which means the pointer doesn't have
> > the provenance for writting, and that means any write to that pointer
> > is UB regardless of data races or not. However, the usage of
> > `THIS_MODULE` includes passing this pointer to functions that may write
> > to it (probably in unsafe code), and this will create soundness issues.
> > 
> > One way to fix this is using `addr_of_mut!()` but that requires the
> > unstable feature "const_mut_refs". So instead of `addr_of_mut()!`,
> > an extern static `Opaque` is used here: since `Opaque<T>` is transparent
> > to `T`, an extern static `Opaque` will just wrap the C symbol (defined
> > in a C compile unit) in an `Opaque`, which provides a pointer with
> > writable provenance via `Opaque::get()`. This fix the potential UBs
> > because of pointer provenance unmatched.
> 
> `const_mut_refs` is getting stablised so we should be able to use it
> soon. I am slightly inclined to use `addr_of_mut!()` over `Opaque` in
> this case so we can use it directly from bindgen.
> 

That's the reason why I put "RFC" in the title, although I feel `Opaque`
is better, one of the reasons is you can easily provide a `*mut T` with
wrong provenance (e.g. casting from a `&T`), but it's harder to
construct a (or an? ;-)) `&Opaque<T>` incorrectly. So if an API takes
`&Opaque<T>` instead of a `*mut T`, it can reduce some user errors.
Therefore personally, I prefer `&Opaque<T>` (or `&UnsafeCell<T>`). But
of course, I don't think this is something really strong, and I might
miss something, so I don't feel bad using `*mut T`.

Moreover, besides `const_mut_refs`, we also may also want to wait for

	https://github.com/rust-lang/rust/pull/125834

to be in stable to avoid unnecessary unsafe.

> That said, the current approach also LGTM.
> 

Thanks!

Regards,
Boqun

> Reviewed-by: Gary Guo <gary@...yguo.net>
> 
> > 
> > Reported-by: Alice Ryhl <aliceryhl@...gle.com>
> > Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> > ---
> >  rust/macros/module.rs | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> > index 571ffa2e189c..aef3b132f32b 100644
> > --- a/rust/macros/module.rs
> > +++ b/rust/macros/module.rs
> > @@ -217,7 +217,11 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
> >              // freed until the module is unloaded.
> >              #[cfg(MODULE)]
> >              static THIS_MODULE: kernel::ThisModule = unsafe {{
> > -                kernel::ThisModule::from_ptr(&kernel::bindings::__this_module as *const _ as *mut _)
> > +                extern \"C\" {{
> > +                    static __this_module: kernel::types::Opaque<kernel::bindings::module>;
> > +                }}
> > +
> > +                kernel::ThisModule::from_ptr(__this_module.get())
> >              }};
> >              #[cfg(not(MODULE))]
> >              static THIS_MODULE: kernel::ThisModule = unsafe {{
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ