ptrace.c 6.8 KB

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