1 /*
2 * linux/fs/exec.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 /*
8 * #!-checking implemented by tytso.
9 */
10 /*
11 * Demand-loading implemented 01.12.91 - no need to read anything but
12 * the header into memory. The inode of the executable is put into
13 * "current->executable", and page faults do the actual loading. Clean.
14 *
15 * Once more I can proudly say that linux stood up to being changed: it
16 * was less than 2 hours work to get demand-loading completely implemented.
17 *
18 * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead,
19 * current->executable is only used by the procfs. This allows a dispatch
20 * table to check for several different types of binary formats. We keep
21 * trying until we recognize the file or we run out of supported binary
22 * formats.
23 */
24
25 #include <linux/config.h>
26 #include <linux/slab.h>
27 #include <linux/file.h>
28 #include <linux/mman.h>
29 #include <linux/a.out.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <linux/pagemap.h>
35 #include <linux/highmem.h>
36 #include <linux/spinlock.h>
37 #define __NO_VERSION__
38 #include <linux/module.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/pgalloc.h>
42 #include <asm/mmu_context.h>
43
44 #ifdef CONFIG_KMOD
45 #include <linux/kmod.h>
46 #endif
47
48 static struct linux_binfmt *formats;
49 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
50
51 int register_binfmt(struct linux_binfmt * fmt)
52 {
53 struct linux_binfmt ** tmp = &formats;
54
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;
64 }
65 tmp = &(*tmp)->next;
66 }
67 fmt->next = formats;
68 formats = fmt;
69 write_unlock(&binfmt_lock);
70 return 0;
71 }
72
73 int unregister_binfmt(struct linux_binfmt * fmt)
74 {
75 struct linux_binfmt ** tmp = &formats;
76
77 write_lock(&binfmt_lock);
78 while (*tmp) {
79 if (fmt == *tmp) {
80 *tmp = fmt->next;
81 write_unlock(&binfmt_lock);
82 return 0;
83 }
84 tmp = &(*tmp)->next;
85 }
86 write_unlock(&binfmt_lock);
87 return -EINVAL;
88 }
89
90 static inline void put_binfmt(struct linux_binfmt * fmt)
91 {
92 if (fmt->module)
93 __MOD_DEC_USE_COUNT(fmt->module);
94 }
95
96 /*
97 * Note that a shared library must be both readable and executable due to
98 * security reasons.
99 *
100 * Also note that we take the address to load from from the file itself.
101 */
102 asmlinkage long sys_uselib(const char * library)
103 {
104 struct file * file;
105 struct nameidata nd;
106 int error;
107
108 error = user_path_walk(library, &nd);
109 if (error)
110 goto out;
111
112 error = -EINVAL;
113 if (!S_ISREG(nd.dentry->d_inode->i_mode))
114 goto exit;
115
116 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC);
117 if (error)
118 goto exit;
119
120 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
121 error = PTR_ERR(file);
122 if (IS_ERR(file))
123 goto out;
124
125 error = -ENOEXEC;
126 if(file->f_op && file->f_op->read) {
127 struct linux_binfmt * fmt;
128
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;
141 }
142 read_unlock(&binfmt_lock);
143 }
144 fput(file);
145 out:
146 return error;
147 exit:
148 path_release(&nd);
149 goto out;
150 }
151
152 /*
153 * count() counts the number of arguments/envelopes
154 */
155 static int count(char ** argv, int max)
156 {
157 int i = 0;
158
159 if (argv != NULL) {
160 for (;;) {
161 char * p;
162 int error;
163
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;
172 }
173 }
174 return i;
175 }
176
177 /*
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.
181 */
182 int copy_strings(int argc,char ** argv, struct linux_binprm *bprm)
183 {
184 while (argc-- > 0) {
185 char *str;
186 int len;
187 unsigned long pos;
188
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;
193
194 bprm->p -= len;
195 /* XXX: add architecture specific overflow check here. */
196
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;
203
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;
214 }
215 kaddr = kmap(page);
216
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);
224 }
225 err = copy_from_user(kaddr + offset, str, bytes_to_copy);
226 kunmap(page);
227
228 if (err)
229 return -EFAULT;
230
231 pos += bytes_to_copy;
232 str += bytes_to_copy;
233 len -= bytes_to_copy;
234 }
235 }
236 return 0;
237 }
238
239 /*
240 * Like copy_strings, but get argv and its values from kernel memory.
241 */
242 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
243 {
244 int r;
245 mm_segment_t oldfs = get_fs();
246 set_fs(KERNEL_DS);
247 r = copy_strings(argc, argv, bprm);
248 set_fs(oldfs);
249 return r;
250 }
251
252 /*
253 * This routine is used to map in a page into an address space: needed by
254 * execve() for the initial stack and environment pages.
255 */
256 void put_dirty_page(struct task_struct * tsk, struct page *page, unsigned long address)
257 {
258 pgd_t * pgd;
259 pmd_t * pmd;
260 pte_t * pte;
261
262 if (page_count(page) != 1)
263 printk("mem_map disagrees with %p at %08lx\n", page, address);
264 pgd = pgd_offset(tsk->mm, address);
265 pmd = pmd_alloc(pgd, address);
266 if (!pmd) {
267 __free_page(page);
268 force_sig(SIGKILL, tsk);
269 return;
270 }
271 pte = pte_alloc(pmd, address);
272 if (!pte) {
273 __free_page(page);
274 force_sig(SIGKILL, tsk);
275 return;
276 }
277 if (!pte_none(*pte)) {
278 pte_ERROR(*pte);
279 __free_page(page);
280 return;
281 }
282 flush_dcache_page(page);
283 flush_page_to_ram(page);
284 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, PAGE_COPY))));
285 /* no need for flush_tlb */
286 }
287
288 int setup_arg_pages(struct linux_binprm *bprm)
289 {
290 unsigned long stack_base;
291 struct vm_area_struct *mpnt;
292 int i;
293
294 stack_base = STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
295
296 bprm->p += stack_base;
297 if (bprm->loader)
298 bprm->loader += stack_base;
299 bprm->exec += stack_base;
300
301 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
302 if (!mpnt)
303 return -ENOMEM;
304
305 down(¤t->mm->mmap_sem);
306 {
307 mpnt->vm_mm = current->mm;
308 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
309 mpnt->vm_end = STACK_TOP;
310 mpnt->vm_page_prot = PAGE_COPY;
311 mpnt->vm_flags = VM_STACK_FLAGS;
312 mpnt->vm_ops = NULL;
313 mpnt->vm_pgoff = 0;
314 mpnt->vm_file = NULL;
315 mpnt->vm_private_data = (void *) 0;
316 insert_vm_struct(current->mm, mpnt);
317 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
318 }
319
320 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
321 struct page *page = bprm->page[i];
322 if (page) {
323 bprm->page[i] = NULL;
324 current->mm->rss++;
325 put_dirty_page(current,page,stack_base);
326 }
327 stack_base += PAGE_SIZE;
328 }
329 up(¤t->mm->mmap_sem);
330
331 return 0;
332 }
333
334 struct file *open_exec(const char *name)
335 {
336 struct nameidata nd;
337 struct inode *inode;
338 struct file *file;
339 int err = 0;
340
341 if (path_init(name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
342 err = path_walk(name, &nd);
343 file = ERR_PTR(err);
344 if (!err) {
345 inode = nd.dentry->d_inode;
346 file = ERR_PTR(-EACCES);
347 if (!IS_NOEXEC(inode) && S_ISREG(inode->i_mode)) {
348 int err = permission(inode, MAY_EXEC);
349 file = ERR_PTR(err);
350 if (!err) {
351 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
352 if (!IS_ERR(file)) {
353 err = deny_write_access(file);
354 if (err) {
355 fput(file);
356 file = ERR_PTR(err);
357 }
358 }
359 out:
360 return file;
361 }
362 }
363 path_release(&nd);
364 }
365 goto out;
366 }
367
368 int kernel_read(struct file *file, unsigned long offset,
369 char * addr, unsigned long count)
370 {
371 mm_segment_t old_fs;
372 loff_t pos = offset;
373 int result = -ENOSYS;
374
375 if (!file->f_op->read)
376 goto fail;
377 old_fs = get_fs();
378 set_fs(get_ds());
379 result = file->f_op->read(file, addr, count, &pos);
380 set_fs(old_fs);
381 fail:
382 return result;
383 }
384
385 static int exec_mmap(void)
386 {
387 struct mm_struct * mm, * old_mm;
388
389 old_mm = current->mm;
390 if (old_mm && atomic_read(&old_mm->mm_users) == 1) {
391 flush_cache_mm(old_mm);
392 mm_release();
393 exit_mmap(old_mm);
394 flush_tlb_mm(old_mm);
395 return 0;
396 }
397
398 mm = mm_alloc();
399 if (mm) {
400 struct mm_struct *active_mm = current->active_mm;
401
402 if (init_new_context(current, mm)) {
403 mmdrop(mm);
404 return -ENOMEM;
405 }
406
407 /* Add it to the list of mm's */
408 spin_lock(&mmlist_lock);
409 list_add(&mm->mmlist, &init_mm.mmlist);
410 spin_unlock(&mmlist_lock);
411
412 task_lock(current);
413 current->mm = mm;
414 current->active_mm = mm;
415 task_unlock(current);
416 activate_mm(active_mm, mm);
417 mm_release();
418 if (old_mm) {
419 if (active_mm != old_mm) BUG();
420 mmput(old_mm);
421 return 0;
422 }
423 mmdrop(active_mm);
424 return 0;
425 }
426 return -ENOMEM;
427 }
428
429 /*
430 * This function makes sure the current process has its own signal table,
431 * so that flush_signal_handlers can later reset the handlers without
432 * disturbing other processes. (Other processes might share the signal
433 * table via the CLONE_SIGNAL option to clone().)
434 */
435
436 static inline int make_private_signals(void)
437 {
438 struct signal_struct * newsig;
439
440 if (atomic_read(¤t->sig->count) <= 1)
441 return 0;
442 newsig = kmem_cache_alloc(sigact_cachep, GFP_KERNEL);
443 if (newsig == NULL)
444 return -ENOMEM;
445 spin_lock_init(&newsig->siglock);
446 atomic_set(&newsig->count, 1);
447 memcpy(newsig->action, current->sig->action, sizeof(newsig->action));
448 spin_lock_irq(¤t->sigmask_lock);
449 current->sig = newsig;
450 spin_unlock_irq(¤t->sigmask_lock);
451 return 0;
452 }
453
454 /*
455 * If make_private_signals() made a copy of the signal table, decrement the
456 * refcount of the original table, and free it if necessary.
457 * We don't do that in make_private_signals() so that we can back off
458 * in flush_old_exec() if an error occurs after calling make_private_signals().
459 */
460
461 static inline void release_old_signals(struct signal_struct * oldsig)
462 {
463 if (current->sig == oldsig)
464 return;
465 if (atomic_dec_and_test(&oldsig->count))
466 kmem_cache_free(sigact_cachep, oldsig);
467 }
468
469 /*
470 * These functions flushes out all traces of the currently running executable
471 * so that a new one can be started
472 */
473
474 static inline void flush_old_files(struct files_struct * files)
475 {
476 long j = -1;
477
478 write_lock(&files->file_lock);
479 for (;;) {
480 unsigned long set, i;
481
482 j++;
483 i = j * __NFDBITS;
484 if (i >= files->max_fds || i >= files->max_fdset)
485 break;
486 set = files->close_on_exec->fds_bits[j];
487 if (!set)
488 continue;
489 files->close_on_exec->fds_bits[j] = 0;
490 write_unlock(&files->file_lock);
491 for ( ; set ; i++,set >>= 1) {
492 if (set & 1) {
493 sys_close(i);
494 }
495 }
496 write_lock(&files->file_lock);
497
498 }
499 write_unlock(&files->file_lock);
500 }
501
502 /*
503 * An execve() will automatically "de-thread" the process.
504 * Note: we don't have to hold the tasklist_lock to test
505 * whether we migth need to do this. If we're not part of
506 * a thread group, there is no way we can become one
507 * dynamically. And if we are, we only need to protect the
508 * unlink - even if we race with the last other thread exit,
509 * at worst the list_del_init() might end up being a no-op.
510 */
511 static inline void de_thread(struct task_struct *tsk)
512 {
513 if (!list_empty(&tsk->thread_group)) {
514 write_lock_irq(&tasklist_lock);
515 list_del_init(&tsk->thread_group);
516 write_unlock_irq(&tasklist_lock);
517 }
518
519 /* Minor oddity: this might stay the same. */
520 tsk->tgid = tsk->pid;
521 }
522
523 int flush_old_exec(struct linux_binprm * bprm)
524 {
525 char * name;
526 int i, ch, retval;
527 struct signal_struct * oldsig;
528
529 /*
530 * Make sure we have a private signal table
531 */
532 oldsig = current->sig;
533 retval = make_private_signals();
534 if (retval) goto flush_failed;
535
536 /*
537 * Release all of the old mmap stuff
538 */
539 retval = exec_mmap();
540 if (retval) goto mmap_failed;
541
542 /* This is the point of no return */
543 release_old_signals(oldsig);
544
545 current->sas_ss_sp = current->sas_ss_size = 0;
546
547 if (current->euid == current->uid && current->egid == current->gid)
548 current->dumpable = 1;
549 name = bprm->filename;
550 for (i=0; (ch = *(name++)) != '\0';) {
551 if (ch == '/')
552 i = 0;
553 else
554 if (i < 15)
555 current->comm[i++] = ch;
556 }
557 current->comm[i] = '\0';
558
559 flush_thread();
560
561 de_thread(current);
562
563 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
564 permission(bprm->file->f_dentry->d_inode,MAY_READ))
565 current->dumpable = 0;
566
567 /* An exec changes our domain. We are no longer part of the thread
568 group */
569
570 current->self_exec_id++;
571
572 flush_signal_handlers(current);
573 flush_old_files(current->files);
574
575 return 0;
576
577 mmap_failed:
578 flush_failed:
579 spin_lock_irq(¤t->sigmask_lock);
580 if (current->sig != oldsig)
581 kfree(current->sig);
582 current->sig = oldsig;
583 spin_unlock_irq(¤t->sigmask_lock);
584 return retval;
585 }
586
587 /*
588 * We mustn't allow tracing of suid binaries, unless
589 * the tracer has the capability to trace anything..
590 */
591 static inline int must_not_trace_exec(struct task_struct * p)
592 {
593 return (p->ptrace & PT_PTRACED) && !cap_raised(p->p_pptr->cap_effective, CAP_SYS_PTRACE);
594 }
595
596 /*
597 * Fill the binprm structure from the inode.
598 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
599 */
600 int prepare_binprm(struct linux_binprm *bprm)
601 {
602 int mode;
603 struct inode * inode = bprm->file->f_dentry->d_inode;
604
605 mode = inode->i_mode;
606 /* Huh? We had already checked for MAY_EXEC, WTF do we check this? */
607 if (!(mode & 0111)) /* with at least _one_ execute bit set */
608 return -EACCES;
609 if (bprm->file->f_op == NULL)
610 return -EACCES;
611
612 bprm->e_uid = current->euid;
613 bprm->e_gid = current->egid;
614
615 if(!IS_NOSUID(inode)) {
616 /* Set-uid? */
617 if (mode & S_ISUID)
618 bprm->e_uid = inode->i_uid;
619
620 /* Set-gid? */
621 /*
622 * If setgid is set but no group execute bit then this
623 * is a candidate for mandatory locking, not a setgid
624 * executable.
625 */
626 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
627 bprm->e_gid = inode->i_gid;
628 }
629
630 /* We don't have VFS support for capabilities yet */
631 cap_clear(bprm->cap_inheritable);
632 cap_clear(bprm->cap_permitted);
633 cap_clear(bprm->cap_effective);
634
635 /* To support inheritance of root-permissions and suid-root
636 * executables under compatibility mode, we raise all three
637 * capability sets for the file.
638 *
639 * If only the real uid is 0, we only raise the inheritable
640 * and permitted sets of the executable file.
641 */
642
643 if (!issecure(SECURE_NOROOT)) {
644 if (bprm->e_uid == 0 || current->uid == 0) {
645 cap_set_full(bprm->cap_inheritable);
646 cap_set_full(bprm->cap_permitted);
647 }
648 if (bprm->e_uid == 0)
649 cap_set_full(bprm->cap_effective);
650 }
651
652 memset(bprm->buf,0,BINPRM_BUF_SIZE);
653 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
654 }
655
656 /*
657 * This function is used to produce the new IDs and capabilities
658 * from the old ones and the file's capabilities.
659 *
660 * The formula used for evolving capabilities is:
661 *
662 * pI' = pI
663 * (***) pP' = (fP & X) | (fI & pI)
664 * pE' = pP' & fE [NB. fE is 0 or ~0]
665 *
666 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
667 * ' indicates post-exec(), and X is the global 'cap_bset'.
668 *
669 */
670
671 void compute_creds(struct linux_binprm *bprm)
672 {
673 kernel_cap_t new_permitted, working;
674 int do_unlock = 0;
675
676 new_permitted = cap_intersect(bprm->cap_permitted, cap_bset);
677 working = cap_intersect(bprm->cap_inheritable,
678 current->cap_inheritable);
679 new_permitted = cap_combine(new_permitted, working);
680
681 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
682 !cap_issubset(new_permitted, current->cap_permitted)) {
683 current->dumpable = 0;
684
685 lock_kernel();
686 if (must_not_trace_exec(current)
687 || atomic_read(¤t->fs->count) > 1
688 || atomic_read(¤t->files->count) > 1
689 || atomic_read(¤t->sig->count) > 1) {
690 if(!capable(CAP_SETUID)) {
691 bprm->e_uid = current->uid;
692 bprm->e_gid = current->gid;
693 }
694 if(!capable(CAP_SETPCAP)) {
695 new_permitted = cap_intersect(new_permitted,
696 current->cap_permitted);
697 }
698 }
699 do_unlock = 1;
700 }
701
702
703 /* For init, we want to retain the capabilities set
704 * in the init_task struct. Thus we skip the usual
705 * capability rules */
706 if (current->pid != 1) {
707 current->cap_permitted = new_permitted;
708 current->cap_effective =
709 cap_intersect(new_permitted, bprm->cap_effective);
710 }
711
712 /* AUD: Audit candidate if current->cap_effective is set */
713
714 current->suid = current->euid = current->fsuid = bprm->e_uid;
715 current->sgid = current->egid = current->fsgid = bprm->e_gid;
716
717 if(do_unlock)
718 unlock_kernel();
719 current->keep_capabilities = 0;
720 }
721
722
723 void remove_arg_zero(struct linux_binprm *bprm)
724 {
725 if (bprm->argc) {
726 unsigned long offset;
727 char * kaddr;
728 struct page *page;
729
730 offset = bprm->p % PAGE_SIZE;
731 goto inside;
732
733 while (bprm->p++, *(kaddr+offset++)) {
734 if (offset != PAGE_SIZE)
735 continue;
736 offset = 0;
737 kunmap(page);
738 inside:
739 page = bprm->page[bprm->p/PAGE_SIZE];
740 kaddr = kmap(page);
741 }
742 kunmap(page);
743 bprm->argc--;
744 }
745 }
746
747 /*
748 * cycle the list of binary formats handler, until one recognizes the image
749 */
750 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
751 {
752 int try,retval=0;
753 struct linux_binfmt *fmt;
754 #ifdef __alpha__
755 /* handle /sbin/loader.. */
756 {
757 struct exec * eh = (struct exec *) bprm->buf;
758
759 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
760 (eh->fh.f_flags & 0x3000) == 0x3000)
761 {
762 char * dynloader[] = { "/sbin/loader" };
763 struct file * file;
764 unsigned long loader;
765
766 allow_write_access(bprm->file);
767 fput(bprm->file);
768 bprm->file = NULL;
769
770 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
771
772 file = open_exec(dynloader[0]);
773 retval = PTR_ERR(file);
774 if (IS_ERR(file))
775 return retval;
776 bprm->file = file;
777 bprm->loader = loader;
778 retval = prepare_binprm(bprm);
779 if (retval<0)
780 return retval;
781 /* should call search_binary_handler recursively here,
782 but it does not matter */
783 }
784 }
785 #endif
786 for (try=0; try<2; try++) {
787 read_lock(&binfmt_lock);
788 for (fmt = formats ; fmt ; fmt = fmt->next) {
789 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
790 if (!fn)
791 continue;
792 if (!try_inc_mod_count(fmt->module))
793 continue;
794 read_unlock(&binfmt_lock);
795 retval = fn(bprm, regs);
796 if (retval >= 0) {
797 put_binfmt(fmt);
798 allow_write_access(bprm->file);
799 if (bprm->file)
800 fput(bprm->file);
801 bprm->file = NULL;
802 current->did_exec = 1;
803 return retval;
804 }
805 read_lock(&binfmt_lock);
806 put_binfmt(fmt);
807 if (retval != -ENOEXEC)
808 break;
809 if (!bprm->file) {
810 read_unlock(&binfmt_lock);
811 return retval;
812 }
813 }
814 read_unlock(&binfmt_lock);
815 if (retval != -ENOEXEC) {
816 break;
817 #ifdef CONFIG_KMOD
818 }else{
819 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
820 char modname[20];
821 if (printable(bprm->buf[0]) &&
822 printable(bprm->buf[1]) &&
823 printable(bprm->buf[2]) &&
824 printable(bprm->buf[3]))
825 break; /* -ENOEXEC */
826 sprintf(modname, "binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
827 request_module(modname);
828 #endif
829 }
830 }
831 return retval;
832 }
833
834
835 /*
836 * sys_execve() executes a new program.
837 */
838 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
839 {
840 struct linux_binprm bprm;
841 struct file *file;
842 int retval;
843 int i;
844
845 file = open_exec(filename);
846
847 retval = PTR_ERR(file);
848 if (IS_ERR(file))
849 return retval;
850
851 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
852 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
853
854 bprm.file = file;
855 bprm.filename = filename;
856 bprm.sh_bang = 0;
857 bprm.loader = 0;
858 bprm.exec = 0;
859 if ((bprm.argc = count(argv, bprm.p / sizeof(void *))) < 0) {
860 allow_write_access(file);
861 fput(file);
862 return bprm.argc;
863 }
864
865 if ((bprm.envc = count(envp, bprm.p / sizeof(void *))) < 0) {
866 allow_write_access(file);
867 fput(file);
868 return bprm.envc;
869 }
870
871 retval = prepare_binprm(&bprm);
872 if (retval < 0)
873 goto out;
874
875 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
876 if (retval < 0)
877 goto out;
878
879 bprm.exec = bprm.p;
880 retval = copy_strings(bprm.envc, envp, &bprm);
881 if (retval < 0)
882 goto out;
883
884 retval = copy_strings(bprm.argc, argv, &bprm);
885 if (retval < 0)
886 goto out;
887
888 retval = search_binary_handler(&bprm,regs);
889 if (retval >= 0)
890 /* execve success */
891 return retval;
892
893 out:
894 /* Something went wrong, return the inode and free the argument pages*/
895 allow_write_access(bprm.file);
896 if (bprm.file)
897 fput(bprm.file);
898
899 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
900 struct page * page = bprm.page[i];
901 if (page)
902 __free_page(page);
903 }
904
905 return retval;
906 }
907
908 void set_binfmt(struct linux_binfmt *new)
909 {
910 struct linux_binfmt *old = current->binfmt;
911 if (new && new->module)
912 __MOD_INC_USE_COUNT(new->module);
913 current->binfmt = new;
914 if (old && old->module)
915 __MOD_DEC_USE_COUNT(old->module);
916 }
917
918 int do_coredump(long signr, struct pt_regs * regs)
919 {
920 struct linux_binfmt * binfmt;
921 char corename[6+sizeof(current->comm)];
922 struct file * file;
923 struct inode * inode;
924
925 lock_kernel();
926 binfmt = current->binfmt;
927 if (!binfmt || !binfmt->core_dump)
928 goto fail;
929 if (!current->dumpable || atomic_read(¤t->mm->mm_users) != 1)
930 goto fail;
931 current->dumpable = 0;
932 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
933 goto fail;
934
935 memcpy(corename,"core.", 5);
936 #if 0
937 memcpy(corename+5,current->comm,sizeof(current->comm));
938 #else
939 corename[4] = '\0';
940 #endif
941 file = filp_open(corename, O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
942 if (IS_ERR(file))
943 goto fail;
944 inode = file->f_dentry->d_inode;
945 if (inode->i_nlink > 1)
946 goto close_fail; /* multiple links - don't dump */
947
948 if (!S_ISREG(inode->i_mode))
949 goto close_fail;
950 if (!file->f_op)
951 goto close_fail;
952 if (!file->f_op->write)
953 goto close_fail;
954 if (!binfmt->core_dump(signr, regs, file))
955 goto close_fail;
956 unlock_kernel();
957 filp_close(file, NULL);
958 return 1;
959
960 close_fail:
961 filp_close(file, NULL);
962 fail:
963 unlock_kernel();
964 return 0;
965 }
966
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.