ptrace_32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. /*
  36. * Offset of eflags on child stack..
  37. */
  38. #define EFL_OFFSET offsetof(struct pt_regs, eflags)
  39. static inline struct pt_regs *get_child_regs(struct task_struct *task)
  40. {
  41. void *stack_top = (void *)task->thread.esp0;
  42. return stack_top - sizeof(struct pt_regs);
  43. }
  44. /*
  45. * This routine will get a word off of the processes privileged stack.
  46. * the offset is bytes into the pt_regs structure on the stack.
  47. * This routine assumes that all the privileged stacks are in our
  48. * data space.
  49. */
  50. static inline int get_stack_long(struct task_struct *task, int offset)
  51. {
  52. unsigned char *stack;
  53. stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs);
  54. stack += offset;
  55. return (*((int *)stack));
  56. }
  57. /*
  58. * This routine will put a word on the processes privileged stack.
  59. * the offset is bytes into the pt_regs structure on the stack.
  60. * This routine assumes that all the privileged stacks are in our
  61. * data space.
  62. */
  63. static inline int put_stack_long(struct task_struct *task, int offset,
  64. unsigned long data)
  65. {
  66. unsigned char * stack;
  67. stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs);
  68. stack += offset;
  69. *(unsigned long *) stack = data;
  70. return 0;
  71. }
  72. static int putreg(struct task_struct *child,
  73. unsigned long regno, unsigned long value)
  74. {
  75. switch (regno >> 2) {
  76. case GS:
  77. if (value && (value & 3) != 3)
  78. return -EIO;
  79. child->thread.gs = value;
  80. return 0;
  81. case DS:
  82. case ES:
  83. case FS:
  84. if (value && (value & 3) != 3)
  85. return -EIO;
  86. value &= 0xffff;
  87. break;
  88. case SS:
  89. case CS:
  90. if ((value & 3) != 3)
  91. return -EIO;
  92. value &= 0xffff;
  93. break;
  94. case EFL:
  95. value &= FLAG_MASK;
  96. value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
  97. break;
  98. }
  99. if (regno > FS*4)
  100. regno -= 1*4;
  101. put_stack_long(child, regno, value);
  102. return 0;
  103. }
  104. static unsigned long getreg(struct task_struct *child,
  105. unsigned long regno)
  106. {
  107. unsigned long retval = ~0UL;
  108. switch (regno >> 2) {
  109. case GS:
  110. retval = child->thread.gs;
  111. break;
  112. case DS:
  113. case ES:
  114. case FS:
  115. case SS:
  116. case CS:
  117. retval = 0xffff;
  118. /* fall through */
  119. default:
  120. if (regno > FS*4)
  121. regno -= 1*4;
  122. retval &= get_stack_long(child, regno);
  123. }
  124. return retval;
  125. }
  126. #define LDT_SEGMENT 4
  127. static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs)
  128. {
  129. unsigned long addr, seg;
  130. addr = regs->eip;
  131. seg = regs->xcs & 0xffff;
  132. if (regs->eflags & VM_MASK) {
  133. addr = (addr & 0xffff) + (seg << 4);
  134. return addr;
  135. }
  136. /*
  137. * We'll assume that the code segments in the GDT
  138. * are all zero-based. That is largely true: the
  139. * TLS segments are used for data, and the PNPBIOS
  140. * and APM bios ones we just ignore here.
  141. */
  142. if (seg & LDT_SEGMENT) {
  143. u32 *desc;
  144. unsigned long base;
  145. seg &= ~7UL;
  146. mutex_lock(&child->mm->context.lock);
  147. if (unlikely((seg >> 3) >= child->mm->context.size))
  148. addr = -1L; /* bogus selector, access would fault */
  149. else {
  150. desc = child->mm->context.ldt + seg;
  151. base = ((desc[0] >> 16) |
  152. ((desc[1] & 0xff) << 16) |
  153. (desc[1] & 0xff000000));
  154. /* 16-bit code segment? */
  155. if (!((desc[1] >> 22) & 1))
  156. addr &= 0xffff;
  157. addr += base;
  158. }
  159. mutex_unlock(&child->mm->context.lock);
  160. }
  161. return addr;
  162. }
  163. static inline int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
  164. {
  165. int i, copied;
  166. unsigned char opcode[15];
  167. unsigned long addr = convert_eip_to_linear(child, regs);
  168. copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
  169. for (i = 0; i < copied; i++) {
  170. switch (opcode[i]) {
  171. /* popf and iret */
  172. case 0x9d: case 0xcf:
  173. return 1;
  174. /* opcode and address size prefixes */
  175. case 0x66: case 0x67:
  176. continue;
  177. /* irrelevant prefixes (segment overrides and repeats) */
  178. case 0x26: case 0x2e:
  179. case 0x36: case 0x3e:
  180. case 0x64: case 0x65:
  181. case 0xf0: case 0xf2: case 0xf3:
  182. continue;
  183. /*
  184. * pushf: NOTE! We should probably not let
  185. * the user see the TF bit being set. But
  186. * it's more pain than it's worth to avoid
  187. * it, and a debugger could emulate this
  188. * all in user space if it _really_ cares.
  189. */
  190. case 0x9c:
  191. default:
  192. return 0;
  193. }
  194. }
  195. return 0;
  196. }
  197. static void set_singlestep(struct task_struct *child)
  198. {
  199. struct pt_regs *regs = get_child_regs(child);
  200. /*
  201. * Always set TIF_SINGLESTEP - this guarantees that
  202. * we single-step system calls etc.. This will also
  203. * cause us to set TF when returning to user mode.
  204. */
  205. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  206. /*
  207. * If TF was already set, don't do anything else
  208. */
  209. if (regs->eflags & X86_EFLAGS_TF)
  210. return;
  211. /* Set TF on the kernel stack.. */
  212. regs->eflags |= X86_EFLAGS_TF;
  213. /*
  214. * ..but if TF is changed by the instruction we will trace,
  215. * don't mark it as being "us" that set it, so that we
  216. * won't clear it by hand later.
  217. */
  218. if (is_setting_trap_flag(child, regs))
  219. return;
  220. child->ptrace |= PT_DTRACE;
  221. }
  222. static void clear_singlestep(struct task_struct *child)
  223. {
  224. /* Always clear TIF_SINGLESTEP... */
  225. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  226. /* But touch TF only if it was set by us.. */
  227. if (child->ptrace & PT_DTRACE) {
  228. struct pt_regs *regs = get_child_regs(child);
  229. regs->eflags &= ~X86_EFLAGS_TF;
  230. child->ptrace &= ~PT_DTRACE;
  231. }
  232. }
  233. /*
  234. * Called by kernel/ptrace.c when detaching..
  235. *
  236. * Make sure the single step bit is not set.
  237. */
  238. void ptrace_disable(struct task_struct *child)
  239. {
  240. clear_singlestep(child);
  241. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  242. }
  243. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  244. {
  245. struct user * dummy = NULL;
  246. int i, ret;
  247. unsigned long __user *datap = (unsigned long __user *)data;
  248. switch (request) {
  249. /* when I and D space are separate, these will need to be fixed. */
  250. case PTRACE_PEEKTEXT: /* read word at location addr. */
  251. case PTRACE_PEEKDATA:
  252. ret = generic_ptrace_peekdata(child, addr, data);
  253. break;
  254. /* read the word at location addr in the USER area. */
  255. case PTRACE_PEEKUSR: {
  256. unsigned long tmp;
  257. ret = -EIO;
  258. if ((addr & 3) || addr < 0 ||
  259. addr > sizeof(struct user) - 3)
  260. break;
  261. tmp = 0; /* Default return condition */
  262. if(addr < FRAME_SIZE*sizeof(long))
  263. tmp = getreg(child, addr);
  264. if(addr >= (long) &dummy->u_debugreg[0] &&
  265. addr <= (long) &dummy->u_debugreg[7]){
  266. addr -= (long) &dummy->u_debugreg[0];
  267. addr = addr >> 2;
  268. tmp = child->thread.debugreg[addr];
  269. }
  270. ret = put_user(tmp, datap);
  271. break;
  272. }
  273. /* when I and D space are separate, this will have to be fixed. */
  274. case PTRACE_POKETEXT: /* write the word at location addr. */
  275. case PTRACE_POKEDATA:
  276. ret = generic_ptrace_pokedata(child, addr, data);
  277. break;
  278. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  279. ret = -EIO;
  280. if ((addr & 3) || addr < 0 ||
  281. addr > sizeof(struct user) - 3)
  282. break;
  283. if (addr < FRAME_SIZE*sizeof(long)) {
  284. ret = putreg(child, addr, data);
  285. break;
  286. }
  287. /* We need to be very careful here. We implicitly
  288. want to modify a portion of the task_struct, and we
  289. have to be selective about what portions we allow someone
  290. to modify. */
  291. ret = -EIO;
  292. if(addr >= (long) &dummy->u_debugreg[0] &&
  293. addr <= (long) &dummy->u_debugreg[7]){
  294. if(addr == (long) &dummy->u_debugreg[4]) break;
  295. if(addr == (long) &dummy->u_debugreg[5]) break;
  296. if(addr < (long) &dummy->u_debugreg[4] &&
  297. ((unsigned long) data) >= TASK_SIZE-3) break;
  298. /* Sanity-check data. Take one half-byte at once with
  299. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  300. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  301. * 2 and 3 are LENi. Given a list of invalid values,
  302. * we do mask |= 1 << invalid_value, so that
  303. * (mask >> check) & 1 is a correct test for invalid
  304. * values.
  305. *
  306. * R/Wi contains the type of the breakpoint /
  307. * watchpoint, LENi contains the length of the watched
  308. * data in the watchpoint case.
  309. *
  310. * The invalid values are:
  311. * - LENi == 0x10 (undefined), so mask |= 0x0f00.
  312. * - R/Wi == 0x10 (break on I/O reads or writes), so
  313. * mask |= 0x4444.
  314. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  315. * 0x1110.
  316. *
  317. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  318. *
  319. * See the Intel Manual "System Programming Guide",
  320. * 15.2.4
  321. *
  322. * Note that LENi == 0x10 is defined on x86_64 in long
  323. * mode (i.e. even for 32-bit userspace software, but
  324. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  325. * See the AMD manual no. 24593 (AMD64 System
  326. * Programming)*/
  327. if(addr == (long) &dummy->u_debugreg[7]) {
  328. data &= ~DR_CONTROL_RESERVED;
  329. for(i=0; i<4; i++)
  330. if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  331. goto out_tsk;
  332. if (data)
  333. set_tsk_thread_flag(child, TIF_DEBUG);
  334. else
  335. clear_tsk_thread_flag(child, TIF_DEBUG);
  336. }
  337. addr -= (long) &dummy->u_debugreg;
  338. addr = addr >> 2;
  339. child->thread.debugreg[addr] = data;
  340. ret = 0;
  341. }
  342. break;
  343. case PTRACE_SYSEMU: /* continue and stop at next syscall, which will not be executed */
  344. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  345. case PTRACE_CONT: /* restart after signal. */
  346. ret = -EIO;
  347. if (!valid_signal(data))
  348. break;
  349. if (request == PTRACE_SYSEMU) {
  350. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  351. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  352. } else if (request == PTRACE_SYSCALL) {
  353. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  354. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  355. } else {
  356. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  357. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  358. }
  359. child->exit_code = data;
  360. /* make sure the single step bit is not set. */
  361. clear_singlestep(child);
  362. wake_up_process(child);
  363. ret = 0;
  364. break;
  365. /*
  366. * make the child exit. Best I can do is send it a sigkill.
  367. * perhaps it should be put in the status that it wants to
  368. * exit.
  369. */
  370. case PTRACE_KILL:
  371. ret = 0;
  372. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  373. break;
  374. child->exit_code = SIGKILL;
  375. /* make sure the single step bit is not set. */
  376. clear_singlestep(child);
  377. wake_up_process(child);
  378. break;
  379. case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */
  380. case PTRACE_SINGLESTEP: /* set the trap flag. */
  381. ret = -EIO;
  382. if (!valid_signal(data))
  383. break;
  384. if (request == PTRACE_SYSEMU_SINGLESTEP)
  385. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  386. else
  387. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  388. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  389. set_singlestep(child);
  390. child->exit_code = data;
  391. /* give it a chance to run. */
  392. wake_up_process(child);
  393. ret = 0;
  394. break;
  395. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  396. if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
  397. ret = -EIO;
  398. break;
  399. }
  400. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  401. __put_user(getreg(child, i), datap);
  402. datap++;
  403. }
  404. ret = 0;
  405. break;
  406. }
  407. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  408. unsigned long tmp;
  409. if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
  410. ret = -EIO;
  411. break;
  412. }
  413. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  414. __get_user(tmp, datap);
  415. putreg(child, i, tmp);
  416. datap++;
  417. }
  418. ret = 0;
  419. break;
  420. }
  421. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  422. if (!access_ok(VERIFY_WRITE, datap,
  423. sizeof(struct user_i387_struct))) {
  424. ret = -EIO;
  425. break;
  426. }
  427. ret = 0;
  428. if (!tsk_used_math(child))
  429. init_fpu(child);
  430. get_fpregs((struct user_i387_struct __user *)data, child);
  431. break;
  432. }
  433. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  434. if (!access_ok(VERIFY_READ, datap,
  435. sizeof(struct user_i387_struct))) {
  436. ret = -EIO;
  437. break;
  438. }
  439. set_stopped_child_used_math(child);
  440. set_fpregs(child, (struct user_i387_struct __user *)data);
  441. ret = 0;
  442. break;
  443. }
  444. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  445. if (!access_ok(VERIFY_WRITE, datap,
  446. sizeof(struct user_fxsr_struct))) {
  447. ret = -EIO;
  448. break;
  449. }
  450. if (!tsk_used_math(child))
  451. init_fpu(child);
  452. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  453. break;
  454. }
  455. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  456. if (!access_ok(VERIFY_READ, datap,
  457. sizeof(struct user_fxsr_struct))) {
  458. ret = -EIO;
  459. break;
  460. }
  461. set_stopped_child_used_math(child);
  462. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  463. break;
  464. }
  465. case PTRACE_GET_THREAD_AREA:
  466. if (addr < 0)
  467. return -EIO;
  468. ret = do_get_thread_area(child, addr,
  469. (struct user_desc __user *) data);
  470. break;
  471. case PTRACE_SET_THREAD_AREA:
  472. if (addr < 0)
  473. return -EIO;
  474. ret = do_set_thread_area(child, addr,
  475. (struct user_desc __user *) data, 0);
  476. break;
  477. default:
  478. ret = ptrace_request(child, request, addr, data);
  479. break;
  480. }
  481. out_tsk:
  482. return ret;
  483. }
  484. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  485. {
  486. struct siginfo info;
  487. tsk->thread.trap_no = 1;
  488. tsk->thread.error_code = error_code;
  489. memset(&info, 0, sizeof(info));
  490. info.si_signo = SIGTRAP;
  491. info.si_code = TRAP_BRKPT;
  492. /* User-mode eip? */
  493. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL;
  494. /* Send us the fake SIGTRAP */
  495. force_sig_info(SIGTRAP, &info, tsk);
  496. }
  497. /* notification of system call entry/exit
  498. * - triggered by current->work.syscall_trace
  499. */
  500. __attribute__((regparm(3)))
  501. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  502. {
  503. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  504. /*
  505. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  506. * interception
  507. */
  508. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  509. int ret = 0;
  510. /* do the secure computing check first */
  511. if (!entryexit)
  512. secure_computing(regs->orig_eax);
  513. if (unlikely(current->audit_context)) {
  514. if (entryexit)
  515. audit_syscall_exit(AUDITSC_RESULT(regs->eax),
  516. regs->eax);
  517. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  518. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  519. * not used, entry.S will call us only on syscall exit, not
  520. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  521. * calling send_sigtrap() on syscall entry.
  522. *
  523. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  524. * is_singlestep is false, despite his name, so we will still do
  525. * the correct thing.
  526. */
  527. else if (is_singlestep)
  528. goto out;
  529. }
  530. if (!(current->ptrace & PT_PTRACED))
  531. goto out;
  532. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  533. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  534. * here. We have to check this and return */
  535. if (is_sysemu && entryexit)
  536. return 0;
  537. /* Fake a debug trap */
  538. if (is_singlestep)
  539. send_sigtrap(current, regs, 0);
  540. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  541. goto out;
  542. /* the 0x80 provides a way for the tracing parent to distinguish
  543. between a syscall stop and SIGTRAP delivery */
  544. /* Note that the debugger could change the result of test_thread_flag!*/
  545. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  546. /*
  547. * this isn't the same as continuing with a signal, but it will do
  548. * for normal use. strace only continues with a signal if the
  549. * stopping signal is not SIGTRAP. -brl
  550. */
  551. if (current->exit_code) {
  552. send_sig(current->exit_code, current, 1);
  553. current->exit_code = 0;
  554. }
  555. ret = is_sysemu;
  556. out:
  557. if (unlikely(current->audit_context) && !entryexit)
  558. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_eax,
  559. regs->ebx, regs->ecx, regs->edx, regs->esi);
  560. if (ret == 0)
  561. return 0;
  562. regs->orig_eax = -1; /* force skip of syscall restarting */
  563. if (unlikely(current->audit_context))
  564. audit_syscall_exit(AUDITSC_RESULT(regs->eax), regs->eax);
  565. return 1;
  566. }