ptrace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 <linux/syscalls.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/uaccess.h>
  25. /*
  26. * ptrace a task: make the debugger its new parent and
  27. * move it to the ptrace list.
  28. *
  29. * Must be called with the tasklist lock write-held.
  30. */
  31. void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
  32. {
  33. BUG_ON(!list_empty(&child->ptrace_list));
  34. if (child->parent == new_parent)
  35. return;
  36. list_add(&child->ptrace_list, &child->parent->ptrace_children);
  37. remove_parent(child);
  38. child->parent = new_parent;
  39. add_parent(child);
  40. }
  41. /*
  42. * Turn a tracing stop into a normal stop now, since with no tracer there
  43. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  44. * signal sent that would resume the child, but didn't because it was in
  45. * TASK_TRACED, resume it now.
  46. * Requires that irqs be disabled.
  47. */
  48. void ptrace_untrace(struct task_struct *child)
  49. {
  50. spin_lock(&child->sighand->siglock);
  51. if (task_is_traced(child)) {
  52. if (child->signal->flags & SIGNAL_STOP_STOPPED) {
  53. __set_task_state(child, TASK_STOPPED);
  54. } else {
  55. signal_wake_up(child, 1);
  56. }
  57. }
  58. spin_unlock(&child->sighand->siglock);
  59. }
  60. /*
  61. * unptrace a task: move it back to its original parent and
  62. * remove it from the ptrace list.
  63. *
  64. * Must be called with the tasklist lock write-held.
  65. */
  66. void __ptrace_unlink(struct task_struct *child)
  67. {
  68. BUG_ON(!child->ptrace);
  69. child->ptrace = 0;
  70. if (ptrace_reparented(child)) {
  71. list_del_init(&child->ptrace_list);
  72. remove_parent(child);
  73. child->parent = child->real_parent;
  74. add_parent(child);
  75. }
  76. if (task_is_traced(child))
  77. ptrace_untrace(child);
  78. }
  79. /*
  80. * Check that we have indeed attached to the thing..
  81. */
  82. int ptrace_check_attach(struct task_struct *child, int kill)
  83. {
  84. int ret = -ESRCH;
  85. /*
  86. * We take the read lock around doing both checks to close a
  87. * possible race where someone else was tracing our child and
  88. * detached between these two checks. After this locked check,
  89. * we are sure that this is our traced child and that can only
  90. * be changed by us so it's not changing right after this.
  91. */
  92. read_lock(&tasklist_lock);
  93. if ((child->ptrace & PT_PTRACED) && child->parent == current) {
  94. ret = 0;
  95. /*
  96. * child->sighand can't be NULL, release_task()
  97. * does ptrace_unlink() before __exit_signal().
  98. */
  99. spin_lock_irq(&child->sighand->siglock);
  100. if (task_is_stopped(child))
  101. child->state = TASK_TRACED;
  102. else if (!task_is_traced(child) && !kill)
  103. ret = -ESRCH;
  104. spin_unlock_irq(&child->sighand->siglock);
  105. }
  106. read_unlock(&tasklist_lock);
  107. if (!ret && !kill)
  108. wait_task_inactive(child);
  109. /* All systems go.. */
  110. return ret;
  111. }
  112. int __ptrace_may_attach(struct task_struct *task)
  113. {
  114. /* May we inspect the given task?
  115. * This check is used both for attaching with ptrace
  116. * and for allowing access to sensitive information in /proc.
  117. *
  118. * ptrace_attach denies several cases that /proc allows
  119. * because setting up the necessary parent/child relationship
  120. * or halting the specified task is impossible.
  121. */
  122. int dumpable = 0;
  123. /* Don't let security modules deny introspection */
  124. if (task == current)
  125. return 0;
  126. if (((current->uid != task->euid) ||
  127. (current->uid != task->suid) ||
  128. (current->uid != task->uid) ||
  129. (current->gid != task->egid) ||
  130. (current->gid != task->sgid) ||
  131. (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
  132. return -EPERM;
  133. smp_rmb();
  134. if (task->mm)
  135. dumpable = get_dumpable(task->mm);
  136. if (!dumpable && !capable(CAP_SYS_PTRACE))
  137. return -EPERM;
  138. return security_ptrace(current, task);
  139. }
  140. int ptrace_may_attach(struct task_struct *task)
  141. {
  142. int err;
  143. task_lock(task);
  144. err = __ptrace_may_attach(task);
  145. task_unlock(task);
  146. return !err;
  147. }
  148. int ptrace_attach(struct task_struct *task)
  149. {
  150. int retval;
  151. unsigned long flags;
  152. audit_ptrace(task);
  153. retval = -EPERM;
  154. if (same_thread_group(task, current))
  155. goto out;
  156. repeat:
  157. /*
  158. * Nasty, nasty.
  159. *
  160. * We want to hold both the task-lock and the
  161. * tasklist_lock for writing at the same time.
  162. * But that's against the rules (tasklist_lock
  163. * is taken for reading by interrupts on other
  164. * cpu's that may have task_lock).
  165. */
  166. task_lock(task);
  167. if (!write_trylock_irqsave(&tasklist_lock, flags)) {
  168. task_unlock(task);
  169. do {
  170. cpu_relax();
  171. } while (!write_can_lock(&tasklist_lock));
  172. goto repeat;
  173. }
  174. if (!task->mm)
  175. goto bad;
  176. /* the same process cannot be attached many times */
  177. if (task->ptrace & PT_PTRACED)
  178. goto bad;
  179. retval = __ptrace_may_attach(task);
  180. if (retval)
  181. goto bad;
  182. /* Go */
  183. task->ptrace |= PT_PTRACED;
  184. if (capable(CAP_SYS_PTRACE))
  185. task->ptrace |= PT_PTRACE_CAP;
  186. __ptrace_link(task, current);
  187. send_sig_info(SIGSTOP, SEND_SIG_FORCED, 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 *info)
  281. {
  282. int error = -ESRCH;
  283. read_lock(&tasklist_lock);
  284. if (likely(child->sighand != NULL)) {
  285. error = -EINVAL;
  286. spin_lock_irq(&child->sighand->siglock);
  287. if (likely(child->last_siginfo != NULL)) {
  288. *info = *child->last_siginfo;
  289. error = 0;
  290. }
  291. spin_unlock_irq(&child->sighand->siglock);
  292. }
  293. read_unlock(&tasklist_lock);
  294. return error;
  295. }
  296. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  297. {
  298. int error = -ESRCH;
  299. read_lock(&tasklist_lock);
  300. if (likely(child->sighand != NULL)) {
  301. error = -EINVAL;
  302. spin_lock_irq(&child->sighand->siglock);
  303. if (likely(child->last_siginfo != NULL)) {
  304. *child->last_siginfo = *info;
  305. error = 0;
  306. }
  307. spin_unlock_irq(&child->sighand->siglock);
  308. }
  309. read_unlock(&tasklist_lock);
  310. return error;
  311. }
  312. #ifdef PTRACE_SINGLESTEP
  313. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  314. #else
  315. #define is_singlestep(request) 0
  316. #endif
  317. #ifdef PTRACE_SINGLEBLOCK
  318. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  319. #else
  320. #define is_singleblock(request) 0
  321. #endif
  322. #ifdef PTRACE_SYSEMU
  323. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  324. #else
  325. #define is_sysemu_singlestep(request) 0
  326. #endif
  327. static int ptrace_resume(struct task_struct *child, long request, long data)
  328. {
  329. if (!valid_signal(data))
  330. return -EIO;
  331. if (request == PTRACE_SYSCALL)
  332. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  333. else
  334. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  335. #ifdef TIF_SYSCALL_EMU
  336. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  337. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  338. else
  339. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  340. #endif
  341. if (is_singleblock(request)) {
  342. if (unlikely(!arch_has_block_step()))
  343. return -EIO;
  344. user_enable_block_step(child);
  345. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  346. if (unlikely(!arch_has_single_step()))
  347. return -EIO;
  348. user_enable_single_step(child);
  349. }
  350. else
  351. user_disable_single_step(child);
  352. child->exit_code = data;
  353. wake_up_process(child);
  354. return 0;
  355. }
  356. int ptrace_request(struct task_struct *child, long request,
  357. long addr, long data)
  358. {
  359. int ret = -EIO;
  360. siginfo_t siginfo;
  361. switch (request) {
  362. case PTRACE_PEEKTEXT:
  363. case PTRACE_PEEKDATA:
  364. return generic_ptrace_peekdata(child, addr, data);
  365. case PTRACE_POKETEXT:
  366. case PTRACE_POKEDATA:
  367. return generic_ptrace_pokedata(child, addr, data);
  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);
  379. if (!ret)
  380. ret = copy_siginfo_to_user((siginfo_t __user *) data,
  381. &siginfo);
  382. break;
  383. case PTRACE_SETSIGINFO:
  384. if (copy_from_user(&siginfo, (siginfo_t __user *) data,
  385. sizeof siginfo))
  386. ret = -EFAULT;
  387. else
  388. ret = ptrace_setsiginfo(child, &siginfo);
  389. break;
  390. case PTRACE_DETACH: /* detach a process that was attached. */
  391. ret = ptrace_detach(child, data);
  392. break;
  393. #ifdef PTRACE_SINGLESTEP
  394. case PTRACE_SINGLESTEP:
  395. #endif
  396. #ifdef PTRACE_SINGLEBLOCK
  397. case PTRACE_SINGLEBLOCK:
  398. #endif
  399. #ifdef PTRACE_SYSEMU
  400. case PTRACE_SYSEMU:
  401. case PTRACE_SYSEMU_SINGLESTEP:
  402. #endif
  403. case PTRACE_SYSCALL:
  404. case PTRACE_CONT:
  405. return ptrace_resume(child, request, data);
  406. case PTRACE_KILL:
  407. if (child->exit_state) /* already dead */
  408. return 0;
  409. return ptrace_resume(child, request, SIGKILL);
  410. default:
  411. break;
  412. }
  413. return ret;
  414. }
  415. /**
  416. * ptrace_traceme -- helper for PTRACE_TRACEME
  417. *
  418. * Performs checks and sets PT_PTRACED.
  419. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  420. */
  421. int ptrace_traceme(void)
  422. {
  423. int ret = -EPERM;
  424. /*
  425. * Are we already being traced?
  426. */
  427. task_lock(current);
  428. if (!(current->ptrace & PT_PTRACED)) {
  429. ret = security_ptrace(current->parent, current);
  430. /*
  431. * Set the ptrace bit in the process ptrace flags.
  432. */
  433. if (!ret)
  434. current->ptrace |= PT_PTRACED;
  435. }
  436. task_unlock(current);
  437. return ret;
  438. }
  439. /**
  440. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  441. * @pid: process id to grab a task_struct reference of
  442. *
  443. * This function is a helper for ptrace implementations. It checks
  444. * permissions and then grabs a task struct for use of the actual
  445. * ptrace implementation.
  446. *
  447. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  448. */
  449. struct task_struct *ptrace_get_task_struct(pid_t pid)
  450. {
  451. struct task_struct *child;
  452. read_lock(&tasklist_lock);
  453. child = find_task_by_vpid(pid);
  454. if (child)
  455. get_task_struct(child);
  456. read_unlock(&tasklist_lock);
  457. if (!child)
  458. return ERR_PTR(-ESRCH);
  459. return child;
  460. }
  461. #ifndef arch_ptrace_attach
  462. #define arch_ptrace_attach(child) do { } while (0)
  463. #endif
  464. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  465. {
  466. struct task_struct *child;
  467. long ret;
  468. /*
  469. * This lock_kernel fixes a subtle race with suid exec
  470. */
  471. lock_kernel();
  472. if (request == PTRACE_TRACEME) {
  473. ret = ptrace_traceme();
  474. if (!ret)
  475. arch_ptrace_attach(current);
  476. goto out;
  477. }
  478. child = ptrace_get_task_struct(pid);
  479. if (IS_ERR(child)) {
  480. ret = PTR_ERR(child);
  481. goto out;
  482. }
  483. if (request == PTRACE_ATTACH) {
  484. ret = ptrace_attach(child);
  485. /*
  486. * Some architectures need to do book-keeping after
  487. * a ptrace attach.
  488. */
  489. if (!ret)
  490. arch_ptrace_attach(child);
  491. goto out_put_task_struct;
  492. }
  493. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  494. if (ret < 0)
  495. goto out_put_task_struct;
  496. ret = arch_ptrace(child, request, addr, data);
  497. if (ret < 0)
  498. goto out_put_task_struct;
  499. out_put_task_struct:
  500. put_task_struct(child);
  501. out:
  502. unlock_kernel();
  503. return ret;
  504. }
  505. int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
  506. {
  507. unsigned long tmp;
  508. int copied;
  509. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  510. if (copied != sizeof(tmp))
  511. return -EIO;
  512. return put_user(tmp, (unsigned long __user *)data);
  513. }
  514. int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
  515. {
  516. int copied;
  517. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  518. return (copied == sizeof(data)) ? 0 : -EIO;
  519. }
  520. #if defined CONFIG_COMPAT && defined __ARCH_WANT_COMPAT_SYS_PTRACE
  521. #include <linux/compat.h>
  522. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  523. compat_ulong_t addr, compat_ulong_t data)
  524. {
  525. compat_ulong_t __user *datap = compat_ptr(data);
  526. compat_ulong_t word;
  527. siginfo_t siginfo;
  528. int ret;
  529. switch (request) {
  530. case PTRACE_PEEKTEXT:
  531. case PTRACE_PEEKDATA:
  532. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  533. if (ret != sizeof(word))
  534. ret = -EIO;
  535. else
  536. ret = put_user(word, datap);
  537. break;
  538. case PTRACE_POKETEXT:
  539. case PTRACE_POKEDATA:
  540. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  541. ret = (ret != sizeof(data) ? -EIO : 0);
  542. break;
  543. case PTRACE_GETEVENTMSG:
  544. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  545. break;
  546. case PTRACE_GETSIGINFO:
  547. ret = ptrace_getsiginfo(child, &siginfo);
  548. if (!ret)
  549. ret = copy_siginfo_to_user32(
  550. (struct compat_siginfo __user *) datap,
  551. &siginfo);
  552. break;
  553. case PTRACE_SETSIGINFO:
  554. memset(&siginfo, 0, sizeof siginfo);
  555. if (copy_siginfo_from_user32(
  556. &siginfo, (struct compat_siginfo __user *) datap))
  557. ret = -EFAULT;
  558. else
  559. ret = ptrace_setsiginfo(child, &siginfo);
  560. break;
  561. default:
  562. ret = ptrace_request(child, request, addr, data);
  563. }
  564. return ret;
  565. }
  566. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  567. compat_long_t addr, compat_long_t data)
  568. {
  569. struct task_struct *child;
  570. long ret;
  571. /*
  572. * This lock_kernel fixes a subtle race with suid exec
  573. */
  574. lock_kernel();
  575. if (request == PTRACE_TRACEME) {
  576. ret = ptrace_traceme();
  577. goto out;
  578. }
  579. child = ptrace_get_task_struct(pid);
  580. if (IS_ERR(child)) {
  581. ret = PTR_ERR(child);
  582. goto out;
  583. }
  584. if (request == PTRACE_ATTACH) {
  585. ret = ptrace_attach(child);
  586. /*
  587. * Some architectures need to do book-keeping after
  588. * a ptrace attach.
  589. */
  590. if (!ret)
  591. arch_ptrace_attach(child);
  592. goto out_put_task_struct;
  593. }
  594. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  595. if (!ret)
  596. ret = compat_arch_ptrace(child, request, addr, data);
  597. out_put_task_struct:
  598. put_task_struct(child);
  599. out:
  600. unlock_kernel();
  601. return ret;
  602. }
  603. #endif /* CONFIG_COMPAT && __ARCH_WANT_COMPAT_SYS_PTRACE */