ptrace.c 19 KB

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