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