ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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(struct task_struct *child, struct task_struct *new_parent)
  29. {
  30. BUG_ON(!list_empty(&child->ptrace_list));
  31. if (child->parent == new_parent)
  32. return;
  33. list_add(&child->ptrace_list, &child->parent->ptrace_children);
  34. remove_parent(child);
  35. child->parent = new_parent;
  36. add_parent(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(struct task_struct *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(struct task_struct *child)
  64. {
  65. BUG_ON(!child->ptrace);
  66. child->ptrace = 0;
  67. if (!list_empty(&child->ptrace_list)) {
  68. list_del_init(&child->ptrace_list);
  69. remove_parent(child);
  70. child->parent = child->real_parent;
  71. add_parent(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. static int may_attach(struct task_struct *task)
  110. {
  111. /* May we inspect the given task?
  112. * This check is used both for attaching with ptrace
  113. * and for allowing access to sensitive information in /proc.
  114. *
  115. * ptrace_attach denies several cases that /proc allows
  116. * because setting up the necessary parent/child relationship
  117. * or halting the specified task is impossible.
  118. */
  119. int dumpable = 0;
  120. /* Don't let security modules deny introspection */
  121. if (task == current)
  122. return 0;
  123. if (((current->uid != task->euid) ||
  124. (current->uid != task->suid) ||
  125. (current->uid != task->uid) ||
  126. (current->gid != task->egid) ||
  127. (current->gid != task->sgid) ||
  128. (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
  129. return -EPERM;
  130. smp_rmb();
  131. if (task->mm)
  132. dumpable = task->mm->dumpable;
  133. if (!dumpable && !capable(CAP_SYS_PTRACE))
  134. return -EPERM;
  135. return security_ptrace(current, task);
  136. }
  137. int ptrace_may_attach(struct task_struct *task)
  138. {
  139. int err;
  140. task_lock(task);
  141. err = may_attach(task);
  142. task_unlock(task);
  143. return !err;
  144. }
  145. int ptrace_attach(struct task_struct *task)
  146. {
  147. int retval;
  148. retval = -EPERM;
  149. if (task->pid <= 1)
  150. goto out;
  151. if (task->tgid == current->tgid)
  152. goto out;
  153. repeat:
  154. /*
  155. * Nasty, nasty.
  156. *
  157. * We want to hold both the task-lock and the
  158. * tasklist_lock for writing at the same time.
  159. * But that's against the rules (tasklist_lock
  160. * is taken for reading by interrupts on other
  161. * cpu's that may have task_lock).
  162. */
  163. task_lock(task);
  164. local_irq_disable();
  165. if (!write_trylock(&tasklist_lock)) {
  166. local_irq_enable();
  167. task_unlock(task);
  168. do {
  169. cpu_relax();
  170. } while (!write_can_lock(&tasklist_lock));
  171. goto repeat;
  172. }
  173. if (!task->mm)
  174. goto bad;
  175. /* the same process cannot be attached many times */
  176. if (task->ptrace & PT_PTRACED)
  177. goto bad;
  178. retval = may_attach(task);
  179. if (retval)
  180. goto bad;
  181. /* Go */
  182. task->ptrace |= PT_PTRACED | ((task->real_parent != current)
  183. ? PT_ATTACHED : 0);
  184. if (capable(CAP_SYS_PTRACE))
  185. task->ptrace |= PT_PTRACE_CAP;
  186. __ptrace_link(task, current);
  187. force_sig_specific(SIGSTOP, task);
  188. bad:
  189. write_unlock_irq(&tasklist_lock);
  190. task_unlock(task);
  191. out:
  192. return retval;
  193. }
  194. static inline void __ptrace_detach(struct task_struct *child, unsigned int data)
  195. {
  196. child->exit_code = data;
  197. /* .. re-parent .. */
  198. __ptrace_unlink(child);
  199. /* .. and wake it up. */
  200. if (child->exit_state != EXIT_ZOMBIE)
  201. wake_up_process(child);
  202. }
  203. int ptrace_detach(struct task_struct *child, unsigned int data)
  204. {
  205. if (!valid_signal(data))
  206. return -EIO;
  207. /* Architecture-specific hardware disable .. */
  208. ptrace_disable(child);
  209. write_lock_irq(&tasklist_lock);
  210. /* protect against de_thread()->release_task() */
  211. if (child->ptrace)
  212. __ptrace_detach(child, data);
  213. write_unlock_irq(&tasklist_lock);
  214. return 0;
  215. }
  216. /*
  217. * Access another process' address space.
  218. * Source/target buffer must be kernel space,
  219. * Do not walk the page table directly, use get_user_pages
  220. */
  221. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  222. {
  223. struct mm_struct *mm;
  224. struct vm_area_struct *vma;
  225. struct page *page;
  226. void *old_buf = buf;
  227. mm = get_task_mm(tsk);
  228. if (!mm)
  229. return 0;
  230. down_read(&mm->mmap_sem);
  231. /* ignore errors, just check how much was sucessfully transfered */
  232. while (len) {
  233. int bytes, ret, offset;
  234. void *maddr;
  235. ret = get_user_pages(tsk, mm, addr, 1,
  236. write, 1, &page, &vma);
  237. if (ret <= 0)
  238. break;
  239. bytes = len;
  240. offset = addr & (PAGE_SIZE-1);
  241. if (bytes > PAGE_SIZE-offset)
  242. bytes = PAGE_SIZE-offset;
  243. maddr = kmap(page);
  244. if (write) {
  245. copy_to_user_page(vma, page, addr,
  246. maddr + offset, buf, bytes);
  247. set_page_dirty_lock(page);
  248. } else {
  249. copy_from_user_page(vma, page, addr,
  250. buf, maddr + offset, bytes);
  251. }
  252. kunmap(page);
  253. page_cache_release(page);
  254. len -= bytes;
  255. buf += bytes;
  256. addr += bytes;
  257. }
  258. up_read(&mm->mmap_sem);
  259. mmput(mm);
  260. return buf - old_buf;
  261. }
  262. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  263. {
  264. int copied = 0;
  265. while (len > 0) {
  266. char buf[128];
  267. int this_len, retval;
  268. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  269. retval = access_process_vm(tsk, src, buf, this_len, 0);
  270. if (!retval) {
  271. if (copied)
  272. break;
  273. return -EIO;
  274. }
  275. if (copy_to_user(dst, buf, retval))
  276. return -EFAULT;
  277. copied += retval;
  278. src += retval;
  279. dst += retval;
  280. len -= retval;
  281. }
  282. return copied;
  283. }
  284. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  285. {
  286. int copied = 0;
  287. while (len > 0) {
  288. char buf[128];
  289. int this_len, retval;
  290. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  291. if (copy_from_user(buf, src, this_len))
  292. return -EFAULT;
  293. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  294. if (!retval) {
  295. if (copied)
  296. break;
  297. return -EIO;
  298. }
  299. copied += retval;
  300. src += retval;
  301. dst += retval;
  302. len -= retval;
  303. }
  304. return copied;
  305. }
  306. static int ptrace_setoptions(struct task_struct *child, long data)
  307. {
  308. child->ptrace &= ~PT_TRACE_MASK;
  309. if (data & PTRACE_O_TRACESYSGOOD)
  310. child->ptrace |= PT_TRACESYSGOOD;
  311. if (data & PTRACE_O_TRACEFORK)
  312. child->ptrace |= PT_TRACE_FORK;
  313. if (data & PTRACE_O_TRACEVFORK)
  314. child->ptrace |= PT_TRACE_VFORK;
  315. if (data & PTRACE_O_TRACECLONE)
  316. child->ptrace |= PT_TRACE_CLONE;
  317. if (data & PTRACE_O_TRACEEXEC)
  318. child->ptrace |= PT_TRACE_EXEC;
  319. if (data & PTRACE_O_TRACEVFORKDONE)
  320. child->ptrace |= PT_TRACE_VFORK_DONE;
  321. if (data & PTRACE_O_TRACEEXIT)
  322. child->ptrace |= PT_TRACE_EXIT;
  323. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  324. }
  325. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  326. {
  327. siginfo_t lastinfo;
  328. int error = -ESRCH;
  329. read_lock(&tasklist_lock);
  330. if (likely(child->sighand != NULL)) {
  331. error = -EINVAL;
  332. spin_lock_irq(&child->sighand->siglock);
  333. if (likely(child->last_siginfo != NULL)) {
  334. lastinfo = *child->last_siginfo;
  335. error = 0;
  336. }
  337. spin_unlock_irq(&child->sighand->siglock);
  338. }
  339. read_unlock(&tasklist_lock);
  340. if (!error)
  341. return copy_siginfo_to_user(data, &lastinfo);
  342. return error;
  343. }
  344. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  345. {
  346. siginfo_t newinfo;
  347. int error = -ESRCH;
  348. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  349. return -EFAULT;
  350. read_lock(&tasklist_lock);
  351. if (likely(child->sighand != NULL)) {
  352. error = -EINVAL;
  353. spin_lock_irq(&child->sighand->siglock);
  354. if (likely(child->last_siginfo != NULL)) {
  355. *child->last_siginfo = newinfo;
  356. error = 0;
  357. }
  358. spin_unlock_irq(&child->sighand->siglock);
  359. }
  360. read_unlock(&tasklist_lock);
  361. return error;
  362. }
  363. int ptrace_request(struct task_struct *child, long request,
  364. long addr, long data)
  365. {
  366. int ret = -EIO;
  367. switch (request) {
  368. #ifdef PTRACE_OLDSETOPTIONS
  369. case PTRACE_OLDSETOPTIONS:
  370. #endif
  371. case PTRACE_SETOPTIONS:
  372. ret = ptrace_setoptions(child, data);
  373. break;
  374. case PTRACE_GETEVENTMSG:
  375. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  376. break;
  377. case PTRACE_GETSIGINFO:
  378. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  379. break;
  380. case PTRACE_SETSIGINFO:
  381. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  382. break;
  383. default:
  384. break;
  385. }
  386. return ret;
  387. }
  388. /**
  389. * ptrace_traceme -- helper for PTRACE_TRACEME
  390. *
  391. * Performs checks and sets PT_PTRACED.
  392. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  393. */
  394. int ptrace_traceme(void)
  395. {
  396. int ret = -EPERM;
  397. /*
  398. * Are we already being traced?
  399. */
  400. task_lock(current);
  401. if (!(current->ptrace & PT_PTRACED)) {
  402. ret = security_ptrace(current->parent, current);
  403. /*
  404. * Set the ptrace bit in the process ptrace flags.
  405. */
  406. if (!ret)
  407. current->ptrace |= PT_PTRACED;
  408. }
  409. task_unlock(current);
  410. return ret;
  411. }
  412. /**
  413. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  414. * @pid: process id to grab a task_struct reference of
  415. *
  416. * This function is a helper for ptrace implementations. It checks
  417. * permissions and then grabs a task struct for use of the actual
  418. * ptrace implementation.
  419. *
  420. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  421. */
  422. struct task_struct *ptrace_get_task_struct(pid_t pid)
  423. {
  424. struct task_struct *child;
  425. /*
  426. * Tracing init is not allowed.
  427. */
  428. if (pid == 1)
  429. return ERR_PTR(-EPERM);
  430. read_lock(&tasklist_lock);
  431. child = find_task_by_pid(pid);
  432. if (child)
  433. get_task_struct(child);
  434. read_unlock(&tasklist_lock);
  435. if (!child)
  436. return ERR_PTR(-ESRCH);
  437. return child;
  438. }
  439. #ifndef __ARCH_SYS_PTRACE
  440. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  441. {
  442. struct task_struct *child;
  443. long ret;
  444. /*
  445. * This lock_kernel fixes a subtle race with suid exec
  446. */
  447. lock_kernel();
  448. if (request == PTRACE_TRACEME) {
  449. ret = ptrace_traceme();
  450. goto out;
  451. }
  452. child = ptrace_get_task_struct(pid);
  453. if (IS_ERR(child)) {
  454. ret = PTR_ERR(child);
  455. goto out;
  456. }
  457. if (request == PTRACE_ATTACH) {
  458. ret = ptrace_attach(child);
  459. goto out_put_task_struct;
  460. }
  461. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  462. if (ret < 0)
  463. goto out_put_task_struct;
  464. ret = arch_ptrace(child, request, addr, data);
  465. if (ret < 0)
  466. goto out_put_task_struct;
  467. out_put_task_struct:
  468. put_task_struct(child);
  469. out:
  470. unlock_kernel();
  471. return ret;
  472. }
  473. #endif /* __ARCH_SYS_PTRACE */