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