Skip to content

Commit d0290bc

Browse files
heicarsttorvalds
authored andcommitted
fs/proc/kcore.c: use probe_kernel_read() instead of memcpy()
Commit df04abf ("fs/proc/kcore.c: Add bounce buffer for ktext data") added a bounce buffer to avoid hardened usercopy checks. Copying to the bounce buffer was implemented with a simple memcpy() assuming that it is always valid to read from kernel memory iff the kern_addr_valid() check passed. A simple, but pointless, test case like "dd if=/proc/kcore of=/dev/null" now can easily crash the kernel, since the former execption handling on invalid kernel addresses now doesn't work anymore. Also adding a kern_addr_valid() implementation wouldn't help here. Most architectures simply return 1 here, while a couple implemented a page table walk to figure out if something is mapped at the address in question. With DEBUG_PAGEALLOC active mappings are established and removed all the time, so that relying on the result of kern_addr_valid() before executing the memcpy() also doesn't work. Therefore simply use probe_kernel_read() to copy to the bounce buffer. This also allows to simplify read_kcore(). At least on s390 this fixes the observed crashes and doesn't introduce warnings that were removed with df04abf ("fs/proc/kcore.c: Add bounce buffer for ktext data"), even though the generic probe_kernel_read() implementation uses uaccess functions. While looking into this I'm also wondering if kern_addr_valid() could be completely removed...(?) Link: http://lkml.kernel.org/r/20171202132739.99971-1-heiko.carstens@de.ibm.com Fixes: df04abf ("fs/proc/kcore.c: Add bounce buffer for ktext data") Fixes: f5509cc ("mm: Hardened usercopy") Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Acked-by: Kees Cook <keescook@chromium.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Al Viro <viro@ZenIV.linux.org.uk> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 171ef91 commit d0290bc

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

‎fs/proc/kcore.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,23 +512,15 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
512512
return -EFAULT;
513513
} else {
514514
if (kern_addr_valid(start)) {
515-
unsigned long n;
516-
517515
/*
518516
* Using bounce buffer to bypass the
519517
* hardened user copy kernel text checks.
520518
*/
521-
memcpy(buf, (char *) start, tsz);
522-
n = copy_to_user(buffer, buf, tsz);
523-
/*
524-
* We cannot distinguish between fault on source
525-
* and fault on destination. When this happens
526-
* we clear too and hope it will trigger the
527-
* EFAULT again.
528-
*/
529-
if (n) {
530-
if (clear_user(buffer + tsz - n,
531-
n))
519+
if (probe_kernel_read(buf, (void *) start, tsz)) {
520+
if (clear_user(buffer, tsz))
521+
return -EFAULT;
522+
} else {
523+
if (copy_to_user(buffer, buf, tsz))
532524
return -EFAULT;
533525
}
534526
} else {

0 commit comments

Comments
 (0)