compat_signal.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/compat.h>
  3. #include <linux/signal.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/syscalls.h>
  6. #include <linux/tracehook.h>
  7. #include <linux/linkage.h>
  8. #include <asm/ucontext.h>
  9. #include <asm/vdso.h>
  10. #include <asm/switch_to.h>
  11. #include <asm/csr.h>
  12. #define COMPAT_DEBUG_SIG 0
  13. struct compat_sigcontext {
  14. struct compat_user_regs_struct sc_regs;
  15. union __riscv_fp_state sc_fpregs;
  16. struct __riscv_v_state sc_vregs;
  17. };
  18. struct compat_ucontext {
  19. compat_ulong_t uc_flags;
  20. struct compat_ucontext *uc_link;
  21. compat_stack_t uc_stack;
  22. sigset_t uc_sigmask;
  23. /* There's some padding here to allow sigset_t to be expanded in the
  24. * future. Though this is unlikely, other architectures put uc_sigmask
  25. * at the end of this structure and explicitly state it can be
  26. * expanded, so we didn't want to box ourselves in here. */
  27. __u8 __unused[1024 / 8 - sizeof(sigset_t)];
  28. /* We can't put uc_sigmask at the end of this structure because we need
  29. * to be able to expand sigcontext in the future. For example, the
  30. * vector ISA extension will almost certainly add ISA state. We want
  31. * to ensure all user-visible ISA state can be saved and restored via a
  32. * ucontext, so we're putting this at the end in order to allow for
  33. * infinite extensibility. Since we know this will be extended and we
  34. * assume sigset_t won't be extended an extreme amount, we're
  35. * prioritizing this. */
  36. struct compat_sigcontext uc_mcontext;
  37. };
  38. struct compat_rt_sigframe {
  39. struct compat_siginfo info;
  40. struct compat_ucontext uc;
  41. };
  42. #ifdef CONFIG_FPU
  43. static long compat_restore_fp_state(struct pt_regs *regs,
  44. union __riscv_fp_state __user *sc_fpregs)
  45. {
  46. long err;
  47. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  48. size_t i;
  49. err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
  50. if (unlikely(err))
  51. return err;
  52. fstate_restore(current, regs);
  53. /* We support no other extension state at this time. */
  54. for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
  55. u32 value;
  56. err = __get_user(value, &sc_fpregs->q.reserved[i]);
  57. if (unlikely(err))
  58. break;
  59. if (value != 0)
  60. return -EINVAL;
  61. }
  62. return err;
  63. }
  64. static long compat_save_fp_state(struct pt_regs *regs,
  65. union __riscv_fp_state __user *sc_fpregs)
  66. {
  67. long err;
  68. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  69. size_t i;
  70. fstate_save(current, regs);
  71. err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
  72. if (unlikely(err))
  73. return err;
  74. /* We support no other extension state at this time. */
  75. for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
  76. err = __put_user(0, &sc_fpregs->q.reserved[i]);
  77. if (unlikely(err))
  78. break;
  79. }
  80. return err;
  81. }
  82. #else
  83. #define compat_save_fp_state(task, regs) (0)
  84. #define compat_restore_fp_state(task, regs) (0)
  85. #endif
  86. #ifdef CONFIG_VECTOR
  87. static long compat_restore_v_state(struct pt_regs *regs,
  88. struct __riscv_v_state *sc_vregs)
  89. {
  90. long err;
  91. struct __riscv_v_state __user *state = sc_vregs;
  92. err = __copy_from_user(&current->thread.vstate, state, sizeof(*state));
  93. if (unlikely(err))
  94. return err;
  95. vstate_restore(current, regs);
  96. return err;
  97. }
  98. static long compat_save_v_state(struct pt_regs *regs,
  99. struct __riscv_v_state *sc_vregs)
  100. {
  101. long err;
  102. struct __riscv_v_state __user *state = sc_vregs;
  103. vstate_save(current, regs);
  104. err = __copy_to_user(state, &current->thread.vstate, sizeof(*state));
  105. if (unlikely(err))
  106. return err;
  107. return err;
  108. }
  109. #else
  110. #define compat_save_v_state(task, regs) (0)
  111. #define compat_restore_v_state(task, regs) (0)
  112. #endif
  113. static long compat_restore_sigcontext(struct pt_regs *regs,
  114. struct compat_sigcontext __user *sc)
  115. {
  116. long err;
  117. struct compat_user_regs_struct cregs;
  118. /* sc_regs is structured the same as the start of pt_regs */
  119. err = __copy_from_user(&cregs, &sc->sc_regs, sizeof(sc->sc_regs));
  120. cregs_to_regs(&cregs, regs);
  121. /* Restore the floating-point state. */
  122. if (has_fpu)
  123. err |= compat_restore_fp_state(regs, &sc->sc_fpregs);
  124. /* Restore the vector state. */
  125. if (has_vector)
  126. err |= compat_restore_v_state(regs, &sc->sc_vregs);
  127. return err;
  128. }
  129. COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
  130. {
  131. struct pt_regs *regs = current_pt_regs();
  132. struct compat_rt_sigframe __user *frame;
  133. struct task_struct *task;
  134. sigset_t set;
  135. /* Always make any pending restarted system calls return -EINTR */
  136. current->restart_block.fn = do_no_restart_syscall;
  137. frame = (struct compat_rt_sigframe __user *)regs->sp;
  138. if (!access_ok(frame, sizeof(*frame)))
  139. goto badframe;
  140. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  141. goto badframe;
  142. set_current_blocked(&set);
  143. if (compat_restore_sigcontext(regs, &frame->uc.uc_mcontext))
  144. goto badframe;
  145. if (compat_restore_altstack(&frame->uc.uc_stack))
  146. goto badframe;
  147. return regs->a0;
  148. badframe:
  149. task = current;
  150. if (show_unhandled_signals) {
  151. pr_info_ratelimited(
  152. "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
  153. task->comm, task_pid_nr(task), __func__,
  154. frame, (void *)regs->epc, (void *)regs->sp);
  155. }
  156. force_sig(SIGSEGV);
  157. return 0;
  158. }
  159. static long compat_setup_sigcontext(struct compat_rt_sigframe __user *frame,
  160. struct pt_regs *regs)
  161. {
  162. struct compat_sigcontext __user *sc = &frame->uc.uc_mcontext;
  163. struct compat_user_regs_struct cregs;
  164. long err;
  165. regs_to_cregs(&cregs, regs);
  166. /* sc_regs is structured the same as the start of pt_regs */
  167. err = __copy_to_user(&sc->sc_regs, &cregs, sizeof(sc->sc_regs));
  168. /* Save the floating-point state. */
  169. if (has_fpu)
  170. err |= compat_save_fp_state(regs, &sc->sc_fpregs);
  171. /* Save the vector state. */
  172. if (has_vector)
  173. err |= compat_save_v_state(regs, &sc->sc_vregs);
  174. return err;
  175. }
  176. static inline void __user *compat_get_sigframe(struct ksignal *ksig,
  177. struct pt_regs *regs, size_t framesize)
  178. {
  179. unsigned long sp;
  180. /* Default to using normal stack */
  181. sp = regs->sp;
  182. /*
  183. * If we are on the alternate signal stack and would overflow it, don't.
  184. * Return an always-bogus address instead so we will die with SIGSEGV.
  185. */
  186. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  187. return (void __user __force *)(-1UL);
  188. /* This is the X/Open sanctioned signal stack switching. */
  189. sp = sigsp(sp, ksig) - framesize;
  190. /* Align the stack frame. */
  191. sp &= ~0xfUL;
  192. return (void __user *)sp;
  193. }
  194. int compat_setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  195. struct pt_regs *regs)
  196. {
  197. struct compat_rt_sigframe __user *frame;
  198. long err = 0;
  199. frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
  200. if (!access_ok(frame, sizeof(*frame)))
  201. return -EFAULT;
  202. err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
  203. /* Create the ucontext. */
  204. err |= __put_user(0, &frame->uc.uc_flags);
  205. err |= __put_user(NULL, &frame->uc.uc_link);
  206. err |= __compat_save_altstack(&frame->uc.uc_stack, regs->sp);
  207. err |= compat_setup_sigcontext(frame, regs);
  208. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  209. if (err)
  210. return -EFAULT;
  211. regs->ra = (unsigned long)COMPAT_VDSO_SYMBOL(
  212. current->mm->context.vdso, rt_sigreturn);
  213. /*
  214. * Set up registers for signal handler.
  215. * Registers that we don't modify keep the value they had from
  216. * user-space at the time we took the signal.
  217. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  218. * since some things rely on this (e.g. glibc's debug/segfault.c).
  219. */
  220. regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
  221. regs->sp = (unsigned long)frame;
  222. regs->a0 = ksig->sig; /* a0: signal number */
  223. regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
  224. regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
  225. #if COMPAT_DEBUG_SIG
  226. pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
  227. current->comm, task_pid_nr(current), ksig->sig,
  228. (void *)regs->epc, (void *)regs->ra, frame);
  229. #endif
  230. return 0;
  231. }