ptrace.c 16 KB

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