From: Lorenzo Delgado <lnsdev@proton.me>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: Lorenzo Delgado <lnsdev@proton.me>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Thorsten Blum <blum@kernel.org>,
Francis Laniel <laniel_francis@npnm.eu>
Subject: [PATCH v2 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf()
Date: Tue, 13 Oct 2026 13:49:48 +0000 [thread overview]
Message-ID: <20261012-nonempty-slice-v2-2-0f2b67d9463e@proton.me> (raw)
In-Reply-To: <20261012-nonempty-slice-v2-0-0f2b67d9463e@proton.me>
`strcpy_into_buf()` returns `EINVAL` for an empty buffer and later
writes the NUL terminator through `last_mut().unwrap_unchecked()`,
relying on that earlier check.
Take the buffer as a `NonEmpty<[u8]>` instead, so the requirement is in
the signature. The `EINVAL` case goes away, and `last_mut()` returns
the element directly, which removes the `unsafe` block. There are no
callers to convert. A caller with an array buffer can pass it through
`NonEmpty::from_array_mut()` without a runtime check.
The `MaybeUninit` cast still starts from `[u8]`, so its SAFETY comment
does not depend on the layout of the wrapper.
Suggested-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/rust-for-linux/DM0D9TERQAM3.2Y3HV6P68E2N8@garyguo.net/
Link: https://github.com/Rust-for-Linux/linux/issues/1262
Signed-off-by: Lorenzo Delgado <lnsdev@proton.me>
---
rust/kernel/uaccess.rs | 15 +++++----------
1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index 5f6c4d7a1a51..1cc5c7580d3a 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -11,6 +11,7 @@
error::Result,
ffi::{c_char, c_void},
fs::file,
+ non_empty::NonEmpty,
prelude::*,
ptr::KnownSize,
transmute::{AsBytes, FromBytes},
@@ -383,20 +384,16 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
/// 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
+ /// Since there must be space to add a NUL-terminator, `buf` is a [`NonEmpty`] slice. 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);
- }
-
+ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut NonEmpty<[u8]>) -> Result<&'buf CStr> {
// SAFETY: The types are compatible and `strncpy_from_user` doesn't write uninitialized
// bytes to `buf`.
- let mut dst = unsafe { &mut *(core::ptr::from_mut(buf) as *mut [MaybeUninit<u8>]) };
+ let mut dst = unsafe { &mut *(core::ptr::from_mut::<[u8]>(buf) as *mut [MaybeUninit<u8>]) };
// We never read more than `self.length` bytes.
if dst.len() > self.length {
@@ -422,9 +419,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
// 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 };
+ *buf.last_mut() = 0;
}
// This method consumes `self`, so it can only be called once, thus we do not need to
--
2.55.0
prev parent reply other threads:[~2026-10-13 13:50 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 13:49 [PATCH v2 0/2] rust: add NonEmpty Lorenzo Delgado
2026-10-13 13:49 ` [PATCH v2 1/2] rust: non_empty: " Lorenzo Delgado
2026-10-13 13:54 ` Gary Guo
2026-10-13 15:11 ` Lorenzo Delgado
2026-10-13 13:49 ` Lorenzo Delgado [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261012-nonempty-slice-v2-2-0f2b67d9463e@proton.me \
--to=lnsdev@proton.me \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=blum@kernel.org \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=laniel_francis@npnm.eu \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®