ptrace.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/audit.h"
  6. #include "linux/ptrace.h"
  7. #include "linux/sched.h"
  8. #include "asm/uaccess.h"
  9. #ifdef CONFIG_PROC_MM
  10. #include "proc_mm.h"
  11. #endif
  12. #include "skas_ptrace.h"
  13. void user_enable_single_step(struct task_struct *child)
  14. {
  15. child->ptrace |= PT_DTRACE;
  16. child->thread.singlestep_syscall = 0;
  17. #ifdef SUBARCH_SET_SINGLESTEPPING
  18. SUBARCH_SET_SINGLESTEPPING(child, 1);
  19. #endif
  20. }
  21. void user_disable_single_step(struct task_struct *child)
  22. {
  23. child->ptrace &= ~PT_DTRACE;
  24. child->thread.singlestep_syscall = 0;
  25. #ifdef SUBARCH_SET_SINGLESTEPPING
  26. SUBARCH_SET_SINGLESTEPPING(child, 0);
  27. #endif
  28. }
  29. /*
  30. * Called by kernel/ptrace.c when detaching..
  31. */
  32. void ptrace_disable(struct task_struct *child)
  33. {
  34. user_disable_single_step(child);
  35. }
  36. extern int peek_user(struct task_struct * child, long addr, long data);
  37. extern int poke_user(struct task_struct * child, long addr, long data);
  38. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  39. {
  40. int i, ret;
  41. unsigned long __user *p = (void __user *)(unsigned long)data;
  42. switch (request) {
  43. /* read word at location addr. */
  44. case PTRACE_PEEKTEXT:
  45. case PTRACE_PEEKDATA:
  46. ret = generic_ptrace_peekdata(child, addr, data);
  47. break;
  48. /* read the word at location addr in the USER area. */
  49. case PTRACE_PEEKUSR:
  50. ret = peek_user(child, addr, data);
  51. break;
  52. /* write the word at location addr. */
  53. case PTRACE_POKETEXT:
  54. case PTRACE_POKEDATA:
  55. ret = generic_ptrace_pokedata(child, addr, data);
  56. break;
  57. /* write the word at location addr in the USER area */
  58. case PTRACE_POKEUSR:
  59. ret = poke_user(child, addr, data);
  60. break;
  61. case PTRACE_SYSEMU:
  62. case PTRACE_SYSEMU_SINGLESTEP:
  63. ret = -EIO;
  64. break;
  65. #ifdef PTRACE_GETREGS
  66. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  67. if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
  68. ret = -EIO;
  69. break;
  70. }
  71. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  72. __put_user(getreg(child, i), p);
  73. p++;
  74. }
  75. ret = 0;
  76. break;
  77. }
  78. #endif
  79. #ifdef PTRACE_SETREGS
  80. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  81. unsigned long tmp = 0;
  82. if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
  83. ret = -EIO;
  84. break;
  85. }
  86. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  87. __get_user(tmp, p);
  88. putreg(child, i, tmp);
  89. p++;
  90. }
  91. ret = 0;
  92. break;
  93. }
  94. #endif
  95. #ifdef PTRACE_GETFPREGS
  96. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  97. ret = get_fpregs((struct user_i387_struct __user *) data,
  98. child);
  99. break;
  100. #endif
  101. #ifdef PTRACE_SETFPREGS
  102. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  103. ret = set_fpregs((struct user_i387_struct __user *) data,
  104. child);
  105. break;
  106. #endif
  107. case PTRACE_GET_THREAD_AREA:
  108. ret = ptrace_get_thread_area(child, addr,
  109. (struct user_desc __user *) data);
  110. break;
  111. case PTRACE_SET_THREAD_AREA:
  112. ret = ptrace_set_thread_area(child, addr,
  113. (struct user_desc __user *) data);
  114. break;
  115. case PTRACE_FAULTINFO: {
  116. /*
  117. * Take the info from thread->arch->faultinfo,
  118. * but transfer max. sizeof(struct ptrace_faultinfo).
  119. * On i386, ptrace_faultinfo is smaller!
  120. */
  121. ret = copy_to_user(p, &child->thread.arch.faultinfo,
  122. sizeof(struct ptrace_faultinfo));
  123. break;
  124. }
  125. #ifdef PTRACE_LDT
  126. case PTRACE_LDT: {
  127. struct ptrace_ldt ldt;
  128. if (copy_from_user(&ldt, p, sizeof(ldt))) {
  129. ret = -EIO;
  130. break;
  131. }
  132. /*
  133. * This one is confusing, so just punt and return -EIO for
  134. * now
  135. */
  136. ret = -EIO;
  137. break;
  138. }
  139. #endif
  140. #ifdef CONFIG_PROC_MM
  141. case PTRACE_SWITCH_MM: {
  142. struct mm_struct *old = child->mm;
  143. struct mm_struct *new = proc_mm_get_mm(data);
  144. if (IS_ERR(new)) {
  145. ret = PTR_ERR(new);
  146. break;
  147. }
  148. atomic_inc(&new->mm_users);
  149. child->mm = new;
  150. child->active_mm = new;
  151. mmput(old);
  152. ret = 0;
  153. break;
  154. }
  155. #endif
  156. #ifdef PTRACE_ARCH_PRCTL
  157. case PTRACE_ARCH_PRCTL:
  158. /* XXX Calls ptrace on the host - needs some SMP thinking */
  159. ret = arch_prctl(child, data, (void *) addr);
  160. break;
  161. #endif
  162. default:
  163. ret = ptrace_request(child, request, addr, data);
  164. if (ret == -EIO)
  165. ret = subarch_ptrace(child, request, addr, data);
  166. break;
  167. }
  168. return ret;
  169. }
  170. static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
  171. int error_code)
  172. {
  173. struct siginfo info;
  174. memset(&info, 0, sizeof(info));
  175. info.si_signo = SIGTRAP;
  176. info.si_code = TRAP_BRKPT;
  177. /* User-mode eip? */
  178. info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
  179. /* Send us the fake SIGTRAP */
  180. force_sig_info(SIGTRAP, &info, tsk);
  181. }
  182. /*
  183. * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
  184. * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
  185. */
  186. void syscall_trace(struct uml_pt_regs *regs, int entryexit)
  187. {
  188. int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
  189. int tracesysgood;
  190. if (unlikely(current->audit_context)) {
  191. if (!entryexit)
  192. audit_syscall_entry(HOST_AUDIT_ARCH,
  193. UPT_SYSCALL_NR(regs),
  194. UPT_SYSCALL_ARG1(regs),
  195. UPT_SYSCALL_ARG2(regs),
  196. UPT_SYSCALL_ARG3(regs),
  197. UPT_SYSCALL_ARG4(regs));
  198. else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
  199. UPT_SYSCALL_RET(regs));
  200. }
  201. /* Fake a debug trap */
  202. if (is_singlestep)
  203. send_sigtrap(current, regs, 0);
  204. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  205. return;
  206. if (!(current->ptrace & PT_PTRACED))
  207. return;
  208. /*
  209. * the 0x80 provides a way for the tracing parent to distinguish
  210. * between a syscall stop and SIGTRAP delivery
  211. */
  212. tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
  213. ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
  214. if (entryexit) /* force do_signal() --> is_syscall() */
  215. set_thread_flag(TIF_SIGPENDING);
  216. /*
  217. * this isn't the same as continuing with a signal, but it will do
  218. * for normal use. strace only continues with a signal if the
  219. * stopping signal is not SIGTRAP. -brl
  220. */
  221. if (current->exit_code) {
  222. send_sig(current->exit_code, current, 1);
  223. current->exit_code = 0;
  224. }
  225. }