public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] uaccess: rust: add strncpy_from_user
@ 2025-04-24 15:17 Alice Ryhl
  2025-04-24 15:57 ` Boqun Feng
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Alice Ryhl @ 2025-04-24 15:17 UTC (permalink / raw)
  To: Miguel Ojeda, Andrew Morton, Alexander Viro, Greg Kroah-Hartman
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Danilo Krummrich, rust-for-linux,
	linux-kernel, Alice Ryhl

This is needed for ioctls that operate on a user-provided string.

It is somewhat unfortunate that strncpy_from_user does not nul-terminate
the string when the end of `buf` is reached. This implies that we can't
return a &CStr from the function, since the buffer may not always be
nul-terminated.

That said, we could add more convenient helpers on top that add a NUL
byte in that case.

This method isn't defined on UserSliceReader because it complicates the
semantics. The UserSliceReader type also has its own maximum length, so
we would have to limit the read by that length too.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/uaccess.rs | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index 80a9782b1c6e98ed6eae308ade8551afa7adc188..1bd82045e81ea887008e30241bd6de27f096b639 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -369,3 +369,30 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {
         Ok(())
     }
 }
+
+/// Reads a nul-terminated string into `buf` and returns the length.
+///
+/// Fails with [`EFAULT`] if the read happens on a bad address. If the end of `buf` is reached,
+/// then the buffer will not be nul-terminated.
+#[inline]
+pub fn strncpy_from_user(ptr: UserPtr, buf: &mut [u8]) -> Result<usize> {
+    // CAST: Slice lengths are guaranteed to be `<= isize::MAX`.
+    let len = buf.len() as isize;
+
+    // SAFETY: `buf` is valid for writing `buf.len()` bytes.
+    let res = unsafe {
+        bindings::strncpy_from_user(
+            buf.as_mut_ptr(),
+            ptr as *const u8,
+            len,
+        )
+    };
+
+    if res < 0 {
+        Err(Error::from_errno(res as i32))
+    } else {
+        #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+        assert!(res <= len);
+        Ok(res as usize)
+    }
+}

---
base-commit: 9c32cda43eb78f78c73aee4aa344b777714e259b
change-id: 20250424-strncpy-from-user-1f2d06b0cdde

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2025-04-25 14:45 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-24 15:17 [PATCH] uaccess: rust: add strncpy_from_user Alice Ryhl
2025-04-24 15:57 ` Boqun Feng
2025-04-25  9:43   ` Alice Ryhl
2025-04-25 13:39     ` Boqun Feng
2025-04-25 13:52       ` Greg Kroah-Hartman
2025-04-25 14:35         ` Boqun Feng
2025-04-25 14:45           ` Greg Kroah-Hartman
2025-04-24 16:32 ` Danilo Krummrich
2025-04-25  9:44   ` Alice Ryhl
2025-04-25 14:14     ` Danilo Krummrich
2025-04-24 16:38 ` Miguel Ojeda
2025-04-25  9:45   ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox