OpenCloudOS-Kernel/rust/macros
Boqun Feng f9275893b0 rust: macros: provide correct provenance when constructing THIS_MODULE
commit a5a3c952e82c1ada12bf8c55b73af26f1a454bd2 upstream.

Currently while defining `THIS_MODULE` symbol in `module!()`, the
pointer used to construct `ThisModule` is derived from an immutable
reference of `__this_module`, which means the pointer doesn't have
the provenance for writing, 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: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Gary Guo <gary@garyguo.net>
Closes: https://rust-for-linux.zulipchat.com/#narrow/stream/x/topic/x/near/465412664
Fixes: 1fbde52bde ("rust: add `macros` crate")
Cc: stable@vger.kernel.org # 6.6.x: be2ca1e03965: ("rust: types: Make Opaque::get const")
Link: https://lore.kernel.org/r/20240828180129.4046355-1-boqun.feng@gmail.com
[ Fixed two typos, reworded title. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-09-12 11:11:25 +02:00
..
concat_idents.rs
helpers.rs rust: macros: refactor generics parsing of `#[pin_data]` into its own function 2023-05-31 18:53:09 +02:00
lib.rs rust: remove `params` from `module` macro example 2024-05-02 16:32:42 +02:00
module.rs rust: macros: provide correct provenance when constructing THIS_MODULE 2024-09-12 11:11:25 +02:00
paste.rs rust: macros: add `paste!` proc macro 2023-08-10 22:28:02 +02:00
pin_data.rs rust: macros: replace Self with the concrete type in #[pin_data] 2023-05-31 18:53:09 +02:00
pinned_drop.rs rust: init: add `PinnedDrop` trait and macros 2023-04-12 18:41:05 +02:00
quote.rs rust: add derive macro for `Zeroable` 2023-08-21 14:31:48 +02:00
vtable.rs rust: macros: vtable: fix `HAS_*` redefinition (`gen_const_name`) 2023-08-09 21:15:07 +02:00
zeroable.rs rust: add derive macro for `Zeroable` 2023-08-21 14:31:48 +02:00