- pre3:
[davej-history.git] / fs / exec.c
blob1cab95172aadc2840d1fc94892c2e9dcf49d4111
1 /*
2 * linux/fs/exec.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * #!-checking implemented by tytso.
9 */
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.
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.
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.
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 #define __NO_VERSION__
38 #include <linux/module.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgalloc.h>
42 #include <asm/mmu_context.h>
44 #ifdef CONFIG_KMOD
45 #include <linux/kmod.h>
46 #endif
48 static struct linux_binfmt *formats;
49 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
51 int register_binfmt(struct linux_binfmt * fmt)
53 struct linux_binfmt ** tmp = &formats;
55 if (!fmt)
56 return -EINVAL;
57 if (fmt->next)
58 return -EBUSY;
59 write_lock(&binfmt_lock);
60 while (*tmp) {
61 if (fmt == *tmp) {
62 write_unlock(&binfmt_lock);
63 return -EBUSY;
65 tmp = &(*tmp)->next;
67 fmt->next = formats;
68 formats = fmt;
69 write_unlock(&binfmt_lock);
70 return 0;
73 int unregister_binfmt(struct linux_binfmt * fmt)
75 struct linux_binfmt ** tmp = &formats;
77 write_lock(&binfmt_lock);
78 while (*tmp) {
79 if (fmt == *tmp) {
80 *tmp = fmt->next;
81 write_unlock(&binfmt_lock);
82 return 0;
84 tmp = &(*tmp)->next;
86 write_unlock(&binfmt_lock);
87 return -EINVAL;
90 static inline void put_binfmt(struct linux_binfmt * fmt)
92 if (fmt->module)
93 __MOD_DEC_USE_COUNT(fmt->module);
97 * Note that a shared library must be both readable and executable due to
98 * security reasons.
100 * Also note that we take the address to load from from the file itself.
102 asmlinkage long sys_uselib(const char * library)
104 struct file * file;
105 struct nameidata nd;
106 int error;
108 error = user_path_walk(library, &nd);
109 if (error)
110 goto out;
112 error = -EINVAL;
113 if (!S_ISREG(nd.dentry->d_inode->i_mode))
114 goto exit;
116 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC);
117 if (error)
118 goto exit;
120 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
121 error = PTR_ERR(file);
122 if (IS_ERR(file))
123 goto out;
125 error = -ENOEXEC;
126 if(file->f_op && file->f_op->read) {
127 struct linux_binfmt * fmt;
129 read_lock(&binfmt_lock);
130 for (fmt = formats ; fmt ; fmt = fmt->next) {
131 if (!fmt->load_shlib)
132 continue;
133 if (!try_inc_mod_count(fmt->module))
134 continue;
135 read_unlock(&binfmt_lock);
136 error = fmt->load_shlib(file);
137 read_lock(&binfmt_lock);
138 put_binfmt(fmt);
139 if (error != -ENOEXEC)
140 break;
142 read_unlock(&binfmt_lock);
144 fput(file);
145 out:
146 return error;
147 exit:
148 path_release(&nd);
149 goto out;
153 * count() counts the number of arguments/envelopes
155 static int count(char ** argv, int max)
157 int i = 0;
159 if (argv != NULL) {
160 for (;;) {
161 char * p;
162 int error;
164 error = get_user(p,argv);
165 if (error)
166 return error;
167 if (!p)
168 break;
169 argv++;
170 if(++i > max)
171 return -E2BIG;
174 return i;
178 * 'copy_strings()' copies argument/envelope strings from user
179 * memory to free pages in kernel mem. These are in a format ready
180 * to be put directly into the top of new user memory.
182 int copy_strings(int argc,char ** argv, struct linux_binprm *bprm)
184 while (argc-- > 0) {
185 char *str;
186 int len;
187 unsigned long pos;
189 if (get_user(str, argv+argc) || !str || !(len = strnlen_user(str, bprm->p)))
190 return -EFAULT;
191 if (bprm->p < len)
192 return -E2BIG;
194 bprm->p -= len;
195 /* XXX: add architecture specific overflow check here. */
197 pos = bprm->p;
198 while (len > 0) {
199 char *kaddr;
200 int i, new, err;
201 struct page *page;
202 int offset, bytes_to_copy;
204 offset = pos % PAGE_SIZE;
205 i = pos/PAGE_SIZE;
206 page = bprm->page[i];
207 new = 0;
208 if (!page) {
209 page = alloc_page(GFP_HIGHUSER);
210 bprm->page[i] = page;
211 if (!page)
212 return -ENOMEM;
213 new = 1;
215 kaddr = (char *)kmap(page);
217 if (new && offset)
218 memset(kaddr, 0, offset);
219 bytes_to_copy = PAGE_SIZE - offset;
220 if (bytes_to_copy > len) {
221 bytes_to_copy = len;
222 if (new)
223 memset(kaddr+offset+len, 0, PAGE_SIZE-offset-len);
225 err = copy_from_user(kaddr + offset, str, bytes_to_copy);
226 flush_dcache_page(page);
227 flush_page_to_ram(page);
228 kunmap(page);
230 if (err)
231 return -EFAULT;
233 pos += bytes_to_copy;
234 str += bytes_to_copy;
235 len -= bytes_to_copy;
238 return 0;
242 * Like copy_strings, but get argv and its values from kernel memory.
244 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
246 int r;
247 mm_segment_t oldfs = get_fs();
248 set_fs(KERNEL_DS);
249 r = copy_strings(argc, argv, bprm);
250 set_fs(oldfs);
251 return r;
255 * This routine is used to map in a page into an address space: needed by
256 * execve() for the initial stack and environment pages.
258 void put_dirty_page(struct task_struct * tsk, struct page *page, unsigned long address)
260 pgd_t * pgd;
261 pmd_t * pmd;
262 pte_t * pte;
264 if (page_count(page) != 1)
265 printk("mem_map disagrees with %p at %08lx\n", page, address);
266 pgd = pgd_offset(tsk->mm, address);
267 pmd = pmd_alloc(pgd, address);
268 if (!pmd) {
269 __free_page(page);
270 force_sig(SIGKILL, tsk);
271 return;
273 pte = pte_alloc(pmd, address);
274 if (!pte) {
275 __free_page(page);
276 force_sig(SIGKILL, tsk);
277 return;
279 if (!pte_none(*pte)) {
280 pte_ERROR(*pte);
281 __free_page(page);
282 return;
284 flush_page_to_ram(page);
285 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, PAGE_COPY))));
286 /* no need for flush_tlb */
289 int setup_arg_pages(struct linux_binprm *bprm)
291 unsigned long stack_base;
292 struct vm_area_struct *mpnt;
293 int i;
295 stack_base = STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
297 bprm->p += stack_base;
298 if (bprm->loader)
299 bprm->loader += stack_base;
300 bprm->exec += stack_base;
302 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
303 if (!mpnt)
304 return -ENOMEM;
306 down(&current->mm->mmap_sem);
308 mpnt->vm_mm = current->mm;
309 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
310 mpnt->vm_end = STACK_TOP;
311 mpnt->vm_page_prot = PAGE_COPY;
312 mpnt->vm_flags = VM_STACK_FLAGS;
313 mpnt->vm_ops = NULL;
314 mpnt->vm_pgoff = 0;
315 mpnt->vm_file = NULL;
316 mpnt->vm_private_data = (void *) 0;
317 vmlist_modify_lock(current->mm);
318 insert_vm_struct(current->mm, mpnt);
319 vmlist_modify_unlock(current->mm);
320 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
323 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
324 struct page *page = bprm->page[i];
325 if (page) {
326 bprm->page[i] = NULL;
327 current->mm->rss++;
328 put_dirty_page(current,page,stack_base);
330 stack_base += PAGE_SIZE;
332 up(&current->mm->mmap_sem);
334 return 0;
337 struct file *open_exec(const char *name)
339 struct nameidata nd;
340 struct inode *inode;
341 struct file *file;
342 int err = 0;
344 if (path_init(name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
345 err = path_walk(name, &nd);
346 file = ERR_PTR(err);
347 if (!err) {
348 inode = nd.dentry->d_inode;
349 file = ERR_PTR(-EACCES);
350 if (!IS_NOEXEC(inode) && S_ISREG(inode->i_mode)) {
351 int err = permission(inode, MAY_EXEC);
352 file = ERR_PTR(err);
353 if (!err) {
354 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
355 if (!IS_ERR(file)) {
356 err = deny_write_access(file);
357 if (err) {
358 fput(file);
359 file = ERR_PTR(err);
362 out:
363 return file;
366 path_release(&nd);
368 goto out;
371 int kernel_read(struct file *file, unsigned long offset,
372 char * addr, unsigned long count)
374 mm_segment_t old_fs;
375 loff_t pos = offset;
376 int result = -ENOSYS;
378 if (!file->f_op->read)
379 goto fail;
380 old_fs = get_fs();
381 set_fs(get_ds());
382 result = file->f_op->read(file, addr, count, &pos);
383 set_fs(old_fs);
384 fail:
385 return result;
388 static int exec_mmap(void)
390 struct mm_struct * mm, * old_mm;
392 old_mm = current->mm;
393 if (old_mm && atomic_read(&old_mm->mm_users) == 1) {
394 flush_cache_mm(old_mm);
395 mm_release();
396 exit_mmap(old_mm);
397 flush_tlb_mm(old_mm);
398 return 0;
401 mm = mm_alloc();
402 if (mm) {
403 struct mm_struct *active_mm = current->active_mm;
405 if (init_new_context(current, mm)) {
406 mmdrop(mm);
407 return -ENOMEM;
409 task_lock(current);
410 current->mm = mm;
411 current->active_mm = mm;
412 task_unlock(current);
413 activate_mm(active_mm, mm);
414 mm_release();
415 if (old_mm) {
416 if (active_mm != old_mm) BUG();
417 mmput(old_mm);
418 return 0;
420 mmdrop(active_mm);
421 return 0;
423 return -ENOMEM;
427 * This function makes sure the current process has its own signal table,
428 * so that flush_signal_handlers can later reset the handlers without
429 * disturbing other processes. (Other processes might share the signal
430 * table via the CLONE_SIGNAL option to clone().)
433 static inline int make_private_signals(void)
435 struct signal_struct * newsig;
437 if (atomic_read(&current->sig->count) <= 1)
438 return 0;
439 newsig = kmem_cache_alloc(sigact_cachep, GFP_KERNEL);
440 if (newsig == NULL)
441 return -ENOMEM;
442 spin_lock_init(&newsig->siglock);
443 atomic_set(&newsig->count, 1);
444 memcpy(newsig->action, current->sig->action, sizeof(newsig->action));
445 spin_lock_irq(&current->sigmask_lock);
446 current->sig = newsig;
447 spin_unlock_irq(&current->sigmask_lock);
448 return 0;
452 * If make_private_signals() made a copy of the signal table, decrement the
453 * refcount of the original table, and free it if necessary.
454 * We don't do that in make_private_signals() so that we can back off
455 * in flush_old_exec() if an error occurs after calling make_private_signals().
458 static inline void release_old_signals(struct signal_struct * oldsig)
460 if (current->sig == oldsig)
461 return;
462 if (atomic_dec_and_test(&oldsig->count))
463 kmem_cache_free(sigact_cachep, oldsig);
467 * These functions flushes out all traces of the currently running executable
468 * so that a new one can be started
471 static inline void flush_old_files(struct files_struct * files)
473 long j = -1;
475 write_lock(&files->file_lock);
476 for (;;) {
477 unsigned long set, i;
479 j++;
480 i = j * __NFDBITS;
481 if (i >= files->max_fds || i >= files->max_fdset)
482 break;
483 set = files->close_on_exec->fds_bits[j];
484 if (!set)
485 continue;
486 files->close_on_exec->fds_bits[j] = 0;
487 write_unlock(&files->file_lock);
488 for ( ; set ; i++,set >>= 1) {
489 if (set & 1) {
490 sys_close(i);
493 write_lock(&files->file_lock);
496 write_unlock(&files->file_lock);
500 * An execve() will automatically "de-thread" the process.
501 * Note: we don't have to hold the tasklist_lock to test
502 * whether we migth need to do this. If we're not part of
503 * a thread group, there is no way we can become one
504 * dynamically. And if we are, we only need to protect the
505 * unlink - even if we race with the last other thread exit,
506 * at worst the list_del_init() might end up being a no-op.
508 static inline void de_thread(struct task_struct *tsk)
510 if (!list_empty(&tsk->thread_group)) {
511 write_lock_irq(&tasklist_lock);
512 list_del_init(&tsk->thread_group);
513 write_unlock_irq(&tasklist_lock);
516 /* Minor oddity: this might stay the same. */
517 tsk->tgid = tsk->pid;
520 int flush_old_exec(struct linux_binprm * bprm)
522 char * name;
523 int i, ch, retval;
524 struct signal_struct * oldsig;
527 * Make sure we have a private signal table
529 oldsig = current->sig;
530 retval = make_private_signals();
531 if (retval) goto flush_failed;
534 * Release all of the old mmap stuff
536 retval = exec_mmap();
537 if (retval) goto mmap_failed;
539 /* This is the point of no return */
540 release_old_signals(oldsig);
542 current->sas_ss_sp = current->sas_ss_size = 0;
544 if (current->euid == current->uid && current->egid == current->gid)
545 current->dumpable = 1;
546 name = bprm->filename;
547 for (i=0; (ch = *(name++)) != '\0';) {
548 if (ch == '/')
549 i = 0;
550 else
551 if (i < 15)
552 current->comm[i++] = ch;
554 current->comm[i] = '\0';
556 flush_thread();
558 de_thread(current);
560 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
561 permission(bprm->file->f_dentry->d_inode,MAY_READ))
562 current->dumpable = 0;
564 /* An exec changes our domain. We are no longer part of the thread
565 group */
567 current->self_exec_id++;
569 flush_signal_handlers(current);
570 flush_old_files(current->files);
572 return 0;
574 mmap_failed:
575 flush_failed:
576 spin_lock_irq(&current->sigmask_lock);
577 if (current->sig != oldsig)
578 kfree(current->sig);
579 current->sig = oldsig;
580 spin_unlock_irq(&current->sigmask_lock);
581 return retval;
585 * We mustn't allow tracing of suid binaries, unless
586 * the tracer has the capability to trace anything..
588 static inline int must_not_trace_exec(struct task_struct * p)
590 return (p->ptrace & PT_PTRACED) && !cap_raised(p->p_pptr->cap_effective, CAP_SYS_PTRACE);
594 * Fill the binprm structure from the inode.
595 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
597 int prepare_binprm(struct linux_binprm *bprm)
599 int mode;
600 int id_change,cap_raised;
601 struct inode * inode = bprm->file->f_dentry->d_inode;
603 mode = inode->i_mode;
604 /* Huh? We had already checked for MAY_EXEC, WTF do we check this? */
605 if (!(mode & 0111)) /* with at least _one_ execute bit set */
606 return -EACCES;
608 bprm->e_uid = current->euid;
609 bprm->e_gid = current->egid;
610 id_change = cap_raised = 0;
612 /* Set-uid? */
613 if (mode & S_ISUID) {
614 bprm->e_uid = inode->i_uid;
615 if (bprm->e_uid != current->euid)
616 id_change = 1;
619 /* Set-gid? */
621 * If setgid is set but no group execute bit then this
622 * is a candidate for mandatory locking, not a setgid
623 * executable.
625 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
626 bprm->e_gid = inode->i_gid;
627 if (!in_group_p(bprm->e_gid))
628 id_change = 1;
631 /* We don't have VFS support for capabilities yet */
632 cap_clear(bprm->cap_inheritable);
633 cap_clear(bprm->cap_permitted);
634 cap_clear(bprm->cap_effective);
636 /* To support inheritance of root-permissions and suid-root
637 * executables under compatibility mode, we raise all three
638 * capability sets for the file.
640 * If only the real uid is 0, we only raise the inheritable
641 * and permitted sets of the executable file.
644 if (!issecure(SECURE_NOROOT)) {
645 if (bprm->e_uid == 0 || current->uid == 0) {
646 cap_set_full(bprm->cap_inheritable);
647 cap_set_full(bprm->cap_permitted);
649 if (bprm->e_uid == 0)
650 cap_set_full(bprm->cap_effective);
653 /* Only if pP' is _not_ a subset of pP, do we consider there
654 * has been a capability related "change of capability". In
655 * such cases, we need to check that the elevation of
656 * privilege does not go against other system constraints.
657 * The new Permitted set is defined below -- see (***). */
659 kernel_cap_t permitted, working;
661 permitted = cap_intersect(bprm->cap_permitted, cap_bset);
662 working = cap_intersect(bprm->cap_inheritable,
663 current->cap_inheritable);
664 working = cap_combine(permitted, working);
665 if (!cap_issubset(working, current->cap_permitted)) {
666 cap_raised = 1;
670 if (id_change || cap_raised) {
671 /* We can't suid-execute if we're sharing parts of the executable */
672 /* or if we're being traced (or if suid execs are not allowed) */
673 /* (current->mm->mm_users > 1 is ok, as we'll get a new mm anyway) */
674 if (IS_NOSUID(inode)
675 || must_not_trace_exec(current)
676 || (atomic_read(&current->fs->count) > 1)
677 || (atomic_read(&current->sig->count) > 1)
678 || (atomic_read(&current->files->count) > 1)) {
679 if (id_change && !capable(CAP_SETUID))
680 return -EPERM;
681 if (cap_raised && !capable(CAP_SETPCAP))
682 return -EPERM;
686 memset(bprm->buf,0,BINPRM_BUF_SIZE);
687 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
691 * This function is used to produce the new IDs and capabilities
692 * from the old ones and the file's capabilities.
694 * The formula used for evolving capabilities is:
696 * pI' = pI
697 * (***) pP' = (fP & X) | (fI & pI)
698 * pE' = pP' & fE [NB. fE is 0 or ~0]
700 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
701 * ' indicates post-exec(), and X is the global 'cap_bset'.
704 void compute_creds(struct linux_binprm *bprm)
706 kernel_cap_t new_permitted, working;
708 new_permitted = cap_intersect(bprm->cap_permitted, cap_bset);
709 working = cap_intersect(bprm->cap_inheritable,
710 current->cap_inheritable);
711 new_permitted = cap_combine(new_permitted, working);
713 /* For init, we want to retain the capabilities set
714 * in the init_task struct. Thus we skip the usual
715 * capability rules */
716 if (current->pid != 1) {
717 current->cap_permitted = new_permitted;
718 current->cap_effective =
719 cap_intersect(new_permitted, bprm->cap_effective);
722 /* AUD: Audit candidate if current->cap_effective is set */
724 current->suid = current->euid = current->fsuid = bprm->e_uid;
725 current->sgid = current->egid = current->fsgid = bprm->e_gid;
726 if (current->euid != current->uid || current->egid != current->gid ||
727 !cap_issubset(new_permitted, current->cap_permitted))
728 current->dumpable = 0;
730 current->keep_capabilities = 0;
734 void remove_arg_zero(struct linux_binprm *bprm)
736 if (bprm->argc) {
737 unsigned long offset;
738 char * kaddr;
739 struct page *page;
741 offset = bprm->p % PAGE_SIZE;
742 goto inside;
744 while (bprm->p++, *(kaddr+offset++)) {
745 if (offset != PAGE_SIZE)
746 continue;
747 offset = 0;
748 kunmap(page);
749 inside:
750 page = bprm->page[bprm->p/PAGE_SIZE];
751 kaddr = (char *)kmap(page);
753 kunmap(page);
754 bprm->argc--;
759 * cycle the list of binary formats handler, until one recognizes the image
761 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
763 int try,retval=0;
764 struct linux_binfmt *fmt;
765 #ifdef __alpha__
766 /* handle /sbin/loader.. */
768 struct exec * eh = (struct exec *) bprm->buf;
769 struct linux_binprm bprm_loader;
771 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
772 (eh->fh.f_flags & 0x3000) == 0x3000)
774 int i;
775 char * dynloader[] = { "/sbin/loader" };
776 struct file * file;
778 allow_write_access(bprm->file);
779 fput(bprm->file);
780 bprm->file = NULL;
782 bprm_loader.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
783 for (i = 0 ; i < MAX_ARG_PAGES ; i++) /* clear page-table */
784 bprm_loader.page[i] = NULL;
786 file = open_exec(dynloader[0]);
787 retval = PTR_ERR(file);
788 if (IS_ERR(file))
789 return retval;
790 bprm->file = file;
791 bprm->loader = bprm_loader.p;
792 retval = prepare_binprm(bprm);
793 if (retval<0)
794 return retval;
795 /* should call search_binary_handler recursively here,
796 but it does not matter */
799 #endif
800 for (try=0; try<2; try++) {
801 read_lock(&binfmt_lock);
802 for (fmt = formats ; fmt ; fmt = fmt->next) {
803 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
804 if (!fn)
805 continue;
806 if (!try_inc_mod_count(fmt->module))
807 continue;
808 read_unlock(&binfmt_lock);
809 retval = fn(bprm, regs);
810 if (retval >= 0) {
811 put_binfmt(fmt);
812 allow_write_access(bprm->file);
813 if (bprm->file)
814 fput(bprm->file);
815 bprm->file = NULL;
816 current->did_exec = 1;
817 return retval;
819 read_lock(&binfmt_lock);
820 put_binfmt(fmt);
821 if (retval != -ENOEXEC)
822 break;
823 if (!bprm->file) {
824 read_unlock(&binfmt_lock);
825 return retval;
828 read_unlock(&binfmt_lock);
829 if (retval != -ENOEXEC) {
830 break;
831 #ifdef CONFIG_KMOD
832 }else{
833 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
834 char modname[20];
835 if (printable(bprm->buf[0]) &&
836 printable(bprm->buf[1]) &&
837 printable(bprm->buf[2]) &&
838 printable(bprm->buf[3]))
839 break; /* -ENOEXEC */
840 sprintf(modname, "binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
841 request_module(modname);
842 #endif
845 return retval;
850 * sys_execve() executes a new program.
852 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
854 struct linux_binprm bprm;
855 struct file *file;
856 int retval;
857 int i;
859 file = open_exec(filename);
861 retval = PTR_ERR(file);
862 if (IS_ERR(file))
863 return retval;
865 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
866 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
868 bprm.file = file;
869 bprm.filename = filename;
870 bprm.sh_bang = 0;
871 bprm.loader = 0;
872 bprm.exec = 0;
873 if ((bprm.argc = count(argv, bprm.p / sizeof(void *))) < 0) {
874 allow_write_access(file);
875 fput(file);
876 return bprm.argc;
879 if ((bprm.envc = count(envp, bprm.p / sizeof(void *))) < 0) {
880 allow_write_access(file);
881 fput(file);
882 return bprm.envc;
885 retval = prepare_binprm(&bprm);
886 if (retval < 0)
887 goto out;
889 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
890 if (retval < 0)
891 goto out;
893 bprm.exec = bprm.p;
894 retval = copy_strings(bprm.envc, envp, &bprm);
895 if (retval < 0)
896 goto out;
898 retval = copy_strings(bprm.argc, argv, &bprm);
899 if (retval < 0)
900 goto out;
902 retval = search_binary_handler(&bprm,regs);
903 if (retval >= 0)
904 /* execve success */
905 return retval;
907 out:
908 /* Something went wrong, return the inode and free the argument pages*/
909 allow_write_access(bprm.file);
910 if (bprm.file)
911 fput(bprm.file);
913 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
914 struct page * page = bprm.page[i];
915 if (page)
916 __free_page(page);
919 return retval;
922 void set_binfmt(struct linux_binfmt *new)
924 struct linux_binfmt *old = current->binfmt;
925 if (new && new->module)
926 __MOD_INC_USE_COUNT(new->module);
927 current->binfmt = new;
928 if (old && old->module)
929 __MOD_DEC_USE_COUNT(old->module);
932 int do_coredump(long signr, struct pt_regs * regs)
934 struct linux_binfmt * binfmt;
935 char corename[6+sizeof(current->comm)];
936 struct file * file;
937 struct inode * inode;
939 lock_kernel();
940 binfmt = current->binfmt;
941 if (!binfmt || !binfmt->core_dump)
942 goto fail;
943 if (!current->dumpable || atomic_read(&current->mm->mm_users) != 1)
944 goto fail;
945 current->dumpable = 0;
946 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
947 goto fail;
949 memcpy(corename,"core.", 5);
950 #if 0
951 memcpy(corename+5,current->comm,sizeof(current->comm));
952 #else
953 corename[4] = '\0';
954 #endif
955 file = filp_open(corename, O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
956 if (IS_ERR(file))
957 goto fail;
958 inode = file->f_dentry->d_inode;
959 if (inode->i_nlink > 1)
960 goto close_fail; /* multiple links - don't dump */
962 if (!S_ISREG(inode->i_mode))
963 goto close_fail;
964 if (!file->f_op)
965 goto close_fail;
966 if (!file->f_op->write)
967 goto close_fail;
968 if (!binfmt->core_dump(signr, regs, file))
969 goto close_fail;
970 unlock_kernel();
971 filp_close(file, NULL);
972 return 1;
974 close_fail:
975 filp_close(file, NULL);
976 fail:
977 unlock_kernel();
978 return 0;