ptrace.c 12 KB

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