From: Jann Horn <jannh@google.com>
To: "Tobin C. Harding" <tobin@kernel.org>
Cc: Kees Cook <keescook@chromium.org>, Shuah Khan <shuah@kernel.org>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Kernel Hardening <kernel-hardening@lists.openwall.com>,
kernel list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 6/6] lib: Add function strscpy_from_user()
Date: Tue, 19 Feb 2019 03:09:34 +0100 [thread overview]
Message-ID: <CAG48ez0t4DJGnv6oVVtiEeR-m3Oz_Zt-JNNzrA2CD2aZWAamvA@mail.gmail.com> (raw)
In-Reply-To: <20190218232308.11241-7-tobin@kernel.org>
On Tue, Feb 19, 2019 at 12:25 AM Tobin C. Harding <tobin@kernel.org> wrote:
> Currently we have strncpy_from_userspace(). If the user string is
> longer than the destination kernel buffer we get an error code -EFAULT.
No, see the other thread. If the user string is too long,
strncpy_from_userspace() fills the output buffer with non-null bytes
and returns the supplied length.
> 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@kernel.org>
> ---
> 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
> @@ -120,3 +120,46 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
> return -EFAULT;
> }
> EXPORT_SYMBOL(strncpy_from_user);
> +
> +/**
> + * strscpy_from_user() - Copy a NUL terminated string from userspace.
> + * @dst: Destination address, in kernel space. This buffer must be at
> + * least @count bytes long.
> + * @src: Source address, in user space.
> + * @count: Maximum number of bytes to copy, including the trailing %NUL.
> + *
> + * Copies a NUL-terminated string from userspace to kernel space. When
> + * the function returns @dst is guaranteed to be null terminated.
> + *
> + * Return: If access to userspace fails, returns -EFAULT.
That's wrong. Actually, you only return -EFAULT if the specified
source address points to an address outside the userspace address
range.
> Otherwise,
> + * return the number of characters copied excluding the trailing
> + * %NUL.
> + */
> +long strscpy_from_user(char *dst, const char __user *src, long count)
> +{
> + unsigned long max_addr, src_addr;
> +
> + if (unlikely(count <= 0))
> + return 0;
The "supply a signed long and quietly bail out if it's smaller than
zero" pattern seems bad to me. If count is zero, you can't guarantee
that the buffer will be null-terminated, and if it's smaller than
zero, something has gone very wrong.
> + max_addr = user_addr_max();
> + src_addr = (unsigned long)src;
> + if (likely(src_addr < max_addr)) {
> + unsigned long max = max_addr - src_addr;
> + long retval;
> +
> + kasan_check_write(dst, count);
> + check_object_size(dst, count, false);
> + if (user_access_begin(src, max)) {
> + retval = do_strncpy_from_user(dst, src, count, max);
> + user_access_end();
> + if (retval == -EFAULT) {
> + dst[count-1] = '\0';
> + return count - 1;
Uh... this looks bad. If do_strncpy_from_user() gets a fault -
anywhere -, you just put a nullbyte at the end of the supplied buffer
and return? As far as I can tell, this means that the caller will
think that you've filled the entire buffer, but actually everything
except for the last byte might be uninitialized.
> + }
> + return retval;
> + }
> + }
> + return -EFAULT;
> +}
> +EXPORT_SYMBOL(strscpy_from_user);
> --
> 2.20.1
>
next prev parent reply other threads:[~2019-02-19 2:09 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-18 23:23 [PATCH 0/6] lib: Add safe string funtions Tobin C. Harding
2019-02-18 23:23 ` [PATCH 1/6] lib/string: Enable string selftesting Tobin C. Harding
2019-02-19 10:55 ` Andy Shevchenko
2019-02-19 21:55 ` Tobin C. Harding
2019-02-20 10:49 ` Andy Shevchenko
2019-02-20 23:58 ` Kees Cook
2019-02-20 23:57 ` Kees Cook
2019-02-21 5:16 ` Tobin C. Harding
2019-02-18 23:23 ` [PATCH 2/6] lib/string: Fix erroneous 'overflow' documentation Tobin C. Harding
2019-02-21 0:02 ` Kees Cook
2019-02-21 5:17 ` Tobin C. Harding
2019-02-18 23:23 ` [PATCH 3/6] lib/string: Use correct docstring format Tobin C. Harding
2019-02-21 0:07 ` Kees Cook
2019-02-21 4:14 ` Randy Dunlap
2019-02-21 5:27 ` Kees Cook
2019-02-18 23:23 ` [PATCH 4/6] lib/string: Add string copy/zero function Tobin C. Harding
2019-02-21 0:48 ` Kees Cook
2019-02-21 5:20 ` Tobin C. Harding
2019-02-21 12:02 ` Andy Shevchenko
2019-02-25 20:09 ` Tobin C. Harding
2019-02-18 23:23 ` [PATCH 5/6] lib: Fix function documentation for strncpy_from_user Tobin C. Harding
2019-02-19 0:51 ` Jann Horn
2019-02-19 21:52 ` Tobin C. Harding
2019-02-21 1:05 ` Kees Cook
2019-02-21 5:24 ` Tobin C. Harding
2019-02-21 6:02 ` Kees Cook
2019-02-21 14:58 ` Rasmus Villemoes
2019-02-21 23:03 ` Kees Cook
2019-02-25 15:41 ` Rasmus Villemoes
2019-02-21 16:06 ` Jann Horn
2019-02-21 23:14 ` Kees Cook
2019-02-21 20:26 ` Stephen Rothwell
2019-02-21 23:16 ` Kees Cook
2019-02-21 14:28 ` Jann Horn
2019-02-21 22:52 ` Kees Cook
2019-02-18 23:23 ` [PATCH 6/6] lib: Add function strscpy_from_user() Tobin C. Harding
2019-02-19 2:09 ` Jann Horn [this message]
2019-02-19 2:12 ` Jann Horn
2019-02-19 21:53 ` Tobin C. Harding
2019-02-20 23:31 ` [PATCH 0/6] lib: Add safe string funtions Kees Cook
2019-02-21 5:15 ` Tobin C. Harding
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=CAG48ez0t4DJGnv6oVVtiEeR-m3Oz_Zt-JNNzrA2CD2aZWAamvA@mail.gmail.com \
--to=jannh@google.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=keescook@chromium.org \
--cc=kernel-hardening@lists.openwall.com \
--cc=linux-kernel@vger.kernel.org \
--cc=shuah@kernel.org \
--cc=tobin@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).