Commit f440fa1a authored by Liam R. Howlett's avatar Liam R. Howlett Committed by Linus Torvalds

mm: make find_extend_vma() fail if write lock not held

Make calls to extend_vma() and find_extend_vma() fail if the write lock
is required.

To avoid making this a flag-day event, this still allows the old
read-locking case for the trivial situations, and passes in a flag to
say "is it write-locked".  That way write-lockers can say "yes, I'm
being careful", and legacy users will continue to work in all the common
cases until they have been fully converted to the new world order.
Co-Developed-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: default avatarLiam R. Howlett <Liam.Howlett@oracle.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 2cd76c50
...@@ -320,10 +320,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, ...@@ -320,10 +320,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
* Grow the stack manually; some architectures have a limit on how * Grow the stack manually; some architectures have a limit on how
* far ahead a user-space access may be in order to grow the stack. * far ahead a user-space access may be in order to grow the stack.
*/ */
if (mmap_read_lock_killable(mm)) if (mmap_write_lock_killable(mm))
return -EINTR; return -EINTR;
vma = find_extend_vma(mm, bprm->p); vma = find_extend_vma_locked(mm, bprm->p, true);
mmap_read_unlock(mm); mmap_write_unlock(mm);
if (!vma) if (!vma)
return -EFAULT; return -EFAULT;
......
...@@ -205,7 +205,8 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, ...@@ -205,7 +205,8 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
#ifdef CONFIG_STACK_GROWSUP #ifdef CONFIG_STACK_GROWSUP
if (write) { if (write) {
ret = expand_downwards(bprm->vma, pos); /* We claim to hold the lock - nobody to race with */
ret = expand_downwards(bprm->vma, pos, true);
if (ret < 0) if (ret < 0)
return NULL; return NULL;
} }
...@@ -853,7 +854,7 @@ int setup_arg_pages(struct linux_binprm *bprm, ...@@ -853,7 +854,7 @@ int setup_arg_pages(struct linux_binprm *bprm,
stack_base = vma->vm_end - stack_expand; stack_base = vma->vm_end - stack_expand;
#endif #endif
current->mm->start_stack = bprm->p; current->mm->start_stack = bprm->p;
ret = expand_stack(vma, stack_base); ret = expand_stack_locked(vma, stack_base, true);
if (ret) if (ret)
ret = -EFAULT; ret = -EFAULT;
......
...@@ -3192,11 +3192,13 @@ extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf); ...@@ -3192,11 +3192,13 @@ extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf);
extern unsigned long stack_guard_gap; extern unsigned long stack_guard_gap;
/* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */ /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
extern int expand_stack(struct vm_area_struct *vma, unsigned long address); int expand_stack_locked(struct vm_area_struct *vma, unsigned long address,
bool write_locked);
#define expand_stack(vma,addr) expand_stack_locked(vma,addr,false)
/* CONFIG_STACK_GROWSUP still needs to grow downwards at some places */ /* CONFIG_STACK_GROWSUP still needs to grow downwards at some places */
extern int expand_downwards(struct vm_area_struct *vma, int expand_downwards(struct vm_area_struct *vma, unsigned long address,
unsigned long address); bool write_locked);
#if VM_GROWSUP #if VM_GROWSUP
extern int expand_upwards(struct vm_area_struct *vma, unsigned long address); extern int expand_upwards(struct vm_area_struct *vma, unsigned long address);
#else #else
...@@ -3297,6 +3299,8 @@ unsigned long change_prot_numa(struct vm_area_struct *vma, ...@@ -3297,6 +3299,8 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
#endif #endif
struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr); struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
struct vm_area_struct *find_extend_vma_locked(struct mm_struct *,
unsigned long addr, bool write_locked);
int remap_pfn_range(struct vm_area_struct *, unsigned long addr, int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
unsigned long pfn, unsigned long size, pgprot_t); unsigned long pfn, unsigned long size, pgprot_t);
int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
......
...@@ -5368,7 +5368,7 @@ struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, ...@@ -5368,7 +5368,7 @@ struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
goto fail; goto fail;
} }
if (expand_stack(vma, addr)) if (expand_stack_locked(vma, addr, true))
goto fail; goto fail;
success: success:
......
...@@ -1935,7 +1935,8 @@ static int acct_stack_growth(struct vm_area_struct *vma, ...@@ -1935,7 +1935,8 @@ static int acct_stack_growth(struct vm_area_struct *vma,
* PA-RISC uses this for its stack; IA64 for its Register Backing Store. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
* vma is the last one with address > vma->vm_end. Have to extend vma. * vma is the last one with address > vma->vm_end. Have to extend vma.
*/ */
int expand_upwards(struct vm_area_struct *vma, unsigned long address) int expand_upwards(struct vm_area_struct *vma, unsigned long address,
bool write_locked)
{ {
struct mm_struct *mm = vma->vm_mm; struct mm_struct *mm = vma->vm_mm;
struct vm_area_struct *next; struct vm_area_struct *next;
...@@ -1959,6 +1960,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) ...@@ -1959,6 +1960,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
if (gap_addr < address || gap_addr > TASK_SIZE) if (gap_addr < address || gap_addr > TASK_SIZE)
gap_addr = TASK_SIZE; gap_addr = TASK_SIZE;
if (!write_locked)
return -EAGAIN;
next = find_vma_intersection(mm, vma->vm_end, gap_addr); next = find_vma_intersection(mm, vma->vm_end, gap_addr);
if (next && vma_is_accessible(next)) { if (next && vma_is_accessible(next)) {
if (!(next->vm_flags & VM_GROWSUP)) if (!(next->vm_flags & VM_GROWSUP))
...@@ -2028,7 +2031,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) ...@@ -2028,7 +2031,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
/* /*
* vma is the first one with address < vma->vm_start. Have to extend vma. * vma is the first one with address < vma->vm_start. Have to extend vma.
*/ */
int expand_downwards(struct vm_area_struct *vma, unsigned long address) int expand_downwards(struct vm_area_struct *vma, unsigned long address,
bool write_locked)
{ {
struct mm_struct *mm = vma->vm_mm; struct mm_struct *mm = vma->vm_mm;
MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start);
...@@ -2042,10 +2046,13 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) ...@@ -2042,10 +2046,13 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
/* Enforce stack_guard_gap */ /* Enforce stack_guard_gap */
prev = mas_prev(&mas, 0); prev = mas_prev(&mas, 0);
/* Check that both stack segments have the same anon_vma? */ /* Check that both stack segments have the same anon_vma? */
if (prev && !(prev->vm_flags & VM_GROWSDOWN) && if (prev) {
vma_is_accessible(prev)) { if (!(prev->vm_flags & VM_GROWSDOWN) &&
if (address - prev->vm_end < stack_guard_gap) vma_is_accessible(prev) &&
(address - prev->vm_end < stack_guard_gap))
return -ENOMEM; return -ENOMEM;
if (!write_locked && (prev->vm_end == address))
return -EAGAIN;
} }
if (mas_preallocate(&mas, GFP_KERNEL)) if (mas_preallocate(&mas, GFP_KERNEL))
...@@ -2124,13 +2131,14 @@ static int __init cmdline_parse_stack_guard_gap(char *p) ...@@ -2124,13 +2131,14 @@ static int __init cmdline_parse_stack_guard_gap(char *p)
__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
#ifdef CONFIG_STACK_GROWSUP #ifdef CONFIG_STACK_GROWSUP
int expand_stack(struct vm_area_struct *vma, unsigned long address) int expand_stack_locked(struct vm_area_struct *vma, unsigned long address,
bool write_locked)
{ {
return expand_upwards(vma, address); return expand_upwards(vma, address, write_locked);
} }
struct vm_area_struct * struct vm_area_struct *find_extend_vma_locked(struct mm_struct *mm,
find_extend_vma(struct mm_struct *mm, unsigned long addr) unsigned long addr, bool write_locked)
{ {
struct vm_area_struct *vma, *prev; struct vm_area_struct *vma, *prev;
...@@ -2138,20 +2146,25 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr) ...@@ -2138,20 +2146,25 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr)
vma = find_vma_prev(mm, addr, &prev); vma = find_vma_prev(mm, addr, &prev);
if (vma && (vma->vm_start <= addr)) if (vma && (vma->vm_start <= addr))
return vma; return vma;
if (!prev || expand_stack(prev, addr)) if (!prev)
return NULL;
if (expand_stack_locked(prev, addr, write_locked))
return NULL; return NULL;
if (prev->vm_flags & VM_LOCKED) if (prev->vm_flags & VM_LOCKED)
populate_vma_page_range(prev, addr, prev->vm_end, NULL); populate_vma_page_range(prev, addr, prev->vm_end, NULL);
return prev; return prev;
} }
#else #else
int expand_stack(struct vm_area_struct *vma, unsigned long address) int expand_stack_locked(struct vm_area_struct *vma, unsigned long address,
bool write_locked)
{ {
return expand_downwards(vma, address); if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
return -EINVAL;
return expand_downwards(vma, address, write_locked);
} }
struct vm_area_struct * struct vm_area_struct *find_extend_vma_locked(struct mm_struct *mm,
find_extend_vma(struct mm_struct *mm, unsigned long addr) unsigned long addr, bool write_locked)
{ {
struct vm_area_struct *vma; struct vm_area_struct *vma;
unsigned long start; unsigned long start;
...@@ -2162,10 +2175,8 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr) ...@@ -2162,10 +2175,8 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr)
return NULL; return NULL;
if (vma->vm_start <= addr) if (vma->vm_start <= addr)
return vma; return vma;
if (!(vma->vm_flags & VM_GROWSDOWN))
return NULL;
start = vma->vm_start; start = vma->vm_start;
if (expand_stack(vma, addr)) if (expand_stack_locked(vma, addr, write_locked))
return NULL; return NULL;
if (vma->vm_flags & VM_LOCKED) if (vma->vm_flags & VM_LOCKED)
populate_vma_page_range(vma, addr, start, NULL); populate_vma_page_range(vma, addr, start, NULL);
...@@ -2173,6 +2184,11 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr) ...@@ -2173,6 +2184,11 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr)
} }
#endif #endif
struct vm_area_struct *find_extend_vma(struct mm_struct *mm,
unsigned long addr)
{
return find_extend_vma_locked(mm, addr, false);
}
EXPORT_SYMBOL_GPL(find_extend_vma); EXPORT_SYMBOL_GPL(find_extend_vma);
/* /*
......
...@@ -643,7 +643,8 @@ struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) ...@@ -643,7 +643,8 @@ struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
* expand a stack to a given address * expand a stack to a given address
* - not supported under NOMMU conditions * - not supported under NOMMU conditions
*/ */
int expand_stack(struct vm_area_struct *vma, unsigned long address) int expand_stack_locked(struct vm_area_struct *vma, unsigned long address,
bool write_locked)
{ {
return -ENOMEM; return -ENOMEM;
} }
......
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