ptrace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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 <linux/audit.h>
  21. #include <linux/pid_namespace.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/uaccess.h>
  24. /*
  25. * ptrace a task: make the debugger its new parent and
  26. * move it to the ptrace list.
  27. *
  28. * Must be called with the tasklist lock write-held.
  29. */
  30. void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
  31. {
  32. BUG_ON(!list_empty(&child->ptrace_list));
  33. if (child->parent == new_parent)
  34. return;
  35. list_add(&child->ptrace_list, &child->parent->ptrace_children);
  36. remove_parent(child);
  37. child->parent = new_parent;
  38. add_parent(child);
  39. }
  40. /*
  41. * Turn a tracing stop into a normal stop now, since with no tracer there
  42. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  43. * signal sent that would resume the child, but didn't because it was in
  44. * TASK_TRACED, resume it now.
  45. * Requires that irqs be disabled.
  46. */
  47. void ptrace_untrace(struct task_struct *child)
  48. {
  49. spin_lock(&child->sighand->siglock);
  50. if (task_is_traced(child)) {
  51. if (child->signal->flags & SIGNAL_STOP_STOPPED) {
  52. __set_task_state(child, TASK_STOPPED);
  53. } else {
  54. signal_wake_up(child, 1);
  55. }
  56. }
  57. spin_unlock(&child->sighand->siglock);
  58. }
  59. /*
  60. * unptrace a task: move it back to its original parent and
  61. * remove it from the ptrace list.
  62. *
  63. * Must be called with the tasklist lock write-held.
  64. */
  65. void __ptrace_unlink(struct task_struct *child)
  66. {
  67. BUG_ON(!child->ptrace);
  68. child->ptrace = 0;
  69. if (!list_empty(&child->ptrace_list)) {
  70. list_del_init(&child->ptrace_list);
  71. remove_parent(child);
  72. child->parent = child->real_parent;
  73. add_parent(child);
  74. }
  75. if (task_is_traced(child))
  76. ptrace_untrace(child);
  77. }
  78. /*
  79. * Check that we have indeed attached to the thing..
  80. */
  81. int ptrace_check_attach(struct task_struct *child, int kill)
  82. {
  83. int ret = -ESRCH;
  84. /*
  85. * We take the read lock around doing both checks to close a
  86. * possible race where someone else was tracing our child and
  87. * detached between these two checks. After this locked check,
  88. * we are sure that this is our traced child and that can only
  89. * be changed by us so it's not changing right after this.
  90. */
  91. read_lock(&tasklist_lock);
  92. if ((child->ptrace & PT_PTRACED) && child->parent == current &&
  93. (!(child->ptrace & PT_ATTACHED) || child->real_parent != current)
  94. && child->signal != NULL) {
  95. ret = 0;
  96. spin_lock_irq(&child->sighand->siglock);
  97. if (task_is_stopped(child))
  98. child->state = TASK_TRACED;
  99. else if (!task_is_traced(child) && !kill)
  100. ret = -ESRCH;
  101. spin_unlock_irq(&child->sighand->siglock);
  102. }
  103. read_unlock(&tasklist_lock);
  104. if (!ret && !kill)
  105. wait_task_inactive(child);
  106. /* All systems go.. */
  107. return ret;
  108. }
  109. int __ptrace_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 = get_dumpable(task->mm);
  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 = __ptrace_may_attach(task);
  142. task_unlock(task);
  143. return !err;
  144. }
  145. int ptrace_attach(struct task_struct *task)
  146. {
  147. int retval;
  148. unsigned long flags;
  149. audit_ptrace(task);
  150. retval = -EPERM;
  151. if (task->pid <= 1)
  152. goto out;
  153. if (same_thread_group(task, current))
  154. goto out;
  155. repeat:
  156. /*
  157. * Nasty, nasty.
  158. *
  159. * We want to hold both the task-lock and the
  160. * tasklist_lock for writing at the same time.
  161. * But that's against the rules (tasklist_lock
  162. * is taken for reading by interrupts on other
  163. * cpu's that may have task_lock).
  164. */
  165. task_lock(task);
  166. if (!write_trylock_irqsave(&tasklist_lock, flags)) {
  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 = __ptrace_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_irqrestore(&tasklist_lock, flags);
  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. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  210. write_lock_irq(&tasklist_lock);
  211. /* protect against de_thread()->release_task() */
  212. if (child->ptrace)
  213. __ptrace_detach(child, data);
  214. write_unlock_irq(&tasklist_lock);
  215. return 0;
  216. }
  217. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  218. {
  219. int copied = 0;
  220. while (len > 0) {
  221. char buf[128];
  222. int this_len, retval;
  223. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  224. retval = access_process_vm(tsk, src, buf, this_len, 0);
  225. if (!retval) {
  226. if (copied)
  227. break;
  228. return -EIO;
  229. }
  230. if (copy_to_user(dst, buf, retval))
  231. return -EFAULT;
  232. copied += retval;
  233. src += retval;
  234. dst += retval;
  235. len -= retval;
  236. }
  237. return copied;
  238. }
  239. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  240. {
  241. int copied = 0;
  242. while (len > 0) {
  243. char buf[128];
  244. int this_len, retval;
  245. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  246. if (copy_from_user(buf, src, this_len))
  247. return -EFAULT;
  248. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  249. if (!retval) {
  250. if (copied)
  251. break;
  252. return -EIO;
  253. }
  254. copied += retval;
  255. src += retval;
  256. dst += retval;
  257. len -= retval;
  258. }
  259. return copied;
  260. }
  261. static int ptrace_setoptions(struct task_struct *child, long data)
  262. {
  263. child->ptrace &= ~PT_TRACE_MASK;
  264. if (data & PTRACE_O_TRACESYSGOOD)
  265. child->ptrace |= PT_TRACESYSGOOD;
  266. if (data & PTRACE_O_TRACEFORK)
  267. child->ptrace |= PT_TRACE_FORK;
  268. if (data & PTRACE_O_TRACEVFORK)
  269. child->ptrace |= PT_TRACE_VFORK;
  270. if (data & PTRACE_O_TRACECLONE)
  271. child->ptrace |= PT_TRACE_CLONE;
  272. if (data & PTRACE_O_TRACEEXEC)
  273. child->ptrace |= PT_TRACE_EXEC;
  274. if (data & PTRACE_O_TRACEVFORKDONE)
  275. child->ptrace |= PT_TRACE_VFORK_DONE;
  276. if (data & PTRACE_O_TRACEEXIT)
  277. child->ptrace |= PT_TRACE_EXIT;
  278. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  279. }
  280. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  281. {
  282. siginfo_t lastinfo;
  283. int error = -ESRCH;
  284. read_lock(&tasklist_lock);
  285. if (likely(child->sighand != NULL)) {
  286. error = -EINVAL;
  287. spin_lock_irq(&child->sighand->siglock);
  288. if (likely(child->last_siginfo != NULL)) {
  289. lastinfo = *child->last_siginfo;
  290. error = 0;
  291. }
  292. spin_unlock_irq(&child->sighand->siglock);
  293. }
  294. read_unlock(&tasklist_lock);
  295. if (!error)
  296. return copy_siginfo_to_user(data, &lastinfo);
  297. return error;
  298. }
  299. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  300. {
  301. siginfo_t newinfo;
  302. int error = -ESRCH;
  303. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  304. return -EFAULT;
  305. read_lock(&tasklist_lock);
  306. if (likely(child->sighand != NULL)) {
  307. error = -EINVAL;
  308. spin_lock_irq(&child->sighand->siglock);
  309. if (likely(child->last_siginfo != NULL)) {
  310. *child->last_siginfo = newinfo;
  311. error = 0;
  312. }
  313. spin_unlock_irq(&child->sighand->siglock);
  314. }
  315. read_unlock(&tasklist_lock);
  316. return error;
  317. }
  318. #ifdef PTRACE_SINGLESTEP
  319. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  320. #else
  321. #define is_singlestep(request) 0
  322. #endif
  323. #ifdef PTRACE_SINGLEBLOCK
  324. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  325. #else
  326. #define is_singleblock(request) 0
  327. #endif
  328. #ifdef PTRACE_SYSEMU
  329. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  330. #else
  331. #define is_sysemu_singlestep(request) 0
  332. #endif
  333. static int ptrace_resume(struct task_struct *child, long request, long data)
  334. {
  335. if (!valid_signal(data))
  336. return -EIO;
  337. if (request == PTRACE_SYSCALL)
  338. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  339. else
  340. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  341. #ifdef TIF_SYSCALL_EMU
  342. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  343. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  344. else
  345. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  346. #endif
  347. if (is_singleblock(request)) {
  348. if (unlikely(!arch_has_block_step()))
  349. return -EIO;
  350. user_enable_block_step(child);
  351. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  352. if (unlikely(!arch_has_single_step()))
  353. return -EIO;
  354. user_enable_single_step(child);
  355. }
  356. else
  357. user_disable_single_step(child);
  358. child->exit_code = data;
  359. wake_up_process(child);
  360. return 0;
  361. }
  362. int ptrace_request(struct task_struct *child, long request,
  363. long addr, long data)
  364. {
  365. int ret = -EIO;
  366. switch (request) {
  367. case PTRACE_PEEKTEXT:
  368. case PTRACE_PEEKDATA:
  369. return generic_ptrace_peekdata(child, addr, data);
  370. case PTRACE_POKETEXT:
  371. case PTRACE_POKEDATA:
  372. return generic_ptrace_pokedata(child, addr, data);
  373. #ifdef PTRACE_OLDSETOPTIONS
  374. case PTRACE_OLDSETOPTIONS:
  375. #endif
  376. case PTRACE_SETOPTIONS:
  377. ret = ptrace_setoptions(child, data);
  378. break;
  379. case PTRACE_GETEVENTMSG:
  380. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  381. break;
  382. case PTRACE_GETSIGINFO:
  383. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  384. break;
  385. case PTRACE_SETSIGINFO:
  386. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  387. break;
  388. case PTRACE_DETACH: /* detach a process that was attached. */
  389. ret = ptrace_detach(child, data);
  390. break;
  391. #ifdef PTRACE_SINGLESTEP
  392. case PTRACE_SINGLESTEP:
  393. #endif
  394. #ifdef PTRACE_SINGLEBLOCK
  395. case PTRACE_SINGLEBLOCK:
  396. #endif
  397. #ifdef PTRACE_SYSEMU
  398. case PTRACE_SYSEMU:
  399. case PTRACE_SYSEMU_SINGLESTEP:
  400. #endif
  401. case PTRACE_SYSCALL:
  402. case PTRACE_CONT:
  403. return ptrace_resume(child, request, data);
  404. case PTRACE_KILL:
  405. if (child->exit_state) /* already dead */
  406. return 0;
  407. return ptrace_resume(child, request, SIGKILL);
  408. default:
  409. break;
  410. }
  411. return ret;
  412. }
  413. /**
  414. * ptrace_traceme -- helper for PTRACE_TRACEME
  415. *
  416. * Performs checks and sets PT_PTRACED.
  417. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  418. */
  419. int ptrace_traceme(void)
  420. {
  421. int ret = -EPERM;
  422. /*
  423. * Are we already being traced?
  424. */
  425. task_lock(current);
  426. if (!(current->ptrace & PT_PTRACED)) {
  427. ret = security_ptrace(current->parent, current);
  428. /*
  429. * Set the ptrace bit in the process ptrace flags.
  430. */
  431. if (!ret)
  432. current->ptrace |= PT_PTRACED;
  433. }
  434. task_unlock(current);
  435. return ret;
  436. }
  437. /**
  438. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  439. * @pid: process id to grab a task_struct reference of
  440. *
  441. * This function is a helper for ptrace implementations. It checks
  442. * permissions and then grabs a task struct for use of the actual
  443. * ptrace implementation.
  444. *
  445. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  446. */
  447. struct task_struct *ptrace_get_task_struct(pid_t pid)
  448. {
  449. struct task_struct *child;
  450. /*
  451. * Tracing init is not allowed.
  452. */
  453. if (pid == 1)
  454. return ERR_PTR(-EPERM);
  455. read_lock(&tasklist_lock);
  456. child = find_task_by_vpid(pid);
  457. if (child)
  458. get_task_struct(child);
  459. read_unlock(&tasklist_lock);
  460. if (!child)
  461. return ERR_PTR(-ESRCH);
  462. return child;
  463. }
  464. #ifndef arch_ptrace_attach
  465. #define arch_ptrace_attach(child) do { } while (0)
  466. #endif
  467. #ifndef __ARCH_SYS_PTRACE
  468. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  469. {
  470. struct task_struct *child;
  471. long ret;
  472. /*
  473. * This lock_kernel fixes a subtle race with suid exec
  474. */
  475. lock_kernel();
  476. if (request == PTRACE_TRACEME) {
  477. ret = ptrace_traceme();
  478. if (!ret)
  479. arch_ptrace_attach(current);
  480. goto out;
  481. }
  482. child = ptrace_get_task_struct(pid);
  483. if (IS_ERR(child)) {
  484. ret = PTR_ERR(child);
  485. goto out;
  486. }
  487. if (request == PTRACE_ATTACH) {
  488. ret = ptrace_attach(child);
  489. /*
  490. * Some architectures need to do book-keeping after
  491. * a ptrace attach.
  492. */
  493. if (!ret)
  494. arch_ptrace_attach(child);
  495. goto out_put_task_struct;
  496. }
  497. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  498. if (ret < 0)
  499. goto out_put_task_struct;
  500. ret = arch_ptrace(child, request, addr, data);
  501. if (ret < 0)
  502. goto out_put_task_struct;
  503. out_put_task_struct:
  504. put_task_struct(child);
  505. out:
  506. unlock_kernel();
  507. return ret;
  508. }
  509. #endif /* __ARCH_SYS_PTRACE */
  510. int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
  511. {
  512. unsigned long tmp;
  513. int copied;
  514. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  515. if (copied != sizeof(tmp))
  516. return -EIO;
  517. return put_user(tmp, (unsigned long __user *)data);
  518. }
  519. int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
  520. {
  521. int copied;
  522. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  523. return (copied == sizeof(data)) ? 0 : -EIO;
  524. }
  525. #ifdef CONFIG_COMPAT
  526. #include <linux/compat.h>
  527. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  528. compat_ulong_t addr, compat_ulong_t data)
  529. {
  530. compat_ulong_t __user *datap = compat_ptr(data);
  531. compat_ulong_t word;
  532. int ret;
  533. switch (request) {
  534. case PTRACE_PEEKTEXT:
  535. case PTRACE_PEEKDATA:
  536. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  537. if (ret != sizeof(word))
  538. ret = -EIO;
  539. else
  540. ret = put_user(word, datap);
  541. break;
  542. case PTRACE_POKETEXT:
  543. case PTRACE_POKEDATA:
  544. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  545. ret = (ret != sizeof(data) ? -EIO : 0);
  546. break;
  547. case PTRACE_GETEVENTMSG:
  548. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  549. break;
  550. default:
  551. ret = ptrace_request(child, request, addr, data);
  552. }
  553. return ret;
  554. }
  555. #ifdef __ARCH_WANT_COMPAT_SYS_PTRACE
  556. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  557. compat_long_t addr, compat_long_t data)
  558. {
  559. struct task_struct *child;
  560. long ret;
  561. /*
  562. * This lock_kernel fixes a subtle race with suid exec
  563. */
  564. lock_kernel();
  565. if (request == PTRACE_TRACEME) {
  566. ret = ptrace_traceme();
  567. goto out;
  568. }
  569. child = ptrace_get_task_struct(pid);
  570. if (IS_ERR(child)) {
  571. ret = PTR_ERR(child);
  572. goto out;
  573. }
  574. if (request == PTRACE_ATTACH) {
  575. ret = ptrace_attach(child);
  576. /*
  577. * Some architectures need to do book-keeping after
  578. * a ptrace attach.
  579. */
  580. if (!ret)
  581. arch_ptrace_attach(child);
  582. goto out_put_task_struct;
  583. }
  584. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  585. if (!ret)
  586. ret = compat_arch_ptrace(child, request, addr, data);
  587. out_put_task_struct:
  588. put_task_struct(child);
  589. out:
  590. unlock_kernel();
  591. return ret;
  592. }
  593. #endif /* __ARCH_WANT_COMPAT_SYS_PTRACE */
  594. #endif /* CONFIG_COMPAT */