* Re: + lib-stringc-strlcpy-might-read-too-far.patch added to -mm tree [not found] <534c5757.VydWMxxcqkXGiwNa%akpm@linux-foundation.org> @ 2014-04-15 10:49 ` Alexey Dobriyan 2014-04-15 11:18 ` Dan Carpenter 0 siblings, 1 reply; 3+ messages in thread From: Alexey Dobriyan @ 2014-04-15 10:49 UTC (permalink / raw) To: Linux Kernel; +Cc: Vegard Nossum, Dan Carpenter, Andrew Morton On Tue, Apr 15, 2014 at 12:47 AM, <akpm@linux-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. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: + lib-stringc-strlcpy-might-read-too-far.patch added to -mm tree 2014-04-15 10:49 ` + lib-stringc-strlcpy-might-read-too-far.patch added to -mm tree Alexey Dobriyan @ 2014-04-15 11:18 ` Dan Carpenter 2014-04-15 11:36 ` Alexey Dobriyan 0 siblings, 1 reply; 3+ messages in thread From: Dan Carpenter @ 2014-04-15 11:18 UTC (permalink / raw) To: Alexey Dobriyan; +Cc: Linux Kernel, Vegard Nossum, Andrew Morton On Tue, Apr 15, 2014 at 01:49:38PM +0300, Alexey Dobriyan wrote: > 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 > Oh... Hm. Maybe we should drop this patch then. > 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. The last byte of the page argument seems possible: foo = kmalloc(); copy_from_user(foo, arg, sizeof(foo)); strlcpy(dest.str, foo->bar, sizeof(dest.str)); It's a very unlikely scenario. You have to be very unlucky to hit it at all. regards, dan carpenter ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: + lib-stringc-strlcpy-might-read-too-far.patch added to -mm tree 2014-04-15 11:18 ` Dan Carpenter @ 2014-04-15 11:36 ` Alexey Dobriyan 0 siblings, 0 replies; 3+ messages in thread From: Alexey Dobriyan @ 2014-04-15 11:36 UTC (permalink / raw) To: Dan Carpenter; +Cc: Linux Kernel, Vegard Nossum, Andrew Morton On Tue, Apr 15, 2014 at 2:18 PM, Dan Carpenter <dan.carpenter@oracle.com> wrote: > On Tue, Apr 15, 2014 at 01:49:38PM +0300, Alexey Dobriyan wrote: >> 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 >> > > Oh... Hm. Maybe we should drop this patch then. > >> 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. > > The last byte of the page argument seems possible: > > foo = kmalloc(); > copy_from_user(foo, arg, sizeof(foo)); Correct code would do foo->bar[sizeof(foo->bar)-1] = '\0'; if this field is a string. > strlcpy(dest.str, foo->bar, sizeof(dest.str)); ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-04-15 11:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <534c5757.VydWMxxcqkXGiwNa%akpm@linux-foundation.org>
2014-04-15 10:49 ` + lib-stringc-strlcpy-might-read-too-far.patch added to -mm tree Alexey Dobriyan
2014-04-15 11:18 ` Dan Carpenter
2014-04-15 11:36 ` Alexey Dobriyan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox