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