ptrace.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * linux/kernel/ptrace.c
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. *
  6. * Common interfaces for "ptrace()" which we do not want
  7. * to continually duplicate across every architecture.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/errno.h>
  12. #include <linux/mm.h>
  13. #include <linux/highmem.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/security.h>
  18. #include <linux/signal.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/uaccess.h>
  21. /*
  22. * ptrace a task: make the debugger its new parent and
  23. * move it to the ptrace list.
  24. *
  25. * Must be called with the tasklist lock write-held.
  26. */
  27. void __ptrace_link(task_t *child, task_t *new_parent)
  28. {
  29. if (!list_empty(&child->ptrace_list))
  30. BUG();
  31. if (child->parent == new_parent)
  32. return;
  33. list_add(&child->ptrace_list, &child->parent->ptrace_children);
  34. REMOVE_LINKS(child);
  35. child->parent = new_parent;
  36. SET_LINKS(child);
  37. }
  38. /*
  39. * Turn a tracing stop into a normal stop now, since with no tracer there
  40. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  41. * signal sent that would resume the child, but didn't because it was in
  42. * TASK_TRACED, resume it now.
  43. * Requires that irqs be disabled.
  44. */
  45. void ptrace_untrace(task_t *child)
  46. {
  47. spin_lock(&child->sighand->siglock);
  48. if (child->state == TASK_TRACED) {
  49. if (child->signal->flags & SIGNAL_STOP_STOPPED) {
  50. child->state = TASK_STOPPED;
  51. } else {
  52. signal_wake_up(child, 1);
  53. }
  54. }
  55. spin_unlock(&child->sighand->siglock);
  56. }
  57. /*
  58. * unptrace a task: move it back to its original parent and
  59. * remove it from the ptrace list.
  60. *
  61. * Must be called with the tasklist lock write-held.
  62. */
  63. void __ptrace_unlink(task_t *child)
  64. {
  65. if (!child->ptrace)
  66. BUG();
  67. child->ptrace = 0;
  68. if (!list_empty(&child->ptrace_list)) {
  69. list_del_init(&child->ptrace_list);
  70. REMOVE_LINKS(child);
  71. child->parent = child->real_parent;
  72. SET_LINKS(child);
  73. }
  74. if (child->state == TASK_TRACED)
  75. ptrace_untrace(child);
  76. }
  77. /*
  78. * Check that we have indeed attached to the thing..
  79. */
  80. int ptrace_check_attach(struct task_struct *child, int kill)
  81. {
  82. int ret = -ESRCH;
  83. /*
  84. * We take the read lock around doing both checks to close a
  85. * possible race where someone else was tracing our child and
  86. * detached between these two checks. After this locked check,
  87. * we are sure that this is our traced child and that can only
  88. * be changed by us so it's not changing right after this.
  89. */
  90. read_lock(&tasklist_lock);
  91. if ((child->ptrace & PT_PTRACED) && child->parent == current &&
  92. (!(child->ptrace & PT_ATTACHED) || child->real_parent != current)
  93. && child->signal != NULL) {
  94. ret = 0;
  95. spin_lock_irq(&child->sighand->siglock);
  96. if (child->state == TASK_STOPPED) {
  97. child->state = TASK_TRACED;
  98. } else if (child->state != TASK_TRACED && !kill) {
  99. ret = -ESRCH;
  100. }
  101. spin_unlock_irq(&child->sighand->siglock);
  102. }
  103. read_unlock(&tasklist_lock);
  104. if (!ret && !kill) {
  105. wait_task_inactive(child);
  106. }
  107. /* All systems go.. */
  108. return ret;
  109. }
  110. int ptrace_attach(struct task_struct *task)
  111. {
  112. int retval;
  113. task_lock(task);
  114. retval = -EPERM;
  115. if (task->pid <= 1)
  116. goto bad;
  117. if (task == current)
  118. goto bad;
  119. if (!task->mm)
  120. goto bad;
  121. if(((current->uid != task->euid) ||
  122. (current->uid != task->suid) ||
  123. (current->uid != task->uid) ||
  124. (current->gid != task->egid) ||
  125. (current->gid != task->sgid) ||
  126. (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
  127. goto bad;
  128. smp_rmb();
  129. if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
  130. goto bad;
  131. /* the same process cannot be attached many times */
  132. if (task->ptrace & PT_PTRACED)
  133. goto bad;
  134. retval = security_ptrace(current, task);
  135. if (retval)
  136. goto bad;
  137. /* Go */
  138. task->ptrace |= PT_PTRACED | ((task->real_parent != current)
  139. ? PT_ATTACHED : 0);
  140. if (capable(CAP_SYS_PTRACE))
  141. task->ptrace |= PT_PTRACE_CAP;
  142. task_unlock(task);
  143. write_lock_irq(&tasklist_lock);
  144. __ptrace_link(task, current);
  145. write_unlock_irq(&tasklist_lock);
  146. force_sig_specific(SIGSTOP, task);
  147. return 0;
  148. bad:
  149. task_unlock(task);
  150. return retval;
  151. }
  152. int ptrace_detach(struct task_struct *child, unsigned int data)
  153. {
  154. if (!valid_signal(data))
  155. return -EIO;
  156. /* Architecture-specific hardware disable .. */
  157. ptrace_disable(child);
  158. /* .. re-parent .. */
  159. child->exit_code = data;
  160. write_lock_irq(&tasklist_lock);
  161. __ptrace_unlink(child);
  162. /* .. and wake it up. */
  163. if (child->exit_state != EXIT_ZOMBIE)
  164. wake_up_process(child);
  165. write_unlock_irq(&tasklist_lock);
  166. return 0;
  167. }
  168. /*
  169. * Access another process' address space.
  170. * Source/target buffer must be kernel space,
  171. * Do not walk the page table directly, use get_user_pages
  172. */
  173. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  174. {
  175. struct mm_struct *mm;
  176. struct vm_area_struct *vma;
  177. struct page *page;
  178. void *old_buf = buf;
  179. mm = get_task_mm(tsk);
  180. if (!mm)
  181. return 0;
  182. down_read(&mm->mmap_sem);
  183. /* ignore errors, just check how much was sucessfully transfered */
  184. while (len) {
  185. int bytes, ret, offset;
  186. void *maddr;
  187. ret = get_user_pages(tsk, mm, addr, 1,
  188. write, 1, &page, &vma);
  189. if (ret <= 0)
  190. break;
  191. bytes = len;
  192. offset = addr & (PAGE_SIZE-1);
  193. if (bytes > PAGE_SIZE-offset)
  194. bytes = PAGE_SIZE-offset;
  195. maddr = kmap(page);
  196. if (write) {
  197. copy_to_user_page(vma, page, addr,
  198. maddr + offset, buf, bytes);
  199. set_page_dirty_lock(page);
  200. } else {
  201. copy_from_user_page(vma, page, addr,
  202. buf, maddr + offset, bytes);
  203. }
  204. kunmap(page);
  205. page_cache_release(page);
  206. len -= bytes;
  207. buf += bytes;
  208. addr += bytes;
  209. }
  210. up_read(&mm->mmap_sem);
  211. mmput(mm);
  212. return buf - old_buf;
  213. }
  214. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  215. {
  216. int copied = 0;
  217. while (len > 0) {
  218. char buf[128];
  219. int this_len, retval;
  220. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  221. retval = access_process_vm(tsk, src, buf, this_len, 0);
  222. if (!retval) {
  223. if (copied)
  224. break;
  225. return -EIO;
  226. }
  227. if (copy_to_user(dst, buf, retval))
  228. return -EFAULT;
  229. copied += retval;
  230. src += retval;
  231. dst += retval;
  232. len -= retval;
  233. }
  234. return copied;
  235. }
  236. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  237. {
  238. int copied = 0;
  239. while (len > 0) {
  240. char buf[128];
  241. int this_len, retval;
  242. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  243. if (copy_from_user(buf, src, this_len))
  244. return -EFAULT;
  245. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  246. if (!retval) {
  247. if (copied)
  248. break;
  249. return -EIO;
  250. }
  251. copied += retval;
  252. src += retval;
  253. dst += retval;
  254. len -= retval;
  255. }
  256. return copied;
  257. }
  258. static int ptrace_setoptions(struct task_struct *child, long data)
  259. {
  260. child->ptrace &= ~PT_TRACE_MASK;
  261. if (data & PTRACE_O_TRACESYSGOOD)
  262. child->ptrace |= PT_TRACESYSGOOD;
  263. if (data & PTRACE_O_TRACEFORK)
  264. child->ptrace |= PT_TRACE_FORK;
  265. if (data & PTRACE_O_TRACEVFORK)
  266. child->ptrace |= PT_TRACE_VFORK;
  267. if (data & PTRACE_O_TRACECLONE)
  268. child->ptrace |= PT_TRACE_CLONE;
  269. if (data & PTRACE_O_TRACEEXEC)
  270. child->ptrace |= PT_TRACE_EXEC;
  271. if (data & PTRACE_O_TRACEVFORKDONE)
  272. child->ptrace |= PT_TRACE_VFORK_DONE;
  273. if (data & PTRACE_O_TRACEEXIT)
  274. child->ptrace |= PT_TRACE_EXIT;
  275. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  276. }
  277. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  278. {
  279. siginfo_t lastinfo;
  280. int error = -ESRCH;
  281. read_lock(&tasklist_lock);
  282. if (likely(child->sighand != NULL)) {
  283. error = -EINVAL;
  284. spin_lock_irq(&child->sighand->siglock);
  285. if (likely(child->last_siginfo != NULL)) {
  286. lastinfo = *child->last_siginfo;
  287. error = 0;
  288. }
  289. spin_unlock_irq(&child->sighand->siglock);
  290. }
  291. read_unlock(&tasklist_lock);
  292. if (!error)
  293. return copy_siginfo_to_user(data, &lastinfo);
  294. return error;
  295. }
  296. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  297. {
  298. siginfo_t newinfo;
  299. int error = -ESRCH;
  300. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  301. return -EFAULT;
  302. read_lock(&tasklist_lock);
  303. if (likely(child->sighand != NULL)) {
  304. error = -EINVAL;
  305. spin_lock_irq(&child->sighand->siglock);
  306. if (likely(child->last_siginfo != NULL)) {
  307. *child->last_siginfo = newinfo;
  308. error = 0;
  309. }
  310. spin_unlock_irq(&child->sighand->siglock);
  311. }
  312. read_unlock(&tasklist_lock);
  313. return error;
  314. }
  315. int ptrace_request(struct task_struct *child, long request,
  316. long addr, long data)
  317. {
  318. int ret = -EIO;
  319. switch (request) {
  320. #ifdef PTRACE_OLDSETOPTIONS
  321. case PTRACE_OLDSETOPTIONS:
  322. #endif
  323. case PTRACE_SETOPTIONS:
  324. ret = ptrace_setoptions(child, data);
  325. break;
  326. case PTRACE_GETEVENTMSG:
  327. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  328. break;
  329. case PTRACE_GETSIGINFO:
  330. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  331. break;
  332. case PTRACE_SETSIGINFO:
  333. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  334. break;
  335. default:
  336. break;
  337. }
  338. return ret;
  339. }