ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  4. *
  5. * Derived from "arch/m68k/kernel/ptrace.c"
  6. * Copyright (C) 1994 by Hamish Macdonald
  7. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  8. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  9. *
  10. * Modified by Cort Dougan ([email protected])
  11. * and Paul Mackerras ([email protected]).
  12. *
  13. * This file is subject to the terms and conditions of the GNU General
  14. * Public License. See the file README.legal in the main directory of
  15. * this archive for more details.
  16. */
  17. #include <linux/regset.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/audit.h>
  20. #include <linux/context_tracking.h>
  21. #include <linux/syscalls.h>
  22. #include <asm/switch_to.h>
  23. #include <asm/debug.h>
  24. #define CREATE_TRACE_POINTS
  25. #include <trace/events/syscalls.h>
  26. #include "ptrace-decl.h"
  27. /*
  28. * Called by kernel/ptrace.c when detaching..
  29. *
  30. * Make sure single step bits etc are not set.
  31. */
  32. void ptrace_disable(struct task_struct *child)
  33. {
  34. /* make sure the single step bit is not set. */
  35. user_disable_single_step(child);
  36. }
  37. long arch_ptrace(struct task_struct *child, long request,
  38. unsigned long addr, unsigned long data)
  39. {
  40. int ret = -EPERM;
  41. void __user *datavp = (void __user *) data;
  42. unsigned long __user *datalp = datavp;
  43. switch (request) {
  44. /* read the word at location addr in the USER area. */
  45. case PTRACE_PEEKUSR: {
  46. unsigned long index, tmp;
  47. ret = -EIO;
  48. /* convert to index and check */
  49. index = addr / sizeof(long);
  50. if ((addr & (sizeof(long) - 1)) || !child->thread.regs)
  51. break;
  52. if (index < PT_FPR0)
  53. ret = ptrace_get_reg(child, (int) index, &tmp);
  54. else
  55. ret = ptrace_get_fpr(child, index, &tmp);
  56. if (ret)
  57. break;
  58. ret = put_user(tmp, datalp);
  59. break;
  60. }
  61. /* write the word at location addr in the USER area */
  62. case PTRACE_POKEUSR: {
  63. unsigned long index;
  64. ret = -EIO;
  65. /* convert to index and check */
  66. index = addr / sizeof(long);
  67. if ((addr & (sizeof(long) - 1)) || !child->thread.regs)
  68. break;
  69. if (index < PT_FPR0)
  70. ret = ptrace_put_reg(child, index, data);
  71. else
  72. ret = ptrace_put_fpr(child, index, data);
  73. break;
  74. }
  75. case PPC_PTRACE_GETHWDBGINFO: {
  76. struct ppc_debug_info dbginfo;
  77. ppc_gethwdinfo(&dbginfo);
  78. if (copy_to_user(datavp, &dbginfo,
  79. sizeof(struct ppc_debug_info)))
  80. return -EFAULT;
  81. return 0;
  82. }
  83. case PPC_PTRACE_SETHWDEBUG: {
  84. struct ppc_hw_breakpoint bp_info;
  85. if (copy_from_user(&bp_info, datavp,
  86. sizeof(struct ppc_hw_breakpoint)))
  87. return -EFAULT;
  88. return ppc_set_hwdebug(child, &bp_info);
  89. }
  90. case PPC_PTRACE_DELHWDEBUG: {
  91. ret = ppc_del_hwdebug(child, data);
  92. break;
  93. }
  94. case PTRACE_GET_DEBUGREG:
  95. ret = ptrace_get_debugreg(child, addr, datalp);
  96. break;
  97. case PTRACE_SET_DEBUGREG:
  98. ret = ptrace_set_debugreg(child, addr, data);
  99. break;
  100. #ifdef CONFIG_PPC64
  101. case PTRACE_GETREGS64:
  102. #endif
  103. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  104. return copy_regset_to_user(child, &user_ppc_native_view,
  105. REGSET_GPR,
  106. 0, sizeof(struct user_pt_regs),
  107. datavp);
  108. #ifdef CONFIG_PPC64
  109. case PTRACE_SETREGS64:
  110. #endif
  111. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  112. return copy_regset_from_user(child, &user_ppc_native_view,
  113. REGSET_GPR,
  114. 0, sizeof(struct user_pt_regs),
  115. datavp);
  116. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  117. return copy_regset_to_user(child, &user_ppc_native_view,
  118. REGSET_FPR,
  119. 0, sizeof(elf_fpregset_t),
  120. datavp);
  121. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  122. return copy_regset_from_user(child, &user_ppc_native_view,
  123. REGSET_FPR,
  124. 0, sizeof(elf_fpregset_t),
  125. datavp);
  126. #ifdef CONFIG_ALTIVEC
  127. case PTRACE_GETVRREGS:
  128. return copy_regset_to_user(child, &user_ppc_native_view,
  129. REGSET_VMX,
  130. 0, (33 * sizeof(vector128) +
  131. sizeof(u32)),
  132. datavp);
  133. case PTRACE_SETVRREGS:
  134. return copy_regset_from_user(child, &user_ppc_native_view,
  135. REGSET_VMX,
  136. 0, (33 * sizeof(vector128) +
  137. sizeof(u32)),
  138. datavp);
  139. #endif
  140. #ifdef CONFIG_VSX
  141. case PTRACE_GETVSRREGS:
  142. return copy_regset_to_user(child, &user_ppc_native_view,
  143. REGSET_VSX,
  144. 0, 32 * sizeof(double),
  145. datavp);
  146. case PTRACE_SETVSRREGS:
  147. return copy_regset_from_user(child, &user_ppc_native_view,
  148. REGSET_VSX,
  149. 0, 32 * sizeof(double),
  150. datavp);
  151. #endif
  152. #ifdef CONFIG_SPE
  153. case PTRACE_GETEVRREGS:
  154. /* Get the child spe register state. */
  155. return copy_regset_to_user(child, &user_ppc_native_view,
  156. REGSET_SPE, 0, 35 * sizeof(u32),
  157. datavp);
  158. case PTRACE_SETEVRREGS:
  159. /* Set the child spe register state. */
  160. return copy_regset_from_user(child, &user_ppc_native_view,
  161. REGSET_SPE, 0, 35 * sizeof(u32),
  162. datavp);
  163. #endif
  164. default:
  165. ret = ptrace_request(child, request, addr, data);
  166. break;
  167. }
  168. return ret;
  169. }
  170. #ifdef CONFIG_SECCOMP
  171. static int do_seccomp(struct pt_regs *regs)
  172. {
  173. if (!test_thread_flag(TIF_SECCOMP))
  174. return 0;
  175. /*
  176. * The ABI we present to seccomp tracers is that r3 contains
  177. * the syscall return value and orig_gpr3 contains the first
  178. * syscall parameter. This is different to the ptrace ABI where
  179. * both r3 and orig_gpr3 contain the first syscall parameter.
  180. */
  181. regs->gpr[3] = -ENOSYS;
  182. /*
  183. * We use the __ version here because we have already checked
  184. * TIF_SECCOMP. If this fails, there is nothing left to do, we
  185. * have already loaded -ENOSYS into r3, or seccomp has put
  186. * something else in r3 (via SECCOMP_RET_ERRNO/TRACE).
  187. */
  188. if (__secure_computing(NULL))
  189. return -1;
  190. /*
  191. * The syscall was allowed by seccomp, restore the register
  192. * state to what audit expects.
  193. * Note that we use orig_gpr3, which means a seccomp tracer can
  194. * modify the first syscall parameter (in orig_gpr3) and also
  195. * allow the syscall to proceed.
  196. */
  197. regs->gpr[3] = regs->orig_gpr3;
  198. return 0;
  199. }
  200. #else
  201. static inline int do_seccomp(struct pt_regs *regs) { return 0; }
  202. #endif /* CONFIG_SECCOMP */
  203. /**
  204. * do_syscall_trace_enter() - Do syscall tracing on kernel entry.
  205. * @regs: the pt_regs of the task to trace (current)
  206. *
  207. * Performs various types of tracing on syscall entry. This includes seccomp,
  208. * ptrace, syscall tracepoints and audit.
  209. *
  210. * The pt_regs are potentially visible to userspace via ptrace, so their
  211. * contents is ABI.
  212. *
  213. * One or more of the tracers may modify the contents of pt_regs, in particular
  214. * to modify arguments or even the syscall number itself.
  215. *
  216. * It's also possible that a tracer can choose to reject the system call. In
  217. * that case this function will return an illegal syscall number, and will put
  218. * an appropriate return value in regs->r3.
  219. *
  220. * Return: the (possibly changed) syscall number.
  221. */
  222. long do_syscall_trace_enter(struct pt_regs *regs)
  223. {
  224. u32 flags;
  225. flags = read_thread_flags() & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE);
  226. if (flags) {
  227. int rc = ptrace_report_syscall_entry(regs);
  228. if (unlikely(flags & _TIF_SYSCALL_EMU)) {
  229. /*
  230. * A nonzero return code from
  231. * ptrace_report_syscall_entry() tells us to prevent
  232. * the syscall execution, but we are not going to
  233. * execute it anyway.
  234. *
  235. * Returning -1 will skip the syscall execution. We want
  236. * to avoid clobbering any registers, so we don't goto
  237. * the skip label below.
  238. */
  239. return -1;
  240. }
  241. if (rc) {
  242. /*
  243. * The tracer decided to abort the syscall. Note that
  244. * the tracer may also just change regs->gpr[0] to an
  245. * invalid syscall number, that is handled below on the
  246. * exit path.
  247. */
  248. goto skip;
  249. }
  250. }
  251. /* Run seccomp after ptrace; allow it to set gpr[3]. */
  252. if (do_seccomp(regs))
  253. return -1;
  254. /* Avoid trace and audit when syscall is invalid. */
  255. if (regs->gpr[0] >= NR_syscalls)
  256. goto skip;
  257. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  258. trace_sys_enter(regs, regs->gpr[0]);
  259. if (!is_32bit_task())
  260. audit_syscall_entry(regs->gpr[0], regs->gpr[3], regs->gpr[4],
  261. regs->gpr[5], regs->gpr[6]);
  262. else
  263. audit_syscall_entry(regs->gpr[0],
  264. regs->gpr[3] & 0xffffffff,
  265. regs->gpr[4] & 0xffffffff,
  266. regs->gpr[5] & 0xffffffff,
  267. regs->gpr[6] & 0xffffffff);
  268. /* Return the possibly modified but valid syscall number */
  269. return regs->gpr[0];
  270. skip:
  271. /*
  272. * If we are aborting explicitly, or if the syscall number is
  273. * now invalid, set the return value to -ENOSYS.
  274. */
  275. regs->gpr[3] = -ENOSYS;
  276. return -1;
  277. }
  278. void do_syscall_trace_leave(struct pt_regs *regs)
  279. {
  280. int step;
  281. audit_syscall_exit(regs);
  282. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  283. trace_sys_exit(regs, regs->result);
  284. step = test_thread_flag(TIF_SINGLESTEP);
  285. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  286. ptrace_report_syscall_exit(regs, step);
  287. }
  288. void __init pt_regs_check(void);
  289. /*
  290. * Dummy function, its purpose is to break the build if struct pt_regs and
  291. * struct user_pt_regs don't match.
  292. */
  293. void __init pt_regs_check(void)
  294. {
  295. BUILD_BUG_ON(offsetof(struct pt_regs, gpr) !=
  296. offsetof(struct user_pt_regs, gpr));
  297. BUILD_BUG_ON(offsetof(struct pt_regs, nip) !=
  298. offsetof(struct user_pt_regs, nip));
  299. BUILD_BUG_ON(offsetof(struct pt_regs, msr) !=
  300. offsetof(struct user_pt_regs, msr));
  301. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  302. offsetof(struct user_pt_regs, orig_gpr3));
  303. BUILD_BUG_ON(offsetof(struct pt_regs, ctr) !=
  304. offsetof(struct user_pt_regs, ctr));
  305. BUILD_BUG_ON(offsetof(struct pt_regs, link) !=
  306. offsetof(struct user_pt_regs, link));
  307. BUILD_BUG_ON(offsetof(struct pt_regs, xer) !=
  308. offsetof(struct user_pt_regs, xer));
  309. BUILD_BUG_ON(offsetof(struct pt_regs, ccr) !=
  310. offsetof(struct user_pt_regs, ccr));
  311. #ifdef __powerpc64__
  312. BUILD_BUG_ON(offsetof(struct pt_regs, softe) !=
  313. offsetof(struct user_pt_regs, softe));
  314. #else
  315. BUILD_BUG_ON(offsetof(struct pt_regs, mq) !=
  316. offsetof(struct user_pt_regs, mq));
  317. #endif
  318. BUILD_BUG_ON(offsetof(struct pt_regs, trap) !=
  319. offsetof(struct user_pt_regs, trap));
  320. BUILD_BUG_ON(offsetof(struct pt_regs, dar) !=
  321. offsetof(struct user_pt_regs, dar));
  322. BUILD_BUG_ON(offsetof(struct pt_regs, dear) !=
  323. offsetof(struct user_pt_regs, dar));
  324. BUILD_BUG_ON(offsetof(struct pt_regs, dsisr) !=
  325. offsetof(struct user_pt_regs, dsisr));
  326. BUILD_BUG_ON(offsetof(struct pt_regs, esr) !=
  327. offsetof(struct user_pt_regs, dsisr));
  328. BUILD_BUG_ON(offsetof(struct pt_regs, result) !=
  329. offsetof(struct user_pt_regs, result));
  330. BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs));
  331. // Now check that the pt_regs offsets match the uapi #defines
  332. #define CHECK_REG(_pt, _reg) \
  333. BUILD_BUG_ON(_pt != (offsetof(struct user_pt_regs, _reg) / \
  334. sizeof(unsigned long)));
  335. CHECK_REG(PT_R0, gpr[0]);
  336. CHECK_REG(PT_R1, gpr[1]);
  337. CHECK_REG(PT_R2, gpr[2]);
  338. CHECK_REG(PT_R3, gpr[3]);
  339. CHECK_REG(PT_R4, gpr[4]);
  340. CHECK_REG(PT_R5, gpr[5]);
  341. CHECK_REG(PT_R6, gpr[6]);
  342. CHECK_REG(PT_R7, gpr[7]);
  343. CHECK_REG(PT_R8, gpr[8]);
  344. CHECK_REG(PT_R9, gpr[9]);
  345. CHECK_REG(PT_R10, gpr[10]);
  346. CHECK_REG(PT_R11, gpr[11]);
  347. CHECK_REG(PT_R12, gpr[12]);
  348. CHECK_REG(PT_R13, gpr[13]);
  349. CHECK_REG(PT_R14, gpr[14]);
  350. CHECK_REG(PT_R15, gpr[15]);
  351. CHECK_REG(PT_R16, gpr[16]);
  352. CHECK_REG(PT_R17, gpr[17]);
  353. CHECK_REG(PT_R18, gpr[18]);
  354. CHECK_REG(PT_R19, gpr[19]);
  355. CHECK_REG(PT_R20, gpr[20]);
  356. CHECK_REG(PT_R21, gpr[21]);
  357. CHECK_REG(PT_R22, gpr[22]);
  358. CHECK_REG(PT_R23, gpr[23]);
  359. CHECK_REG(PT_R24, gpr[24]);
  360. CHECK_REG(PT_R25, gpr[25]);
  361. CHECK_REG(PT_R26, gpr[26]);
  362. CHECK_REG(PT_R27, gpr[27]);
  363. CHECK_REG(PT_R28, gpr[28]);
  364. CHECK_REG(PT_R29, gpr[29]);
  365. CHECK_REG(PT_R30, gpr[30]);
  366. CHECK_REG(PT_R31, gpr[31]);
  367. CHECK_REG(PT_NIP, nip);
  368. CHECK_REG(PT_MSR, msr);
  369. CHECK_REG(PT_ORIG_R3, orig_gpr3);
  370. CHECK_REG(PT_CTR, ctr);
  371. CHECK_REG(PT_LNK, link);
  372. CHECK_REG(PT_XER, xer);
  373. CHECK_REG(PT_CCR, ccr);
  374. #ifdef CONFIG_PPC64
  375. CHECK_REG(PT_SOFTE, softe);
  376. #else
  377. CHECK_REG(PT_MQ, mq);
  378. #endif
  379. CHECK_REG(PT_TRAP, trap);
  380. CHECK_REG(PT_DAR, dar);
  381. CHECK_REG(PT_DSISR, dsisr);
  382. CHECK_REG(PT_RESULT, result);
  383. #undef CHECK_REG
  384. BUILD_BUG_ON(PT_REGS_COUNT != sizeof(struct user_pt_regs) / sizeof(unsigned long));
  385. /*
  386. * PT_DSCR isn't a real reg, but it's important that it doesn't overlap the
  387. * real registers.
  388. */
  389. BUILD_BUG_ON(PT_DSCR < sizeof(struct user_pt_regs) / sizeof(unsigned long));
  390. // ptrace_get/put_fpr() rely on PPC32 and VSX being incompatible
  391. BUILD_BUG_ON(IS_ENABLED(CONFIG_PPC32) && IS_ENABLED(CONFIG_VSX));
  392. }