ptrace.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #if defined(CONFIG_UML_X86) && !defined(CONFIG_64BIT)
  88. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  89. (addr <= offsetof(struct user, u_debugreg[7]))){
  90. addr -= offsetof(struct user, u_debugreg[0]);
  91. addr = addr >> 2;
  92. tmp = child->thread.arch.debugregs[addr];
  93. }
  94. #endif
  95. ret = put_user(tmp, (unsigned long __user *) data);
  96. break;
  97. }
  98. /* when I and D space are separate, this will have to be fixed. */
  99. case PTRACE_POKETEXT: /* write the word at location addr. */
  100. case PTRACE_POKEDATA:
  101. ret = -EIO;
  102. if (access_process_vm(child, addr, &data, sizeof(data),
  103. 1) != sizeof(data))
  104. break;
  105. ret = 0;
  106. break;
  107. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  108. ret = -EIO;
  109. if ((addr & 3) || addr < 0)
  110. break;
  111. if (addr < MAX_REG_OFFSET) {
  112. ret = putreg(child, addr, data);
  113. break;
  114. }
  115. #if defined(CONFIG_UML_X86) && !defined(CONFIG_64BIT)
  116. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  117. (addr <= offsetof(struct user, u_debugreg[7]))){
  118. addr -= offsetof(struct user, u_debugreg[0]);
  119. addr = addr >> 2;
  120. if((addr == 4) || (addr == 5)) break;
  121. child->thread.arch.debugregs[addr] = data;
  122. ret = 0;
  123. }
  124. #endif
  125. break;
  126. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  127. case PTRACE_CONT: { /* restart after signal. */
  128. ret = -EIO;
  129. if (!valid_signal(data))
  130. break;
  131. child->ptrace &= ~PT_DTRACE;
  132. child->thread.singlestep_syscall = 0;
  133. if (request == PTRACE_SYSCALL) {
  134. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  135. }
  136. else {
  137. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  138. }
  139. child->exit_code = data;
  140. wake_up_process(child);
  141. ret = 0;
  142. break;
  143. }
  144. /*
  145. * make the child exit. Best I can do is send it a sigkill.
  146. * perhaps it should be put in the status that it wants to
  147. * exit.
  148. */
  149. case PTRACE_KILL: {
  150. ret = 0;
  151. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  152. break;
  153. child->ptrace &= ~PT_DTRACE;
  154. child->thread.singlestep_syscall = 0;
  155. child->exit_code = SIGKILL;
  156. wake_up_process(child);
  157. break;
  158. }
  159. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  160. ret = -EIO;
  161. if (!valid_signal(data))
  162. break;
  163. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  164. child->ptrace |= PT_DTRACE;
  165. child->thread.singlestep_syscall = 0;
  166. child->exit_code = data;
  167. /* give it a chance to run. */
  168. wake_up_process(child);
  169. ret = 0;
  170. break;
  171. }
  172. case PTRACE_DETACH:
  173. /* detach a process that was attached. */
  174. ret = ptrace_detach(child, data);
  175. break;
  176. #ifdef PTRACE_GETREGS
  177. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  178. if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
  179. MAX_REG_OFFSET)) {
  180. ret = -EIO;
  181. break;
  182. }
  183. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  184. __put_user(getreg(child, i),
  185. (unsigned long __user *) data);
  186. data += sizeof(long);
  187. }
  188. ret = 0;
  189. break;
  190. }
  191. #endif
  192. #ifdef PTRACE_SETREGS
  193. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  194. unsigned long tmp = 0;
  195. if (!access_ok(VERIFY_READ, (unsigned *)data,
  196. MAX_REG_OFFSET)) {
  197. ret = -EIO;
  198. break;
  199. }
  200. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  201. __get_user(tmp, (unsigned long __user *) data);
  202. putreg(child, i, tmp);
  203. data += sizeof(long);
  204. }
  205. ret = 0;
  206. break;
  207. }
  208. #endif
  209. #ifdef PTRACE_GETFPREGS
  210. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  211. ret = get_fpregs(data, child);
  212. break;
  213. #endif
  214. #ifdef PTRACE_SETFPREGS
  215. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  216. ret = set_fpregs(data, child);
  217. break;
  218. #endif
  219. #ifdef PTRACE_GETFPXREGS
  220. case PTRACE_GETFPXREGS: /* Get the child FPU state. */
  221. ret = get_fpxregs(data, child);
  222. break;
  223. #endif
  224. #ifdef PTRACE_SETFPXREGS
  225. case PTRACE_SETFPXREGS: /* Set the child FPU state. */
  226. ret = set_fpxregs(data, child);
  227. break;
  228. #endif
  229. case PTRACE_FAULTINFO: {
  230. /* Take the info from thread->arch->faultinfo,
  231. * but transfer max. sizeof(struct ptrace_faultinfo).
  232. * On i386, ptrace_faultinfo is smaller!
  233. */
  234. ret = copy_to_user((unsigned long __user *) data,
  235. &child->thread.arch.faultinfo,
  236. sizeof(struct ptrace_faultinfo));
  237. if(ret)
  238. break;
  239. break;
  240. }
  241. case PTRACE_SIGPENDING:
  242. ret = copy_to_user((unsigned long __user *) data,
  243. &child->pending.signal,
  244. sizeof(child->pending.signal));
  245. break;
  246. #ifdef PTRACE_LDT
  247. case PTRACE_LDT: {
  248. struct ptrace_ldt ldt;
  249. if(copy_from_user(&ldt, (unsigned long __user *) data,
  250. sizeof(ldt))){
  251. ret = -EIO;
  252. break;
  253. }
  254. /* This one is confusing, so just punt and return -EIO for
  255. * now
  256. */
  257. ret = -EIO;
  258. break;
  259. }
  260. #endif
  261. #ifdef CONFIG_PROC_MM
  262. case PTRACE_SWITCH_MM: {
  263. struct mm_struct *old = child->mm;
  264. struct mm_struct *new = proc_mm_get_mm(data);
  265. if(IS_ERR(new)){
  266. ret = PTR_ERR(new);
  267. break;
  268. }
  269. atomic_inc(&new->mm_users);
  270. child->mm = new;
  271. child->active_mm = new;
  272. mmput(old);
  273. ret = 0;
  274. break;
  275. }
  276. #endif
  277. default:
  278. ret = ptrace_request(child, request, addr, data);
  279. break;
  280. }
  281. out_tsk:
  282. put_task_struct(child);
  283. out:
  284. unlock_kernel();
  285. return ret;
  286. }
  287. void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
  288. int error_code)
  289. {
  290. struct siginfo info;
  291. memset(&info, 0, sizeof(info));
  292. info.si_signo = SIGTRAP;
  293. info.si_code = TRAP_BRKPT;
  294. /* User-mode eip? */
  295. info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
  296. /* Send us the fakey SIGTRAP */
  297. force_sig_info(SIGTRAP, &info, tsk);
  298. }
  299. /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
  300. * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
  301. */
  302. void syscall_trace(union uml_pt_regs *regs, int entryexit)
  303. {
  304. int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
  305. int tracesysgood;
  306. if (unlikely(current->audit_context)) {
  307. if (!entryexit)
  308. audit_syscall_entry(current,
  309. HOST_AUDIT_ARCH,
  310. UPT_SYSCALL_NR(regs),
  311. UPT_SYSCALL_ARG1(regs),
  312. UPT_SYSCALL_ARG2(regs),
  313. UPT_SYSCALL_ARG3(regs),
  314. UPT_SYSCALL_ARG4(regs));
  315. else {
  316. int res = UPT_SYSCALL_RET(regs);
  317. audit_syscall_exit(current, AUDITSC_RESULT(res),
  318. res);
  319. }
  320. }
  321. /* Fake a debug trap */
  322. if (is_singlestep)
  323. send_sigtrap(current, regs, 0);
  324. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  325. return;
  326. if (!(current->ptrace & PT_PTRACED))
  327. return;
  328. /* the 0x80 provides a way for the tracing parent to distinguish
  329. between a syscall stop and SIGTRAP delivery */
  330. tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
  331. ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
  332. if (entryexit) /* force do_signal() --> is_syscall() */
  333. set_thread_flag(TIF_SIGPENDING);
  334. /* this isn't the same as continuing with a signal, but it will do
  335. * for normal use. strace only continues with a signal if the
  336. * stopping signal is not SIGTRAP. -brl
  337. */
  338. if (current->exit_code) {
  339. send_sig(current->exit_code, current, 1);
  340. current->exit_code = 0;
  341. }
  342. }
  343. /*
  344. * Overrides for Emacs so that we follow Linus's tabbing style.
  345. * Emacs will notice this stuff at the end of the file and automatically
  346. * adjust the settings for this buffer only. This must remain at the end
  347. * of the file.
  348. * ---------------------------------------------------------------------------
  349. * Local variables:
  350. * c-file-style: "linux"
  351. * End:
  352. */