ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/errno.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/user.h>
  13. #include <linux/security.h>
  14. #include <linux/audit.h>
  15. #include <linux/seccomp.h>
  16. #include <linux/signal.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/system.h>
  20. #include <asm/processor.h>
  21. #include <asm/i387.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/ldt.h>
  24. #include <asm/desc.h>
  25. /*
  26. * does not yet catch signals sent when the child dies.
  27. * in exit.c or in signal.c.
  28. */
  29. /*
  30. * Determines which flags the user has access to [1 = access, 0 = no access].
  31. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
  32. * Also masks reserved bits (31-22, 15, 5, 3, 1).
  33. */
  34. #define FLAG_MASK 0x00050dd5
  35. static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  36. {
  37. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  38. if (regno > FS)
  39. --regno;
  40. return &regs->bx + regno;
  41. }
  42. static int putreg(struct task_struct *child,
  43. unsigned long regno, unsigned long value)
  44. {
  45. struct pt_regs *regs = task_pt_regs(child);
  46. regno >>= 2;
  47. switch (regno) {
  48. case GS:
  49. if (value && (value & 3) != 3)
  50. return -EIO;
  51. child->thread.gs = value;
  52. if (child == current)
  53. /*
  54. * The user-mode %gs is not affected by
  55. * kernel entry, so we must update the CPU.
  56. */
  57. loadsegment(gs, value);
  58. return 0;
  59. case DS:
  60. case ES:
  61. case FS:
  62. if (value && (value & 3) != 3)
  63. return -EIO;
  64. value &= 0xffff;
  65. break;
  66. case SS:
  67. case CS:
  68. if ((value & 3) != 3)
  69. return -EIO;
  70. value &= 0xffff;
  71. break;
  72. case EFL:
  73. value &= FLAG_MASK;
  74. /*
  75. * If the user value contains TF, mark that
  76. * it was not "us" (the debugger) that set it.
  77. * If not, make sure it stays set if we had.
  78. */
  79. if (value & X86_EFLAGS_TF)
  80. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  81. else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  82. value |= X86_EFLAGS_TF;
  83. value |= regs->flags & ~FLAG_MASK;
  84. break;
  85. }
  86. *pt_regs_access(regs, regno) = value;
  87. return 0;
  88. }
  89. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  90. {
  91. struct pt_regs *regs = task_pt_regs(child);
  92. unsigned long retval = ~0UL;
  93. regno >>= 2;
  94. switch (regno) {
  95. case EFL:
  96. /*
  97. * If the debugger set TF, hide it from the readout.
  98. */
  99. retval = regs->flags;
  100. if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  101. retval &= ~X86_EFLAGS_TF;
  102. break;
  103. case GS:
  104. retval = child->thread.gs;
  105. if (child == current)
  106. savesegment(gs, retval);
  107. break;
  108. case DS:
  109. case ES:
  110. case FS:
  111. case SS:
  112. case CS:
  113. retval = 0xffff;
  114. /* fall through */
  115. default:
  116. retval &= *pt_regs_access(regs, regno);
  117. }
  118. return retval;
  119. }
  120. /*
  121. * This function is trivial and will be inlined by the compiler.
  122. * Having it separates the implementation details of debug
  123. * registers from the interface details of ptrace.
  124. */
  125. static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  126. {
  127. switch (n) {
  128. case 0: return child->thread.debugreg0;
  129. case 1: return child->thread.debugreg1;
  130. case 2: return child->thread.debugreg2;
  131. case 3: return child->thread.debugreg3;
  132. case 6: return child->thread.debugreg6;
  133. case 7: return child->thread.debugreg7;
  134. }
  135. return 0;
  136. }
  137. static int ptrace_set_debugreg(struct task_struct *child,
  138. int n, unsigned long data)
  139. {
  140. int i;
  141. if (unlikely(n == 4 || n == 5))
  142. return -EIO;
  143. if (n < 4 && unlikely(data >= TASK_SIZE - 3))
  144. return -EIO;
  145. switch (n) {
  146. case 0: child->thread.debugreg0 = data; break;
  147. case 1: child->thread.debugreg1 = data; break;
  148. case 2: child->thread.debugreg2 = data; break;
  149. case 3: child->thread.debugreg3 = data; break;
  150. case 6:
  151. child->thread.debugreg6 = data;
  152. break;
  153. case 7:
  154. /*
  155. * Sanity-check data. Take one half-byte at once with
  156. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  157. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  158. * 2 and 3 are LENi. Given a list of invalid values,
  159. * we do mask |= 1 << invalid_value, so that
  160. * (mask >> check) & 1 is a correct test for invalid
  161. * values.
  162. *
  163. * R/Wi contains the type of the breakpoint /
  164. * watchpoint, LENi contains the length of the watched
  165. * data in the watchpoint case.
  166. *
  167. * The invalid values are:
  168. * - LENi == 0x10 (undefined), so mask |= 0x0f00.
  169. * - R/Wi == 0x10 (break on I/O reads or writes), so
  170. * mask |= 0x4444.
  171. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  172. * 0x1110.
  173. *
  174. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  175. *
  176. * See the Intel Manual "System Programming Guide",
  177. * 15.2.4
  178. *
  179. * Note that LENi == 0x10 is defined on x86_64 in long
  180. * mode (i.e. even for 32-bit userspace software, but
  181. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  182. * See the AMD manual no. 24593 (AMD64 System Programming)
  183. */
  184. data &= ~DR_CONTROL_RESERVED;
  185. for (i = 0; i < 4; i++)
  186. if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  187. return -EIO;
  188. child->thread.debugreg7 = data;
  189. if (data)
  190. set_tsk_thread_flag(child, TIF_DEBUG);
  191. else
  192. clear_tsk_thread_flag(child, TIF_DEBUG);
  193. break;
  194. }
  195. return 0;
  196. }
  197. /*
  198. * Called by kernel/ptrace.c when detaching..
  199. *
  200. * Make sure the single step bit is not set.
  201. */
  202. void ptrace_disable(struct task_struct *child)
  203. {
  204. user_disable_single_step(child);
  205. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  206. }
  207. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  208. {
  209. struct user * dummy = NULL;
  210. int i, ret;
  211. unsigned long __user *datap = (unsigned long __user *)data;
  212. switch (request) {
  213. /* when I and D space are separate, these will need to be fixed. */
  214. case PTRACE_PEEKTEXT: /* read word at location addr. */
  215. case PTRACE_PEEKDATA:
  216. ret = generic_ptrace_peekdata(child, addr, data);
  217. break;
  218. /* read the word at location addr in the USER area. */
  219. case PTRACE_PEEKUSR: {
  220. unsigned long tmp;
  221. ret = -EIO;
  222. if ((addr & 3) || addr < 0 ||
  223. addr > sizeof(struct user) - 3)
  224. break;
  225. tmp = 0; /* Default return condition */
  226. if(addr < FRAME_SIZE*sizeof(long))
  227. tmp = getreg(child, addr);
  228. if(addr >= (long) &dummy->u_debugreg[0] &&
  229. addr <= (long) &dummy->u_debugreg[7]){
  230. addr -= (long) &dummy->u_debugreg[0];
  231. addr = addr >> 2;
  232. tmp = ptrace_get_debugreg(child, addr);
  233. }
  234. ret = put_user(tmp, datap);
  235. break;
  236. }
  237. /* when I and D space are separate, this will have to be fixed. */
  238. case PTRACE_POKETEXT: /* write the word at location addr. */
  239. case PTRACE_POKEDATA:
  240. ret = generic_ptrace_pokedata(child, addr, data);
  241. break;
  242. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  243. ret = -EIO;
  244. if ((addr & 3) || addr < 0 ||
  245. addr > sizeof(struct user) - 3)
  246. break;
  247. if (addr < FRAME_SIZE*sizeof(long)) {
  248. ret = putreg(child, addr, data);
  249. break;
  250. }
  251. /* We need to be very careful here. We implicitly
  252. want to modify a portion of the task_struct, and we
  253. have to be selective about what portions we allow someone
  254. to modify. */
  255. ret = -EIO;
  256. if(addr >= (long) &dummy->u_debugreg[0] &&
  257. addr <= (long) &dummy->u_debugreg[7]){
  258. addr -= (long) &dummy->u_debugreg;
  259. addr = addr >> 2;
  260. ret = ptrace_set_debugreg(child, addr, data);
  261. }
  262. break;
  263. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  264. if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
  265. ret = -EIO;
  266. break;
  267. }
  268. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  269. __put_user(getreg(child, i), datap);
  270. datap++;
  271. }
  272. ret = 0;
  273. break;
  274. }
  275. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  276. unsigned long tmp;
  277. if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
  278. ret = -EIO;
  279. break;
  280. }
  281. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  282. __get_user(tmp, datap);
  283. putreg(child, i, tmp);
  284. datap++;
  285. }
  286. ret = 0;
  287. break;
  288. }
  289. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  290. if (!access_ok(VERIFY_WRITE, datap,
  291. sizeof(struct user_i387_struct))) {
  292. ret = -EIO;
  293. break;
  294. }
  295. ret = 0;
  296. if (!tsk_used_math(child))
  297. init_fpu(child);
  298. get_fpregs((struct user_i387_struct __user *)data, child);
  299. break;
  300. }
  301. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  302. if (!access_ok(VERIFY_READ, datap,
  303. sizeof(struct user_i387_struct))) {
  304. ret = -EIO;
  305. break;
  306. }
  307. set_stopped_child_used_math(child);
  308. set_fpregs(child, (struct user_i387_struct __user *)data);
  309. ret = 0;
  310. break;
  311. }
  312. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  313. if (!access_ok(VERIFY_WRITE, datap,
  314. sizeof(struct user_fxsr_struct))) {
  315. ret = -EIO;
  316. break;
  317. }
  318. if (!tsk_used_math(child))
  319. init_fpu(child);
  320. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  321. break;
  322. }
  323. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  324. if (!access_ok(VERIFY_READ, datap,
  325. sizeof(struct user_fxsr_struct))) {
  326. ret = -EIO;
  327. break;
  328. }
  329. set_stopped_child_used_math(child);
  330. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  331. break;
  332. }
  333. case PTRACE_GET_THREAD_AREA:
  334. if (addr < 0)
  335. return -EIO;
  336. ret = do_get_thread_area(child, addr,
  337. (struct user_desc __user *) data);
  338. break;
  339. case PTRACE_SET_THREAD_AREA:
  340. if (addr < 0)
  341. return -EIO;
  342. ret = do_set_thread_area(child, addr,
  343. (struct user_desc __user *) data, 0);
  344. break;
  345. default:
  346. ret = ptrace_request(child, request, addr, data);
  347. break;
  348. }
  349. return ret;
  350. }
  351. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  352. {
  353. struct siginfo info;
  354. tsk->thread.trap_no = 1;
  355. tsk->thread.error_code = error_code;
  356. memset(&info, 0, sizeof(info));
  357. info.si_signo = SIGTRAP;
  358. info.si_code = TRAP_BRKPT;
  359. /* User-mode ip? */
  360. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  361. /* Send us the fake SIGTRAP */
  362. force_sig_info(SIGTRAP, &info, tsk);
  363. }
  364. /* notification of system call entry/exit
  365. * - triggered by current->work.syscall_trace
  366. */
  367. __attribute__((regparm(3)))
  368. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  369. {
  370. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  371. /*
  372. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  373. * interception
  374. */
  375. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  376. int ret = 0;
  377. /* do the secure computing check first */
  378. if (!entryexit)
  379. secure_computing(regs->orig_ax);
  380. if (unlikely(current->audit_context)) {
  381. if (entryexit)
  382. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  383. regs->ax);
  384. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  385. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  386. * not used, entry.S will call us only on syscall exit, not
  387. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  388. * calling send_sigtrap() on syscall entry.
  389. *
  390. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  391. * is_singlestep is false, despite his name, so we will still do
  392. * the correct thing.
  393. */
  394. else if (is_singlestep)
  395. goto out;
  396. }
  397. if (!(current->ptrace & PT_PTRACED))
  398. goto out;
  399. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  400. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  401. * here. We have to check this and return */
  402. if (is_sysemu && entryexit)
  403. return 0;
  404. /* Fake a debug trap */
  405. if (is_singlestep)
  406. send_sigtrap(current, regs, 0);
  407. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  408. goto out;
  409. /* the 0x80 provides a way for the tracing parent to distinguish
  410. between a syscall stop and SIGTRAP delivery */
  411. /* Note that the debugger could change the result of test_thread_flag!*/
  412. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  413. /*
  414. * this isn't the same as continuing with a signal, but it will do
  415. * for normal use. strace only continues with a signal if the
  416. * stopping signal is not SIGTRAP. -brl
  417. */
  418. if (current->exit_code) {
  419. send_sig(current->exit_code, current, 1);
  420. current->exit_code = 0;
  421. }
  422. ret = is_sysemu;
  423. out:
  424. if (unlikely(current->audit_context) && !entryexit)
  425. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  426. regs->bx, regs->cx, regs->dx, regs->si);
  427. if (ret == 0)
  428. return 0;
  429. regs->orig_ax = -1; /* force skip of syscall restarting */
  430. if (unlikely(current->audit_context))
  431. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  432. return 1;
  433. }