Skip to contentSkip to conversation

rust: Provide correct provenance when constructing THIS_MODULE

RFC PATCH Applied Boqun Feng 6 participants 7 messages Aug 28, 2024 – Sep 2, 2024

Subsystems (2)
lore
Share & tools
lore

Boqun Feng

author
#0 lore raw

RFC PATCH 1/1 rust: Provide correct provenance when constructing THIS_MODULE

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.

---
 rust/macros/module.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/macros/module.rs +5 -1
index 571ffa2e189c..aef3b132f32b 100644
@@ -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 {{
Signature · 1 line
2.45.2
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Boqun Feng

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.

Quoted 2 lines

Thanks. This probably didn't need to be an RFC.

reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Boqun Feng
Quoted 17 lines · Boqun Feng

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.

Quoted 2 lines

Thanks for the fix!

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Boqun Feng
Quoted 8 lines · On Wed, 28 Aug 2024 11:01:29 -0700 Boqun Feng <[email protected]> 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.

Quoted 7 lines

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.

Quoted 24 lines
---
 rust/macros/module.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/macros/module.rs +5 -1
index 571ffa2e189c..aef3b132f32b 100644
@@ -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 {{
author
#4 lore raw
Quoted 19 lines · Gary Guo
Quoted 8 lines · On Wed, 28 Aug 2024 11:01:29 -0700 Boqun Feng <[email protected]> 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.

Quoted 7 lines

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.

Quoted 4 lines

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.

Quoted 2 lines

That said, the current approach also LGTM.

Thanks!

Regards,
Boqun

Quoted 27 lines
Quoted 24 lines
---
 rust/macros/module.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/macros/module.rs +5 -1
index 571ffa2e189c..aef3b132f32b 100644
@@ -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 {{
Reviewed-by on v1 1/1
#5 lore raw
In reply to Boqun Feng
Quoted 16 lines · Boqun Feng

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.

Quoted 2 lines

---
Cheers,
Benno

Quoted 24 lines
---
 rust/macros/module.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/macros/module.rs +5 -2
index 571ffa2e189c..aef3b132f32b 100644
@@ -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

Applied
#6 lore raw
In reply to Boqun Feng
Quoted 17 lines · Boqun Feng

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.

Quoted 2 lines

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: [email protected] # 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