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