signal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  7. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  8. * 2000-2002 x86-64 support by Andi Kleen
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/sched.h>
  12. #include <linux/sched/task_stack.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/wait.h>
  18. #include <linux/tracehook.h>
  19. #include <linux/unistd.h>
  20. #include <linux/stddef.h>
  21. #include <linux/personality.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/user-return-notifier.h>
  24. #include <linux/uprobes.h>
  25. #include <linux/context_tracking.h>
  26. #include <linux/entry-common.h>
  27. #include <linux/syscalls.h>
  28. #include <asm/processor.h>
  29. #include <asm/ucontext.h>
  30. #include <asm/fpu/internal.h>
  31. #include <asm/fpu/signal.h>
  32. #include <asm/vdso.h>
  33. #include <asm/mce.h>
  34. #include <asm/sighandling.h>
  35. #include <asm/vm86.h>
  36. #ifdef CONFIG_X86_64
  37. #include <linux/compat.h>
  38. #include <asm/proto.h>
  39. #include <asm/ia32_unistd.h>
  40. #endif /* CONFIG_X86_64 */
  41. #include <asm/syscall.h>
  42. #include <asm/sigframe.h>
  43. #include <asm/signal.h>
  44. #ifdef CONFIG_X86_64
  45. /*
  46. * If regs->ss will cause an IRET fault, change it. Otherwise leave it
  47. * alone. Using this generally makes no sense unless
  48. * user_64bit_mode(regs) would return true.
  49. */
  50. static void force_valid_ss(struct pt_regs *regs)
  51. {
  52. u32 ar;
  53. asm volatile ("lar %[old_ss], %[ar]\n\t"
  54. "jz 1f\n\t" /* If invalid: */
  55. "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
  56. "1:"
  57. : [ar] "=r" (ar)
  58. : [old_ss] "rm" ((u16)regs->ss));
  59. /*
  60. * For a valid 64-bit user context, we need DPL 3, type
  61. * read-write data or read-write exp-down data, and S and P
  62. * set. We can't use VERW because VERW doesn't check the
  63. * P bit.
  64. */
  65. ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
  66. if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
  67. ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
  68. regs->ss = __USER_DS;
  69. }
  70. # define CONTEXT_COPY_SIZE offsetof(struct sigcontext, reserved1)
  71. #else
  72. # define CONTEXT_COPY_SIZE sizeof(struct sigcontext)
  73. #endif
  74. static int restore_sigcontext(struct pt_regs *regs,
  75. struct sigcontext __user *usc,
  76. unsigned long uc_flags)
  77. {
  78. struct sigcontext sc;
  79. /* Always make any pending restarted system calls return -EINTR */
  80. current->restart_block.fn = do_no_restart_syscall;
  81. if (copy_from_user(&sc, usc, CONTEXT_COPY_SIZE))
  82. return -EFAULT;
  83. #ifdef CONFIG_X86_32
  84. set_user_gs(regs, sc.gs);
  85. regs->fs = sc.fs;
  86. regs->es = sc.es;
  87. regs->ds = sc.ds;
  88. #endif /* CONFIG_X86_32 */
  89. regs->bx = sc.bx;
  90. regs->cx = sc.cx;
  91. regs->dx = sc.dx;
  92. regs->si = sc.si;
  93. regs->di = sc.di;
  94. regs->bp = sc.bp;
  95. regs->ax = sc.ax;
  96. regs->sp = sc.sp;
  97. regs->ip = sc.ip;
  98. #ifdef CONFIG_X86_64
  99. regs->r8 = sc.r8;
  100. regs->r9 = sc.r9;
  101. regs->r10 = sc.r10;
  102. regs->r11 = sc.r11;
  103. regs->r12 = sc.r12;
  104. regs->r13 = sc.r13;
  105. regs->r14 = sc.r14;
  106. regs->r15 = sc.r15;
  107. #endif /* CONFIG_X86_64 */
  108. /* Get CS/SS and force CPL3 */
  109. regs->cs = sc.cs | 0x03;
  110. regs->ss = sc.ss | 0x03;
  111. regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
  112. /* disable syscall checks */
  113. regs->orig_ax = -1;
  114. #ifdef CONFIG_X86_64
  115. /*
  116. * Fix up SS if needed for the benefit of old DOSEMU and
  117. * CRIU.
  118. */
  119. if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
  120. force_valid_ss(regs);
  121. #endif
  122. return fpu__restore_sig((void __user *)sc.fpstate,
  123. IS_ENABLED(CONFIG_X86_32));
  124. }
  125. static __always_inline int
  126. __unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
  127. struct pt_regs *regs, unsigned long mask)
  128. {
  129. #ifdef CONFIG_X86_32
  130. unsafe_put_user(get_user_gs(regs),
  131. (unsigned int __user *)&sc->gs, Efault);
  132. unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
  133. unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
  134. unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
  135. #endif /* CONFIG_X86_32 */
  136. unsafe_put_user(regs->di, &sc->di, Efault);
  137. unsafe_put_user(regs->si, &sc->si, Efault);
  138. unsafe_put_user(regs->bp, &sc->bp, Efault);
  139. unsafe_put_user(regs->sp, &sc->sp, Efault);
  140. unsafe_put_user(regs->bx, &sc->bx, Efault);
  141. unsafe_put_user(regs->dx, &sc->dx, Efault);
  142. unsafe_put_user(regs->cx, &sc->cx, Efault);
  143. unsafe_put_user(regs->ax, &sc->ax, Efault);
  144. #ifdef CONFIG_X86_64
  145. unsafe_put_user(regs->r8, &sc->r8, Efault);
  146. unsafe_put_user(regs->r9, &sc->r9, Efault);
  147. unsafe_put_user(regs->r10, &sc->r10, Efault);
  148. unsafe_put_user(regs->r11, &sc->r11, Efault);
  149. unsafe_put_user(regs->r12, &sc->r12, Efault);
  150. unsafe_put_user(regs->r13, &sc->r13, Efault);
  151. unsafe_put_user(regs->r14, &sc->r14, Efault);
  152. unsafe_put_user(regs->r15, &sc->r15, Efault);
  153. #endif /* CONFIG_X86_64 */
  154. unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
  155. unsafe_put_user(current->thread.error_code, &sc->err, Efault);
  156. unsafe_put_user(regs->ip, &sc->ip, Efault);
  157. #ifdef CONFIG_X86_32
  158. unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
  159. unsafe_put_user(regs->flags, &sc->flags, Efault);
  160. unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
  161. unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
  162. #else /* !CONFIG_X86_32 */
  163. unsafe_put_user(regs->flags, &sc->flags, Efault);
  164. unsafe_put_user(regs->cs, &sc->cs, Efault);
  165. unsafe_put_user(0, &sc->gs, Efault);
  166. unsafe_put_user(0, &sc->fs, Efault);
  167. unsafe_put_user(regs->ss, &sc->ss, Efault);
  168. #endif /* CONFIG_X86_32 */
  169. unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
  170. /* non-iBCS2 extensions.. */
  171. unsafe_put_user(mask, &sc->oldmask, Efault);
  172. unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
  173. return 0;
  174. Efault:
  175. return -EFAULT;
  176. }
  177. #define unsafe_put_sigcontext(sc, fp, regs, set, label) \
  178. do { \
  179. if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0])) \
  180. goto label; \
  181. } while(0);
  182. #define unsafe_put_sigmask(set, frame, label) \
  183. unsafe_put_user(*(__u64 *)(set), \
  184. (__u64 __user *)&(frame)->uc.uc_sigmask, \
  185. label)
  186. /*
  187. * Set up a signal frame.
  188. */
  189. /*
  190. * Determine which stack to use..
  191. */
  192. static unsigned long align_sigframe(unsigned long sp)
  193. {
  194. #ifdef CONFIG_X86_32
  195. /*
  196. * Align the stack pointer according to the i386 ABI,
  197. * i.e. so that on function entry ((sp + 4) & 15) == 0.
  198. */
  199. sp = ((sp + 4) & -16ul) - 4;
  200. #else /* !CONFIG_X86_32 */
  201. sp = round_down(sp, 16) - 8;
  202. #endif
  203. return sp;
  204. }
  205. static void __user *
  206. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
  207. void __user **fpstate)
  208. {
  209. /* Default to using normal stack */
  210. bool nested_altstack = on_sig_stack(regs->sp);
  211. bool entering_altstack = false;
  212. unsigned long math_size = 0;
  213. unsigned long sp = regs->sp;
  214. unsigned long buf_fx = 0;
  215. int ret;
  216. /* redzone */
  217. if (IS_ENABLED(CONFIG_X86_64))
  218. sp -= 128;
  219. /* This is the X/Open sanctioned signal stack switching. */
  220. if (ka->sa.sa_flags & SA_ONSTACK) {
  221. /*
  222. * This checks nested_altstack via sas_ss_flags(). Sensible
  223. * programs use SS_AUTODISARM, which disables that check, and
  224. * programs that don't use SS_AUTODISARM get compatible.
  225. */
  226. if (sas_ss_flags(sp) == 0) {
  227. sp = current->sas_ss_sp + current->sas_ss_size;
  228. entering_altstack = true;
  229. }
  230. } else if (IS_ENABLED(CONFIG_X86_32) &&
  231. !nested_altstack &&
  232. regs->ss != __USER_DS &&
  233. !(ka->sa.sa_flags & SA_RESTORER) &&
  234. ka->sa.sa_restorer) {
  235. /* This is the legacy signal stack switching. */
  236. sp = (unsigned long) ka->sa.sa_restorer;
  237. entering_altstack = true;
  238. }
  239. sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
  240. &buf_fx, &math_size);
  241. *fpstate = (void __user *)sp;
  242. sp = align_sigframe(sp - frame_size);
  243. /*
  244. * If we are on the alternate signal stack and would overflow it, don't.
  245. * Return an always-bogus address instead so we will die with SIGSEGV.
  246. */
  247. if (unlikely((nested_altstack || entering_altstack) &&
  248. !__on_sig_stack(sp))) {
  249. if (show_unhandled_signals && printk_ratelimit())
  250. pr_info("%s[%d] overflowed sigaltstack\n",
  251. current->comm, task_pid_nr(current));
  252. return (void __user *)-1L;
  253. }
  254. /* save i387 and extended state */
  255. ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
  256. if (ret < 0)
  257. return (void __user *)-1L;
  258. return (void __user *)sp;
  259. }
  260. #ifdef CONFIG_X86_32
  261. static const struct {
  262. u16 poplmovl;
  263. u32 val;
  264. u16 int80;
  265. } __attribute__((packed)) retcode = {
  266. 0xb858, /* popl %eax; movl $..., %eax */
  267. __NR_sigreturn,
  268. 0x80cd, /* int $0x80 */
  269. };
  270. static const struct {
  271. u8 movl;
  272. u32 val;
  273. u16 int80;
  274. u8 pad;
  275. } __attribute__((packed)) rt_retcode = {
  276. 0xb8, /* movl $..., %eax */
  277. __NR_rt_sigreturn,
  278. 0x80cd, /* int $0x80 */
  279. 0
  280. };
  281. static int
  282. __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
  283. struct pt_regs *regs)
  284. {
  285. struct sigframe __user *frame;
  286. void __user *restorer;
  287. void __user *fp = NULL;
  288. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
  289. if (!user_access_begin(frame, sizeof(*frame)))
  290. return -EFAULT;
  291. unsafe_put_user(sig, &frame->sig, Efault);
  292. unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
  293. unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
  294. if (current->mm->context.vdso)
  295. restorer = current->mm->context.vdso +
  296. vdso_image_32.sym___kernel_sigreturn;
  297. else
  298. restorer = &frame->retcode;
  299. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  300. restorer = ksig->ka.sa.sa_restorer;
  301. /* Set up to return from userspace. */
  302. unsafe_put_user(restorer, &frame->pretcode, Efault);
  303. /*
  304. * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
  305. *
  306. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  307. * reasons and because gdb uses it as a signature to notice
  308. * signal handler stack frames.
  309. */
  310. unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
  311. user_access_end();
  312. /* Set up registers for signal handler */
  313. regs->sp = (unsigned long)frame;
  314. regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
  315. regs->ax = (unsigned long)sig;
  316. regs->dx = 0;
  317. regs->cx = 0;
  318. regs->ds = __USER_DS;
  319. regs->es = __USER_DS;
  320. regs->ss = __USER_DS;
  321. regs->cs = __USER_CS;
  322. return 0;
  323. Efault:
  324. user_access_end();
  325. return -EFAULT;
  326. }
  327. static int __setup_rt_frame(int sig, struct ksignal *ksig,
  328. sigset_t *set, struct pt_regs *regs)
  329. {
  330. struct rt_sigframe __user *frame;
  331. void __user *restorer;
  332. void __user *fp = NULL;
  333. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
  334. if (!user_access_begin(frame, sizeof(*frame)))
  335. return -EFAULT;
  336. unsafe_put_user(sig, &frame->sig, Efault);
  337. unsafe_put_user(&frame->info, &frame->pinfo, Efault);
  338. unsafe_put_user(&frame->uc, &frame->puc, Efault);
  339. /* Create the ucontext. */
  340. if (static_cpu_has(X86_FEATURE_XSAVE))
  341. unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
  342. else
  343. unsafe_put_user(0, &frame->uc.uc_flags, Efault);
  344. unsafe_put_user(0, &frame->uc.uc_link, Efault);
  345. unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
  346. /* Set up to return from userspace. */
  347. restorer = current->mm->context.vdso +
  348. vdso_image_32.sym___kernel_rt_sigreturn;
  349. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  350. restorer = ksig->ka.sa.sa_restorer;
  351. unsafe_put_user(restorer, &frame->pretcode, Efault);
  352. /*
  353. * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
  354. *
  355. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  356. * reasons and because gdb uses it as a signature to notice
  357. * signal handler stack frames.
  358. */
  359. unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
  360. unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
  361. unsafe_put_sigmask(set, frame, Efault);
  362. user_access_end();
  363. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  364. return -EFAULT;
  365. /* Set up registers for signal handler */
  366. regs->sp = (unsigned long)frame;
  367. regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
  368. regs->ax = (unsigned long)sig;
  369. regs->dx = (unsigned long)&frame->info;
  370. regs->cx = (unsigned long)&frame->uc;
  371. regs->ds = __USER_DS;
  372. regs->es = __USER_DS;
  373. regs->ss = __USER_DS;
  374. regs->cs = __USER_CS;
  375. return 0;
  376. Efault:
  377. user_access_end();
  378. return -EFAULT;
  379. }
  380. #else /* !CONFIG_X86_32 */
  381. static unsigned long frame_uc_flags(struct pt_regs *regs)
  382. {
  383. unsigned long flags;
  384. if (boot_cpu_has(X86_FEATURE_XSAVE))
  385. flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
  386. else
  387. flags = UC_SIGCONTEXT_SS;
  388. if (likely(user_64bit_mode(regs)))
  389. flags |= UC_STRICT_RESTORE_SS;
  390. return flags;
  391. }
  392. static int __setup_rt_frame(int sig, struct ksignal *ksig,
  393. sigset_t *set, struct pt_regs *regs)
  394. {
  395. struct rt_sigframe __user *frame;
  396. void __user *fp = NULL;
  397. unsigned long uc_flags;
  398. /* x86-64 should always use SA_RESTORER. */
  399. if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
  400. return -EFAULT;
  401. frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
  402. uc_flags = frame_uc_flags(regs);
  403. if (!user_access_begin(frame, sizeof(*frame)))
  404. return -EFAULT;
  405. /* Create the ucontext. */
  406. unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
  407. unsafe_put_user(0, &frame->uc.uc_link, Efault);
  408. unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
  409. /* Set up to return from userspace. If provided, use a stub
  410. already in userspace. */
  411. unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
  412. unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
  413. unsafe_put_sigmask(set, frame, Efault);
  414. user_access_end();
  415. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  416. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  417. return -EFAULT;
  418. }
  419. /* Set up registers for signal handler */
  420. regs->di = sig;
  421. /* In case the signal handler was declared without prototypes */
  422. regs->ax = 0;
  423. /* This also works for non SA_SIGINFO handlers because they expect the
  424. next argument after the signal number on the stack. */
  425. regs->si = (unsigned long)&frame->info;
  426. regs->dx = (unsigned long)&frame->uc;
  427. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  428. regs->sp = (unsigned long)frame;
  429. /*
  430. * Set up the CS and SS registers to run signal handlers in
  431. * 64-bit mode, even if the handler happens to be interrupting
  432. * 32-bit or 16-bit code.
  433. *
  434. * SS is subtle. In 64-bit mode, we don't need any particular
  435. * SS descriptor, but we do need SS to be valid. It's possible
  436. * that the old SS is entirely bogus -- this can happen if the
  437. * signal we're trying to deliver is #GP or #SS caused by a bad
  438. * SS value. We also have a compatbility issue here: DOSEMU
  439. * relies on the contents of the SS register indicating the
  440. * SS value at the time of the signal, even though that code in
  441. * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
  442. * avoids relying on sigreturn to restore SS; instead it uses
  443. * a trampoline.) So we do our best: if the old SS was valid,
  444. * we keep it. Otherwise we replace it.
  445. */
  446. regs->cs = __USER_CS;
  447. if (unlikely(regs->ss != __USER_DS))
  448. force_valid_ss(regs);
  449. return 0;
  450. Efault:
  451. user_access_end();
  452. return -EFAULT;
  453. }
  454. #endif /* CONFIG_X86_32 */
  455. #ifdef CONFIG_X86_X32_ABI
  456. static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
  457. const struct kernel_siginfo *from)
  458. {
  459. struct compat_siginfo new;
  460. copy_siginfo_to_external32(&new, from);
  461. if (from->si_signo == SIGCHLD) {
  462. new._sifields._sigchld_x32._utime = from->si_utime;
  463. new._sifields._sigchld_x32._stime = from->si_stime;
  464. }
  465. if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
  466. return -EFAULT;
  467. return 0;
  468. }
  469. int copy_siginfo_to_user32(struct compat_siginfo __user *to,
  470. const struct kernel_siginfo *from)
  471. {
  472. if (in_x32_syscall())
  473. return x32_copy_siginfo_to_user(to, from);
  474. return __copy_siginfo_to_user32(to, from);
  475. }
  476. #endif /* CONFIG_X86_X32_ABI */
  477. static int x32_setup_rt_frame(struct ksignal *ksig,
  478. compat_sigset_t *set,
  479. struct pt_regs *regs)
  480. {
  481. #ifdef CONFIG_X86_X32_ABI
  482. struct rt_sigframe_x32 __user *frame;
  483. unsigned long uc_flags;
  484. void __user *restorer;
  485. void __user *fp = NULL;
  486. if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
  487. return -EFAULT;
  488. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
  489. uc_flags = frame_uc_flags(regs);
  490. if (!user_access_begin(frame, sizeof(*frame)))
  491. return -EFAULT;
  492. /* Create the ucontext. */
  493. unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
  494. unsafe_put_user(0, &frame->uc.uc_link, Efault);
  495. unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
  496. unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
  497. restorer = ksig->ka.sa.sa_restorer;
  498. unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
  499. unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
  500. unsafe_put_sigmask(set, frame, Efault);
  501. user_access_end();
  502. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  503. if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
  504. return -EFAULT;
  505. }
  506. /* Set up registers for signal handler */
  507. regs->sp = (unsigned long) frame;
  508. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  509. /* We use the x32 calling convention here... */
  510. regs->di = ksig->sig;
  511. regs->si = (unsigned long) &frame->info;
  512. regs->dx = (unsigned long) &frame->uc;
  513. loadsegment(ds, __USER_DS);
  514. loadsegment(es, __USER_DS);
  515. regs->cs = __USER_CS;
  516. regs->ss = __USER_DS;
  517. #endif /* CONFIG_X86_X32_ABI */
  518. return 0;
  519. #ifdef CONFIG_X86_X32_ABI
  520. Efault:
  521. user_access_end();
  522. return -EFAULT;
  523. #endif
  524. }
  525. /*
  526. * Do a signal return; undo the signal stack.
  527. */
  528. #ifdef CONFIG_X86_32
  529. SYSCALL_DEFINE0(sigreturn)
  530. {
  531. struct pt_regs *regs = current_pt_regs();
  532. struct sigframe __user *frame;
  533. sigset_t set;
  534. frame = (struct sigframe __user *)(regs->sp - 8);
  535. if (!access_ok(frame, sizeof(*frame)))
  536. goto badframe;
  537. if (__get_user(set.sig[0], &frame->sc.oldmask) ||
  538. __get_user(set.sig[1], &frame->extramask[0]))
  539. goto badframe;
  540. set_current_blocked(&set);
  541. /*
  542. * x86_32 has no uc_flags bits relevant to restore_sigcontext.
  543. * Save a few cycles by skipping the __get_user.
  544. */
  545. if (restore_sigcontext(regs, &frame->sc, 0))
  546. goto badframe;
  547. return regs->ax;
  548. badframe:
  549. signal_fault(regs, frame, "sigreturn");
  550. return 0;
  551. }
  552. #endif /* CONFIG_X86_32 */
  553. SYSCALL_DEFINE0(rt_sigreturn)
  554. {
  555. struct pt_regs *regs = current_pt_regs();
  556. struct rt_sigframe __user *frame;
  557. sigset_t set;
  558. unsigned long uc_flags;
  559. frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
  560. if (!access_ok(frame, sizeof(*frame)))
  561. goto badframe;
  562. if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
  563. goto badframe;
  564. if (__get_user(uc_flags, &frame->uc.uc_flags))
  565. goto badframe;
  566. set_current_blocked(&set);
  567. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
  568. goto badframe;
  569. if (restore_altstack(&frame->uc.uc_stack))
  570. goto badframe;
  571. return regs->ax;
  572. badframe:
  573. signal_fault(regs, frame, "rt_sigreturn");
  574. return 0;
  575. }
  576. static inline int is_ia32_compat_frame(struct ksignal *ksig)
  577. {
  578. return IS_ENABLED(CONFIG_IA32_EMULATION) &&
  579. ksig->ka.sa.sa_flags & SA_IA32_ABI;
  580. }
  581. static inline int is_ia32_frame(struct ksignal *ksig)
  582. {
  583. return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
  584. }
  585. static inline int is_x32_frame(struct ksignal *ksig)
  586. {
  587. return IS_ENABLED(CONFIG_X86_X32_ABI) &&
  588. ksig->ka.sa.sa_flags & SA_X32_ABI;
  589. }
  590. static int
  591. setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  592. {
  593. int usig = ksig->sig;
  594. sigset_t *set = sigmask_to_save();
  595. compat_sigset_t *cset = (compat_sigset_t *) set;
  596. /* Perform fixup for the pre-signal frame. */
  597. rseq_signal_deliver(ksig, regs);
  598. /* Set up the stack frame */
  599. if (is_ia32_frame(ksig)) {
  600. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  601. return ia32_setup_rt_frame(usig, ksig, cset, regs);
  602. else
  603. return ia32_setup_frame(usig, ksig, cset, regs);
  604. } else if (is_x32_frame(ksig)) {
  605. return x32_setup_rt_frame(ksig, cset, regs);
  606. } else {
  607. return __setup_rt_frame(ksig->sig, ksig, set, regs);
  608. }
  609. }
  610. static void
  611. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  612. {
  613. bool stepping, failed;
  614. struct fpu *fpu = &current->thread.fpu;
  615. if (v8086_mode(regs))
  616. save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
  617. /* Are we from a system call? */
  618. if (syscall_get_nr(current, regs) >= 0) {
  619. /* If so, check system call restarting.. */
  620. switch (syscall_get_error(current, regs)) {
  621. case -ERESTART_RESTARTBLOCK:
  622. case -ERESTARTNOHAND:
  623. regs->ax = -EINTR;
  624. break;
  625. case -ERESTARTSYS:
  626. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  627. regs->ax = -EINTR;
  628. break;
  629. }
  630. fallthrough;
  631. case -ERESTARTNOINTR:
  632. regs->ax = regs->orig_ax;
  633. regs->ip -= 2;
  634. break;
  635. }
  636. }
  637. /*
  638. * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
  639. * so that register information in the sigcontext is correct and
  640. * then notify the tracer before entering the signal handler.
  641. */
  642. stepping = test_thread_flag(TIF_SINGLESTEP);
  643. if (stepping)
  644. user_disable_single_step(current);
  645. failed = (setup_rt_frame(ksig, regs) < 0);
  646. if (!failed) {
  647. /*
  648. * Clear the direction flag as per the ABI for function entry.
  649. *
  650. * Clear RF when entering the signal handler, because
  651. * it might disable possible debug exception from the
  652. * signal handler.
  653. *
  654. * Clear TF for the case when it wasn't set by debugger to
  655. * avoid the recursive send_sigtrap() in SIGTRAP handler.
  656. */
  657. regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
  658. /*
  659. * Ensure the signal handler starts with the new fpu state.
  660. */
  661. fpu__clear_user_states(fpu);
  662. }
  663. signal_setup_done(failed, ksig, stepping);
  664. }
  665. static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
  666. {
  667. #ifdef CONFIG_IA32_EMULATION
  668. if (current_thread_info()->status & TS_COMPAT_RESTART)
  669. return __NR_ia32_restart_syscall;
  670. #endif
  671. #ifdef CONFIG_X86_X32_ABI
  672. return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
  673. #else
  674. return __NR_restart_syscall;
  675. #endif
  676. }
  677. /*
  678. * Note that 'init' is a special process: it doesn't get signals it doesn't
  679. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  680. * mistake.
  681. */
  682. void arch_do_signal(struct pt_regs *regs)
  683. {
  684. struct ksignal ksig;
  685. if (get_signal(&ksig)) {
  686. /* Whee! Actually deliver the signal. */
  687. handle_signal(&ksig, regs);
  688. return;
  689. }
  690. /* Did we come from a system call? */
  691. if (syscall_get_nr(current, regs) >= 0) {
  692. /* Restart the system call - no handlers present */
  693. switch (syscall_get_error(current, regs)) {
  694. case -ERESTARTNOHAND:
  695. case -ERESTARTSYS:
  696. case -ERESTARTNOINTR:
  697. regs->ax = regs->orig_ax;
  698. regs->ip -= 2;
  699. break;
  700. case -ERESTART_RESTARTBLOCK:
  701. regs->ax = get_nr_restart_syscall(regs);
  702. regs->ip -= 2;
  703. break;
  704. }
  705. }
  706. /*
  707. * If there's no signal to deliver, we just put the saved sigmask
  708. * back.
  709. */
  710. restore_saved_sigmask();
  711. }
  712. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  713. {
  714. struct task_struct *me = current;
  715. if (show_unhandled_signals && printk_ratelimit()) {
  716. printk("%s"
  717. "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
  718. task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
  719. me->comm, me->pid, where, frame,
  720. regs->ip, regs->sp, regs->orig_ax);
  721. print_vma_addr(KERN_CONT " in ", regs->ip);
  722. pr_cont("\n");
  723. }
  724. force_sig(SIGSEGV);
  725. }
  726. #ifdef CONFIG_X86_X32_ABI
  727. COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
  728. {
  729. struct pt_regs *regs = current_pt_regs();
  730. struct rt_sigframe_x32 __user *frame;
  731. sigset_t set;
  732. unsigned long uc_flags;
  733. frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
  734. if (!access_ok(frame, sizeof(*frame)))
  735. goto badframe;
  736. if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
  737. goto badframe;
  738. if (__get_user(uc_flags, &frame->uc.uc_flags))
  739. goto badframe;
  740. set_current_blocked(&set);
  741. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
  742. goto badframe;
  743. if (compat_restore_altstack(&frame->uc.uc_stack))
  744. goto badframe;
  745. return regs->ax;
  746. badframe:
  747. signal_fault(regs, frame, "x32 rt_sigreturn");
  748. return 0;
  749. }
  750. #endif