uaccess: rust: use newtype for user pointers
Subsystems (2)
Alice Ryhl
reviewerPATCH v3 1/1 uaccess: rust: use newtype for user pointers
Currently, Rust code uses a typedef for unsigned long to represent userspace addresses. This is unfortunate because it means that userspace addresses could accidentally be mixed up with other integers. To alleviate that, we introduce a new UserPtr struct that wraps a raw pointer to represent a userspace address. By using a struct, type checking enforces that userspace addresses cannot be mixed up with anything else.
This is similar to the __user annotation in C that detects cases where user pointers are mixed with non-user pointers.
Note that unlike __user pointers in C, this type is just a pointer without a target type. This means that it can't detect cases such as mixing up which struct this user pointer references. However, that is okay due to the way this is intended to be used - generally, you create a UserPtr in your ioctl callback from the provided usize *before* dispatching on which ioctl is in use, and then after dispatching on the ioctl you pass the UserPtr into a UserSliceReader or UserSliceWriter; selecting the target type does not happen until you have obtained the UserSliceReader/Writer.
The UserPtr type is not marked with #[derive(Debug)], which means that it's not possible to print values of this type. This avoids ASLR leakage.
The type is added to the prelude as it is a fairly fundamental type similar to c_int. The wrapping_add() method is renamed to wrapping_byte_add() for consistency with the method name found on raw pointers.
---
This is based on top of the strncpy_from_user for Rust patch.
https://lore.kernel.org/r/[email protected]
I don't see a clear owner of uaccess in MAINTAINERS, so I suggest that
it lands through Miguel's tree.
---
Changes in v3:
- Use ptr::wrapping_byte_add in UserPtr::wrapping_byte_add.
- Add missing #[inline].
- Add Reviewed-by tags.
- Rewrite commit message.
- Rebase on v5 of strncpy_from_user.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Change usize to raw pointer.
- Make field private.
- Rename wrapping_add to wrapping_byte_add.
- Add to prelude.
- Rebase on v4 of strncpy_from_user
- Link to v1: https://lore.kernel.org/r/[email protected]
--- rust/kernel/prelude.rs | 2 ++ rust/kernel/uaccess.rs | 71 +++++++++++++++++++++++++++++++++------- samples/rust/rust_misc_device.rs | 2 ++ 3 files changed, 63 insertions(+), 12 deletions(-)
rust/kernel/prelude.rs +2 -0
@@ -46,3 +46,5 @@pub use super ::init::InPlaceInit ;pub use super ::current; + +pub use super ::uaccess::UserPtr ;
rust/kernel/uaccess.rs +59 -12
131 lines not on this page · raw message
samples/rust/rust_misc_device.rs +2 -0
@@ -176,6 +176,8 @@ fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<Sefn ioctl (me:Pin <&RustMiscDevice >, _file: &File , cmd:u32 , arg:usize ) ->Result <isize > {dev_info! (me.dev,"IOCTLing Rust Misc Device Sample\n" ); +// Treat the ioctl argument as a user pointer. +let arg =UserPtr ::from_addr (arg);let size =_IOC_SIZE (cmd);match cmd {
---
base-commit: d9946fe286439c2aeaa7953b8c316efe5b83d515
change-id: 20250506-userptr-newtype-2f060985c33b
prerequisite-change-id: 20250424-strncpy-from-user-1f2d06b0cdde:v5
prerequisite-patch-id: a683dbdea04daab269af0a6c90873e4dd139fe31
prerequisite-patch-id: 3db271c7acaff1a96836640e034d0321e5f412f5
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Quoted 6 lines · Alice Ryhl
This is based on top of the
strncpy_from_userfor Rust patch.
https://lore.kernel.org/r/[email protected]I don't see a clear owner of uaccess in
MAINTAINERS, so I suggest that it lands through Miguel's tree.
Sounds good to me, also for the dependency, unless someone really needs it earlier.
Cheers,
Miguel
Miguel Ojeda
AppliedQuoted 31 lines · Alice Ryhl
Currently, Rust code uses a typedef for unsigned long to represent userspace addresses. This is unfortunate because it means that userspace addresses could accidentally be mixed up with other integers. To alleviate that, we introduce a new UserPtr struct that wraps a raw pointer to represent a userspace address. By using a struct, type checking enforces that userspace addresses cannot be mixed up with anything else.
This is similar to the
__userannotation in C that detects cases where user pointers are mixed with non-user pointers.Note that unlike
__userpointers in C, this type is just a pointer without a target type. This means that it can't detect cases such as mixing up which struct this user pointer references. However, that is okay due to the way this is intended to be used - generally, you create a UserPtr in your ioctl callback from the provided usize *before* dispatching on which ioctl is in use, and then after dispatching on the ioctl you pass the UserPtr into a UserSliceReader or UserSliceWriter; selecting the target type does not happen until you have obtained the UserSliceReader/Writer.The UserPtr type is not marked with #[derive(Debug)], which means that it's not possible to print values of this type. This avoids ASLR leakage.
The type is added to the prelude as it is a fairly fundamental type similar to
c_int. Thewrapping_add()method is renamed towrapping_byte_add()for consistency with the method name found on raw pointers.
Quoted 6 lines
Reviewed-by Benno Lossin <[email protected]>Reviewed-by Danilo Krummrich <[email protected]>Reviewed-by Christian Schrefl <[email protected]>Reviewed-by Boqun Feng <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Applied to rust-next -- thanks everyone!
[ Reworded title. - Miguel ]
Cheers,
Miguel