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