[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <CACVxJT-f+K_z-XxqjDVU_QK-VJPqnCEbWuMJF1fyTL0V0xiTgA@mail.gmail.com>
Date: Tue, 15 Apr 2014 13:49:38 +0300
From: Alexey Dobriyan <adobriyan@...il.com>
To: Linux Kernel <linux-kernel@...r.kernel.org>
Cc: Vegard Nossum <vegard.nossum@...il.com>,
Dan Carpenter <dan.carpenter@...cle.com>,
Andrew Morton <akpm@...ux-foundation.org>
Subject: Re: + lib-stringc-strlcpy-might-read-too-far.patch added to -mm tree
On Tue, Apr 15, 2014 at 12:47 AM, <akpm@...ux-foundation.org> wrote:
> Subject: lib/string.c: strlcpy() might read too far
>
> Imagine you have a user controlled variable at the end of a struct which
> is allocated at the end of a page. The strlen() could read beyond the
> mapped memory and cause an oops.
>
> Probably there are two reasons why we have never hit this condition in
> real life. First you would have to be really unlucky for all the
> variables to line up so the oops can happen. Second we don't do a lot of
> fuzzing with invalid strings.
>
> The strnlen() call is obviously a little bit slower than strlen() but I
> have tested it and I think it's probably ok.
> --- a/lib/string.c~lib-stringc-strlcpy-might-read-too-far
> +++ a/lib/string.c
> @@ -148,10 +148,10 @@ EXPORT_SYMBOL(strncpy);
> */
> size_t strlcpy(char *dest, const char *src, size_t size)
> {
> - size_t ret = strlen(src);
> + size_t ret = strnlen(src, size);
>
> if (size) {
> - size_t len = (ret >= size) ? size - 1 : ret;
> + size_t len = (ret < size) ? ret : ret - 1;
> memcpy(dest, src, len);
> dest[len] = '\0';
> }
Return value matters. It may not matter for kernel, because kernel is
not heavy string user.
But it is better to not diverge from master code:
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/strlcpy.c?rev=1.11
Counter-rationale:
* strlcpy() accepts strings, so if you're giving raw buffer you're
doing it wrong.
* last byte of last page argument is bogus because kernel copies data
from userspace first.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists