Commit 091d0d55 authored by Li Zefan's avatar Li Zefan Committed by Linus Torvalds

shm: fix null pointer deref when userspace specifies invalid hugepage size

Dave reported an oops triggered by trinity:

  BUG: unable to handle kernel NULL pointer dereference at 0000000000000008
  IP: newseg+0x10d/0x390
  PGD cf8c1067 PUD cf8c2067 PMD 0
  Oops: 0000 [#1] PREEMPT SMP DEBUG_PAGEALLOC
  CPU: 2 PID: 7636 Comm: trinity-child2 Not tainted 3.9.0+#67
  ...
  Call Trace:
    ipcget+0x182/0x380
    SyS_shmget+0x5a/0x60
    tracesys+0xdd/0xe2

This bug was introduced by commit af73e4d9 ("hugetlbfs: fix mmap
failure in unaligned size request").
Reported-by: default avatarDave Jones <davej@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarLi Zefan <lizfan@huawei.com>
Reviewed-by: default avatarNaoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Acked-by: default avatarRik van Riel <riel@redhat.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent de2657f9
...@@ -493,7 +493,13 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -493,7 +493,13 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
if (shmflg & SHM_HUGETLB) { if (shmflg & SHM_HUGETLB) {
struct hstate *hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) struct hstate *hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT)
& SHM_HUGE_MASK); & SHM_HUGE_MASK);
size_t hugesize = ALIGN(size, huge_page_size(hs)); size_t hugesize;
if (!hs) {
error = -EINVAL;
goto no_file;
}
hugesize = ALIGN(size, huge_page_size(hs));
/* hugetlb_file_setup applies strict accounting */ /* hugetlb_file_setup applies strict accounting */
if (shmflg & SHM_NORESERVE) if (shmflg & SHM_NORESERVE)
......
...@@ -1367,9 +1367,13 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, ...@@ -1367,9 +1367,13 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
len = ALIGN(len, huge_page_size(hstate_file(file))); len = ALIGN(len, huge_page_size(hstate_file(file)));
} else if (flags & MAP_HUGETLB) { } else if (flags & MAP_HUGETLB) {
struct user_struct *user = NULL; struct user_struct *user = NULL;
struct hstate *hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) &
SHM_HUGE_MASK);
len = ALIGN(len, huge_page_size(hstate_sizelog( if (!hs)
(flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK))); return -EINVAL;
len = ALIGN(len, huge_page_size(hs));
/* /*
* VM_NORESERVE is used because the reservations will be * VM_NORESERVE is used because the reservations will be
* taken when vm_ops->mmap() is called * taken when vm_ops->mmap() is called
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment