This source file includes following definitions.
- register_binfmt
- unregister_binfmt
- open_inode
- aout_core_dump
- sys_uselib
- create_tables
- count
- copy_strings
- setup_arg_pages
- read_exec
- exec_mmap
- flush_old_exec
- do_execve
- set_brk
- load_aout_binary
- load_aout_library
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/fs.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/mman.h>
30 #include <linux/a.out.h>
31 #include <linux/errno.h>
32 #include <linux/signal.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/user.h>
38 #include <linux/malloc.h>
39 #include <linux/binfmts.h>
40 #include <linux/personality.h>
41
42 #include <asm/system.h>
43 #include <asm/segment.h>
44 #include <asm/pgtable.h>
45
46 #include <linux/config.h>
47 #ifdef CONFIG_KERNELD
48 #include <linux/kerneld.h>
49 #endif
50
51 asmlinkage int sys_exit(int exit_code);
52 asmlinkage int sys_brk(unsigned long);
53
54 static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
55 static int load_aout_library(int fd);
56
57 extern void dump_thread(struct pt_regs *, struct user *);
58
59
60
61
62
63 extern struct linux_binfmt elf_format;
64
65 static struct linux_binfmt aout_format = {
66 #ifndef CONFIG_BINFMT_ELF
67 NULL, NULL, load_aout_binary, load_aout_library, aout_core_dump
68 #else
69 &elf_format, NULL, load_aout_binary, load_aout_library, aout_core_dump
70 #endif
71 };
72
73 static struct linux_binfmt *formats = &aout_format;
74
75 #ifdef CONFIG_MODULES
76 int register_binfmt(struct linux_binfmt * fmt)
77 {
78 struct linux_binfmt ** tmp = &formats;
79
80 if (!fmt)
81 return -EINVAL;
82 if (fmt->next)
83 return -EBUSY;
84 while (*tmp) {
85 if (fmt == *tmp)
86 return -EBUSY;
87 tmp = &(*tmp)->next;
88 }
89 fmt->next = formats;
90 formats = fmt;
91 return 0;
92 }
93
94 int unregister_binfmt(struct linux_binfmt * fmt)
95 {
96 struct linux_binfmt ** tmp = &formats;
97
98 while (*tmp) {
99 if (fmt == *tmp) {
100 *tmp = fmt->next;
101 return 0;
102 }
103 tmp = &(*tmp)->next;
104 }
105 return -EINVAL;
106 }
107 #endif
108
109 int open_inode(struct inode * inode, int mode)
110 {
111 int error, fd;
112 struct file *f, **fpp;
113
114 if (!inode->i_op || !inode->i_op->default_file_ops)
115 return -EINVAL;
116 f = get_empty_filp();
117 if (!f)
118 return -ENFILE;
119 fd = 0;
120 fpp = current->files->fd;
121 for (;;) {
122 if (!*fpp)
123 break;
124 if (++fd >= NR_OPEN) {
125 f->f_count--;
126 return -EMFILE;
127 }
128 fpp++;
129 }
130 *fpp = f;
131 f->f_flags = mode;
132 f->f_mode = (mode+1) & O_ACCMODE;
133 f->f_inode = inode;
134 f->f_pos = 0;
135 f->f_reada = 0;
136 f->f_op = inode->i_op->default_file_ops;
137 if (f->f_op->open) {
138 error = f->f_op->open(inode,f);
139 if (error) {
140 *fpp = NULL;
141 f->f_count--;
142 return error;
143 }
144 }
145 inode->i_count++;
146 return fd;
147 }
148
149
150
151
152
153 #define DUMP_WRITE(addr,nr) \
154 while (file.f_op->write(inode,&file,(char *)(addr),(nr)) != (nr)) goto close_coredump
155
156 #define DUMP_SEEK(offset) \
157 if (file.f_op->lseek) { \
158 if (file.f_op->lseek(inode,&file,(offset),0) != (offset)) \
159 goto close_coredump; \
160 } else file.f_pos = (offset)
161
162
163
164
165
166
167
168
169
170
171 int aout_core_dump(long signr, struct pt_regs * regs)
172 {
173 struct inode * inode = NULL;
174 struct file file;
175 unsigned short fs;
176 int has_dumped = 0;
177 char corefile[6+sizeof(current->comm)];
178 unsigned long dump_start, dump_size;
179 struct user dump;
180 #ifdef __alpha__
181 # define START_DATA(u) (u.start_data)
182 #else
183 # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
184 #endif
185
186 if (!current->dumpable)
187 return 0;
188 current->dumpable = 0;
189
190
191 if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)
192 return 0;
193 fs = get_fs();
194 set_fs(KERNEL_DS);
195 memcpy(corefile,"core.",5);
196 #if 0
197 memcpy(corefile+5,current->comm,sizeof(current->comm));
198 #else
199 corefile[4] = '\0';
200 #endif
201 if (open_namei(corefile,O_CREAT | 2 | O_TRUNC,0600,&inode,NULL)) {
202 inode = NULL;
203 goto end_coredump;
204 }
205 if (!S_ISREG(inode->i_mode))
206 goto end_coredump;
207 if (!inode->i_op || !inode->i_op->default_file_ops)
208 goto end_coredump;
209 if (get_write_access(inode))
210 goto end_coredump;
211 file.f_mode = 3;
212 file.f_flags = 0;
213 file.f_count = 1;
214 file.f_inode = inode;
215 file.f_pos = 0;
216 file.f_reada = 0;
217 file.f_op = inode->i_op->default_file_ops;
218 if (file.f_op->open)
219 if (file.f_op->open(inode,&file))
220 goto done_coredump;
221 if (!file.f_op->write)
222 goto close_coredump;
223 has_dumped = 1;
224 strncpy(dump.u_comm, current->comm, sizeof(current->comm));
225 dump.u_ar0 = (void *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
226 dump.signal = signr;
227 dump_thread(regs, &dump);
228
229
230
231 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
232 current->rlim[RLIMIT_CORE].rlim_cur)
233 dump.u_dsize = 0;
234
235
236 if ((dump.u_ssize+1) * PAGE_SIZE >
237 current->rlim[RLIMIT_CORE].rlim_cur)
238 dump.u_ssize = 0;
239
240
241 set_fs(USER_DS);
242 if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
243 dump.u_dsize = 0;
244 if (verify_area(VERIFY_READ, (void *) dump.start_stack, dump.u_ssize << PAGE_SHIFT))
245 dump.u_ssize = 0;
246
247 set_fs(KERNEL_DS);
248
249 DUMP_WRITE(&dump,sizeof(dump));
250
251 DUMP_SEEK(PAGE_SIZE);
252
253 set_fs(USER_DS);
254
255 if (dump.u_dsize != 0) {
256 dump_start = START_DATA(dump);
257 dump_size = dump.u_dsize << PAGE_SHIFT;
258 DUMP_WRITE(dump_start,dump_size);
259 }
260
261 if (dump.u_ssize != 0) {
262 dump_start = dump.start_stack;
263 dump_size = dump.u_ssize << PAGE_SHIFT;
264 DUMP_WRITE(dump_start,dump_size);
265 }
266
267 set_fs(KERNEL_DS);
268 DUMP_WRITE(current,sizeof(*current));
269 close_coredump:
270 if (file.f_op->release)
271 file.f_op->release(inode,&file);
272 done_coredump:
273 put_write_access(inode);
274 end_coredump:
275 set_fs(fs);
276 iput(inode);
277 return has_dumped;
278 }
279
280
281
282
283
284
285
286 asmlinkage int sys_uselib(const char * library)
287 {
288 int fd, retval;
289 struct file * file;
290 struct linux_binfmt * fmt;
291
292 fd = sys_open(library, 0, 0);
293 if (fd < 0)
294 return fd;
295 file = current->files->fd[fd];
296 retval = -ENOEXEC;
297 if (file && file->f_inode && file->f_op && file->f_op->read) {
298 for (fmt = formats ; fmt ; fmt = fmt->next) {
299 int (*fn)(int) = fmt->load_shlib;
300 if (!fn)
301 break;
302 retval = fn(fd);
303 if (retval != -ENOEXEC)
304 break;
305 }
306 }
307 sys_close(fd);
308 return retval;
309 }
310
311
312
313
314
315
316 unsigned long * create_tables(char * p, struct linux_binprm * bprm, int ibcs)
317 {
318 unsigned long *argv,*envp;
319 unsigned long * sp;
320 struct vm_area_struct *mpnt;
321 int argc = bprm->argc;
322 int envc = bprm->envc;
323
324 mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
325 if (mpnt) {
326 mpnt->vm_mm = current->mm;
327 mpnt->vm_start = PAGE_MASK & (unsigned long) p;
328 mpnt->vm_end = STACK_TOP;
329 mpnt->vm_page_prot = PAGE_COPY;
330 mpnt->vm_flags = VM_STACK_FLAGS;
331 mpnt->vm_ops = NULL;
332 mpnt->vm_offset = 0;
333 mpnt->vm_inode = NULL;
334 mpnt->vm_pte = 0;
335 insert_vm_struct(current, mpnt);
336 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
337 }
338 sp = (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
339 #ifdef __alpha__
340
341 put_user(0, --sp);
342 put_user(0, --sp);
343 if (bprm->loader) {
344 put_user(0, --sp);
345 put_user(0x3eb, --sp);
346 put_user(bprm->loader, --sp);
347 put_user(0x3ea, --sp);
348 }
349 put_user(bprm->exec, --sp);
350 put_user(0x3e9, --sp);
351 #endif
352 sp -= envc+1;
353 envp = sp;
354 sp -= argc+1;
355 argv = sp;
356 #ifdef __i386__
357 if (!ibcs) {
358 put_user(envp,--sp);
359 put_user(argv,--sp);
360 }
361 #endif
362 put_user(argc,--sp);
363 current->mm->arg_start = (unsigned long) p;
364 while (argc-->0) {
365 put_user(p,argv++);
366 while (get_user(p++)) ;
367 }
368 put_user(NULL,argv);
369 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
370 while (envc-->0) {
371 put_user(p,envp++);
372 while (get_user(p++)) ;
373 }
374 put_user(NULL,envp);
375 current->mm->env_end = (unsigned long) p;
376 return sp;
377 }
378
379
380
381
382
383
384
385 static int count(char ** argv)
386 {
387 int error, i = 0;
388 char ** tmp, *p;
389
390 if ((tmp = argv) != NULL) {
391 error = verify_area(VERIFY_READ, tmp, sizeof(char *));
392 if (error)
393 return error;
394 while ((p = get_user(tmp++)) != NULL) {
395 i++;
396 error = verify_area(VERIFY_READ, p, 1);
397 if (error)
398 return error;
399 }
400 }
401 return i;
402 }
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
422 unsigned long p, int from_kmem)
423 {
424 char *tmp, *pag = NULL;
425 int len, offset = 0;
426 unsigned long old_fs, new_fs;
427
428 if (!p)
429 return 0;
430 new_fs = get_ds();
431 old_fs = get_fs();
432 if (from_kmem==2)
433 set_fs(new_fs);
434 while (argc-- > 0) {
435 if (from_kmem == 1)
436 set_fs(new_fs);
437 if (!(tmp = get_user(argv+argc)))
438 panic("VFS: argc is wrong");
439 if (from_kmem == 1)
440 set_fs(old_fs);
441 len=0;
442 do {
443 len++;
444 } while (get_user(tmp++));
445 if (p < len) {
446 set_fs(old_fs);
447 return 0;
448 }
449 while (len) {
450 --p; --tmp; --len;
451 if (--offset < 0) {
452 offset = p % PAGE_SIZE;
453 if (from_kmem==2)
454 set_fs(old_fs);
455 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
456 !(pag = (char *) page[p/PAGE_SIZE] =
457 (unsigned long *) get_free_page(GFP_USER)))
458 return 0;
459 if (from_kmem==2)
460 set_fs(new_fs);
461
462 }
463 *(pag + offset) = get_user(tmp);
464 }
465 }
466 if (from_kmem==2)
467 set_fs(old_fs);
468 return p;
469 }
470
471 unsigned long setup_arg_pages(unsigned long text_size, unsigned long * page)
472 {
473 unsigned long data_base;
474 int i;
475
476 data_base = STACK_TOP;
477 for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
478 data_base -= PAGE_SIZE;
479 if (page[i]) {
480 current->mm->rss++;
481 put_dirty_page(current,page[i],data_base);
482 }
483 }
484 return STACK_TOP;
485 }
486
487
488
489
490
491
492 int read_exec(struct inode *inode, unsigned long offset,
493 char * addr, unsigned long count, int to_kmem)
494 {
495 struct file file;
496 int result = -ENOEXEC;
497
498 if (!inode->i_op || !inode->i_op->default_file_ops)
499 goto end_readexec;
500 file.f_mode = 1;
501 file.f_flags = 0;
502 file.f_count = 1;
503 file.f_inode = inode;
504 file.f_pos = 0;
505 file.f_reada = 0;
506 file.f_op = inode->i_op->default_file_ops;
507 if (file.f_op->open)
508 if (file.f_op->open(inode,&file))
509 goto end_readexec;
510 if (!file.f_op || !file.f_op->read)
511 goto close_readexec;
512 if (file.f_op->lseek) {
513 if (file.f_op->lseek(inode,&file,offset,0) != offset)
514 goto close_readexec;
515 } else
516 file.f_pos = offset;
517 if (to_kmem) {
518 unsigned long old_fs = get_fs();
519 set_fs(get_ds());
520 result = file.f_op->read(inode, &file, addr, count);
521 set_fs(old_fs);
522 } else {
523 result = verify_area(VERIFY_WRITE, addr, count);
524 if (result)
525 goto close_readexec;
526 result = file.f_op->read(inode, &file, addr, count);
527 }
528 close_readexec:
529 if (file.f_op->release)
530 file.f_op->release(inode,&file);
531 end_readexec:
532 return result;
533 }
534
535 static void exec_mmap(void)
536 {
537
538
539
540
541
542 if (current->mm->count > 1) {
543 struct mm_struct *mm = kmalloc(sizeof(*mm), GFP_KERNEL);
544 if (!mm) {
545
546 oom(current);
547 return;
548 }
549 *mm = *current->mm;
550 mm->def_flags = 0;
551 mm->count = 1;
552 mm->mmap = NULL;
553 mm->mmap_avl = NULL;
554 mm->total_vm = 0;
555 mm->rss = 0;
556 current->mm->count--;
557 current->mm = mm;
558 new_page_tables(current);
559 return;
560 }
561 exit_mmap(current->mm);
562 clear_page_tables(current);
563 }
564
565
566
567
568
569
570 void flush_old_exec(struct linux_binprm * bprm)
571 {
572 int i;
573 int ch;
574 char * name;
575
576 if (current->euid == current->uid && current->egid == current->gid)
577 current->dumpable = 1;
578 name = bprm->filename;
579 for (i=0; (ch = *(name++)) != '\0';) {
580 if (ch == '/')
581 i = 0;
582 else
583 if (i < 15)
584 current->comm[i++] = ch;
585 }
586 current->comm[i] = '\0';
587
588
589 exec_mmap();
590
591 flush_thread();
592
593 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
594 permission(bprm->inode,MAY_READ))
595 current->dumpable = 0;
596 current->signal = 0;
597 for (i=0 ; i<32 ; i++) {
598 current->sig->action[i].sa_mask = 0;
599 current->sig->action[i].sa_flags = 0;
600 if (current->sig->action[i].sa_handler != SIG_IGN)
601 current->sig->action[i].sa_handler = NULL;
602 }
603 for (i=0 ; i<NR_OPEN ; i++)
604 if (FD_ISSET(i,¤t->files->close_on_exec))
605 sys_close(i);
606 FD_ZERO(¤t->files->close_on_exec);
607 if (last_task_used_math == current)
608 last_task_used_math = NULL;
609 current->used_math = 0;
610 }
611
612
613
614
615 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
616 {
617 struct linux_binprm bprm;
618 struct linux_binfmt * fmt;
619 int i;
620 int retval;
621 int sh_bang = 0;
622 int try;
623 #ifdef __alpha__
624 int loader = 0;
625 #endif
626
627 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
628 for (i=0 ; i<MAX_ARG_PAGES ; i++)
629 bprm.page[i] = 0;
630 retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
631 if (retval)
632 return retval;
633 bprm.filename = filename;
634 bprm.loader = 0;
635 bprm.exec = 0;
636 if ((bprm.argc = count(argv)) < 0)
637 return bprm.argc;
638 if ((bprm.envc = count(envp)) < 0)
639 return bprm.envc;
640
641 restart_interp:
642 if (!S_ISREG(bprm.inode->i_mode)) {
643 retval = -EACCES;
644 goto exec_error2;
645 }
646 if (IS_NOEXEC(bprm.inode)) {
647 retval = -EPERM;
648 goto exec_error2;
649 }
650 if (!bprm.inode->i_sb) {
651 retval = -EACCES;
652 goto exec_error2;
653 }
654 i = bprm.inode->i_mode;
655 if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
656 euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) && !suser()) {
657 retval = -EPERM;
658 goto exec_error2;
659 }
660
661 if (current->flags & PF_PTRACED) {
662 bprm.e_uid = current->euid;
663 bprm.e_gid = current->egid;
664 } else {
665 bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
666 bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
667 }
668 if ((retval = permission(bprm.inode, MAY_EXEC)) != 0)
669 goto exec_error2;
670 if (!(bprm.inode->i_mode & 0111) && fsuser()) {
671 retval = -EACCES;
672 goto exec_error2;
673 }
674
675 if (bprm.inode->i_writecount > 0) {
676 retval = -ETXTBSY;
677 goto exec_error2;
678 }
679 memset(bprm.buf,0,sizeof(bprm.buf));
680 retval = read_exec(bprm.inode,0,bprm.buf,128,1);
681 if (retval < 0)
682 goto exec_error2;
683 if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
684
685
686
687
688
689 char *cp, *interp, *i_name, *i_arg;
690
691 iput(bprm.inode);
692 bprm.buf[127] = '\0';
693 if ((cp = strchr(bprm.buf, '\n')) == NULL)
694 cp = bprm.buf+127;
695 *cp = '\0';
696 while (cp > bprm.buf) {
697 cp--;
698 if ((*cp == ' ') || (*cp == '\t'))
699 *cp = '\0';
700 else
701 break;
702 }
703 for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
704 if (!cp || *cp == '\0') {
705 retval = -ENOEXEC;
706 goto exec_error1;
707 }
708 interp = i_name = cp;
709 i_arg = 0;
710 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
711 if (*cp == '/')
712 i_name = cp+1;
713 }
714 while ((*cp == ' ') || (*cp == '\t'))
715 *cp++ = '\0';
716 if (*cp)
717 i_arg = cp;
718
719
720
721
722 if (sh_bang++ == 0) {
723 bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
724 bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
725 }
726
727
728
729
730
731
732
733
734 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
735 bprm.argc++;
736 if (i_arg) {
737 bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
738 bprm.argc++;
739 }
740 bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
741 bprm.argc++;
742 if (!bprm.p) {
743 retval = -E2BIG;
744 goto exec_error1;
745 }
746
747
748
749
750
751 retval = open_namei(interp, 0, 0, &bprm.inode, NULL);
752 if (retval)
753 goto exec_error1;
754 goto restart_interp;
755 }
756 #ifdef __alpha__
757
758 {
759 struct exec * eh = (struct exec *) bprm.buf;
760
761 if (!loader && eh->fh.f_magic == 0x183 &&
762 (eh->fh.f_flags & 0x3000) == 0x3000)
763 {
764 char * dynloader[] = { "/sbin/loader" };
765 iput(bprm.inode);
766 loader = 1;
767 bprm.p = copy_strings(1, dynloader, bprm.page, bprm.p, 2);
768 bprm.loader = bprm.p;
769 retval = open_namei(dynloader[0], 0, 0, &bprm.inode, NULL);
770 if (retval)
771 goto exec_error1;
772 goto restart_interp;
773 }
774 }
775 #endif
776 if (!sh_bang) {
777 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
778 bprm.exec = bprm.p;
779 bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
780 bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
781 if (!bprm.p) {
782 retval = -E2BIG;
783 goto exec_error2;
784 }
785 }
786
787 bprm.sh_bang = sh_bang;
788 for (try=0; try<2; try++) {
789 for (fmt = formats ; fmt ; fmt = fmt->next) {
790 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
791 if (!fn)
792 break;
793 retval = fn(&bprm, regs);
794 if (retval >= 0) {
795 iput(bprm.inode);
796 current->did_exec = 1;
797 return retval;
798 }
799 if (retval != -ENOEXEC)
800 break;
801 }
802 if (retval != -ENOEXEC) {
803 break;
804 #ifdef CONFIG_KERNELD
805 }else{
806 char modname[20];
807 sprintf(modname, "binfmt-%hd", *(short*)(&bprm.buf));
808 request_module(modname);
809 #endif
810 }
811 }
812 exec_error2:
813 iput(bprm.inode);
814 exec_error1:
815 for (i=0 ; i<MAX_ARG_PAGES ; i++)
816 free_page(bprm.page[i]);
817 return(retval);
818 }
819
820 static void set_brk(unsigned long start, unsigned long end)
821 {
822 start = PAGE_ALIGN(start);
823 end = PAGE_ALIGN(end);
824 if (end <= start)
825 return;
826 do_mmap(NULL, start, end - start,
827 PROT_READ | PROT_WRITE | PROT_EXEC,
828 MAP_FIXED | MAP_PRIVATE, 0);
829 }
830
831
832
833
834
835
836 static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
837 {
838 struct exec ex;
839 struct file * file;
840 int fd;
841 unsigned long error;
842 unsigned long p = bprm->p;
843 unsigned long fd_offset;
844 unsigned long rlim;
845
846 ex = *((struct exec *) bprm->buf);
847 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
848 N_MAGIC(ex) != QMAGIC) ||
849 N_TRSIZE(ex) || N_DRSIZE(ex) ||
850 bprm->inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
851 return -ENOEXEC;
852 }
853
854 current->personality = PER_LINUX;
855 fd_offset = N_TXTOFF(ex);
856
857 #ifdef __i386__
858 if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
859 printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
860 return -ENOEXEC;
861 }
862
863 if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
864 (fd_offset < bprm->inode->i_sb->s_blocksize)) {
865 printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
866 return -ENOEXEC;
867 }
868 #endif
869
870
871
872
873
874 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
875 if (rlim >= RLIM_INFINITY)
876 rlim = ~0;
877 if (ex.a_data + ex.a_bss > rlim)
878 return -ENOMEM;
879
880
881 flush_old_exec(bprm);
882
883 current->mm->end_code = ex.a_text +
884 (current->mm->start_code = N_TXTADDR(ex));
885 current->mm->end_data = ex.a_data +
886 (current->mm->start_data = N_DATADDR(ex));
887 current->mm->brk = ex.a_bss +
888 (current->mm->start_brk = N_BSSADDR(ex));
889
890 current->mm->rss = 0;
891 current->mm->mmap = NULL;
892 current->suid = current->euid = current->fsuid = bprm->e_uid;
893 current->sgid = current->egid = current->fsgid = bprm->e_gid;
894 if (N_MAGIC(ex) == OMAGIC) {
895 #ifdef __alpha__
896 do_mmap(NULL, N_TXTADDR(ex) & PAGE_MASK,
897 ex.a_text+ex.a_data + PAGE_SIZE - 1,
898 PROT_READ|PROT_WRITE|PROT_EXEC,
899 MAP_FIXED|MAP_PRIVATE, 0);
900 read_exec(bprm->inode, fd_offset, (char *) N_TXTADDR(ex),
901 ex.a_text+ex.a_data, 0);
902 #else
903 do_mmap(NULL, 0, ex.a_text+ex.a_data,
904 PROT_READ|PROT_WRITE|PROT_EXEC,
905 MAP_FIXED|MAP_PRIVATE, 0);
906 read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data, 0);
907 #endif
908 } else {
909 if (ex.a_text & 0xfff || ex.a_data & 0xfff)
910 printk(KERN_NOTICE "executable not page aligned\n");
911
912 fd = open_inode(bprm->inode, O_RDONLY);
913
914 if (fd < 0)
915 return fd;
916 file = current->files->fd[fd];
917 if (!file->f_op || !file->f_op->mmap) {
918 sys_close(fd);
919 do_mmap(NULL, 0, ex.a_text+ex.a_data,
920 PROT_READ|PROT_WRITE|PROT_EXEC,
921 MAP_FIXED|MAP_PRIVATE, 0);
922 read_exec(bprm->inode, fd_offset,
923 (char *) N_TXTADDR(ex), ex.a_text+ex.a_data, 0);
924 goto beyond_if;
925 }
926
927 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
928 PROT_READ | PROT_EXEC,
929 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
930 fd_offset);
931
932 if (error != N_TXTADDR(ex)) {
933 sys_close(fd);
934 send_sig(SIGKILL, current, 0);
935 return error;
936 }
937
938 error = do_mmap(file, N_DATADDR(ex), ex.a_data,
939 PROT_READ | PROT_WRITE | PROT_EXEC,
940 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
941 fd_offset + ex.a_text);
942 sys_close(fd);
943 if (error != N_DATADDR(ex)) {
944 send_sig(SIGKILL, current, 0);
945 return error;
946 }
947 }
948 beyond_if:
949 if (current->exec_domain && current->exec_domain->use_count)
950 (*current->exec_domain->use_count)--;
951 if (current->binfmt && current->binfmt->use_count)
952 (*current->binfmt->use_count)--;
953 current->exec_domain = lookup_exec_domain(current->personality);
954 current->binfmt = &aout_format;
955 if (current->exec_domain && current->exec_domain->use_count)
956 (*current->exec_domain->use_count)++;
957 if (current->binfmt && current->binfmt->use_count)
958 (*current->binfmt->use_count)++;
959
960 set_brk(current->mm->start_brk, current->mm->brk);
961
962 fd_offset = setup_arg_pages(ex.a_text,bprm->page) - MAX_ARG_PAGES*PAGE_SIZE;
963 p += fd_offset;
964 if (bprm->loader)
965 bprm->loader += fd_offset;
966 bprm->exec += fd_offset;
967
968 p = (unsigned long)create_tables((char *)p, bprm,
969 current->personality != PER_LINUX);
970 current->mm->start_stack = p;
971 #ifdef __alpha__
972 regs->gp = ex.a_gpvalue;
973 #endif
974 start_thread(regs, ex.a_entry, p);
975 if (current->flags & PF_PTRACED)
976 send_sig(SIGTRAP, current, 0);
977 return 0;
978 }
979
980
981 static int load_aout_library(int fd)
982 {
983 struct file * file;
984 struct exec ex;
985 struct inode * inode;
986 unsigned int len;
987 unsigned int bss;
988 unsigned int start_addr;
989 unsigned long error;
990
991 file = current->files->fd[fd];
992 inode = file->f_inode;
993
994 if (!file || !file->f_op)
995 return -EACCES;
996
997
998 if (file->f_op->lseek) {
999 if ((error = file->f_op->lseek(inode, file, 0, 0)) != 0)
1000 return -ENOEXEC;
1001 } else
1002 file->f_pos = 0;
1003
1004 set_fs(KERNEL_DS);
1005 error = file->f_op->read(inode, file, (char *) &ex, sizeof(ex));
1006 set_fs(USER_DS);
1007 if (error != sizeof(ex))
1008 return -ENOEXEC;
1009
1010
1011 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
1012 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
1013 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
1014 return -ENOEXEC;
1015 }
1016 if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
1017 (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
1018 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
1019 return -ENOEXEC;
1020 }
1021
1022 if (N_FLAGS(ex)) return -ENOEXEC;
1023
1024
1025
1026
1027 start_addr = ex.a_entry & 0xfffff000;
1028
1029
1030 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
1031 PROT_READ | PROT_WRITE | PROT_EXEC,
1032 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1033 N_TXTOFF(ex));
1034 if (error != start_addr)
1035 return error;
1036 len = PAGE_ALIGN(ex.a_text + ex.a_data);
1037 bss = ex.a_text + ex.a_data + ex.a_bss;
1038 if (bss > len)
1039 do_mmap(NULL, start_addr + len, bss-len,
1040 PROT_READ|PROT_WRITE|PROT_EXEC,
1041 MAP_PRIVATE|MAP_FIXED, 0);
1042 return 0;
1043 }