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