ptrace.c 16 KB

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