1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/config.h>
26#include <linux/slab.h>
27#include <linux/file.h>
28#include <linux/mman.h>
29#include <linux/a.out.h>
30#include <linux/stat.h>
31#include <linux/fcntl.h>
32#include <linux/smp_lock.h>
33#include <linux/init.h>
34#include <linux/pagemap.h>
35#include <linux/highmem.h>
36#include <linux/spinlock.h>
37#include <linux/personality.h>
38#include <linux/binfmts.h>
39#include <linux/swap.h>
40#include <linux/utsname.h>
41#include <linux/module.h>
42#include <linux/namei.h>
43#include <linux/proc_fs.h>
44#include <linux/ptrace.h>
45#include <linux/mount.h>
46#include <linux/security.h>
47#include <linux/rmap-locking.h>
48
49#include <asm/uaccess.h>
50#include <asm/pgalloc.h>
51#include <asm/mmu_context.h>
52
53#ifdef CONFIG_KMOD
54#include <linux/kmod.h>
55#endif
56
57int core_uses_pid;
58char core_pattern[65] = "core";
59
60
61static struct linux_binfmt *formats;
62static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
63
64int register_binfmt(struct linux_binfmt * fmt)
65{
66 struct linux_binfmt ** tmp = &formats;
67
68 if (!fmt)
69 return -EINVAL;
70 if (fmt->next)
71 return -EBUSY;
72 write_lock(&binfmt_lock);
73 while (*tmp) {
74 if (fmt == *tmp) {
75 write_unlock(&binfmt_lock);
76 return -EBUSY;
77 }
78 tmp = &(*tmp)->next;
79 }
80 fmt->next = formats;
81 formats = fmt;
82 write_unlock(&binfmt_lock);
83 return 0;
84}
85
86EXPORT_SYMBOL(register_binfmt);
87
88int unregister_binfmt(struct linux_binfmt * fmt)
89{
90 struct linux_binfmt ** tmp = &formats;
91
92 write_lock(&binfmt_lock);
93 while (*tmp) {
94 if (fmt == *tmp) {
95 *tmp = fmt->next;
96 write_unlock(&binfmt_lock);
97 return 0;
98 }
99 tmp = &(*tmp)->next;
100 }
101 write_unlock(&binfmt_lock);
102 return -EINVAL;
103}
104
105EXPORT_SYMBOL(unregister_binfmt);
106
107static inline void put_binfmt(struct linux_binfmt * fmt)
108{
109 module_put(fmt->module);
110}
111
112
113
114
115
116
117
118asmlinkage long sys_uselib(const char __user * library)
119{
120 struct file * file;
121 struct nameidata nd;
122 int error;
123
124 nd.intent.open.flags = O_RDONLY;
125 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
126 if (error)
127 goto out;
128
129 error = -EINVAL;
130 if (!S_ISREG(nd.dentry->d_inode->i_mode))
131 goto exit;
132
133 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
134 if (error)
135 goto exit;
136
137 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
138 error = PTR_ERR(file);
139 if (IS_ERR(file))
140 goto out;
141
142 error = -ENOEXEC;
143 if(file->f_op) {
144 struct linux_binfmt * fmt;
145
146 read_lock(&binfmt_lock);
147 for (fmt = formats ; fmt ; fmt = fmt->next) {
148 if (!fmt->load_shlib)
149 continue;
150 if (!try_module_get(fmt->module))
151 continue;
152 read_unlock(&binfmt_lock);
153 error = fmt->load_shlib(file);
154 read_lock(&binfmt_lock);
155 put_binfmt(fmt);
156 if (error != -ENOEXEC)
157 break;
158 }
159 read_unlock(&binfmt_lock);
160 }
161 fput(file);
162out:
163 return error;
164exit:
165 path_release(&nd);
166 goto out;
167}
168
169
170
171
172static int count(char __user * __user * argv, int max)
173{
174 int i = 0;
175
176 if (argv != NULL) {
177 for (;;) {
178 char __user * p;
179
180 if (get_user(p, argv))
181 return -EFAULT;
182 if (!p)
183 break;
184 argv++;
185 if(++i > max)
186 return -E2BIG;
187 }
188 }
189 return i;
190}
191
192
193
194
195
196
197int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
198{
199 struct page *kmapped_page = NULL;
200 char *kaddr = NULL;
201 int ret;
202
203 while (argc-- > 0) {
204 char __user *str;
205 int len;
206 unsigned long pos;
207
208 if (get_user(str, argv+argc) ||
209 !(len = strnlen_user(str, bprm->p))) {
210 ret = -EFAULT;
211 goto out;
212 }
213
214 if (bprm->p < len) {
215 ret = -E2BIG;
216 goto out;
217 }
218
219 bprm->p -= len;
220
221 pos = bprm->p;
222
223 while (len > 0) {
224 int i, new, err;
225 int offset, bytes_to_copy;
226 struct page *page;
227
228 offset = pos % PAGE_SIZE;
229 i = pos/PAGE_SIZE;
230 page = bprm->page[i];
231 new = 0;
232 if (!page) {
233 page = alloc_page(GFP_HIGHUSER);
234 bprm->page[i] = page;
235 if (!page) {
236 ret = -ENOMEM;
237 goto out;
238 }
239 new = 1;
240 }
241
242 if (page != kmapped_page) {
243 if (kmapped_page)
244 kunmap(kmapped_page);
245 kmapped_page = page;
246 kaddr = kmap(kmapped_page);
247 }
248 if (new && offset)
249 memset(kaddr, 0, offset);
250 bytes_to_copy = PAGE_SIZE - offset;
251 if (bytes_to_copy > len) {
252 bytes_to_copy = len;
253 if (new)
254 memset(kaddr+offset+len, 0,
255 PAGE_SIZE-offset-len);
256 }
257 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
258 if (err) {
259 ret = -EFAULT;
260 goto out;
261 }
262
263 pos += bytes_to_copy;
264 str += bytes_to_copy;
265 len -= bytes_to_copy;
266 }
267 }
268 ret = 0;
269out:
270 if (kmapped_page)
271 kunmap(kmapped_page);
272 return ret;
273}
274
275
276
277
278int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
279{
280 int r;
281 mm_segment_t oldfs = get_fs();
282 set_fs(KERNEL_DS);
283 r = copy_strings(argc, (char __user * __user *)argv, bprm);
284 set_fs(oldfs);
285 return r;
286}
287
288EXPORT_SYMBOL(copy_strings_kernel);
289
290#ifdef CONFIG_MMU
291
292
293
294
295
296
297void put_dirty_page(struct task_struct *tsk, struct page *page,
298 unsigned long address, pgprot_t prot)
299{
300 pgd_t * pgd;
301 pmd_t * pmd;
302 pte_t * pte;
303 struct pte_chain *pte_chain;
304
305 if (page_count(page) != 1)
306 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n",
307 page, address);
308
309 pgd = pgd_offset(tsk->mm, address);
310 pte_chain = pte_chain_alloc(GFP_KERNEL);
311 if (!pte_chain)
312 goto out_sig;
313 spin_lock(&tsk->mm->page_table_lock);
314 pmd = pmd_alloc(tsk->mm, pgd, address);
315 if (!pmd)
316 goto out;
317 pte = pte_alloc_map(tsk->mm, pmd, address);
318 if (!pte)
319 goto out;
320 if (!pte_none(*pte)) {
321 pte_unmap(pte);
322 goto out;
323 }
324 lru_cache_add_active(page);
325 flush_dcache_page(page);
326 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, prot))));
327 pte_chain = page_add_rmap(page, pte, pte_chain);
328 pte_unmap(pte);
329 tsk->mm->rss++;
330 spin_unlock(&tsk->mm->page_table_lock);
331
332
333 pte_chain_free(pte_chain);
334 return;
335out:
336 spin_unlock(&tsk->mm->page_table_lock);
337out_sig:
338 __free_page(page);
339 force_sig(SIGKILL, tsk);
340 pte_chain_free(pte_chain);
341 return;
342}
343
344int setup_arg_pages(struct linux_binprm *bprm)
345{
346 unsigned long stack_base;
347 struct vm_area_struct *mpnt;
348 struct mm_struct *mm = current->mm;
349 int i;
350 long arg_size;
351
352#ifdef CONFIG_STACK_GROWSUP
353
354
355
356 int offset, j;
357 char *to, *from;
358
359
360 i = 0;
361 for (j = 0; j < MAX_ARG_PAGES; j++) {
362 struct page *page = bprm->page[j];
363 if (!page)
364 continue;
365 bprm->page[i++] = page;
366 }
367
368
369 offset = bprm->p % PAGE_SIZE;
370 to = kmap(bprm->page[0]);
371 for (j = 1; j < i; j++) {
372 memmove(to, to + offset, PAGE_SIZE - offset);
373 from = kmap(bprm->page[j]);
374 memcpy(to + PAGE_SIZE - offset, from, offset);
375 kunmap(bprm->page[j - 1]);
376 to = from;
377 }
378 memmove(to, to + offset, PAGE_SIZE - offset);
379 kunmap(bprm->page[j - 1]);
380
381
382 bprm->p = PAGE_SIZE * i - offset;
383
384
385 stack_base = current->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 mm->arg_start = stack_base;
391 arg_size = i << PAGE_SHIFT;
392
393
394 while (i < MAX_ARG_PAGES)
395 bprm->page[i++] = NULL;
396#else
397 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
398 mm->arg_start = bprm->p + stack_base;
399 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
400#endif
401
402 bprm->p += stack_base;
403 if (bprm->loader)
404 bprm->loader += stack_base;
405 bprm->exec += stack_base;
406
407 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
408 if (!mpnt)
409 return -ENOMEM;
410
411 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
412 kmem_cache_free(vm_area_cachep, mpnt);
413 return -ENOMEM;
414 }
415
416 down_write(&mm->mmap_sem);
417 {
418 mpnt->vm_mm = mm;
419#ifdef CONFIG_STACK_GROWSUP
420 mpnt->vm_start = stack_base;
421 mpnt->vm_end = PAGE_MASK &
422 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
423#else
424 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
425 mpnt->vm_end = STACK_TOP;
426#endif
427 mpnt->vm_page_prot = protection_map[VM_STACK_FLAGS & 0x7];
428 mpnt->vm_flags = VM_STACK_FLAGS;
429 mpnt->vm_ops = NULL;
430 mpnt->vm_pgoff = 0;
431 mpnt->vm_file = NULL;
432 INIT_LIST_HEAD(&mpnt->shared);
433 mpnt->vm_private_data = (void *) 0;
434 insert_vm_struct(mm, mpnt);
435 mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
436 }
437
438 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
439 struct page *page = bprm->page[i];
440 if (page) {
441 bprm->page[i] = NULL;
442 put_dirty_page(current, page, stack_base,
443 mpnt->vm_page_prot);
444 }
445 stack_base += PAGE_SIZE;
446 }
447 up_write(&mm->mmap_sem);
448
449 return 0;
450}
451
452EXPORT_SYMBOL(setup_arg_pages);
453
454#define free_arg_pages(bprm) do { } while (0)
455
456#else
457
458static inline void free_arg_pages(struct linux_binprm *bprm)
459{
460 int i;
461
462 for (i = 0; i < MAX_ARG_PAGES; i++) {
463 if (bprm->page[i])
464 __free_page(bprm->page[i]);
465 bprm->page[i] = NULL;
466 }
467}
468
469#endif
470
471struct file *open_exec(const char *name)
472{
473 struct nameidata nd;
474 int err = path_lookup(name, LOOKUP_FOLLOW, &nd);
475 struct file *file = ERR_PTR(err);
476
477 if (!err) {
478 struct inode *inode = nd.dentry->d_inode;
479 file = ERR_PTR(-EACCES);
480 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
481 S_ISREG(inode->i_mode)) {
482 int err = permission(inode, MAY_EXEC, &nd);
483 if (!err && !(inode->i_mode & 0111))
484 err = -EACCES;
485 file = ERR_PTR(err);
486 if (!err) {
487 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
488 if (!IS_ERR(file)) {
489 err = deny_write_access(file);
490 if (err) {
491 fput(file);
492 file = ERR_PTR(err);
493 }
494 }
495out:
496 return file;
497 }
498 }
499 path_release(&nd);
500 }
501 goto out;
502}
503
504EXPORT_SYMBOL(open_exec);
505
506int kernel_read(struct file *file, unsigned long offset,
507 char *addr, unsigned long count)
508{
509 mm_segment_t old_fs;
510 loff_t pos = offset;
511 int result;
512
513 old_fs = get_fs();
514 set_fs(get_ds());
515
516 result = vfs_read(file, (void __user *)addr, count, &pos);
517 set_fs(old_fs);
518 return result;
519}
520
521EXPORT_SYMBOL(kernel_read);
522
523static int exec_mmap(struct mm_struct *mm)
524{
525 struct task_struct *tsk;
526 struct mm_struct * old_mm, *active_mm;
527
528
529 spin_lock(&mmlist_lock);
530 list_add(&mm->mmlist, &init_mm.mmlist);
531 mmlist_nr++;
532 spin_unlock(&mmlist_lock);
533
534
535 tsk = current;
536 old_mm = current->mm;
537 mm_release(tsk, old_mm);
538
539 task_lock(tsk);
540 active_mm = tsk->active_mm;
541 tsk->mm = mm;
542 tsk->active_mm = mm;
543 activate_mm(active_mm, mm);
544 task_unlock(tsk);
545 if (old_mm) {
546 if (active_mm != old_mm) BUG();
547 mmput(old_mm);
548 return 0;
549 }
550 mmdrop(active_mm);
551 return 0;
552}
553
554
555
556
557
558
559
560static inline int de_thread(struct task_struct *tsk)
561{
562 struct signal_struct *newsig, *oldsig = tsk->signal;
563 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
564 spinlock_t *lock = &oldsighand->siglock;
565 int count;
566
567
568
569
570
571 if (atomic_read(&oldsighand->count) <= 1)
572 return 0;
573
574 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
575 if (!newsighand)
576 return -ENOMEM;
577
578 spin_lock_init(&newsighand->siglock);
579 atomic_set(&newsighand->count, 1);
580 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
581
582
583
584
585 newsig = NULL;
586 if (atomic_read(&oldsig->count) > 1) {
587 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
588 if (!newsig) {
589 kmem_cache_free(sighand_cachep, newsighand);
590 return -ENOMEM;
591 }
592 atomic_set(&newsig->count, 1);
593 newsig->group_exit = 0;
594 newsig->group_exit_code = 0;
595 newsig->group_exit_task = NULL;
596 newsig->group_stop_count = 0;
597 newsig->curr_target = NULL;
598 init_sigpending(&newsig->shared_pending);
599 }
600
601 if (thread_group_empty(current))
602 goto no_thread_group;
603
604
605
606
607
608 read_lock(&tasklist_lock);
609 spin_lock_irq(lock);
610 if (oldsig->group_exit) {
611
612
613
614
615 spin_unlock_irq(lock);
616 read_unlock(&tasklist_lock);
617 kmem_cache_free(sighand_cachep, newsighand);
618 if (newsig)
619 kmem_cache_free(signal_cachep, newsig);
620 return -EAGAIN;
621 }
622 oldsig->group_exit = 1;
623 zap_other_threads(current);
624 read_unlock(&tasklist_lock);
625
626
627
628
629 count = 2;
630 if (current->pid == current->tgid)
631 count = 1;
632 while (atomic_read(&oldsig->count) > count) {
633 oldsig->group_exit_task = current;
634 oldsig->notify_count = count;
635 __set_current_state(TASK_UNINTERRUPTIBLE);
636 spin_unlock_irq(lock);
637 schedule();
638 spin_lock_irq(lock);
639 }
640 spin_unlock_irq(lock);
641
642
643
644
645
646
647 if (current->pid != current->tgid) {
648 struct task_struct *leader = current->group_leader, *parent;
649 struct dentry *proc_dentry1, *proc_dentry2;
650 unsigned long state, ptrace;
651
652
653
654
655
656
657 while (leader->state != TASK_ZOMBIE)
658 yield();
659
660 spin_lock(&leader->proc_lock);
661 spin_lock(¤t->proc_lock);
662 proc_dentry1 = proc_pid_unhash(current);
663 proc_dentry2 = proc_pid_unhash(leader);
664 write_lock_irq(&tasklist_lock);
665
666 if (leader->tgid != current->tgid)
667 BUG();
668 if (current->pid == current->tgid)
669 BUG();
670
671
672
673
674
675
676 ptrace = leader->ptrace;
677 parent = leader->parent;
678
679 ptrace_unlink(current);
680 ptrace_unlink(leader);
681 remove_parent(current);
682 remove_parent(leader);
683
684 switch_exec_pids(leader, current);
685
686 current->parent = current->real_parent = leader->real_parent;
687 leader->parent = leader->real_parent = child_reaper;
688 current->group_leader = current;
689 leader->group_leader = leader;
690
691 add_parent(current, current->parent);
692 add_parent(leader, leader->parent);
693 if (ptrace) {
694 current->ptrace = ptrace;
695 __ptrace_link(current, parent);
696 }
697
698 list_del(¤t->tasks);
699 list_add_tail(¤t->tasks, &init_task.tasks);
700 current->exit_signal = SIGCHLD;
701 state = leader->state;
702
703 write_unlock_irq(&tasklist_lock);
704 spin_unlock(&leader->proc_lock);
705 spin_unlock(¤t->proc_lock);
706 proc_pid_flush(proc_dentry1);
707 proc_pid_flush(proc_dentry2);
708
709 if (state != TASK_ZOMBIE)
710 BUG();
711 release_task(leader);
712 }
713
714no_thread_group:
715
716 write_lock_irq(&tasklist_lock);
717 spin_lock(&oldsighand->siglock);
718 spin_lock(&newsighand->siglock);
719
720 if (current == oldsig->curr_target)
721 oldsig->curr_target = next_thread(current);
722 if (newsig)
723 current->signal = newsig;
724 current->sighand = newsighand;
725 init_sigpending(¤t->pending);
726 recalc_sigpending();
727
728 spin_unlock(&newsighand->siglock);
729 spin_unlock(&oldsighand->siglock);
730 write_unlock_irq(&tasklist_lock);
731
732 if (newsig && atomic_dec_and_test(&oldsig->count))
733 kmem_cache_free(signal_cachep, oldsig);
734
735 if (atomic_dec_and_test(&oldsighand->count))
736 kmem_cache_free(sighand_cachep, oldsighand);
737
738 if (!thread_group_empty(current))
739 BUG();
740 if (current->tgid != current->pid)
741 BUG();
742 return 0;
743}
744
745
746
747
748
749
750static inline void flush_old_files(struct files_struct * files)
751{
752 long j = -1;
753
754 spin_lock(&files->file_lock);
755 for (;;) {
756 unsigned long set, i;
757
758 j++;
759 i = j * __NFDBITS;
760 if (i >= files->max_fds || i >= files->max_fdset)
761 break;
762 set = files->close_on_exec->fds_bits[j];
763 if (!set)
764 continue;
765 files->close_on_exec->fds_bits[j] = 0;
766 spin_unlock(&files->file_lock);
767 for ( ; set ; i++,set >>= 1) {
768 if (set & 1) {
769 sys_close(i);
770 }
771 }
772 spin_lock(&files->file_lock);
773
774 }
775 spin_unlock(&files->file_lock);
776}
777
778int flush_old_exec(struct linux_binprm * bprm)
779{
780 char * name;
781 int i, ch, retval;
782 struct files_struct *files;
783
784
785
786
787
788 retval = de_thread(current);
789 if (retval)
790 goto out;
791
792
793
794
795
796
797 files = current->files;
798 retval = unshare_files();
799 if (retval)
800 goto out;
801
802
803
804 retval = exec_mmap(bprm->mm);
805 if (retval)
806 goto mmap_failed;
807
808 bprm->mm = NULL;
809
810
811 steal_locks(files);
812 put_files_struct(files);
813
814 current->sas_ss_sp = current->sas_ss_size = 0;
815
816 if (current->euid == current->uid && current->egid == current->gid)
817 current->mm->dumpable = 1;
818 name = bprm->filename;
819 for (i=0; (ch = *(name++)) != '\0';) {
820 if (ch == '/')
821 i = 0;
822 else
823 if (i < 15)
824 current->comm[i++] = ch;
825 }
826 current->comm[i] = '\0';
827
828 flush_thread();
829
830 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
831 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
832 current->mm->dumpable = 0;
833
834
835
836
837 current->self_exec_id++;
838
839 flush_signal_handlers(current, 0);
840 flush_old_files(current->files);
841 exit_itimers(current);
842
843 return 0;
844
845mmap_failed:
846 put_files_struct(current->files);
847 current->files = files;
848out:
849 return retval;
850}
851
852EXPORT_SYMBOL(flush_old_exec);
853
854
855
856
857
858static inline int must_not_trace_exec(struct task_struct * p)
859{
860 return (p->ptrace & PT_PTRACED) && !(p->ptrace & PT_PTRACE_CAP);
861}
862
863
864
865
866
867int prepare_binprm(struct linux_binprm *bprm)
868{
869 int mode;
870 struct inode * inode = bprm->file->f_dentry->d_inode;
871 int retval;
872
873 mode = inode->i_mode;
874
875
876
877
878 if (!(mode & 0111))
879 return -EACCES;
880 if (bprm->file->f_op == NULL)
881 return -EACCES;
882
883 bprm->e_uid = current->euid;
884 bprm->e_gid = current->egid;
885
886 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
887
888 if (mode & S_ISUID)
889 bprm->e_uid = inode->i_uid;
890
891
892
893
894
895
896
897 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
898 bprm->e_gid = inode->i_gid;
899 }
900
901
902 retval = security_bprm_set(bprm);
903 if (retval)
904 return retval;
905
906 memset(bprm->buf,0,BINPRM_BUF_SIZE);
907 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
908}
909
910EXPORT_SYMBOL(prepare_binprm);
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927void compute_creds(struct linux_binprm *bprm)
928{
929 task_lock(current);
930 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid) {
931 current->mm->dumpable = 0;
932
933 if (must_not_trace_exec(current)
934 || atomic_read(¤t->fs->count) > 1
935 || atomic_read(¤t->files->count) > 1
936 || atomic_read(¤t->sighand->count) > 1) {
937 if(!capable(CAP_SETUID)) {
938 bprm->e_uid = current->uid;
939 bprm->e_gid = current->gid;
940 }
941 }
942 }
943
944 current->suid = current->euid = current->fsuid = bprm->e_uid;
945 current->sgid = current->egid = current->fsgid = bprm->e_gid;
946
947 task_unlock(current);
948
949 security_bprm_compute_creds(bprm);
950}
951
952EXPORT_SYMBOL(compute_creds);
953
954void remove_arg_zero(struct linux_binprm *bprm)
955{
956 if (bprm->argc) {
957 unsigned long offset;
958 char * kaddr;
959 struct page *page;
960
961 offset = bprm->p % PAGE_SIZE;
962 goto inside;
963
964 while (bprm->p++, *(kaddr+offset++)) {
965 if (offset != PAGE_SIZE)
966 continue;
967 offset = 0;
968 kunmap_atomic(kaddr, KM_USER0);
969inside:
970 page = bprm->page[bprm->p/PAGE_SIZE];
971 kaddr = kmap_atomic(page, KM_USER0);
972 }
973 kunmap_atomic(kaddr, KM_USER0);
974 bprm->argc--;
975 }
976}
977
978EXPORT_SYMBOL(remove_arg_zero);
979
980
981
982
983int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
984{
985 int try,retval=0;
986 struct linux_binfmt *fmt;
987#ifdef __alpha__
988
989 {
990 struct exec * eh = (struct exec *) bprm->buf;
991
992 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
993 (eh->fh.f_flags & 0x3000) == 0x3000)
994 {
995 struct file * file;
996 unsigned long loader;
997
998 allow_write_access(bprm->file);
999 fput(bprm->file);
1000 bprm->file = NULL;
1001
1002 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1003
1004 file = open_exec("/sbin/loader");
1005 retval = PTR_ERR(file);
1006 if (IS_ERR(file))
1007 return retval;
1008
1009
1010 bprm->sh_bang = eh->ah.entry < 0x100000000;
1011
1012 bprm->file = file;
1013 bprm->loader = loader;
1014 retval = prepare_binprm(bprm);
1015 if (retval<0)
1016 return retval;
1017
1018
1019 }
1020 }
1021#endif
1022 retval = security_bprm_check(bprm);
1023 if (retval)
1024 return retval;
1025
1026
1027
1028 set_fs(USER_DS);
1029 for (try=0; try<2; try++) {
1030 read_lock(&binfmt_lock);
1031 for (fmt = formats ; fmt ; fmt = fmt->next) {
1032 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1033 if (!fn)
1034 continue;
1035 if (!try_module_get(fmt->module))
1036 continue;
1037 read_unlock(&binfmt_lock);
1038 retval = fn(bprm, regs);
1039 if (retval >= 0) {
1040 put_binfmt(fmt);
1041 allow_write_access(bprm->file);
1042 if (bprm->file)
1043 fput(bprm->file);
1044 bprm->file = NULL;
1045 current->did_exec = 1;
1046 return retval;
1047 }
1048 read_lock(&binfmt_lock);
1049 put_binfmt(fmt);
1050 if (retval != -ENOEXEC || bprm->mm == NULL)
1051 break;
1052 if (!bprm->file) {
1053 read_unlock(&binfmt_lock);
1054 return retval;
1055 }
1056 }
1057 read_unlock(&binfmt_lock);
1058 if (retval != -ENOEXEC || bprm->mm == NULL) {
1059 break;
1060#ifdef CONFIG_KMOD
1061 }else{
1062#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1063 if (printable(bprm->buf[0]) &&
1064 printable(bprm->buf[1]) &&
1065 printable(bprm->buf[2]) &&
1066 printable(bprm->buf[3]))
1067 break;
1068 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1069#endif
1070 }
1071 }
1072 return retval;
1073}
1074
1075EXPORT_SYMBOL(search_binary_handler);
1076
1077
1078
1079
1080int do_execve(char * filename,
1081 char __user *__user *argv,
1082 char __user *__user *envp,
1083 struct pt_regs * regs)
1084{
1085 struct linux_binprm bprm;
1086 struct file *file;
1087 int retval;
1088 int i;
1089
1090 sched_balance_exec();
1091
1092 file = open_exec(filename);
1093
1094 retval = PTR_ERR(file);
1095 if (IS_ERR(file))
1096 return retval;
1097
1098 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1099 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1100
1101 bprm.file = file;
1102 bprm.filename = filename;
1103 bprm.interp = filename;
1104 bprm.sh_bang = 0;
1105 bprm.loader = 0;
1106 bprm.exec = 0;
1107 bprm.security = NULL;
1108 bprm.mm = mm_alloc();
1109 retval = -ENOMEM;
1110 if (!bprm.mm)
1111 goto out_file;
1112
1113 retval = init_new_context(current, bprm.mm);
1114 if (retval < 0)
1115 goto out_mm;
1116
1117 bprm.argc = count(argv, bprm.p / sizeof(void *));
1118 if ((retval = bprm.argc) < 0)
1119 goto out_mm;
1120
1121 bprm.envc = count(envp, bprm.p / sizeof(void *));
1122 if ((retval = bprm.envc) < 0)
1123 goto out_mm;
1124
1125 retval = security_bprm_alloc(&bprm);
1126 if (retval)
1127 goto out;
1128
1129 retval = prepare_binprm(&bprm);
1130 if (retval < 0)
1131 goto out;
1132
1133 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1134 if (retval < 0)
1135 goto out;
1136
1137 bprm.exec = bprm.p;
1138 retval = copy_strings(bprm.envc, envp, &bprm);
1139 if (retval < 0)
1140 goto out;
1141
1142 retval = copy_strings(bprm.argc, argv, &bprm);
1143 if (retval < 0)
1144 goto out;
1145
1146 retval = search_binary_handler(&bprm,regs);
1147 if (retval >= 0) {
1148 free_arg_pages(&bprm);
1149
1150
1151 security_bprm_free(&bprm);
1152 return retval;
1153 }
1154
1155out:
1156
1157 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1158 struct page * page = bprm.page[i];
1159 if (page)
1160 __free_page(page);
1161 }
1162
1163 if (bprm.security)
1164 security_bprm_free(&bprm);
1165
1166out_mm:
1167 if (bprm.mm)
1168 mmdrop(bprm.mm);
1169
1170out_file:
1171 if (bprm.file) {
1172 allow_write_access(bprm.file);
1173 fput(bprm.file);
1174 }
1175 return retval;
1176}
1177
1178EXPORT_SYMBOL(do_execve);
1179
1180int set_binfmt(struct linux_binfmt *new)
1181{
1182 struct linux_binfmt *old = current->binfmt;
1183
1184 if (new) {
1185 if (!try_module_get(new->module))
1186 return -1;
1187 }
1188 current->binfmt = new;
1189 if (old)
1190 module_put(old->module);
1191 return 0;
1192}
1193
1194EXPORT_SYMBOL(set_binfmt);
1195
1196#define CORENAME_MAX_SIZE 64
1197
1198
1199
1200
1201
1202void format_corename(char *corename, const char *pattern, long signr)
1203{
1204 const char *pat_ptr = pattern;
1205 char *out_ptr = corename;
1206 char *const out_end = corename + CORENAME_MAX_SIZE;
1207 int rc;
1208 int pid_in_pattern = 0;
1209
1210
1211
1212 while (*pat_ptr) {
1213 if (*pat_ptr != '%') {
1214 if (out_ptr == out_end)
1215 goto out;
1216 *out_ptr++ = *pat_ptr++;
1217 } else {
1218 switch (*++pat_ptr) {
1219 case 0:
1220 goto out;
1221
1222 case '%':
1223 if (out_ptr == out_end)
1224 goto out;
1225 *out_ptr++ = '%';
1226 break;
1227
1228 case 'p':
1229 pid_in_pattern = 1;
1230 rc = snprintf(out_ptr, out_end - out_ptr,
1231 "%d", current->tgid);
1232 if (rc > out_end - out_ptr)
1233 goto out;
1234 out_ptr += rc;
1235 break;
1236
1237 case 'u':
1238 rc = snprintf(out_ptr, out_end - out_ptr,
1239 "%d", current->uid);
1240 if (rc > out_end - out_ptr)
1241 goto out;
1242 out_ptr += rc;
1243 break;
1244
1245 case 'g':
1246 rc = snprintf(out_ptr, out_end - out_ptr,
1247 "%d", current->gid);
1248 if (rc > out_end - out_ptr)
1249 goto out;
1250 out_ptr += rc;
1251 break;
1252
1253 case 's':
1254 rc = snprintf(out_ptr, out_end - out_ptr,
1255 "%ld", signr);
1256 if (rc > out_end - out_ptr)
1257 goto out;
1258 out_ptr += rc;
1259 break;
1260
1261 case 't': {
1262 struct timeval tv;
1263 do_gettimeofday(&tv);
1264 rc = snprintf(out_ptr, out_end - out_ptr,
1265 "%lu", tv.tv_sec);
1266 if (rc > out_end - out_ptr)
1267 goto out;
1268 out_ptr += rc;
1269 break;
1270 }
1271
1272 case 'h':
1273 down_read(&uts_sem);
1274 rc = snprintf(out_ptr, out_end - out_ptr,
1275 "%s", system_utsname.nodename);
1276 up_read(&uts_sem);
1277 if (rc > out_end - out_ptr)
1278 goto out;
1279 out_ptr += rc;
1280 break;
1281
1282 case 'e':
1283 rc = snprintf(out_ptr, out_end - out_ptr,
1284 "%s", current->comm);
1285 if (rc > out_end - out_ptr)
1286 goto out;
1287 out_ptr += rc;
1288 break;
1289 default:
1290 break;
1291 }
1292 ++pat_ptr;
1293 }
1294 }
1295
1296
1297
1298
1299
1300 if (!pid_in_pattern
1301 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1302 rc = snprintf(out_ptr, out_end - out_ptr,
1303 ".%d", current->tgid);
1304 if (rc > out_end - out_ptr)
1305 goto out;
1306 out_ptr += rc;
1307 }
1308 out:
1309 *out_ptr = 0;
1310}
1311
1312static void zap_threads (struct mm_struct *mm)
1313{
1314 struct task_struct *g, *p;
1315 struct task_struct *tsk = current;
1316 struct completion *vfork_done = tsk->vfork_done;
1317
1318
1319
1320
1321
1322 if (vfork_done) {
1323 tsk->vfork_done = NULL;
1324 complete(vfork_done);
1325 }
1326
1327 read_lock(&tasklist_lock);
1328 do_each_thread(g,p)
1329 if (mm == p->mm && p != tsk) {
1330 force_sig_specific(SIGKILL, p);
1331 mm->core_waiters++;
1332 }
1333 while_each_thread(g,p);
1334
1335 read_unlock(&tasklist_lock);
1336}
1337
1338static void coredump_wait(struct mm_struct *mm)
1339{
1340 DECLARE_COMPLETION(startup_done);
1341
1342 mm->core_waiters++;
1343 mm->core_startup_done = &startup_done;
1344
1345
1346 yield();
1347
1348 zap_threads(mm);
1349 if (--mm->core_waiters) {
1350 up_write(&mm->mmap_sem);
1351 wait_for_completion(&startup_done);
1352 } else
1353 up_write(&mm->mmap_sem);
1354 BUG_ON(mm->core_waiters);
1355}
1356
1357int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1358{
1359 char corename[CORENAME_MAX_SIZE + 1];
1360 struct mm_struct *mm = current->mm;
1361 struct linux_binfmt * binfmt;
1362 struct inode * inode;
1363 struct file * file;
1364 int retval = 0;
1365
1366 lock_kernel();
1367 binfmt = current->binfmt;
1368 if (!binfmt || !binfmt->core_dump)
1369 goto fail;
1370 down_write(&mm->mmap_sem);
1371 if (!mm->dumpable) {
1372 up_write(&mm->mmap_sem);
1373 goto fail;
1374 }
1375 mm->dumpable = 0;
1376 init_completion(&mm->core_done);
1377 current->signal->group_exit = 1;
1378 current->signal->group_exit_code = exit_code;
1379 coredump_wait(mm);
1380
1381 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1382 goto fail_unlock;
1383
1384 format_corename(corename, core_pattern, signr);
1385 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW, 0600);
1386 if (IS_ERR(file))
1387 goto fail_unlock;
1388 inode = file->f_dentry->d_inode;
1389 if (inode->i_nlink > 1)
1390 goto close_fail;
1391 if (d_unhashed(file->f_dentry))
1392 goto close_fail;
1393
1394 if (!S_ISREG(inode->i_mode))
1395 goto close_fail;
1396 if (!file->f_op)
1397 goto close_fail;
1398 if (!file->f_op->write)
1399 goto close_fail;
1400 if (do_truncate(file->f_dentry, 0) != 0)
1401 goto close_fail;
1402
1403 retval = binfmt->core_dump(signr, regs, file);
1404
1405 current->signal->group_exit_code |= 0x80;
1406close_fail:
1407 filp_close(file, NULL);
1408fail_unlock:
1409 complete_all(&mm->core_done);
1410fail:
1411 unlock_kernel();
1412 return retval;
1413}
1414