ptrace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. unsigned long tmp;
  280. int copied;
  281. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  282. ret = -EIO;
  283. if (copied != sizeof(tmp))
  284. break;
  285. ret = put_user(tmp,(unsigned long __user *) data);
  286. break;
  287. }
  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 = 0;
  328. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  329. break;
  330. ret = -EIO;
  331. break;
  332. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  333. {
  334. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  335. ret = -EIO;
  336. if ((addr & 7) ||
  337. addr > sizeof(struct user) - 7)
  338. break;
  339. switch (addr) {
  340. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  341. ret = putreg(child, addr, data);
  342. break;
  343. /* Disallows to set a breakpoint into the vsyscall */
  344. case offsetof(struct user, u_debugreg[0]):
  345. if (data >= TASK_SIZE_OF(child) - dsize) break;
  346. child->thread.debugreg0 = data;
  347. ret = 0;
  348. break;
  349. case offsetof(struct user, u_debugreg[1]):
  350. if (data >= TASK_SIZE_OF(child) - dsize) break;
  351. child->thread.debugreg1 = data;
  352. ret = 0;
  353. break;
  354. case offsetof(struct user, u_debugreg[2]):
  355. if (data >= TASK_SIZE_OF(child) - dsize) break;
  356. child->thread.debugreg2 = data;
  357. ret = 0;
  358. break;
  359. case offsetof(struct user, u_debugreg[3]):
  360. if (data >= TASK_SIZE_OF(child) - dsize) break;
  361. child->thread.debugreg3 = data;
  362. ret = 0;
  363. break;
  364. case offsetof(struct user, u_debugreg[6]):
  365. if (data >> 32)
  366. break;
  367. child->thread.debugreg6 = data;
  368. ret = 0;
  369. break;
  370. case offsetof(struct user, u_debugreg[7]):
  371. /* See arch/i386/kernel/ptrace.c for an explanation of
  372. * this awkward check.*/
  373. data &= ~DR_CONTROL_RESERVED;
  374. for(i=0; i<4; i++)
  375. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  376. break;
  377. if (i == 4) {
  378. child->thread.debugreg7 = data;
  379. if (data)
  380. set_tsk_thread_flag(child, TIF_DEBUG);
  381. else
  382. clear_tsk_thread_flag(child, TIF_DEBUG);
  383. ret = 0;
  384. }
  385. break;
  386. }
  387. break;
  388. }
  389. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  390. case PTRACE_CONT: /* restart after signal. */
  391. ret = -EIO;
  392. if (!valid_signal(data))
  393. break;
  394. if (request == PTRACE_SYSCALL)
  395. set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  396. else
  397. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  398. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  399. child->exit_code = data;
  400. /* make sure the single step bit is not set. */
  401. clear_singlestep(child);
  402. wake_up_process(child);
  403. ret = 0;
  404. break;
  405. #ifdef CONFIG_IA32_EMULATION
  406. /* This makes only sense with 32bit programs. Allow a
  407. 64bit debugger to fully examine them too. Better
  408. don't use it against 64bit processes, use
  409. PTRACE_ARCH_PRCTL instead. */
  410. case PTRACE_SET_THREAD_AREA: {
  411. struct user_desc __user *p;
  412. int old;
  413. p = (struct user_desc __user *)data;
  414. get_user(old, &p->entry_number);
  415. put_user(addr, &p->entry_number);
  416. ret = do_set_thread_area(&child->thread, p);
  417. put_user(old, &p->entry_number);
  418. break;
  419. case PTRACE_GET_THREAD_AREA:
  420. p = (struct user_desc __user *)data;
  421. get_user(old, &p->entry_number);
  422. put_user(addr, &p->entry_number);
  423. ret = do_get_thread_area(&child->thread, p);
  424. put_user(old, &p->entry_number);
  425. break;
  426. }
  427. #endif
  428. /* normal 64bit interface to access TLS data.
  429. Works just like arch_prctl, except that the arguments
  430. are reversed. */
  431. case PTRACE_ARCH_PRCTL:
  432. ret = do_arch_prctl(child, data, addr);
  433. break;
  434. /*
  435. * make the child exit. Best I can do is send it a sigkill.
  436. * perhaps it should be put in the status that it wants to
  437. * exit.
  438. */
  439. case PTRACE_KILL:
  440. ret = 0;
  441. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  442. break;
  443. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  444. child->exit_code = SIGKILL;
  445. /* make sure the single step bit is not set. */
  446. clear_singlestep(child);
  447. wake_up_process(child);
  448. break;
  449. case PTRACE_SINGLESTEP: /* set the trap flag. */
  450. ret = -EIO;
  451. if (!valid_signal(data))
  452. break;
  453. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  454. set_singlestep(child);
  455. child->exit_code = data;
  456. /* give it a chance to run. */
  457. wake_up_process(child);
  458. ret = 0;
  459. break;
  460. case PTRACE_DETACH:
  461. /* detach a process that was attached. */
  462. ret = ptrace_detach(child, data);
  463. break;
  464. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  465. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  466. sizeof(struct user_regs_struct))) {
  467. ret = -EIO;
  468. break;
  469. }
  470. ret = 0;
  471. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  472. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  473. data += sizeof(long);
  474. }
  475. break;
  476. }
  477. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  478. unsigned long tmp;
  479. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  480. sizeof(struct user_regs_struct))) {
  481. ret = -EIO;
  482. break;
  483. }
  484. ret = 0;
  485. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  486. ret = __get_user(tmp, (unsigned long __user *) data);
  487. if (ret)
  488. break;
  489. ret = putreg(child, ui, tmp);
  490. if (ret)
  491. break;
  492. data += sizeof(long);
  493. }
  494. break;
  495. }
  496. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  497. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  498. sizeof(struct user_i387_struct))) {
  499. ret = -EIO;
  500. break;
  501. }
  502. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  503. break;
  504. }
  505. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  506. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  507. sizeof(struct user_i387_struct))) {
  508. ret = -EIO;
  509. break;
  510. }
  511. set_stopped_child_used_math(child);
  512. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  513. break;
  514. }
  515. default:
  516. ret = ptrace_request(child, request, addr, data);
  517. break;
  518. }
  519. return ret;
  520. }
  521. static void syscall_trace(struct pt_regs *regs)
  522. {
  523. #if 0
  524. printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  525. current->comm,
  526. regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
  527. current_thread_info()->flags, current->ptrace);
  528. #endif
  529. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  530. ? 0x80 : 0));
  531. /*
  532. * this isn't the same as continuing with a signal, but it will do
  533. * for normal use. strace only continues with a signal if the
  534. * stopping signal is not SIGTRAP. -brl
  535. */
  536. if (current->exit_code) {
  537. send_sig(current->exit_code, current, 1);
  538. current->exit_code = 0;
  539. }
  540. }
  541. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  542. {
  543. /* do the secure computing check first */
  544. secure_computing(regs->orig_rax);
  545. if (test_thread_flag(TIF_SYSCALL_TRACE)
  546. && (current->ptrace & PT_PTRACED))
  547. syscall_trace(regs);
  548. if (unlikely(current->audit_context)) {
  549. if (test_thread_flag(TIF_IA32)) {
  550. audit_syscall_entry(AUDIT_ARCH_I386,
  551. regs->orig_rax,
  552. regs->rbx, regs->rcx,
  553. regs->rdx, regs->rsi);
  554. } else {
  555. audit_syscall_entry(AUDIT_ARCH_X86_64,
  556. regs->orig_rax,
  557. regs->rdi, regs->rsi,
  558. regs->rdx, regs->r10);
  559. }
  560. }
  561. }
  562. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  563. {
  564. if (unlikely(current->audit_context))
  565. audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
  566. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  567. || test_thread_flag(TIF_SINGLESTEP))
  568. && (current->ptrace & PT_PTRACED))
  569. syscall_trace(regs);
  570. }