signal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/kstrtox.h>
  17. #include <linux/errno.h>
  18. #include <linux/wait.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 <linux/rseq.h>
  29. #include <asm/processor.h>
  30. #include <asm/ucontext.h>
  31. #include <asm/fpu/signal.h>
  32. #include <asm/fpu/xstate.h>
  33. #include <asm/vdso.h>
  34. #include <asm/mce.h>
  35. #include <asm/sighandling.h>
  36. #include <asm/vm86.h>
  37. #include <asm/syscall.h>
  38. #include <asm/sigframe.h>
  39. #include <asm/signal.h>
  40. #include <asm/shstk.h>
  41. static inline int is_ia32_compat_frame(struct ksignal *ksig)
  42. {
  43. return IS_ENABLED(CONFIG_IA32_EMULATION) &&
  44. ksig->ka.sa.sa_flags & SA_IA32_ABI;
  45. }
  46. static inline int is_ia32_frame(struct ksignal *ksig)
  47. {
  48. return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
  49. }
  50. static inline int is_x32_frame(struct ksignal *ksig)
  51. {
  52. return IS_ENABLED(CONFIG_X86_X32_ABI) &&
  53. ksig->ka.sa.sa_flags & SA_X32_ABI;
  54. }
  55. /*
  56. * Enable all pkeys temporarily, so as to ensure that both the current
  57. * execution stack as well as the alternate signal stack are writeable.
  58. * The application can use any of the available pkeys to protect the
  59. * alternate signal stack, and we don't know which one it is, so enable
  60. * all. The PKRU register will be reset to init_pkru later in the flow,
  61. * in fpu__clear_user_states(), and it is the application's responsibility
  62. * to enable the appropriate pkey as the first step in the signal handler
  63. * so that the handler does not segfault.
  64. */
  65. static inline u32 sig_prepare_pkru(void)
  66. {
  67. u32 orig_pkru = read_pkru();
  68. write_pkru(0);
  69. return orig_pkru;
  70. }
  71. /*
  72. * Set up a signal frame.
  73. */
  74. /* x86 ABI requires 16-byte alignment */
  75. #define FRAME_ALIGNMENT 16UL
  76. #define MAX_FRAME_PADDING (FRAME_ALIGNMENT - 1)
  77. /*
  78. * Determine which stack to use..
  79. */
  80. void __user *
  81. get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size,
  82. void __user **fpstate)
  83. {
  84. struct k_sigaction *ka = &ksig->ka;
  85. int ia32_frame = is_ia32_frame(ksig);
  86. /* Default to using normal stack */
  87. bool nested_altstack = on_sig_stack(regs->sp);
  88. bool entering_altstack = false;
  89. unsigned long math_size = 0;
  90. unsigned long sp = regs->sp;
  91. unsigned long buf_fx = 0;
  92. u32 pkru;
  93. /* redzone */
  94. if (!ia32_frame)
  95. sp -= 128;
  96. /* This is the X/Open sanctioned signal stack switching. */
  97. if (ka->sa.sa_flags & SA_ONSTACK) {
  98. /*
  99. * This checks nested_altstack via sas_ss_flags(). Sensible
  100. * programs use SS_AUTODISARM, which disables that check, and
  101. * programs that don't use SS_AUTODISARM get compatible.
  102. */
  103. if (sas_ss_flags(sp) == 0) {
  104. sp = current->sas_ss_sp + current->sas_ss_size;
  105. entering_altstack = true;
  106. }
  107. } else if (ia32_frame &&
  108. !nested_altstack &&
  109. regs->ss != __USER_DS &&
  110. !(ka->sa.sa_flags & SA_RESTORER) &&
  111. ka->sa.sa_restorer) {
  112. /* This is the legacy signal stack switching. */
  113. sp = (unsigned long) ka->sa.sa_restorer;
  114. entering_altstack = true;
  115. }
  116. sp = fpu__alloc_mathframe(sp, ia32_frame, &buf_fx, &math_size);
  117. *fpstate = (void __user *)sp;
  118. sp -= frame_size;
  119. if (ia32_frame)
  120. /*
  121. * Align the stack pointer according to the i386 ABI,
  122. * i.e. so that on function entry ((sp + 4) & 15) == 0.
  123. */
  124. sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
  125. else
  126. sp = round_down(sp, FRAME_ALIGNMENT) - 8;
  127. /*
  128. * If we are on the alternate signal stack and would overflow it, don't.
  129. * Return an always-bogus address instead so we will die with SIGSEGV.
  130. */
  131. if (unlikely((nested_altstack || entering_altstack) &&
  132. !__on_sig_stack(sp))) {
  133. if (show_unhandled_signals && printk_ratelimit())
  134. pr_info("%s[%d] overflowed sigaltstack\n",
  135. current->comm, task_pid_nr(current));
  136. return (void __user *)-1L;
  137. }
  138. /* Update PKRU to enable access to the alternate signal stack. */
  139. pkru = sig_prepare_pkru();
  140. /* save i387 and extended state */
  141. if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size, pkru)) {
  142. /*
  143. * Restore PKRU to the original, user-defined value; disable
  144. * extra pkeys enabled for the alternate signal stack, if any.
  145. */
  146. write_pkru(pkru);
  147. return (void __user *)-1L;
  148. }
  149. return (void __user *)sp;
  150. }
  151. /*
  152. * There are four different struct types for signal frame: sigframe_ia32,
  153. * rt_sigframe_ia32, rt_sigframe_x32, and rt_sigframe. Use the worst case
  154. * -- the largest size. It means the size for 64-bit apps is a bit more
  155. * than needed, but this keeps the code simple.
  156. */
  157. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  158. # define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct sigframe_ia32)
  159. #else
  160. # define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct rt_sigframe)
  161. #endif
  162. /*
  163. * The FP state frame contains an XSAVE buffer which must be 64-byte aligned.
  164. * If a signal frame starts at an unaligned address, extra space is required.
  165. * This is the max alignment padding, conservatively.
  166. */
  167. #define MAX_XSAVE_PADDING 63UL
  168. /*
  169. * The frame data is composed of the following areas and laid out as:
  170. *
  171. * -------------------------
  172. * | alignment padding |
  173. * -------------------------
  174. * | (f)xsave frame |
  175. * -------------------------
  176. * | fsave header |
  177. * -------------------------
  178. * | alignment padding |
  179. * -------------------------
  180. * | siginfo + ucontext |
  181. * -------------------------
  182. */
  183. /* max_frame_size tells userspace the worst case signal stack size. */
  184. static unsigned long __ro_after_init max_frame_size;
  185. static unsigned int __ro_after_init fpu_default_state_size;
  186. static int __init init_sigframe_size(void)
  187. {
  188. fpu_default_state_size = fpu__get_fpstate_size();
  189. max_frame_size = MAX_FRAME_SIGINFO_UCTXT_SIZE + MAX_FRAME_PADDING;
  190. max_frame_size += fpu_default_state_size + MAX_XSAVE_PADDING;
  191. /* Userspace expects an aligned size. */
  192. max_frame_size = round_up(max_frame_size, FRAME_ALIGNMENT);
  193. pr_info("max sigframe size: %lu\n", max_frame_size);
  194. return 0;
  195. }
  196. early_initcall(init_sigframe_size);
  197. unsigned long get_sigframe_size(void)
  198. {
  199. return max_frame_size;
  200. }
  201. static int
  202. setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  203. {
  204. /* Perform fixup for the pre-signal frame. */
  205. rseq_signal_deliver(ksig, regs);
  206. /* Set up the stack frame */
  207. if (is_ia32_frame(ksig)) {
  208. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  209. return ia32_setup_rt_frame(ksig, regs);
  210. else
  211. return ia32_setup_frame(ksig, regs);
  212. } else if (is_x32_frame(ksig)) {
  213. return x32_setup_rt_frame(ksig, regs);
  214. } else {
  215. return x64_setup_rt_frame(ksig, regs);
  216. }
  217. }
  218. static void
  219. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  220. {
  221. bool stepping, failed;
  222. struct fpu *fpu = &current->thread.fpu;
  223. if (v8086_mode(regs))
  224. save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
  225. /* Are we from a system call? */
  226. if (syscall_get_nr(current, regs) != -1) {
  227. /* If so, check system call restarting.. */
  228. switch (syscall_get_error(current, regs)) {
  229. case -ERESTART_RESTARTBLOCK:
  230. case -ERESTARTNOHAND:
  231. regs->ax = -EINTR;
  232. break;
  233. case -ERESTARTSYS:
  234. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  235. regs->ax = -EINTR;
  236. break;
  237. }
  238. fallthrough;
  239. case -ERESTARTNOINTR:
  240. regs->ax = regs->orig_ax;
  241. regs->ip -= 2;
  242. break;
  243. }
  244. }
  245. /*
  246. * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
  247. * so that register information in the sigcontext is correct and
  248. * then notify the tracer before entering the signal handler.
  249. */
  250. stepping = test_thread_flag(TIF_SINGLESTEP);
  251. if (stepping)
  252. user_disable_single_step(current);
  253. failed = (setup_rt_frame(ksig, regs) < 0);
  254. if (!failed) {
  255. /*
  256. * Clear the direction flag as per the ABI for function entry.
  257. *
  258. * Clear RF when entering the signal handler, because
  259. * it might disable possible debug exception from the
  260. * signal handler.
  261. *
  262. * Clear TF for the case when it wasn't set by debugger to
  263. * avoid the recursive send_sigtrap() in SIGTRAP handler.
  264. */
  265. regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
  266. /*
  267. * Ensure the signal handler starts with the new fpu state.
  268. */
  269. fpu__clear_user_states(fpu);
  270. }
  271. signal_setup_done(failed, ksig, stepping);
  272. }
  273. static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
  274. {
  275. #ifdef CONFIG_IA32_EMULATION
  276. if (current->restart_block.arch_data & TS_COMPAT)
  277. return __NR_ia32_restart_syscall;
  278. #endif
  279. #ifdef CONFIG_X86_X32_ABI
  280. return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
  281. #else
  282. return __NR_restart_syscall;
  283. #endif
  284. }
  285. /*
  286. * Note that 'init' is a special process: it doesn't get signals it doesn't
  287. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  288. * mistake.
  289. */
  290. void arch_do_signal_or_restart(struct pt_regs *regs)
  291. {
  292. struct ksignal ksig;
  293. if (get_signal(&ksig)) {
  294. /* Whee! Actually deliver the signal. */
  295. handle_signal(&ksig, regs);
  296. return;
  297. }
  298. /* Did we come from a system call? */
  299. if (syscall_get_nr(current, regs) != -1) {
  300. /* Restart the system call - no handlers present */
  301. switch (syscall_get_error(current, regs)) {
  302. case -ERESTARTNOHAND:
  303. case -ERESTARTSYS:
  304. case -ERESTARTNOINTR:
  305. regs->ax = regs->orig_ax;
  306. regs->ip -= 2;
  307. break;
  308. case -ERESTART_RESTARTBLOCK:
  309. regs->ax = get_nr_restart_syscall(regs);
  310. regs->ip -= 2;
  311. break;
  312. }
  313. }
  314. /*
  315. * If there's no signal to deliver, we just put the saved sigmask
  316. * back.
  317. */
  318. restore_saved_sigmask();
  319. }
  320. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  321. {
  322. struct task_struct *me = current;
  323. if (show_unhandled_signals && printk_ratelimit()) {
  324. printk("%s"
  325. "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
  326. task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
  327. me->comm, me->pid, where, frame,
  328. regs->ip, regs->sp, regs->orig_ax);
  329. print_vma_addr(KERN_CONT " in ", regs->ip);
  330. pr_cont("\n");
  331. }
  332. force_sig(SIGSEGV);
  333. }
  334. #ifdef CONFIG_DYNAMIC_SIGFRAME
  335. #ifdef CONFIG_STRICT_SIGALTSTACK_SIZE
  336. static bool strict_sigaltstack_size __ro_after_init = true;
  337. #else
  338. static bool strict_sigaltstack_size __ro_after_init = false;
  339. #endif
  340. static int __init strict_sas_size(char *arg)
  341. {
  342. return kstrtobool(arg, &strict_sigaltstack_size) == 0;
  343. }
  344. __setup("strict_sas_size", strict_sas_size);
  345. /*
  346. * MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512
  347. * exceeds that size already. As such programs might never use the
  348. * sigaltstack they just continued to work. While always checking against
  349. * the real size would be correct, this might be considered a regression.
  350. *
  351. * Therefore avoid the sanity check, unless enforced by kernel
  352. * configuration or command line option.
  353. *
  354. * When dynamic FPU features are supported, the check is also enforced when
  355. * the task has permissions to use dynamic features. Tasks which have no
  356. * permission are checked against the size of the non-dynamic feature set
  357. * if strict checking is enabled. This avoids forcing all tasks on the
  358. * system to allocate large sigaltstacks even if they are never going
  359. * to use a dynamic feature. As this is serialized via sighand::siglock
  360. * any permission request for a dynamic feature either happened already
  361. * or will see the newly install sigaltstack size in the permission checks.
  362. */
  363. bool sigaltstack_size_valid(size_t ss_size)
  364. {
  365. unsigned long fsize = max_frame_size - fpu_default_state_size;
  366. u64 mask;
  367. lockdep_assert_held(&current->sighand->siglock);
  368. if (!fpu_state_size_dynamic() && !strict_sigaltstack_size)
  369. return true;
  370. fsize += current->group_leader->thread.fpu.perm.__user_state_size;
  371. if (likely(ss_size > fsize))
  372. return true;
  373. if (strict_sigaltstack_size)
  374. return ss_size > fsize;
  375. mask = current->group_leader->thread.fpu.perm.__state_perm;
  376. if (mask & XFEATURE_MASK_USER_DYNAMIC)
  377. return ss_size > fsize;
  378. return true;
  379. }
  380. #endif /* CONFIG_DYNAMIC_SIGFRAME */