ptrace.c 8.7 KB

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