1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25ceb40daSHiroshi Shimamoto /*
35ceb40daSHiroshi Shimamoto * Copyright (C) 1991, 1992 Linus Torvalds
45ceb40daSHiroshi Shimamoto * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
55ceb40daSHiroshi Shimamoto *
65ceb40daSHiroshi Shimamoto * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
75ceb40daSHiroshi Shimamoto * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
85ceb40daSHiroshi Shimamoto * 2000-2002 x86-64 support by Andi Kleen
95ceb40daSHiroshi Shimamoto */
10c767a54bSJoe Perches
11c767a54bSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12c767a54bSJoe Perches
135ceb40daSHiroshi Shimamoto #include <linux/sched.h>
1468db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
155ceb40daSHiroshi Shimamoto #include <linux/mm.h>
165ceb40daSHiroshi Shimamoto #include <linux/smp.h>
175ceb40daSHiroshi Shimamoto #include <linux/kernel.h>
183aac3ebeSThomas Gleixner #include <linux/kstrtox.h>
195ceb40daSHiroshi Shimamoto #include <linux/errno.h>
205ceb40daSHiroshi Shimamoto #include <linux/wait.h>
215ceb40daSHiroshi Shimamoto #include <linux/unistd.h>
225ceb40daSHiroshi Shimamoto #include <linux/stddef.h>
235ceb40daSHiroshi Shimamoto #include <linux/personality.h>
245ceb40daSHiroshi Shimamoto #include <linux/uaccess.h>
257c68af6eSAvi Kivity #include <linux/user-return-notifier.h>
260326f5a9SSrikar Dronamraju #include <linux/uprobes.h>
2791d1aa43SFrederic Weisbecker #include <linux/context_tracking.h>
28167fd210SThomas Gleixner #include <linux/entry-common.h>
294c8ca51aSTautschnig, Michael #include <linux/syscalls.h>
305ceb40daSHiroshi Shimamoto
315ceb40daSHiroshi Shimamoto #include <asm/processor.h>
325ceb40daSHiroshi Shimamoto #include <asm/ucontext.h>
33fcbc99c4SIngo Molnar #include <asm/fpu/signal.h>
344b7ca609SThomas Gleixner #include <asm/fpu/xstate.h>
355ceb40daSHiroshi Shimamoto #include <asm/vdso.h>
364efc0670SAndi Kleen #include <asm/mce.h>
37f28f0c23SH. Peter Anvin #include <asm/sighandling.h>
38ba3e127eSBrian Gerst #include <asm/vm86.h>
395ceb40daSHiroshi Shimamoto
405ceb40daSHiroshi Shimamoto #include <asm/syscall.h>
4141af86faSHiroshi Shimamoto #include <asm/sigframe.h>
4268463510SDmitry Safonov #include <asm/signal.h>
4305e36022SRick Edgecombe #include <asm/shstk.h>
445ceb40daSHiroshi Shimamoto
is_ia32_compat_frame(struct ksignal * ksig)459c36e592SBrian Gerst static inline int is_ia32_compat_frame(struct ksignal *ksig)
469c36e592SBrian Gerst {
479c36e592SBrian Gerst return IS_ENABLED(CONFIG_IA32_EMULATION) &&
489c36e592SBrian Gerst ksig->ka.sa.sa_flags & SA_IA32_ABI;
499c36e592SBrian Gerst }
509c36e592SBrian Gerst
is_ia32_frame(struct ksignal * ksig)519c36e592SBrian Gerst static inline int is_ia32_frame(struct ksignal *ksig)
529c36e592SBrian Gerst {
539c36e592SBrian Gerst return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
549c36e592SBrian Gerst }
559c36e592SBrian Gerst
is_x32_frame(struct ksignal * ksig)569c36e592SBrian Gerst static inline int is_x32_frame(struct ksignal *ksig)
579c36e592SBrian Gerst {
589c36e592SBrian Gerst return IS_ENABLED(CONFIG_X86_X32_ABI) &&
599c36e592SBrian Gerst ksig->ka.sa.sa_flags & SA_X32_ABI;
609c36e592SBrian Gerst }
619c36e592SBrian Gerst
625ceb40daSHiroshi Shimamoto /*
635ceb40daSHiroshi Shimamoto * Set up a signal frame.
645ceb40daSHiroshi Shimamoto */
6575779f05SHiroshi Shimamoto
66939ef713SChang S. Bae /* x86 ABI requires 16-byte alignment */
67939ef713SChang S. Bae #define FRAME_ALIGNMENT 16UL
68939ef713SChang S. Bae
69939ef713SChang S. Bae #define MAX_FRAME_PADDING (FRAME_ALIGNMENT - 1)
70939ef713SChang S. Bae
7175779f05SHiroshi Shimamoto /*
7275779f05SHiroshi Shimamoto * Determine which stack to use..
7375779f05SHiroshi Shimamoto */
749c36e592SBrian Gerst void __user *
get_sigframe(struct ksignal * ksig,struct pt_regs * regs,size_t frame_size,void __user ** fpstate)759c36e592SBrian Gerst get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size,
7675779f05SHiroshi Shimamoto void __user **fpstate)
7775779f05SHiroshi Shimamoto {
789c36e592SBrian Gerst struct k_sigaction *ka = &ksig->ka;
799c36e592SBrian Gerst int ia32_frame = is_ia32_frame(ksig);
8075779f05SHiroshi Shimamoto /* Default to using normal stack */
812beb4a53SChang S. Bae bool nested_altstack = on_sig_stack(regs->sp);
822beb4a53SChang S. Bae bool entering_altstack = false;
8372a671ceSSuresh Siddha unsigned long math_size = 0;
8475779f05SHiroshi Shimamoto unsigned long sp = regs->sp;
8572a671ceSSuresh Siddha unsigned long buf_fx = 0;
86*19059128SAruna Ramakrishna u32 pkru = read_pkru();
8775779f05SHiroshi Shimamoto
8875779f05SHiroshi Shimamoto /* redzone */
899c36e592SBrian Gerst if (!ia32_frame)
9075779f05SHiroshi Shimamoto sp -= 128;
9175779f05SHiroshi Shimamoto
9275779f05SHiroshi Shimamoto /* This is the X/Open sanctioned signal stack switching. */
9375779f05SHiroshi Shimamoto if (ka->sa.sa_flags & SA_ONSTACK) {
942beb4a53SChang S. Bae /*
952beb4a53SChang S. Bae * This checks nested_altstack via sas_ss_flags(). Sensible
962beb4a53SChang S. Bae * programs use SS_AUTODISARM, which disables that check, and
972beb4a53SChang S. Bae * programs that don't use SS_AUTODISARM get compatible.
982beb4a53SChang S. Bae */
992beb4a53SChang S. Bae if (sas_ss_flags(sp) == 0) {
10075779f05SHiroshi Shimamoto sp = current->sas_ss_sp + current->sas_ss_size;
1012beb4a53SChang S. Bae entering_altstack = true;
1022beb4a53SChang S. Bae }
1039c36e592SBrian Gerst } else if (ia32_frame &&
1042beb4a53SChang S. Bae !nested_altstack &&
10599504819SAndy Lutomirski regs->ss != __USER_DS &&
10675779f05SHiroshi Shimamoto !(ka->sa.sa_flags & SA_RESTORER) &&
107050902c0SSuresh Siddha ka->sa.sa_restorer) {
108050902c0SSuresh Siddha /* This is the legacy signal stack switching. */
10975779f05SHiroshi Shimamoto sp = (unsigned long) ka->sa.sa_restorer;
1102beb4a53SChang S. Bae entering_altstack = true;
11175779f05SHiroshi Shimamoto }
11275779f05SHiroshi Shimamoto
1139c36e592SBrian Gerst sp = fpu__alloc_mathframe(sp, ia32_frame, &buf_fx, &math_size);
11475779f05SHiroshi Shimamoto *fpstate = (void __user *)sp;
11575779f05SHiroshi Shimamoto
1169c36e592SBrian Gerst sp -= frame_size;
1179c36e592SBrian Gerst
1189c36e592SBrian Gerst if (ia32_frame)
1199c36e592SBrian Gerst /*
1209c36e592SBrian Gerst * Align the stack pointer according to the i386 ABI,
1219c36e592SBrian Gerst * i.e. so that on function entry ((sp + 4) & 15) == 0.
1229c36e592SBrian Gerst */
1239c36e592SBrian Gerst sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
1249c36e592SBrian Gerst else
1259c36e592SBrian Gerst sp = round_down(sp, FRAME_ALIGNMENT) - 8;
12614fc9fbcSHiroshi Shimamoto
12714fc9fbcSHiroshi Shimamoto /*
12814fc9fbcSHiroshi Shimamoto * If we are on the alternate signal stack and would overflow it, don't.
12914fc9fbcSHiroshi Shimamoto * Return an always-bogus address instead so we will die with SIGSEGV.
13014fc9fbcSHiroshi Shimamoto */
1312beb4a53SChang S. Bae if (unlikely((nested_altstack || entering_altstack) &&
1322beb4a53SChang S. Bae !__on_sig_stack(sp))) {
1332beb4a53SChang S. Bae
1342beb4a53SChang S. Bae if (show_unhandled_signals && printk_ratelimit())
1352beb4a53SChang S. Bae pr_info("%s[%d] overflowed sigaltstack\n",
1362beb4a53SChang S. Bae current->comm, task_pid_nr(current));
1372beb4a53SChang S. Bae
13814fc9fbcSHiroshi Shimamoto return (void __user *)-1L;
1392beb4a53SChang S. Bae }
14014fc9fbcSHiroshi Shimamoto
14172a671ceSSuresh Siddha /* save i387 and extended state */
142*19059128SAruna Ramakrishna if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size, pkru))
14314fc9fbcSHiroshi Shimamoto return (void __user *)-1L;
14414fc9fbcSHiroshi Shimamoto
14514fc9fbcSHiroshi Shimamoto return (void __user *)sp;
14675779f05SHiroshi Shimamoto }
14775779f05SHiroshi Shimamoto
148939ef713SChang S. Bae /*
149939ef713SChang S. Bae * There are four different struct types for signal frame: sigframe_ia32,
150939ef713SChang S. Bae * rt_sigframe_ia32, rt_sigframe_x32, and rt_sigframe. Use the worst case
151939ef713SChang S. Bae * -- the largest size. It means the size for 64-bit apps is a bit more
152939ef713SChang S. Bae * than needed, but this keeps the code simple.
153939ef713SChang S. Bae */
154939ef713SChang S. Bae #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
155939ef713SChang S. Bae # define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct sigframe_ia32)
156939ef713SChang S. Bae #else
157939ef713SChang S. Bae # define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct rt_sigframe)
158939ef713SChang S. Bae #endif
159939ef713SChang S. Bae
160939ef713SChang S. Bae /*
161939ef713SChang S. Bae * The FP state frame contains an XSAVE buffer which must be 64-byte aligned.
162939ef713SChang S. Bae * If a signal frame starts at an unaligned address, extra space is required.
163939ef713SChang S. Bae * This is the max alignment padding, conservatively.
164939ef713SChang S. Bae */
165939ef713SChang S. Bae #define MAX_XSAVE_PADDING 63UL
166939ef713SChang S. Bae
167939ef713SChang S. Bae /*
168939ef713SChang S. Bae * The frame data is composed of the following areas and laid out as:
169939ef713SChang S. Bae *
170939ef713SChang S. Bae * -------------------------
171939ef713SChang S. Bae * | alignment padding |
172939ef713SChang S. Bae * -------------------------
173939ef713SChang S. Bae * | (f)xsave frame |
174939ef713SChang S. Bae * -------------------------
175939ef713SChang S. Bae * | fsave header |
176939ef713SChang S. Bae * -------------------------
177939ef713SChang S. Bae * | alignment padding |
178939ef713SChang S. Bae * -------------------------
179939ef713SChang S. Bae * | siginfo + ucontext |
180939ef713SChang S. Bae * -------------------------
181939ef713SChang S. Bae */
182939ef713SChang S. Bae
183939ef713SChang S. Bae /* max_frame_size tells userspace the worst case signal stack size. */
184939ef713SChang S. Bae static unsigned long __ro_after_init max_frame_size;
1854b7ca609SThomas Gleixner static unsigned int __ro_after_init fpu_default_state_size;
186939ef713SChang S. Bae
init_sigframe_size(void)18754d9a91aSThomas Gleixner static int __init init_sigframe_size(void)
188939ef713SChang S. Bae {
1894b7ca609SThomas Gleixner fpu_default_state_size = fpu__get_fpstate_size();
1904b7ca609SThomas Gleixner
191939ef713SChang S. Bae max_frame_size = MAX_FRAME_SIGINFO_UCTXT_SIZE + MAX_FRAME_PADDING;
192939ef713SChang S. Bae
1934b7ca609SThomas Gleixner max_frame_size += fpu_default_state_size + MAX_XSAVE_PADDING;
194939ef713SChang S. Bae
195939ef713SChang S. Bae /* Userspace expects an aligned size. */
196939ef713SChang S. Bae max_frame_size = round_up(max_frame_size, FRAME_ALIGNMENT);
197939ef713SChang S. Bae
198939ef713SChang S. Bae pr_info("max sigframe size: %lu\n", max_frame_size);
19954d9a91aSThomas Gleixner return 0;
200939ef713SChang S. Bae }
20154d9a91aSThomas Gleixner early_initcall(init_sigframe_size);
202939ef713SChang S. Bae
get_sigframe_size(void)2031c33bb05SChang S. Bae unsigned long get_sigframe_size(void)
2041c33bb05SChang S. Bae {
2051c33bb05SChang S. Bae return max_frame_size;
2061c33bb05SChang S. Bae }
2071c33bb05SChang S. Bae
2085ceb40daSHiroshi Shimamoto static int
setup_rt_frame(struct ksignal * ksig,struct pt_regs * regs)209235b8022SAl Viro setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
2105ceb40daSHiroshi Shimamoto {
211bff9504bSMathieu Desnoyers /* Perform fixup for the pre-signal frame. */
212784e0300SWill Deacon rseq_signal_deliver(ksig, regs);
213d6761b8fSMathieu Desnoyers
2145ceb40daSHiroshi Shimamoto /* Set up the stack frame */
21568463510SDmitry Safonov if (is_ia32_frame(ksig)) {
216235b8022SAl Viro if (ksig->ka.sa.sa_flags & SA_SIGINFO)
217f544822eSBrian Gerst return ia32_setup_rt_frame(ksig, regs);
2185ceb40daSHiroshi Shimamoto else
219f544822eSBrian Gerst return ia32_setup_frame(ksig, regs);
22068463510SDmitry Safonov } else if (is_x32_frame(ksig)) {
221f544822eSBrian Gerst return x32_setup_rt_frame(ksig, regs);
222c5a37394SH. Peter Anvin } else {
223c461ae39SBrian Gerst return x64_setup_rt_frame(ksig, regs);
224a610d6e6SAl Viro }
225c5a37394SH. Peter Anvin }
2265ceb40daSHiroshi Shimamoto
227a610d6e6SAl Viro static void
handle_signal(struct ksignal * ksig,struct pt_regs * regs)228235b8022SAl Viro handle_signal(struct ksignal *ksig, struct pt_regs *regs)
2295ceb40daSHiroshi Shimamoto {
230fd0f86b6SOleg Nesterov bool stepping, failed;
231c5bedc68SIngo Molnar struct fpu *fpu = ¤t->thread.fpu;
232fd0f86b6SOleg Nesterov
2335ed92a8aSBrian Gerst if (v8086_mode(regs))
2345ed92a8aSBrian Gerst save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
2355ed92a8aSBrian Gerst
2365ceb40daSHiroshi Shimamoto /* Are we from a system call? */
2379ddcb87bSH. Peter Anvin if (syscall_get_nr(current, regs) != -1) {
2385ceb40daSHiroshi Shimamoto /* If so, check system call restarting.. */
2395ceb40daSHiroshi Shimamoto switch (syscall_get_error(current, regs)) {
2405ceb40daSHiroshi Shimamoto case -ERESTART_RESTARTBLOCK:
2415ceb40daSHiroshi Shimamoto case -ERESTARTNOHAND:
2425ceb40daSHiroshi Shimamoto regs->ax = -EINTR;
2435ceb40daSHiroshi Shimamoto break;
2445ceb40daSHiroshi Shimamoto
2455ceb40daSHiroshi Shimamoto case -ERESTARTSYS:
246235b8022SAl Viro if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
2475ceb40daSHiroshi Shimamoto regs->ax = -EINTR;
2485ceb40daSHiroshi Shimamoto break;
2495ceb40daSHiroshi Shimamoto }
250df561f66SGustavo A. R. Silva fallthrough;
2515ceb40daSHiroshi Shimamoto case -ERESTARTNOINTR:
2525ceb40daSHiroshi Shimamoto regs->ax = regs->orig_ax;
2535ceb40daSHiroshi Shimamoto regs->ip -= 2;
2545ceb40daSHiroshi Shimamoto break;
2555ceb40daSHiroshi Shimamoto }
2565ceb40daSHiroshi Shimamoto }
2575ceb40daSHiroshi Shimamoto
2585ceb40daSHiroshi Shimamoto /*
259fd0f86b6SOleg Nesterov * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
260fd0f86b6SOleg Nesterov * so that register information in the sigcontext is correct and
261fd0f86b6SOleg Nesterov * then notify the tracer before entering the signal handler.
2625ceb40daSHiroshi Shimamoto */
263fd0f86b6SOleg Nesterov stepping = test_thread_flag(TIF_SINGLESTEP);
264fd0f86b6SOleg Nesterov if (stepping)
265fd0f86b6SOleg Nesterov user_disable_single_step(current);
2665ceb40daSHiroshi Shimamoto
267235b8022SAl Viro failed = (setup_rt_frame(ksig, regs) < 0);
268235b8022SAl Viro if (!failed) {
2695ceb40daSHiroshi Shimamoto /*
2705ceb40daSHiroshi Shimamoto * Clear the direction flag as per the ABI for function entry.
271ddd40da4SJiri Olsa *
27224cda109SJiri Olsa * Clear RF when entering the signal handler, because
27324cda109SJiri Olsa * it might disable possible debug exception from the
27424cda109SJiri Olsa * signal handler.
275ddd40da4SJiri Olsa *
276fd0f86b6SOleg Nesterov * Clear TF for the case when it wasn't set by debugger to
277fd0f86b6SOleg Nesterov * avoid the recursive send_sigtrap() in SIGTRAP handler.
2785ceb40daSHiroshi Shimamoto */
279ddd40da4SJiri Olsa regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
28066463db4SOleg Nesterov /*
28166463db4SOleg Nesterov * Ensure the signal handler starts with the new fpu state.
28266463db4SOleg Nesterov */
283b860eb8dSFenghua Yu fpu__clear_user_states(fpu);
284235b8022SAl Viro }
285fd0f86b6SOleg Nesterov signal_setup_done(failed, ksig, stepping);
2865ceb40daSHiroshi Shimamoto }
2875ceb40daSHiroshi Shimamoto
get_nr_restart_syscall(const struct pt_regs * regs)28822eab110SDmitry V. Levin static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
28922eab110SDmitry V. Levin {
290609c19a3SAndy Lutomirski #ifdef CONFIG_IA32_EMULATION
291b2e9df85SOleg Nesterov if (current->restart_block.arch_data & TS_COMPAT)
29295d97adbSDmitry V. Levin return __NR_ia32_restart_syscall;
29395d97adbSDmitry V. Levin #endif
29495d97adbSDmitry V. Levin #ifdef CONFIG_X86_X32_ABI
29595d97adbSDmitry V. Levin return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
29695d97adbSDmitry V. Levin #else
29722eab110SDmitry V. Levin return __NR_restart_syscall;
29895d97adbSDmitry V. Levin #endif
29922eab110SDmitry V. Levin }
3005ceb40daSHiroshi Shimamoto
3015ceb40daSHiroshi Shimamoto /*
3025ceb40daSHiroshi Shimamoto * Note that 'init' is a special process: it doesn't get signals it doesn't
3035ceb40daSHiroshi Shimamoto * want to handle. Thus you cannot kill init even with a SIGKILL even by
3045ceb40daSHiroshi Shimamoto * mistake.
3055ceb40daSHiroshi Shimamoto */
arch_do_signal_or_restart(struct pt_regs * regs)3068ba62d37SEric W. Biederman void arch_do_signal_or_restart(struct pt_regs *regs)
3075ceb40daSHiroshi Shimamoto {
308235b8022SAl Viro struct ksignal ksig;
3095ceb40daSHiroshi Shimamoto
3108ba62d37SEric W. Biederman if (get_signal(&ksig)) {
3115ceb40daSHiroshi Shimamoto /* Whee! Actually deliver the signal. */
312235b8022SAl Viro handle_signal(&ksig, regs);
3135ceb40daSHiroshi Shimamoto return;
3145ceb40daSHiroshi Shimamoto }
3155ceb40daSHiroshi Shimamoto
3165ceb40daSHiroshi Shimamoto /* Did we come from a system call? */
3179ddcb87bSH. Peter Anvin if (syscall_get_nr(current, regs) != -1) {
3185ceb40daSHiroshi Shimamoto /* Restart the system call - no handlers present */
3195ceb40daSHiroshi Shimamoto switch (syscall_get_error(current, regs)) {
3205ceb40daSHiroshi Shimamoto case -ERESTARTNOHAND:
3215ceb40daSHiroshi Shimamoto case -ERESTARTSYS:
3225ceb40daSHiroshi Shimamoto case -ERESTARTNOINTR:
3235ceb40daSHiroshi Shimamoto regs->ax = regs->orig_ax;
3245ceb40daSHiroshi Shimamoto regs->ip -= 2;
3255ceb40daSHiroshi Shimamoto break;
3265ceb40daSHiroshi Shimamoto
3275ceb40daSHiroshi Shimamoto case -ERESTART_RESTARTBLOCK:
32822eab110SDmitry V. Levin regs->ax = get_nr_restart_syscall(regs);
3295ceb40daSHiroshi Shimamoto regs->ip -= 2;
3305ceb40daSHiroshi Shimamoto break;
3315ceb40daSHiroshi Shimamoto }
3325ceb40daSHiroshi Shimamoto }
3335ceb40daSHiroshi Shimamoto
3345ceb40daSHiroshi Shimamoto /*
3355ceb40daSHiroshi Shimamoto * If there's no signal to deliver, we just put the saved sigmask
3365ceb40daSHiroshi Shimamoto * back.
3375ceb40daSHiroshi Shimamoto */
33851a7b448SAl Viro restore_saved_sigmask();
3395ceb40daSHiroshi Shimamoto }
3405ceb40daSHiroshi Shimamoto
signal_fault(struct pt_regs * regs,void __user * frame,char * where)3415ceb40daSHiroshi Shimamoto void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
3425ceb40daSHiroshi Shimamoto {
3435ceb40daSHiroshi Shimamoto struct task_struct *me = current;
3445ceb40daSHiroshi Shimamoto
3455ceb40daSHiroshi Shimamoto if (show_unhandled_signals && printk_ratelimit()) {
346ae417bb4SHiroshi Shimamoto printk("%s"
3475ceb40daSHiroshi Shimamoto "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
348ae417bb4SHiroshi Shimamoto task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
3495ceb40daSHiroshi Shimamoto me->comm, me->pid, where, frame,
3505ceb40daSHiroshi Shimamoto regs->ip, regs->sp, regs->orig_ax);
3511c99a687SMarkus Trippelsdorf print_vma_addr(KERN_CONT " in ", regs->ip);
352c767a54bSJoe Perches pr_cont("\n");
3535ceb40daSHiroshi Shimamoto }
3545ceb40daSHiroshi Shimamoto
3553cf5d076SEric W. Biederman force_sig(SIGSEGV);
3565ceb40daSHiroshi Shimamoto }
357c5a37394SH. Peter Anvin
3583aac3ebeSThomas Gleixner #ifdef CONFIG_DYNAMIC_SIGFRAME
3593aac3ebeSThomas Gleixner #ifdef CONFIG_STRICT_SIGALTSTACK_SIZE
3603aac3ebeSThomas Gleixner static bool strict_sigaltstack_size __ro_after_init = true;
3613aac3ebeSThomas Gleixner #else
3623aac3ebeSThomas Gleixner static bool strict_sigaltstack_size __ro_after_init = false;
3633aac3ebeSThomas Gleixner #endif
3643aac3ebeSThomas Gleixner
strict_sas_size(char * arg)3653aac3ebeSThomas Gleixner static int __init strict_sas_size(char *arg)
3663aac3ebeSThomas Gleixner {
367ef6dfc4bSChristophe JAILLET return kstrtobool(arg, &strict_sigaltstack_size) == 0;
3683aac3ebeSThomas Gleixner }
3693aac3ebeSThomas Gleixner __setup("strict_sas_size", strict_sas_size);
3703aac3ebeSThomas Gleixner
3713aac3ebeSThomas Gleixner /*
3723aac3ebeSThomas Gleixner * MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512
3733aac3ebeSThomas Gleixner * exceeds that size already. As such programs might never use the
3743aac3ebeSThomas Gleixner * sigaltstack they just continued to work. While always checking against
3753aac3ebeSThomas Gleixner * the real size would be correct, this might be considered a regression.
3763aac3ebeSThomas Gleixner *
3774b7ca609SThomas Gleixner * Therefore avoid the sanity check, unless enforced by kernel
3784b7ca609SThomas Gleixner * configuration or command line option.
3794b7ca609SThomas Gleixner *
3804b7ca609SThomas Gleixner * When dynamic FPU features are supported, the check is also enforced when
3814b7ca609SThomas Gleixner * the task has permissions to use dynamic features. Tasks which have no
3824b7ca609SThomas Gleixner * permission are checked against the size of the non-dynamic feature set
3834b7ca609SThomas Gleixner * if strict checking is enabled. This avoids forcing all tasks on the
3844b7ca609SThomas Gleixner * system to allocate large sigaltstacks even if they are never going
3854b7ca609SThomas Gleixner * to use a dynamic feature. As this is serialized via sighand::siglock
3864b7ca609SThomas Gleixner * any permission request for a dynamic feature either happened already
3874b7ca609SThomas Gleixner * or will see the newly install sigaltstack size in the permission checks.
3883aac3ebeSThomas Gleixner */
sigaltstack_size_valid(size_t ss_size)3893aac3ebeSThomas Gleixner bool sigaltstack_size_valid(size_t ss_size)
3903aac3ebeSThomas Gleixner {
3914b7ca609SThomas Gleixner unsigned long fsize = max_frame_size - fpu_default_state_size;
3924b7ca609SThomas Gleixner u64 mask;
3934b7ca609SThomas Gleixner
3943aac3ebeSThomas Gleixner lockdep_assert_held(¤t->sighand->siglock);
3953aac3ebeSThomas Gleixner
3964b7ca609SThomas Gleixner if (!fpu_state_size_dynamic() && !strict_sigaltstack_size)
3974b7ca609SThomas Gleixner return true;
3984b7ca609SThomas Gleixner
3994b7ca609SThomas Gleixner fsize += current->group_leader->thread.fpu.perm.__user_state_size;
4004b7ca609SThomas Gleixner if (likely(ss_size > fsize))
4014b7ca609SThomas Gleixner return true;
4024b7ca609SThomas Gleixner
4033aac3ebeSThomas Gleixner if (strict_sigaltstack_size)
4044b7ca609SThomas Gleixner return ss_size > fsize;
4054b7ca609SThomas Gleixner
4064b7ca609SThomas Gleixner mask = current->group_leader->thread.fpu.perm.__state_perm;
4074b7ca609SThomas Gleixner if (mask & XFEATURE_MASK_USER_DYNAMIC)
4084b7ca609SThomas Gleixner return ss_size > fsize;
4093aac3ebeSThomas Gleixner
4103aac3ebeSThomas Gleixner return true;
4113aac3ebeSThomas Gleixner }
4123aac3ebeSThomas Gleixner #endif /* CONFIG_DYNAMIC_SIGFRAME */
413