ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 <asm/pgtable.h>
  22. #include <asm/uaccess.h>
  23. /*
  24. * ptrace a task: make the debugger its new parent and
  25. * move it to the ptrace list.
  26. *
  27. * Must be called with the tasklist lock write-held.
  28. */
  29. void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
  30. {
  31. BUG_ON(!list_empty(&child->ptrace_list));
  32. if (child->parent == new_parent)
  33. return;
  34. list_add(&child->ptrace_list, &child->parent->ptrace_children);
  35. remove_parent(child);
  36. child->parent = new_parent;
  37. add_parent(child);
  38. }
  39. /*
  40. * Turn a tracing stop into a normal stop now, since with no tracer there
  41. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  42. * signal sent that would resume the child, but didn't because it was in
  43. * TASK_TRACED, resume it now.
  44. * Requires that irqs be disabled.
  45. */
  46. void ptrace_untrace(struct task_struct *child)
  47. {
  48. spin_lock(&child->sighand->siglock);
  49. if (child->state == TASK_TRACED) {
  50. if (child->signal->flags & SIGNAL_STOP_STOPPED) {
  51. child->state = TASK_STOPPED;
  52. } else {
  53. signal_wake_up(child, 1);
  54. }
  55. }
  56. spin_unlock(&child->sighand->siglock);
  57. }
  58. /*
  59. * unptrace a task: move it back to its original parent and
  60. * remove it from the ptrace list.
  61. *
  62. * Must be called with the tasklist lock write-held.
  63. */
  64. void __ptrace_unlink(struct task_struct *child)
  65. {
  66. BUG_ON(!child->ptrace);
  67. child->ptrace = 0;
  68. if (!list_empty(&child->ptrace_list)) {
  69. list_del_init(&child->ptrace_list);
  70. remove_parent(child);
  71. child->parent = child->real_parent;
  72. add_parent(child);
  73. }
  74. if (child->state == TASK_TRACED)
  75. ptrace_untrace(child);
  76. }
  77. /*
  78. * Check that we have indeed attached to the thing..
  79. */
  80. int ptrace_check_attach(struct task_struct *child, int kill)
  81. {
  82. int ret = -ESRCH;
  83. /*
  84. * We take the read lock around doing both checks to close a
  85. * possible race where someone else was tracing our child and
  86. * detached between these two checks. After this locked check,
  87. * we are sure that this is our traced child and that can only
  88. * be changed by us so it's not changing right after this.
  89. */
  90. read_lock(&tasklist_lock);
  91. if ((child->ptrace & PT_PTRACED) && child->parent == current &&
  92. (!(child->ptrace & PT_ATTACHED) || child->real_parent != current)
  93. && child->signal != NULL) {
  94. ret = 0;
  95. spin_lock_irq(&child->sighand->siglock);
  96. if (child->state == TASK_STOPPED) {
  97. child->state = TASK_TRACED;
  98. } else if (child->state != TASK_TRACED && !kill) {
  99. ret = -ESRCH;
  100. }
  101. spin_unlock_irq(&child->sighand->siglock);
  102. }
  103. read_unlock(&tasklist_lock);
  104. if (!ret && !kill) {
  105. wait_task_inactive(child);
  106. }
  107. /* All systems go.. */
  108. return ret;
  109. }
  110. static int may_attach(struct task_struct *task)
  111. {
  112. /* May we inspect the given task?
  113. * This check is used both for attaching with ptrace
  114. * and for allowing access to sensitive information in /proc.
  115. *
  116. * ptrace_attach denies several cases that /proc allows
  117. * because setting up the necessary parent/child relationship
  118. * or halting the specified task is impossible.
  119. */
  120. int dumpable = 0;
  121. /* Don't let security modules deny introspection */
  122. if (task == current)
  123. return 0;
  124. if (((current->uid != task->euid) ||
  125. (current->uid != task->suid) ||
  126. (current->uid != task->uid) ||
  127. (current->gid != task->egid) ||
  128. (current->gid != task->sgid) ||
  129. (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
  130. return -EPERM;
  131. smp_rmb();
  132. if (task->mm)
  133. dumpable = get_dumpable(task->mm);
  134. if (!dumpable && !capable(CAP_SYS_PTRACE))
  135. return -EPERM;
  136. return security_ptrace(current, task);
  137. }
  138. int ptrace_may_attach(struct task_struct *task)
  139. {
  140. int err;
  141. task_lock(task);
  142. err = may_attach(task);
  143. task_unlock(task);
  144. return !err;
  145. }
  146. int ptrace_attach(struct task_struct *task)
  147. {
  148. int retval;
  149. unsigned long flags;
  150. audit_ptrace(task);
  151. retval = -EPERM;
  152. if (task->pid <= 1)
  153. goto out;
  154. if (task->tgid == current->tgid)
  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 = may_attach(task);
  180. if (retval)
  181. goto bad;
  182. /* Go */
  183. task->ptrace |= PT_PTRACED | ((task->real_parent != current)
  184. ? PT_ATTACHED : 0);
  185. if (capable(CAP_SYS_PTRACE))
  186. task->ptrace |= PT_PTRACE_CAP;
  187. __ptrace_link(task, current);
  188. force_sig_specific(SIGSTOP, task);
  189. bad:
  190. write_unlock_irqrestore(&tasklist_lock, flags);
  191. task_unlock(task);
  192. out:
  193. return retval;
  194. }
  195. static inline void __ptrace_detach(struct task_struct *child, unsigned int data)
  196. {
  197. child->exit_code = data;
  198. /* .. re-parent .. */
  199. __ptrace_unlink(child);
  200. /* .. and wake it up. */
  201. if (child->exit_state != EXIT_ZOMBIE)
  202. wake_up_process(child);
  203. }
  204. int ptrace_detach(struct task_struct *child, unsigned int data)
  205. {
  206. if (!valid_signal(data))
  207. return -EIO;
  208. /* Architecture-specific hardware disable .. */
  209. ptrace_disable(child);
  210. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  211. write_lock_irq(&tasklist_lock);
  212. /* protect against de_thread()->release_task() */
  213. if (child->ptrace)
  214. __ptrace_detach(child, data);
  215. write_unlock_irq(&tasklist_lock);
  216. return 0;
  217. }
  218. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  219. {
  220. int copied = 0;
  221. while (len > 0) {
  222. char buf[128];
  223. int this_len, retval;
  224. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  225. retval = access_process_vm(tsk, src, buf, this_len, 0);
  226. if (!retval) {
  227. if (copied)
  228. break;
  229. return -EIO;
  230. }
  231. if (copy_to_user(dst, buf, retval))
  232. return -EFAULT;
  233. copied += retval;
  234. src += retval;
  235. dst += retval;
  236. len -= retval;
  237. }
  238. return copied;
  239. }
  240. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  241. {
  242. int copied = 0;
  243. while (len > 0) {
  244. char buf[128];
  245. int this_len, retval;
  246. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  247. if (copy_from_user(buf, src, this_len))
  248. return -EFAULT;
  249. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  250. if (!retval) {
  251. if (copied)
  252. break;
  253. return -EIO;
  254. }
  255. copied += retval;
  256. src += retval;
  257. dst += retval;
  258. len -= retval;
  259. }
  260. return copied;
  261. }
  262. static int ptrace_setoptions(struct task_struct *child, long data)
  263. {
  264. child->ptrace &= ~PT_TRACE_MASK;
  265. if (data & PTRACE_O_TRACESYSGOOD)
  266. child->ptrace |= PT_TRACESYSGOOD;
  267. if (data & PTRACE_O_TRACEFORK)
  268. child->ptrace |= PT_TRACE_FORK;
  269. if (data & PTRACE_O_TRACEVFORK)
  270. child->ptrace |= PT_TRACE_VFORK;
  271. if (data & PTRACE_O_TRACECLONE)
  272. child->ptrace |= PT_TRACE_CLONE;
  273. if (data & PTRACE_O_TRACEEXEC)
  274. child->ptrace |= PT_TRACE_EXEC;
  275. if (data & PTRACE_O_TRACEVFORKDONE)
  276. child->ptrace |= PT_TRACE_VFORK_DONE;
  277. if (data & PTRACE_O_TRACEEXIT)
  278. child->ptrace |= PT_TRACE_EXIT;
  279. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  280. }
  281. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
  282. {
  283. siginfo_t lastinfo;
  284. int error = -ESRCH;
  285. read_lock(&tasklist_lock);
  286. if (likely(child->sighand != NULL)) {
  287. error = -EINVAL;
  288. spin_lock_irq(&child->sighand->siglock);
  289. if (likely(child->last_siginfo != NULL)) {
  290. lastinfo = *child->last_siginfo;
  291. error = 0;
  292. }
  293. spin_unlock_irq(&child->sighand->siglock);
  294. }
  295. read_unlock(&tasklist_lock);
  296. if (!error)
  297. return copy_siginfo_to_user(data, &lastinfo);
  298. return error;
  299. }
  300. static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
  301. {
  302. siginfo_t newinfo;
  303. int error = -ESRCH;
  304. if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
  305. return -EFAULT;
  306. read_lock(&tasklist_lock);
  307. if (likely(child->sighand != NULL)) {
  308. error = -EINVAL;
  309. spin_lock_irq(&child->sighand->siglock);
  310. if (likely(child->last_siginfo != NULL)) {
  311. *child->last_siginfo = newinfo;
  312. error = 0;
  313. }
  314. spin_unlock_irq(&child->sighand->siglock);
  315. }
  316. read_unlock(&tasklist_lock);
  317. return error;
  318. }
  319. int ptrace_request(struct task_struct *child, long request,
  320. long addr, long data)
  321. {
  322. int ret = -EIO;
  323. switch (request) {
  324. #ifdef PTRACE_OLDSETOPTIONS
  325. case PTRACE_OLDSETOPTIONS:
  326. #endif
  327. case PTRACE_SETOPTIONS:
  328. ret = ptrace_setoptions(child, data);
  329. break;
  330. case PTRACE_GETEVENTMSG:
  331. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  332. break;
  333. case PTRACE_GETSIGINFO:
  334. ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
  335. break;
  336. case PTRACE_SETSIGINFO:
  337. ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
  338. break;
  339. default:
  340. break;
  341. }
  342. return ret;
  343. }
  344. /**
  345. * ptrace_traceme -- helper for PTRACE_TRACEME
  346. *
  347. * Performs checks and sets PT_PTRACED.
  348. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  349. */
  350. int ptrace_traceme(void)
  351. {
  352. int ret = -EPERM;
  353. /*
  354. * Are we already being traced?
  355. */
  356. task_lock(current);
  357. if (!(current->ptrace & PT_PTRACED)) {
  358. ret = security_ptrace(current->parent, current);
  359. /*
  360. * Set the ptrace bit in the process ptrace flags.
  361. */
  362. if (!ret)
  363. current->ptrace |= PT_PTRACED;
  364. }
  365. task_unlock(current);
  366. return ret;
  367. }
  368. /**
  369. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  370. * @pid: process id to grab a task_struct reference of
  371. *
  372. * This function is a helper for ptrace implementations. It checks
  373. * permissions and then grabs a task struct for use of the actual
  374. * ptrace implementation.
  375. *
  376. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  377. */
  378. struct task_struct *ptrace_get_task_struct(pid_t pid)
  379. {
  380. struct task_struct *child;
  381. /*
  382. * Tracing init is not allowed.
  383. */
  384. if (pid == 1)
  385. return ERR_PTR(-EPERM);
  386. read_lock(&tasklist_lock);
  387. child = find_task_by_pid(pid);
  388. if (child)
  389. get_task_struct(child);
  390. read_unlock(&tasklist_lock);
  391. if (!child)
  392. return ERR_PTR(-ESRCH);
  393. return child;
  394. }
  395. #ifndef __ARCH_SYS_PTRACE
  396. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  397. {
  398. struct task_struct *child;
  399. long ret;
  400. /*
  401. * This lock_kernel fixes a subtle race with suid exec
  402. */
  403. lock_kernel();
  404. if (request == PTRACE_TRACEME) {
  405. ret = ptrace_traceme();
  406. goto out;
  407. }
  408. child = ptrace_get_task_struct(pid);
  409. if (IS_ERR(child)) {
  410. ret = PTR_ERR(child);
  411. goto out;
  412. }
  413. if (request == PTRACE_ATTACH) {
  414. ret = ptrace_attach(child);
  415. goto out_put_task_struct;
  416. }
  417. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  418. if (ret < 0)
  419. goto out_put_task_struct;
  420. ret = arch_ptrace(child, request, addr, data);
  421. if (ret < 0)
  422. goto out_put_task_struct;
  423. out_put_task_struct:
  424. put_task_struct(child);
  425. out:
  426. unlock_kernel();
  427. return ret;
  428. }
  429. #endif /* __ARCH_SYS_PTRACE */
  430. int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
  431. {
  432. unsigned long tmp;
  433. int copied;
  434. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  435. if (copied != sizeof(tmp))
  436. return -EIO;
  437. return put_user(tmp, (unsigned long __user *)data);
  438. }
  439. int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
  440. {
  441. int copied;
  442. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  443. return (copied == sizeof(data)) ? 0 : -EIO;
  444. }