signal_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/unistd.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/syscalls.h>
  12. #include <asm/ucontext.h>
  13. #include <asm/fpu/signal.h>
  14. #include <asm/sighandling.h>
  15. #include <asm/syscall.h>
  16. #include <asm/sigframe.h>
  17. #include <asm/signal.h>
  18. /*
  19. * If regs->ss will cause an IRET fault, change it. Otherwise leave it
  20. * alone. Using this generally makes no sense unless
  21. * user_64bit_mode(regs) would return true.
  22. */
  23. static void force_valid_ss(struct pt_regs *regs)
  24. {
  25. u32 ar;
  26. asm volatile ("lar %[old_ss], %[ar]\n\t"
  27. "jz 1f\n\t" /* If invalid: */
  28. "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
  29. "1:"
  30. : [ar] "=r" (ar)
  31. : [old_ss] "rm" ((u16)regs->ss));
  32. /*
  33. * For a valid 64-bit user context, we need DPL 3, type
  34. * read-write data or read-write exp-down data, and S and P
  35. * set. We can't use VERW because VERW doesn't check the
  36. * P bit.
  37. */
  38. ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
  39. if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
  40. ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
  41. regs->ss = __USER_DS;
  42. }
  43. static bool restore_sigcontext(struct pt_regs *regs,
  44. struct sigcontext __user *usc,
  45. unsigned long uc_flags)
  46. {
  47. struct sigcontext sc;
  48. /* Always make any pending restarted system calls return -EINTR */
  49. current->restart_block.fn = do_no_restart_syscall;
  50. if (copy_from_user(&sc, usc, offsetof(struct sigcontext, reserved1)))
  51. return false;
  52. regs->bx = sc.bx;
  53. regs->cx = sc.cx;
  54. regs->dx = sc.dx;
  55. regs->si = sc.si;
  56. regs->di = sc.di;
  57. regs->bp = sc.bp;
  58. regs->ax = sc.ax;
  59. regs->sp = sc.sp;
  60. regs->ip = sc.ip;
  61. regs->r8 = sc.r8;
  62. regs->r9 = sc.r9;
  63. regs->r10 = sc.r10;
  64. regs->r11 = sc.r11;
  65. regs->r12 = sc.r12;
  66. regs->r13 = sc.r13;
  67. regs->r14 = sc.r14;
  68. regs->r15 = sc.r15;
  69. /* Get CS/SS and force CPL3 */
  70. regs->cs = sc.cs | 0x03;
  71. regs->ss = sc.ss | 0x03;
  72. regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
  73. /* disable syscall checks */
  74. regs->orig_ax = -1;
  75. /*
  76. * Fix up SS if needed for the benefit of old DOSEMU and
  77. * CRIU.
  78. */
  79. if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
  80. force_valid_ss(regs);
  81. return fpu__restore_sig((void __user *)sc.fpstate, 0);
  82. }
  83. static __always_inline int
  84. __unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
  85. struct pt_regs *regs, unsigned long mask)
  86. {
  87. unsafe_put_user(regs->di, &sc->di, Efault);
  88. unsafe_put_user(regs->si, &sc->si, Efault);
  89. unsafe_put_user(regs->bp, &sc->bp, Efault);
  90. unsafe_put_user(regs->sp, &sc->sp, Efault);
  91. unsafe_put_user(regs->bx, &sc->bx, Efault);
  92. unsafe_put_user(regs->dx, &sc->dx, Efault);
  93. unsafe_put_user(regs->cx, &sc->cx, Efault);
  94. unsafe_put_user(regs->ax, &sc->ax, Efault);
  95. unsafe_put_user(regs->r8, &sc->r8, Efault);
  96. unsafe_put_user(regs->r9, &sc->r9, Efault);
  97. unsafe_put_user(regs->r10, &sc->r10, Efault);
  98. unsafe_put_user(regs->r11, &sc->r11, Efault);
  99. unsafe_put_user(regs->r12, &sc->r12, Efault);
  100. unsafe_put_user(regs->r13, &sc->r13, Efault);
  101. unsafe_put_user(regs->r14, &sc->r14, Efault);
  102. unsafe_put_user(regs->r15, &sc->r15, Efault);
  103. unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
  104. unsafe_put_user(current->thread.error_code, &sc->err, Efault);
  105. unsafe_put_user(regs->ip, &sc->ip, Efault);
  106. unsafe_put_user(regs->flags, &sc->flags, Efault);
  107. unsafe_put_user(regs->cs, &sc->cs, Efault);
  108. unsafe_put_user(0, &sc->gs, Efault);
  109. unsafe_put_user(0, &sc->fs, Efault);
  110. unsafe_put_user(regs->ss, &sc->ss, Efault);
  111. unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
  112. /* non-iBCS2 extensions.. */
  113. unsafe_put_user(mask, &sc->oldmask, Efault);
  114. unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
  115. return 0;
  116. Efault:
  117. return -EFAULT;
  118. }
  119. #define unsafe_put_sigcontext(sc, fp, regs, set, label) \
  120. do { \
  121. if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0])) \
  122. goto label; \
  123. } while(0);
  124. #define unsafe_put_sigmask(set, frame, label) \
  125. unsafe_put_user(*(__u64 *)(set), \
  126. (__u64 __user *)&(frame)->uc.uc_sigmask, \
  127. label)
  128. static unsigned long frame_uc_flags(struct pt_regs *regs)
  129. {
  130. unsigned long flags;
  131. if (boot_cpu_has(X86_FEATURE_XSAVE))
  132. flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
  133. else
  134. flags = UC_SIGCONTEXT_SS;
  135. if (likely(user_64bit_mode(regs)))
  136. flags |= UC_STRICT_RESTORE_SS;
  137. return flags;
  138. }
  139. int x64_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  140. {
  141. sigset_t *set = sigmask_to_save();
  142. struct rt_sigframe __user *frame;
  143. void __user *fp = NULL;
  144. unsigned long uc_flags;
  145. /* x86-64 should always use SA_RESTORER. */
  146. if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
  147. return -EFAULT;
  148. frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe), &fp);
  149. uc_flags = frame_uc_flags(regs);
  150. if (!user_access_begin(frame, sizeof(*frame)))
  151. return -EFAULT;
  152. /* Create the ucontext. */
  153. unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
  154. unsafe_put_user(0, &frame->uc.uc_link, Efault);
  155. unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
  156. /* Set up to return from userspace. If provided, use a stub
  157. already in userspace. */
  158. unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
  159. unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
  160. unsafe_put_sigmask(set, frame, Efault);
  161. user_access_end();
  162. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  163. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  164. return -EFAULT;
  165. }
  166. if (setup_signal_shadow_stack(ksig))
  167. return -EFAULT;
  168. /* Set up registers for signal handler */
  169. regs->di = ksig->sig;
  170. /* In case the signal handler was declared without prototypes */
  171. regs->ax = 0;
  172. /* This also works for non SA_SIGINFO handlers because they expect the
  173. next argument after the signal number on the stack. */
  174. regs->si = (unsigned long)&frame->info;
  175. regs->dx = (unsigned long)&frame->uc;
  176. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  177. regs->sp = (unsigned long)frame;
  178. /*
  179. * Set up the CS and SS registers to run signal handlers in
  180. * 64-bit mode, even if the handler happens to be interrupting
  181. * 32-bit or 16-bit code.
  182. *
  183. * SS is subtle. In 64-bit mode, we don't need any particular
  184. * SS descriptor, but we do need SS to be valid. It's possible
  185. * that the old SS is entirely bogus -- this can happen if the
  186. * signal we're trying to deliver is #GP or #SS caused by a bad
  187. * SS value. We also have a compatibility issue here: DOSEMU
  188. * relies on the contents of the SS register indicating the
  189. * SS value at the time of the signal, even though that code in
  190. * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
  191. * avoids relying on sigreturn to restore SS; instead it uses
  192. * a trampoline.) So we do our best: if the old SS was valid,
  193. * we keep it. Otherwise we replace it.
  194. */
  195. regs->cs = __USER_CS;
  196. if (unlikely(regs->ss != __USER_DS))
  197. force_valid_ss(regs);
  198. return 0;
  199. Efault:
  200. user_access_end();
  201. return -EFAULT;
  202. }
  203. /*
  204. * Do a signal return; undo the signal stack.
  205. */
  206. SYSCALL_DEFINE0(rt_sigreturn)
  207. {
  208. struct pt_regs *regs = current_pt_regs();
  209. struct rt_sigframe __user *frame;
  210. sigset_t set;
  211. unsigned long uc_flags;
  212. prevent_single_step_upon_eretu(regs);
  213. frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
  214. if (!access_ok(frame, sizeof(*frame)))
  215. goto badframe;
  216. if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
  217. goto badframe;
  218. if (__get_user(uc_flags, &frame->uc.uc_flags))
  219. goto badframe;
  220. set_current_blocked(&set);
  221. if (restore_altstack(&frame->uc.uc_stack))
  222. goto badframe;
  223. if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
  224. goto badframe;
  225. if (restore_signal_shadow_stack())
  226. goto badframe;
  227. return regs->ax;
  228. badframe:
  229. signal_fault(regs, frame, "rt_sigreturn");
  230. return 0;
  231. }
  232. #ifdef CONFIG_X86_X32_ABI
  233. static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
  234. const struct kernel_siginfo *from)
  235. {
  236. struct compat_siginfo new;
  237. copy_siginfo_to_external32(&new, from);
  238. if (from->si_signo == SIGCHLD) {
  239. new._sifields._sigchld_x32._utime = from->si_utime;
  240. new._sifields._sigchld_x32._stime = from->si_stime;
  241. }
  242. if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
  243. return -EFAULT;
  244. return 0;
  245. }
  246. int copy_siginfo_to_user32(struct compat_siginfo __user *to,
  247. const struct kernel_siginfo *from)
  248. {
  249. if (in_x32_syscall())
  250. return x32_copy_siginfo_to_user(to, from);
  251. return __copy_siginfo_to_user32(to, from);
  252. }
  253. int x32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  254. {
  255. compat_sigset_t *set = (compat_sigset_t *) sigmask_to_save();
  256. struct rt_sigframe_x32 __user *frame;
  257. unsigned long uc_flags;
  258. void __user *restorer;
  259. void __user *fp = NULL;
  260. if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
  261. return -EFAULT;
  262. frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
  263. uc_flags = frame_uc_flags(regs);
  264. if (setup_signal_shadow_stack(ksig))
  265. return -EFAULT;
  266. if (!user_access_begin(frame, sizeof(*frame)))
  267. return -EFAULT;
  268. /* Create the ucontext. */
  269. unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
  270. unsafe_put_user(0, &frame->uc.uc_link, Efault);
  271. unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
  272. unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
  273. restorer = ksig->ka.sa.sa_restorer;
  274. unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
  275. unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
  276. unsafe_put_sigmask(set, frame, Efault);
  277. user_access_end();
  278. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  279. if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
  280. return -EFAULT;
  281. }
  282. /* Set up registers for signal handler */
  283. regs->sp = (unsigned long) frame;
  284. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  285. /* We use the x32 calling convention here... */
  286. regs->di = ksig->sig;
  287. regs->si = (unsigned long) &frame->info;
  288. regs->dx = (unsigned long) &frame->uc;
  289. loadsegment(ds, __USER_DS);
  290. loadsegment(es, __USER_DS);
  291. regs->cs = __USER_CS;
  292. regs->ss = __USER_DS;
  293. return 0;
  294. Efault:
  295. user_access_end();
  296. return -EFAULT;
  297. }
  298. COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
  299. {
  300. struct pt_regs *regs = current_pt_regs();
  301. struct rt_sigframe_x32 __user *frame;
  302. sigset_t set;
  303. unsigned long uc_flags;
  304. prevent_single_step_upon_eretu(regs);
  305. frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
  306. if (!access_ok(frame, sizeof(*frame)))
  307. goto badframe;
  308. if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
  309. goto badframe;
  310. if (__get_user(uc_flags, &frame->uc.uc_flags))
  311. goto badframe;
  312. set_current_blocked(&set);
  313. if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
  314. goto badframe;
  315. if (restore_signal_shadow_stack())
  316. goto badframe;
  317. if (compat_restore_altstack(&frame->uc.uc_stack))
  318. goto badframe;
  319. return regs->ax;
  320. badframe:
  321. signal_fault(regs, frame, "x32 rt_sigreturn");
  322. return 0;
  323. }
  324. #endif /* CONFIG_X86_X32_ABI */
  325. #ifdef CONFIG_COMPAT
  326. void sigaction_compat_abi(struct k_sigaction *act, struct k_sigaction *oact)
  327. {
  328. if (!act)
  329. return;
  330. if (in_ia32_syscall())
  331. act->sa.sa_flags |= SA_IA32_ABI;
  332. if (in_x32_syscall())
  333. act->sa.sa_flags |= SA_X32_ABI;
  334. }
  335. #endif /* CONFIG_COMPAT */
  336. /*
  337. * If adding a new si_code, there is probably new data in
  338. * the siginfo. Make sure folks bumping the si_code
  339. * limits also have to look at this code. Make sure any
  340. * new fields are handled in copy_siginfo_to_user32()!
  341. */
  342. static_assert(NSIGILL == 11);
  343. static_assert(NSIGFPE == 15);
  344. static_assert(NSIGSEGV == 10);
  345. static_assert(NSIGBUS == 5);
  346. static_assert(NSIGTRAP == 6);
  347. static_assert(NSIGCHLD == 6);
  348. static_assert(NSIGSYS == 2);
  349. /* This is part of the ABI and can never change in size: */
  350. static_assert(sizeof(siginfo_t) == 128);
  351. /* This is a part of the ABI and can never change in alignment */
  352. static_assert(__alignof__(siginfo_t) == 8);
  353. /*
  354. * The offsets of all the (unioned) si_fields are fixed
  355. * in the ABI, of course. Make sure none of them ever
  356. * move and are always at the beginning:
  357. */
  358. static_assert(offsetof(siginfo_t, si_signo) == 0);
  359. static_assert(offsetof(siginfo_t, si_errno) == 4);
  360. static_assert(offsetof(siginfo_t, si_code) == 8);
  361. /*
  362. * Ensure that the size of each si_field never changes.
  363. * If it does, it is a sign that the
  364. * copy_siginfo_to_user32() code below needs to updated
  365. * along with the size in the CHECK_SI_SIZE().
  366. *
  367. * We repeat this check for both the generic and compat
  368. * siginfos.
  369. *
  370. * Note: it is OK for these to grow as long as the whole
  371. * structure stays within the padding size (checked
  372. * above).
  373. */
  374. #define CHECK_SI_OFFSET(name) \
  375. static_assert(offsetof(siginfo_t, _sifields) == \
  376. offsetof(siginfo_t, _sifields.name))
  377. #define CHECK_SI_SIZE(name, size) \
  378. static_assert(sizeof_field(siginfo_t, _sifields.name) == size)
  379. CHECK_SI_OFFSET(_kill);
  380. CHECK_SI_SIZE (_kill, 2*sizeof(int));
  381. static_assert(offsetof(siginfo_t, si_pid) == 0x10);
  382. static_assert(offsetof(siginfo_t, si_uid) == 0x14);
  383. CHECK_SI_OFFSET(_timer);
  384. CHECK_SI_SIZE (_timer, 6*sizeof(int));
  385. static_assert(offsetof(siginfo_t, si_tid) == 0x10);
  386. static_assert(offsetof(siginfo_t, si_overrun) == 0x14);
  387. static_assert(offsetof(siginfo_t, si_value) == 0x18);
  388. CHECK_SI_OFFSET(_rt);
  389. CHECK_SI_SIZE (_rt, 4*sizeof(int));
  390. static_assert(offsetof(siginfo_t, si_pid) == 0x10);
  391. static_assert(offsetof(siginfo_t, si_uid) == 0x14);
  392. static_assert(offsetof(siginfo_t, si_value) == 0x18);
  393. CHECK_SI_OFFSET(_sigchld);
  394. CHECK_SI_SIZE (_sigchld, 8*sizeof(int));
  395. static_assert(offsetof(siginfo_t, si_pid) == 0x10);
  396. static_assert(offsetof(siginfo_t, si_uid) == 0x14);
  397. static_assert(offsetof(siginfo_t, si_status) == 0x18);
  398. static_assert(offsetof(siginfo_t, si_utime) == 0x20);
  399. static_assert(offsetof(siginfo_t, si_stime) == 0x28);
  400. #ifdef CONFIG_X86_X32_ABI
  401. /* no _sigchld_x32 in the generic siginfo_t */
  402. static_assert(sizeof_field(compat_siginfo_t, _sifields._sigchld_x32) ==
  403. 7*sizeof(int));
  404. static_assert(offsetof(compat_siginfo_t, _sifields) ==
  405. offsetof(compat_siginfo_t, _sifields._sigchld_x32));
  406. static_assert(offsetof(compat_siginfo_t, _sifields._sigchld_x32._utime) == 0x18);
  407. static_assert(offsetof(compat_siginfo_t, _sifields._sigchld_x32._stime) == 0x20);
  408. #endif
  409. CHECK_SI_OFFSET(_sigfault);
  410. CHECK_SI_SIZE (_sigfault, 8*sizeof(int));
  411. static_assert(offsetof(siginfo_t, si_addr) == 0x10);
  412. static_assert(offsetof(siginfo_t, si_trapno) == 0x18);
  413. static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18);
  414. static_assert(offsetof(siginfo_t, si_lower) == 0x20);
  415. static_assert(offsetof(siginfo_t, si_upper) == 0x28);
  416. static_assert(offsetof(siginfo_t, si_pkey) == 0x20);
  417. static_assert(offsetof(siginfo_t, si_perf_data) == 0x18);
  418. static_assert(offsetof(siginfo_t, si_perf_type) == 0x20);
  419. static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
  420. CHECK_SI_OFFSET(_sigpoll);
  421. CHECK_SI_SIZE (_sigpoll, 4*sizeof(int));
  422. static_assert(offsetof(siginfo_t, si_band) == 0x10);
  423. static_assert(offsetof(siginfo_t, si_fd) == 0x18);
  424. CHECK_SI_OFFSET(_sigsys);
  425. CHECK_SI_SIZE (_sigsys, 4*sizeof(int));
  426. static_assert(offsetof(siginfo_t, si_call_addr) == 0x10);
  427. static_assert(offsetof(siginfo_t, si_syscall) == 0x18);
  428. static_assert(offsetof(siginfo_t, si_arch) == 0x1C);
  429. /* any new si_fields should be added here */