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