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