ptrace.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. static int may_attach(struct task_struct *task)
  111. {
  112. if (!task->mm)
  113. return -EPERM;
  114. if (((current->uid != task->euid) ||
  115. (current->uid != task->suid) ||
  116. (current->uid != task->uid) ||
  117. (current->gid != task->egid) ||
  118. (current->gid != task->sgid) ||
  119. (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
  120. return -EPERM;
  121. smp_rmb();
  122. if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
  123. return -EPERM;
  124. return security_ptrace(current, task);
  125. }
  126. int ptrace_may_attach(struct task_struct *task)
  127. {
  128. int err;
  129. task_lock(task);
  130. err = may_attach(task);
  131. task_unlock(task);
  132. return !err;
  133. }
  134. int ptrace_attach(struct task_struct *task)
  135. {
  136. int retval;
  137. task_lock(task);
  138. retval = -EPERM;
  139. if (task->pid <= 1)
  140. goto bad;
  141. if (task == current)
  142. goto bad;
  143. /* the same process cannot be attached many times */
  144. if (task->ptrace & PT_PTRACED)
  145. goto bad;
  146. retval = may_attach(task);
  147. if (retval)
  148. goto bad;
  149. /* Go */
  150. task->ptrace |= PT_PTRACED | ((task->real_parent != current)
  151. ? PT_ATTACHED : 0);
  152. if (capable(CAP_SYS_PTRACE))
  153. task->ptrace |= PT_PTRACE_CAP;
  154. task_unlock(task);
  155. write_lock_irq(&tasklist_lock);
  156. __ptrace_link(task, current);
  157. write_unlock_irq(&tasklist_lock);
  158. force_sig_specific(SIGSTOP, task);
  159. return 0;
  160. bad:
  161. task_unlock(task);
  162. return retval;
  163. }
  164. int ptrace_detach(struct task_struct *child, unsigned int data)
  165. {
  166. if (!valid_signal(data))
  167. return -EIO;
  168. /* Architecture-specific hardware disable .. */
  169. ptrace_disable(child);
  170. /* .. re-parent .. */
  171. child->exit_code = data;
  172. write_lock_irq(&tasklist_lock);
  173. __ptrace_unlink(child);
  174. /* .. and wake it up. */
  175. if (child->exit_state != EXIT_ZOMBIE)
  176. wake_up_process(child);
  177. write_unlock_irq(&tasklist_lock);
  178. return 0;
  179. }
  180. /*
  181. * Access another process' address space.
  182. * Source/target buffer must be kernel space,
  183. * Do not walk the page table directly, use get_user_pages
  184. */
  185. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  186. {
  187. struct mm_struct *mm;
  188. struct vm_area_struct *vma;
  189. struct page *page;
  190. void *old_buf = buf;
  191. mm = get_task_mm(tsk);
  192. if (!mm)
  193. return 0;
  194. down_read(&mm->mmap_sem);
  195. /* ignore errors, just check how much was sucessfully transfered */
  196. while (len) {
  197. int bytes, ret, offset;
  198. void *maddr;
  199. ret = get_user_pages(tsk, mm, addr, 1,
  200. write, 1, &page, &vma);
  201. if (ret <= 0)
  202. break;
  203. bytes = len;
  204. offset = addr & (PAGE_SIZE-1);
  205. if (bytes > PAGE_SIZE-offset)
  206. bytes = PAGE_SIZE-offset;
  207. maddr = kmap(page);
  208. if (write) {
  209. copy_to_user_page(vma, page, addr,
  210. maddr + offset, buf, bytes);
  211. set_page_dirty_lock(page);
  212. } else {
  213. copy_from_user_page(vma, page, addr,
  214. buf, maddr + offset, bytes);
  215. }
  216. kunmap(page);
  217. page_cache_release(page);
  218. len -= bytes;
  219. buf += bytes;
  220. addr += bytes;
  221. }
  222. up_read(&mm->mmap_sem);
  223. mmput(mm);
  224. return buf - old_buf;
  225. }
  226. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  227. {
  228. int copied = 0;
  229. while (len > 0) {
  230. char buf[128];
  231. int this_len, retval;
  232. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  233. retval = access_process_vm(tsk, src, buf, this_len, 0);
  234. if (!retval) {
  235. if (copied)
  236. break;
  237. return -EIO;
  238. }
  239. if (copy_to_user(dst, buf, retval))
  240. return -EFAULT;
  241. copied += retval;
  242. src += retval;
  243. dst += retval;
  244. len -= retval;
  245. }
  246. return copied;
  247. }
  248. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  249. {
  250. int copied = 0;
  251. while (len > 0) {
  252. char buf[128];
  253. int this_len, retval;
  254. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  255. if (copy_from_user(buf, src, this_len))
  256. return -EFAULT;
  257. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  258. if (!retval) {
  259. if (copied)
  260. break;
  261. return -EIO;
  262. }
  263. copied += retval;
  264. src += retval;
  265. dst += retval;
  266. len -= retval;
  267. }
  268. return copied;
  269. }
  270. static int ptrace_setoptions(struct task_struct *child, long data)
  271. {
  272. child->ptrace &= ~PT_TRACE_MASK;
  273. if (data & PTRACE_O_TRACESYSGOOD)
  274. child->ptrace |= PT_TRACESYSGOOD;
  275. if (data & PTRACE_O_TRACEFORK)
  276. child->ptrace |= PT_TRACE_FORK;
  277. if (data & PTRACE_O_TRACEVFORK)
  278. child->ptrace |= PT_TRACE_VFORK;
  279. if (data & PTRACE_O_TRACECLONE)
  280. child->ptrace |= PT_TRACE_CLONE;
  281. if (data & PTRACE_O_TRACEEXEC)
  282. child->ptrace |= PT_TRACE_EXEC;
  283. if (data & PTRACE_O_TRACEVFORKDONE)
  284. child->ptrace |= PT_TRACE_VFORK_DONE;
  285. if (data & PTRACE_O_TRACEEXIT)
  286. child->ptrace |= PT_TRACE_EXIT;
  287. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  288. }
  289. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  290. {
  291. siginfo_t lastinfo;
  292. int error = -ESRCH;
  293. read_lock(&tasklist_lock);
  294. if (likely(child->sighand != NULL)) {
  295. error = -EINVAL;
  296. spin_lock_irq(&child->sighand->siglock);
  297. if (likely(child->last_siginfo != NULL)) {
  298. lastinfo = *child->last_siginfo;
  299. error = 0;
  300. }
  301. spin_unlock_irq(&child->sighand->siglock);
  302. }
  303. read_unlock(&tasklist_lock);
  304. if (!error)
  305. return copy_siginfo_to_user(data, &lastinfo);
  306. return error;
  307. }
  308. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  309. {
  310. siginfo_t newinfo;
  311. int error = -ESRCH;
  312. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  313. return -EFAULT;
  314. read_lock(&tasklist_lock);
  315. if (likely(child->sighand != NULL)) {
  316. error = -EINVAL;
  317. spin_lock_irq(&child->sighand->siglock);
  318. if (likely(child->last_siginfo != NULL)) {
  319. *child->last_siginfo = newinfo;
  320. error = 0;
  321. }
  322. spin_unlock_irq(&child->sighand->siglock);
  323. }
  324. read_unlock(&tasklist_lock);
  325. return error;
  326. }
  327. int ptrace_request(struct task_struct *child, long request,
  328. long addr, long data)
  329. {
  330. int ret = -EIO;
  331. switch (request) {
  332. #ifdef PTRACE_OLDSETOPTIONS
  333. case PTRACE_OLDSETOPTIONS:
  334. #endif
  335. case PTRACE_SETOPTIONS:
  336. ret = ptrace_setoptions(child, data);
  337. break;
  338. case PTRACE_GETEVENTMSG:
  339. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  340. break;
  341. case PTRACE_GETSIGINFO:
  342. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  343. break;
  344. case PTRACE_SETSIGINFO:
  345. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  346. break;
  347. default:
  348. break;
  349. }
  350. return ret;
  351. }