ptrace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 (!list_empty(&child->ptrace_list)) {
  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. 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;
  183. if (capable(CAP_SYS_PTRACE))
  184. task->ptrace |= PT_PTRACE_CAP;
  185. __ptrace_link(task, current);
  186. force_sig_specific(SIGSTOP, task);
  187. bad:
  188. write_unlock_irqrestore(&tasklist_lock, flags);
  189. task_unlock(task);
  190. out:
  191. return retval;
  192. }
  193. static inline void __ptrace_detach(struct task_struct *child, unsigned int data)
  194. {
  195. child->exit_code = data;
  196. /* .. re-parent .. */
  197. __ptrace_unlink(child);
  198. /* .. and wake it up. */
  199. if (child->exit_state != EXIT_ZOMBIE)
  200. wake_up_process(child);
  201. }
  202. int ptrace_detach(struct task_struct *child, unsigned int data)
  203. {
  204. if (!valid_signal(data))
  205. return -EIO;
  206. /* Architecture-specific hardware disable .. */
  207. ptrace_disable(child);
  208. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  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. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  217. {
  218. int copied = 0;
  219. while (len > 0) {
  220. char buf[128];
  221. int this_len, retval;
  222. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  223. retval = access_process_vm(tsk, src, buf, this_len, 0);
  224. if (!retval) {
  225. if (copied)
  226. break;
  227. return -EIO;
  228. }
  229. if (copy_to_user(dst, buf, retval))
  230. return -EFAULT;
  231. copied += retval;
  232. src += retval;
  233. dst += retval;
  234. len -= retval;
  235. }
  236. return copied;
  237. }
  238. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  239. {
  240. int copied = 0;
  241. while (len > 0) {
  242. char buf[128];
  243. int this_len, retval;
  244. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  245. if (copy_from_user(buf, src, this_len))
  246. return -EFAULT;
  247. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  248. if (!retval) {
  249. if (copied)
  250. break;
  251. return -EIO;
  252. }
  253. copied += retval;
  254. src += retval;
  255. dst += retval;
  256. len -= retval;
  257. }
  258. return copied;
  259. }
  260. static int ptrace_setoptions(struct task_struct *child, long data)
  261. {
  262. child->ptrace &= ~PT_TRACE_MASK;
  263. if (data & PTRACE_O_TRACESYSGOOD)
  264. child->ptrace |= PT_TRACESYSGOOD;
  265. if (data & PTRACE_O_TRACEFORK)
  266. child->ptrace |= PT_TRACE_FORK;
  267. if (data & PTRACE_O_TRACEVFORK)
  268. child->ptrace |= PT_TRACE_VFORK;
  269. if (data & PTRACE_O_TRACECLONE)
  270. child->ptrace |= PT_TRACE_CLONE;
  271. if (data & PTRACE_O_TRACEEXEC)
  272. child->ptrace |= PT_TRACE_EXEC;
  273. if (data & PTRACE_O_TRACEVFORKDONE)
  274. child->ptrace |= PT_TRACE_VFORK_DONE;
  275. if (data & PTRACE_O_TRACEEXIT)
  276. child->ptrace |= PT_TRACE_EXIT;
  277. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  278. }
  279. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  280. {
  281. siginfo_t lastinfo;
  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. lastinfo = *child->last_siginfo;
  289. error = 0;
  290. }
  291. spin_unlock_irq(&child->sighand->siglock);
  292. }
  293. read_unlock(&tasklist_lock);
  294. if (!error)
  295. return copy_siginfo_to_user(data, &lastinfo);
  296. return error;
  297. }
  298. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  299. {
  300. siginfo_t newinfo;
  301. int error = -ESRCH;
  302. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  303. return -EFAULT;
  304. read_lock(&tasklist_lock);
  305. if (likely(child->sighand != NULL)) {
  306. error = -EINVAL;
  307. spin_lock_irq(&child->sighand->siglock);
  308. if (likely(child->last_siginfo != NULL)) {
  309. *child->last_siginfo = newinfo;
  310. error = 0;
  311. }
  312. spin_unlock_irq(&child->sighand->siglock);
  313. }
  314. read_unlock(&tasklist_lock);
  315. return error;
  316. }
  317. #ifdef PTRACE_SINGLESTEP
  318. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  319. #else
  320. #define is_singlestep(request) 0
  321. #endif
  322. #ifdef PTRACE_SINGLEBLOCK
  323. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  324. #else
  325. #define is_singleblock(request) 0
  326. #endif
  327. #ifdef PTRACE_SYSEMU
  328. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  329. #else
  330. #define is_sysemu_singlestep(request) 0
  331. #endif
  332. static int ptrace_resume(struct task_struct *child, long request, long data)
  333. {
  334. if (!valid_signal(data))
  335. return -EIO;
  336. if (request == PTRACE_SYSCALL)
  337. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  338. else
  339. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  340. #ifdef TIF_SYSCALL_EMU
  341. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  342. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  343. else
  344. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  345. #endif
  346. if (is_singleblock(request)) {
  347. if (unlikely(!arch_has_block_step()))
  348. return -EIO;
  349. user_enable_block_step(child);
  350. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  351. if (unlikely(!arch_has_single_step()))
  352. return -EIO;
  353. user_enable_single_step(child);
  354. }
  355. else
  356. user_disable_single_step(child);
  357. child->exit_code = data;
  358. wake_up_process(child);
  359. return 0;
  360. }
  361. int ptrace_request(struct task_struct *child, long request,
  362. long addr, long data)
  363. {
  364. int ret = -EIO;
  365. switch (request) {
  366. case PTRACE_PEEKTEXT:
  367. case PTRACE_PEEKDATA:
  368. return generic_ptrace_peekdata(child, addr, data);
  369. case PTRACE_POKETEXT:
  370. case PTRACE_POKEDATA:
  371. return generic_ptrace_pokedata(child, addr, data);
  372. #ifdef PTRACE_OLDSETOPTIONS
  373. case PTRACE_OLDSETOPTIONS:
  374. #endif
  375. case PTRACE_SETOPTIONS:
  376. ret = ptrace_setoptions(child, data);
  377. break;
  378. case PTRACE_GETEVENTMSG:
  379. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  380. break;
  381. case PTRACE_GETSIGINFO:
  382. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  383. break;
  384. case PTRACE_SETSIGINFO:
  385. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  386. break;
  387. case PTRACE_DETACH: /* detach a process that was attached. */
  388. ret = ptrace_detach(child, data);
  389. break;
  390. #ifdef PTRACE_SINGLESTEP
  391. case PTRACE_SINGLESTEP:
  392. #endif
  393. #ifdef PTRACE_SINGLEBLOCK
  394. case PTRACE_SINGLEBLOCK:
  395. #endif
  396. #ifdef PTRACE_SYSEMU
  397. case PTRACE_SYSEMU:
  398. case PTRACE_SYSEMU_SINGLESTEP:
  399. #endif
  400. case PTRACE_SYSCALL:
  401. case PTRACE_CONT:
  402. return ptrace_resume(child, request, data);
  403. case PTRACE_KILL:
  404. if (child->exit_state) /* already dead */
  405. return 0;
  406. return ptrace_resume(child, request, SIGKILL);
  407. default:
  408. break;
  409. }
  410. return ret;
  411. }
  412. /**
  413. * ptrace_traceme -- helper for PTRACE_TRACEME
  414. *
  415. * Performs checks and sets PT_PTRACED.
  416. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  417. */
  418. int ptrace_traceme(void)
  419. {
  420. int ret = -EPERM;
  421. /*
  422. * Are we already being traced?
  423. */
  424. task_lock(current);
  425. if (!(current->ptrace & PT_PTRACED)) {
  426. ret = security_ptrace(current->parent, current);
  427. /*
  428. * Set the ptrace bit in the process ptrace flags.
  429. */
  430. if (!ret)
  431. current->ptrace |= PT_PTRACED;
  432. }
  433. task_unlock(current);
  434. return ret;
  435. }
  436. /**
  437. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  438. * @pid: process id to grab a task_struct reference of
  439. *
  440. * This function is a helper for ptrace implementations. It checks
  441. * permissions and then grabs a task struct for use of the actual
  442. * ptrace implementation.
  443. *
  444. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  445. */
  446. struct task_struct *ptrace_get_task_struct(pid_t pid)
  447. {
  448. struct task_struct *child;
  449. /*
  450. * Tracing init is not allowed.
  451. */
  452. if (pid == 1)
  453. return ERR_PTR(-EPERM);
  454. read_lock(&tasklist_lock);
  455. child = find_task_by_vpid(pid);
  456. if (child)
  457. get_task_struct(child);
  458. read_unlock(&tasklist_lock);
  459. if (!child)
  460. return ERR_PTR(-ESRCH);
  461. return child;
  462. }
  463. #ifndef arch_ptrace_attach
  464. #define arch_ptrace_attach(child) do { } while (0)
  465. #endif
  466. #ifndef __ARCH_SYS_PTRACE
  467. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  468. {
  469. struct task_struct *child;
  470. long ret;
  471. /*
  472. * This lock_kernel fixes a subtle race with suid exec
  473. */
  474. lock_kernel();
  475. if (request == PTRACE_TRACEME) {
  476. ret = ptrace_traceme();
  477. if (!ret)
  478. arch_ptrace_attach(current);
  479. goto out;
  480. }
  481. child = ptrace_get_task_struct(pid);
  482. if (IS_ERR(child)) {
  483. ret = PTR_ERR(child);
  484. goto out;
  485. }
  486. if (request == PTRACE_ATTACH) {
  487. ret = ptrace_attach(child);
  488. /*
  489. * Some architectures need to do book-keeping after
  490. * a ptrace attach.
  491. */
  492. if (!ret)
  493. arch_ptrace_attach(child);
  494. goto out_put_task_struct;
  495. }
  496. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  497. if (ret < 0)
  498. goto out_put_task_struct;
  499. ret = arch_ptrace(child, request, addr, data);
  500. if (ret < 0)
  501. goto out_put_task_struct;
  502. out_put_task_struct:
  503. put_task_struct(child);
  504. out:
  505. unlock_kernel();
  506. return ret;
  507. }
  508. #endif /* __ARCH_SYS_PTRACE */
  509. int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
  510. {
  511. unsigned long tmp;
  512. int copied;
  513. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  514. if (copied != sizeof(tmp))
  515. return -EIO;
  516. return put_user(tmp, (unsigned long __user *)data);
  517. }
  518. int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
  519. {
  520. int copied;
  521. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  522. return (copied == sizeof(data)) ? 0 : -EIO;
  523. }
  524. #ifdef CONFIG_COMPAT
  525. #include <linux/compat.h>
  526. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  527. compat_ulong_t addr, compat_ulong_t data)
  528. {
  529. compat_ulong_t __user *datap = compat_ptr(data);
  530. compat_ulong_t word;
  531. int ret;
  532. switch (request) {
  533. case PTRACE_PEEKTEXT:
  534. case PTRACE_PEEKDATA:
  535. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  536. if (ret != sizeof(word))
  537. ret = -EIO;
  538. else
  539. ret = put_user(word, datap);
  540. break;
  541. case PTRACE_POKETEXT:
  542. case PTRACE_POKEDATA:
  543. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  544. ret = (ret != sizeof(data) ? -EIO : 0);
  545. break;
  546. case PTRACE_GETEVENTMSG:
  547. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  548. break;
  549. default:
  550. ret = ptrace_request(child, request, addr, data);
  551. }
  552. return ret;
  553. }
  554. #ifdef __ARCH_WANT_COMPAT_SYS_PTRACE
  555. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  556. compat_long_t addr, compat_long_t data)
  557. {
  558. struct task_struct *child;
  559. long ret;
  560. /*
  561. * This lock_kernel fixes a subtle race with suid exec
  562. */
  563. lock_kernel();
  564. if (request == PTRACE_TRACEME) {
  565. ret = ptrace_traceme();
  566. goto out;
  567. }
  568. child = ptrace_get_task_struct(pid);
  569. if (IS_ERR(child)) {
  570. ret = PTR_ERR(child);
  571. goto out;
  572. }
  573. if (request == PTRACE_ATTACH) {
  574. ret = ptrace_attach(child);
  575. /*
  576. * Some architectures need to do book-keeping after
  577. * a ptrace attach.
  578. */
  579. if (!ret)
  580. arch_ptrace_attach(child);
  581. goto out_put_task_struct;
  582. }
  583. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  584. if (!ret)
  585. ret = compat_arch_ptrace(child, request, addr, data);
  586. out_put_task_struct:
  587. put_task_struct(child);
  588. out:
  589. unlock_kernel();
  590. return ret;
  591. }
  592. #endif /* __ARCH_WANT_COMPAT_SYS_PTRACE */
  593. #endif /* CONFIG_COMPAT */