blob: f20561ff4528f21ad7a36d35d56513346c5f740b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/file.h>
27#include <linux/mman.h>
28#include <linux/a.out.h>
29#include <linux/stat.h>
30#include <linux/fcntl.h>
31#include <linux/smp_lock.h>
32#include <linux/init.h>
33#include <linux/pagemap.h>
34#include <linux/highmem.h>
35#include <linux/spinlock.h>
36#include <linux/key.h>
37#include <linux/personality.h>
38#include <linux/binfmts.h>
39#include <linux/swap.h>
40#include <linux/utsname.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080041#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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>
Jay Lan8f0ab512006-09-30 23:28:59 -070050#include <linux/tsacct_kern.h>
Matt Helsley9f460802005-11-07 00:59:16 -080051#include <linux/cn_proc.h>
Al Viro473ae302006-04-26 14:04:08 -040052#include <linux/audit.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070053#include <linux/signalfd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/uaccess.h>
56#include <asm/mmu_context.h>
57
58#ifdef CONFIG_KMOD
59#include <linux/kmod.h>
60#endif
61
62int core_uses_pid;
Dan Aloni71ce92f2007-05-16 22:11:16 -070063char core_pattern[CORENAME_MAX_SIZE] = "core";
Alan Coxd6e71142005-06-23 00:09:43 -070064int suid_dumpable = 0;
65
66EXPORT_SYMBOL(suid_dumpable);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* The maximal length of core_pattern is also specified in sysctl.c */
68
69static struct linux_binfmt *formats;
70static DEFINE_RWLOCK(binfmt_lock);
71
72int register_binfmt(struct linux_binfmt * fmt)
73{
74 struct linux_binfmt ** tmp = &formats;
75
76 if (!fmt)
77 return -EINVAL;
78 if (fmt->next)
79 return -EBUSY;
80 write_lock(&binfmt_lock);
81 while (*tmp) {
82 if (fmt == *tmp) {
83 write_unlock(&binfmt_lock);
84 return -EBUSY;
85 }
86 tmp = &(*tmp)->next;
87 }
88 fmt->next = formats;
89 formats = fmt;
90 write_unlock(&binfmt_lock);
91 return 0;
92}
93
94EXPORT_SYMBOL(register_binfmt);
95
96int unregister_binfmt(struct linux_binfmt * fmt)
97{
98 struct linux_binfmt ** tmp = &formats;
99
100 write_lock(&binfmt_lock);
101 while (*tmp) {
102 if (fmt == *tmp) {
103 *tmp = fmt->next;
kalash nainwal98701d12007-05-08 00:28:31 -0700104 fmt->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 write_unlock(&binfmt_lock);
106 return 0;
107 }
108 tmp = &(*tmp)->next;
109 }
110 write_unlock(&binfmt_lock);
111 return -EINVAL;
112}
113
114EXPORT_SYMBOL(unregister_binfmt);
115
116static inline void put_binfmt(struct linux_binfmt * fmt)
117{
118 module_put(fmt->module);
119}
120
121/*
122 * Note that a shared library must be both readable and executable due to
123 * security reasons.
124 *
125 * Also note that we take the address to load from from the file itself.
126 */
127asmlinkage long sys_uselib(const char __user * library)
128{
129 struct file * file;
130 struct nameidata nd;
131 int error;
132
Oleg Drokinb5005312006-03-25 03:07:01 -0800133 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (error)
135 goto out;
136
Christoph Hellwig492c8b32007-05-23 13:57:53 -0700137 error = -EACCES;
138 if (nd.mnt->mnt_flags & MNT_NOEXEC)
139 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 error = -EINVAL;
141 if (!S_ISREG(nd.dentry->d_inode->i_mode))
142 goto exit;
143
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800144 error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (error)
146 goto exit;
147
Trond Myklebust834f2a42005-10-18 14:20:16 -0700148 file = nameidata_to_filp(&nd, O_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 error = PTR_ERR(file);
150 if (IS_ERR(file))
151 goto out;
152
153 error = -ENOEXEC;
154 if(file->f_op) {
155 struct linux_binfmt * fmt;
156
157 read_lock(&binfmt_lock);
158 for (fmt = formats ; fmt ; fmt = fmt->next) {
159 if (!fmt->load_shlib)
160 continue;
161 if (!try_module_get(fmt->module))
162 continue;
163 read_unlock(&binfmt_lock);
164 error = fmt->load_shlib(file);
165 read_lock(&binfmt_lock);
166 put_binfmt(fmt);
167 if (error != -ENOEXEC)
168 break;
169 }
170 read_unlock(&binfmt_lock);
171 }
172 fput(file);
173out:
174 return error;
175exit:
Trond Myklebust834f2a42005-10-18 14:20:16 -0700176 release_open_intent(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 path_release(&nd);
178 goto out;
179}
180
181/*
182 * count() counts the number of strings in array ARGV.
183 */
184static int count(char __user * __user * argv, int max)
185{
186 int i = 0;
187
188 if (argv != NULL) {
189 for (;;) {
190 char __user * p;
191
192 if (get_user(p, argv))
193 return -EFAULT;
194 if (!p)
195 break;
196 argv++;
197 if(++i > max)
198 return -E2BIG;
199 cond_resched();
200 }
201 }
202 return i;
203}
204
205/*
206 * 'copy_strings()' copies argument/environment strings from user
207 * memory to free pages in kernel mem. These are in a format ready
208 * to be put directly into the top of new user memory.
209 */
Adrian Bunk75c96f82005-05-05 16:16:09 -0700210static int copy_strings(int argc, char __user * __user * argv,
211 struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct page *kmapped_page = NULL;
214 char *kaddr = NULL;
215 int ret;
216
217 while (argc-- > 0) {
218 char __user *str;
219 int len;
220 unsigned long pos;
221
222 if (get_user(str, argv+argc) ||
223 !(len = strnlen_user(str, bprm->p))) {
224 ret = -EFAULT;
225 goto out;
226 }
227
228 if (bprm->p < len) {
229 ret = -E2BIG;
230 goto out;
231 }
232
233 bprm->p -= len;
234 /* XXX: add architecture specific overflow check here. */
235 pos = bprm->p;
236
237 while (len > 0) {
238 int i, new, err;
239 int offset, bytes_to_copy;
240 struct page *page;
241
242 offset = pos % PAGE_SIZE;
243 i = pos/PAGE_SIZE;
244 page = bprm->page[i];
245 new = 0;
246 if (!page) {
247 page = alloc_page(GFP_HIGHUSER);
248 bprm->page[i] = page;
249 if (!page) {
250 ret = -ENOMEM;
251 goto out;
252 }
253 new = 1;
254 }
255
256 if (page != kmapped_page) {
257 if (kmapped_page)
258 kunmap(kmapped_page);
259 kmapped_page = page;
260 kaddr = kmap(kmapped_page);
261 }
262 if (new && offset)
263 memset(kaddr, 0, offset);
264 bytes_to_copy = PAGE_SIZE - offset;
265 if (bytes_to_copy > len) {
266 bytes_to_copy = len;
267 if (new)
268 memset(kaddr+offset+len, 0,
269 PAGE_SIZE-offset-len);
270 }
271 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
272 if (err) {
273 ret = -EFAULT;
274 goto out;
275 }
276
277 pos += bytes_to_copy;
278 str += bytes_to_copy;
279 len -= bytes_to_copy;
280 }
281 }
282 ret = 0;
283out:
284 if (kmapped_page)
285 kunmap(kmapped_page);
286 return ret;
287}
288
289/*
290 * Like copy_strings, but get argv and its values from kernel memory.
291 */
292int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
293{
294 int r;
295 mm_segment_t oldfs = get_fs();
296 set_fs(KERNEL_DS);
297 r = copy_strings(argc, (char __user * __user *)argv, bprm);
298 set_fs(oldfs);
299 return r;
300}
301
302EXPORT_SYMBOL(copy_strings_kernel);
303
304#ifdef CONFIG_MMU
305/*
306 * This routine is used to map in a page into an address space: needed by
307 * execve() for the initial stack and environment pages.
308 *
309 * vma->vm_mm->mmap_sem is held for writing.
310 */
311void install_arg_page(struct vm_area_struct *vma,
312 struct page *page, unsigned long address)
313{
314 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 pte_t * pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700316 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if (unlikely(anon_vma_prepare(vma)))
Hugh Dickinsc74df322005-10-29 18:16:23 -0700319 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 flush_dcache_page(page);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -0800322 pte = get_locked_pte(mm, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!pte)
324 goto out;
325 if (!pte_none(*pte)) {
Hugh Dickinsc74df322005-10-29 18:16:23 -0700326 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto out;
328 }
Hugh Dickins42946212005-10-29 18:16:05 -0700329 inc_mm_counter(mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 lru_cache_add_active(page);
331 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
332 page, vma->vm_page_prot))));
Nick Piggin9617d952006-01-06 00:11:12 -0800333 page_add_new_anon_rmap(page, vma, address);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700334 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* no need for flush_tlb */
337 return;
338out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 __free_page(page);
340 force_sig(SIGKILL, current);
341}
342
343#define EXTRA_STACK_VM_PAGES 20 /* random */
344
345int setup_arg_pages(struct linux_binprm *bprm,
346 unsigned long stack_top,
347 int executable_stack)
348{
349 unsigned long stack_base;
350 struct vm_area_struct *mpnt;
351 struct mm_struct *mm = current->mm;
352 int i, ret;
353 long arg_size;
354
355#ifdef CONFIG_STACK_GROWSUP
356 /* Move the argument and environment strings to the bottom of the
357 * stack space.
358 */
359 int offset, j;
360 char *to, *from;
361
362 /* Start by shifting all the pages down */
363 i = 0;
364 for (j = 0; j < MAX_ARG_PAGES; j++) {
365 struct page *page = bprm->page[j];
366 if (!page)
367 continue;
368 bprm->page[i++] = page;
369 }
370
371 /* Now move them within their pages */
372 offset = bprm->p % PAGE_SIZE;
373 to = kmap(bprm->page[0]);
374 for (j = 1; j < i; j++) {
375 memmove(to, to + offset, PAGE_SIZE - offset);
376 from = kmap(bprm->page[j]);
377 memcpy(to + PAGE_SIZE - offset, from, offset);
378 kunmap(bprm->page[j - 1]);
379 to = from;
380 }
381 memmove(to, to + offset, PAGE_SIZE - offset);
382 kunmap(bprm->page[j - 1]);
383
384 /* Limit stack size to 1GB */
385 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
386 if (stack_base > (1 << 30))
387 stack_base = 1 << 30;
388 stack_base = PAGE_ALIGN(stack_top - stack_base);
389
390 /* Adjust bprm->p to point to the end of the strings. */
391 bprm->p = stack_base + PAGE_SIZE * i - offset;
392
393 mm->arg_start = stack_base;
394 arg_size = i << PAGE_SHIFT;
395
396 /* zero pages that were copied above */
397 while (i < MAX_ARG_PAGES)
398 bprm->page[i++] = NULL;
399#else
400 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
401 stack_base = PAGE_ALIGN(stack_base);
402 bprm->p += stack_base;
403 mm->arg_start = bprm->p;
404 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
405#endif
406
407 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
408
409 if (bprm->loader)
410 bprm->loader += stack_base;
411 bprm->exec += stack_base;
412
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800413 mpnt = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!mpnt)
415 return -ENOMEM;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 down_write(&mm->mmap_sem);
418 {
419 mpnt->vm_mm = mm;
420#ifdef CONFIG_STACK_GROWSUP
421 mpnt->vm_start = stack_base;
422 mpnt->vm_end = stack_base + arg_size;
423#else
424 mpnt->vm_end = stack_top;
425 mpnt->vm_start = mpnt->vm_end - arg_size;
426#endif
427 /* Adjust stack execute permissions; explicitly enable
428 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
429 * and leave alone (arch default) otherwise. */
430 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
431 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
432 else if (executable_stack == EXSTACK_DISABLE_X)
433 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
434 else
435 mpnt->vm_flags = VM_STACK_FLAGS;
436 mpnt->vm_flags |= mm->def_flags;
437 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
438 if ((ret = insert_vm_struct(mm, mpnt))) {
439 up_write(&mm->mmap_sem);
440 kmem_cache_free(vm_area_cachep, mpnt);
441 return ret;
442 }
443 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
444 }
445
446 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
447 struct page *page = bprm->page[i];
448 if (page) {
449 bprm->page[i] = NULL;
450 install_arg_page(mpnt, page, stack_base);
451 }
452 stack_base += PAGE_SIZE;
453 }
454 up_write(&mm->mmap_sem);
455
456 return 0;
457}
458
459EXPORT_SYMBOL(setup_arg_pages);
460
461#define free_arg_pages(bprm) do { } while (0)
462
463#else
464
465static inline void free_arg_pages(struct linux_binprm *bprm)
466{
467 int i;
468
469 for (i = 0; i < MAX_ARG_PAGES; i++) {
470 if (bprm->page[i])
471 __free_page(bprm->page[i]);
472 bprm->page[i] = NULL;
473 }
474}
475
476#endif /* CONFIG_MMU */
477
478struct file *open_exec(const char *name)
479{
480 struct nameidata nd;
481 int err;
482 struct file *file;
483
Oleg Drokinb5005312006-03-25 03:07:01 -0800484 err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 file = ERR_PTR(err);
486
487 if (!err) {
488 struct inode *inode = nd.dentry->d_inode;
489 file = ERR_PTR(-EACCES);
490 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
491 S_ISREG(inode->i_mode)) {
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800492 int err = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 file = ERR_PTR(err);
494 if (!err) {
Trond Myklebust834f2a42005-10-18 14:20:16 -0700495 file = nameidata_to_filp(&nd, O_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!IS_ERR(file)) {
497 err = deny_write_access(file);
498 if (err) {
499 fput(file);
500 file = ERR_PTR(err);
501 }
502 }
503out:
504 return file;
505 }
506 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700507 release_open_intent(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 path_release(&nd);
509 }
510 goto out;
511}
512
513EXPORT_SYMBOL(open_exec);
514
515int kernel_read(struct file *file, unsigned long offset,
516 char *addr, unsigned long count)
517{
518 mm_segment_t old_fs;
519 loff_t pos = offset;
520 int result;
521
522 old_fs = get_fs();
523 set_fs(get_ds());
524 /* The cast to a user pointer is valid due to the set_fs() */
525 result = vfs_read(file, (void __user *)addr, count, &pos);
526 set_fs(old_fs);
527 return result;
528}
529
530EXPORT_SYMBOL(kernel_read);
531
532static int exec_mmap(struct mm_struct *mm)
533{
534 struct task_struct *tsk;
535 struct mm_struct * old_mm, *active_mm;
536
537 /* Notify parent that we're no longer interested in the old VM */
538 tsk = current;
539 old_mm = current->mm;
540 mm_release(tsk, old_mm);
541
542 if (old_mm) {
543 /*
544 * Make sure that if there is a core dump in progress
545 * for the old mm, we get out and die instead of going
546 * through with the exec. We must hold mmap_sem around
547 * checking core_waiters and changing tsk->mm. The
548 * core-inducing thread will increment core_waiters for
549 * each thread whose ->mm == old_mm.
550 */
551 down_read(&old_mm->mmap_sem);
552 if (unlikely(old_mm->core_waiters)) {
553 up_read(&old_mm->mmap_sem);
554 return -EINTR;
555 }
556 }
557 task_lock(tsk);
558 active_mm = tsk->active_mm;
559 tsk->mm = mm;
560 tsk->active_mm = mm;
561 activate_mm(active_mm, mm);
562 task_unlock(tsk);
563 arch_pick_mmap_layout(mm);
564 if (old_mm) {
565 up_read(&old_mm->mmap_sem);
Eric Sesterhenn7dddb122006-04-01 01:13:38 +0200566 BUG_ON(active_mm != old_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 mmput(old_mm);
568 return 0;
569 }
570 mmdrop(active_mm);
571 return 0;
572}
573
574/*
575 * This function makes sure the current process has its own signal table,
576 * so that flush_signal_handlers can later reset the handlers without
577 * disturbing other processes. (Other processes might share the signal
578 * table via the CLONE_SIGHAND option to clone().)
579 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800580static int de_thread(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct signal_struct *sig = tsk->signal;
583 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
584 spinlock_t *lock = &oldsighand->siglock;
Oleg Nesterov329f7db2005-11-07 21:12:43 +0300585 struct task_struct *leader = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int count;
587
588 /*
Davide Libenzifba2afa2007-05-10 22:23:13 -0700589 * Tell all the sighand listeners that this sighand has
590 * been detached. The signalfd_detach() function grabs the
591 * sighand lock, if signal listeners are present on the sighand.
592 */
593 signalfd_detach(tsk);
594
595 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * If we don't share sighandlers, then we aren't sharing anything
597 * and we can just re-use it all.
598 */
599 if (atomic_read(&oldsighand->count) <= 1) {
600 BUG_ON(atomic_read(&sig->count) != 1);
601 exit_itimers(sig);
602 return 0;
603 }
604
605 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
606 if (!newsighand)
607 return -ENOMEM;
608
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700609 if (thread_group_empty(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto no_thread_group;
611
612 /*
613 * Kill all other threads in the thread group.
614 * We must hold tasklist_lock to call zap_other_threads.
615 */
616 read_lock(&tasklist_lock);
617 spin_lock_irq(lock);
618 if (sig->flags & SIGNAL_GROUP_EXIT) {
619 /*
620 * Another group action in progress, just
621 * return so that the signal is processed.
622 */
623 spin_unlock_irq(lock);
624 read_unlock(&tasklist_lock);
625 kmem_cache_free(sighand_cachep, newsighand);
626 return -EAGAIN;
627 }
Oleg Nesterov14342612006-03-28 16:10:59 -0800628
629 /*
630 * child_reaper ignores SIGKILL, change it now.
631 * Reparenting needs write_lock on tasklist_lock,
632 * so it is safe to do it under read_lock.
633 */
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -0800634 if (unlikely(tsk->group_leader == child_reaper(tsk)))
635 tsk->nsproxy->pid_ns->child_reaper = tsk;
Oleg Nesterov14342612006-03-28 16:10:59 -0800636
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700637 zap_other_threads(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 read_unlock(&tasklist_lock);
639
640 /*
641 * Account for the thread group leader hanging around:
642 */
Oleg Nesterov9e4e23b2005-10-30 15:01:37 -0800643 count = 1;
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700644 if (!thread_group_leader(tsk)) {
Oleg Nesterov9e4e23b2005-10-30 15:01:37 -0800645 count = 2;
Roland McGrath53231252005-07-12 13:58:27 -0700646 /*
647 * The SIGALRM timer survives the exec, but needs to point
648 * at us as the new group leader now. We have a race with
649 * a timer firing now getting the old leader, so we need to
650 * synchronize with any firing (by calling del_timer_sync)
651 * before we can safely let the old group leader die.
652 */
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700653 sig->tsk = tsk;
Oleg Nesterov932aeaf2005-10-30 15:02:17 -0800654 spin_unlock_irq(lock);
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800655 if (hrtimer_cancel(&sig->real_timer))
656 hrtimer_restart(&sig->real_timer);
Oleg Nesterov932aeaf2005-10-30 15:02:17 -0800657 spin_lock_irq(lock);
Roland McGrath53231252005-07-12 13:58:27 -0700658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 while (atomic_read(&sig->count) > count) {
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700660 sig->group_exit_task = tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 sig->notify_count = count;
662 __set_current_state(TASK_UNINTERRUPTIBLE);
663 spin_unlock_irq(lock);
664 schedule();
665 spin_lock_irq(lock);
666 }
667 sig->group_exit_task = NULL;
668 sig->notify_count = 0;
669 spin_unlock_irq(lock);
670
671 /*
672 * At this point all other threads have exited, all we have to
673 * do is to wait for the thread group leader to become inactive,
674 * and to assume its PID:
675 */
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700676 if (!thread_group_leader(tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Wait for the thread group leader to be a zombie.
679 * It should already be zombie at this point, most
680 * of the time.
681 */
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700682 leader = tsk->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 while (leader->exit_state != EXIT_ZOMBIE)
684 yield();
685
Roland McGrathf5e90282006-04-10 22:54:16 -0700686 /*
687 * The only record we have of the real-time age of a
688 * process, regardless of execs it's done, is start_time.
689 * All the past CPU time is accumulated in signal_struct
690 * from sister threads now dead. But in this non-leader
691 * exec, nothing survives from the original leader thread,
692 * whose birth marks the true age of this process now.
693 * When we take on its identity by switching to its PID, we
694 * also take its birthdate (always earlier than our own).
695 */
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700696 tsk->start_time = leader->start_time;
Roland McGrathf5e90282006-04-10 22:54:16 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 write_lock_irq(&tasklist_lock);
699
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700700 BUG_ON(leader->tgid != tsk->tgid);
701 BUG_ON(tsk->pid == tsk->tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * An exec() starts a new thread group with the
704 * TGID of the previous thread group. Rehash the
705 * two threads with a switched PID, and release
706 * the former thread group leader:
707 */
Eric W. Biedermand73d6522006-03-28 16:11:03 -0800708
709 /* Become a process group leader with the old leader's pid.
Eric W. Biedermanc18258c2006-09-27 01:51:06 -0700710 * The old leader becomes a thread of the this thread group.
711 * Note: The old leader also uses this pid until release_task
Eric W. Biedermand73d6522006-03-28 16:11:03 -0800712 * is called. Odd but simple and correct.
713 */
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700714 detach_pid(tsk, PIDTYPE_PID);
715 tsk->pid = leader->pid;
Sukadev Bhattiprolue713d0d2007-05-10 22:22:58 -0700716 attach_pid(tsk, PIDTYPE_PID, find_pid(tsk->pid));
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700717 transfer_pid(leader, tsk, PIDTYPE_PGID);
718 transfer_pid(leader, tsk, PIDTYPE_SID);
719 list_replace_rcu(&leader->tasks, &tsk->tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700721 tsk->group_leader = tsk;
722 leader->group_leader = tsk;
Eric W. Biedermande12a782006-04-10 17:16:49 -0600723
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700724 tsk->exit_signal = SIGCHLD;
Oleg Nesterov962b5642005-11-23 13:37:43 -0800725
726 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
727 leader->exit_state = EXIT_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 /*
Alexander Nybergfb085cf2005-09-14 18:54:06 +0200733 * There may be one thread left which is just exiting,
734 * but it's safe to stop telling the group to kill themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
736 sig->flags = 0;
737
738no_thread_group:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 exit_itimers(sig);
Oleg Nesterov329f7db2005-11-07 21:12:43 +0300740 if (leader)
741 release_task(leader);
742
743 BUG_ON(atomic_read(&sig->count) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (atomic_read(&oldsighand->count) == 1) {
746 /*
747 * Now that we nuked the rest of the thread group,
748 * it turns out we are not sharing sighand any more either.
749 * So we can just keep it.
750 */
751 kmem_cache_free(sighand_cachep, newsighand);
752 } else {
753 /*
754 * Move our state over to newsighand and switch it in.
755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 atomic_set(&newsighand->count, 1);
757 memcpy(newsighand->action, oldsighand->action,
758 sizeof(newsighand->action));
759
760 write_lock_irq(&tasklist_lock);
761 spin_lock(&oldsighand->siglock);
Dave Jones513627d2006-08-27 01:23:57 -0700762 spin_lock_nested(&newsighand->siglock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700764 rcu_assign_pointer(tsk->sighand, newsighand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 recalc_sigpending();
766
767 spin_unlock(&newsighand->siglock);
768 spin_unlock(&oldsighand->siglock);
769 write_unlock_irq(&tasklist_lock);
770
Davide Libenzifba2afa2007-05-10 22:23:13 -0700771 __cleanup_sighand(oldsighand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Eric W. Biedermanaafe6c22006-09-27 01:51:13 -0700774 BUG_ON(!thread_group_leader(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 0;
776}
777
778/*
779 * These functions flushes out all traces of the currently running executable
780 * so that a new one can be started
781 */
782
Arjan van de Ven858119e2006-01-14 13:20:43 -0800783static void flush_old_files(struct files_struct * files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 long j = -1;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700786 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 spin_lock(&files->file_lock);
789 for (;;) {
790 unsigned long set, i;
791
792 j++;
793 i = j * __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700794 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800795 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700797 set = fdt->close_on_exec->fds_bits[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!set)
799 continue;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700800 fdt->close_on_exec->fds_bits[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 spin_unlock(&files->file_lock);
802 for ( ; set ; i++,set >>= 1) {
803 if (set & 1) {
804 sys_close(i);
805 }
806 }
807 spin_lock(&files->file_lock);
808
809 }
810 spin_unlock(&files->file_lock);
811}
812
813void get_task_comm(char *buf, struct task_struct *tsk)
814{
815 /* buf must be at least sizeof(tsk->comm) in size */
816 task_lock(tsk);
817 strncpy(buf, tsk->comm, sizeof(tsk->comm));
818 task_unlock(tsk);
819}
820
821void set_task_comm(struct task_struct *tsk, char *buf)
822{
823 task_lock(tsk);
824 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
825 task_unlock(tsk);
826}
827
828int flush_old_exec(struct linux_binprm * bprm)
829{
830 char * name;
831 int i, ch, retval;
832 struct files_struct *files;
833 char tcomm[sizeof(current->comm)];
834
835 /*
836 * Make sure we have a private signal table and that
837 * we are unassociated from the previous thread group.
838 */
839 retval = de_thread(current);
840 if (retval)
841 goto out;
842
843 /*
844 * Make sure we have private file handles. Ask the
845 * fork helper to do the work for us and the exit
846 * helper to do the cleanup of the old one.
847 */
848 files = current->files; /* refcounted so safe to hold */
849 retval = unshare_files();
850 if (retval)
851 goto out;
852 /*
853 * Release all of the old mmap stuff
854 */
855 retval = exec_mmap(bprm->mm);
856 if (retval)
857 goto mmap_failed;
858
859 bprm->mm = NULL; /* We're using it now */
860
861 /* This is the point of no return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 put_files_struct(files);
863
864 current->sas_ss_sp = current->sas_ss_size = 0;
865
866 if (current->euid == current->uid && current->egid == current->gid)
867 current->mm->dumpable = 1;
Alan Coxd6e71142005-06-23 00:09:43 -0700868 else
869 current->mm->dumpable = suid_dumpable;
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 name = bprm->filename;
Paolo 'Blaisorblade' Giarrusso36772092005-05-05 16:16:12 -0700872
873 /* Copies the binary name from after last slash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 for (i=0; (ch = *(name++)) != '\0';) {
875 if (ch == '/')
Paolo 'Blaisorblade' Giarrusso36772092005-05-05 16:16:12 -0700876 i = 0; /* overwrite what we wrote */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 else
878 if (i < (sizeof(tcomm) - 1))
879 tcomm[i++] = ch;
880 }
881 tcomm[i] = '\0';
882 set_task_comm(current, tcomm);
883
884 current->flags &= ~PF_RANDOMIZE;
885 flush_thread();
886
Benjamin Herrenschmidt0551fbd2006-02-28 16:59:19 -0800887 /* Set the new mm task size. We have to do that late because it may
888 * depend on TIF_32BIT which is only updated in flush_thread() on
889 * some architectures like powerpc
890 */
891 current->mm->task_size = TASK_SIZE;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800894 file_permission(bprm->file, MAY_READ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
896 suid_keys(current);
Alan Coxd6e71142005-06-23 00:09:43 -0700897 current->mm->dumpable = suid_dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
900 /* An exec changes our domain. We are no longer part of the thread
901 group */
902
903 current->self_exec_id++;
904
905 flush_signal_handlers(current, 0);
906 flush_old_files(current->files);
907
908 return 0;
909
910mmap_failed:
Kirill Korotaev3b9b8ab2006-09-29 02:00:05 -0700911 reset_files_struct(current, files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912out:
913 return retval;
914}
915
916EXPORT_SYMBOL(flush_old_exec);
917
918/*
919 * Fill the binprm structure from the inode.
920 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
921 */
922int prepare_binprm(struct linux_binprm *bprm)
923{
924 int mode;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800925 struct inode * inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int retval;
927
928 mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (bprm->file->f_op == NULL)
930 return -EACCES;
931
932 bprm->e_uid = current->euid;
933 bprm->e_gid = current->egid;
934
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800935 if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /* Set-uid? */
937 if (mode & S_ISUID) {
938 current->personality &= ~PER_CLEAR_ON_SETID;
939 bprm->e_uid = inode->i_uid;
940 }
941
942 /* Set-gid? */
943 /*
944 * If setgid is set but no group execute bit then this
945 * is a candidate for mandatory locking, not a setgid
946 * executable.
947 */
948 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
949 current->personality &= ~PER_CLEAR_ON_SETID;
950 bprm->e_gid = inode->i_gid;
951 }
952 }
953
954 /* fill in binprm security blob */
955 retval = security_bprm_set(bprm);
956 if (retval)
957 return retval;
958
959 memset(bprm->buf,0,BINPRM_BUF_SIZE);
960 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
961}
962
963EXPORT_SYMBOL(prepare_binprm);
964
Arjan van de Ven858119e2006-01-14 13:20:43 -0800965static int unsafe_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 int unsafe = 0;
968 if (p->ptrace & PT_PTRACED) {
969 if (p->ptrace & PT_PTRACE_CAP)
970 unsafe |= LSM_UNSAFE_PTRACE_CAP;
971 else
972 unsafe |= LSM_UNSAFE_PTRACE;
973 }
974 if (atomic_read(&p->fs->count) > 1 ||
975 atomic_read(&p->files->count) > 1 ||
976 atomic_read(&p->sighand->count) > 1)
977 unsafe |= LSM_UNSAFE_SHARE;
978
979 return unsafe;
980}
981
982void compute_creds(struct linux_binprm *bprm)
983{
984 int unsafe;
985
986 if (bprm->e_uid != current->uid)
987 suid_keys(current);
988 exec_keys(current);
989
990 task_lock(current);
991 unsafe = unsafe_exec(current);
992 security_bprm_apply_creds(bprm, unsafe);
993 task_unlock(current);
994 security_bprm_post_apply_creds(bprm);
995}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996EXPORT_SYMBOL(compute_creds);
997
Nick Piggin4fc75ff2007-05-08 00:25:16 -0700998/*
999 * Arguments are '\0' separated strings found at the location bprm->p
1000 * points to; chop off the first by relocating brpm->p to right after
1001 * the first '\0' encountered.
1002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003void remove_arg_zero(struct linux_binprm *bprm)
1004{
1005 if (bprm->argc) {
Nick Piggin4fc75ff2007-05-08 00:25:16 -07001006 char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Nick Piggin4fc75ff2007-05-08 00:25:16 -07001008 do {
1009 unsigned long offset;
1010 unsigned long index;
1011 char *kaddr;
1012 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Nick Piggin4fc75ff2007-05-08 00:25:16 -07001014 offset = bprm->p & ~PAGE_MASK;
1015 index = bprm->p >> PAGE_SHIFT;
1016
1017 page = bprm->page[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 kaddr = kmap_atomic(page, KM_USER0);
Nick Piggin4fc75ff2007-05-08 00:25:16 -07001019
1020 /* run through page until we reach end or find NUL */
1021 do {
1022 ch = *(kaddr + offset);
1023
1024 /* discard that character... */
1025 bprm->p++;
1026 offset++;
1027 } while (offset < PAGE_SIZE && ch != '\0');
1028
1029 kunmap_atomic(kaddr, KM_USER0);
1030
1031 /* free the old page */
1032 if (offset == PAGE_SIZE) {
1033 __free_page(page);
1034 bprm->page[index] = NULL;
1035 }
1036 } while (ch != '\0');
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 bprm->argc--;
1039 }
1040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041EXPORT_SYMBOL(remove_arg_zero);
1042
1043/*
1044 * cycle the list of binary formats handler, until one recognizes the image
1045 */
1046int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1047{
1048 int try,retval;
1049 struct linux_binfmt *fmt;
1050#ifdef __alpha__
1051 /* handle /sbin/loader.. */
1052 {
1053 struct exec * eh = (struct exec *) bprm->buf;
1054
1055 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1056 (eh->fh.f_flags & 0x3000) == 0x3000)
1057 {
1058 struct file * file;
1059 unsigned long loader;
1060
1061 allow_write_access(bprm->file);
1062 fput(bprm->file);
1063 bprm->file = NULL;
1064
1065 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1066
1067 file = open_exec("/sbin/loader");
1068 retval = PTR_ERR(file);
1069 if (IS_ERR(file))
1070 return retval;
1071
1072 /* Remember if the application is TASO. */
1073 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1074
1075 bprm->file = file;
1076 bprm->loader = loader;
1077 retval = prepare_binprm(bprm);
1078 if (retval<0)
1079 return retval;
1080 /* should call search_binary_handler recursively here,
1081 but it does not matter */
1082 }
1083 }
1084#endif
1085 retval = security_bprm_check(bprm);
1086 if (retval)
1087 return retval;
1088
1089 /* kernel module loader fixup */
1090 /* so we don't try to load run modprobe in kernel space. */
1091 set_fs(USER_DS);
Al Viro473ae302006-04-26 14:04:08 -04001092
1093 retval = audit_bprm(bprm);
1094 if (retval)
1095 return retval;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 retval = -ENOENT;
1098 for (try=0; try<2; try++) {
1099 read_lock(&binfmt_lock);
1100 for (fmt = formats ; fmt ; fmt = fmt->next) {
1101 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1102 if (!fn)
1103 continue;
1104 if (!try_module_get(fmt->module))
1105 continue;
1106 read_unlock(&binfmt_lock);
1107 retval = fn(bprm, regs);
1108 if (retval >= 0) {
1109 put_binfmt(fmt);
1110 allow_write_access(bprm->file);
1111 if (bprm->file)
1112 fput(bprm->file);
1113 bprm->file = NULL;
1114 current->did_exec = 1;
Matt Helsley9f460802005-11-07 00:59:16 -08001115 proc_exec_connector(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return retval;
1117 }
1118 read_lock(&binfmt_lock);
1119 put_binfmt(fmt);
1120 if (retval != -ENOEXEC || bprm->mm == NULL)
1121 break;
1122 if (!bprm->file) {
1123 read_unlock(&binfmt_lock);
1124 return retval;
1125 }
1126 }
1127 read_unlock(&binfmt_lock);
1128 if (retval != -ENOEXEC || bprm->mm == NULL) {
1129 break;
1130#ifdef CONFIG_KMOD
1131 }else{
1132#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1133 if (printable(bprm->buf[0]) &&
1134 printable(bprm->buf[1]) &&
1135 printable(bprm->buf[2]) &&
1136 printable(bprm->buf[3]))
1137 break; /* -ENOEXEC */
1138 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1139#endif
1140 }
1141 }
1142 return retval;
1143}
1144
1145EXPORT_SYMBOL(search_binary_handler);
1146
1147/*
1148 * sys_execve() executes a new program.
1149 */
1150int do_execve(char * filename,
1151 char __user *__user *argv,
1152 char __user *__user *envp,
1153 struct pt_regs * regs)
1154{
1155 struct linux_binprm *bprm;
1156 struct file *file;
1157 int retval;
1158 int i;
1159
1160 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001161 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!bprm)
1163 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 file = open_exec(filename);
1166 retval = PTR_ERR(file);
1167 if (IS_ERR(file))
1168 goto out_kfree;
1169
1170 sched_exec();
1171
1172 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1173
1174 bprm->file = file;
1175 bprm->filename = filename;
1176 bprm->interp = filename;
1177 bprm->mm = mm_alloc();
1178 retval = -ENOMEM;
1179 if (!bprm->mm)
1180 goto out_file;
1181
1182 retval = init_new_context(current, bprm->mm);
1183 if (retval < 0)
1184 goto out_mm;
1185
1186 bprm->argc = count(argv, bprm->p / sizeof(void *));
1187 if ((retval = bprm->argc) < 0)
1188 goto out_mm;
1189
1190 bprm->envc = count(envp, bprm->p / sizeof(void *));
1191 if ((retval = bprm->envc) < 0)
1192 goto out_mm;
1193
1194 retval = security_bprm_alloc(bprm);
1195 if (retval)
1196 goto out;
1197
1198 retval = prepare_binprm(bprm);
1199 if (retval < 0)
1200 goto out;
1201
1202 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1203 if (retval < 0)
1204 goto out;
1205
1206 bprm->exec = bprm->p;
1207 retval = copy_strings(bprm->envc, envp, bprm);
1208 if (retval < 0)
1209 goto out;
1210
1211 retval = copy_strings(bprm->argc, argv, bprm);
1212 if (retval < 0)
1213 goto out;
1214
1215 retval = search_binary_handler(bprm,regs);
1216 if (retval >= 0) {
1217 free_arg_pages(bprm);
1218
1219 /* execve success */
1220 security_bprm_free(bprm);
1221 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 kfree(bprm);
1223 return retval;
1224 }
1225
1226out:
1227 /* Something went wrong, return the inode and free the argument pages*/
1228 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1229 struct page * page = bprm->page[i];
1230 if (page)
1231 __free_page(page);
1232 }
1233
1234 if (bprm->security)
1235 security_bprm_free(bprm);
1236
1237out_mm:
1238 if (bprm->mm)
1239 mmdrop(bprm->mm);
1240
1241out_file:
1242 if (bprm->file) {
1243 allow_write_access(bprm->file);
1244 fput(bprm->file);
1245 }
1246
1247out_kfree:
1248 kfree(bprm);
1249
1250out_ret:
1251 return retval;
1252}
1253
1254int set_binfmt(struct linux_binfmt *new)
1255{
1256 struct linux_binfmt *old = current->binfmt;
1257
1258 if (new) {
1259 if (!try_module_get(new->module))
1260 return -1;
1261 }
1262 current->binfmt = new;
1263 if (old)
1264 module_put(old->module);
1265 return 0;
1266}
1267
1268EXPORT_SYMBOL(set_binfmt);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/* format_corename will inspect the pattern parameter, and output a
1271 * name into corename, which must have space for at least
1272 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1273 */
Alan Coxc4bbafd2007-04-16 22:53:13 -07001274static int format_corename(char *corename, const char *pattern, long signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 const char *pat_ptr = pattern;
1277 char *out_ptr = corename;
1278 char *const out_end = corename + CORENAME_MAX_SIZE;
1279 int rc;
1280 int pid_in_pattern = 0;
Alan Coxc4bbafd2007-04-16 22:53:13 -07001281 int ispipe = 0;
1282
1283 if (*pattern == '|')
1284 ispipe = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /* Repeat as long as we have more pattern to process and more output
1287 space */
1288 while (*pat_ptr) {
1289 if (*pat_ptr != '%') {
1290 if (out_ptr == out_end)
1291 goto out;
1292 *out_ptr++ = *pat_ptr++;
1293 } else {
1294 switch (*++pat_ptr) {
1295 case 0:
1296 goto out;
1297 /* Double percent, output one percent */
1298 case '%':
1299 if (out_ptr == out_end)
1300 goto out;
1301 *out_ptr++ = '%';
1302 break;
1303 /* pid */
1304 case 'p':
1305 pid_in_pattern = 1;
1306 rc = snprintf(out_ptr, out_end - out_ptr,
1307 "%d", current->tgid);
1308 if (rc > out_end - out_ptr)
1309 goto out;
1310 out_ptr += rc;
1311 break;
1312 /* uid */
1313 case 'u':
1314 rc = snprintf(out_ptr, out_end - out_ptr,
1315 "%d", current->uid);
1316 if (rc > out_end - out_ptr)
1317 goto out;
1318 out_ptr += rc;
1319 break;
1320 /* gid */
1321 case 'g':
1322 rc = snprintf(out_ptr, out_end - out_ptr,
1323 "%d", current->gid);
1324 if (rc > out_end - out_ptr)
1325 goto out;
1326 out_ptr += rc;
1327 break;
1328 /* signal that caused the coredump */
1329 case 's':
1330 rc = snprintf(out_ptr, out_end - out_ptr,
1331 "%ld", signr);
1332 if (rc > out_end - out_ptr)
1333 goto out;
1334 out_ptr += rc;
1335 break;
1336 /* UNIX time of coredump */
1337 case 't': {
1338 struct timeval tv;
1339 do_gettimeofday(&tv);
1340 rc = snprintf(out_ptr, out_end - out_ptr,
1341 "%lu", tv.tv_sec);
1342 if (rc > out_end - out_ptr)
1343 goto out;
1344 out_ptr += rc;
1345 break;
1346 }
1347 /* hostname */
1348 case 'h':
1349 down_read(&uts_sem);
1350 rc = snprintf(out_ptr, out_end - out_ptr,
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001351 "%s", utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 up_read(&uts_sem);
1353 if (rc > out_end - out_ptr)
1354 goto out;
1355 out_ptr += rc;
1356 break;
1357 /* executable */
1358 case 'e':
1359 rc = snprintf(out_ptr, out_end - out_ptr,
1360 "%s", current->comm);
1361 if (rc > out_end - out_ptr)
1362 goto out;
1363 out_ptr += rc;
1364 break;
1365 default:
1366 break;
1367 }
1368 ++pat_ptr;
1369 }
1370 }
1371 /* Backward compatibility with core_uses_pid:
1372 *
1373 * If core_pattern does not include a %p (as is the default)
1374 * and core_uses_pid is set, then .%pid will be appended to
Alan Coxc4bbafd2007-04-16 22:53:13 -07001375 * the filename. Do not do this for piped commands. */
1376 if (!ispipe && !pid_in_pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1378 rc = snprintf(out_ptr, out_end - out_ptr,
1379 ".%d", current->tgid);
1380 if (rc > out_end - out_ptr)
1381 goto out;
1382 out_ptr += rc;
1383 }
Alan Coxc4bbafd2007-04-16 22:53:13 -07001384out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 *out_ptr = 0;
Alan Coxc4bbafd2007-04-16 22:53:13 -07001386 return ispipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001389static void zap_process(struct task_struct *start)
Oleg Nesterovaceecc042006-06-26 00:26:05 -07001390{
1391 struct task_struct *t;
Oleg Nesterov281de332006-06-26 00:26:06 -07001392
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001393 start->signal->flags = SIGNAL_GROUP_EXIT;
1394 start->signal->group_stop_count = 0;
Oleg Nesterovaceecc042006-06-26 00:26:05 -07001395
1396 t = start;
1397 do {
1398 if (t != current && t->mm) {
1399 t->mm->core_waiters++;
Oleg Nesterov281de332006-06-26 00:26:06 -07001400 sigaddset(&t->pending.signal, SIGKILL);
1401 signal_wake_up(t, 1);
Oleg Nesterovaceecc042006-06-26 00:26:05 -07001402 }
1403 } while ((t = next_thread(t)) != start);
1404}
1405
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001406static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1407 int exit_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct task_struct *g, *p;
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001410 unsigned long flags;
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001411 int err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001413 spin_lock_irq(&tsk->sighand->siglock);
1414 if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT)) {
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001415 tsk->signal->group_exit_code = exit_code;
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001416 zap_process(tsk);
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001417 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001419 spin_unlock_irq(&tsk->sighand->siglock);
1420 if (err)
1421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001423 if (atomic_read(&mm->mm_users) == mm->core_waiters + 1)
1424 goto done;
1425
Oleg Nesterov7b1c6152006-06-26 00:26:08 -07001426 rcu_read_lock();
Oleg Nesterovaceecc042006-06-26 00:26:05 -07001427 for_each_process(g) {
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001428 if (g == tsk->group_leader)
1429 continue;
1430
Oleg Nesterovaceecc042006-06-26 00:26:05 -07001431 p = g;
1432 do {
1433 if (p->mm) {
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001434 if (p->mm == mm) {
1435 /*
1436 * p->sighand can't disappear, but
1437 * may be changed by de_thread()
1438 */
1439 lock_task_sighand(p, &flags);
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001440 zap_process(p);
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001441 unlock_task_sighand(p, &flags);
1442 }
Oleg Nesterovaceecc042006-06-26 00:26:05 -07001443 break;
1444 }
1445 } while ((p = next_thread(p)) != g);
1446 }
Oleg Nesterov7b1c6152006-06-26 00:26:08 -07001447 rcu_read_unlock();
Oleg Nesterov5debfa62006-06-26 00:26:09 -07001448done:
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001449 return mm->core_waiters;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001452static int coredump_wait(int exit_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001454 struct task_struct *tsk = current;
1455 struct mm_struct *mm = tsk->mm;
1456 struct completion startup_done;
1457 struct completion *vfork_done;
Oleg Nesterov2384f552005-10-30 15:02:47 -08001458 int core_waiters;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001460 init_completion(&mm->core_done);
1461 init_completion(&startup_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 mm->core_startup_done = &startup_done;
1463
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001464 core_waiters = zap_threads(tsk, mm, exit_code);
Oleg Nesterov2384f552005-10-30 15:02:47 -08001465 up_write(&mm->mmap_sem);
1466
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001467 if (unlikely(core_waiters < 0))
1468 goto fail;
1469
1470 /*
1471 * Make sure nobody is waiting for us to release the VM,
1472 * otherwise we can deadlock when we wait on each other
1473 */
1474 vfork_done = tsk->vfork_done;
1475 if (vfork_done) {
1476 tsk->vfork_done = NULL;
1477 complete(vfork_done);
1478 }
1479
Oleg Nesterov2384f552005-10-30 15:02:47 -08001480 if (core_waiters)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 wait_for_completion(&startup_done);
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001482fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 BUG_ON(mm->core_waiters);
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001484 return core_waiters;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1488{
1489 char corename[CORENAME_MAX_SIZE + 1];
1490 struct mm_struct *mm = current->mm;
1491 struct linux_binfmt * binfmt;
1492 struct inode * inode;
1493 struct file * file;
1494 int retval = 0;
Alan Coxd6e71142005-06-23 00:09:43 -07001495 int fsuid = current->fsuid;
1496 int flag = 0;
Andi Kleend025c9d2006-09-30 23:29:28 -07001497 int ispipe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04001499 audit_core_dumps(signr);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 binfmt = current->binfmt;
1502 if (!binfmt || !binfmt->core_dump)
1503 goto fail;
1504 down_write(&mm->mmap_sem);
1505 if (!mm->dumpable) {
1506 up_write(&mm->mmap_sem);
1507 goto fail;
1508 }
Alan Coxd6e71142005-06-23 00:09:43 -07001509
1510 /*
1511 * We cannot trust fsuid as being the "true" uid of the
1512 * process nor do we know its entire history. We only know it
1513 * was tainted so we dump it as root in mode 2.
1514 */
1515 if (mm->dumpable == 2) { /* Setuid core dump mode */
1516 flag = O_EXCL; /* Stop rewrite attacks */
1517 current->fsuid = 0; /* Dump root private */
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 mm->dumpable = 0;
Oleg Nesterov1291cf42005-10-30 15:02:54 -08001520
Oleg Nesterovdcf560c2006-06-26 00:26:08 -07001521 retval = coredump_wait(exit_code);
1522 if (retval < 0)
Oleg Nesterov1291cf42005-10-30 15:02:54 -08001523 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 /*
1526 * Clear any false indication of pending signals that might
1527 * be seen by the filesystem code called to write the core file.
1528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 clear_thread_flag(TIF_SIGPENDING);
1530
1531 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1532 goto fail_unlock;
1533
1534 /*
1535 * lock_kernel() because format_corename() is controlled by sysctl, which
1536 * uses lock_kernel()
1537 */
1538 lock_kernel();
Alan Coxc4bbafd2007-04-16 22:53:13 -07001539 ispipe = format_corename(corename, core_pattern, signr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 unlock_kernel();
Alan Coxc4bbafd2007-04-16 22:53:13 -07001541 if (ispipe) {
Andi Kleend025c9d2006-09-30 23:29:28 -07001542 /* SIGPIPE can happen, but it's just never processed */
1543 if(call_usermodehelper_pipe(corename+1, NULL, NULL, &file)) {
1544 printk(KERN_INFO "Core dump to %s pipe failed\n",
1545 corename);
1546 goto fail_unlock;
1547 }
Andi Kleend025c9d2006-09-30 23:29:28 -07001548 } else
1549 file = filp_open(corename,
Alexey Dobriyan6d4df672006-12-06 20:40:39 -08001550 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
1551 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (IS_ERR(file))
1553 goto fail_unlock;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001554 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (inode->i_nlink > 1)
1556 goto close_fail; /* multiple links - don't dump */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001557 if (!ispipe && d_unhashed(file->f_path.dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 goto close_fail;
1559
Andi Kleend025c9d2006-09-30 23:29:28 -07001560 /* AK: actually i see no reason to not allow this for named pipes etc.,
1561 but keep the previous behaviour for now. */
1562 if (!ispipe && !S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 goto close_fail;
1564 if (!file->f_op)
1565 goto close_fail;
1566 if (!file->f_op->write)
1567 goto close_fail;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001568 if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 goto close_fail;
1570
1571 retval = binfmt->core_dump(signr, regs, file);
1572
1573 if (retval)
1574 current->signal->group_exit_code |= 0x80;
1575close_fail:
1576 filp_close(file, NULL);
1577fail_unlock:
Alan Coxd6e71142005-06-23 00:09:43 -07001578 current->fsuid = fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 complete_all(&mm->core_done);
1580fail:
1581 return retval;
1582}