ptrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. if (child->signal->flags & SIGNAL_GROUP_EXIT) {
  56. sigaddset(&child->pending.signal, SIGKILL);
  57. signal_wake_up(child, 1);
  58. }
  59. spin_unlock(&child->sighand->siglock);
  60. }
  61. /*
  62. * unptrace a task: move it back to its original parent and
  63. * remove it from the ptrace list.
  64. *
  65. * Must be called with the tasklist lock write-held.
  66. */
  67. void __ptrace_unlink(task_t *child)
  68. {
  69. if (!child->ptrace)
  70. BUG();
  71. child->ptrace = 0;
  72. if (!list_empty(&child->ptrace_list)) {
  73. list_del_init(&child->ptrace_list);
  74. REMOVE_LINKS(child);
  75. child->parent = child->real_parent;
  76. SET_LINKS(child);
  77. }
  78. ptrace_untrace(child);
  79. }
  80. /*
  81. * Check that we have indeed attached to the thing..
  82. */
  83. int ptrace_check_attach(struct task_struct *child, int kill)
  84. {
  85. int ret = -ESRCH;
  86. /*
  87. * We take the read lock around doing both checks to close a
  88. * possible race where someone else was tracing our child and
  89. * detached between these two checks. After this locked check,
  90. * we are sure that this is our traced child and that can only
  91. * be changed by us so it's not changing right after this.
  92. */
  93. read_lock(&tasklist_lock);
  94. if ((child->ptrace & PT_PTRACED) && child->parent == current &&
  95. (!(child->ptrace & PT_ATTACHED) || child->real_parent != current)
  96. && child->signal != NULL) {
  97. ret = 0;
  98. spin_lock_irq(&child->sighand->siglock);
  99. if (child->state == TASK_STOPPED) {
  100. child->state = TASK_TRACED;
  101. } else if (child->state != TASK_TRACED && !kill) {
  102. ret = -ESRCH;
  103. }
  104. spin_unlock_irq(&child->sighand->siglock);
  105. }
  106. read_unlock(&tasklist_lock);
  107. if (!ret && !kill) {
  108. wait_task_inactive(child);
  109. }
  110. /* All systems go.. */
  111. return ret;
  112. }
  113. static int may_attach(struct task_struct *task)
  114. {
  115. if (!task->mm)
  116. return -EPERM;
  117. if (((current->uid != task->euid) ||
  118. (current->uid != task->suid) ||
  119. (current->uid != task->uid) ||
  120. (current->gid != task->egid) ||
  121. (current->gid != task->sgid) ||
  122. (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
  123. return -EPERM;
  124. smp_rmb();
  125. if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
  126. return -EPERM;
  127. return security_ptrace(current, task);
  128. }
  129. int ptrace_may_attach(struct task_struct *task)
  130. {
  131. int err;
  132. task_lock(task);
  133. err = may_attach(task);
  134. task_unlock(task);
  135. return !err;
  136. }
  137. int ptrace_attach(struct task_struct *task)
  138. {
  139. int retval;
  140. task_lock(task);
  141. retval = -EPERM;
  142. if (task->pid <= 1)
  143. goto bad;
  144. if (task->tgid == current->tgid)
  145. goto bad;
  146. /* the same process cannot be attached many times */
  147. if (task->ptrace & PT_PTRACED)
  148. goto bad;
  149. retval = may_attach(task);
  150. if (retval)
  151. goto bad;
  152. /* Go */
  153. task->ptrace |= PT_PTRACED | ((task->real_parent != current)
  154. ? PT_ATTACHED : 0);
  155. if (capable(CAP_SYS_PTRACE))
  156. task->ptrace |= PT_PTRACE_CAP;
  157. task_unlock(task);
  158. write_lock_irq(&tasklist_lock);
  159. __ptrace_link(task, current);
  160. write_unlock_irq(&tasklist_lock);
  161. force_sig_specific(SIGSTOP, task);
  162. return 0;
  163. bad:
  164. task_unlock(task);
  165. return retval;
  166. }
  167. int ptrace_detach(struct task_struct *child, unsigned int data)
  168. {
  169. if (!valid_signal(data))
  170. return -EIO;
  171. /* Architecture-specific hardware disable .. */
  172. ptrace_disable(child);
  173. /* .. re-parent .. */
  174. child->exit_code = data;
  175. write_lock_irq(&tasklist_lock);
  176. __ptrace_unlink(child);
  177. /* .. and wake it up. */
  178. if (child->exit_state != EXIT_ZOMBIE)
  179. wake_up_process(child);
  180. write_unlock_irq(&tasklist_lock);
  181. return 0;
  182. }
  183. /*
  184. * Access another process' address space.
  185. * Source/target buffer must be kernel space,
  186. * Do not walk the page table directly, use get_user_pages
  187. */
  188. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  189. {
  190. struct mm_struct *mm;
  191. struct vm_area_struct *vma;
  192. struct page *page;
  193. void *old_buf = buf;
  194. mm = get_task_mm(tsk);
  195. if (!mm)
  196. return 0;
  197. down_read(&mm->mmap_sem);
  198. /* ignore errors, just check how much was sucessfully transfered */
  199. while (len) {
  200. int bytes, ret, offset;
  201. void *maddr;
  202. ret = get_user_pages(tsk, mm, addr, 1,
  203. write, 1, &page, &vma);
  204. if (ret <= 0)
  205. break;
  206. bytes = len;
  207. offset = addr & (PAGE_SIZE-1);
  208. if (bytes > PAGE_SIZE-offset)
  209. bytes = PAGE_SIZE-offset;
  210. maddr = kmap(page);
  211. if (write) {
  212. copy_to_user_page(vma, page, addr,
  213. maddr + offset, buf, bytes);
  214. if (!PageCompound(page))
  215. set_page_dirty_lock(page);
  216. } else {
  217. copy_from_user_page(vma, page, addr,
  218. buf, maddr + offset, bytes);
  219. }
  220. kunmap(page);
  221. page_cache_release(page);
  222. len -= bytes;
  223. buf += bytes;
  224. addr += bytes;
  225. }
  226. up_read(&mm->mmap_sem);
  227. mmput(mm);
  228. return buf - old_buf;
  229. }
  230. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  231. {
  232. int copied = 0;
  233. while (len > 0) {
  234. char buf[128];
  235. int this_len, retval;
  236. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  237. retval = access_process_vm(tsk, src, buf, this_len, 0);
  238. if (!retval) {
  239. if (copied)
  240. break;
  241. return -EIO;
  242. }
  243. if (copy_to_user(dst, buf, retval))
  244. return -EFAULT;
  245. copied += retval;
  246. src += retval;
  247. dst += retval;
  248. len -= retval;
  249. }
  250. return copied;
  251. }
  252. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  253. {
  254. int copied = 0;
  255. while (len > 0) {
  256. char buf[128];
  257. int this_len, retval;
  258. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  259. if (copy_from_user(buf, src, this_len))
  260. return -EFAULT;
  261. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  262. if (!retval) {
  263. if (copied)
  264. break;
  265. return -EIO;
  266. }
  267. copied += retval;
  268. src += retval;
  269. dst += retval;
  270. len -= retval;
  271. }
  272. return copied;
  273. }
  274. static int ptrace_setoptions(struct task_struct *child, long data)
  275. {
  276. child->ptrace &= ~PT_TRACE_MASK;
  277. if (data & PTRACE_O_TRACESYSGOOD)
  278. child->ptrace |= PT_TRACESYSGOOD;
  279. if (data & PTRACE_O_TRACEFORK)
  280. child->ptrace |= PT_TRACE_FORK;
  281. if (data & PTRACE_O_TRACEVFORK)
  282. child->ptrace |= PT_TRACE_VFORK;
  283. if (data & PTRACE_O_TRACECLONE)
  284. child->ptrace |= PT_TRACE_CLONE;
  285. if (data & PTRACE_O_TRACEEXEC)
  286. child->ptrace |= PT_TRACE_EXEC;
  287. if (data & PTRACE_O_TRACEVFORKDONE)
  288. child->ptrace |= PT_TRACE_VFORK_DONE;
  289. if (data & PTRACE_O_TRACEEXIT)
  290. child->ptrace |= PT_TRACE_EXIT;
  291. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  292. }
  293. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  294. {
  295. siginfo_t lastinfo;
  296. int error = -ESRCH;
  297. read_lock(&tasklist_lock);
  298. if (likely(child->sighand != NULL)) {
  299. error = -EINVAL;
  300. spin_lock_irq(&child->sighand->siglock);
  301. if (likely(child->last_siginfo != NULL)) {
  302. lastinfo = *child->last_siginfo;
  303. error = 0;
  304. }
  305. spin_unlock_irq(&child->sighand->siglock);
  306. }
  307. read_unlock(&tasklist_lock);
  308. if (!error)
  309. return copy_siginfo_to_user(data, &lastinfo);
  310. return error;
  311. }
  312. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  313. {
  314. siginfo_t newinfo;
  315. int error = -ESRCH;
  316. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  317. return -EFAULT;
  318. read_lock(&tasklist_lock);
  319. if (likely(child->sighand != NULL)) {
  320. error = -EINVAL;
  321. spin_lock_irq(&child->sighand->siglock);
  322. if (likely(child->last_siginfo != NULL)) {
  323. *child->last_siginfo = newinfo;
  324. error = 0;
  325. }
  326. spin_unlock_irq(&child->sighand->siglock);
  327. }
  328. read_unlock(&tasklist_lock);
  329. return error;
  330. }
  331. int ptrace_request(struct task_struct *child, long request,
  332. long addr, long data)
  333. {
  334. int ret = -EIO;
  335. switch (request) {
  336. #ifdef PTRACE_OLDSETOPTIONS
  337. case PTRACE_OLDSETOPTIONS:
  338. #endif
  339. case PTRACE_SETOPTIONS:
  340. ret = ptrace_setoptions(child, data);
  341. break;
  342. case PTRACE_GETEVENTMSG:
  343. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  344. break;
  345. case PTRACE_GETSIGINFO:
  346. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  347. break;
  348. case PTRACE_SETSIGINFO:
  349. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  350. break;
  351. default:
  352. break;
  353. }
  354. return ret;
  355. }
  356. #ifndef __ARCH_SYS_PTRACE
  357. static int ptrace_get_task_struct(long request, long pid,
  358. struct task_struct **childp)
  359. {
  360. struct task_struct *child;
  361. int ret;
  362. /*
  363. * Callers use child == NULL as an indication to exit early even
  364. * when the return value is 0, so make sure it is non-NULL here.
  365. */
  366. *childp = NULL;
  367. if (request == PTRACE_TRACEME) {
  368. /*
  369. * Are we already being traced?
  370. */
  371. if (current->ptrace & PT_PTRACED)
  372. return -EPERM;
  373. ret = security_ptrace(current->parent, current);
  374. if (ret)
  375. return -EPERM;
  376. /*
  377. * Set the ptrace bit in the process ptrace flags.
  378. */
  379. current->ptrace |= PT_PTRACED;
  380. return 0;
  381. }
  382. /*
  383. * You may not mess with init
  384. */
  385. if (pid == 1)
  386. return -EPERM;
  387. ret = -ESRCH;
  388. read_lock(&tasklist_lock);
  389. child = find_task_by_pid(pid);
  390. if (child)
  391. get_task_struct(child);
  392. read_unlock(&tasklist_lock);
  393. if (!child)
  394. return -ESRCH;
  395. *childp = child;
  396. return 0;
  397. }
  398. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  399. {
  400. struct task_struct *child;
  401. long ret;
  402. /*
  403. * This lock_kernel fixes a subtle race with suid exec
  404. */
  405. lock_kernel();
  406. ret = ptrace_get_task_struct(request, pid, &child);
  407. if (!child)
  408. goto out;
  409. if (request == PTRACE_ATTACH) {
  410. ret = ptrace_attach(child);
  411. goto out_put_task_struct;
  412. }
  413. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  414. if (ret < 0)
  415. goto out_put_task_struct;
  416. ret = arch_ptrace(child, request, addr, data);
  417. if (ret < 0)
  418. goto out_put_task_struct;
  419. out_put_task_struct:
  420. put_task_struct(child);
  421. out:
  422. unlock_kernel();
  423. return ret;
  424. }
  425. #endif /* __ARCH_SYS_PTRACE */