* [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
@ 2024-08-28 18:01 Boqun Feng
2024-08-29 9:25 ` Alice Ryhl
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Boqun Feng @ 2024-08-28 18:01 UTC (permalink / raw)
To: rust-for-linux, linux-kernel
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross
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.
Reported-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.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 {{
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
2024-08-28 18:01 [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE Boqun Feng
@ 2024-08-29 9:25 ` Alice Ryhl
2024-08-29 20:41 ` Trevor Gross
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Alice Ryhl @ 2024-08-29 9:25 UTC (permalink / raw)
To: Boqun Feng
Cc: rust-for-linux, linux-kernel, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross
On Wed, Aug 28, 2024 at 8:02 PM Boqun Feng <boqun.feng@gmail.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.
>
> Reported-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Thanks. This probably didn't need to be an RFC.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
2024-08-28 18:01 [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE Boqun Feng
2024-08-29 9:25 ` Alice Ryhl
@ 2024-08-29 20:41 ` Trevor Gross
2024-08-30 15:05 ` Gary Guo
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Trevor Gross @ 2024-08-29 20:41 UTC (permalink / raw)
To: Boqun Feng
Cc: rust-for-linux, linux-kernel, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl
On Wed, Aug 28, 2024 at 1:02 PM Boqun Feng <boqun.feng@gmail.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.
>
> Reported-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Thanks for the fix!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
2024-08-28 18:01 [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE Boqun Feng
2024-08-29 9:25 ` Alice Ryhl
2024-08-29 20:41 ` Trevor Gross
@ 2024-08-30 15:05 ` Gary Guo
2024-08-30 16:34 ` Boqun Feng
2024-08-31 20:48 ` Benno Lossin
2024-09-02 9:39 ` Miguel Ojeda
4 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2024-08-30 15:05 UTC (permalink / raw)
To: Boqun Feng
Cc: rust-for-linux, linux-kernel, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross
On Wed, 28 Aug 2024 11:01:29 -0700
Boqun Feng <boqun.feng@gmail.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 said, the current approach also LGTM.
Reviewed-by: Gary Guo <gary@garyguo.net>
>
> Reported-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.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 {{
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
2024-08-30 15:05 ` Gary Guo
@ 2024-08-30 16:34 ` Boqun Feng
0 siblings, 0 replies; 7+ messages in thread
From: Boqun Feng @ 2024-08-30 16:34 UTC (permalink / raw)
To: Gary Guo
Cc: rust-for-linux, linux-kernel, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross
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@gmail.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@garyguo.net>
>
> >
> > Reported-by: Alice Ryhl <aliceryhl@google.com>
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.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 {{
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
2024-08-28 18:01 [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE Boqun Feng
` (2 preceding siblings ...)
2024-08-30 15:05 ` Gary Guo
@ 2024-08-31 20:48 ` Benno Lossin
2024-09-02 9:39 ` Miguel Ojeda
4 siblings, 0 replies; 7+ messages in thread
From: Benno Lossin @ 2024-08-31 20:48 UTC (permalink / raw)
To: Boqun Feng, rust-for-linux, linux-kernel
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Gary Guo,
Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross
On 28.08.24 20:01, Boqun Feng 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.
>
> Reported-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
---
Cheers,
Benno
> ---
> 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 {{
> --
> 2.45.2
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE
2024-08-28 18:01 [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE Boqun Feng
` (3 preceding siblings ...)
2024-08-31 20:48 ` Benno Lossin
@ 2024-09-02 9:39 ` Miguel Ojeda
4 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2024-09-02 9:39 UTC (permalink / raw)
To: Boqun Feng
Cc: rust-for-linux, linux-kernel, Miguel Ojeda, Alex Gaynor,
Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross
On Wed, Aug 28, 2024 at 8:02 PM Boqun Feng <boqun.feng@gmail.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.
>
> Reported-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Applied to `rust-fixes` -- thanks everyone!
Closes: https://rust-for-linux.zulipchat.com/#narrow/stream/x/topic/x/near/465412664
Fixes: 1fbde52bde73 ("rust: add `macros` crate")
Cc: stable@vger.kernel.org # 6.6.x: be2ca1e03965: ("rust: types:
Make Opaque::get const")
[ Fixed two typos, reworded title. - Miguel ]
For 6.1 we will send a targeted patch via Option 3 instead of
cherry-picking `Opaque` and a bunch of other bits that would be
required.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-09-02 9:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-28 18:01 [RFC PATCH] rust: Provide correct provenance when constructing THIS_MODULE Boqun Feng
2024-08-29 9:25 ` Alice Ryhl
2024-08-29 20:41 ` Trevor Gross
2024-08-30 15:05 ` Gary Guo
2024-08-30 16:34 ` Boqun Feng
2024-08-31 20:48 ` Benno Lossin
2024-09-02 9:39 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).