This source file includes following definitions.
- core_dump
- sys_uselib
- create_tables
- count
- copy_strings
- change_ldt
- read_exec
- do_execve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <linux/fs.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/a.out.h>
25 #include <linux/errno.h>
26 #include <linux/signal.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/fcntl.h>
30 #include <linux/ptrace.h>
31 #include <linux/user.h>
32
33 #include <asm/segment.h>
34
35 extern int sys_exit(int exit_code);
36 extern int sys_close(int fd);
37
38
39
40
41
42
43 #define MAX_ARG_PAGES 32
44
45
46
47
48
49 #define DUMP_WRITE(addr,nr) \
50 while (file.f_op->write(inode,&file,(char *)(addr),(nr)) != (nr)) goto close_coredump
51
52 #define DUMP_SEEK(offset) \
53 if (file.f_op->lseek) { \
54 if (file.f_op->lseek(inode,&file,(offset),0) != (offset)) \
55 goto close_coredump; \
56 } else file.f_pos = (offset)
57
58
59
60
61
62
63
64
65
66
67 int core_dump(long signr, struct pt_regs * regs)
68 {
69 struct inode * inode = NULL;
70 struct file file;
71 unsigned short fs;
72 int has_dumped = 0;
73 register int dump_start, dump_size;
74 struct user dump;
75
76 if (!current->dumpable)
77 return 0;
78 current->dumpable = 0;
79
80 if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)
81 return 0;
82 __asm__("mov %%fs,%0":"=r" (fs));
83 __asm__("mov %0,%%fs"::"r" ((unsigned short) 0x10));
84 if (open_namei("core",O_CREAT | O_WRONLY | O_TRUNC,0600,&inode,NULL)) {
85 inode = NULL;
86 goto end_coredump;
87 }
88 if (!S_ISREG(inode->i_mode))
89 goto end_coredump;
90 if (!inode->i_op || !inode->i_op->default_file_ops)
91 goto end_coredump;
92 file.f_mode = 3;
93 file.f_flags = 0;
94 file.f_count = 1;
95 file.f_inode = inode;
96 file.f_pos = 0;
97 file.f_reada = 0;
98 file.f_op = inode->i_op->default_file_ops;
99 if (file.f_op->open)
100 if (file.f_op->open(inode,&file))
101 goto end_coredump;
102 if (!file.f_op->write)
103 goto close_coredump;
104 has_dumped = 1;
105
106 dump.magic = CMAGIC;
107 dump.start_code = 0;
108 dump.start_stack = regs->esp & ~(PAGE_SIZE - 1);
109 dump.u_tsize = ((unsigned long) current->end_code) >> 12;
110 dump.u_dsize = ((unsigned long) (current->brk + (PAGE_SIZE-1))) >> 12;
111 dump.u_dsize -= dump.u_tsize;
112 dump.u_ssize = 0;
113 if (dump.start_stack < TASK_SIZE)
114 dump.u_ssize = ((unsigned long) (TASK_SIZE - dump.start_stack)) >> 12;
115
116
117 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
118 current->rlim[RLIMIT_CORE].rlim_cur)
119 dump.u_dsize = 0;
120
121 if ((dump.u_ssize+1) * PAGE_SIZE >
122 current->rlim[RLIMIT_CORE].rlim_cur)
123 dump.u_ssize = 0;
124 dump.u_comm = 0;
125 dump.u_ar0 = (struct pt_regs *)(((int)(&dump.regs)) -((int)(&dump)));
126 dump.signal = signr;
127 dump.regs = *regs;
128
129
130 if (hard_math) {
131 if ((dump.u_fpvalid = current->used_math) != 0) {
132 if (last_task_used_math == current)
133 __asm__("clts ; fnsave %0"::"m" (dump.i387));
134 else
135 memcpy(&dump.i387,¤t->tss.i387.hard,sizeof(dump.i387));
136 }
137 } else {
138
139
140 dump.u_fpvalid = 0;
141 }
142 __asm__("mov %0,%%fs"::"r" ((unsigned short) 0x10));
143
144 DUMP_WRITE(&dump,sizeof(dump));
145
146 DUMP_WRITE(current->comm,16);
147
148 DUMP_SEEK(PAGE_SIZE);
149
150 __asm__("mov %0,%%fs"::"r" ((unsigned short) 0x17));
151
152 if (dump.u_dsize != 0) {
153 dump_start = dump.u_tsize << 12;
154 dump_size = dump.u_dsize << 12;
155 DUMP_WRITE(dump_start,dump_size);
156 };
157
158 if (dump.u_ssize != 0) {
159 dump_start = dump.start_stack;
160 dump_size = dump.u_ssize << 12;
161 DUMP_WRITE(dump_start,dump_size);
162 };
163
164 __asm__("mov %0,%%fs"::"r" ((unsigned short) 0x10));
165 DUMP_WRITE(current,sizeof(*current));
166 close_coredump:
167 if (file.f_op->release)
168 file.f_op->release(inode,&file);
169 end_coredump:
170 __asm__("mov %0,%%fs"::"r" (fs));
171 iput(inode);
172 return has_dumped;
173 }
174
175
176
177
178
179
180
181 int sys_uselib(const char * library)
182 {
183 #define libnum (current->numlibraries)
184 struct inode * inode;
185 struct buffer_head * bh;
186 struct exec ex;
187 int error;
188
189 if (!library || get_limit(0x17) != TASK_SIZE)
190 return -EINVAL;
191 if ((libnum >= MAX_SHARED_LIBS) || (libnum < 0))
192 return -EINVAL;
193 error = namei(library,&inode);
194 if (error)
195 return error;
196 if (!inode->i_sb || !S_ISREG(inode->i_mode) || !permission(inode,MAY_READ)) {
197 iput(inode);
198 return -EACCES;
199 }
200 if (!inode->i_op || !inode->i_op->bmap) {
201 iput(inode);
202 return -ENOEXEC;
203 }
204 if (!(bh = bread(inode->i_dev,bmap(inode,0),inode->i_sb->s_blocksize))) {
205 iput(inode);
206 return -EACCES;
207 }
208 if (!IS_RDONLY(inode)) {
209 inode->i_atime = CURRENT_TIME;
210 inode->i_dirt = 1;
211 }
212 ex = *(struct exec *) bh->b_data;
213 brelse(bh);
214 if (N_MAGIC(ex) != ZMAGIC || ex.a_trsize || ex.a_drsize ||
215 ex.a_text+ex.a_data+ex.a_bss>0x3000000 ||
216 inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
217 iput(inode);
218 return -ENOEXEC;
219 }
220 current->libraries[libnum].library = inode;
221 current->libraries[libnum].start = ex.a_entry;
222 current->libraries[libnum].length = (ex.a_data+ex.a_text+0xfff) & 0xfffff000;
223 current->libraries[libnum].bss = (ex.a_bss+0xfff) & 0xfffff000;
224 #if 0
225 printk("Loaded library %d at %08x, length %08x\n",
226 libnum,
227 current->libraries[libnum].start,
228 current->libraries[libnum].length);
229 #endif
230 libnum++;
231 return 0;
232 #undef libnum
233 }
234
235
236
237
238
239
240 static unsigned long * create_tables(char * p,int argc,int envc)
241 {
242 unsigned long *argv,*envp;
243 unsigned long * sp;
244
245 sp = (unsigned long *) (0xfffffffc & (unsigned long) p);
246 sp -= envc+1;
247 envp = sp;
248 sp -= argc+1;
249 argv = sp;
250 put_fs_long((unsigned long)envp,--sp);
251 put_fs_long((unsigned long)argv,--sp);
252 put_fs_long((unsigned long)argc,--sp);
253 current->arg_start = (unsigned long) p;
254 while (argc-->0) {
255 put_fs_long((unsigned long) p,argv++);
256 while (get_fs_byte(p++)) ;
257 }
258 put_fs_long(0,argv);
259 current->arg_end = current->env_start = (unsigned long) p;
260 while (envc-->0) {
261 put_fs_long((unsigned long) p,envp++);
262 while (get_fs_byte(p++)) ;
263 }
264 put_fs_long(0,envp);
265 current->env_end = (unsigned long) p;
266 return sp;
267 }
268
269
270
271
272 static int count(char ** argv)
273 {
274 int i=0;
275 char ** tmp;
276
277 if ((tmp = argv) != 0)
278 while (get_fs_long((unsigned long *) (tmp++)))
279 i++;
280
281 return i;
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 static unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
302 unsigned long p, int from_kmem)
303 {
304 char *tmp, *pag = NULL;
305 int len, offset = 0;
306 unsigned long old_fs, new_fs;
307
308 if (!p)
309 return 0;
310 new_fs = get_ds();
311 old_fs = get_fs();
312 if (from_kmem==2)
313 set_fs(new_fs);
314 while (argc-- > 0) {
315 if (from_kmem == 1)
316 set_fs(new_fs);
317 if (!(tmp = (char *)get_fs_long(((unsigned long *)argv)+argc)))
318 panic("argc is wrong");
319 if (from_kmem == 1)
320 set_fs(old_fs);
321 len=0;
322 do {
323 len++;
324 } while (get_fs_byte(tmp++));
325 if (p < len) {
326 set_fs(old_fs);
327 return 0;
328 }
329 while (len) {
330 --p; --tmp; --len;
331 if (--offset < 0) {
332 offset = p % PAGE_SIZE;
333 if (from_kmem==2)
334 set_fs(old_fs);
335 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
336 !(pag = (char *) page[p/PAGE_SIZE] =
337 (unsigned long *) get_free_page(GFP_USER)))
338 return 0;
339 if (from_kmem==2)
340 set_fs(new_fs);
341
342 }
343 *(pag + offset) = get_fs_byte(tmp);
344 }
345 }
346 if (from_kmem==2)
347 set_fs(old_fs);
348 return p;
349 }
350
351 static unsigned long change_ldt(unsigned long text_size,unsigned long * page)
352 {
353 unsigned long code_limit,data_limit,code_base,data_base;
354 int i;
355
356 code_limit = TASK_SIZE;
357 data_limit = TASK_SIZE;
358 code_base = data_base = 0;
359 current->start_code = code_base;
360 set_base(current->ldt[1],code_base);
361 set_limit(current->ldt[1],code_limit);
362 set_base(current->ldt[2],data_base);
363 set_limit(current->ldt[2],data_limit);
364
365 __asm__("pushl $0x17\n\tpop %%fs"::);
366 data_base += data_limit;
367 for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
368 data_base -= PAGE_SIZE;
369 if (page[i])
370 put_dirty_page(current,page[i],data_base);
371 }
372 return data_limit;
373 }
374
375
376
377
378
379
380 static int read_exec(struct inode *inode, unsigned long offset,
381 char * addr, unsigned long count)
382 {
383 struct file file;
384 int result = -ENOEXEC;
385
386 if (!inode->i_op || !inode->i_op->default_file_ops)
387 goto end_readexec;
388 file.f_mode = 1;
389 file.f_flags = 0;
390 file.f_count = 1;
391 file.f_inode = inode;
392 file.f_pos = 0;
393 file.f_reada = 0;
394 file.f_op = inode->i_op->default_file_ops;
395 if (file.f_op->open)
396 if (file.f_op->open(inode,&file))
397 goto end_readexec;
398 if (!file.f_op || !file.f_op->read)
399 goto close_readexec;
400 if (file.f_op->lseek) {
401 if (file.f_op->lseek(inode,&file,offset,0) != offset)
402 goto close_readexec;
403 } else
404 file.f_pos = offset;
405 result = file.f_op->read(inode, &file, addr, count);
406 close_readexec:
407 if (file.f_op->release)
408 file.f_op->release(inode,&file);
409 end_readexec:
410 return result;
411 }
412
413
414
415
416
417
418
419 int do_execve(unsigned long * eip,long tmp,char * filename,
420 char ** argv, char ** envp)
421 {
422 struct inode * inode;
423 char buf[128];
424 unsigned long old_fs;
425 struct exec ex;
426 unsigned long page[MAX_ARG_PAGES];
427 int i,argc,envc;
428 int e_uid, e_gid;
429 int retval;
430 int sh_bang = 0;
431 unsigned long p=PAGE_SIZE*MAX_ARG_PAGES-4;
432 int ch;
433
434 if ((0xffff & eip[1]) != 0x000f)
435 panic("execve called from supervisor mode");
436 for (i=0 ; i<MAX_ARG_PAGES ; i++)
437 page[i]=0;
438 retval = namei(filename,&inode);
439 if (retval)
440 return retval;
441 argc = count(argv);
442 envc = count(envp);
443
444 restart_interp:
445 if (!S_ISREG(inode->i_mode)) {
446 retval = -EACCES;
447 goto exec_error2;
448 }
449 if (IS_NOEXEC(inode)) {
450 retval = -EPERM;
451 goto exec_error2;
452 }
453 if (!inode->i_sb) {
454 retval = -EACCES;
455 goto exec_error2;
456 }
457 i = inode->i_mode;
458 if (IS_NOSUID(inode) && (((i & S_ISUID) && inode->i_uid != current->
459 euid) || ((i & S_ISGID) && inode->i_gid != current->egid)) &&
460 !suser()) {
461 retval = -EPERM;
462 goto exec_error2;
463 }
464
465 if (current->flags & PF_PTRACED) {
466 e_uid = current->euid;
467 e_gid = current->egid;
468 } else {
469 e_uid = (i & S_ISUID) ? inode->i_uid : current->euid;
470 e_gid = (i & S_ISGID) ? inode->i_gid : current->egid;
471 }
472 if (current->euid == inode->i_uid)
473 i >>= 6;
474 else if (in_group_p(inode->i_gid))
475 i >>= 3;
476 if (!(i & 1) &&
477 !((inode->i_mode & 0111) && suser())) {
478 retval = -EACCES;
479 goto exec_error2;
480 }
481 memset(buf,0,sizeof(buf));
482 old_fs = get_fs();
483 set_fs(get_ds());
484 retval = read_exec(inode,0,buf,128);
485 set_fs(old_fs);
486 if (retval < 0)
487 goto exec_error2;
488 ex = *((struct exec *) buf);
489 if ((buf[0] == '#') && (buf[1] == '!') && (!sh_bang)) {
490
491
492
493
494
495 char *cp, *interp, *i_name, *i_arg;
496
497 iput(inode);
498 buf[127] = '\0';
499 if ((cp = strchr(buf, '\n')) == NULL)
500 cp = buf+127;
501 *cp = '\0';
502 while (cp > buf) {
503 cp--;
504 if ((*cp == ' ') || (*cp == '\t'))
505 *cp = '\0';
506 else
507 break;
508 }
509 for (cp = buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
510 if (!cp || *cp == '\0') {
511 retval = -ENOEXEC;
512 goto exec_error1;
513 }
514 interp = i_name = cp;
515 i_arg = 0;
516 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
517 if (*cp == '/')
518 i_name = cp+1;
519 }
520 while ((*cp == ' ') || (*cp == '\t'))
521 *cp++ = '\0';
522 if (*cp)
523 i_arg = cp;
524
525
526
527
528 if (sh_bang++ == 0) {
529 p = copy_strings(envc, envp, page, p, 0);
530 p = copy_strings(--argc, argv+1, page, p, 0);
531 }
532
533
534
535
536
537
538
539
540 p = copy_strings(1, &filename, page, p, 1);
541 argc++;
542 if (i_arg) {
543 p = copy_strings(1, &i_arg, page, p, 2);
544 argc++;
545 }
546 p = copy_strings(1, &i_name, page, p, 2);
547 argc++;
548 if (!p) {
549 retval = -ENOMEM;
550 goto exec_error1;
551 }
552
553
554
555 old_fs = get_fs();
556 set_fs(get_ds());
557 retval = namei(interp,&inode);
558 set_fs(old_fs);
559 if (retval)
560 goto exec_error1;
561 goto restart_interp;
562 }
563 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC) ||
564 ex.a_trsize || ex.a_drsize ||
565 ex.a_text+ex.a_data+ex.a_bss>0x3000000 ||
566 inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
567 retval = -ENOEXEC;
568 goto exec_error2;
569 }
570 if (N_TXTOFF(ex) != BLOCK_SIZE && N_MAGIC(ex) != OMAGIC) {
571 printk("%s: N_TXTOFF != BLOCK_SIZE. See a.out.h.", filename);
572 retval = -ENOEXEC;
573 goto exec_error2;
574 }
575 if (!sh_bang) {
576 p = copy_strings(envc,envp,page,p,0);
577 p = copy_strings(argc,argv,page,p,0);
578 if (!p) {
579 retval = -ENOMEM;
580 goto exec_error2;
581 }
582 }
583
584 current->dumpable = 1;
585 for (i=0; (ch = get_fs_byte(filename++)) != '\0';)
586 if (ch == '/')
587 i = 0;
588 else
589 if (i < 15)
590 current->comm[i++] = ch;
591 current->comm[i] = '\0';
592 if (current->executable) {
593 iput(current->executable);
594 current->executable = NULL;
595 }
596 i = current->numlibraries;
597 while (i-- > 0) {
598 iput(current->libraries[i].library);
599 current->libraries[i].library = NULL;
600 }
601 if (e_uid != current->euid || e_gid != current->egid ||
602 !permission(inode,MAY_READ))
603 current->dumpable = 0;
604 current->numlibraries = 0;
605 current->signal = 0;
606 for (i=0 ; i<32 ; i++) {
607 current->sigaction[i].sa_mask = 0;
608 current->sigaction[i].sa_flags = 0;
609 if (current->sigaction[i].sa_handler != SIG_IGN)
610 current->sigaction[i].sa_handler = NULL;
611 }
612 for (i=0 ; i<NR_OPEN ; i++)
613 if (FD_ISSET(i,¤t->close_on_exec))
614 sys_close(i);
615 FD_ZERO(¤t->close_on_exec);
616 clear_page_tables(current);
617 if (last_task_used_math == current)
618 last_task_used_math = NULL;
619 current->used_math = 0;
620 p += change_ldt(ex.a_text,page);
621 p -= MAX_ARG_PAGES*PAGE_SIZE;
622 p = (unsigned long) create_tables((char *)p,argc,envc);
623 current->brk = ex.a_bss +
624 (current->end_data = ex.a_data +
625 (current->end_code = ex.a_text));
626 current->start_stack = p;
627 current->rss = (TASK_SIZE - p + PAGE_SIZE-1) / PAGE_SIZE;
628 current->suid = current->euid = e_uid;
629 current->sgid = current->egid = e_gid;
630 if (N_MAGIC(ex) == OMAGIC) {
631 read_exec(inode, 32, (char *) 0, ex.a_text+ex.a_data);
632 iput(inode);
633 } else if (!inode->i_op || !inode->i_op->bmap) {
634 read_exec(inode, 1024, (char *) 0, ex.a_text+ex.a_data);
635 iput(inode);
636 } else
637 current->executable = inode;
638 eip[0] = ex.a_entry;
639 eip[3] = p;
640 if (current->flags & PF_PTRACED)
641 send_sig(SIGTRAP, current, 0);
642 return 0;
643 exec_error2:
644 iput(inode);
645 exec_error1:
646 for (i=0 ; i<MAX_ARG_PAGES ; i++)
647 free_page(page[i]);
648 return(retval);
649 }