ptrace.c 19 KB

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