ptrace.c 16 KB

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