Skip to contentSkip to conversation

mm: fix use-after-free in sys_remap_file_pages

PATCH In review Kees Cook 4 participants 5 messages Dec 12, 2013 – Dec 13, 2013 Memory management

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 mm: fix use-after-free in sys_remap_file_pages

From: PaX Team <[email protected]>

http://lkml.org/lkml/2013/9/17/30

SyS_remap_file_pages() calls mmap_region(), which calls remove_vma_list(), which calls remove_vma(), which frees the vma. Later (after out label) SyS_remap_file_pages() accesses the freed vma in vm_flags = vma->vm_flags.

---
 mm/fremap.c |    2 ++
 1 file changed, 2 insertions(+)
mm/fremap.c +2 -0
index 5bff08147768..afad07b85ef2 100644
@@ -218,6 +218,8 @@ get_write_lock:
 				BUG_ON(addr != start);
 				err = 0;
 			}
+			vm_flags = vma->vm_flags;
+			vma = NULL;
 			goto out;
 		}
 		mutex_lock(&mapping->i_mmap_mutex);
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw
Quoted 12 lines · On Thu, 12 Dec 2013 14:07:57 -0800 Kees Cook <[email protected]> wrote:

From: PaX Team <[email protected]>

http://lkml.org/lkml/2013/9/17/30

SyS_remap_file_pages() calls mmap_region(), which calls remove_vma_list(), which calls remove_vma(), which frees the vma. Later (after out label) SyS_remap_file_pages() accesses the freed vma in vm_flags = vma->vm_flags.

index 5bff08147768..afad07b85ef2 100644

mm/fremap.c get_write_lock: · line 223

Quoted 6 lines
mm/fremap.c +2 -0
@@ -218,6 +218,8 @@ get_write_lock:
 				BUG_ON(addr != start);
 				err = 0;
 			}
+			vm_flags = vma->vm_flags;
+			vma = NULL;
 			goto out;

If the vma has been freed by the time the code jumps to the out label (because it was freed by a function called from mmap_region), surely it will also already have been freed by the time this patch dereferences it?

Also, setting vma = NULL to avoid the if (vma) branch at the out: label is unnecessarily obfuscated. Lets make things clear by documenting what is going on, and having a label after that dereference.

Maybe something like this patch:

---8<---

Subject: [PATCH] mm: fix use-after-free in sys_remap_file_pages

remap_file_pages calls mmap_region, which may merge the VMA with other existing VMAs, and free "vma". This can lead to a use-after-free bug. Avoid the bug by remembering vm_flags before calling mmap_region, and not trying to dereference vma later.

---
 mm/fremap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/fremap.c +4 -1
index 87da359..d28ede5 100644
@@ -203,6 +203,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 		if (mapping_cap_account_dirty(mapping)) {
 			unsigned long addr;
 			struct file *file = get_file(vma->vm_file);
+			vm_flags = vma->vm_flags;
 
 			addr = mmap_region(file, start, size,
 					vma->vm_flags, pgoff);
@@ -213,7 +214,8 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 				BUG_ON(addr != start);
 				err = 0;
 			}
-			goto out;
+			/* mmap_region may have freed vma */
+			goto out_freed;
 		}
 		mutex_lock(&mapping->i_mmap_mutex);
 		flush_dcache_mmap_lock(mapping);
@@ -248,6 +250,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 out:
 	if (vma)
 		vm_flags = vma->vm_flags;
+out_freed:
 	if (likely(!has_write_lock))
 		up_read(&mm->mmap_sem);
 	else
#2 lore raw
Quoted 10 lines · Rik van Riel

If the vma has been freed by the time the code jumps to the out label (because it was freed by a function called from mmap_region), surely it will also already have been freed by the time this patch dereferences it?

Also, setting vma = NULL to avoid the if (vma) branch at the out: label is unnecessarily obfuscated. Lets make things clear by documenting what is going on, and having a label after that dereference.

This patch is a bit easier to read, at least for me. And if I understand the code flow right, the issue is due to remap_file_pages -> mmap_region -> find_vma_links -> do_munmap.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
In reply to Rik van Riel
Quoted 4 lines · Rik van Riel

If the vma has been freed by the time the code jumps to the out label (because it was freed by a function called from mmap_region), surely it will also already have been freed by the time this patch dereferences it?

oops, yes, i meant to save the flags away before mmap_region, no idea how i ended up with this ;).

Quoted 4 lines

Also, setting vma = NULL to avoid the if (vma) branch at the out: label is unnecessarily obfuscated. Lets make things clear by documenting what is going on, and having a label after that dereference.

on that note, how about this as well:

mm/fremap.c SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, · line 209

Quoted 10 lines
mm/fremap.c +1 -0
@@ -203,6 +203,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 		if (mapping_cap_account_dirty(mapping)) {
 			unsigned long addr;
 			struct file *file = get_file(vma->vm_file);
+			vm_flags = vma->vm_flags;
 
 			addr = mmap_region(file, start, size,
 					vma->vm_flags, pgoff);
                                    ^^^^^^^^^^^^^

pass in vm_flags instead of vma->vm_flags just to prevent someone from 'optimizing' away the read in the future?

mm/fremap.c SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, · line 218

Quoted 7 lines
mm/fremap.c +2 -1
@@ -213,7 +214,8 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 				BUG_ON(addr != start);
 				err = 0;
 			}
-			goto out;
+			/* mmap_region may have freed vma */
+			goto out_freed;

perhaps {copy,move} this comment above the previous hunk since that's where the relevant action is?

cheers,
PaX Team

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

PATCH 1/1 mm: fix use-after-free in sys_remap_file_pages

Quoted 2 lines · On Fri, 13 Dec 2013 12:10:17 +0100 "PaX Team" <[email protected]> wrote:

pass in vm_flags instead of vma->vm_flags just to prevent someone from 'optimizing' away the read in the future?

In that case, we should probably also use ACCESS_ONCE, if only to be explicit about it.

Quoted 2 lines

perhaps {copy,move} this comment above the previous hunk since that's where the relevant action is?

See the new version below:

---8<---
Subject: mm: fix use-after-free in sys_remap_file_pages

remap_file_pages calls mmap_region, which may merge the VMA with other existing VMAs, and free "vma". This can lead to a use-after-free bug. Avoid the bug by remembering vm_flags before calling mmap_region, and not trying to dereference vma later.

---
 mm/fremap.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/fremap.c +5 -3
index 87da359..c85e2ec 100644
@@ -203,9 +203,10 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 		if (mapping_cap_account_dirty(mapping)) {
 			unsigned long addr;
 			struct file *file = get_file(vma->vm_file);
+			/* mmap_region may free vma; grab the info now */
+			vm_flags = ACCESS_ONCE(vma->vm_flags);
 
-			addr = mmap_region(file, start, size,
-					vma->vm_flags, pgoff);
+			addr = mmap_region(file, start, size, vm_flags, pgoff);
 			fput(file);
 			if (IS_ERR_VALUE(addr)) {
 				err = addr;
@@ -213,7 +214,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 				BUG_ON(addr != start);
 				err = 0;
 			}
-			goto out;
+			goto out_freed;
 		}
 		mutex_lock(&mapping->i_mmap_mutex);
 		flush_dcache_mmap_lock(mapping);
@@ -248,6 +249,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 out:
 	if (vma)
 		vm_flags = vma->vm_flags;
+out_freed:
 	if (likely(!has_write_lock))
 		up_read(&mm->mmap_sem);
 	else