diff -urN 2.3.99-pre4-pre2/arch/alpha/mm/fault.c user-stack-overflow/arch/alpha/mm/fault.c --- 2.3.99-pre4-pre2/arch/alpha/mm/fault.c Fri Mar 3 02:19:23 2000 +++ user-stack-overflow/arch/alpha/mm/fault.c Sat Apr 1 18:27:55 2000 @@ -115,7 +115,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.3.99-pre4-pre2/arch/i386/mm/fault.c user-stack-overflow/arch/i386/mm/fault.c --- 2.3.99-pre4-pre2/arch/i386/mm/fault.c Sun Jan 30 15:43:27 2000 +++ user-stack-overflow/arch/i386/mm/fault.c Sat Apr 1 18:28:37 2000 @@ -30,13 +30,13 @@ */ int __verify_write(const void * addr, unsigned long size) { - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; unsigned long start = (unsigned long) addr; if (!size) return 1; - vma = find_vma(current->mm, start); + vma = find_vma_prev(current->mm, start, &prev_vma); if (!vma) goto bad_area; if (vma->vm_start > start) @@ -70,7 +70,7 @@ check_stack: if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, start) == 0) + if (expand_stack(vma, start, prev_vma) == 0) goto good_area; bad_area: @@ -119,7 +119,7 @@ { struct task_struct *tsk; struct mm_struct *mm; - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; unsigned long address; unsigned long page; unsigned long fixup; @@ -141,7 +141,7 @@ down(&mm->mmap_sem); - vma = find_vma(mm, address); + vma = find_vma_prev(mm, address, &prev_vma); if (!vma) goto bad_area; if (vma->vm_start <= address) @@ -158,7 +158,7 @@ if (address + 32 < regs->esp) goto bad_area; } - if (expand_stack(vma, address)) + if (expand_stack(vma, address, prev_vma)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.3.99-pre4-pre2/arch/ppc/mm/fault.c user-stack-overflow/arch/ppc/mm/fault.c --- 2.3.99-pre4-pre2/arch/ppc/mm/fault.c Fri Mar 31 05:30:19 2000 +++ user-stack-overflow/arch/ppc/mm/fault.c Sat Apr 1 18:27:55 2000 @@ -60,7 +60,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long address, unsigned long error_code) { - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; struct mm_struct *mm = current->mm; #if defined(CONFIG_4xx) int is_write = error_code & ESR_DST; @@ -96,14 +96,14 @@ return; } down(&mm->mmap_sem); - vma = find_vma(mm, address); + vma = find_vma_prev(mm, address, &prev_vma); if (!vma) goto bad_area; if (vma->vm_start <= address) goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, prev_vma)) goto bad_area; good_area: diff -urN 2.3.99-pre4-pre2/include/linux/mm.h user-stack-overflow/include/linux/mm.h --- 2.3.99-pre4-pre2/include/linux/mm.h Fri Mar 31 05:30:20 2000 +++ user-stack-overflow/include/linux/mm.h Sat Apr 1 18:27:55 2000 @@ -491,13 +491,18 @@ #define GFP_HIGHMEM __GFP_HIGHMEM +extern int heap_stack_gap; + /* vma is the first one with address < vma->vm_end, * and even address < vma->vm_start. Have to extend vma. */ -static inline int expand_stack(struct vm_area_struct * vma, unsigned long address) +static inline int expand_stack(struct vm_area_struct * vma, unsigned long address, + struct vm_area_struct * prev_vma) { unsigned long grow; address &= PAGE_MASK; + if (prev_vma && prev_vma->vm_end + (heap_stack_gap << PAGE_SHIFT) > address) + return -ENOMEM; grow = (vma->vm_start - address) >> PAGE_SHIFT; if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur || ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur) diff -urN 2.3.99-pre4-pre2/include/linux/sysctl.h user-stack-overflow/include/linux/sysctl.h --- 2.3.99-pre4-pre2/include/linux/sysctl.h Sat Mar 11 20:02:33 2000 +++ user-stack-overflow/include/linux/sysctl.h Sat Apr 1 18:27:55 2000 @@ -127,7 +127,8 @@ VM_PAGECACHE=7, /* struct: Set cache memory thresholds */ VM_PAGERDAEMON=8, /* struct: Control kswapd behaviour */ VM_PGT_CACHE=9, /* struct: Set page table cache parameters */ - VM_PAGE_CLUSTER=10 /* int: set number of pages to swap together */ + VM_PAGE_CLUSTER=10, /* int: set number of pages to swap together */ + VM_HEAP_STACK_GAP=11, /* int: page gap between heap and stack */ }; diff -urN 2.3.99-pre4-pre2/kernel/sysctl.c user-stack-overflow/kernel/sysctl.c --- 2.3.99-pre4-pre2/kernel/sysctl.c Mon Mar 27 22:44:50 2000 +++ user-stack-overflow/kernel/sysctl.c Sat Apr 1 18:27:55 2000 @@ -243,6 +243,8 @@ &pgt_cache_water, 2*sizeof(int), 0600, NULL, &proc_dointvec}, {VM_PAGE_CLUSTER, "page-cluster", &page_cluster, sizeof(int), 0600, NULL, &proc_dointvec}, + {VM_HEAP_STACK_GAP, "heap-stack-gap", + &heap_stack_gap, sizeof(int), 0644, NULL, &proc_dointvec}, {0} }; diff -urN 2.3.99-pre4-pre2/mm/memory.c user-stack-overflow/mm/memory.c --- 2.3.99-pre4-pre2/mm/memory.c Mon Mar 27 22:44:50 2000 +++ user-stack-overflow/mm/memory.c Sat Apr 1 18:28:57 2000 @@ -457,13 +457,14 @@ */ while (ptr < end) { if (!vma || ptr >= vma->vm_end) { - vma = find_vma(current->mm, ptr); + struct vm_area_struct * prev_vma; + vma = find_vma_prev(current->mm, ptr, &prev_vma); if (!vma) goto out_unlock; if (vma->vm_start > ptr) { if (!(vma->vm_flags & VM_GROWSDOWN)) goto out_unlock; - if (expand_stack(vma, ptr)) + if (expand_stack(vma, ptr, prev_vma)) goto out_unlock; } if (((datain) && (!(vma->vm_flags & VM_WRITE))) || diff -urN 2.3.99-pre4-pre2/mm/mmap.c user-stack-overflow/mm/mmap.c --- 2.3.99-pre4-pre2/mm/mmap.c Mon Mar 27 22:44:50 2000 +++ user-stack-overflow/mm/mmap.c Sat Apr 1 18:27:55 2000 @@ -40,6 +40,7 @@ kmem_cache_t *vm_area_cachep; int sysctl_overcommit_memory; +int heap_stack_gap = 128; /* Check that a process has enough memory to allocate a * new virtual mapping. @@ -365,9 +366,14 @@ for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) { /* At this point: (!vmm || addr < vmm->vm_end). */ + unsigned long __heap_stack_gap = 0; if (TASK_SIZE - len < addr) return 0; - if (!vmm || addr + len <= vmm->vm_start) + if (!vmm) + return addr; + if (vmm->vm_flags & VM_GROWSDOWN) + __heap_stack_gap = heap_stack_gap << PAGE_SHIFT; + if (addr + len + __heap_stack_gap <= vmm->vm_start) return addr; addr = vmm->vm_end; } @@ -470,11 +476,11 @@ struct vm_area_struct * find_extend_vma(struct task_struct * tsk, unsigned long addr) { - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; unsigned long start; addr &= PAGE_MASK; - vma = find_vma(tsk->mm,addr); + vma = find_vma_prev(tsk->mm,addr, &prev_vma); if (!vma) return NULL; if (vma->vm_start <= addr) @@ -482,7 +488,7 @@ if (!(vma->vm_flags & VM_GROWSDOWN)) return NULL; start = vma->vm_start; - if (expand_stack(vma, addr)) + if (expand_stack(vma, addr, prev_vma)) return NULL; if (vma->vm_flags & VM_LOCKED) { make_pages_present(addr, start);