Re: [PATCH 6/6] lib: Add function strscpy_from_user()

From: Jann Horn
Date: Mon Feb 18 2019 - 21:13:02 EST


On Tue, Feb 19, 2019 at 12:25 AM Tobin C. Harding <tobin@xxxxxxxxxx> wrote:
> Currently we have strncpy_from_userspace(). If the user string is
> longer than the destination kernel buffer we get an error code -EFAULT.
> We are unable to recover from here because this is the same error
> returned if the access to userspace fails totally.
>
> There is no reason we cannot continue execution with the user string
> truncated.
>
> Add a function strscpy_from_user() that guarantees the string written is
> null-terminated. If user string is longer than destination buffer
> truncates the string. Returns the number of characters written
> excluding the null-terminator.
>
> Signed-off-by: Tobin C. Harding <tobin@xxxxxxxxxx>
> ---
> lib/strncpy_from_user.c | 43 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 43 insertions(+)
>
> diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
> index 11fe9a4a00fd..6bd603ccec7a 100644
> --- a/lib/strncpy_from_user.c
> +++ b/lib/strncpy_from_user.c

This file is only built when CONFIG_GENERIC_STRNCPY_FROM_USER is set.
Some architectures have their own versions of strncpy_from_user() and
don't set that, so on those architectures, your code wouldn't be built
into the kernel.