linux-uconsole/arch
Jiri Olsa 45b0dfabab x86, 64-bit: Fix copy_[to/from]_user() checks for the userspace address limit
commit 26afb7c661 upstream.

As reported in BZ #30352:

  https://bugzilla.kernel.org/show_bug.cgi?id=30352

there's a kernel bug related to reading the last allowed page on x86_64.

The _copy_to_user() and _copy_from_user() functions use the following
check for address limit:

  if (buf + size >= limit)
	fail();

while it should be more permissive:

  if (buf + size > limit)
	fail();

That's because the size represents the number of bytes being
read/write from/to buf address AND including the buf address.
So the copy function will actually never touch the limit
address even if "buf + size == limit".

Following program fails to use the last page as buffer
due to the wrong limit check:

 #include <sys/mman.h>
 #include <sys/socket.h>
 #include <assert.h>

 #define PAGE_SIZE       (4096)
 #define LAST_PAGE       ((void*)(0x7fffffffe000))

 int main()
 {
        int fds[2], err;
        void * ptr = mmap(LAST_PAGE, PAGE_SIZE, PROT_READ | PROT_WRITE,
                          MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
        assert(ptr == LAST_PAGE);
        err = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
        assert(err == 0);
        err = send(fds[0], ptr, PAGE_SIZE, 0);
        perror("send");
        assert(err == PAGE_SIZE);
        err = recv(fds[1], ptr, PAGE_SIZE, MSG_WAITALL);
        perror("recv");
        assert(err == PAGE_SIZE);
        return 0;
 }

The other place checking the addr limit is the access_ok() function,
which is working properly. There's just a misleading comment
for the __range_not_ok() macro - which this patch fixes as well.

The last page of the user-space address range is a guard page and
Brian Gerst observed that the guard page itself due to an erratum on K8 cpus
(#121 Sequential Execution Across Non-Canonical Boundary Causes Processor
Hang).

However, the test code is using the last valid page before the guard page.
The bug is that the last byte before the guard page can't be read
because of the off-by-one error. The guard page is left in place.

This bug would normally not show up because the last page is
part of the process stack and never accessed via syscalls.

Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Acked-by: Brian Gerst <brgerst@gmail.com>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/1305210630-7136-1-git-send-email-jolsa@redhat.com
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2011-06-23 15:24:01 -07:00
..
alpha alpha: Fix printk format errors 2010-09-26 17:21:43 -07:00
arm ARM: 6891/1: prevent heap corruption in OABI semtimedop 2011-05-09 15:54:58 -07:00
avr32 untangle the do_mremap() mess 2010-01-18 10:19:11 -08:00
blackfin Blackfin: set ARCH_KMALLOC_MINALIGN 2010-07-05 11:10:50 -07:00
cris untangle the do_mremap() mess 2010-01-18 10:19:11 -08:00
frv frv: set ARCH_KMALLOC_MINALIGN 2010-07-05 11:10:49 -07:00
h8300 untangle the do_mremap() mess 2010-01-18 10:19:11 -08:00
ia64 mca.c: Fix cast from integer to pointer warning 2011-04-22 08:44:15 -07:00
m32r untangle the do_mremap() mess 2010-01-18 10:19:11 -08:00
m68k m68k/mm: Set all online nodes in N_NORMAL_MEMORY 2011-05-09 15:54:53 -07:00
m68knommu untangle the do_mremap() mess 2010-01-18 10:19:11 -08:00
microblaze microblaze: Fix build with make 3.82 2010-12-09 13:26:34 -08:00
mips MIPS: DMA: Fix computation of DMA flags from device's coherent_dma_mask. 2011-05-09 15:55:38 -07:00
mn10300 mn10300: set ARCH_KMALLOC_MINALIGN 2010-07-05 11:10:47 -07:00
parisc set memory ranges in N_NORMAL_MEMORY when onlined 2011-05-09 15:54:52 -07:00
powerpc powerpc/oprofile: Handle events that raise an exception without overflowing 2011-06-23 15:24:00 -07:00
s390 s390: remove task_show_regs 2011-03-02 09:46:49 -05:00
score untangle the do_mremap() mess 2010-01-18 10:19:11 -08:00
sh sh: Fix FDPIC binary loader 2010-04-26 07:41:17 -07:00
sparc sparc: Prevent no-handler signal syscall restart recursion. 2010-12-09 13:27:07 -08:00
um uml: disable winch irq before freeing handler data 2010-12-09 13:27:01 -08:00
x86 x86, 64-bit: Fix copy_[to/from]_user() checks for the userspace address limit 2011-06-23 15:24:01 -07:00
xtensa xtensa: set ARCH_KMALLOC_MINALIGN 2010-07-05 11:10:50 -07:00
.gitignore
Kconfig oprofile: remove tracing build dependency 2010-03-15 08:49:47 -07:00