ia64/xen-unstable

view xenolinux-2.4.21-pre4-sparse/fs/exec.c @ 302:18f05ecf3fef

bitkeeper revision 1.124 (3e71f8a4QvveKwitZNAJi1H3BJpPEQ)

ac_timer.c:
rewrite of do timer/add_timer + perfcounters
apic.c:
added perfcounter and try to disable APIC when no timeout value is zero.
irq.c:
count interrupts and cycles spent in them
sched.h:
added fields for BVT
schedule.c:
BVT without warping
keyhandler.c:
added handler for dumping run queues
moved handler for ac_timers here
.del-dom0_ops.h~f77c7a14cfa618f8:
Delete: tools/domain_builder/dom0_ops.h
author rn@wyvis.research.intel-research.net
date Fri Mar 14 15:43:32 2003 +0000 (2003-03-14)
parents d7d0a23b2e07
children
line source
1 /*
2 * linux/fs/exec.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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 */
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/personality.h>
38 #include <linux/swap.h>
39 #include <linux/utsname.h>
40 #define __NO_VERSION__
41 #include <linux/module.h>
43 #include <asm/uaccess.h>
44 #include <asm/pgalloc.h>
45 #include <asm/mmu_context.h>
47 #ifdef CONFIG_KMOD
48 #include <linux/kmod.h>
49 #endif
51 int core_uses_pid;
52 char core_pattern[65] = "core";
53 /* The maximal length of core_pattern is also specified in sysctl.c */
55 static struct linux_binfmt *formats;
56 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
58 int register_binfmt(struct linux_binfmt * fmt)
59 {
60 struct linux_binfmt ** tmp = &formats;
62 if (!fmt)
63 return -EINVAL;
64 if (fmt->next)
65 return -EBUSY;
66 write_lock(&binfmt_lock);
67 while (*tmp) {
68 if (fmt == *tmp) {
69 write_unlock(&binfmt_lock);
70 return -EBUSY;
71 }
72 tmp = &(*tmp)->next;
73 }
74 fmt->next = formats;
75 formats = fmt;
76 write_unlock(&binfmt_lock);
77 return 0;
78 }
80 int unregister_binfmt(struct linux_binfmt * fmt)
81 {
82 struct linux_binfmt ** tmp = &formats;
84 write_lock(&binfmt_lock);
85 while (*tmp) {
86 if (fmt == *tmp) {
87 *tmp = fmt->next;
88 write_unlock(&binfmt_lock);
89 return 0;
90 }
91 tmp = &(*tmp)->next;
92 }
93 write_unlock(&binfmt_lock);
94 return -EINVAL;
95 }
97 static inline void put_binfmt(struct linux_binfmt * fmt)
98 {
99 if (fmt->module)
100 __MOD_DEC_USE_COUNT(fmt->module);
101 }
103 /*
104 * Note that a shared library must be both readable and executable due to
105 * security reasons.
106 *
107 * Also note that we take the address to load from from the file itself.
108 */
109 asmlinkage long sys_uselib(const char * library)
110 {
111 struct file * file;
112 struct nameidata nd;
113 int error;
115 error = user_path_walk(library, &nd);
116 if (error)
117 goto out;
119 error = -EINVAL;
120 if (!S_ISREG(nd.dentry->d_inode->i_mode))
121 goto exit;
123 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC);
124 if (error)
125 goto exit;
127 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
128 error = PTR_ERR(file);
129 if (IS_ERR(file))
130 goto out;
132 error = -ENOEXEC;
133 if(file->f_op && file->f_op->read) {
134 struct linux_binfmt * fmt;
136 read_lock(&binfmt_lock);
137 for (fmt = formats ; fmt ; fmt = fmt->next) {
138 if (!fmt->load_shlib)
139 continue;
140 if (!try_inc_mod_count(fmt->module))
141 continue;
142 read_unlock(&binfmt_lock);
143 error = fmt->load_shlib(file);
144 read_lock(&binfmt_lock);
145 put_binfmt(fmt);
146 if (error != -ENOEXEC)
147 break;
148 }
149 read_unlock(&binfmt_lock);
150 }
151 fput(file);
152 out:
153 return error;
154 exit:
155 path_release(&nd);
156 goto out;
157 }
159 /*
160 * count() counts the number of arguments/envelopes
161 */
162 static int count(char ** argv, int max)
163 {
164 int i = 0;
166 if (argv != NULL) {
167 for (;;) {
168 char * p;
170 if (get_user(p, argv))
171 return -EFAULT;
172 if (!p)
173 break;
174 argv++;
175 if(++i > max)
176 return -E2BIG;
177 }
178 }
179 return i;
180 }
182 /*
183 * 'copy_strings()' copies argument/envelope strings from user
184 * memory to free pages in kernel mem. These are in a format ready
185 * to be put directly into the top of new user memory.
186 */
187 int copy_strings(int argc,char ** argv, struct linux_binprm *bprm)
188 {
189 struct page *kmapped_page = NULL;
190 char *kaddr = NULL;
191 int ret;
193 while (argc-- > 0) {
194 char *str;
195 int len;
196 unsigned long pos;
198 if (get_user(str, argv+argc) ||
199 !(len = strnlen_user(str, bprm->p))) {
200 ret = -EFAULT;
201 goto out;
202 }
204 if (bprm->p < len) {
205 ret = -E2BIG;
206 goto out;
207 }
209 bprm->p -= len;
210 /* XXX: add architecture specific overflow check here. */
211 pos = bprm->p;
213 while (len > 0) {
214 int i, new, err;
215 int offset, bytes_to_copy;
216 struct page *page;
218 offset = pos % PAGE_SIZE;
219 i = pos/PAGE_SIZE;
220 page = bprm->page[i];
221 new = 0;
222 if (!page) {
223 page = alloc_page(GFP_HIGHUSER);
224 bprm->page[i] = page;
225 if (!page) {
226 ret = -ENOMEM;
227 goto out;
228 }
229 new = 1;
230 }
232 if (page != kmapped_page) {
233 if (kmapped_page)
234 kunmap(kmapped_page);
235 kmapped_page = page;
236 kaddr = kmap(kmapped_page);
237 }
238 if (new && offset)
239 memset(kaddr, 0, offset);
240 bytes_to_copy = PAGE_SIZE - offset;
241 if (bytes_to_copy > len) {
242 bytes_to_copy = len;
243 if (new)
244 memset(kaddr+offset+len, 0,
245 PAGE_SIZE-offset-len);
246 }
247 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
248 if (err) {
249 ret = -EFAULT;
250 goto out;
251 }
253 pos += bytes_to_copy;
254 str += bytes_to_copy;
255 len -= bytes_to_copy;
256 }
257 }
258 ret = 0;
259 out:
260 if (kmapped_page)
261 kunmap(kmapped_page);
262 return ret;
263 }
265 /*
266 * Like copy_strings, but get argv and its values from kernel memory.
267 */
268 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
269 {
270 int r;
271 mm_segment_t oldfs = get_fs();
272 set_fs(KERNEL_DS);
273 r = copy_strings(argc, argv, bprm);
274 set_fs(oldfs);
275 return r;
276 }
278 /*
279 * This routine is used to map in a page into an address space: needed by
280 * execve() for the initial stack and environment pages.
281 *
282 * tsk->mmap_sem is held for writing.
283 */
284 void put_dirty_page(struct task_struct * tsk, struct page *page, unsigned long address)
285 {
286 pgd_t * pgd;
287 pmd_t * pmd;
288 pte_t * pte;
290 if (page_count(page) != 1)
291 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n", page, address);
292 pgd = pgd_offset(tsk->mm, address);
294 spin_lock(&tsk->mm->page_table_lock);
295 pmd = pmd_alloc(tsk->mm, pgd, address);
296 if (!pmd)
297 goto out;
298 pte = pte_alloc(tsk->mm, pmd, address);
299 if (!pte)
300 goto out;
301 if (!pte_none(*pte))
302 goto out;
303 lru_cache_add(page);
304 flush_dcache_page(page);
305 flush_page_to_ram(page);
306 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, PAGE_COPY))));
307 XENO_flush_page_update_queue();
308 tsk->mm->rss++;
309 spin_unlock(&tsk->mm->page_table_lock);
311 /* no need for flush_tlb */
312 return;
313 out:
314 spin_unlock(&tsk->mm->page_table_lock);
315 __free_page(page);
316 force_sig(SIGKILL, tsk);
317 return;
318 }
320 int setup_arg_pages(struct linux_binprm *bprm)
321 {
322 unsigned long stack_base;
323 struct vm_area_struct *mpnt;
324 int i;
326 stack_base = STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
328 bprm->p += stack_base;
329 if (bprm->loader)
330 bprm->loader += stack_base;
331 bprm->exec += stack_base;
333 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
334 if (!mpnt)
335 return -ENOMEM;
337 down_write(&current->mm->mmap_sem);
338 {
339 mpnt->vm_mm = current->mm;
340 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
341 mpnt->vm_end = STACK_TOP;
342 mpnt->vm_page_prot = PAGE_COPY;
343 mpnt->vm_flags = VM_STACK_FLAGS;
344 mpnt->vm_ops = NULL;
345 mpnt->vm_pgoff = 0;
346 mpnt->vm_file = NULL;
347 mpnt->vm_private_data = (void *) 0;
348 insert_vm_struct(current->mm, mpnt);
349 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
350 }
352 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
353 struct page *page = bprm->page[i];
354 if (page) {
355 bprm->page[i] = NULL;
356 put_dirty_page(current,page,stack_base);
357 }
358 stack_base += PAGE_SIZE;
359 }
360 up_write(&current->mm->mmap_sem);
362 return 0;
363 }
365 struct file *open_exec(const char *name)
366 {
367 struct nameidata nd;
368 struct inode *inode;
369 struct file *file;
370 int err = 0;
372 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd);
373 file = ERR_PTR(err);
374 if (!err) {
375 inode = nd.dentry->d_inode;
376 file = ERR_PTR(-EACCES);
377 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
378 S_ISREG(inode->i_mode)) {
379 int err = permission(inode, MAY_EXEC);
380 if (!err && !(inode->i_mode & 0111))
381 err = -EACCES;
382 file = ERR_PTR(err);
383 if (!err) {
384 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
385 if (!IS_ERR(file)) {
386 err = deny_write_access(file);
387 if (err) {
388 fput(file);
389 file = ERR_PTR(err);
390 }
391 }
392 out:
393 return file;
394 }
395 }
396 path_release(&nd);
397 }
398 goto out;
399 }
401 int kernel_read(struct file *file, unsigned long offset,
402 char * addr, unsigned long count)
403 {
404 mm_segment_t old_fs;
405 loff_t pos = offset;
406 int result = -ENOSYS;
408 if (!file->f_op->read)
409 goto fail;
410 old_fs = get_fs();
411 set_fs(get_ds());
412 result = file->f_op->read(file, addr, count, &pos);
413 set_fs(old_fs);
414 fail:
415 return result;
416 }
418 static int exec_mmap(void)
419 {
420 struct mm_struct * mm, * old_mm;
422 old_mm = current->mm;
423 if (old_mm && atomic_read(&old_mm->mm_users) == 1) {
424 mm_release();
425 exit_mmap(old_mm);
426 return 0;
427 }
429 mm = mm_alloc();
430 if (mm) {
431 struct mm_struct *active_mm;
433 if (init_new_context(current, mm)) {
434 mmdrop(mm);
435 return -ENOMEM;
436 }
438 /* Add it to the list of mm's */
439 spin_lock(&mmlist_lock);
440 list_add(&mm->mmlist, &init_mm.mmlist);
441 mmlist_nr++;
442 spin_unlock(&mmlist_lock);
444 task_lock(current);
445 active_mm = current->active_mm;
446 current->mm = mm;
447 current->active_mm = mm;
448 task_unlock(current);
449 activate_mm(active_mm, mm);
450 mm_release();
451 if (old_mm) {
452 if (active_mm != old_mm) BUG();
453 mmput(old_mm);
454 return 0;
455 }
456 mmdrop(active_mm);
457 return 0;
458 }
459 return -ENOMEM;
460 }
462 /*
463 * This function makes sure the current process has its own signal table,
464 * so that flush_signal_handlers can later reset the handlers without
465 * disturbing other processes. (Other processes might share the signal
466 * table via the CLONE_SIGNAL option to clone().)
467 */
469 static inline int make_private_signals(void)
470 {
471 struct signal_struct * newsig;
473 if (atomic_read(&current->sig->count) <= 1)
474 return 0;
475 newsig = kmem_cache_alloc(sigact_cachep, GFP_KERNEL);
476 if (newsig == NULL)
477 return -ENOMEM;
478 spin_lock_init(&newsig->siglock);
479 atomic_set(&newsig->count, 1);
480 memcpy(newsig->action, current->sig->action, sizeof(newsig->action));
481 spin_lock_irq(&current->sigmask_lock);
482 current->sig = newsig;
483 spin_unlock_irq(&current->sigmask_lock);
484 return 0;
485 }
487 /*
488 * If make_private_signals() made a copy of the signal table, decrement the
489 * refcount of the original table, and free it if necessary.
490 * We don't do that in make_private_signals() so that we can back off
491 * in flush_old_exec() if an error occurs after calling make_private_signals().
492 */
494 static inline void release_old_signals(struct signal_struct * oldsig)
495 {
496 if (current->sig == oldsig)
497 return;
498 if (atomic_dec_and_test(&oldsig->count))
499 kmem_cache_free(sigact_cachep, oldsig);
500 }
502 /*
503 * These functions flushes out all traces of the currently running executable
504 * so that a new one can be started
505 */
507 static inline void flush_old_files(struct files_struct * files)
508 {
509 long j = -1;
511 write_lock(&files->file_lock);
512 for (;;) {
513 unsigned long set, i;
515 j++;
516 i = j * __NFDBITS;
517 if (i >= files->max_fds || i >= files->max_fdset)
518 break;
519 set = files->close_on_exec->fds_bits[j];
520 if (!set)
521 continue;
522 files->close_on_exec->fds_bits[j] = 0;
523 write_unlock(&files->file_lock);
524 for ( ; set ; i++,set >>= 1) {
525 if (set & 1) {
526 sys_close(i);
527 }
528 }
529 write_lock(&files->file_lock);
531 }
532 write_unlock(&files->file_lock);
533 }
535 /*
536 * An execve() will automatically "de-thread" the process.
537 * Note: we don't have to hold the tasklist_lock to test
538 * whether we migth need to do this. If we're not part of
539 * a thread group, there is no way we can become one
540 * dynamically. And if we are, we only need to protect the
541 * unlink - even if we race with the last other thread exit,
542 * at worst the list_del_init() might end up being a no-op.
543 */
544 static inline void de_thread(struct task_struct *tsk)
545 {
546 if (!list_empty(&tsk->thread_group)) {
547 write_lock_irq(&tasklist_lock);
548 list_del_init(&tsk->thread_group);
549 write_unlock_irq(&tasklist_lock);
550 }
552 /* Minor oddity: this might stay the same. */
553 tsk->tgid = tsk->pid;
554 }
556 int flush_old_exec(struct linux_binprm * bprm)
557 {
558 char * name;
559 int i, ch, retval;
560 struct signal_struct * oldsig;
562 /*
563 * Make sure we have a private signal table
564 */
565 oldsig = current->sig;
566 retval = make_private_signals();
567 if (retval) goto flush_failed;
569 /*
570 * Release all of the old mmap stuff
571 */
572 retval = exec_mmap();
573 if (retval) goto mmap_failed;
575 /* This is the point of no return */
576 release_old_signals(oldsig);
578 current->sas_ss_sp = current->sas_ss_size = 0;
580 if (current->euid == current->uid && current->egid == current->gid)
581 current->mm->dumpable = 1;
582 name = bprm->filename;
583 for (i=0; (ch = *(name++)) != '\0';) {
584 if (ch == '/')
585 i = 0;
586 else
587 if (i < 15)
588 current->comm[i++] = ch;
589 }
590 current->comm[i] = '\0';
592 flush_thread();
594 de_thread(current);
596 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
597 permission(bprm->file->f_dentry->d_inode,MAY_READ))
598 current->mm->dumpable = 0;
600 /* An exec changes our domain. We are no longer part of the thread
601 group */
603 current->self_exec_id++;
605 flush_signal_handlers(current);
606 flush_old_files(current->files);
608 return 0;
610 mmap_failed:
611 flush_failed:
612 spin_lock_irq(&current->sigmask_lock);
613 if (current->sig != oldsig) {
614 kmem_cache_free(sigact_cachep, current->sig);
615 current->sig = oldsig;
616 }
617 spin_unlock_irq(&current->sigmask_lock);
618 return retval;
619 }
621 /*
622 * We mustn't allow tracing of suid binaries, unless
623 * the tracer has the capability to trace anything..
624 */
625 static inline int must_not_trace_exec(struct task_struct * p)
626 {
627 return (p->ptrace & PT_PTRACED) && !(p->ptrace & PT_PTRACE_CAP);
628 }
630 /*
631 * Fill the binprm structure from the inode.
632 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
633 */
634 int prepare_binprm(struct linux_binprm *bprm)
635 {
636 int mode;
637 struct inode * inode = bprm->file->f_dentry->d_inode;
639 mode = inode->i_mode;
640 /*
641 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
642 * vfs_permission lets a non-executable through
643 */
644 if (!(mode & 0111)) /* with at least _one_ execute bit set */
645 return -EACCES;
646 if (bprm->file->f_op == NULL)
647 return -EACCES;
649 bprm->e_uid = current->euid;
650 bprm->e_gid = current->egid;
652 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
653 /* Set-uid? */
654 if (mode & S_ISUID)
655 bprm->e_uid = inode->i_uid;
657 /* Set-gid? */
658 /*
659 * If setgid is set but no group execute bit then this
660 * is a candidate for mandatory locking, not a setgid
661 * executable.
662 */
663 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
664 bprm->e_gid = inode->i_gid;
665 }
667 /* We don't have VFS support for capabilities yet */
668 cap_clear(bprm->cap_inheritable);
669 cap_clear(bprm->cap_permitted);
670 cap_clear(bprm->cap_effective);
672 /* To support inheritance of root-permissions and suid-root
673 * executables under compatibility mode, we raise all three
674 * capability sets for the file.
675 *
676 * If only the real uid is 0, we only raise the inheritable
677 * and permitted sets of the executable file.
678 */
680 if (!issecure(SECURE_NOROOT)) {
681 if (bprm->e_uid == 0 || current->uid == 0) {
682 cap_set_full(bprm->cap_inheritable);
683 cap_set_full(bprm->cap_permitted);
684 }
685 if (bprm->e_uid == 0)
686 cap_set_full(bprm->cap_effective);
687 }
689 memset(bprm->buf,0,BINPRM_BUF_SIZE);
690 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
691 }
693 /*
694 * This function is used to produce the new IDs and capabilities
695 * from the old ones and the file's capabilities.
696 *
697 * The formula used for evolving capabilities is:
698 *
699 * pI' = pI
700 * (***) pP' = (fP & X) | (fI & pI)
701 * pE' = pP' & fE [NB. fE is 0 or ~0]
702 *
703 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
704 * ' indicates post-exec(), and X is the global 'cap_bset'.
705 *
706 */
708 void compute_creds(struct linux_binprm *bprm)
709 {
710 kernel_cap_t new_permitted, working;
711 int do_unlock = 0;
713 new_permitted = cap_intersect(bprm->cap_permitted, cap_bset);
714 working = cap_intersect(bprm->cap_inheritable,
715 current->cap_inheritable);
716 new_permitted = cap_combine(new_permitted, working);
718 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
719 !cap_issubset(new_permitted, current->cap_permitted)) {
720 current->mm->dumpable = 0;
722 lock_kernel();
723 if (must_not_trace_exec(current)
724 || atomic_read(&current->fs->count) > 1
725 || atomic_read(&current->files->count) > 1
726 || atomic_read(&current->sig->count) > 1) {
727 if(!capable(CAP_SETUID)) {
728 bprm->e_uid = current->uid;
729 bprm->e_gid = current->gid;
730 }
731 if(!capable(CAP_SETPCAP)) {
732 new_permitted = cap_intersect(new_permitted,
733 current->cap_permitted);
734 }
735 }
736 do_unlock = 1;
737 }
740 /* For init, we want to retain the capabilities set
741 * in the init_task struct. Thus we skip the usual
742 * capability rules */
743 if (current->pid != 1) {
744 current->cap_permitted = new_permitted;
745 current->cap_effective =
746 cap_intersect(new_permitted, bprm->cap_effective);
747 }
749 /* AUD: Audit candidate if current->cap_effective is set */
751 current->suid = current->euid = current->fsuid = bprm->e_uid;
752 current->sgid = current->egid = current->fsgid = bprm->e_gid;
754 if(do_unlock)
755 unlock_kernel();
756 current->keep_capabilities = 0;
757 }
760 void remove_arg_zero(struct linux_binprm *bprm)
761 {
762 if (bprm->argc) {
763 unsigned long offset;
764 char * kaddr;
765 struct page *page;
767 offset = bprm->p % PAGE_SIZE;
768 goto inside;
770 while (bprm->p++, *(kaddr+offset++)) {
771 if (offset != PAGE_SIZE)
772 continue;
773 offset = 0;
774 kunmap(page);
775 inside:
776 page = bprm->page[bprm->p/PAGE_SIZE];
777 kaddr = kmap(page);
778 }
779 kunmap(page);
780 bprm->argc--;
781 }
782 }
784 /*
785 * cycle the list of binary formats handler, until one recognizes the image
786 */
787 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
788 {
789 int try,retval=0;
790 struct linux_binfmt *fmt;
791 #ifdef __alpha__
792 /* handle /sbin/loader.. */
793 {
794 struct exec * eh = (struct exec *) bprm->buf;
796 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
797 (eh->fh.f_flags & 0x3000) == 0x3000)
798 {
799 struct file * file;
800 unsigned long loader;
802 allow_write_access(bprm->file);
803 fput(bprm->file);
804 bprm->file = NULL;
806 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
808 file = open_exec("/sbin/loader");
809 retval = PTR_ERR(file);
810 if (IS_ERR(file))
811 return retval;
813 /* Remember if the application is TASO. */
814 bprm->sh_bang = eh->ah.entry < 0x100000000;
816 bprm->file = file;
817 bprm->loader = loader;
818 retval = prepare_binprm(bprm);
819 if (retval<0)
820 return retval;
821 /* should call search_binary_handler recursively here,
822 but it does not matter */
823 }
824 }
825 #endif
826 /* kernel module loader fixup */
827 /* so we don't try to load run modprobe in kernel space. */
828 set_fs(USER_DS);
829 for (try=0; try<2; try++) {
830 read_lock(&binfmt_lock);
831 for (fmt = formats ; fmt ; fmt = fmt->next) {
832 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
833 if (!fn)
834 continue;
835 if (!try_inc_mod_count(fmt->module))
836 continue;
837 read_unlock(&binfmt_lock);
838 retval = fn(bprm, regs);
839 if (retval >= 0) {
840 put_binfmt(fmt);
841 allow_write_access(bprm->file);
842 if (bprm->file)
843 fput(bprm->file);
844 bprm->file = NULL;
845 current->did_exec = 1;
846 return retval;
847 }
848 read_lock(&binfmt_lock);
849 put_binfmt(fmt);
850 if (retval != -ENOEXEC)
851 break;
852 if (!bprm->file) {
853 read_unlock(&binfmt_lock);
854 return retval;
855 }
856 }
857 read_unlock(&binfmt_lock);
858 if (retval != -ENOEXEC) {
859 break;
860 #ifdef CONFIG_KMOD
861 }else{
862 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
863 char modname[20];
864 if (printable(bprm->buf[0]) &&
865 printable(bprm->buf[1]) &&
866 printable(bprm->buf[2]) &&
867 printable(bprm->buf[3]))
868 break; /* -ENOEXEC */
869 sprintf(modname, "binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
870 request_module(modname);
871 #endif
872 }
873 }
874 return retval;
875 }
878 /*
879 * sys_execve() executes a new program.
880 */
881 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
882 {
883 struct linux_binprm bprm;
884 struct file *file;
885 int retval;
886 int i;
888 file = open_exec(filename);
890 retval = PTR_ERR(file);
891 if (IS_ERR(file))
892 return retval;
894 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
895 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
897 bprm.file = file;
898 bprm.filename = filename;
899 bprm.sh_bang = 0;
900 bprm.loader = 0;
901 bprm.exec = 0;
902 if ((bprm.argc = count(argv, bprm.p / sizeof(void *))) < 0) {
903 allow_write_access(file);
904 fput(file);
905 return bprm.argc;
906 }
908 if ((bprm.envc = count(envp, bprm.p / sizeof(void *))) < 0) {
909 allow_write_access(file);
910 fput(file);
911 return bprm.envc;
912 }
914 retval = prepare_binprm(&bprm);
915 if (retval < 0)
916 goto out;
918 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
919 if (retval < 0)
920 goto out;
922 bprm.exec = bprm.p;
923 retval = copy_strings(bprm.envc, envp, &bprm);
924 if (retval < 0)
925 goto out;
927 retval = copy_strings(bprm.argc, argv, &bprm);
928 if (retval < 0)
929 goto out;
931 retval = search_binary_handler(&bprm,regs);
932 if (retval >= 0)
933 /* execve success */
934 return retval;
936 out:
937 /* Something went wrong, return the inode and free the argument pages*/
938 allow_write_access(bprm.file);
939 if (bprm.file)
940 fput(bprm.file);
942 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
943 struct page * page = bprm.page[i];
944 if (page)
945 __free_page(page);
946 }
948 return retval;
949 }
951 void set_binfmt(struct linux_binfmt *new)
952 {
953 struct linux_binfmt *old = current->binfmt;
954 if (new && new->module)
955 __MOD_INC_USE_COUNT(new->module);
956 current->binfmt = new;
957 if (old && old->module)
958 __MOD_DEC_USE_COUNT(old->module);
959 }
961 #define CORENAME_MAX_SIZE 64
963 /* format_corename will inspect the pattern parameter, and output a
964 * name into corename, which must have space for at least
965 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
966 */
967 void format_corename(char *corename, const char *pattern, long signr)
968 {
969 const char *pat_ptr = pattern;
970 char *out_ptr = corename;
971 char *const out_end = corename + CORENAME_MAX_SIZE;
972 int rc;
973 int pid_in_pattern = 0;
975 /* Repeat as long as we have more pattern to process and more output
976 space */
977 while (*pat_ptr) {
978 if (*pat_ptr != '%') {
979 if (out_ptr == out_end)
980 goto out;
981 *out_ptr++ = *pat_ptr++;
982 } else {
983 switch (*++pat_ptr) {
984 case 0:
985 goto out;
986 /* Double percent, output one percent */
987 case '%':
988 if (out_ptr == out_end)
989 goto out;
990 *out_ptr++ = '%';
991 break;
992 /* pid */
993 case 'p':
994 pid_in_pattern = 1;
995 rc = snprintf(out_ptr, out_end - out_ptr,
996 "%d", current->pid);
997 if (rc > out_end - out_ptr)
998 goto out;
999 out_ptr += rc;
1000 break;
1001 /* uid */
1002 case 'u':
1003 rc = snprintf(out_ptr, out_end - out_ptr,
1004 "%d", current->uid);
1005 if (rc > out_end - out_ptr)
1006 goto out;
1007 out_ptr += rc;
1008 break;
1009 /* gid */
1010 case 'g':
1011 rc = snprintf(out_ptr, out_end - out_ptr,
1012 "%d", current->gid);
1013 if (rc > out_end - out_ptr)
1014 goto out;
1015 out_ptr += rc;
1016 break;
1017 /* signal that caused the coredump */
1018 case 's':
1019 rc = snprintf(out_ptr, out_end - out_ptr,
1020 "%ld", signr);
1021 if (rc > out_end - out_ptr)
1022 goto out;
1023 out_ptr += rc;
1024 break;
1025 /* UNIX time of coredump */
1026 case 't': {
1027 struct timeval tv;
1028 do_gettimeofday(&tv);
1029 rc = snprintf(out_ptr, out_end - out_ptr,
1030 "%ld", tv.tv_sec);
1031 if (rc > out_end - out_ptr)
1032 goto out;
1033 out_ptr += rc;
1034 break;
1036 /* hostname */
1037 case 'h':
1038 down_read(&uts_sem);
1039 rc = snprintf(out_ptr, out_end - out_ptr,
1040 "%s", system_utsname.nodename);
1041 up_read(&uts_sem);
1042 if (rc > out_end - out_ptr)
1043 goto out;
1044 out_ptr += rc;
1045 break;
1046 /* executable */
1047 case 'e':
1048 rc = snprintf(out_ptr, out_end - out_ptr,
1049 "%s", current->comm);
1050 if (rc > out_end - out_ptr)
1051 goto out;
1052 out_ptr += rc;
1053 break;
1054 default:
1055 break;
1057 ++pat_ptr;
1060 /* Backward compatibility with core_uses_pid:
1062 * If core_pattern does not include a %p (as is the default)
1063 * and core_uses_pid is set, then .%pid will be appended to
1064 * the filename */
1065 if (!pid_in_pattern
1066 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1067 rc = snprintf(out_ptr, out_end - out_ptr,
1068 ".%d", current->pid);
1069 if (rc > out_end - out_ptr)
1070 goto out;
1071 out_ptr += rc;
1073 out:
1074 *out_ptr = 0;
1077 int do_coredump(long signr, struct pt_regs * regs)
1079 struct linux_binfmt * binfmt;
1080 char corename[CORENAME_MAX_SIZE + 1];
1081 struct file * file;
1082 struct inode * inode;
1083 int retval = 0;
1085 lock_kernel();
1086 binfmt = current->binfmt;
1087 if (!binfmt || !binfmt->core_dump)
1088 goto fail;
1089 if (!current->mm->dumpable)
1090 goto fail;
1091 current->mm->dumpable = 0;
1092 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1093 goto fail;
1095 format_corename(corename, core_pattern, signr);
1096 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW, 0600);
1097 if (IS_ERR(file))
1098 goto fail;
1099 inode = file->f_dentry->d_inode;
1100 if (inode->i_nlink > 1)
1101 goto close_fail; /* multiple links - don't dump */
1102 if (d_unhashed(file->f_dentry))
1103 goto close_fail;
1105 if (!S_ISREG(inode->i_mode))
1106 goto close_fail;
1107 if (!file->f_op)
1108 goto close_fail;
1109 if (!file->f_op->write)
1110 goto close_fail;
1111 if (do_truncate(file->f_dentry, 0) != 0)
1112 goto close_fail;
1114 retval = binfmt->core_dump(signr, regs, file);
1116 close_fail:
1117 filp_close(file, NULL);
1118 fail:
1119 unlock_kernel();
1120 return retval;