[NET_SCHED]: cls_basic: fix NULL pointer dereference
[linux-2.6/verdex.git] / arch / i386 / kernel / ptrace.c
blob4a8f8a2597233d06316afa6463d9e1eabdd9592e
1 /* ptrace.c */
2 /* By Ross Biro 1/23/92 */
3 /*
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
6 */
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18 #include <linux/seccomp.h>
19 #include <linux/signal.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/processor.h>
25 #include <asm/i387.h>
26 #include <asm/debugreg.h>
27 #include <asm/ldt.h>
28 #include <asm/desc.h>
31 * does not yet catch signals sent when the child dies.
32 * in exit.c or in signal.c.
36 * Determines which flags the user has access to [1 = access, 0 = no access].
37 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
38 * Also masks reserved bits (31-22, 15, 5, 3, 1).
40 #define FLAG_MASK 0x00050dd5
42 /* set's the trap flag. */
43 #define TRAP_FLAG 0x100
46 * Offset of eflags on child stack..
48 #define EFL_OFFSET offsetof(struct pt_regs, eflags)
50 static inline struct pt_regs *get_child_regs(struct task_struct *task)
52 void *stack_top = (void *)task->thread.esp0;
53 return stack_top - sizeof(struct pt_regs);
57 * This routine will get a word off of the processes privileged stack.
58 * the offset is bytes into the pt_regs structure on the stack.
59 * This routine assumes that all the privileged stacks are in our
60 * data space.
61 */
62 static inline int get_stack_long(struct task_struct *task, int offset)
64 unsigned char *stack;
66 stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs);
67 stack += offset;
68 return (*((int *)stack));
72 * This routine will put a word on the processes privileged stack.
73 * the offset is bytes into the pt_regs structure on the stack.
74 * This routine assumes that all the privileged stacks are in our
75 * data space.
77 static inline int put_stack_long(struct task_struct *task, int offset,
78 unsigned long data)
80 unsigned char * stack;
82 stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs);
83 stack += offset;
84 *(unsigned long *) stack = data;
85 return 0;
88 static int putreg(struct task_struct *child,
89 unsigned long regno, unsigned long value)
91 switch (regno >> 2) {
92 case GS:
93 if (value && (value & 3) != 3)
94 return -EIO;
95 child->thread.gs = value;
96 return 0;
97 case DS:
98 case ES:
99 case FS:
100 if (value && (value & 3) != 3)
101 return -EIO;
102 value &= 0xffff;
103 break;
104 case SS:
105 case CS:
106 if ((value & 3) != 3)
107 return -EIO;
108 value &= 0xffff;
109 break;
110 case EFL:
111 value &= FLAG_MASK;
112 value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
113 break;
115 if (regno > FS*4)
116 regno -= 1*4;
117 put_stack_long(child, regno, value);
118 return 0;
121 static unsigned long getreg(struct task_struct *child,
122 unsigned long regno)
124 unsigned long retval = ~0UL;
126 switch (regno >> 2) {
127 case GS:
128 retval = child->thread.gs;
129 break;
130 case DS:
131 case ES:
132 case FS:
133 case SS:
134 case CS:
135 retval = 0xffff;
136 /* fall through */
137 default:
138 if (regno > FS*4)
139 regno -= 1*4;
140 retval &= get_stack_long(child, regno);
142 return retval;
145 #define LDT_SEGMENT 4
147 static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs)
149 unsigned long addr, seg;
151 addr = regs->eip;
152 seg = regs->xcs & 0xffff;
153 if (regs->eflags & VM_MASK) {
154 addr = (addr & 0xffff) + (seg << 4);
155 return addr;
159 * We'll assume that the code segments in the GDT
160 * are all zero-based. That is largely true: the
161 * TLS segments are used for data, and the PNPBIOS
162 * and APM bios ones we just ignore here.
164 if (seg & LDT_SEGMENT) {
165 u32 *desc;
166 unsigned long base;
168 down(&child->mm->context.sem);
169 desc = child->mm->context.ldt + (seg & ~7);
170 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
172 /* 16-bit code segment? */
173 if (!((desc[1] >> 22) & 1))
174 addr &= 0xffff;
175 addr += base;
176 up(&child->mm->context.sem);
178 return addr;
181 static inline int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
183 int i, copied;
184 unsigned char opcode[15];
185 unsigned long addr = convert_eip_to_linear(child, regs);
187 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
188 for (i = 0; i < copied; i++) {
189 switch (opcode[i]) {
190 /* popf and iret */
191 case 0x9d: case 0xcf:
192 return 1;
193 /* opcode and address size prefixes */
194 case 0x66: case 0x67:
195 continue;
196 /* irrelevant prefixes (segment overrides and repeats) */
197 case 0x26: case 0x2e:
198 case 0x36: case 0x3e:
199 case 0x64: case 0x65:
200 case 0xf0: case 0xf2: case 0xf3:
201 continue;
204 * pushf: NOTE! We should probably not let
205 * the user see the TF bit being set. But
206 * it's more pain than it's worth to avoid
207 * it, and a debugger could emulate this
208 * all in user space if it _really_ cares.
210 case 0x9c:
211 default:
212 return 0;
215 return 0;
218 static void set_singlestep(struct task_struct *child)
220 struct pt_regs *regs = get_child_regs(child);
223 * Always set TIF_SINGLESTEP - this guarantees that
224 * we single-step system calls etc.. This will also
225 * cause us to set TF when returning to user mode.
227 set_tsk_thread_flag(child, TIF_SINGLESTEP);
230 * If TF was already set, don't do anything else
232 if (regs->eflags & TRAP_FLAG)
233 return;
235 /* Set TF on the kernel stack.. */
236 regs->eflags |= TRAP_FLAG;
239 * ..but if TF is changed by the instruction we will trace,
240 * don't mark it as being "us" that set it, so that we
241 * won't clear it by hand later.
243 if (is_setting_trap_flag(child, regs))
244 return;
246 child->ptrace |= PT_DTRACE;
249 static void clear_singlestep(struct task_struct *child)
251 /* Always clear TIF_SINGLESTEP... */
252 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
254 /* But touch TF only if it was set by us.. */
255 if (child->ptrace & PT_DTRACE) {
256 struct pt_regs *regs = get_child_regs(child);
257 regs->eflags &= ~TRAP_FLAG;
258 child->ptrace &= ~PT_DTRACE;
263 * Called by kernel/ptrace.c when detaching..
265 * Make sure the single step bit is not set.
267 void ptrace_disable(struct task_struct *child)
269 clear_singlestep(child);
270 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
271 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
275 * Perform get_thread_area on behalf of the traced child.
277 static int
278 ptrace_get_thread_area(struct task_struct *child,
279 int idx, struct user_desc __user *user_desc)
281 struct user_desc info;
282 struct desc_struct *desc;
285 * Get the current Thread-Local Storage area:
288 #define GET_BASE(desc) ( \
289 (((desc)->a >> 16) & 0x0000ffff) | \
290 (((desc)->b << 16) & 0x00ff0000) | \
291 ( (desc)->b & 0xff000000) )
293 #define GET_LIMIT(desc) ( \
294 ((desc)->a & 0x0ffff) | \
295 ((desc)->b & 0xf0000) )
297 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
298 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
299 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
300 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
301 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
302 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
304 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
305 return -EINVAL;
307 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
309 info.entry_number = idx;
310 info.base_addr = GET_BASE(desc);
311 info.limit = GET_LIMIT(desc);
312 info.seg_32bit = GET_32BIT(desc);
313 info.contents = GET_CONTENTS(desc);
314 info.read_exec_only = !GET_WRITABLE(desc);
315 info.limit_in_pages = GET_LIMIT_PAGES(desc);
316 info.seg_not_present = !GET_PRESENT(desc);
317 info.useable = GET_USEABLE(desc);
319 if (copy_to_user(user_desc, &info, sizeof(info)))
320 return -EFAULT;
322 return 0;
326 * Perform set_thread_area on behalf of the traced child.
328 static int
329 ptrace_set_thread_area(struct task_struct *child,
330 int idx, struct user_desc __user *user_desc)
332 struct user_desc info;
333 struct desc_struct *desc;
335 if (copy_from_user(&info, user_desc, sizeof(info)))
336 return -EFAULT;
338 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
339 return -EINVAL;
341 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
342 if (LDT_empty(&info)) {
343 desc->a = 0;
344 desc->b = 0;
345 } else {
346 desc->a = LDT_entry_a(&info);
347 desc->b = LDT_entry_b(&info);
350 return 0;
353 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
355 struct user * dummy = NULL;
356 int i, ret;
357 unsigned long __user *datap = (unsigned long __user *)data;
359 switch (request) {
360 /* when I and D space are separate, these will need to be fixed. */
361 case PTRACE_PEEKTEXT: /* read word at location addr. */
362 case PTRACE_PEEKDATA: {
363 unsigned long tmp;
364 int copied;
366 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
367 ret = -EIO;
368 if (copied != sizeof(tmp))
369 break;
370 ret = put_user(tmp, datap);
371 break;
374 /* read the word at location addr in the USER area. */
375 case PTRACE_PEEKUSR: {
376 unsigned long tmp;
378 ret = -EIO;
379 if ((addr & 3) || addr < 0 ||
380 addr > sizeof(struct user) - 3)
381 break;
383 tmp = 0; /* Default return condition */
384 if(addr < FRAME_SIZE*sizeof(long))
385 tmp = getreg(child, addr);
386 if(addr >= (long) &dummy->u_debugreg[0] &&
387 addr <= (long) &dummy->u_debugreg[7]){
388 addr -= (long) &dummy->u_debugreg[0];
389 addr = addr >> 2;
390 tmp = child->thread.debugreg[addr];
392 ret = put_user(tmp, datap);
393 break;
396 /* when I and D space are separate, this will have to be fixed. */
397 case PTRACE_POKETEXT: /* write the word at location addr. */
398 case PTRACE_POKEDATA:
399 ret = 0;
400 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
401 break;
402 ret = -EIO;
403 break;
405 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
406 ret = -EIO;
407 if ((addr & 3) || addr < 0 ||
408 addr > sizeof(struct user) - 3)
409 break;
411 if (addr < FRAME_SIZE*sizeof(long)) {
412 ret = putreg(child, addr, data);
413 break;
415 /* We need to be very careful here. We implicitly
416 want to modify a portion of the task_struct, and we
417 have to be selective about what portions we allow someone
418 to modify. */
420 ret = -EIO;
421 if(addr >= (long) &dummy->u_debugreg[0] &&
422 addr <= (long) &dummy->u_debugreg[7]){
424 if(addr == (long) &dummy->u_debugreg[4]) break;
425 if(addr == (long) &dummy->u_debugreg[5]) break;
426 if(addr < (long) &dummy->u_debugreg[4] &&
427 ((unsigned long) data) >= TASK_SIZE-3) break;
429 /* Sanity-check data. Take one half-byte at once with
430 * check = (val >> (16 + 4*i)) & 0xf. It contains the
431 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
432 * 2 and 3 are LENi. Given a list of invalid values,
433 * we do mask |= 1 << invalid_value, so that
434 * (mask >> check) & 1 is a correct test for invalid
435 * values.
437 * R/Wi contains the type of the breakpoint /
438 * watchpoint, LENi contains the length of the watched
439 * data in the watchpoint case.
441 * The invalid values are:
442 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
443 * - R/Wi == 0x10 (break on I/O reads or writes), so
444 * mask |= 0x4444.
445 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
446 * 0x1110.
448 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
450 * See the Intel Manual "System Programming Guide",
451 * 15.2.4
453 * Note that LENi == 0x10 is defined on x86_64 in long
454 * mode (i.e. even for 32-bit userspace software, but
455 * 64-bit kernel), so the x86_64 mask value is 0x5454.
456 * See the AMD manual no. 24593 (AMD64 System
457 * Programming)*/
459 if(addr == (long) &dummy->u_debugreg[7]) {
460 data &= ~DR_CONTROL_RESERVED;
461 for(i=0; i<4; i++)
462 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
463 goto out_tsk;
464 if (data)
465 set_tsk_thread_flag(child, TIF_DEBUG);
466 else
467 clear_tsk_thread_flag(child, TIF_DEBUG);
469 addr -= (long) &dummy->u_debugreg;
470 addr = addr >> 2;
471 child->thread.debugreg[addr] = data;
472 ret = 0;
474 break;
476 case PTRACE_SYSEMU: /* continue and stop at next syscall, which will not be executed */
477 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
478 case PTRACE_CONT: /* restart after signal. */
479 ret = -EIO;
480 if (!valid_signal(data))
481 break;
482 if (request == PTRACE_SYSEMU) {
483 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
484 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
485 } else if (request == PTRACE_SYSCALL) {
486 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
487 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
488 } else {
489 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
490 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
492 child->exit_code = data;
493 /* make sure the single step bit is not set. */
494 clear_singlestep(child);
495 wake_up_process(child);
496 ret = 0;
497 break;
500 * make the child exit. Best I can do is send it a sigkill.
501 * perhaps it should be put in the status that it wants to
502 * exit.
504 case PTRACE_KILL:
505 ret = 0;
506 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
507 break;
508 child->exit_code = SIGKILL;
509 /* make sure the single step bit is not set. */
510 clear_singlestep(child);
511 wake_up_process(child);
512 break;
514 case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */
515 case PTRACE_SINGLESTEP: /* set the trap flag. */
516 ret = -EIO;
517 if (!valid_signal(data))
518 break;
520 if (request == PTRACE_SYSEMU_SINGLESTEP)
521 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
522 else
523 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
525 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
526 set_singlestep(child);
527 child->exit_code = data;
528 /* give it a chance to run. */
529 wake_up_process(child);
530 ret = 0;
531 break;
533 case PTRACE_DETACH:
534 /* detach a process that was attached. */
535 ret = ptrace_detach(child, data);
536 break;
538 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
539 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
540 ret = -EIO;
541 break;
543 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
544 __put_user(getreg(child, i), datap);
545 datap++;
547 ret = 0;
548 break;
551 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
552 unsigned long tmp;
553 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
554 ret = -EIO;
555 break;
557 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
558 __get_user(tmp, datap);
559 putreg(child, i, tmp);
560 datap++;
562 ret = 0;
563 break;
566 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
567 if (!access_ok(VERIFY_WRITE, datap,
568 sizeof(struct user_i387_struct))) {
569 ret = -EIO;
570 break;
572 ret = 0;
573 if (!tsk_used_math(child))
574 init_fpu(child);
575 get_fpregs((struct user_i387_struct __user *)data, child);
576 break;
579 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
580 if (!access_ok(VERIFY_READ, datap,
581 sizeof(struct user_i387_struct))) {
582 ret = -EIO;
583 break;
585 set_stopped_child_used_math(child);
586 set_fpregs(child, (struct user_i387_struct __user *)data);
587 ret = 0;
588 break;
591 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
592 if (!access_ok(VERIFY_WRITE, datap,
593 sizeof(struct user_fxsr_struct))) {
594 ret = -EIO;
595 break;
597 if (!tsk_used_math(child))
598 init_fpu(child);
599 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
600 break;
603 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
604 if (!access_ok(VERIFY_READ, datap,
605 sizeof(struct user_fxsr_struct))) {
606 ret = -EIO;
607 break;
609 set_stopped_child_used_math(child);
610 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
611 break;
614 case PTRACE_GET_THREAD_AREA:
615 ret = ptrace_get_thread_area(child, addr,
616 (struct user_desc __user *) data);
617 break;
619 case PTRACE_SET_THREAD_AREA:
620 ret = ptrace_set_thread_area(child, addr,
621 (struct user_desc __user *) data);
622 break;
624 default:
625 ret = ptrace_request(child, request, addr, data);
626 break;
628 out_tsk:
629 return ret;
632 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
634 struct siginfo info;
636 tsk->thread.trap_no = 1;
637 tsk->thread.error_code = error_code;
639 memset(&info, 0, sizeof(info));
640 info.si_signo = SIGTRAP;
641 info.si_code = TRAP_BRKPT;
643 /* User-mode eip? */
644 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL;
646 /* Send us the fakey SIGTRAP */
647 force_sig_info(SIGTRAP, &info, tsk);
650 /* notification of system call entry/exit
651 * - triggered by current->work.syscall_trace
653 __attribute__((regparm(3)))
654 int do_syscall_trace(struct pt_regs *regs, int entryexit)
656 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
658 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
659 * interception
661 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
662 int ret = 0;
664 /* do the secure computing check first */
665 if (!entryexit)
666 secure_computing(regs->orig_eax);
668 if (unlikely(current->audit_context)) {
669 if (entryexit)
670 audit_syscall_exit(AUDITSC_RESULT(regs->eax),
671 regs->eax);
672 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
673 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
674 * not used, entry.S will call us only on syscall exit, not
675 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
676 * calling send_sigtrap() on syscall entry.
678 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
679 * is_singlestep is false, despite his name, so we will still do
680 * the correct thing.
682 else if (is_singlestep)
683 goto out;
686 if (!(current->ptrace & PT_PTRACED))
687 goto out;
689 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
690 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
691 * here. We have to check this and return */
692 if (is_sysemu && entryexit)
693 return 0;
695 /* Fake a debug trap */
696 if (is_singlestep)
697 send_sigtrap(current, regs, 0);
699 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
700 goto out;
702 /* the 0x80 provides a way for the tracing parent to distinguish
703 between a syscall stop and SIGTRAP delivery */
704 /* Note that the debugger could change the result of test_thread_flag!*/
705 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
708 * this isn't the same as continuing with a signal, but it will do
709 * for normal use. strace only continues with a signal if the
710 * stopping signal is not SIGTRAP. -brl
712 if (current->exit_code) {
713 send_sig(current->exit_code, current, 1);
714 current->exit_code = 0;
716 ret = is_sysemu;
717 out:
718 if (unlikely(current->audit_context) && !entryexit)
719 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_eax,
720 regs->ebx, regs->ecx, regs->edx, regs->esi);
721 if (ret == 0)
722 return 0;
724 regs->orig_eax = -1; /* force skip of syscall restarting */
725 if (unlikely(current->audit_context))
726 audit_syscall_exit(AUDITSC_RESULT(regs->eax), regs->eax);
727 return 1;