Skip to content

Commit 159590f

Browse files
lizf-osgregkh
authored andcommitted
shm: fix null pointer deref when userspace specifies invalid hugepage size
commit 091d0d5 upstream. 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 af73e4d ("hugetlbfs: fix mmap failure in unaligned size request"). Reported-by: Dave Jones <[email protected]> Signed-off-by: Li Zefan <[email protected]> Reviewed-by: Naoya Horiguchi <[email protected]> Acked-by: Rik van Riel <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7b44587 commit 159590f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

ipc/shm.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,13 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
493493
if (shmflg & SHM_HUGETLB) {
494494
struct hstate *hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT)
495495
& SHM_HUGE_MASK);
496-
size_t hugesize = ALIGN(size, huge_page_size(hs));
496+
size_t hugesize;
497+
498+
if (!hs) {
499+
error = -EINVAL;
500+
goto no_file;
501+
}
502+
hugesize = ALIGN(size, huge_page_size(hs));
497503

498504
/* hugetlb_file_setup applies strict accounting */
499505
if (shmflg & SHM_NORESERVE)

mm/mmap.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,9 +1331,13 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
13311331
len = ALIGN(len, huge_page_size(hstate_file(file)));
13321332
} else if (flags & MAP_HUGETLB) {
13331333
struct user_struct *user = NULL;
1334+
struct hstate *hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) &
1335+
SHM_HUGE_MASK);
13341336

1335-
len = ALIGN(len, huge_page_size(hstate_sizelog(
1336-
(flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK)));
1337+
if (!hs)
1338+
return -EINVAL;
1339+
1340+
len = ALIGN(len, huge_page_size(hs));
13371341
/*
13381342
* VM_NORESERVE is used because the reservations will be
13391343
* taken when vm_ops->mmap() is called

0 commit comments

Comments
 (0)