ptrace.c 7.5 KB

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