ptrace.c 7.7 KB

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