ptrace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 <linux/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_entry));
  33. list_add(&child->ptrace_entry, &new_parent->ptraced);
  34. child->parent = new_parent;
  35. }
  36. /*
  37. * Turn a tracing stop into a normal stop now, since with no tracer there
  38. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  39. * signal sent that would resume the child, but didn't because it was in
  40. * TASK_TRACED, resume it now.
  41. * Requires that irqs be disabled.
  42. */
  43. static void ptrace_untrace(struct task_struct *child)
  44. {
  45. spin_lock(&child->sighand->siglock);
  46. if (task_is_traced(child)) {
  47. /*
  48. * If the group stop is completed or in progress,
  49. * this thread was already counted as stopped.
  50. */
  51. if (child->signal->flags & SIGNAL_STOP_STOPPED ||
  52. child->signal->group_stop_count)
  53. __set_task_state(child, TASK_STOPPED);
  54. else
  55. signal_wake_up(child, 1);
  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. child->parent = child->real_parent;
  70. list_del_init(&child->ptrace_entry);
  71. arch_ptrace_untrace(child);
  72. if (task_is_traced(child))
  73. ptrace_untrace(child);
  74. }
  75. /*
  76. * Check that we have indeed attached to the thing..
  77. */
  78. int ptrace_check_attach(struct task_struct *child, int kill)
  79. {
  80. int ret = -ESRCH;
  81. /*
  82. * We take the read lock around doing both checks to close a
  83. * possible race where someone else was tracing our child and
  84. * detached between these two checks. After this locked check,
  85. * we are sure that this is our traced child and that can only
  86. * be changed by us so it's not changing right after this.
  87. */
  88. read_lock(&tasklist_lock);
  89. if ((child->ptrace & PT_PTRACED) && child->parent == current) {
  90. ret = 0;
  91. /*
  92. * child->sighand can't be NULL, release_task()
  93. * does ptrace_unlink() before __exit_signal().
  94. */
  95. spin_lock_irq(&child->sighand->siglock);
  96. if (task_is_stopped(child))
  97. child->state = TASK_TRACED;
  98. else if (!task_is_traced(child) && !kill)
  99. ret = -ESRCH;
  100. spin_unlock_irq(&child->sighand->siglock);
  101. }
  102. read_unlock(&tasklist_lock);
  103. if (!ret && !kill)
  104. ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
  105. /* All systems go.. */
  106. return ret;
  107. }
  108. int __ptrace_may_access(struct task_struct *task, unsigned int mode)
  109. {
  110. const struct cred *cred = current_cred(), *tcred;
  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. rcu_read_lock();
  124. tcred = __task_cred(task);
  125. if ((cred->uid != tcred->euid ||
  126. cred->uid != tcred->suid ||
  127. cred->uid != tcred->uid ||
  128. cred->gid != tcred->egid ||
  129. cred->gid != tcred->sgid ||
  130. cred->gid != tcred->gid) &&
  131. !capable(CAP_SYS_PTRACE)) {
  132. rcu_read_unlock();
  133. return -EPERM;
  134. }
  135. rcu_read_unlock();
  136. smp_rmb();
  137. if (task->mm)
  138. dumpable = get_dumpable(task->mm);
  139. if (!dumpable && !capable(CAP_SYS_PTRACE))
  140. return -EPERM;
  141. return security_ptrace_may_access(task, mode);
  142. }
  143. bool ptrace_may_access(struct task_struct *task, unsigned int mode)
  144. {
  145. int err;
  146. task_lock(task);
  147. err = __ptrace_may_access(task, mode);
  148. task_unlock(task);
  149. return !err;
  150. }
  151. int ptrace_attach(struct task_struct *task)
  152. {
  153. int retval;
  154. unsigned long flags;
  155. audit_ptrace(task);
  156. retval = -EPERM;
  157. if (same_thread_group(task, current))
  158. goto out;
  159. /* Protect exec's credential calculations against our interference;
  160. * SUID, SGID and LSM creds get determined differently under ptrace.
  161. */
  162. retval = mutex_lock_interruptible(&task->cred_exec_mutex);
  163. if (retval < 0)
  164. goto out;
  165. retval = -EPERM;
  166. repeat:
  167. /*
  168. * Nasty, nasty.
  169. *
  170. * We want to hold both the task-lock and the
  171. * tasklist_lock for writing at the same time.
  172. * But that's against the rules (tasklist_lock
  173. * is taken for reading by interrupts on other
  174. * cpu's that may have task_lock).
  175. */
  176. task_lock(task);
  177. if (!write_trylock_irqsave(&tasklist_lock, flags)) {
  178. task_unlock(task);
  179. do {
  180. cpu_relax();
  181. } while (!write_can_lock(&tasklist_lock));
  182. goto repeat;
  183. }
  184. if (!task->mm)
  185. goto bad;
  186. /* the same process cannot be attached many times */
  187. if (task->ptrace & PT_PTRACED)
  188. goto bad;
  189. retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
  190. if (retval)
  191. goto bad;
  192. /* Go */
  193. task->ptrace |= PT_PTRACED;
  194. if (capable(CAP_SYS_PTRACE))
  195. task->ptrace |= PT_PTRACE_CAP;
  196. __ptrace_link(task, current);
  197. send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
  198. bad:
  199. write_unlock_irqrestore(&tasklist_lock, flags);
  200. task_unlock(task);
  201. mutex_unlock(&task->cred_exec_mutex);
  202. out:
  203. return retval;
  204. }
  205. /*
  206. * Called with irqs disabled, returns true if childs should reap themselves.
  207. */
  208. static int ignoring_children(struct sighand_struct *sigh)
  209. {
  210. int ret;
  211. spin_lock(&sigh->siglock);
  212. ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
  213. (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
  214. spin_unlock(&sigh->siglock);
  215. return ret;
  216. }
  217. /*
  218. * Called with tasklist_lock held for writing.
  219. * Unlink a traced task, and clean it up if it was a traced zombie.
  220. * Return true if it needs to be reaped with release_task().
  221. * (We can't call release_task() here because we already hold tasklist_lock.)
  222. *
  223. * If it's a zombie, our attachedness prevented normal parent notification
  224. * or self-reaping. Do notification now if it would have happened earlier.
  225. * If it should reap itself, return true.
  226. *
  227. * If it's our own child, there is no notification to do.
  228. * But if our normal children self-reap, then this child
  229. * was prevented by ptrace and we must reap it now.
  230. */
  231. static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
  232. {
  233. __ptrace_unlink(p);
  234. if (p->exit_state == EXIT_ZOMBIE) {
  235. if (!task_detached(p) && thread_group_empty(p)) {
  236. if (!same_thread_group(p->real_parent, tracer))
  237. do_notify_parent(p, p->exit_signal);
  238. else if (ignoring_children(tracer->sighand))
  239. p->exit_signal = -1;
  240. }
  241. if (task_detached(p)) {
  242. /* Mark it as in the process of being reaped. */
  243. p->exit_state = EXIT_DEAD;
  244. return true;
  245. }
  246. }
  247. return false;
  248. }
  249. int ptrace_detach(struct task_struct *child, unsigned int data)
  250. {
  251. bool dead = false;
  252. if (!valid_signal(data))
  253. return -EIO;
  254. /* Architecture-specific hardware disable .. */
  255. ptrace_disable(child);
  256. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  257. write_lock_irq(&tasklist_lock);
  258. /*
  259. * This child can be already killed. Make sure de_thread() or
  260. * our sub-thread doing do_wait() didn't do release_task() yet.
  261. */
  262. if (child->ptrace) {
  263. child->exit_code = data;
  264. dead = __ptrace_detach(current, child);
  265. if (!child->exit_state)
  266. wake_up_process(child);
  267. }
  268. write_unlock_irq(&tasklist_lock);
  269. if (unlikely(dead))
  270. release_task(child);
  271. return 0;
  272. }
  273. /*
  274. * Detach all tasks we were using ptrace on.
  275. */
  276. void exit_ptrace(struct task_struct *tracer)
  277. {
  278. struct task_struct *p, *n;
  279. LIST_HEAD(ptrace_dead);
  280. write_lock_irq(&tasklist_lock);
  281. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  282. if (__ptrace_detach(tracer, p))
  283. list_add(&p->ptrace_entry, &ptrace_dead);
  284. }
  285. write_unlock_irq(&tasklist_lock);
  286. BUG_ON(!list_empty(&tracer->ptraced));
  287. list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
  288. list_del_init(&p->ptrace_entry);
  289. release_task(p);
  290. }
  291. }
  292. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  293. {
  294. int copied = 0;
  295. while (len > 0) {
  296. char buf[128];
  297. int this_len, retval;
  298. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  299. retval = access_process_vm(tsk, src, buf, this_len, 0);
  300. if (!retval) {
  301. if (copied)
  302. break;
  303. return -EIO;
  304. }
  305. if (copy_to_user(dst, buf, retval))
  306. return -EFAULT;
  307. copied += retval;
  308. src += retval;
  309. dst += retval;
  310. len -= retval;
  311. }
  312. return copied;
  313. }
  314. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  315. {
  316. int copied = 0;
  317. while (len > 0) {
  318. char buf[128];
  319. int this_len, retval;
  320. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  321. if (copy_from_user(buf, src, this_len))
  322. return -EFAULT;
  323. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  324. if (!retval) {
  325. if (copied)
  326. break;
  327. return -EIO;
  328. }
  329. copied += retval;
  330. src += retval;
  331. dst += retval;
  332. len -= retval;
  333. }
  334. return copied;
  335. }
  336. static int ptrace_setoptions(struct task_struct *child, long data)
  337. {
  338. child->ptrace &= ~PT_TRACE_MASK;
  339. if (data & PTRACE_O_TRACESYSGOOD)
  340. child->ptrace |= PT_TRACESYSGOOD;
  341. if (data & PTRACE_O_TRACEFORK)
  342. child->ptrace |= PT_TRACE_FORK;
  343. if (data & PTRACE_O_TRACEVFORK)
  344. child->ptrace |= PT_TRACE_VFORK;
  345. if (data & PTRACE_O_TRACECLONE)
  346. child->ptrace |= PT_TRACE_CLONE;
  347. if (data & PTRACE_O_TRACEEXEC)
  348. child->ptrace |= PT_TRACE_EXEC;
  349. if (data & PTRACE_O_TRACEVFORKDONE)
  350. child->ptrace |= PT_TRACE_VFORK_DONE;
  351. if (data & PTRACE_O_TRACEEXIT)
  352. child->ptrace |= PT_TRACE_EXIT;
  353. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  354. }
  355. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  356. {
  357. int error = -ESRCH;
  358. read_lock(&tasklist_lock);
  359. if (likely(child->sighand != NULL)) {
  360. error = -EINVAL;
  361. spin_lock_irq(&child->sighand->siglock);
  362. if (likely(child->last_siginfo != NULL)) {
  363. *info = *child->last_siginfo;
  364. error = 0;
  365. }
  366. spin_unlock_irq(&child->sighand->siglock);
  367. }
  368. read_unlock(&tasklist_lock);
  369. return error;
  370. }
  371. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  372. {
  373. int error = -ESRCH;
  374. read_lock(&tasklist_lock);
  375. if (likely(child->sighand != NULL)) {
  376. error = -EINVAL;
  377. spin_lock_irq(&child->sighand->siglock);
  378. if (likely(child->last_siginfo != NULL)) {
  379. *child->last_siginfo = *info;
  380. error = 0;
  381. }
  382. spin_unlock_irq(&child->sighand->siglock);
  383. }
  384. read_unlock(&tasklist_lock);
  385. return error;
  386. }
  387. #ifdef PTRACE_SINGLESTEP
  388. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  389. #else
  390. #define is_singlestep(request) 0
  391. #endif
  392. #ifdef PTRACE_SINGLEBLOCK
  393. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  394. #else
  395. #define is_singleblock(request) 0
  396. #endif
  397. #ifdef PTRACE_SYSEMU
  398. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  399. #else
  400. #define is_sysemu_singlestep(request) 0
  401. #endif
  402. static int ptrace_resume(struct task_struct *child, long request, long data)
  403. {
  404. if (!valid_signal(data))
  405. return -EIO;
  406. if (request == PTRACE_SYSCALL)
  407. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  408. else
  409. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  410. #ifdef TIF_SYSCALL_EMU
  411. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  412. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  413. else
  414. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  415. #endif
  416. if (is_singleblock(request)) {
  417. if (unlikely(!arch_has_block_step()))
  418. return -EIO;
  419. user_enable_block_step(child);
  420. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  421. if (unlikely(!arch_has_single_step()))
  422. return -EIO;
  423. user_enable_single_step(child);
  424. } else {
  425. user_disable_single_step(child);
  426. }
  427. child->exit_code = data;
  428. wake_up_process(child);
  429. return 0;
  430. }
  431. int ptrace_request(struct task_struct *child, long request,
  432. long addr, long data)
  433. {
  434. int ret = -EIO;
  435. siginfo_t siginfo;
  436. switch (request) {
  437. case PTRACE_PEEKTEXT:
  438. case PTRACE_PEEKDATA:
  439. return generic_ptrace_peekdata(child, addr, data);
  440. case PTRACE_POKETEXT:
  441. case PTRACE_POKEDATA:
  442. return generic_ptrace_pokedata(child, addr, data);
  443. #ifdef PTRACE_OLDSETOPTIONS
  444. case PTRACE_OLDSETOPTIONS:
  445. #endif
  446. case PTRACE_SETOPTIONS:
  447. ret = ptrace_setoptions(child, data);
  448. break;
  449. case PTRACE_GETEVENTMSG:
  450. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  451. break;
  452. case PTRACE_GETSIGINFO:
  453. ret = ptrace_getsiginfo(child, &siginfo);
  454. if (!ret)
  455. ret = copy_siginfo_to_user((siginfo_t __user *) data,
  456. &siginfo);
  457. break;
  458. case PTRACE_SETSIGINFO:
  459. if (copy_from_user(&siginfo, (siginfo_t __user *) data,
  460. sizeof siginfo))
  461. ret = -EFAULT;
  462. else
  463. ret = ptrace_setsiginfo(child, &siginfo);
  464. break;
  465. case PTRACE_DETACH: /* detach a process that was attached. */
  466. ret = ptrace_detach(child, data);
  467. break;
  468. #ifdef PTRACE_SINGLESTEP
  469. case PTRACE_SINGLESTEP:
  470. #endif
  471. #ifdef PTRACE_SINGLEBLOCK
  472. case PTRACE_SINGLEBLOCK:
  473. #endif
  474. #ifdef PTRACE_SYSEMU
  475. case PTRACE_SYSEMU:
  476. case PTRACE_SYSEMU_SINGLESTEP:
  477. #endif
  478. case PTRACE_SYSCALL:
  479. case PTRACE_CONT:
  480. return ptrace_resume(child, request, data);
  481. case PTRACE_KILL:
  482. if (child->exit_state) /* already dead */
  483. return 0;
  484. return ptrace_resume(child, request, SIGKILL);
  485. default:
  486. break;
  487. }
  488. return ret;
  489. }
  490. /**
  491. * ptrace_traceme -- helper for PTRACE_TRACEME
  492. *
  493. * Performs checks and sets PT_PTRACED.
  494. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  495. */
  496. int ptrace_traceme(void)
  497. {
  498. int ret = -EPERM;
  499. /*
  500. * Are we already being traced?
  501. */
  502. repeat:
  503. task_lock(current);
  504. if (!(current->ptrace & PT_PTRACED)) {
  505. /*
  506. * See ptrace_attach() comments about the locking here.
  507. */
  508. unsigned long flags;
  509. if (!write_trylock_irqsave(&tasklist_lock, flags)) {
  510. task_unlock(current);
  511. do {
  512. cpu_relax();
  513. } while (!write_can_lock(&tasklist_lock));
  514. goto repeat;
  515. }
  516. ret = security_ptrace_traceme(current->parent);
  517. /*
  518. * Check PF_EXITING to ensure ->real_parent has not passed
  519. * exit_ptrace(). Otherwise we don't report the error but
  520. * pretend ->real_parent untraces us right after return.
  521. */
  522. if (!ret && !(current->real_parent->flags & PF_EXITING)) {
  523. current->ptrace |= PT_PTRACED;
  524. __ptrace_link(current, current->real_parent);
  525. }
  526. write_unlock_irqrestore(&tasklist_lock, flags);
  527. }
  528. task_unlock(current);
  529. return ret;
  530. }
  531. /**
  532. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  533. * @pid: process id to grab a task_struct reference of
  534. *
  535. * This function is a helper for ptrace implementations. It checks
  536. * permissions and then grabs a task struct for use of the actual
  537. * ptrace implementation.
  538. *
  539. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  540. */
  541. struct task_struct *ptrace_get_task_struct(pid_t pid)
  542. {
  543. struct task_struct *child;
  544. read_lock(&tasklist_lock);
  545. child = find_task_by_vpid(pid);
  546. if (child)
  547. get_task_struct(child);
  548. read_unlock(&tasklist_lock);
  549. if (!child)
  550. return ERR_PTR(-ESRCH);
  551. return child;
  552. }
  553. #ifndef arch_ptrace_attach
  554. #define arch_ptrace_attach(child) do { } while (0)
  555. #endif
  556. SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, 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. if (!ret)
  567. arch_ptrace_attach(current);
  568. goto out;
  569. }
  570. child = ptrace_get_task_struct(pid);
  571. if (IS_ERR(child)) {
  572. ret = PTR_ERR(child);
  573. goto out;
  574. }
  575. if (request == PTRACE_ATTACH) {
  576. ret = ptrace_attach(child);
  577. /*
  578. * Some architectures need to do book-keeping after
  579. * a ptrace attach.
  580. */
  581. if (!ret)
  582. arch_ptrace_attach(child);
  583. goto out_put_task_struct;
  584. }
  585. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  586. if (ret < 0)
  587. goto out_put_task_struct;
  588. ret = arch_ptrace(child, request, addr, data);
  589. out_put_task_struct:
  590. put_task_struct(child);
  591. out:
  592. unlock_kernel();
  593. return ret;
  594. }
  595. int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
  596. {
  597. unsigned long tmp;
  598. int copied;
  599. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  600. if (copied != sizeof(tmp))
  601. return -EIO;
  602. return put_user(tmp, (unsigned long __user *)data);
  603. }
  604. int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
  605. {
  606. int copied;
  607. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  608. return (copied == sizeof(data)) ? 0 : -EIO;
  609. }
  610. #if defined CONFIG_COMPAT
  611. #include <linux/compat.h>
  612. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  613. compat_ulong_t addr, compat_ulong_t data)
  614. {
  615. compat_ulong_t __user *datap = compat_ptr(data);
  616. compat_ulong_t word;
  617. siginfo_t siginfo;
  618. int ret;
  619. switch (request) {
  620. case PTRACE_PEEKTEXT:
  621. case PTRACE_PEEKDATA:
  622. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  623. if (ret != sizeof(word))
  624. ret = -EIO;
  625. else
  626. ret = put_user(word, datap);
  627. break;
  628. case PTRACE_POKETEXT:
  629. case PTRACE_POKEDATA:
  630. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  631. ret = (ret != sizeof(data) ? -EIO : 0);
  632. break;
  633. case PTRACE_GETEVENTMSG:
  634. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  635. break;
  636. case PTRACE_GETSIGINFO:
  637. ret = ptrace_getsiginfo(child, &siginfo);
  638. if (!ret)
  639. ret = copy_siginfo_to_user32(
  640. (struct compat_siginfo __user *) datap,
  641. &siginfo);
  642. break;
  643. case PTRACE_SETSIGINFO:
  644. memset(&siginfo, 0, sizeof siginfo);
  645. if (copy_siginfo_from_user32(
  646. &siginfo, (struct compat_siginfo __user *) datap))
  647. ret = -EFAULT;
  648. else
  649. ret = ptrace_setsiginfo(child, &siginfo);
  650. break;
  651. default:
  652. ret = ptrace_request(child, request, addr, data);
  653. }
  654. return ret;
  655. }
  656. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  657. compat_long_t addr, compat_long_t data)
  658. {
  659. struct task_struct *child;
  660. long ret;
  661. /*
  662. * This lock_kernel fixes a subtle race with suid exec
  663. */
  664. lock_kernel();
  665. if (request == PTRACE_TRACEME) {
  666. ret = ptrace_traceme();
  667. goto out;
  668. }
  669. child = ptrace_get_task_struct(pid);
  670. if (IS_ERR(child)) {
  671. ret = PTR_ERR(child);
  672. goto out;
  673. }
  674. if (request == PTRACE_ATTACH) {
  675. ret = ptrace_attach(child);
  676. /*
  677. * Some architectures need to do book-keeping after
  678. * a ptrace attach.
  679. */
  680. if (!ret)
  681. arch_ptrace_attach(child);
  682. goto out_put_task_struct;
  683. }
  684. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  685. if (!ret)
  686. ret = compat_arch_ptrace(child, request, addr, data);
  687. out_put_task_struct:
  688. put_task_struct(child);
  689. out:
  690. unlock_kernel();
  691. return ret;
  692. }
  693. #endif /* CONFIG_COMPAT */