OpenCloudOS-Kernel/rust
Benno Lossin a4dec33d27 rust: macros: fix soundness issue in `module!` macro
[ Upstream commit 7044dcff8301b29269016ebd17df27c4736140d2 ]

The `module!` macro creates glue code that are called by C to initialize
the Rust modules using the `Module::init` function. Part of this glue
code are the local functions `__init` and `__exit` that are used to
initialize/destroy the Rust module.

These functions are safe and also visible to the Rust mod in which the
`module!` macro is invoked. This means that they can be called by other
safe Rust code. But since they contain `unsafe` blocks that rely on only
being called at the right time, this is a soundness issue.

Wrap these generated functions inside of two private modules, this
guarantees that the public functions cannot be called from the outside.
Make the safe functions `unsafe` and add SAFETY comments.

Cc: stable@vger.kernel.org
Reported-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/629
Fixes: 1fbde52bde ("rust: add `macros` crate")
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Wedson Almeida Filho <walmeida@microsoft.com>
Link: https://lore.kernel.org/r/20240401185222.12015-1-benno.lossin@proton.me
[ Moved `THIS_MODULE` out of the private-in-private modules since it
  should remain public, as Dirk Behme noticed [1]. Capitalized comments,
  avoided newline in non-list SAFETY comments and reworded to add
  Reported-by and newline. ]
Link: https://rust-for-linux.zulipchat.com/#narrow/stream/291565-Help/topic/x/near/433512583 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-05-17 12:01:56 +02:00
..
alloc rust: upgrade to Rust 1.73.0 2024-02-16 19:10:43 +01:00
bindings linux-kselftest-kunit-6.6-rc1 2023-08-28 18:56:38 -07:00
kernel rust: init: remove impl Zeroable for Infallible 2024-05-02 16:32:42 +02:00
macros rust: macros: fix soundness issue in `module!` macro 2024-05-17 12:01:56 +02:00
uapi rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
.gitignore rust: support running Rust documentation tests as KUnit ones 2023-07-19 09:32:53 -06:00
Makefile kbuild: rust: remove unneeded `@rustc_cfg` to avoid ICE 2024-05-02 16:32:42 +02:00
bindgen_parameters rust: Ignore preserve-most functions 2024-01-25 15:35:41 -08:00
build_error.rs rust: add `build_error` crate 2022-12-04 01:59:16 +01:00
compiler_builtins.rs rust: upgrade to Rust 1.73.0 2024-02-16 19:10:43 +01:00
exports.c rust: add `build_error` crate 2022-12-04 01:59:16 +01:00
helpers.c Rust changes for v6.6 2023-08-29 08:19:46 -07:00