ptrace.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 = -EIO;
  59. if (access_process_vm(child, addr, &data, sizeof(data),
  60. 1) != sizeof(data))
  61. break;
  62. ret = 0;
  63. break;
  64. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  65. ret = poke_user(child, addr, data);
  66. break;
  67. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  68. case PTRACE_CONT: { /* restart after signal. */
  69. ret = -EIO;
  70. if (!valid_signal(data))
  71. break;
  72. set_singlestepping(child, 0);
  73. if (request == PTRACE_SYSCALL) {
  74. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  75. }
  76. else {
  77. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  78. }
  79. child->exit_code = data;
  80. wake_up_process(child);
  81. ret = 0;
  82. break;
  83. }
  84. /*
  85. * make the child exit. Best I can do is send it a sigkill.
  86. * perhaps it should be put in the status that it wants to
  87. * exit.
  88. */
  89. case PTRACE_KILL: {
  90. ret = 0;
  91. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  92. break;
  93. set_singlestepping(child, 0);
  94. child->exit_code = SIGKILL;
  95. wake_up_process(child);
  96. break;
  97. }
  98. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  99. ret = -EIO;
  100. if (!valid_signal(data))
  101. break;
  102. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  103. set_singlestepping(child, 1);
  104. child->exit_code = data;
  105. /* give it a chance to run. */
  106. wake_up_process(child);
  107. ret = 0;
  108. break;
  109. }
  110. case PTRACE_DETACH:
  111. /* detach a process that was attached. */
  112. ret = ptrace_detach(child, data);
  113. break;
  114. #ifdef PTRACE_GETREGS
  115. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  116. if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
  117. ret = -EIO;
  118. break;
  119. }
  120. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  121. __put_user(getreg(child, i), p);
  122. p++;
  123. }
  124. ret = 0;
  125. break;
  126. }
  127. #endif
  128. #ifdef PTRACE_SETREGS
  129. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  130. unsigned long tmp = 0;
  131. if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
  132. ret = -EIO;
  133. break;
  134. }
  135. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  136. __get_user(tmp, p);
  137. putreg(child, i, tmp);
  138. p++;
  139. }
  140. ret = 0;
  141. break;
  142. }
  143. #endif
  144. #ifdef PTRACE_GETFPREGS
  145. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  146. ret = get_fpregs(data, child);
  147. break;
  148. #endif
  149. #ifdef PTRACE_SETFPREGS
  150. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  151. ret = set_fpregs(data, child);
  152. break;
  153. #endif
  154. #ifdef PTRACE_GETFPXREGS
  155. case PTRACE_GETFPXREGS: /* Get the child FPU state. */
  156. ret = get_fpxregs(data, child);
  157. break;
  158. #endif
  159. #ifdef PTRACE_SETFPXREGS
  160. case PTRACE_SETFPXREGS: /* Set the child FPU state. */
  161. ret = set_fpxregs(data, child);
  162. break;
  163. #endif
  164. case PTRACE_GET_THREAD_AREA:
  165. ret = ptrace_get_thread_area(child, addr,
  166. (struct user_desc __user *) data);
  167. break;
  168. case PTRACE_SET_THREAD_AREA:
  169. ret = ptrace_set_thread_area(child, addr,
  170. (struct user_desc __user *) data);
  171. break;
  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(p, &child->thread.arch.faultinfo,
  178. sizeof(struct ptrace_faultinfo));
  179. if(ret)
  180. break;
  181. break;
  182. }
  183. #ifdef PTRACE_LDT
  184. case PTRACE_LDT: {
  185. struct ptrace_ldt ldt;
  186. if(copy_from_user(&ldt, p, sizeof(ldt))){
  187. ret = -EIO;
  188. break;
  189. }
  190. /* This one is confusing, so just punt and return -EIO for
  191. * now
  192. */
  193. ret = -EIO;
  194. break;
  195. }
  196. #endif
  197. #ifdef CONFIG_PROC_MM
  198. case PTRACE_SWITCH_MM: {
  199. struct mm_struct *old = child->mm;
  200. struct mm_struct *new = proc_mm_get_mm(data);
  201. if(IS_ERR(new)){
  202. ret = PTR_ERR(new);
  203. break;
  204. }
  205. atomic_inc(&new->mm_users);
  206. child->mm = new;
  207. child->active_mm = new;
  208. mmput(old);
  209. ret = 0;
  210. break;
  211. }
  212. #endif
  213. #ifdef PTRACE_ARCH_PRCTL
  214. case PTRACE_ARCH_PRCTL:
  215. /* XXX Calls ptrace on the host - needs some SMP thinking */
  216. ret = arch_prctl_skas(child, data, (void *) addr);
  217. break;
  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. }