Re: [PATCH v3 0/2] strncpy_from_user for Rust

From: Alice Ryhl
Date: Mon May 05 2025 - 08:21:37 EST


On Mon, May 5, 2025 at 2:17 PM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
>
> There is currently no easy way to read NUL-terminated strings from
> userspace. Trying to use the ordinary read function on an array of the
> maximum length doesn't work because it could fail with EFAULT when the C
> string is shorter than the maximum length. In this case,
> strncpy_from_user is better because it doesn't return EFAULT even if it
> encounters a page fault on bytes that are after the NUL-terminator but
> before the maximum length.
>
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> ---
> Changes in v3:
> - Remove pub from raw_strncpy_from_user.
> - Mention that some data may have been copied on EFAULT.
> - Add more comments to strcpy_into_buf about tricky cases.
> - Rewrite documentation of strcpy_into_buf.
> - Pick up Reviewed-by tags.
> - Link to v2: https://lore.kernel.org/r/20250429-strncpy-from-user-v2-0-7e6facac0bf0@xxxxxxxxxx

Oh I forgot to mention that I added a documentation alias so that
searching for strncpy_from_user using the search box on
https://rust.docs.kernel.org/ will show this function in the search
results.

Alice