ptrace.c 16 KB

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