ptrace.c 6.7 KB

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