Skip to content

Commit b6a2fea

Browse files
Ollie WildLinus Torvalds
authored andcommitted
mm: variable length argument support
Remove the arg+env limit of MAX_ARG_PAGES by copying the strings directly from the old mm into the new mm. We create the new mm before the binfmt code runs, and place the new stack at the very top of the address space. Once the binfmt code runs and figures out where the stack should be, we move it downwards. It is a bit peculiar in that we have one task with two mm's, one of which is inactive. [a.p.zijlstra@chello.nl: limit stack size] Signed-off-by: Ollie Wild <aaw@google.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: <linux-arch@vger.kernel.org> Cc: Hugh Dickins <hugh@veritas.com> [bunk@stusta.de: unexport bprm_mm_init] Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bdf4c48 commit b6a2fea

15 files changed

Lines changed: 554 additions & 453 deletions

File tree

‎arch/ia64/ia32/binfmt_elf32.c‎

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -195,62 +195,27 @@ ia64_elf32_init (struct pt_regs *regs)
195195
ia32_load_state(current);
196196
}
197197

198+
/*
199+
* Undo the override of setup_arg_pages() without this ia32_setup_arg_pages()
200+
* will suffer infinite self recursion.
201+
*/
202+
#undef setup_arg_pages
203+
198204
int
199205
ia32_setup_arg_pages (struct linux_binprm *bprm, int executable_stack)
200206
{
201-
unsigned long stack_base;
202-
struct vm_area_struct *mpnt;
203-
struct mm_struct *mm = current->mm;
204-
int i, ret;
205-
206-
stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
207-
mm->arg_start = bprm->p + stack_base;
208-
209-
bprm->p += stack_base;
210-
if (bprm->loader)
211-
bprm->loader += stack_base;
212-
bprm->exec += stack_base;
213-
214-
mpnt = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
215-
if (!mpnt)
216-
return -ENOMEM;
217-
218-
down_write(&current->mm->mmap_sem);
219-
{
220-
mpnt->vm_mm = current->mm;
221-
mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
222-
mpnt->vm_end = IA32_STACK_TOP;
223-
if (executable_stack == EXSTACK_ENABLE_X)
224-
mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
225-
else if (executable_stack == EXSTACK_DISABLE_X)
226-
mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
227-
else
228-
mpnt->vm_flags = VM_STACK_FLAGS;
229-
mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC)?
230-
PAGE_COPY_EXEC: PAGE_COPY;
231-
if ((ret = insert_vm_struct(current->mm, mpnt))) {
232-
up_write(&current->mm->mmap_sem);
233-
kmem_cache_free(vm_area_cachep, mpnt);
234-
return ret;
235-
}
236-
current->mm->stack_vm = current->mm->total_vm = vma_pages(mpnt);
207+
int ret;
208+
209+
ret = setup_arg_pages(bprm, IA32_STACK_TOP, executable_stack);
210+
if (!ret) {
211+
/*
212+
* Can't do it in ia64_elf32_init(). Needs to be done before
213+
* calls to elf32_map()
214+
*/
215+
current->thread.ppl = ia32_init_pp_list();
237216
}
238217

239-
for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
240-
struct page *page = bprm->page[i];
241-
if (page) {
242-
bprm->page[i] = NULL;
243-
install_arg_page(mpnt, page, stack_base);
244-
}
245-
stack_base += PAGE_SIZE;
246-
}
247-
up_write(&current->mm->mmap_sem);
248-
249-
/* Can't do it in ia64_elf32_init(). Needs to be done before calls to
250-
elf32_map() */
251-
current->thread.ppl = ia32_init_pp_list();
252-
253-
return 0;
218+
return ret;
254219
}
255220

256221
static void

‎arch/x86_64/ia32/ia32_aout.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
404404

405405
set_brk(current->mm->start_brk, current->mm->brk);
406406

407-
retval = ia32_setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
407+
retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
408408
if (retval < 0) {
409409
/* Someone check-me: is this error path enough? */
410410
send_sig(SIGKILL, current, 0);

‎arch/x86_64/ia32/ia32_binfmt.c‎

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,6 @@ do { \
232232
#define load_elf_binary load_elf32_binary
233233

234234
#define ELF_PLAT_INIT(r, load_addr) elf32_init(r)
235-
#define setup_arg_pages(bprm, stack_top, exec_stack) \
236-
ia32_setup_arg_pages(bprm, stack_top, exec_stack)
237-
int ia32_setup_arg_pages(struct linux_binprm *bprm, unsigned long stack_top, int executable_stack);
238235

239236
#undef start_thread
240237
#define start_thread(regs,new_rip,new_rsp) do { \
@@ -286,61 +283,6 @@ static void elf32_init(struct pt_regs *regs)
286283
me->thread.es = __USER_DS;
287284
}
288285

289-
int ia32_setup_arg_pages(struct linux_binprm *bprm, unsigned long stack_top,
290-
int executable_stack)
291-
{
292-
unsigned long stack_base;
293-
struct vm_area_struct *mpnt;
294-
struct mm_struct *mm = current->mm;
295-
int i, ret;
296-
297-
stack_base = stack_top - MAX_ARG_PAGES * PAGE_SIZE;
298-
mm->arg_start = bprm->p + stack_base;
299-
300-
bprm->p += stack_base;
301-
if (bprm->loader)
302-
bprm->loader += stack_base;
303-
bprm->exec += stack_base;
304-
305-
mpnt = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
306-
if (!mpnt)
307-
return -ENOMEM;
308-
309-
down_write(&mm->mmap_sem);
310-
{
311-
mpnt->vm_mm = mm;
312-
mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
313-
mpnt->vm_end = stack_top;
314-
if (executable_stack == EXSTACK_ENABLE_X)
315-
mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
316-
else if (executable_stack == EXSTACK_DISABLE_X)
317-
mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
318-
else
319-
mpnt->vm_flags = VM_STACK_FLAGS;
320-
mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC) ?
321-
PAGE_COPY_EXEC : PAGE_COPY;
322-
if ((ret = insert_vm_struct(mm, mpnt))) {
323-
up_write(&mm->mmap_sem);
324-
kmem_cache_free(vm_area_cachep, mpnt);
325-
return ret;
326-
}
327-
mm->stack_vm = mm->total_vm = vma_pages(mpnt);
328-
}
329-
330-
for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
331-
struct page *page = bprm->page[i];
332-
if (page) {
333-
bprm->page[i] = NULL;
334-
install_arg_page(mpnt, page, stack_base);
335-
}
336-
stack_base += PAGE_SIZE;
337-
}
338-
up_write(&mm->mmap_sem);
339-
340-
return 0;
341-
}
342-
EXPORT_SYMBOL(ia32_setup_arg_pages);
343-
344286
#ifdef CONFIG_SYSCTL
345287
/* Register vsyscall32 into the ABI table */
346288
#include <linux/sysctl.h>

‎fs/binfmt_elf.c‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
148148
elf_addr_t *elf_info;
149149
int ei_index = 0;
150150
struct task_struct *tsk = current;
151+
struct vm_area_struct *vma;
151152

152153
/*
153154
* If this architecture has a platform capability string, copy it
@@ -234,6 +235,15 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
234235
sp = (elf_addr_t __user *)bprm->p;
235236
#endif
236237

238+
239+
/*
240+
* Grow the stack manually; some architectures have a limit on how
241+
* far ahead a user-space access may be in order to grow the stack.
242+
*/
243+
vma = find_extend_vma(current->mm, bprm->p);
244+
if (!vma)
245+
return -EFAULT;
246+
237247
/* Now, let's put argc (and argv, envp if appropriate) on the stack */
238248
if (__put_user(argc, sp++))
239249
return -EFAULT;
@@ -254,8 +264,8 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
254264
size_t len;
255265
if (__put_user((elf_addr_t)p, argv++))
256266
return -EFAULT;
257-
len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
258-
if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
267+
len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
268+
if (!len || len > MAX_ARG_STRLEN)
259269
return 0;
260270
p += len;
261271
}
@@ -266,8 +276,8 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
266276
size_t len;
267277
if (__put_user((elf_addr_t)p, envp++))
268278
return -EFAULT;
269-
len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
270-
if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
279+
len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
280+
if (!len || len > MAX_ARG_STRLEN)
271281
return 0;
272282
p += len;
273283
}
@@ -826,10 +836,6 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
826836
}
827837

828838
/* OK, This is the point of no return */
829-
current->mm->start_data = 0;
830-
current->mm->end_data = 0;
831-
current->mm->end_code = 0;
832-
current->mm->mmap = NULL;
833839
current->flags &= ~PF_FORKNOEXEC;
834840
current->mm->def_flags = def_flags;
835841

@@ -1051,9 +1057,13 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
10511057

10521058
compute_creds(bprm);
10531059
current->flags &= ~PF_FORKNOEXEC;
1054-
create_elf_tables(bprm, &loc->elf_ex,
1060+
retval = create_elf_tables(bprm, &loc->elf_ex,
10551061
(interpreter_type == INTERPRETER_AOUT),
10561062
load_addr, interp_load_addr);
1063+
if (retval < 0) {
1064+
send_sig(SIGKILL, current, 0);
1065+
goto out;
1066+
}
10571067
/* N.B. passed_fileno might not be initialized? */
10581068
if (interpreter_type == INTERPRETER_AOUT)
10591069
current->mm->arg_start += strlen(passed_fileno) + 1;

‎fs/binfmt_elf_fdpic.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
621621
p = (char __user *) current->mm->arg_start;
622622
for (loop = bprm->argc; loop > 0; loop--) {
623623
__put_user((elf_caddr_t) p, argv++);
624-
len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
625-
if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
624+
len = strnlen_user(p, MAX_ARG_STRLEN);
625+
if (!len || len > MAX_ARG_STRLEN)
626626
return -EINVAL;
627627
p += len;
628628
}
@@ -633,8 +633,8 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
633633
current->mm->env_start = (unsigned long) p;
634634
for (loop = bprm->envc; loop > 0; loop--) {
635635
__put_user((elf_caddr_t)(unsigned long) p, envp++);
636-
len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
637-
if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
636+
len = strnlen_user(p, MAX_ARG_STRLEN);
637+
if (!len || len > MAX_ARG_STRLEN)
638638
return -EINVAL;
639639
p += len;
640640
}

‎fs/binfmt_misc.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
126126
goto _ret;
127127

128128
if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
129-
remove_arg_zero(bprm);
129+
retval = remove_arg_zero(bprm);
130+
if (retval)
131+
goto _ret;
130132
}
131133

132134
if (fmt->flags & MISC_FMT_OPEN_BINARY) {

‎fs/binfmt_script.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
6767
* This is done in reverse order, because of how the
6868
* user environment and arguments are stored.
6969
*/
70-
remove_arg_zero(bprm);
70+
retval = remove_arg_zero(bprm);
71+
if (retval)
72+
return retval;
7173
retval = copy_strings_kernel(1, &bprm->interp, bprm);
7274
if (retval < 0) return retval;
7375
bprm->argc++;

0 commit comments

Comments
 (0)