Vulnerability: mm/memory.c in the Linux kernel before 4.1.4 mishandles anonymous pages, which allows local users to gain privileges or cause a denial of service (page tainting) via a crafted application that triggers writing to page zero.
Vulnerability ID: CVE-2015-3288
CWE: CWE-20
Severity score: 10
Project: https://github.com/torvalds/linux
Published: 2016-10-16
File: mm/memory.c
Commit message: mm: avoid setting up anonymous pages into file mapping

Reading page fault handler code I've noticed that under right
circumstances kernel would map anonymous pages into file mappings: if
the VMA doesn't have vm_ops->fault() and the VMA wasn't fully populated
on ->mmap(), kernel would handle page fault to not populated pte with
do_anonymous_page().

Let's change page fault handler to use do_anonymous_page() only on
anonymous VMA (->vm_ops == NULL) and make sure that the VMA is not
shared.

For file mappings without vm_ops->fault() or shred VMA without vm_ops,
page fault on pte_none() entry would lead to SIGBUS.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Willy Tarreau <w@1wt.eu>
Cc: stable@vger.kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Commit author: Kirill A. Shutemov

Vulnerable version (before fix):
 static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,  	pte_unmap(page_table);  	/* Check if we need to add a guard page to the stack */ 	if (check_stack_guard_page(vma, address) < 0) 		return VM_FAULT_SIGSEGV; static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;  	pte_unmap(page_table); 	if (!(flags & FAULT_FLAG_WRITE)) 		return do_read_fault(mm, vma, address, pmd, pgoff, flags, 				orig_pte); static int handle_pte_fault(struct mm_struct *mm, 	barrier(); 	if (!pte_present(entry)) { 		if (pte_none(entry)) { 			if (vma->vm_ops) { 				if (likely(vma->vm_ops->fault)) 					return do_fault(mm, vma, address, pte, 							pmd, flags, entry); 			} 			return do_anonymous_page(mm, vma, address, 						 pte, pmd, flags); 		} 		return do_swap_page(mm, vma, address, 					pte, pmd, flags, entry);

Fixed version (after fix):
 static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,  	pte_unmap(page_table);  	/* File mapping without ->vm_ops ? */ 	if (vma->vm_flags & VM_SHARED) 		return VM_FAULT_SIGBUS;  	/* Check if we need to add a guard page to the stack */ 	if (check_stack_guard_page(vma, address) < 0) 		return VM_FAULT_SIGSEGV; static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;  	pte_unmap(page_table); 	/* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ 	if (!vma->vm_ops->fault) 		return VM_FAULT_SIGBUS; 	if (!(flags & FAULT_FLAG_WRITE)) 		return do_read_fault(mm, vma, address, pmd, pgoff, flags, 				orig_pte); static int handle_pte_fault(struct mm_struct *mm, 	barrier(); 	if (!pte_present(entry)) { 		if (pte_none(entry)) { 			if (vma->vm_ops) 				return do_fault(mm, vma, address, pte, pmd, 						flags, entry);  			return do_anonymous_page(mm, vma, address, pte, pmd, 					flags); 		} 		return do_swap_page(mm, vma, address, 					pte, pmd, flags, entry);

Patch:
patch: @@ -2670,6 +2670,10 @@ static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
 
 	pte_unmap(page_table);
 
+	/* File mapping without ->vm_ops ? */
+	if (vma->vm_flags & VM_SHARED)
+		return VM_FAULT_SIGBUS;
+
 	/* Check if we need to add a guard page to the stack */
 	if (check_stack_guard_page(vma, address) < 0)
 		return VM_FAULT_SIGSEGV;
@@ -3099,6 +3103,9 @@ static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
 
 	pte_unmap(page_table);
+	/* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
+	if (!vma->vm_ops->fault)
+		return VM_FAULT_SIGBUS;
 	if (!(flags & FAULT_FLAG_WRITE))
 		return do_read_fault(mm, vma, address, pmd, pgoff, flags,
 				orig_pte);
@@ -3244,13 +3251,12 @@ static int handle_pte_fault(struct mm_struct *mm,
 	barrier();
 	if (!pte_present(entry)) {
 		if (pte_none(entry)) {
-			if (vma->vm_ops) {
-				if (likely(vma->vm_ops->fault))
-					return do_fault(mm, vma, address, pte,
-							pmd, flags, entry);
-			}
-			return do_anonymous_page(mm, vma, address,
-						 pte, pmd, flags);
+			if (vma->vm_ops)
+				return do_fault(mm, vma, address, pte, pmd,
+						flags, entry);
+
+			return do_anonymous_page(mm, vma, address, pte, pmd,
+					flags);
 		}
 		return do_swap_page(mm, vma, address,
 					pte, pmd, flags, entry);
path: mm/memory.c