vserver 1.9.5.x5
[linux-2.6.git] / fs / exec.c
1 /*
2  *  linux/fs/exec.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * #!-checking implemented by tytso.
9  */
10 /*
11  * Demand-loading implemented 01.12.91 - no need to read anything but
12  * the header into memory. The inode of the executable is put into
13  * "current->executable", and page faults do the actual loading. Clean.
14  *
15  * Once more I can proudly say that linux stood up to being changed: it
16  * was less than 2 hours work to get demand-loading completely implemented.
17  *
18  * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
19  * current->executable is only used by the procfs.  This allows a dispatch
20  * table to check for several different types  of binary formats.  We keep
21  * trying until we recognize the file or we run out of supported binary
22  * formats. 
23  */
24
25 #include <linux/config.h>
26 #include <linux/slab.h>
27 #include <linux/file.h>
28 #include <linux/mman.h>
29 #include <linux/a.out.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <linux/pagemap.h>
35 #include <linux/highmem.h>
36 #include <linux/spinlock.h>
37 #include <linux/key.h>
38 #include <linux/personality.h>
39 #include <linux/binfmts.h>
40 #include <linux/swap.h>
41 #include <linux/utsname.h>
42 #include <linux/module.h>
43 #include <linux/namei.h>
44 #include <linux/proc_fs.h>
45 #include <linux/ptrace.h>
46 #include <linux/mount.h>
47 #include <linux/security.h>
48 #include <linux/syscalls.h>
49 #include <linux/rmap.h>
50 #include <linux/acct.h>
51 #include <linux/vs_memory.h>
52
53 #include <asm/uaccess.h>
54 #include <asm/mmu_context.h>
55
56 #ifdef CONFIG_KMOD
57 #include <linux/kmod.h>
58 #endif
59
60 int core_uses_pid;
61 char core_pattern[65] = "core";
62 /* The maximal length of core_pattern is also specified in sysctl.c */
63
64 static struct linux_binfmt *formats;
65 static DEFINE_RWLOCK(binfmt_lock);
66
67 int register_binfmt(struct linux_binfmt * fmt)
68 {
69         struct linux_binfmt ** tmp = &formats;
70
71         if (!fmt)
72                 return -EINVAL;
73         if (fmt->next)
74                 return -EBUSY;
75         write_lock(&binfmt_lock);
76         while (*tmp) {
77                 if (fmt == *tmp) {
78                         write_unlock(&binfmt_lock);
79                         return -EBUSY;
80                 }
81                 tmp = &(*tmp)->next;
82         }
83         fmt->next = formats;
84         formats = fmt;
85         write_unlock(&binfmt_lock);
86         return 0;       
87 }
88
89 EXPORT_SYMBOL(register_binfmt);
90
91 int unregister_binfmt(struct linux_binfmt * fmt)
92 {
93         struct linux_binfmt ** tmp = &formats;
94
95         write_lock(&binfmt_lock);
96         while (*tmp) {
97                 if (fmt == *tmp) {
98                         *tmp = fmt->next;
99                         write_unlock(&binfmt_lock);
100                         return 0;
101                 }
102                 tmp = &(*tmp)->next;
103         }
104         write_unlock(&binfmt_lock);
105         return -EINVAL;
106 }
107
108 EXPORT_SYMBOL(unregister_binfmt);
109
110 static inline void put_binfmt(struct linux_binfmt * fmt)
111 {
112         module_put(fmt->module);
113 }
114
115 /*
116  * Note that a shared library must be both readable and executable due to
117  * security reasons.
118  *
119  * Also note that we take the address to load from from the file itself.
120  */
121 asmlinkage long sys_uselib(const char __user * library)
122 {
123         struct file * file;
124         struct nameidata nd;
125         int error;
126
127         nd.intent.open.flags = FMODE_READ;
128         error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
129         if (error)
130                 goto out;
131
132         error = -EINVAL;
133         if (!S_ISREG(nd.dentry->d_inode->i_mode))
134                 goto exit;
135
136         error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
137         if (error)
138                 goto exit;
139
140         file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
141         error = PTR_ERR(file);
142         if (IS_ERR(file))
143                 goto out;
144
145         error = -ENOEXEC;
146         if(file->f_op) {
147                 struct linux_binfmt * fmt;
148
149                 read_lock(&binfmt_lock);
150                 for (fmt = formats ; fmt ; fmt = fmt->next) {
151                         if (!fmt->load_shlib)
152                                 continue;
153                         if (!try_module_get(fmt->module))
154                                 continue;
155                         read_unlock(&binfmt_lock);
156                         error = fmt->load_shlib(file);
157                         read_lock(&binfmt_lock);
158                         put_binfmt(fmt);
159                         if (error != -ENOEXEC)
160                                 break;
161                 }
162                 read_unlock(&binfmt_lock);
163         }
164         fput(file);
165 out:
166         return error;
167 exit:
168         path_release(&nd);
169         goto out;
170 }
171
172 /*
173  * count() counts the number of strings in array ARGV.
174  */
175 static int count(char __user * __user * argv, int max)
176 {
177         int i = 0;
178
179         if (argv != NULL) {
180                 for (;;) {
181                         char __user * p;
182
183                         if (get_user(p, argv))
184                                 return -EFAULT;
185                         if (!p)
186                                 break;
187                         argv++;
188                         if(++i > max)
189                                 return -E2BIG;
190                         cond_resched();
191                 }
192         }
193         return i;
194 }
195
196 /*
197  * 'copy_strings()' copies argument/environment strings from user
198  * memory to free pages in kernel mem. These are in a format ready
199  * to be put directly into the top of new user memory.
200  */
201 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
202 {
203         struct page *kmapped_page = NULL;
204         char *kaddr = NULL;
205         int ret;
206
207         while (argc-- > 0) {
208                 char __user *str;
209                 int len;
210                 unsigned long pos;
211
212                 if (get_user(str, argv+argc) ||
213                                 !(len = strnlen_user(str, bprm->p))) {
214                         ret = -EFAULT;
215                         goto out;
216                 }
217
218                 if (bprm->p < len)  {
219                         ret = -E2BIG;
220                         goto out;
221                 }
222
223                 bprm->p -= len;
224                 /* XXX: add architecture specific overflow check here. */
225                 pos = bprm->p;
226
227                 while (len > 0) {
228                         int i, new, err;
229                         int offset, bytes_to_copy;
230                         struct page *page;
231
232                         offset = pos % PAGE_SIZE;
233                         i = pos/PAGE_SIZE;
234                         page = bprm->page[i];
235                         new = 0;
236                         if (!page) {
237                                 page = alloc_page(GFP_HIGHUSER);
238                                 bprm->page[i] = page;
239                                 if (!page) {
240                                         ret = -ENOMEM;
241                                         goto out;
242                                 }
243                                 new = 1;
244                         }
245
246                         if (page != kmapped_page) {
247                                 if (kmapped_page)
248                                         kunmap(kmapped_page);
249                                 kmapped_page = page;
250                                 kaddr = kmap(kmapped_page);
251                         }
252                         if (new && offset)
253                                 memset(kaddr, 0, offset);
254                         bytes_to_copy = PAGE_SIZE - offset;
255                         if (bytes_to_copy > len) {
256                                 bytes_to_copy = len;
257                                 if (new)
258                                         memset(kaddr+offset+len, 0,
259                                                 PAGE_SIZE-offset-len);
260                         }
261                         err = copy_from_user(kaddr+offset, str, bytes_to_copy);
262                         if (err) {
263                                 ret = -EFAULT;
264                                 goto out;
265                         }
266
267                         pos += bytes_to_copy;
268                         str += bytes_to_copy;
269                         len -= bytes_to_copy;
270                 }
271         }
272         ret = 0;
273 out:
274         if (kmapped_page)
275                 kunmap(kmapped_page);
276         return ret;
277 }
278
279 /*
280  * Like copy_strings, but get argv and its values from kernel memory.
281  */
282 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
283 {
284         int r;
285         mm_segment_t oldfs = get_fs();
286         set_fs(KERNEL_DS);
287         r = copy_strings(argc, (char __user * __user *)argv, bprm);
288         set_fs(oldfs);
289         return r;
290 }
291
292 EXPORT_SYMBOL(copy_strings_kernel);
293
294 #ifdef CONFIG_MMU
295 /*
296  * This routine is used to map in a page into an address space: needed by
297  * execve() for the initial stack and environment pages.
298  *
299  * vma->vm_mm->mmap_sem is held for writing.
300  */
301 void install_arg_page(struct vm_area_struct *vma,
302                         struct page *page, unsigned long address)
303 {
304         struct mm_struct *mm = vma->vm_mm;
305         pgd_t * pgd;
306         pud_t * pud;
307         pmd_t * pmd;
308         pte_t * pte;
309
310         if (unlikely(anon_vma_prepare(vma)))
311                 goto out_sig;
312
313         flush_dcache_page(page);
314         pgd = pgd_offset(mm, address);
315
316         spin_lock(&mm->page_table_lock);
317         pud = pud_alloc(mm, pgd, address);
318         if (!pud)
319                 goto out;
320         pmd = pmd_alloc(mm, pud, address);
321         if (!pmd)
322                 goto out;
323         pte = pte_alloc_map(mm, pmd, address);
324         if (!pte)
325                 goto out;
326         if (!pte_none(*pte)) {
327                 pte_unmap(pte);
328                 goto out;
329         }
330         // mm->rss++;
331         vx_rsspages_inc(mm);
332         lru_cache_add_active(page);
333         set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
334                                         page, vma->vm_page_prot))));
335         page_add_anon_rmap(page, vma, address);
336         pte_unmap(pte);
337         spin_unlock(&mm->page_table_lock);
338
339         /* no need for flush_tlb */
340         return;
341 out:
342         spin_unlock(&mm->page_table_lock);
343 out_sig:
344         __free_page(page);
345         force_sig(SIGKILL, current);
346 }
347
348 #define EXTRA_STACK_VM_PAGES    20      /* random */
349
350 int setup_arg_pages(struct linux_binprm *bprm,
351                     unsigned long stack_top,
352                     int executable_stack)
353 {
354         unsigned long stack_base;
355         struct vm_area_struct *mpnt;
356         struct mm_struct *mm = current->mm;
357         int i, ret;
358         long arg_size;
359
360 #ifdef CONFIG_STACK_GROWSUP
361         /* Move the argument and environment strings to the bottom of the
362          * stack space.
363          */
364         int offset, j;
365         char *to, *from;
366
367         /* Start by shifting all the pages down */
368         i = 0;
369         for (j = 0; j < MAX_ARG_PAGES; j++) {
370                 struct page *page = bprm->page[j];
371                 if (!page)
372                         continue;
373                 bprm->page[i++] = page;
374         }
375
376         /* Now move them within their pages */
377         offset = bprm->p % PAGE_SIZE;
378         to = kmap(bprm->page[0]);
379         for (j = 1; j < i; j++) {
380                 memmove(to, to + offset, PAGE_SIZE - offset);
381                 from = kmap(bprm->page[j]);
382                 memcpy(to + PAGE_SIZE - offset, from, offset);
383                 kunmap(bprm->page[j - 1]);
384                 to = from;
385         }
386         memmove(to, to + offset, PAGE_SIZE - offset);
387         kunmap(bprm->page[j - 1]);
388
389         /* Limit stack size to 1GB */
390         stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
391         if (stack_base > (1 << 30))
392                 stack_base = 1 << 30;
393         stack_base = PAGE_ALIGN(stack_top - stack_base);
394
395         /* Adjust bprm->p to point to the end of the strings. */
396         bprm->p = stack_base + PAGE_SIZE * i - offset;
397
398         mm->arg_start = stack_base;
399         arg_size = i << PAGE_SHIFT;
400
401         /* zero pages that were copied above */
402         while (i < MAX_ARG_PAGES)
403                 bprm->page[i++] = NULL;
404 #else
405         stack_base = stack_top - MAX_ARG_PAGES * PAGE_SIZE;
406         bprm->p += stack_base;
407         mm->arg_start = bprm->p;
408         arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
409 #endif
410
411         arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
412
413         if (bprm->loader)
414                 bprm->loader += stack_base;
415         bprm->exec += stack_base;
416
417         mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
418         if (!mpnt)
419                 return -ENOMEM;
420
421         if (security_vm_enough_memory(arg_size >> PAGE_SHIFT) ||
422                 !vx_vmpages_avail(mm, arg_size >> PAGE_SHIFT)) {
423                 kmem_cache_free(vm_area_cachep, mpnt);
424                 return -ENOMEM;
425         }
426
427         memset(mpnt, 0, sizeof(*mpnt));
428
429         down_write(&mm->mmap_sem);
430         {
431                 mpnt->vm_mm = mm;
432 #ifdef CONFIG_STACK_GROWSUP
433                 mpnt->vm_start = stack_base;
434                 mpnt->vm_end = stack_base + arg_size;
435 #else
436                 mpnt->vm_end = stack_top;
437                 mpnt->vm_start = mpnt->vm_end - arg_size;
438 #endif
439                 /* Adjust stack execute permissions; explicitly enable
440                  * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
441                  * and leave alone (arch default) otherwise. */
442                 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
443                         mpnt->vm_flags = VM_STACK_FLAGS |  VM_EXEC;
444                 else if (executable_stack == EXSTACK_DISABLE_X)
445                         mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
446                 else
447                         mpnt->vm_flags = VM_STACK_FLAGS;
448                 mpnt->vm_flags |= mm->def_flags;
449                 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
450                 if ((ret = insert_vm_struct(mm, mpnt))) {
451                         up_write(&mm->mmap_sem);
452                         kmem_cache_free(vm_area_cachep, mpnt);
453                         return ret;
454                 }
455                 // mm->stack_vm = mm->total_vm = vma_pages(mpnt);
456                 vx_vmpages_sub(mm, mm->total_vm - vma_pages(mpnt));
457                 mm->stack_vm = mm->total_vm;
458         }
459
460         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
461                 struct page *page = bprm->page[i];
462                 if (page) {
463                         bprm->page[i] = NULL;
464                         install_arg_page(mpnt, page, stack_base);
465                 }
466                 stack_base += PAGE_SIZE;
467         }
468         up_write(&mm->mmap_sem);
469         
470         return 0;
471 }
472
473 EXPORT_SYMBOL(setup_arg_pages);
474
475 #define free_arg_pages(bprm) do { } while (0)
476
477 #else
478
479 static inline void free_arg_pages(struct linux_binprm *bprm)
480 {
481         int i;
482
483         for (i = 0; i < MAX_ARG_PAGES; i++) {
484                 if (bprm->page[i])
485                         __free_page(bprm->page[i]);
486                 bprm->page[i] = NULL;
487         }
488 }
489
490 #endif /* CONFIG_MMU */
491
492 struct file *open_exec(const char *name)
493 {
494         struct nameidata nd;
495         int err;
496         struct file *file;
497
498         nd.intent.open.flags = FMODE_READ;
499         err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
500         file = ERR_PTR(err);
501
502         if (!err) {
503                 struct inode *inode = nd.dentry->d_inode;
504                 file = ERR_PTR(-EACCES);
505                 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
506                     S_ISREG(inode->i_mode)) {
507                         int err = permission(inode, MAY_EXEC, &nd);
508                         if (!err && !(inode->i_mode & 0111))
509                                 err = -EACCES;
510                         file = ERR_PTR(err);
511                         if (!err) {
512                                 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
513                                 if (!IS_ERR(file)) {
514                                         err = deny_write_access(file);
515                                         if (err) {
516                                                 fput(file);
517                                                 file = ERR_PTR(err);
518                                         }
519                                 }
520 out:
521                                 return file;
522                         }
523                 }
524                 path_release(&nd);
525         }
526         goto out;
527 }
528
529 EXPORT_SYMBOL(open_exec);
530
531 int kernel_read(struct file *file, unsigned long offset,
532         char *addr, unsigned long count)
533 {
534         mm_segment_t old_fs;
535         loff_t pos = offset;
536         int result;
537
538         old_fs = get_fs();
539         set_fs(get_ds());
540         /* The cast to a user pointer is valid due to the set_fs() */
541         result = vfs_read(file, (void __user *)addr, count, &pos);
542         set_fs(old_fs);
543         return result;
544 }
545
546 EXPORT_SYMBOL(kernel_read);
547
548 static int exec_mmap(struct mm_struct *mm)
549 {
550         struct task_struct *tsk;
551         struct mm_struct * old_mm, *active_mm;
552
553         /* Notify parent that we're no longer interested in the old VM */
554         tsk = current;
555         old_mm = current->mm;
556         mm_release(tsk, old_mm);
557
558         if (old_mm) {
559                 /*
560                  * Make sure that if there is a core dump in progress
561                  * for the old mm, we get out and die instead of going
562                  * through with the exec.  We must hold mmap_sem around
563                  * checking core_waiters and changing tsk->mm.  The
564                  * core-inducing thread will increment core_waiters for
565                  * each thread whose ->mm == old_mm.
566                  */
567                 down_read(&old_mm->mmap_sem);
568                 if (unlikely(old_mm->core_waiters)) {
569                         up_read(&old_mm->mmap_sem);
570                         return -EINTR;
571                 }
572         }
573         task_lock(tsk);
574         active_mm = tsk->active_mm;
575         tsk->mm = mm;
576         tsk->active_mm = mm;
577         activate_mm(active_mm, mm);
578         task_unlock(tsk);
579         arch_pick_mmap_layout(mm);
580         if (old_mm) {
581                 up_read(&old_mm->mmap_sem);
582                 if (active_mm != old_mm) BUG();
583                 mmput(old_mm);
584                 return 0;
585         }
586         mmdrop(active_mm);
587         return 0;
588 }
589
590 /*
591  * This function makes sure the current process has its own signal table,
592  * so that flush_signal_handlers can later reset the handlers without
593  * disturbing other processes.  (Other processes might share the signal
594  * table via the CLONE_SIGHAND option to clone().)
595  */
596 static inline int de_thread(struct task_struct *tsk)
597 {
598         struct signal_struct *sig = tsk->signal;
599         struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
600         spinlock_t *lock = &oldsighand->siglock;
601         int count;
602
603         /*
604          * If we don't share sighandlers, then we aren't sharing anything
605          * and we can just re-use it all.
606          */
607         if (atomic_read(&oldsighand->count) <= 1) {
608                 BUG_ON(atomic_read(&sig->count) != 1);
609                 exit_itimers(sig);
610                 return 0;
611         }
612
613         newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
614         if (!newsighand)
615                 return -ENOMEM;
616
617         if (thread_group_empty(current))
618                 goto no_thread_group;
619
620         /*
621          * Kill all other threads in the thread group.
622          * We must hold tasklist_lock to call zap_other_threads.
623          */
624         read_lock(&tasklist_lock);
625         spin_lock_irq(lock);
626         if (sig->flags & SIGNAL_GROUP_EXIT) {
627                 /*
628                  * Another group action in progress, just
629                  * return so that the signal is processed.
630                  */
631                 spin_unlock_irq(lock);
632                 read_unlock(&tasklist_lock);
633                 kmem_cache_free(sighand_cachep, newsighand);
634                 return -EAGAIN;
635         }
636         zap_other_threads(current);
637         read_unlock(&tasklist_lock);
638
639         /*
640          * Account for the thread group leader hanging around:
641          */
642         count = 2;
643         if (thread_group_leader(current))
644                 count = 1;
645         while (atomic_read(&sig->count) > count) {
646                 sig->group_exit_task = current;
647                 sig->notify_count = count;
648                 __set_current_state(TASK_UNINTERRUPTIBLE);
649                 spin_unlock_irq(lock);
650                 schedule();
651                 spin_lock_irq(lock);
652         }
653         sig->group_exit_task = NULL;
654         sig->notify_count = 0;
655         spin_unlock_irq(lock);
656
657         /*
658          * At this point all other threads have exited, all we have to
659          * do is to wait for the thread group leader to become inactive,
660          * and to assume its PID:
661          */
662         if (!thread_group_leader(current)) {
663                 struct task_struct *leader = current->group_leader, *parent;
664                 struct dentry *proc_dentry1, *proc_dentry2;
665                 unsigned long exit_state, ptrace;
666
667                 /*
668                  * Wait for the thread group leader to be a zombie.
669                  * It should already be zombie at this point, most
670                  * of the time.
671                  */
672                 while (leader->exit_state != EXIT_ZOMBIE)
673                         yield();
674
675                 spin_lock(&leader->proc_lock);
676                 spin_lock(&current->proc_lock);
677                 proc_dentry1 = proc_pid_unhash(current);
678                 proc_dentry2 = proc_pid_unhash(leader);
679                 write_lock_irq(&tasklist_lock);
680
681                 if (leader->tgid != current->tgid)
682                         BUG();
683                 if (current->pid == current->tgid)
684                         BUG();
685                 /*
686                  * An exec() starts a new thread group with the
687                  * TGID of the previous thread group. Rehash the
688                  * two threads with a switched PID, and release
689                  * the former thread group leader:
690                  */
691                 ptrace = leader->ptrace;
692                 parent = leader->parent;
693                 if (unlikely(ptrace) && unlikely(parent == current)) {
694                         /*
695                          * Joker was ptracing his own group leader,
696                          * and now he wants to be his own parent!
697                          * We can't have that.
698                          */
699                         ptrace = 0;
700                 }
701
702                 ptrace_unlink(current);
703                 ptrace_unlink(leader);
704                 remove_parent(current);
705                 remove_parent(leader);
706
707                 switch_exec_pids(leader, current);
708
709                 current->parent = current->real_parent = leader->real_parent;
710                 leader->parent = leader->real_parent = child_reaper;
711                 current->group_leader = current;
712                 leader->group_leader = leader;
713
714                 add_parent(current, current->parent);
715                 add_parent(leader, leader->parent);
716                 if (ptrace) {
717                         current->ptrace = ptrace;
718                         __ptrace_link(current, parent);
719                 }
720
721                 list_del(&current->tasks);
722                 list_add_tail(&current->tasks, &init_task.tasks);
723                 current->exit_signal = SIGCHLD;
724                 exit_state = leader->exit_state;
725
726                 write_unlock_irq(&tasklist_lock);
727                 spin_unlock(&leader->proc_lock);
728                 spin_unlock(&current->proc_lock);
729                 proc_pid_flush(proc_dentry1);
730                 proc_pid_flush(proc_dentry2);
731
732                 if (exit_state != EXIT_ZOMBIE)
733                         BUG();
734                 release_task(leader);
735         }
736
737         /*
738          * Now there are really no other threads at all,
739          * so it's safe to stop telling them to kill themselves.
740          */
741         sig->flags = 0;
742
743 no_thread_group:
744         BUG_ON(atomic_read(&sig->count) != 1);
745         exit_itimers(sig);
746
747         if (atomic_read(&oldsighand->count) == 1) {
748                 /*
749                  * Now that we nuked the rest of the thread group,
750                  * it turns out we are not sharing sighand any more either.
751                  * So we can just keep it.
752                  */
753                 kmem_cache_free(sighand_cachep, newsighand);
754         } else {
755                 /*
756                  * Move our state over to newsighand and switch it in.
757                  */
758                 spin_lock_init(&newsighand->siglock);
759                 atomic_set(&newsighand->count, 1);
760                 memcpy(newsighand->action, oldsighand->action,
761                        sizeof(newsighand->action));
762
763                 write_lock_irq(&tasklist_lock);
764                 spin_lock(&oldsighand->siglock);
765                 spin_lock(&newsighand->siglock);
766
767                 current->sighand = newsighand;
768                 recalc_sigpending();
769
770                 spin_unlock(&newsighand->siglock);
771                 spin_unlock(&oldsighand->siglock);
772                 write_unlock_irq(&tasklist_lock);
773
774                 if (atomic_dec_and_test(&oldsighand->count))
775                         kmem_cache_free(sighand_cachep, oldsighand);
776         }
777
778         if (!thread_group_empty(current))
779                 BUG();
780         if (!thread_group_leader(current))
781                 BUG();
782         return 0;
783 }
784         
785 /*
786  * These functions flushes out all traces of the currently running executable
787  * so that a new one can be started
788  */
789
790 static inline void flush_old_files(struct files_struct * files)
791 {
792         long j = -1;
793
794         spin_lock(&files->file_lock);
795         for (;;) {
796                 unsigned long set, i;
797
798                 j++;
799                 i = j * __NFDBITS;
800                 if (i >= files->max_fds || i >= files->max_fdset)
801                         break;
802                 set = files->close_on_exec->fds_bits[j];
803                 if (!set)
804                         continue;
805                 files->close_on_exec->fds_bits[j] = 0;
806                 spin_unlock(&files->file_lock);
807                 for ( ; set ; i++,set >>= 1) {
808                         if (set & 1) {
809                                 sys_close(i);
810                         }
811                 }
812                 spin_lock(&files->file_lock);
813
814         }
815         spin_unlock(&files->file_lock);
816 }
817
818 void get_task_comm(char *buf, struct task_struct *tsk)
819 {
820         /* buf must be at least sizeof(tsk->comm) in size */
821         task_lock(tsk);
822         strncpy(buf, tsk->comm, sizeof(tsk->comm));
823         task_unlock(tsk);
824 }
825
826 void set_task_comm(struct task_struct *tsk, char *buf)
827 {
828         task_lock(tsk);
829         strlcpy(tsk->comm, buf, sizeof(tsk->comm));
830         task_unlock(tsk);
831 }
832
833 int flush_old_exec(struct linux_binprm * bprm)
834 {
835         char * name;
836         int i, ch, retval;
837         struct files_struct *files;
838         char tcomm[sizeof(current->comm)];
839
840         /*
841          * Make sure we have a private signal table and that
842          * we are unassociated from the previous thread group.
843          */
844         retval = de_thread(current);
845         if (retval)
846                 goto out;
847
848         /*
849          * Make sure we have private file handles. Ask the
850          * fork helper to do the work for us and the exit
851          * helper to do the cleanup of the old one.
852          */
853         files = current->files;         /* refcounted so safe to hold */
854         retval = unshare_files();
855         if (retval)
856                 goto out;
857         /*
858          * Release all of the old mmap stuff
859          */
860         retval = exec_mmap(bprm->mm);
861         if (retval)
862                 goto mmap_failed;
863
864         bprm->mm = NULL;                /* We're using it now */
865
866         /* This is the point of no return */
867         steal_locks(files);
868         put_files_struct(files);
869
870         current->sas_ss_sp = current->sas_ss_size = 0;
871
872         if (current->euid == current->uid && current->egid == current->gid)
873                 current->mm->dumpable = 1;
874         name = bprm->filename;
875         for (i=0; (ch = *(name++)) != '\0';) {
876                 if (ch == '/')
877                         i = 0;
878                 else
879                         if (i < (sizeof(tcomm) - 1))
880                                 tcomm[i++] = ch;
881         }
882         tcomm[i] = '\0';
883         set_task_comm(current, tcomm);
884
885         flush_thread();
886
887         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
888             permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
889             (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
890                 suid_keys(current);
891                 current->mm->dumpable = 0;
892         }
893
894         /* An exec changes our domain. We are no longer part of the thread
895            group */
896
897         current->self_exec_id++;
898                         
899         flush_signal_handlers(current, 0);
900         flush_old_files(current->files);
901
902         return 0;
903
904 mmap_failed:
905         put_files_struct(current->files);
906         current->files = files;
907 out:
908         return retval;
909 }
910
911 EXPORT_SYMBOL(flush_old_exec);
912
913 /* 
914  * Fill the binprm structure from the inode. 
915  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
916  */
917 int prepare_binprm(struct linux_binprm *bprm)
918 {
919         int mode;
920         struct inode * inode = bprm->file->f_dentry->d_inode;
921         int retval;
922
923         mode = inode->i_mode;
924         /*
925          * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
926          * generic_permission lets a non-executable through
927          */
928         if (!(mode & 0111))     /* with at least _one_ execute bit set */
929                 return -EACCES;
930         if (bprm->file->f_op == NULL)
931                 return -EACCES;
932
933         bprm->e_uid = current->euid;
934         bprm->e_gid = current->egid;
935
936         if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
937                 /* Set-uid? */
938                 if (mode & S_ISUID) {
939                         current->personality &= ~PER_CLEAR_ON_SETID;
940                         bprm->e_uid = inode->i_uid;
941                 }
942
943                 /* Set-gid? */
944                 /*
945                  * If setgid is set but no group execute bit then this
946                  * is a candidate for mandatory locking, not a setgid
947                  * executable.
948                  */
949                 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
950                         current->personality &= ~PER_CLEAR_ON_SETID;
951                         bprm->e_gid = inode->i_gid;
952                 }
953         }
954
955         /* fill in binprm security blob */
956         retval = security_bprm_set(bprm);
957         if (retval)
958                 return retval;
959
960         memset(bprm->buf,0,BINPRM_BUF_SIZE);
961         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
962 }
963
964 EXPORT_SYMBOL(prepare_binprm);
965
966 static inline int unsafe_exec(struct task_struct *p)
967 {
968         int unsafe = 0;
969         if (p->ptrace & PT_PTRACED) {
970                 if (p->ptrace & PT_PTRACE_CAP)
971                         unsafe |= LSM_UNSAFE_PTRACE_CAP;
972                 else
973                         unsafe |= LSM_UNSAFE_PTRACE;
974         }
975         if (atomic_read(&p->fs->count) > 1 ||
976             atomic_read(&p->files->count) > 1 ||
977             atomic_read(&p->sighand->count) > 1)
978                 unsafe |= LSM_UNSAFE_SHARE;
979
980         return unsafe;
981 }
982
983 void compute_creds(struct linux_binprm *bprm)
984 {
985         int unsafe;
986
987         if (bprm->e_uid != current->uid)
988                 suid_keys(current);
989         exec_keys(current);
990
991         task_lock(current);
992         unsafe = unsafe_exec(current);
993         security_bprm_apply_creds(bprm, unsafe);
994         task_unlock(current);
995         security_bprm_post_apply_creds(bprm);
996 }
997
998 EXPORT_SYMBOL(compute_creds);
999
1000 void remove_arg_zero(struct linux_binprm *bprm)
1001 {
1002         if (bprm->argc) {
1003                 unsigned long offset;
1004                 char * kaddr;
1005                 struct page *page;
1006
1007                 offset = bprm->p % PAGE_SIZE;
1008                 goto inside;
1009
1010                 while (bprm->p++, *(kaddr+offset++)) {
1011                         if (offset != PAGE_SIZE)
1012                                 continue;
1013                         offset = 0;
1014                         kunmap_atomic(kaddr, KM_USER0);
1015 inside:
1016                         page = bprm->page[bprm->p/PAGE_SIZE];
1017                         kaddr = kmap_atomic(page, KM_USER0);
1018                 }
1019                 kunmap_atomic(kaddr, KM_USER0);
1020                 bprm->argc--;
1021         }
1022 }
1023
1024 EXPORT_SYMBOL(remove_arg_zero);
1025
1026 /*
1027  * cycle the list of binary formats handler, until one recognizes the image
1028  */
1029 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1030 {
1031         int try,retval;
1032         struct linux_binfmt *fmt;
1033 #ifdef __alpha__
1034         /* handle /sbin/loader.. */
1035         {
1036             struct exec * eh = (struct exec *) bprm->buf;
1037
1038             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1039                 (eh->fh.f_flags & 0x3000) == 0x3000)
1040             {
1041                 struct file * file;
1042                 unsigned long loader;
1043
1044                 allow_write_access(bprm->file);
1045                 fput(bprm->file);
1046                 bprm->file = NULL;
1047
1048                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1049
1050                 file = open_exec("/sbin/loader");
1051                 retval = PTR_ERR(file);
1052                 if (IS_ERR(file))
1053                         return retval;
1054
1055                 /* Remember if the application is TASO.  */
1056                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1057
1058                 bprm->file = file;
1059                 bprm->loader = loader;
1060                 retval = prepare_binprm(bprm);
1061                 if (retval<0)
1062                         return retval;
1063                 /* should call search_binary_handler recursively here,
1064                    but it does not matter */
1065             }
1066         }
1067 #endif
1068         retval = security_bprm_check(bprm);
1069         if (retval)
1070                 return retval;
1071
1072         /* kernel module loader fixup */
1073         /* so we don't try to load run modprobe in kernel space. */
1074         set_fs(USER_DS);
1075         retval = -ENOENT;
1076         for (try=0; try<2; try++) {
1077                 read_lock(&binfmt_lock);
1078                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1079                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1080                         if (!fn)
1081                                 continue;
1082                         if (!try_module_get(fmt->module))
1083                                 continue;
1084                         read_unlock(&binfmt_lock);
1085                         retval = fn(bprm, regs);
1086                         if (retval >= 0) {
1087                                 put_binfmt(fmt);
1088                                 allow_write_access(bprm->file);
1089                                 if (bprm->file)
1090                                         fput(bprm->file);
1091                                 bprm->file = NULL;
1092                                 current->did_exec = 1;
1093                                 return retval;
1094                         }
1095                         read_lock(&binfmt_lock);
1096                         put_binfmt(fmt);
1097                         if (retval != -ENOEXEC || bprm->mm == NULL)
1098                                 break;
1099                         if (!bprm->file) {
1100                                 read_unlock(&binfmt_lock);
1101                                 return retval;
1102                         }
1103                 }
1104                 read_unlock(&binfmt_lock);
1105                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1106                         break;
1107 #ifdef CONFIG_KMOD
1108                 }else{
1109 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1110                         if (printable(bprm->buf[0]) &&
1111                             printable(bprm->buf[1]) &&
1112                             printable(bprm->buf[2]) &&
1113                             printable(bprm->buf[3]))
1114                                 break; /* -ENOEXEC */
1115                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1116 #endif
1117                 }
1118         }
1119         return retval;
1120 }
1121
1122 EXPORT_SYMBOL(search_binary_handler);
1123
1124 /*
1125  * sys_execve() executes a new program.
1126  */
1127 int do_execve(char * filename,
1128         char __user *__user *argv,
1129         char __user *__user *envp,
1130         struct pt_regs * regs)
1131 {
1132         struct linux_binprm *bprm;
1133         struct file *file;
1134         int retval;
1135         int i;
1136
1137         retval = -ENOMEM;
1138         bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1139         if (!bprm)
1140                 goto out_ret;
1141         memset(bprm, 0, sizeof(*bprm));
1142
1143         file = open_exec(filename);
1144         retval = PTR_ERR(file);
1145         if (IS_ERR(file))
1146                 goto out_kfree;
1147
1148         sched_exec();
1149
1150         bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1151
1152         bprm->file = file;
1153         bprm->filename = filename;
1154         bprm->interp = filename;
1155         bprm->mm = mm_alloc();
1156         retval = -ENOMEM;
1157         if (!bprm->mm)
1158                 goto out_file;
1159
1160         retval = init_new_context(current, bprm->mm);
1161         if (retval < 0)
1162                 goto out_mm;
1163
1164         bprm->argc = count(argv, bprm->p / sizeof(void *));
1165         if ((retval = bprm->argc) < 0)
1166                 goto out_mm;
1167
1168         bprm->envc = count(envp, bprm->p / sizeof(void *));
1169         if ((retval = bprm->envc) < 0)
1170                 goto out_mm;
1171
1172         retval = security_bprm_alloc(bprm);
1173         if (retval)
1174                 goto out;
1175
1176         retval = prepare_binprm(bprm);
1177         if (retval < 0)
1178                 goto out;
1179
1180         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1181         if (retval < 0)
1182                 goto out;
1183
1184         bprm->exec = bprm->p;
1185         retval = copy_strings(bprm->envc, envp, bprm);
1186         if (retval < 0)
1187                 goto out;
1188
1189         retval = copy_strings(bprm->argc, argv, bprm);
1190         if (retval < 0)
1191                 goto out;
1192
1193         retval = search_binary_handler(bprm,regs);
1194         if (retval >= 0) {
1195                 free_arg_pages(bprm);
1196
1197                 /* execve success */
1198                 security_bprm_free(bprm);
1199                 acct_update_integrals();
1200                 update_mem_hiwater();
1201                 kfree(bprm);
1202                 return retval;
1203         }
1204
1205 out:
1206         /* Something went wrong, return the inode and free the argument pages*/
1207         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1208                 struct page * page = bprm->page[i];
1209                 if (page)
1210                         __free_page(page);
1211         }
1212
1213         if (bprm->security)
1214                 security_bprm_free(bprm);
1215
1216 out_mm:
1217         if (bprm->mm)
1218                 mmdrop(bprm->mm);
1219
1220 out_file:
1221         if (bprm->file) {
1222                 allow_write_access(bprm->file);
1223                 fput(bprm->file);
1224         }
1225
1226 out_kfree:
1227         kfree(bprm);
1228
1229 out_ret:
1230         return retval;
1231 }
1232
1233 int set_binfmt(struct linux_binfmt *new)
1234 {
1235         struct linux_binfmt *old = current->binfmt;
1236
1237         if (new) {
1238                 if (!try_module_get(new->module))
1239                         return -1;
1240         }
1241         current->binfmt = new;
1242         if (old)
1243                 module_put(old->module);
1244         return 0;
1245 }
1246
1247 EXPORT_SYMBOL(set_binfmt);
1248
1249 #define CORENAME_MAX_SIZE 64
1250
1251 /* format_corename will inspect the pattern parameter, and output a
1252  * name into corename, which must have space for at least
1253  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1254  */
1255 static void format_corename(char *corename, const char *pattern, long signr)
1256 {
1257         const char *pat_ptr = pattern;
1258         char *out_ptr = corename;
1259         char *const out_end = corename + CORENAME_MAX_SIZE;
1260         int rc;
1261         int pid_in_pattern = 0;
1262
1263         /* Repeat as long as we have more pattern to process and more output
1264            space */
1265         while (*pat_ptr) {
1266                 if (*pat_ptr != '%') {
1267                         if (out_ptr == out_end)
1268                                 goto out;
1269                         *out_ptr++ = *pat_ptr++;
1270                 } else {
1271                         switch (*++pat_ptr) {
1272                         case 0:
1273                                 goto out;
1274                         /* Double percent, output one percent */
1275                         case '%':
1276                                 if (out_ptr == out_end)
1277                                         goto out;
1278                                 *out_ptr++ = '%';
1279                                 break;
1280                         /* pid */
1281                         case 'p':
1282                                 pid_in_pattern = 1;
1283                                 rc = snprintf(out_ptr, out_end - out_ptr,
1284                                               "%d", current->tgid);
1285                                 if (rc > out_end - out_ptr)
1286                                         goto out;
1287                                 out_ptr += rc;
1288                                 break;
1289                         /* uid */
1290                         case 'u':
1291                                 rc = snprintf(out_ptr, out_end - out_ptr,
1292                                               "%d", current->uid);
1293                                 if (rc > out_end - out_ptr)
1294                                         goto out;
1295                                 out_ptr += rc;
1296                                 break;
1297                         /* gid */
1298                         case 'g':
1299                                 rc = snprintf(out_ptr, out_end - out_ptr,
1300                                               "%d", current->gid);
1301                                 if (rc > out_end - out_ptr)
1302                                         goto out;
1303                                 out_ptr += rc;
1304                                 break;
1305                         /* signal that caused the coredump */
1306                         case 's':
1307                                 rc = snprintf(out_ptr, out_end - out_ptr,
1308                                               "%ld", signr);
1309                                 if (rc > out_end - out_ptr)
1310                                         goto out;
1311                                 out_ptr += rc;
1312                                 break;
1313                         /* UNIX time of coredump */
1314                         case 't': {
1315                                 struct timeval tv;
1316                                 do_gettimeofday(&tv);
1317                                 rc = snprintf(out_ptr, out_end - out_ptr,
1318                                               "%lu", tv.tv_sec);
1319                                 if (rc > out_end - out_ptr)
1320                                         goto out;
1321                                 out_ptr += rc;
1322                                 break;
1323                         }
1324                         /* hostname */
1325                         case 'h':
1326                                 down_read(&uts_sem);
1327                                 rc = snprintf(out_ptr, out_end - out_ptr,
1328                                               "%s", system_utsname.nodename);
1329                                 up_read(&uts_sem);
1330                                 if (rc > out_end - out_ptr)
1331                                         goto out;
1332                                 out_ptr += rc;
1333                                 break;
1334                         /* executable */
1335                         case 'e':
1336                                 rc = snprintf(out_ptr, out_end - out_ptr,
1337                                               "%s", current->comm);
1338                                 if (rc > out_end - out_ptr)
1339                                         goto out;
1340                                 out_ptr += rc;
1341                                 break;
1342                         default:
1343                                 break;
1344                         }
1345                         ++pat_ptr;
1346                 }
1347         }
1348         /* Backward compatibility with core_uses_pid:
1349          *
1350          * If core_pattern does not include a %p (as is the default)
1351          * and core_uses_pid is set, then .%pid will be appended to
1352          * the filename */
1353         if (!pid_in_pattern
1354             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1355                 rc = snprintf(out_ptr, out_end - out_ptr,
1356                               ".%d", current->tgid);
1357                 if (rc > out_end - out_ptr)
1358                         goto out;
1359                 out_ptr += rc;
1360         }
1361       out:
1362         *out_ptr = 0;
1363 }
1364
1365 static void zap_threads (struct mm_struct *mm)
1366 {
1367         struct task_struct *g, *p;
1368         struct task_struct *tsk = current;
1369         struct completion *vfork_done = tsk->vfork_done;
1370         int traced = 0;
1371
1372         /*
1373          * Make sure nobody is waiting for us to release the VM,
1374          * otherwise we can deadlock when we wait on each other
1375          */
1376         if (vfork_done) {
1377                 tsk->vfork_done = NULL;
1378                 complete(vfork_done);
1379         }
1380
1381         read_lock(&tasklist_lock);
1382         do_each_thread(g,p)
1383                 if (mm == p->mm && p != tsk) {
1384                         force_sig_specific(SIGKILL, p);
1385                         mm->core_waiters++;
1386                         if (unlikely(p->ptrace) &&
1387                             unlikely(p->parent->mm == mm))
1388                                 traced = 1;
1389                 }
1390         while_each_thread(g,p);
1391
1392         read_unlock(&tasklist_lock);
1393
1394         if (unlikely(traced)) {
1395                 /*
1396                  * We are zapping a thread and the thread it ptraces.
1397                  * If the tracee went into a ptrace stop for exit tracing,
1398                  * we could deadlock since the tracer is waiting for this
1399                  * coredump to finish.  Detach them so they can both die.
1400                  */
1401                 write_lock_irq(&tasklist_lock);
1402                 do_each_thread(g,p) {
1403                         if (mm == p->mm && p != tsk &&
1404                             p->ptrace && p->parent->mm == mm) {
1405                                 __ptrace_unlink(p);
1406                         }
1407                 } while_each_thread(g,p);
1408                 write_unlock_irq(&tasklist_lock);
1409         }
1410 }
1411
1412 static void coredump_wait(struct mm_struct *mm)
1413 {
1414         DECLARE_COMPLETION(startup_done);
1415
1416         mm->core_waiters++; /* let other threads block */
1417         mm->core_startup_done = &startup_done;
1418
1419         /* give other threads a chance to run: */
1420         yield();
1421
1422         zap_threads(mm);
1423         if (--mm->core_waiters) {
1424                 up_write(&mm->mmap_sem);
1425                 wait_for_completion(&startup_done);
1426         } else
1427                 up_write(&mm->mmap_sem);
1428         BUG_ON(mm->core_waiters);
1429 }
1430
1431 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1432 {
1433         char corename[CORENAME_MAX_SIZE + 1];
1434         struct mm_struct *mm = current->mm;
1435         struct linux_binfmt * binfmt;
1436         struct inode * inode;
1437         struct file * file;
1438         int retval = 0;
1439
1440         binfmt = current->binfmt;
1441         if (!binfmt || !binfmt->core_dump)
1442                 goto fail;
1443         down_write(&mm->mmap_sem);
1444         if (!mm->dumpable) {
1445                 up_write(&mm->mmap_sem);
1446                 goto fail;
1447         }
1448         mm->dumpable = 0;
1449         init_completion(&mm->core_done);
1450         spin_lock_irq(&current->sighand->siglock);
1451         current->signal->flags = SIGNAL_GROUP_EXIT;
1452         current->signal->group_exit_code = exit_code;
1453         spin_unlock_irq(&current->sighand->siglock);
1454         coredump_wait(mm);
1455
1456         /*
1457          * Clear any false indication of pending signals that might
1458          * be seen by the filesystem code called to write the core file.
1459          */
1460         current->signal->group_stop_count = 0;
1461         clear_thread_flag(TIF_SIGPENDING);
1462
1463         if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1464                 goto fail_unlock;
1465
1466         /*
1467          * lock_kernel() because format_corename() is controlled by sysctl, which
1468          * uses lock_kernel()
1469          */
1470         lock_kernel();
1471         format_corename(corename, core_pattern, signr);
1472         unlock_kernel();
1473         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1474         if (IS_ERR(file))
1475                 goto fail_unlock;
1476         inode = file->f_dentry->d_inode;
1477         if (inode->i_nlink > 1)
1478                 goto close_fail;        /* multiple links - don't dump */
1479         if (d_unhashed(file->f_dentry))
1480                 goto close_fail;
1481
1482         if (!S_ISREG(inode->i_mode))
1483                 goto close_fail;
1484         if (!file->f_op)
1485                 goto close_fail;
1486         if (!file->f_op->write)
1487                 goto close_fail;
1488         if (do_truncate(file->f_dentry, 0) != 0)
1489                 goto close_fail;
1490
1491         retval = binfmt->core_dump(signr, regs, file);
1492
1493         if (retval)
1494                 current->signal->group_exit_code |= 0x80;
1495 close_fail:
1496         filp_close(file, NULL);
1497 fail_unlock:
1498         complete_all(&mm->core_done);
1499 fail:
1500         return retval;
1501 }