ptrace.c 7.5 KB

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