common.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * common.c - C code for kernel entry and exit
  3. * Copyright (c) 2015 Andrew Lutomirski
  4. * GPL v2
  5. *
  6. * Based on asm and ptrace code by many authors. The code here originated
  7. * in ptrace.c and signal.c.
  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/tracehook.h>
  16. #include <linux/audit.h>
  17. #include <linux/seccomp.h>
  18. #include <linux/signal.h>
  19. #include <linux/export.h>
  20. #include <linux/context_tracking.h>
  21. #include <linux/user-return-notifier.h>
  22. #include <linux/uprobes.h>
  23. #include <asm/desc.h>
  24. #include <asm/traps.h>
  25. #define CREATE_TRACE_POINTS
  26. #include <trace/events/syscalls.h>
  27. #ifdef CONFIG_CONTEXT_TRACKING
  28. /* Called on entry from user mode with IRQs off. */
  29. __visible void enter_from_user_mode(void)
  30. {
  31. CT_WARN_ON(ct_state() != CONTEXT_USER);
  32. user_exit();
  33. }
  34. #endif
  35. static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
  36. {
  37. #ifdef CONFIG_X86_64
  38. if (arch == AUDIT_ARCH_X86_64) {
  39. audit_syscall_entry(regs->orig_ax, regs->di,
  40. regs->si, regs->dx, regs->r10);
  41. } else
  42. #endif
  43. {
  44. audit_syscall_entry(regs->orig_ax, regs->bx,
  45. regs->cx, regs->dx, regs->si);
  46. }
  47. }
  48. /*
  49. * We can return 0 to resume the syscall or anything else to go to phase
  50. * 2. If we resume the syscall, we need to put something appropriate in
  51. * regs->orig_ax.
  52. *
  53. * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
  54. * are fully functional.
  55. *
  56. * For phase 2's benefit, our return value is:
  57. * 0: resume the syscall
  58. * 1: go to phase 2; no seccomp phase 2 needed
  59. * anything else: go to phase 2; pass return value to seccomp
  60. */
  61. unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
  62. {
  63. unsigned long ret = 0;
  64. u32 work;
  65. BUG_ON(regs != task_pt_regs(current));
  66. work = ACCESS_ONCE(current_thread_info()->flags) &
  67. _TIF_WORK_SYSCALL_ENTRY;
  68. #ifdef CONFIG_CONTEXT_TRACKING
  69. /*
  70. * If TIF_NOHZ is set, we are required to call user_exit() before
  71. * doing anything that could touch RCU.
  72. */
  73. if (work & _TIF_NOHZ) {
  74. enter_from_user_mode();
  75. work &= ~_TIF_NOHZ;
  76. }
  77. #endif
  78. #ifdef CONFIG_SECCOMP
  79. /*
  80. * Do seccomp first -- it should minimize exposure of other
  81. * code, and keeping seccomp fast is probably more valuable
  82. * than the rest of this.
  83. */
  84. if (work & _TIF_SECCOMP) {
  85. struct seccomp_data sd;
  86. sd.arch = arch;
  87. sd.nr = regs->orig_ax;
  88. sd.instruction_pointer = regs->ip;
  89. #ifdef CONFIG_X86_64
  90. if (arch == AUDIT_ARCH_X86_64) {
  91. sd.args[0] = regs->di;
  92. sd.args[1] = regs->si;
  93. sd.args[2] = regs->dx;
  94. sd.args[3] = regs->r10;
  95. sd.args[4] = regs->r8;
  96. sd.args[5] = regs->r9;
  97. } else
  98. #endif
  99. {
  100. sd.args[0] = regs->bx;
  101. sd.args[1] = regs->cx;
  102. sd.args[2] = regs->dx;
  103. sd.args[3] = regs->si;
  104. sd.args[4] = regs->di;
  105. sd.args[5] = regs->bp;
  106. }
  107. BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
  108. BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
  109. ret = seccomp_phase1(&sd);
  110. if (ret == SECCOMP_PHASE1_SKIP) {
  111. regs->orig_ax = -1;
  112. ret = 0;
  113. } else if (ret != SECCOMP_PHASE1_OK) {
  114. return ret; /* Go directly to phase 2 */
  115. }
  116. work &= ~_TIF_SECCOMP;
  117. }
  118. #endif
  119. /* Do our best to finish without phase 2. */
  120. if (work == 0)
  121. return ret; /* seccomp and/or nohz only (ret == 0 here) */
  122. #ifdef CONFIG_AUDITSYSCALL
  123. if (work == _TIF_SYSCALL_AUDIT) {
  124. /*
  125. * If there is no more work to be done except auditing,
  126. * then audit in phase 1. Phase 2 always audits, so, if
  127. * we audit here, then we can't go on to phase 2.
  128. */
  129. do_audit_syscall_entry(regs, arch);
  130. return 0;
  131. }
  132. #endif
  133. return 1; /* Something is enabled that we can't handle in phase 1 */
  134. }
  135. /* Returns the syscall nr to run (which should match regs->orig_ax). */
  136. long syscall_trace_enter_phase2(struct pt_regs *regs, u32 arch,
  137. unsigned long phase1_result)
  138. {
  139. long ret = 0;
  140. u32 work = ACCESS_ONCE(current_thread_info()->flags) &
  141. _TIF_WORK_SYSCALL_ENTRY;
  142. BUG_ON(regs != task_pt_regs(current));
  143. /*
  144. * If we stepped into a sysenter/syscall insn, it trapped in
  145. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  146. * If user-mode had set TF itself, then it's still clear from
  147. * do_debug() and we need to set it again to restore the user
  148. * state. If we entered on the slow path, TF was already set.
  149. */
  150. if (work & _TIF_SINGLESTEP)
  151. regs->flags |= X86_EFLAGS_TF;
  152. #ifdef CONFIG_SECCOMP
  153. /*
  154. * Call seccomp_phase2 before running the other hooks so that
  155. * they can see any changes made by a seccomp tracer.
  156. */
  157. if (phase1_result > 1 && seccomp_phase2(phase1_result)) {
  158. /* seccomp failures shouldn't expose any additional code. */
  159. return -1;
  160. }
  161. #endif
  162. if (unlikely(work & _TIF_SYSCALL_EMU))
  163. ret = -1L;
  164. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  165. tracehook_report_syscall_entry(regs))
  166. ret = -1L;
  167. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  168. trace_sys_enter(regs, regs->orig_ax);
  169. do_audit_syscall_entry(regs, arch);
  170. return ret ?: regs->orig_ax;
  171. }
  172. long syscall_trace_enter(struct pt_regs *regs)
  173. {
  174. u32 arch = is_ia32_task() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
  175. unsigned long phase1_result = syscall_trace_enter_phase1(regs, arch);
  176. if (phase1_result == 0)
  177. return regs->orig_ax;
  178. else
  179. return syscall_trace_enter_phase2(regs, arch, phase1_result);
  180. }
  181. void syscall_trace_leave(struct pt_regs *regs)
  182. {
  183. bool step;
  184. /*
  185. * We may come here right after calling schedule_user()
  186. * or do_notify_resume(), in which case we can be in RCU
  187. * user mode.
  188. */
  189. user_exit();
  190. audit_syscall_exit(regs);
  191. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  192. trace_sys_exit(regs, regs->ax);
  193. /*
  194. * If TIF_SYSCALL_EMU is set, we only get here because of
  195. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  196. * We already reported this syscall instruction in
  197. * syscall_trace_enter().
  198. */
  199. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  200. !test_thread_flag(TIF_SYSCALL_EMU);
  201. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  202. tracehook_report_syscall_exit(regs, step);
  203. user_enter();
  204. }
  205. /*
  206. * notification of userspace execution resumption
  207. * - triggered by the TIF_WORK_MASK flags
  208. */
  209. __visible void
  210. do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
  211. {
  212. user_exit();
  213. if (thread_info_flags & _TIF_UPROBE)
  214. uprobe_notify_resume(regs);
  215. /* deal with pending signal delivery */
  216. if (thread_info_flags & _TIF_SIGPENDING)
  217. do_signal(regs);
  218. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  219. clear_thread_flag(TIF_NOTIFY_RESUME);
  220. tracehook_notify_resume(regs);
  221. }
  222. if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
  223. fire_user_return_notifiers();
  224. user_enter();
  225. }