ptrace.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/sched.h"
  6. #include "linux/mm.h"
  7. #include "linux/errno.h"
  8. #include "linux/smp_lock.h"
  9. #include "linux/security.h"
  10. #include "linux/ptrace.h"
  11. #include "linux/audit.h"
  12. #ifdef CONFIG_PROC_MM
  13. #include "linux/proc_mm.h"
  14. #endif
  15. #include "asm/ptrace.h"
  16. #include "asm/uaccess.h"
  17. #include "kern_util.h"
  18. #include "skas_ptrace.h"
  19. #include "sysdep/ptrace.h"
  20. /*
  21. * Called by kernel/ptrace.c when detaching..
  22. */
  23. void ptrace_disable(struct task_struct *child)
  24. {
  25. child->ptrace &= ~PT_DTRACE;
  26. child->thread.singlestep_syscall = 0;
  27. }
  28. long sys_ptrace(long request, long pid, long addr, long data)
  29. {
  30. struct task_struct *child;
  31. int i, ret;
  32. lock_kernel();
  33. ret = -EPERM;
  34. if (request == PTRACE_TRACEME) {
  35. /* are we already being traced? */
  36. if (current->ptrace & PT_PTRACED)
  37. goto out;
  38. ret = security_ptrace(current->parent, current);
  39. if (ret)
  40. goto out;
  41. /* set the ptrace bit in the process flags. */
  42. current->ptrace |= PT_PTRACED;
  43. ret = 0;
  44. goto out;
  45. }
  46. ret = -ESRCH;
  47. read_lock(&tasklist_lock);
  48. child = find_task_by_pid(pid);
  49. if (child)
  50. get_task_struct(child);
  51. read_unlock(&tasklist_lock);
  52. if (!child)
  53. goto out;
  54. ret = -EPERM;
  55. if (pid == 1) /* you may not mess with init */
  56. goto out_tsk;
  57. if (request == PTRACE_ATTACH) {
  58. ret = ptrace_attach(child);
  59. goto out_tsk;
  60. }
  61. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  62. if (ret < 0)
  63. goto out_tsk;
  64. switch (request) {
  65. /* when I and D space are separate, these will need to be fixed. */
  66. case PTRACE_PEEKTEXT: /* read word at location addr. */
  67. case PTRACE_PEEKDATA: {
  68. unsigned long tmp;
  69. int copied;
  70. ret = -EIO;
  71. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  72. if (copied != sizeof(tmp))
  73. break;
  74. ret = put_user(tmp, (unsigned long __user *) data);
  75. break;
  76. }
  77. /* read the word at location addr in the USER area. */
  78. case PTRACE_PEEKUSR: {
  79. unsigned long tmp;
  80. ret = -EIO;
  81. if ((addr & 3) || addr < 0)
  82. break;
  83. tmp = 0; /* Default return condition */
  84. if(addr < MAX_REG_OFFSET){
  85. tmp = getreg(child, addr);
  86. }
  87. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  88. (addr <= offsetof(struct user, u_debugreg[7]))){
  89. addr -= offsetof(struct user, u_debugreg[0]);
  90. addr = addr >> 2;
  91. tmp = child->thread.arch.debugregs[addr];
  92. }
  93. ret = put_user(tmp, (unsigned long __user *) data);
  94. break;
  95. }
  96. /* when I and D space are separate, this will have to be fixed. */
  97. case PTRACE_POKETEXT: /* write the word at location addr. */
  98. case PTRACE_POKEDATA:
  99. ret = -EIO;
  100. if (access_process_vm(child, addr, &data, sizeof(data),
  101. 1) != sizeof(data))
  102. break;
  103. ret = 0;
  104. break;
  105. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  106. ret = -EIO;
  107. if ((addr & 3) || addr < 0)
  108. break;
  109. if (addr < MAX_REG_OFFSET) {
  110. ret = putreg(child, addr, data);
  111. break;
  112. }
  113. #if 0 /* XXX x86_64 */
  114. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  115. (addr <= offsetof(struct user, u_debugreg[7]))){
  116. addr -= offsetof(struct user, u_debugreg[0]);
  117. addr = addr >> 2;
  118. if((addr == 4) || (addr == 5)) break;
  119. child->thread.arch.debugregs[addr] = data;
  120. ret = 0;
  121. }
  122. #endif
  123. break;
  124. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  125. case PTRACE_CONT: { /* restart after signal. */
  126. ret = -EIO;
  127. if ((unsigned long) data > _NSIG)
  128. break;
  129. child->ptrace &= ~PT_DTRACE;
  130. child->thread.singlestep_syscall = 0;
  131. if (request == PTRACE_SYSCALL) {
  132. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  133. }
  134. else {
  135. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  136. }
  137. child->exit_code = data;
  138. wake_up_process(child);
  139. ret = 0;
  140. break;
  141. }
  142. /*
  143. * make the child exit. Best I can do is send it a sigkill.
  144. * perhaps it should be put in the status that it wants to
  145. * exit.
  146. */
  147. case PTRACE_KILL: {
  148. ret = 0;
  149. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  150. break;
  151. child->ptrace &= ~PT_DTRACE;
  152. child->thread.singlestep_syscall = 0;
  153. child->exit_code = SIGKILL;
  154. wake_up_process(child);
  155. break;
  156. }
  157. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  158. ret = -EIO;
  159. if ((unsigned long) data > _NSIG)
  160. break;
  161. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  162. child->ptrace |= PT_DTRACE;
  163. child->thread.singlestep_syscall = 0;
  164. child->exit_code = data;
  165. /* give it a chance to run. */
  166. wake_up_process(child);
  167. ret = 0;
  168. break;
  169. }
  170. case PTRACE_DETACH:
  171. /* detach a process that was attached. */
  172. ret = ptrace_detach(child, data);
  173. break;
  174. #ifdef PTRACE_GETREGS
  175. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  176. if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
  177. MAX_REG_OFFSET)) {
  178. ret = -EIO;
  179. break;
  180. }
  181. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  182. __put_user(getreg(child, i),
  183. (unsigned long __user *) data);
  184. data += sizeof(long);
  185. }
  186. ret = 0;
  187. break;
  188. }
  189. #endif
  190. #ifdef PTRACE_SETREGS
  191. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  192. unsigned long tmp = 0;
  193. if (!access_ok(VERIFY_READ, (unsigned *)data,
  194. MAX_REG_OFFSET)) {
  195. ret = -EIO;
  196. break;
  197. }
  198. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  199. __get_user(tmp, (unsigned long __user *) data);
  200. putreg(child, i, tmp);
  201. data += sizeof(long);
  202. }
  203. ret = 0;
  204. break;
  205. }
  206. #endif
  207. #ifdef PTRACE_GETFPREGS
  208. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  209. ret = get_fpregs(data, child);
  210. break;
  211. #endif
  212. #ifdef PTRACE_SETFPREGS
  213. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  214. ret = set_fpregs(data, child);
  215. break;
  216. #endif
  217. #ifdef PTRACE_GETFPXREGS
  218. case PTRACE_GETFPXREGS: /* Get the child FPU state. */
  219. ret = get_fpxregs(data, child);
  220. break;
  221. #endif
  222. #ifdef PTRACE_SETFPXREGS
  223. case PTRACE_SETFPXREGS: /* Set the child FPU state. */
  224. ret = set_fpxregs(data, child);
  225. break;
  226. #endif
  227. case PTRACE_FAULTINFO: {
  228. struct ptrace_faultinfo fault;
  229. fault = ((struct ptrace_faultinfo)
  230. { .is_write = child->thread.err,
  231. .addr = child->thread.cr2 });
  232. ret = copy_to_user((unsigned long __user *) data, &fault,
  233. sizeof(fault));
  234. if(ret)
  235. break;
  236. break;
  237. }
  238. case PTRACE_SIGPENDING:
  239. ret = copy_to_user((unsigned long __user *) data,
  240. &child->pending.signal,
  241. sizeof(child->pending.signal));
  242. break;
  243. case PTRACE_LDT: {
  244. struct ptrace_ldt ldt;
  245. if(copy_from_user(&ldt, (unsigned long __user *) data,
  246. sizeof(ldt))){
  247. ret = -EIO;
  248. break;
  249. }
  250. /* This one is confusing, so just punt and return -EIO for
  251. * now
  252. */
  253. ret = -EIO;
  254. break;
  255. }
  256. #ifdef CONFIG_PROC_MM
  257. case PTRACE_SWITCH_MM: {
  258. struct mm_struct *old = child->mm;
  259. struct mm_struct *new = proc_mm_get_mm(data);
  260. if(IS_ERR(new)){
  261. ret = PTR_ERR(new);
  262. break;
  263. }
  264. atomic_inc(&new->mm_users);
  265. child->mm = new;
  266. child->active_mm = new;
  267. mmput(old);
  268. ret = 0;
  269. break;
  270. }
  271. #endif
  272. default:
  273. ret = ptrace_request(child, request, addr, data);
  274. break;
  275. }
  276. out_tsk:
  277. put_task_struct(child);
  278. out:
  279. unlock_kernel();
  280. return ret;
  281. }
  282. void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
  283. int error_code)
  284. {
  285. struct siginfo info;
  286. memset(&info, 0, sizeof(info));
  287. info.si_signo = SIGTRAP;
  288. info.si_code = TRAP_BRKPT;
  289. /* User-mode eip? */
  290. info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
  291. /* Send us the fakey SIGTRAP */
  292. force_sig_info(SIGTRAP, &info, tsk);
  293. }
  294. /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
  295. * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
  296. */
  297. void syscall_trace(union uml_pt_regs *regs, int entryexit)
  298. {
  299. int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
  300. int tracesysgood;
  301. if (unlikely(current->audit_context)) {
  302. if (!entryexit)
  303. audit_syscall_entry(current,
  304. UPT_SYSCALL_NR(&regs->regs),
  305. UPT_SYSCALL_ARG1(&regs->regs),
  306. UPT_SYSCALL_ARG2(&regs->regs),
  307. UPT_SYSCALL_ARG3(&regs->regs),
  308. UPT_SYSCALL_ARG4(&regs->regs));
  309. else
  310. audit_syscall_exit(current,
  311. UPT_SYSCALL_RET(&regs->regs));
  312. }
  313. /* Fake a debug trap */
  314. if (is_singlestep)
  315. send_sigtrap(current, regs, 0);
  316. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  317. return;
  318. if (!(current->ptrace & PT_PTRACED))
  319. return;
  320. /* the 0x80 provides a way for the tracing parent to distinguish
  321. between a syscall stop and SIGTRAP delivery */
  322. tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
  323. ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
  324. if (entryexit) /* force do_signal() --> is_syscall() */
  325. set_thread_flag(TIF_SIGPENDING);
  326. /* this isn't the same as continuing with a signal, but it will do
  327. * for normal use. strace only continues with a signal if the
  328. * stopping signal is not SIGTRAP. -brl
  329. */
  330. if (current->exit_code) {
  331. send_sig(current->exit_code, current, 1);
  332. current->exit_code = 0;
  333. }
  334. }
  335. /*
  336. * Overrides for Emacs so that we follow Linus's tabbing style.
  337. * Emacs will notice this stuff at the end of the file and automatically
  338. * adjust the settings for this buffer only. This must remain at the end
  339. * of the file.
  340. * ---------------------------------------------------------------------------
  341. * Local variables:
  342. * c-file-style: "linux"
  343. * End:
  344. */