ptrace.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2000-2003, Axis Communications AB.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/sched.h>
  6. #include <linux/mm.h>
  7. #include <linux/smp.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/errno.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/user.h>
  12. #include <linux/signal.h>
  13. #include <linux/security.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/page.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/system.h>
  18. #include <asm/processor.h>
  19. /*
  20. * Determines which bits in DCCR the user has access to.
  21. * 1 = access, 0 = no access.
  22. */
  23. #define DCCR_MASK 0x0000001f /* XNZVC */
  24. /*
  25. * Get contents of register REGNO in task TASK.
  26. */
  27. inline long get_reg(struct task_struct *task, unsigned int regno)
  28. {
  29. /* USP is a special case, it's not in the pt_regs struct but
  30. * in the tasks thread struct
  31. */
  32. if (regno == PT_USP)
  33. return task->thread.usp;
  34. else if (regno < PT_MAX)
  35. return ((unsigned long *)user_regs(task->thread_info))[regno];
  36. else
  37. return 0;
  38. }
  39. /*
  40. * Write contents of register REGNO in task TASK.
  41. */
  42. inline int put_reg(struct task_struct *task, unsigned int regno,
  43. unsigned long data)
  44. {
  45. if (regno == PT_USP)
  46. task->thread.usp = data;
  47. else if (regno < PT_MAX)
  48. ((unsigned long *)user_regs(task->thread_info))[regno] = data;
  49. else
  50. return -1;
  51. return 0;
  52. }
  53. /*
  54. * Called by kernel/ptrace.c when detaching.
  55. *
  56. * Make sure the single step bit is not set.
  57. */
  58. void
  59. ptrace_disable(struct task_struct *child)
  60. {
  61. /* Todo - pending singlesteps? */
  62. }
  63. /*
  64. * Note that this implementation of ptrace behaves differently from vanilla
  65. * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
  66. * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
  67. * ignored. Instead, the data variable is expected to point at a location
  68. * (in user space) where the result of the ptrace call is written (instead of
  69. * being returned).
  70. */
  71. asmlinkage int
  72. sys_ptrace(long request, long pid, long addr, long data)
  73. {
  74. struct task_struct *child;
  75. int ret;
  76. unsigned long __user *datap = (unsigned long __user *)data;
  77. lock_kernel();
  78. ret = -EPERM;
  79. if (request == PTRACE_TRACEME) {
  80. /* are we already being traced? */
  81. if (current->ptrace & PT_PTRACED)
  82. goto out;
  83. ret = security_ptrace(current->parent, current);
  84. if (ret)
  85. goto out;
  86. /* set the ptrace bit in the process flags. */
  87. current->ptrace |= PT_PTRACED;
  88. ret = 0;
  89. goto out;
  90. }
  91. ret = -ESRCH;
  92. read_lock(&tasklist_lock);
  93. child = find_task_by_pid(pid);
  94. if (child)
  95. get_task_struct(child);
  96. read_unlock(&tasklist_lock);
  97. if (!child)
  98. goto out;
  99. ret = -EPERM;
  100. if (pid == 1) /* Leave the init process alone! */
  101. goto out_tsk;
  102. if (request == PTRACE_ATTACH) {
  103. ret = ptrace_attach(child);
  104. goto out_tsk;
  105. }
  106. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  107. if (ret < 0)
  108. goto out_tsk;
  109. switch (request) {
  110. /* Read word at location address. */
  111. case PTRACE_PEEKTEXT:
  112. case PTRACE_PEEKDATA: {
  113. unsigned long tmp;
  114. int copied;
  115. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  116. ret = -EIO;
  117. if (copied != sizeof(tmp))
  118. break;
  119. ret = put_user(tmp,datap);
  120. break;
  121. }
  122. /* Read the word at location address in the USER area. */
  123. case PTRACE_PEEKUSR: {
  124. unsigned long tmp;
  125. ret = -EIO;
  126. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  127. break;
  128. tmp = get_reg(child, addr >> 2);
  129. ret = put_user(tmp, datap);
  130. break;
  131. }
  132. /* Write the word at location address. */
  133. case PTRACE_POKETEXT:
  134. case PTRACE_POKEDATA:
  135. ret = 0;
  136. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  137. break;
  138. ret = -EIO;
  139. break;
  140. /* Write the word at location address in the USER area. */
  141. case PTRACE_POKEUSR:
  142. ret = -EIO;
  143. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  144. break;
  145. addr >>= 2;
  146. if (addr == PT_DCCR) {
  147. /* don't allow the tracing process to change stuff like
  148. * interrupt enable, kernel/user bit, dma enables etc.
  149. */
  150. data &= DCCR_MASK;
  151. data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  152. }
  153. if (put_reg(child, addr, data))
  154. break;
  155. ret = 0;
  156. break;
  157. case PTRACE_SYSCALL:
  158. case PTRACE_CONT:
  159. ret = -EIO;
  160. if (!valid_signal(data))
  161. break;
  162. if (request == PTRACE_SYSCALL) {
  163. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  164. }
  165. else {
  166. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  167. }
  168. child->exit_code = data;
  169. /* TODO: make sure any pending breakpoint is killed */
  170. wake_up_process(child);
  171. ret = 0;
  172. break;
  173. /* Make the child exit by sending it a sigkill. */
  174. case PTRACE_KILL:
  175. ret = 0;
  176. if (child->exit_state == EXIT_ZOMBIE)
  177. break;
  178. child->exit_code = SIGKILL;
  179. /* TODO: make sure any pending breakpoint is killed */
  180. wake_up_process(child);
  181. break;
  182. /* Set the trap flag. */
  183. case PTRACE_SINGLESTEP:
  184. ret = -EIO;
  185. if (!valid_signal(data))
  186. break;
  187. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  188. /* TODO: set some clever breakpoint mechanism... */
  189. child->exit_code = data;
  190. wake_up_process(child);
  191. ret = 0;
  192. break;
  193. case PTRACE_DETACH:
  194. ret = ptrace_detach(child, data);
  195. break;
  196. /* Get all GP registers from the child. */
  197. case PTRACE_GETREGS: {
  198. int i;
  199. unsigned long tmp;
  200. for (i = 0; i <= PT_MAX; i++) {
  201. tmp = get_reg(child, i);
  202. if (put_user(tmp, datap)) {
  203. ret = -EFAULT;
  204. goto out_tsk;
  205. }
  206. data += sizeof(long);
  207. }
  208. ret = 0;
  209. break;
  210. }
  211. /* Set all GP registers in the child. */
  212. case PTRACE_SETREGS: {
  213. int i;
  214. unsigned long tmp;
  215. for (i = 0; i <= PT_MAX; i++) {
  216. if (get_user(tmp, datap)) {
  217. ret = -EFAULT;
  218. goto out_tsk;
  219. }
  220. if (i == PT_DCCR) {
  221. tmp &= DCCR_MASK;
  222. tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  223. }
  224. put_reg(child, i, tmp);
  225. data += sizeof(long);
  226. }
  227. ret = 0;
  228. break;
  229. }
  230. default:
  231. ret = ptrace_request(child, request, addr, data);
  232. break;
  233. }
  234. out_tsk:
  235. put_task_struct(child);
  236. out:
  237. unlock_kernel();
  238. return ret;
  239. }
  240. void do_syscall_trace(void)
  241. {
  242. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  243. return;
  244. if (!(current->ptrace & PT_PTRACED))
  245. return;
  246. /* the 0x80 provides a way for the tracing parent to distinguish
  247. between a syscall stop and SIGTRAP delivery */
  248. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  249. ? 0x80 : 0));
  250. /*
  251. * This isn't the same as continuing with a signal, but it will do for
  252. * normal use.
  253. */
  254. if (current->exit_code) {
  255. send_sig(current->exit_code, current, 1);
  256. current->exit_code = 0;
  257. }
  258. }