ptrace_32.c 16 KB

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