This patch adds a more convenient method for reading C strings from
userspace. Logic is added to NUL-terminate the buffer when necessary so
that a &CStr can be returned.
Note that we treat attempts to read past `self.length` as a fault, so
this returns EFAULT if that limit is exceeded before `buf.len()` is
reached.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
rust/kernel/uaccess.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 52 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index a7b123915e9aa2329f376d67cad93e2fc17ae017..978205289d297a4001a51fa40ac29039bff73672 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -293,6 +293,58 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
unsafe { buf.set_len(buf.len() + len) };
Ok(())
}
+
+ /// Read a NUL-terminated string from userspace and return it.
+ ///
+ /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
+ /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
+ /// returned `&CStr` points into `buf`.
+ ///
+ /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
+ /// copied).
+ #[doc(alias = "strncpy_from_user")]
+ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
+ if buf.is_empty() {
+ return Err(EINVAL);
+ }
+
+ // SAFETY: The types are compatible and `strncpy_from_user` doesn't write uninitialized
+ // bytes to `buf`.
+ let mut dst = unsafe { &mut *(buf as *mut [u8] as *mut [MaybeUninit<u8>]) };
+
+ // We never read more than `self.length` bytes.
+ if dst.len() > self.length {
+ dst = &mut dst[..self.length];
+ }
+
+ let mut len = raw_strncpy_from_user(self.ptr, dst)?;
+ if len < dst.len() {
+ // Add one to include the NUL-terminator.
+ len += 1;
+ } else if len < buf.len() {
+ // This implies that len == dst.len() < buf.len().
+ //
+ // This means that we could not fill the entire buffer, but we had to stop reading
+ // because we hit the `self.length` limit of this `UserSliceReader`. Since we did not
+ // fill the buffer, we treat this case as if we tried to read past the `self.length`
+ // limit and received a page fault, which is consistent with other `UserSliceReader`
+ // methods that also return page faults when you exceed `self.length`.
+ return Err(EFAULT);
+ } else {
+ // This implies that len == buf.len().
+ //
+ // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
+ // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
+ // already represents the length including the NUL-terminator.
+ //
+ // SAFETY: Due to the check at the beginning, the buffer is not empty.
+ unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+ }
+
+ // SAFETY: `raw_strncpy_from_user` guarantees that this range of bytes represents a
+ // NUL-terminated string with the only NUL byte being at the end.
+ Ok(unsafe { CStr::from_bytes_with_nul_unchecked(&buf[..len]) })
+ }
}
/// A writer for [`UserSlice`].
@@ -383,7 +435,6 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {
/// initialized and non-zero. Furthermore, if `len < buf.len()`, then `buf[len]` is a NUL byte.
/// Unsafe code may rely on these guarantees.
#[inline]
-#[expect(dead_code)]
fn raw_strncpy_from_user(ptr: UserPtr, buf: &mut [MaybeUninit<u8>]) -> Result<usize> {
// CAST: Slice lengths are guaranteed to be `<= isize::MAX`.
let len = buf.len() as isize;
--
2.49.0.967.g6a0df3ecc3-goog
On Mon, May 05, 2025 at 12:17:32PM +0000, Alice Ryhl wrote:
> + /// Read a NUL-terminated string from userspace and return it.
> + ///
> + /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
> + /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
> + /// returned `&CStr` points into `buf`.
> + ///
> + /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
> + /// copied).
> + #[doc(alias = "strncpy_from_user")]
> + pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
> + if buf.is_empty() {
> + return Err(EINVAL);
> + }
> +
> + // SAFETY: The types are compatible and `strncpy_from_user` doesn't write uninitialized
> + // bytes to `buf`.
> + let mut dst = unsafe { &mut *(buf as *mut [u8] as *mut [MaybeUninit<u8>]) };
> +
> + // We never read more than `self.length` bytes.
> + if dst.len() > self.length {
> + dst = &mut dst[..self.length];
> + }
> +
> + let mut len = raw_strncpy_from_user(self.ptr, dst)?;
> + if len < dst.len() {
> + // Add one to include the NUL-terminator.
> + len += 1;
> + } else if len < buf.len() {
> + // This implies that len == dst.len() < buf.len().
> + //
> + // This means that we could not fill the entire buffer, but we had to stop reading
> + // because we hit the `self.length` limit of this `UserSliceReader`. Since we did not
> + // fill the buffer, we treat this case as if we tried to read past the `self.length`
> + // limit and received a page fault, which is consistent with other `UserSliceReader`
> + // methods that also return page faults when you exceed `self.length`.
> + return Err(EFAULT);
> + } else {
> + // This implies that len == buf.len().
> + //
> + // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
> + // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
> + // already represents the length including the NUL-terminator.
> + //
> + // SAFETY: Due to the check at the beginning, the buffer is not empty.
> + unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
> + }
> +
> + // SAFETY: `raw_strncpy_from_user` guarantees that this range of bytes represents a
> + // NUL-terminated string with the only NUL byte being at the end.
This isn't true if we hit the else case above, no?
With that fixed,
Reviewed-by: Danilo Krummrich <dakr@kernel.org>
On Mon, May 05, 2025 at 06:22:31PM +0200, Danilo Krummrich wrote:
> On Mon, May 05, 2025 at 12:17:32PM +0000, Alice Ryhl wrote:
> > + /// Read a NUL-terminated string from userspace and return it.
> > + ///
> > + /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
> > + /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
> > + /// returned `&CStr` points into `buf`.
> > + ///
> > + /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
> > + /// copied).
> > + #[doc(alias = "strncpy_from_user")]
> > + pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
> > + if buf.is_empty() {
> > + return Err(EINVAL);
> > + }
> > +
> > + // SAFETY: The types are compatible and `strncpy_from_user` doesn't write uninitialized
> > + // bytes to `buf`.
> > + let mut dst = unsafe { &mut *(buf as *mut [u8] as *mut [MaybeUninit<u8>]) };
> > +
> > + // We never read more than `self.length` bytes.
> > + if dst.len() > self.length {
> > + dst = &mut dst[..self.length];
> > + }
> > +
> > + let mut len = raw_strncpy_from_user(self.ptr, dst)?;
> > + if len < dst.len() {
> > + // Add one to include the NUL-terminator.
> > + len += 1;
> > + } else if len < buf.len() {
> > + // This implies that len == dst.len() < buf.len().
> > + //
> > + // This means that we could not fill the entire buffer, but we had to stop reading
> > + // because we hit the `self.length` limit of this `UserSliceReader`. Since we did not
> > + // fill the buffer, we treat this case as if we tried to read past the `self.length`
> > + // limit and received a page fault, which is consistent with other `UserSliceReader`
> > + // methods that also return page faults when you exceed `self.length`.
> > + return Err(EFAULT);
> > + } else {
> > + // This implies that len == buf.len().
> > + //
> > + // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
> > + // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
> > + // already represents the length including the NUL-terminator.
> > + //
> > + // SAFETY: Due to the check at the beginning, the buffer is not empty.
> > + unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
> > + }
> > +
> > + // SAFETY: `raw_strncpy_from_user` guarantees that this range of bytes represents a
> > + // NUL-terminated string with the only NUL byte being at the end.
>
> This isn't true if we hit the else case above, no?
>
> With that fixed,
>
> Reviewed-by: Danilo Krummrich <dakr@kernel.org>
I guess the wording is a bit off. I will update to this:
// SAFETY: There are two cases:
// * If we hit the `len < dst.len()` case, then `raw_strncpy_from_user`
// guarantees that this slice contains exactly one NUL byte at the end
// of the string.
// * Otherwise, `raw_strncpy_from_user` guarantees that the string
// contained no NUL bytes, and we have since added a NUL byte at the
// end.
Alice
© 2016 - 2026 Red Hat, Inc.