ptrace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. switch (regno) {
  200. case offsetof(struct user_regs_struct,fs):
  201. if (value && (value & 3) != 3)
  202. return -EIO;
  203. child->thread.fsindex = value & 0xffff;
  204. return 0;
  205. case offsetof(struct user_regs_struct,gs):
  206. if (value && (value & 3) != 3)
  207. return -EIO;
  208. child->thread.gsindex = value & 0xffff;
  209. return 0;
  210. case offsetof(struct user_regs_struct,ds):
  211. if (value && (value & 3) != 3)
  212. return -EIO;
  213. child->thread.ds = value & 0xffff;
  214. return 0;
  215. case offsetof(struct user_regs_struct,es):
  216. if (value && (value & 3) != 3)
  217. return -EIO;
  218. child->thread.es = value & 0xffff;
  219. return 0;
  220. case offsetof(struct user_regs_struct,ss):
  221. if ((value & 3) != 3)
  222. return -EIO;
  223. value &= 0xffff;
  224. return 0;
  225. case offsetof(struct user_regs_struct,fs_base):
  226. if (value >= TASK_SIZE_OF(child))
  227. return -EIO;
  228. child->thread.fs = value;
  229. return 0;
  230. case offsetof(struct user_regs_struct,gs_base):
  231. if (value >= TASK_SIZE_OF(child))
  232. return -EIO;
  233. child->thread.gs = value;
  234. return 0;
  235. case offsetof(struct user_regs_struct, eflags):
  236. value &= FLAG_MASK;
  237. tmp = get_stack_long(child, EFL_OFFSET);
  238. tmp &= ~FLAG_MASK;
  239. value |= tmp;
  240. break;
  241. case offsetof(struct user_regs_struct,cs):
  242. if ((value & 3) != 3)
  243. return -EIO;
  244. value &= 0xffff;
  245. break;
  246. }
  247. put_stack_long(child, regno - sizeof(struct pt_regs), value);
  248. return 0;
  249. }
  250. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  251. {
  252. unsigned long val;
  253. switch (regno) {
  254. case offsetof(struct user_regs_struct, fs):
  255. return child->thread.fsindex;
  256. case offsetof(struct user_regs_struct, gs):
  257. return child->thread.gsindex;
  258. case offsetof(struct user_regs_struct, ds):
  259. return child->thread.ds;
  260. case offsetof(struct user_regs_struct, es):
  261. return child->thread.es;
  262. case offsetof(struct user_regs_struct, fs_base):
  263. return child->thread.fs;
  264. case offsetof(struct user_regs_struct, gs_base):
  265. return child->thread.gs;
  266. default:
  267. regno = regno - sizeof(struct pt_regs);
  268. val = get_stack_long(child, regno);
  269. if (test_tsk_thread_flag(child, TIF_IA32))
  270. val &= 0xffffffff;
  271. return val;
  272. }
  273. }
  274. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  275. {
  276. long i, ret;
  277. unsigned ui;
  278. switch (request) {
  279. /* when I and D space are separate, these will need to be fixed. */
  280. case PTRACE_PEEKTEXT: /* read word at location addr. */
  281. case PTRACE_PEEKDATA:
  282. ret = generic_ptrace_peekdata(child, addr, data);
  283. break;
  284. /* read the word at location addr in the USER area. */
  285. case PTRACE_PEEKUSR: {
  286. unsigned long tmp;
  287. ret = -EIO;
  288. if ((addr & 7) ||
  289. addr > sizeof(struct user) - 7)
  290. break;
  291. switch (addr) {
  292. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  293. tmp = getreg(child, addr);
  294. break;
  295. case offsetof(struct user, u_debugreg[0]):
  296. tmp = child->thread.debugreg0;
  297. break;
  298. case offsetof(struct user, u_debugreg[1]):
  299. tmp = child->thread.debugreg1;
  300. break;
  301. case offsetof(struct user, u_debugreg[2]):
  302. tmp = child->thread.debugreg2;
  303. break;
  304. case offsetof(struct user, u_debugreg[3]):
  305. tmp = child->thread.debugreg3;
  306. break;
  307. case offsetof(struct user, u_debugreg[6]):
  308. tmp = child->thread.debugreg6;
  309. break;
  310. case offsetof(struct user, u_debugreg[7]):
  311. tmp = child->thread.debugreg7;
  312. break;
  313. default:
  314. tmp = 0;
  315. break;
  316. }
  317. ret = put_user(tmp,(unsigned long __user *) data);
  318. break;
  319. }
  320. /* when I and D space are separate, this will have to be fixed. */
  321. case PTRACE_POKETEXT: /* write the word at location addr. */
  322. case PTRACE_POKEDATA:
  323. ret = generic_ptrace_pokedata(child, addr, data);
  324. break;
  325. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  326. {
  327. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  328. ret = -EIO;
  329. if ((addr & 7) ||
  330. addr > sizeof(struct user) - 7)
  331. break;
  332. switch (addr) {
  333. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  334. ret = putreg(child, addr, data);
  335. break;
  336. /* Disallows to set a breakpoint into the vsyscall */
  337. case offsetof(struct user, u_debugreg[0]):
  338. if (data >= TASK_SIZE_OF(child) - dsize) break;
  339. child->thread.debugreg0 = data;
  340. ret = 0;
  341. break;
  342. case offsetof(struct user, u_debugreg[1]):
  343. if (data >= TASK_SIZE_OF(child) - dsize) break;
  344. child->thread.debugreg1 = data;
  345. ret = 0;
  346. break;
  347. case offsetof(struct user, u_debugreg[2]):
  348. if (data >= TASK_SIZE_OF(child) - dsize) break;
  349. child->thread.debugreg2 = data;
  350. ret = 0;
  351. break;
  352. case offsetof(struct user, u_debugreg[3]):
  353. if (data >= TASK_SIZE_OF(child) - dsize) break;
  354. child->thread.debugreg3 = data;
  355. ret = 0;
  356. break;
  357. case offsetof(struct user, u_debugreg[6]):
  358. if (data >> 32)
  359. break;
  360. child->thread.debugreg6 = data;
  361. ret = 0;
  362. break;
  363. case offsetof(struct user, u_debugreg[7]):
  364. /* See arch/i386/kernel/ptrace.c for an explanation of
  365. * this awkward check.*/
  366. data &= ~DR_CONTROL_RESERVED;
  367. for(i=0; i<4; i++)
  368. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  369. break;
  370. if (i == 4) {
  371. child->thread.debugreg7 = data;
  372. if (data)
  373. set_tsk_thread_flag(child, TIF_DEBUG);
  374. else
  375. clear_tsk_thread_flag(child, TIF_DEBUG);
  376. ret = 0;
  377. }
  378. break;
  379. }
  380. break;
  381. }
  382. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  383. case PTRACE_CONT: /* restart after signal. */
  384. ret = -EIO;
  385. if (!valid_signal(data))
  386. break;
  387. if (request == PTRACE_SYSCALL)
  388. set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  389. else
  390. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  391. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  392. child->exit_code = data;
  393. /* make sure the single step bit is not set. */
  394. clear_singlestep(child);
  395. wake_up_process(child);
  396. ret = 0;
  397. break;
  398. #ifdef CONFIG_IA32_EMULATION
  399. /* This makes only sense with 32bit programs. Allow a
  400. 64bit debugger to fully examine them too. Better
  401. don't use it against 64bit processes, use
  402. PTRACE_ARCH_PRCTL instead. */
  403. case PTRACE_SET_THREAD_AREA: {
  404. struct user_desc __user *p;
  405. int old;
  406. p = (struct user_desc __user *)data;
  407. get_user(old, &p->entry_number);
  408. put_user(addr, &p->entry_number);
  409. ret = do_set_thread_area(&child->thread, p);
  410. put_user(old, &p->entry_number);
  411. break;
  412. case PTRACE_GET_THREAD_AREA:
  413. p = (struct user_desc __user *)data;
  414. get_user(old, &p->entry_number);
  415. put_user(addr, &p->entry_number);
  416. ret = do_get_thread_area(&child->thread, p);
  417. put_user(old, &p->entry_number);
  418. break;
  419. }
  420. #endif
  421. /* normal 64bit interface to access TLS data.
  422. Works just like arch_prctl, except that the arguments
  423. are reversed. */
  424. case PTRACE_ARCH_PRCTL:
  425. ret = do_arch_prctl(child, data, addr);
  426. break;
  427. /*
  428. * make the child exit. Best I can do is send it a sigkill.
  429. * perhaps it should be put in the status that it wants to
  430. * exit.
  431. */
  432. case PTRACE_KILL:
  433. ret = 0;
  434. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  435. break;
  436. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  437. child->exit_code = SIGKILL;
  438. /* make sure the single step bit is not set. */
  439. clear_singlestep(child);
  440. wake_up_process(child);
  441. break;
  442. case PTRACE_SINGLESTEP: /* set the trap flag. */
  443. ret = -EIO;
  444. if (!valid_signal(data))
  445. break;
  446. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  447. set_singlestep(child);
  448. child->exit_code = data;
  449. /* give it a chance to run. */
  450. wake_up_process(child);
  451. ret = 0;
  452. break;
  453. case PTRACE_DETACH:
  454. /* detach a process that was attached. */
  455. ret = ptrace_detach(child, data);
  456. break;
  457. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  458. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  459. sizeof(struct user_regs_struct))) {
  460. ret = -EIO;
  461. break;
  462. }
  463. ret = 0;
  464. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  465. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  466. data += sizeof(long);
  467. }
  468. break;
  469. }
  470. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  471. unsigned long tmp;
  472. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  473. sizeof(struct user_regs_struct))) {
  474. ret = -EIO;
  475. break;
  476. }
  477. ret = 0;
  478. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  479. ret = __get_user(tmp, (unsigned long __user *) data);
  480. if (ret)
  481. break;
  482. ret = putreg(child, ui, tmp);
  483. if (ret)
  484. break;
  485. data += sizeof(long);
  486. }
  487. break;
  488. }
  489. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  490. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  491. sizeof(struct user_i387_struct))) {
  492. ret = -EIO;
  493. break;
  494. }
  495. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  496. break;
  497. }
  498. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  499. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  500. sizeof(struct user_i387_struct))) {
  501. ret = -EIO;
  502. break;
  503. }
  504. set_stopped_child_used_math(child);
  505. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  506. break;
  507. }
  508. default:
  509. ret = ptrace_request(child, request, addr, data);
  510. break;
  511. }
  512. return ret;
  513. }
  514. static void syscall_trace(struct pt_regs *regs)
  515. {
  516. #if 0
  517. printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  518. current->comm,
  519. regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
  520. current_thread_info()->flags, current->ptrace);
  521. #endif
  522. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  523. ? 0x80 : 0));
  524. /*
  525. * this isn't the same as continuing with a signal, but it will do
  526. * for normal use. strace only continues with a signal if the
  527. * stopping signal is not SIGTRAP. -brl
  528. */
  529. if (current->exit_code) {
  530. send_sig(current->exit_code, current, 1);
  531. current->exit_code = 0;
  532. }
  533. }
  534. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  535. {
  536. /* do the secure computing check first */
  537. secure_computing(regs->orig_rax);
  538. if (test_thread_flag(TIF_SYSCALL_TRACE)
  539. && (current->ptrace & PT_PTRACED))
  540. syscall_trace(regs);
  541. if (unlikely(current->audit_context)) {
  542. if (test_thread_flag(TIF_IA32)) {
  543. audit_syscall_entry(AUDIT_ARCH_I386,
  544. regs->orig_rax,
  545. regs->rbx, regs->rcx,
  546. regs->rdx, regs->rsi);
  547. } else {
  548. audit_syscall_entry(AUDIT_ARCH_X86_64,
  549. regs->orig_rax,
  550. regs->rdi, regs->rsi,
  551. regs->rdx, regs->r10);
  552. }
  553. }
  554. }
  555. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  556. {
  557. if (unlikely(current->audit_context))
  558. audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
  559. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  560. || test_thread_flag(TIF_SINGLESTEP))
  561. && (current->ptrace & PT_PTRACED))
  562. syscall_trace(regs);
  563. }