ptrace.c 11 KB

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