ptrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/capability.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/mm.h>
  14. #include <linux/highmem.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/security.h>
  19. #include <linux/signal.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/uaccess.h>
  22. /*
  23. * ptrace a task: make the debugger its new parent and
  24. * move it to the ptrace list.
  25. *
  26. * Must be called with the tasklist lock write-held.
  27. */
  28. void __ptrace_link(task_t *child, task_t *new_parent)
  29. {
  30. if (!list_empty(&child->ptrace_list))
  31. BUG();
  32. if (child->parent == new_parent)
  33. return;
  34. list_add(&child->ptrace_list, &child->parent->ptrace_children);
  35. REMOVE_LINKS(child);
  36. child->parent = new_parent;
  37. SET_LINKS(child);
  38. }
  39. /*
  40. * Turn a tracing stop into a normal stop now, since with no tracer there
  41. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  42. * signal sent that would resume the child, but didn't because it was in
  43. * TASK_TRACED, resume it now.
  44. * Requires that irqs be disabled.
  45. */
  46. void ptrace_untrace(task_t *child)
  47. {
  48. spin_lock(&child->sighand->siglock);
  49. if (child->state == TASK_TRACED) {
  50. if (child->signal->flags & SIGNAL_STOP_STOPPED) {
  51. child->state = TASK_STOPPED;
  52. } else {
  53. signal_wake_up(child, 1);
  54. }
  55. }
  56. if (child->signal->flags & SIGNAL_GROUP_EXIT) {
  57. sigaddset(&child->pending.signal, SIGKILL);
  58. signal_wake_up(child, 1);
  59. }
  60. spin_unlock(&child->sighand->siglock);
  61. }
  62. /*
  63. * unptrace a task: move it back to its original parent and
  64. * remove it from the ptrace list.
  65. *
  66. * Must be called with the tasklist lock write-held.
  67. */
  68. void __ptrace_unlink(task_t *child)
  69. {
  70. BUG_ON(!child->ptrace);
  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. void __ptrace_detach(struct task_struct *child, unsigned int data)
  168. {
  169. child->exit_code = data;
  170. /* .. re-parent .. */
  171. __ptrace_unlink(child);
  172. /* .. and wake it up. */
  173. if (child->exit_state != EXIT_ZOMBIE)
  174. wake_up_process(child);
  175. }
  176. int ptrace_detach(struct task_struct *child, unsigned int data)
  177. {
  178. if (!valid_signal(data))
  179. return -EIO;
  180. /* Architecture-specific hardware disable .. */
  181. ptrace_disable(child);
  182. write_lock_irq(&tasklist_lock);
  183. if (child->ptrace)
  184. __ptrace_detach(child, data);
  185. write_unlock_irq(&tasklist_lock);
  186. return 0;
  187. }
  188. /*
  189. * Access another process' address space.
  190. * Source/target buffer must be kernel space,
  191. * Do not walk the page table directly, use get_user_pages
  192. */
  193. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  194. {
  195. struct mm_struct *mm;
  196. struct vm_area_struct *vma;
  197. struct page *page;
  198. void *old_buf = buf;
  199. mm = get_task_mm(tsk);
  200. if (!mm)
  201. return 0;
  202. down_read(&mm->mmap_sem);
  203. /* ignore errors, just check how much was sucessfully transfered */
  204. while (len) {
  205. int bytes, ret, offset;
  206. void *maddr;
  207. ret = get_user_pages(tsk, mm, addr, 1,
  208. write, 1, &page, &vma);
  209. if (ret <= 0)
  210. break;
  211. bytes = len;
  212. offset = addr & (PAGE_SIZE-1);
  213. if (bytes > PAGE_SIZE-offset)
  214. bytes = PAGE_SIZE-offset;
  215. maddr = kmap(page);
  216. if (write) {
  217. copy_to_user_page(vma, page, addr,
  218. maddr + offset, buf, bytes);
  219. set_page_dirty_lock(page);
  220. } else {
  221. copy_from_user_page(vma, page, addr,
  222. buf, maddr + offset, bytes);
  223. }
  224. kunmap(page);
  225. page_cache_release(page);
  226. len -= bytes;
  227. buf += bytes;
  228. addr += bytes;
  229. }
  230. up_read(&mm->mmap_sem);
  231. mmput(mm);
  232. return buf - old_buf;
  233. }
  234. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  235. {
  236. int copied = 0;
  237. while (len > 0) {
  238. char buf[128];
  239. int this_len, retval;
  240. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  241. retval = access_process_vm(tsk, src, buf, this_len, 0);
  242. if (!retval) {
  243. if (copied)
  244. break;
  245. return -EIO;
  246. }
  247. if (copy_to_user(dst, buf, retval))
  248. return -EFAULT;
  249. copied += retval;
  250. src += retval;
  251. dst += retval;
  252. len -= retval;
  253. }
  254. return copied;
  255. }
  256. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  257. {
  258. int copied = 0;
  259. while (len > 0) {
  260. char buf[128];
  261. int this_len, retval;
  262. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  263. if (copy_from_user(buf, src, this_len))
  264. return -EFAULT;
  265. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  266. if (!retval) {
  267. if (copied)
  268. break;
  269. return -EIO;
  270. }
  271. copied += retval;
  272. src += retval;
  273. dst += retval;
  274. len -= retval;
  275. }
  276. return copied;
  277. }
  278. static int ptrace_setoptions(struct task_struct *child, long data)
  279. {
  280. child->ptrace &= ~PT_TRACE_MASK;
  281. if (data & PTRACE_O_TRACESYSGOOD)
  282. child->ptrace |= PT_TRACESYSGOOD;
  283. if (data & PTRACE_O_TRACEFORK)
  284. child->ptrace |= PT_TRACE_FORK;
  285. if (data & PTRACE_O_TRACEVFORK)
  286. child->ptrace |= PT_TRACE_VFORK;
  287. if (data & PTRACE_O_TRACECLONE)
  288. child->ptrace |= PT_TRACE_CLONE;
  289. if (data & PTRACE_O_TRACEEXEC)
  290. child->ptrace |= PT_TRACE_EXEC;
  291. if (data & PTRACE_O_TRACEVFORKDONE)
  292. child->ptrace |= PT_TRACE_VFORK_DONE;
  293. if (data & PTRACE_O_TRACEEXIT)
  294. child->ptrace |= PT_TRACE_EXIT;
  295. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  296. }
  297. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  298. {
  299. siginfo_t lastinfo;
  300. int error = -ESRCH;
  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. lastinfo = *child->last_siginfo;
  307. error = 0;
  308. }
  309. spin_unlock_irq(&child->sighand->siglock);
  310. }
  311. read_unlock(&tasklist_lock);
  312. if (!error)
  313. return copy_siginfo_to_user(data, &lastinfo);
  314. return error;
  315. }
  316. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  317. {
  318. siginfo_t newinfo;
  319. int error = -ESRCH;
  320. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  321. return -EFAULT;
  322. read_lock(&tasklist_lock);
  323. if (likely(child->sighand != NULL)) {
  324. error = -EINVAL;
  325. spin_lock_irq(&child->sighand->siglock);
  326. if (likely(child->last_siginfo != NULL)) {
  327. *child->last_siginfo = newinfo;
  328. error = 0;
  329. }
  330. spin_unlock_irq(&child->sighand->siglock);
  331. }
  332. read_unlock(&tasklist_lock);
  333. return error;
  334. }
  335. int ptrace_request(struct task_struct *child, long request,
  336. long addr, long data)
  337. {
  338. int ret = -EIO;
  339. switch (request) {
  340. #ifdef PTRACE_OLDSETOPTIONS
  341. case PTRACE_OLDSETOPTIONS:
  342. #endif
  343. case PTRACE_SETOPTIONS:
  344. ret = ptrace_setoptions(child, data);
  345. break;
  346. case PTRACE_GETEVENTMSG:
  347. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  348. break;
  349. case PTRACE_GETSIGINFO:
  350. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  351. break;
  352. case PTRACE_SETSIGINFO:
  353. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  354. break;
  355. default:
  356. break;
  357. }
  358. return ret;
  359. }
  360. /**
  361. * ptrace_traceme -- helper for PTRACE_TRACEME
  362. *
  363. * Performs checks and sets PT_PTRACED.
  364. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  365. */
  366. int ptrace_traceme(void)
  367. {
  368. int ret;
  369. /*
  370. * Are we already being traced?
  371. */
  372. if (current->ptrace & PT_PTRACED)
  373. return -EPERM;
  374. ret = security_ptrace(current->parent, current);
  375. if (ret)
  376. return -EPERM;
  377. /*
  378. * Set the ptrace bit in the process ptrace flags.
  379. */
  380. current->ptrace |= PT_PTRACED;
  381. return 0;
  382. }
  383. /**
  384. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  385. * @pid: process id to grab a task_struct reference of
  386. *
  387. * This function is a helper for ptrace implementations. It checks
  388. * permissions and then grabs a task struct for use of the actual
  389. * ptrace implementation.
  390. *
  391. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  392. */
  393. struct task_struct *ptrace_get_task_struct(pid_t pid)
  394. {
  395. struct task_struct *child;
  396. /*
  397. * Tracing init is not allowed.
  398. */
  399. if (pid == 1)
  400. return ERR_PTR(-EPERM);
  401. read_lock(&tasklist_lock);
  402. child = find_task_by_pid(pid);
  403. if (child)
  404. get_task_struct(child);
  405. read_unlock(&tasklist_lock);
  406. if (!child)
  407. return ERR_PTR(-ESRCH);
  408. return child;
  409. }
  410. #ifndef __ARCH_SYS_PTRACE
  411. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  412. {
  413. struct task_struct *child;
  414. long ret;
  415. /*
  416. * This lock_kernel fixes a subtle race with suid exec
  417. */
  418. lock_kernel();
  419. if (request == PTRACE_TRACEME) {
  420. ret = ptrace_traceme();
  421. goto out;
  422. }
  423. child = ptrace_get_task_struct(pid);
  424. if (IS_ERR(child)) {
  425. ret = PTR_ERR(child);
  426. goto out;
  427. }
  428. if (request == PTRACE_ATTACH) {
  429. ret = ptrace_attach(child);
  430. goto out_put_task_struct;
  431. }
  432. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  433. if (ret < 0)
  434. goto out_put_task_struct;
  435. ret = arch_ptrace(child, request, addr, data);
  436. if (ret < 0)
  437. goto out_put_task_struct;
  438. out_put_task_struct:
  439. put_task_struct(child);
  440. out:
  441. unlock_kernel();
  442. return ret;
  443. }
  444. #endif /* __ARCH_SYS_PTRACE */