ptrace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /* ptrace.c */
  2. /* By Ross Biro 1/23/92 */
  3. /*
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. *
  7. * x86-64 port 2000-2002 Andi Kleen
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/user.h>
  16. #include <linux/security.h>
  17. #include <linux/audit.h>
  18. #include <linux/seccomp.h>
  19. #include <linux/signal.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/system.h>
  23. #include <asm/processor.h>
  24. #include <asm/i387.h>
  25. #include <asm/debugreg.h>
  26. #include <asm/ldt.h>
  27. #include <asm/desc.h>
  28. #include <asm/proto.h>
  29. #include <asm/ia32.h>
  30. /*
  31. * does not yet catch signals sent when the child dies.
  32. * in exit.c or in signal.c.
  33. */
  34. /*
  35. * Determines which flags the user has access to [1 = access, 0 = no access].
  36. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
  37. * Also masks reserved bits (63-22, 15, 5, 3, 1).
  38. */
  39. #define FLAG_MASK 0x54dd5UL
  40. /* set's the trap flag. */
  41. #define TRAP_FLAG 0x100UL
  42. /*
  43. * eflags and offset of eflags on child stack..
  44. */
  45. #define EFLAGS offsetof(struct pt_regs, eflags)
  46. #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
  47. /*
  48. * this routine will get a word off of the processes privileged stack.
  49. * the offset is how far from the base addr as stored in the TSS.
  50. * this routine assumes that all the privileged stacks are in our
  51. * data space.
  52. */
  53. static inline unsigned long get_stack_long(struct task_struct *task, int offset)
  54. {
  55. unsigned char *stack;
  56. stack = (unsigned char *)task->thread.rsp0;
  57. stack += offset;
  58. return (*((unsigned long *)stack));
  59. }
  60. /*
  61. * this routine will put a word on the processes privileged stack.
  62. * the offset is how far from the base addr as stored in the TSS.
  63. * this routine assumes that all the privileged stacks are in our
  64. * data space.
  65. */
  66. static inline long put_stack_long(struct task_struct *task, int offset,
  67. unsigned long data)
  68. {
  69. unsigned char * stack;
  70. stack = (unsigned char *) task->thread.rsp0;
  71. stack += offset;
  72. *(unsigned long *) stack = data;
  73. return 0;
  74. }
  75. #define LDT_SEGMENT 4
  76. unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
  77. {
  78. unsigned long addr, seg;
  79. addr = regs->rip;
  80. seg = regs->cs & 0xffff;
  81. /*
  82. * We'll assume that the code segments in the GDT
  83. * are all zero-based. That is largely true: the
  84. * TLS segments are used for data, and the PNPBIOS
  85. * and APM bios ones we just ignore here.
  86. */
  87. if (seg & LDT_SEGMENT) {
  88. u32 *desc;
  89. unsigned long base;
  90. seg &= ~7UL;
  91. down(&child->mm->context.sem);
  92. if (unlikely((seg >> 3) >= child->mm->context.size))
  93. addr = -1L; /* bogus selector, access would fault */
  94. else {
  95. desc = child->mm->context.ldt + seg;
  96. base = ((desc[0] >> 16) |
  97. ((desc[1] & 0xff) << 16) |
  98. (desc[1] & 0xff000000));
  99. /* 16-bit code segment? */
  100. if (!((desc[1] >> 22) & 1))
  101. addr &= 0xffff;
  102. addr += base;
  103. }
  104. up(&child->mm->context.sem);
  105. }
  106. return addr;
  107. }
  108. static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
  109. {
  110. int i, copied;
  111. unsigned char opcode[15];
  112. unsigned long addr = convert_rip_to_linear(child, regs);
  113. copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
  114. for (i = 0; i < copied; i++) {
  115. switch (opcode[i]) {
  116. /* popf and iret */
  117. case 0x9d: case 0xcf:
  118. return 1;
  119. /* CHECKME: 64 65 */
  120. /* opcode and address size prefixes */
  121. case 0x66: case 0x67:
  122. continue;
  123. /* irrelevant prefixes (segment overrides and repeats) */
  124. case 0x26: case 0x2e:
  125. case 0x36: case 0x3e:
  126. case 0x64: case 0x65:
  127. case 0xf2: case 0xf3:
  128. continue;
  129. case 0x40 ... 0x4f:
  130. if (regs->cs != __USER_CS)
  131. /* 32-bit mode: register increment */
  132. return 0;
  133. /* 64-bit mode: REX prefix */
  134. continue;
  135. /* CHECKME: f2, f3 */
  136. /*
  137. * pushf: NOTE! We should probably not let
  138. * the user see the TF bit being set. But
  139. * it's more pain than it's worth to avoid
  140. * it, and a debugger could emulate this
  141. * all in user space if it _really_ cares.
  142. */
  143. case 0x9c:
  144. default:
  145. return 0;
  146. }
  147. }
  148. return 0;
  149. }
  150. static void set_singlestep(struct task_struct *child)
  151. {
  152. struct pt_regs *regs = task_pt_regs(child);
  153. /*
  154. * Always set TIF_SINGLESTEP - this guarantees that
  155. * we single-step system calls etc.. This will also
  156. * cause us to set TF when returning to user mode.
  157. */
  158. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  159. /*
  160. * If TF was already set, don't do anything else
  161. */
  162. if (regs->eflags & TRAP_FLAG)
  163. return;
  164. /* Set TF on the kernel stack.. */
  165. regs->eflags |= TRAP_FLAG;
  166. /*
  167. * ..but if TF is changed by the instruction we will trace,
  168. * don't mark it as being "us" that set it, so that we
  169. * won't clear it by hand later.
  170. */
  171. if (is_setting_trap_flag(child, regs))
  172. return;
  173. child->ptrace |= PT_DTRACE;
  174. }
  175. static void clear_singlestep(struct task_struct *child)
  176. {
  177. /* Always clear TIF_SINGLESTEP... */
  178. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  179. /* But touch TF only if it was set by us.. */
  180. if (child->ptrace & PT_DTRACE) {
  181. struct pt_regs *regs = task_pt_regs(child);
  182. regs->eflags &= ~TRAP_FLAG;
  183. child->ptrace &= ~PT_DTRACE;
  184. }
  185. }
  186. /*
  187. * Called by kernel/ptrace.c when detaching..
  188. *
  189. * Make sure the single step bit is not set.
  190. */
  191. void ptrace_disable(struct task_struct *child)
  192. {
  193. clear_singlestep(child);
  194. }
  195. static int putreg(struct task_struct *child,
  196. unsigned long regno, unsigned long value)
  197. {
  198. unsigned long tmp;
  199. /* Some code in the 64bit emulation may not be 64bit clean.
  200. Don't take any chances. */
  201. if (test_tsk_thread_flag(child, TIF_IA32))
  202. value &= 0xffffffff;
  203. switch (regno) {
  204. case offsetof(struct user_regs_struct,fs):
  205. if (value && (value & 3) != 3)
  206. return -EIO;
  207. child->thread.fsindex = value & 0xffff;
  208. return 0;
  209. case offsetof(struct user_regs_struct,gs):
  210. if (value && (value & 3) != 3)
  211. return -EIO;
  212. child->thread.gsindex = value & 0xffff;
  213. return 0;
  214. case offsetof(struct user_regs_struct,ds):
  215. if (value && (value & 3) != 3)
  216. return -EIO;
  217. child->thread.ds = value & 0xffff;
  218. return 0;
  219. case offsetof(struct user_regs_struct,es):
  220. if (value && (value & 3) != 3)
  221. return -EIO;
  222. child->thread.es = value & 0xffff;
  223. return 0;
  224. case offsetof(struct user_regs_struct,ss):
  225. if ((value & 3) != 3)
  226. return -EIO;
  227. value &= 0xffff;
  228. return 0;
  229. case offsetof(struct user_regs_struct,fs_base):
  230. if (value >= TASK_SIZE_OF(child))
  231. return -EIO;
  232. child->thread.fs = value;
  233. return 0;
  234. case offsetof(struct user_regs_struct,gs_base):
  235. if (value >= TASK_SIZE_OF(child))
  236. return -EIO;
  237. child->thread.gs = value;
  238. return 0;
  239. case offsetof(struct user_regs_struct, eflags):
  240. value &= FLAG_MASK;
  241. tmp = get_stack_long(child, EFL_OFFSET);
  242. tmp &= ~FLAG_MASK;
  243. value |= tmp;
  244. break;
  245. case offsetof(struct user_regs_struct,cs):
  246. if ((value & 3) != 3)
  247. return -EIO;
  248. value &= 0xffff;
  249. break;
  250. }
  251. put_stack_long(child, regno - sizeof(struct pt_regs), value);
  252. return 0;
  253. }
  254. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  255. {
  256. unsigned long val;
  257. switch (regno) {
  258. case offsetof(struct user_regs_struct, fs):
  259. return child->thread.fsindex;
  260. case offsetof(struct user_regs_struct, gs):
  261. return child->thread.gsindex;
  262. case offsetof(struct user_regs_struct, ds):
  263. return child->thread.ds;
  264. case offsetof(struct user_regs_struct, es):
  265. return child->thread.es;
  266. case offsetof(struct user_regs_struct, fs_base):
  267. return child->thread.fs;
  268. case offsetof(struct user_regs_struct, gs_base):
  269. return child->thread.gs;
  270. default:
  271. regno = regno - sizeof(struct pt_regs);
  272. val = get_stack_long(child, regno);
  273. if (test_tsk_thread_flag(child, TIF_IA32))
  274. val &= 0xffffffff;
  275. return val;
  276. }
  277. }
  278. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  279. {
  280. long i, ret;
  281. unsigned ui;
  282. switch (request) {
  283. /* when I and D space are separate, these will need to be fixed. */
  284. case PTRACE_PEEKTEXT: /* read word at location addr. */
  285. case PTRACE_PEEKDATA:
  286. ret = generic_ptrace_peekdata(child, addr, data);
  287. break;
  288. /* read the word at location addr in the USER area. */
  289. case PTRACE_PEEKUSR: {
  290. unsigned long tmp;
  291. ret = -EIO;
  292. if ((addr & 7) ||
  293. addr > sizeof(struct user) - 7)
  294. break;
  295. switch (addr) {
  296. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  297. tmp = getreg(child, addr);
  298. break;
  299. case offsetof(struct user, u_debugreg[0]):
  300. tmp = child->thread.debugreg0;
  301. break;
  302. case offsetof(struct user, u_debugreg[1]):
  303. tmp = child->thread.debugreg1;
  304. break;
  305. case offsetof(struct user, u_debugreg[2]):
  306. tmp = child->thread.debugreg2;
  307. break;
  308. case offsetof(struct user, u_debugreg[3]):
  309. tmp = child->thread.debugreg3;
  310. break;
  311. case offsetof(struct user, u_debugreg[6]):
  312. tmp = child->thread.debugreg6;
  313. break;
  314. case offsetof(struct user, u_debugreg[7]):
  315. tmp = child->thread.debugreg7;
  316. break;
  317. default:
  318. tmp = 0;
  319. break;
  320. }
  321. ret = put_user(tmp,(unsigned long __user *) data);
  322. break;
  323. }
  324. /* when I and D space are separate, this will have to be fixed. */
  325. case PTRACE_POKETEXT: /* write the word at location addr. */
  326. case PTRACE_POKEDATA:
  327. ret = generic_ptrace_pokedata(child, addr, data);
  328. break;
  329. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  330. {
  331. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  332. ret = -EIO;
  333. if ((addr & 7) ||
  334. addr > sizeof(struct user) - 7)
  335. break;
  336. switch (addr) {
  337. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  338. ret = putreg(child, addr, data);
  339. break;
  340. /* Disallows to set a breakpoint into the vsyscall */
  341. case offsetof(struct user, u_debugreg[0]):
  342. if (data >= TASK_SIZE_OF(child) - dsize) break;
  343. child->thread.debugreg0 = data;
  344. ret = 0;
  345. break;
  346. case offsetof(struct user, u_debugreg[1]):
  347. if (data >= TASK_SIZE_OF(child) - dsize) break;
  348. child->thread.debugreg1 = data;
  349. ret = 0;
  350. break;
  351. case offsetof(struct user, u_debugreg[2]):
  352. if (data >= TASK_SIZE_OF(child) - dsize) break;
  353. child->thread.debugreg2 = data;
  354. ret = 0;
  355. break;
  356. case offsetof(struct user, u_debugreg[3]):
  357. if (data >= TASK_SIZE_OF(child) - dsize) break;
  358. child->thread.debugreg3 = data;
  359. ret = 0;
  360. break;
  361. case offsetof(struct user, u_debugreg[6]):
  362. if (data >> 32)
  363. break;
  364. child->thread.debugreg6 = data;
  365. ret = 0;
  366. break;
  367. case offsetof(struct user, u_debugreg[7]):
  368. /* See arch/i386/kernel/ptrace.c for an explanation of
  369. * this awkward check.*/
  370. data &= ~DR_CONTROL_RESERVED;
  371. for(i=0; i<4; i++)
  372. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  373. break;
  374. if (i == 4) {
  375. child->thread.debugreg7 = data;
  376. if (data)
  377. set_tsk_thread_flag(child, TIF_DEBUG);
  378. else
  379. clear_tsk_thread_flag(child, TIF_DEBUG);
  380. ret = 0;
  381. }
  382. break;
  383. }
  384. break;
  385. }
  386. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  387. case PTRACE_CONT: /* restart after signal. */
  388. ret = -EIO;
  389. if (!valid_signal(data))
  390. break;
  391. if (request == PTRACE_SYSCALL)
  392. set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  393. else
  394. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  395. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  396. child->exit_code = data;
  397. /* make sure the single step bit is not set. */
  398. clear_singlestep(child);
  399. wake_up_process(child);
  400. ret = 0;
  401. break;
  402. #ifdef CONFIG_IA32_EMULATION
  403. /* This makes only sense with 32bit programs. Allow a
  404. 64bit debugger to fully examine them too. Better
  405. don't use it against 64bit processes, use
  406. PTRACE_ARCH_PRCTL instead. */
  407. case PTRACE_SET_THREAD_AREA: {
  408. struct user_desc __user *p;
  409. int old;
  410. p = (struct user_desc __user *)data;
  411. get_user(old, &p->entry_number);
  412. put_user(addr, &p->entry_number);
  413. ret = do_set_thread_area(&child->thread, p);
  414. put_user(old, &p->entry_number);
  415. break;
  416. case PTRACE_GET_THREAD_AREA:
  417. p = (struct user_desc __user *)data;
  418. get_user(old, &p->entry_number);
  419. put_user(addr, &p->entry_number);
  420. ret = do_get_thread_area(&child->thread, p);
  421. put_user(old, &p->entry_number);
  422. break;
  423. }
  424. #endif
  425. /* normal 64bit interface to access TLS data.
  426. Works just like arch_prctl, except that the arguments
  427. are reversed. */
  428. case PTRACE_ARCH_PRCTL:
  429. ret = do_arch_prctl(child, data, addr);
  430. break;
  431. /*
  432. * make the child exit. Best I can do is send it a sigkill.
  433. * perhaps it should be put in the status that it wants to
  434. * exit.
  435. */
  436. case PTRACE_KILL:
  437. ret = 0;
  438. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  439. break;
  440. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  441. child->exit_code = SIGKILL;
  442. /* make sure the single step bit is not set. */
  443. clear_singlestep(child);
  444. wake_up_process(child);
  445. break;
  446. case PTRACE_SINGLESTEP: /* set the trap flag. */
  447. ret = -EIO;
  448. if (!valid_signal(data))
  449. break;
  450. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  451. set_singlestep(child);
  452. child->exit_code = data;
  453. /* give it a chance to run. */
  454. wake_up_process(child);
  455. ret = 0;
  456. break;
  457. case PTRACE_DETACH:
  458. /* detach a process that was attached. */
  459. ret = ptrace_detach(child, data);
  460. break;
  461. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  462. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  463. sizeof(struct user_regs_struct))) {
  464. ret = -EIO;
  465. break;
  466. }
  467. ret = 0;
  468. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  469. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  470. data += sizeof(long);
  471. }
  472. break;
  473. }
  474. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  475. unsigned long tmp;
  476. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  477. sizeof(struct user_regs_struct))) {
  478. ret = -EIO;
  479. break;
  480. }
  481. ret = 0;
  482. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  483. ret = __get_user(tmp, (unsigned long __user *) data);
  484. if (ret)
  485. break;
  486. ret = putreg(child, ui, tmp);
  487. if (ret)
  488. break;
  489. data += sizeof(long);
  490. }
  491. break;
  492. }
  493. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  494. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  495. sizeof(struct user_i387_struct))) {
  496. ret = -EIO;
  497. break;
  498. }
  499. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  500. break;
  501. }
  502. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  503. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  504. sizeof(struct user_i387_struct))) {
  505. ret = -EIO;
  506. break;
  507. }
  508. set_stopped_child_used_math(child);
  509. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  510. break;
  511. }
  512. default:
  513. ret = ptrace_request(child, request, addr, data);
  514. break;
  515. }
  516. return ret;
  517. }
  518. static void syscall_trace(struct pt_regs *regs)
  519. {
  520. #if 0
  521. printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  522. current->comm,
  523. regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
  524. current_thread_info()->flags, current->ptrace);
  525. #endif
  526. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  527. ? 0x80 : 0));
  528. /*
  529. * this isn't the same as continuing with a signal, but it will do
  530. * for normal use. strace only continues with a signal if the
  531. * stopping signal is not SIGTRAP. -brl
  532. */
  533. if (current->exit_code) {
  534. send_sig(current->exit_code, current, 1);
  535. current->exit_code = 0;
  536. }
  537. }
  538. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  539. {
  540. /* do the secure computing check first */
  541. secure_computing(regs->orig_rax);
  542. if (test_thread_flag(TIF_SYSCALL_TRACE)
  543. && (current->ptrace & PT_PTRACED))
  544. syscall_trace(regs);
  545. if (unlikely(current->audit_context)) {
  546. if (test_thread_flag(TIF_IA32)) {
  547. audit_syscall_entry(AUDIT_ARCH_I386,
  548. regs->orig_rax,
  549. regs->rbx, regs->rcx,
  550. regs->rdx, regs->rsi);
  551. } else {
  552. audit_syscall_entry(AUDIT_ARCH_X86_64,
  553. regs->orig_rax,
  554. regs->rdi, regs->rsi,
  555. regs->rdx, regs->r10);
  556. }
  557. }
  558. }
  559. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  560. {
  561. if (unlikely(current->audit_context))
  562. audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
  563. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  564. || test_thread_flag(TIF_SINGLESTEP))
  565. && (current->ptrace & PT_PTRACED))
  566. syscall_trace(regs);
  567. }