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