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