ptrace.c 7.6 KB

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