• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
force_valid_ss(struct pt_regs * regs)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 
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * usc,unsigned long uc_flags)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
__unsafe_setup_sigcontext(struct sigcontext __user * sc,void __user * fpstate,struct pt_regs * regs,unsigned long mask)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 /*
216  * Determine which stack to use..
217  */
align_sigframe(unsigned long sp)218 static unsigned long align_sigframe(unsigned long sp)
219 {
220 #ifdef CONFIG_X86_32
221 	/*
222 	 * Align the stack pointer according to the i386 ABI,
223 	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
224 	 */
225 	sp = ((sp + 4) & -16ul) - 4;
226 #else /* !CONFIG_X86_32 */
227 	sp = round_down(sp, 16) - 8;
228 #endif
229 	return sp;
230 }
231 
232 static void __user *
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size,void __user ** fpstate)233 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
234 	     void __user **fpstate)
235 {
236 	/* Default to using normal stack */
237 	bool nested_altstack = on_sig_stack(regs->sp);
238 	bool entering_altstack = false;
239 	unsigned long math_size = 0;
240 	unsigned long sp = regs->sp;
241 	unsigned long buf_fx = 0;
242 	int ret;
243 
244 	/* redzone */
245 	if (IS_ENABLED(CONFIG_X86_64))
246 		sp -= 128;
247 
248 	/* This is the X/Open sanctioned signal stack switching.  */
249 	if (ka->sa.sa_flags & SA_ONSTACK) {
250 		/*
251 		 * This checks nested_altstack via sas_ss_flags(). Sensible
252 		 * programs use SS_AUTODISARM, which disables that check, and
253 		 * programs that don't use SS_AUTODISARM get compatible.
254 		 */
255 		if (sas_ss_flags(sp) == 0) {
256 			sp = current->sas_ss_sp + current->sas_ss_size;
257 			entering_altstack = true;
258 		}
259 	} else if (IS_ENABLED(CONFIG_X86_32) &&
260 		   !nested_altstack &&
261 		   regs->ss != __USER_DS &&
262 		   !(ka->sa.sa_flags & SA_RESTORER) &&
263 		   ka->sa.sa_restorer) {
264 		/* This is the legacy signal stack switching. */
265 		sp = (unsigned long) ka->sa.sa_restorer;
266 		entering_altstack = true;
267 	}
268 
269 	sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
270 				  &buf_fx, &math_size);
271 	*fpstate = (void __user *)sp;
272 
273 	sp = align_sigframe(sp - frame_size);
274 
275 	/*
276 	 * If we are on the alternate signal stack and would overflow it, don't.
277 	 * Return an always-bogus address instead so we will die with SIGSEGV.
278 	 */
279 	if (unlikely((nested_altstack || entering_altstack) &&
280 		     !__on_sig_stack(sp))) {
281 
282 		if (show_unhandled_signals && printk_ratelimit())
283 			pr_info("%s[%d] overflowed sigaltstack\n",
284 				current->comm, task_pid_nr(current));
285 
286 		return (void __user *)-1L;
287 	}
288 
289 	/* save i387 and extended state */
290 	ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
291 	if (ret < 0)
292 		return (void __user *)-1L;
293 
294 	return (void __user *)sp;
295 }
296 
297 #ifdef CONFIG_X86_32
298 static const struct {
299 	u16 poplmovl;
300 	u32 val;
301 	u16 int80;
302 } __attribute__((packed)) retcode = {
303 	0xb858,		/* popl %eax; movl $..., %eax */
304 	__NR_sigreturn,
305 	0x80cd,		/* int $0x80 */
306 };
307 
308 static const struct {
309 	u8  movl;
310 	u32 val;
311 	u16 int80;
312 	u8  pad;
313 } __attribute__((packed)) rt_retcode = {
314 	0xb8,		/* movl $..., %eax */
315 	__NR_rt_sigreturn,
316 	0x80cd,		/* int $0x80 */
317 	0
318 };
319 
320 static int
__setup_frame(int sig,struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)321 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
322 	      struct pt_regs *regs)
323 {
324 	struct sigframe __user *frame;
325 	void __user *restorer;
326 	void __user *fp = NULL;
327 
328 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
329 
330 	if (!user_access_begin(frame, sizeof(*frame)))
331 		return -EFAULT;
332 
333 	unsafe_put_user(sig, &frame->sig, Efault);
334 	unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
335 	unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
336 	if (current->mm->context.vdso)
337 		restorer = current->mm->context.vdso +
338 			vdso_image_32.sym___kernel_sigreturn;
339 	else
340 		restorer = &frame->retcode;
341 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
342 		restorer = ksig->ka.sa.sa_restorer;
343 
344 	/* Set up to return from userspace.  */
345 	unsafe_put_user(restorer, &frame->pretcode, Efault);
346 
347 	/*
348 	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
349 	 *
350 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
351 	 * reasons and because gdb uses it as a signature to notice
352 	 * signal handler stack frames.
353 	 */
354 	unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
355 	user_access_end();
356 
357 	/* Set up registers for signal handler */
358 	regs->sp = (unsigned long)frame;
359 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
360 	regs->ax = (unsigned long)sig;
361 	regs->dx = 0;
362 	regs->cx = 0;
363 
364 	regs->ds = __USER_DS;
365 	regs->es = __USER_DS;
366 	regs->ss = __USER_DS;
367 	regs->cs = __USER_CS;
368 
369 	return 0;
370 
371 Efault:
372 	user_access_end();
373 	return -EFAULT;
374 }
375 
__setup_rt_frame(int sig,struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)376 static int __setup_rt_frame(int sig, struct ksignal *ksig,
377 			    sigset_t *set, struct pt_regs *regs)
378 {
379 	struct rt_sigframe __user *frame;
380 	void __user *restorer;
381 	void __user *fp = NULL;
382 
383 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
384 
385 	if (!user_access_begin(frame, sizeof(*frame)))
386 		return -EFAULT;
387 
388 	unsafe_put_user(sig, &frame->sig, Efault);
389 	unsafe_put_user(&frame->info, &frame->pinfo, Efault);
390 	unsafe_put_user(&frame->uc, &frame->puc, Efault);
391 
392 	/* Create the ucontext.  */
393 	if (static_cpu_has(X86_FEATURE_XSAVE))
394 		unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
395 	else
396 		unsafe_put_user(0, &frame->uc.uc_flags, Efault);
397 	unsafe_put_user(0, &frame->uc.uc_link, Efault);
398 	unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
399 
400 	/* Set up to return from userspace.  */
401 	restorer = current->mm->context.vdso +
402 		vdso_image_32.sym___kernel_rt_sigreturn;
403 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
404 		restorer = ksig->ka.sa.sa_restorer;
405 	unsafe_put_user(restorer, &frame->pretcode, Efault);
406 
407 	/*
408 	 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
409 	 *
410 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
411 	 * reasons and because gdb uses it as a signature to notice
412 	 * signal handler stack frames.
413 	 */
414 	unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
415 	unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
416 	unsafe_put_sigmask(set, frame, Efault);
417 	user_access_end();
418 
419 	if (copy_siginfo_to_user(&frame->info, &ksig->info))
420 		return -EFAULT;
421 
422 	/* Set up registers for signal handler */
423 	regs->sp = (unsigned long)frame;
424 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
425 	regs->ax = (unsigned long)sig;
426 	regs->dx = (unsigned long)&frame->info;
427 	regs->cx = (unsigned long)&frame->uc;
428 
429 	regs->ds = __USER_DS;
430 	regs->es = __USER_DS;
431 	regs->ss = __USER_DS;
432 	regs->cs = __USER_CS;
433 
434 	return 0;
435 Efault:
436 	user_access_end();
437 	return -EFAULT;
438 }
439 #else /* !CONFIG_X86_32 */
frame_uc_flags(struct pt_regs * regs)440 static unsigned long frame_uc_flags(struct pt_regs *regs)
441 {
442 	unsigned long flags;
443 
444 	if (boot_cpu_has(X86_FEATURE_XSAVE))
445 		flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
446 	else
447 		flags = UC_SIGCONTEXT_SS;
448 
449 	if (likely(user_64bit_mode(regs)))
450 		flags |= UC_STRICT_RESTORE_SS;
451 
452 	return flags;
453 }
454 
__setup_rt_frame(int sig,struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)455 static int __setup_rt_frame(int sig, struct ksignal *ksig,
456 			    sigset_t *set, struct pt_regs *regs)
457 {
458 	struct rt_sigframe __user *frame;
459 	void __user *fp = NULL;
460 	unsigned long uc_flags;
461 
462 	/* x86-64 should always use SA_RESTORER. */
463 	if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
464 		return -EFAULT;
465 
466 	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
467 	uc_flags = frame_uc_flags(regs);
468 
469 	if (!user_access_begin(frame, sizeof(*frame)))
470 		return -EFAULT;
471 
472 	/* Create the ucontext.  */
473 	unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
474 	unsafe_put_user(0, &frame->uc.uc_link, Efault);
475 	unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
476 
477 	/* Set up to return from userspace.  If provided, use a stub
478 	   already in userspace.  */
479 	unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
480 	unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
481 	unsafe_put_sigmask(set, frame, Efault);
482 	user_access_end();
483 
484 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
485 		if (copy_siginfo_to_user(&frame->info, &ksig->info))
486 			return -EFAULT;
487 	}
488 
489 	/* Set up registers for signal handler */
490 	regs->di = sig;
491 	/* In case the signal handler was declared without prototypes */
492 	regs->ax = 0;
493 
494 	/* This also works for non SA_SIGINFO handlers because they expect the
495 	   next argument after the signal number on the stack. */
496 	regs->si = (unsigned long)&frame->info;
497 	regs->dx = (unsigned long)&frame->uc;
498 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
499 
500 	regs->sp = (unsigned long)frame;
501 
502 	/*
503 	 * Set up the CS and SS registers to run signal handlers in
504 	 * 64-bit mode, even if the handler happens to be interrupting
505 	 * 32-bit or 16-bit code.
506 	 *
507 	 * SS is subtle.  In 64-bit mode, we don't need any particular
508 	 * SS descriptor, but we do need SS to be valid.  It's possible
509 	 * that the old SS is entirely bogus -- this can happen if the
510 	 * signal we're trying to deliver is #GP or #SS caused by a bad
511 	 * SS value.  We also have a compatbility issue here: DOSEMU
512 	 * relies on the contents of the SS register indicating the
513 	 * SS value at the time of the signal, even though that code in
514 	 * DOSEMU predates sigreturn's ability to restore SS.  (DOSEMU
515 	 * avoids relying on sigreturn to restore SS; instead it uses
516 	 * a trampoline.)  So we do our best: if the old SS was valid,
517 	 * we keep it.  Otherwise we replace it.
518 	 */
519 	regs->cs = __USER_CS;
520 
521 	if (unlikely(regs->ss != __USER_DS))
522 		force_valid_ss(regs);
523 
524 	return 0;
525 
526 Efault:
527 	user_access_end();
528 	return -EFAULT;
529 }
530 #endif /* CONFIG_X86_32 */
531 
532 #ifdef CONFIG_X86_X32_ABI
x32_copy_siginfo_to_user(struct compat_siginfo __user * to,const struct kernel_siginfo * from)533 static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
534 		const struct kernel_siginfo *from)
535 {
536 	struct compat_siginfo new;
537 
538 	copy_siginfo_to_external32(&new, from);
539 	if (from->si_signo == SIGCHLD) {
540 		new._sifields._sigchld_x32._utime = from->si_utime;
541 		new._sifields._sigchld_x32._stime = from->si_stime;
542 	}
543 	if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
544 		return -EFAULT;
545 	return 0;
546 }
547 
copy_siginfo_to_user32(struct compat_siginfo __user * to,const struct kernel_siginfo * from)548 int copy_siginfo_to_user32(struct compat_siginfo __user *to,
549 			   const struct kernel_siginfo *from)
550 {
551 	if (in_x32_syscall())
552 		return x32_copy_siginfo_to_user(to, from);
553 	return __copy_siginfo_to_user32(to, from);
554 }
555 #endif /* CONFIG_X86_X32_ABI */
556 
x32_setup_rt_frame(struct ksignal * ksig,compat_sigset_t * set,struct pt_regs * regs)557 static int x32_setup_rt_frame(struct ksignal *ksig,
558 			      compat_sigset_t *set,
559 			      struct pt_regs *regs)
560 {
561 #ifdef CONFIG_X86_X32_ABI
562 	struct rt_sigframe_x32 __user *frame;
563 	unsigned long uc_flags;
564 	void __user *restorer;
565 	void __user *fp = NULL;
566 
567 	if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
568 		return -EFAULT;
569 
570 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
571 
572 	uc_flags = frame_uc_flags(regs);
573 
574 	if (!user_access_begin(frame, sizeof(*frame)))
575 		return -EFAULT;
576 
577 	/* Create the ucontext.  */
578 	unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
579 	unsafe_put_user(0, &frame->uc.uc_link, Efault);
580 	unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
581 	unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
582 	restorer = ksig->ka.sa.sa_restorer;
583 	unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
584 	unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
585 	unsafe_put_sigmask(set, frame, Efault);
586 	user_access_end();
587 
588 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
589 		if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
590 			return -EFAULT;
591 	}
592 
593 	/* Set up registers for signal handler */
594 	regs->sp = (unsigned long) frame;
595 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
596 
597 	/* We use the x32 calling convention here... */
598 	regs->di = ksig->sig;
599 	regs->si = (unsigned long) &frame->info;
600 	regs->dx = (unsigned long) &frame->uc;
601 
602 	loadsegment(ds, __USER_DS);
603 	loadsegment(es, __USER_DS);
604 
605 	regs->cs = __USER_CS;
606 	regs->ss = __USER_DS;
607 #endif	/* CONFIG_X86_X32_ABI */
608 
609 	return 0;
610 #ifdef CONFIG_X86_X32_ABI
611 Efault:
612 	user_access_end();
613 	return -EFAULT;
614 #endif
615 }
616 
617 /*
618  * Do a signal return; undo the signal stack.
619  */
620 #ifdef CONFIG_X86_32
SYSCALL_DEFINE0(sigreturn)621 SYSCALL_DEFINE0(sigreturn)
622 {
623 	struct pt_regs *regs = current_pt_regs();
624 	struct sigframe __user *frame;
625 	sigset_t set;
626 
627 	frame = (struct sigframe __user *)(regs->sp - 8);
628 
629 	if (!access_ok(frame, sizeof(*frame)))
630 		goto badframe;
631 	if (__get_user(set.sig[0], &frame->sc.oldmask) ||
632 	    __get_user(set.sig[1], &frame->extramask[0]))
633 		goto badframe;
634 
635 	set_current_blocked(&set);
636 
637 	/*
638 	 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
639 	 * Save a few cycles by skipping the __get_user.
640 	 */
641 	if (restore_sigcontext(regs, &frame->sc, 0))
642 		goto badframe;
643 	return regs->ax;
644 
645 badframe:
646 	signal_fault(regs, frame, "sigreturn");
647 
648 	return 0;
649 }
650 #endif /* CONFIG_X86_32 */
651 
SYSCALL_DEFINE0(rt_sigreturn)652 SYSCALL_DEFINE0(rt_sigreturn)
653 {
654 	struct pt_regs *regs = current_pt_regs();
655 	struct rt_sigframe __user *frame;
656 	sigset_t set;
657 	unsigned long uc_flags;
658 
659 	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
660 	if (!access_ok(frame, sizeof(*frame)))
661 		goto badframe;
662 	if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
663 		goto badframe;
664 	if (__get_user(uc_flags, &frame->uc.uc_flags))
665 		goto badframe;
666 
667 	set_current_blocked(&set);
668 
669 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
670 		goto badframe;
671 
672 	if (restore_altstack(&frame->uc.uc_stack))
673 		goto badframe;
674 
675 	return regs->ax;
676 
677 badframe:
678 	signal_fault(regs, frame, "rt_sigreturn");
679 	return 0;
680 }
681 
is_ia32_compat_frame(struct ksignal * ksig)682 static inline int is_ia32_compat_frame(struct ksignal *ksig)
683 {
684 	return IS_ENABLED(CONFIG_IA32_EMULATION) &&
685 		ksig->ka.sa.sa_flags & SA_IA32_ABI;
686 }
687 
is_ia32_frame(struct ksignal * ksig)688 static inline int is_ia32_frame(struct ksignal *ksig)
689 {
690 	return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
691 }
692 
is_x32_frame(struct ksignal * ksig)693 static inline int is_x32_frame(struct ksignal *ksig)
694 {
695 	return IS_ENABLED(CONFIG_X86_X32_ABI) &&
696 		ksig->ka.sa.sa_flags & SA_X32_ABI;
697 }
698 
699 static int
setup_rt_frame(struct ksignal * ksig,struct pt_regs * regs)700 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
701 {
702 	int usig = ksig->sig;
703 	sigset_t *set = sigmask_to_save();
704 	compat_sigset_t *cset = (compat_sigset_t *) set;
705 
706 	/* Perform fixup for the pre-signal frame. */
707 	rseq_signal_deliver(ksig, regs);
708 
709 	/* Set up the stack frame */
710 	if (is_ia32_frame(ksig)) {
711 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
712 			return ia32_setup_rt_frame(usig, ksig, cset, regs);
713 		else
714 			return ia32_setup_frame(usig, ksig, cset, regs);
715 	} else if (is_x32_frame(ksig)) {
716 		return x32_setup_rt_frame(ksig, cset, regs);
717 	} else {
718 		return __setup_rt_frame(ksig->sig, ksig, set, regs);
719 	}
720 }
721 
722 static void
handle_signal(struct ksignal * ksig,struct pt_regs * regs)723 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
724 {
725 	bool stepping, failed;
726 	struct fpu *fpu = &current->thread.fpu;
727 
728 	if (v8086_mode(regs))
729 		save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
730 
731 	/* Are we from a system call? */
732 	if (syscall_get_nr(current, regs) >= 0) {
733 		/* If so, check system call restarting.. */
734 		switch (syscall_get_error(current, regs)) {
735 		case -ERESTART_RESTARTBLOCK:
736 		case -ERESTARTNOHAND:
737 			regs->ax = -EINTR;
738 			break;
739 
740 		case -ERESTARTSYS:
741 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
742 				regs->ax = -EINTR;
743 				break;
744 			}
745 			fallthrough;
746 		case -ERESTARTNOINTR:
747 			regs->ax = regs->orig_ax;
748 			regs->ip -= 2;
749 			break;
750 		}
751 	}
752 
753 	/*
754 	 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
755 	 * so that register information in the sigcontext is correct and
756 	 * then notify the tracer before entering the signal handler.
757 	 */
758 	stepping = test_thread_flag(TIF_SINGLESTEP);
759 	if (stepping)
760 		user_disable_single_step(current);
761 
762 	failed = (setup_rt_frame(ksig, regs) < 0);
763 	if (!failed) {
764 		/*
765 		 * Clear the direction flag as per the ABI for function entry.
766 		 *
767 		 * Clear RF when entering the signal handler, because
768 		 * it might disable possible debug exception from the
769 		 * signal handler.
770 		 *
771 		 * Clear TF for the case when it wasn't set by debugger to
772 		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
773 		 */
774 		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
775 		/*
776 		 * Ensure the signal handler starts with the new fpu state.
777 		 */
778 		fpu__clear_user_states(fpu);
779 	}
780 	signal_setup_done(failed, ksig, stepping);
781 }
782 
get_nr_restart_syscall(const struct pt_regs * regs)783 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
784 {
785 #ifdef CONFIG_IA32_EMULATION
786 	if (current_thread_info()->status & TS_COMPAT_RESTART)
787 		return __NR_ia32_restart_syscall;
788 #endif
789 #ifdef CONFIG_X86_X32_ABI
790 	return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
791 #else
792 	return __NR_restart_syscall;
793 #endif
794 }
795 
796 /*
797  * Note that 'init' is a special process: it doesn't get signals it doesn't
798  * want to handle. Thus you cannot kill init even with a SIGKILL even by
799  * mistake.
800  */
arch_do_signal_or_restart(struct pt_regs * regs,bool has_signal)801 void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal)
802 {
803 	struct ksignal ksig;
804 
805 	if (has_signal && get_signal(&ksig)) {
806 		/* Whee! Actually deliver the signal.  */
807 		handle_signal(&ksig, regs);
808 		return;
809 	}
810 
811 	/* Did we come from a system call? */
812 	if (syscall_get_nr(current, regs) >= 0) {
813 		/* Restart the system call - no handlers present */
814 		switch (syscall_get_error(current, regs)) {
815 		case -ERESTARTNOHAND:
816 		case -ERESTARTSYS:
817 		case -ERESTARTNOINTR:
818 			regs->ax = regs->orig_ax;
819 			regs->ip -= 2;
820 			break;
821 
822 		case -ERESTART_RESTARTBLOCK:
823 			regs->ax = get_nr_restart_syscall(regs);
824 			regs->ip -= 2;
825 			break;
826 		}
827 	}
828 
829 	/*
830 	 * If there's no signal to deliver, we just put the saved sigmask
831 	 * back.
832 	 */
833 	restore_saved_sigmask();
834 }
835 
signal_fault(struct pt_regs * regs,void __user * frame,char * where)836 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
837 {
838 	struct task_struct *me = current;
839 
840 	if (show_unhandled_signals && printk_ratelimit()) {
841 		printk("%s"
842 		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
843 		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
844 		       me->comm, me->pid, where, frame,
845 		       regs->ip, regs->sp, regs->orig_ax);
846 		print_vma_addr(KERN_CONT " in ", regs->ip);
847 		pr_cont("\n");
848 	}
849 
850 	force_sig(SIGSEGV);
851 }
852 
853 #ifdef CONFIG_X86_X32_ABI
COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)854 COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
855 {
856 	struct pt_regs *regs = current_pt_regs();
857 	struct rt_sigframe_x32 __user *frame;
858 	sigset_t set;
859 	unsigned long uc_flags;
860 
861 	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
862 
863 	if (!access_ok(frame, sizeof(*frame)))
864 		goto badframe;
865 	if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
866 		goto badframe;
867 	if (__get_user(uc_flags, &frame->uc.uc_flags))
868 		goto badframe;
869 
870 	set_current_blocked(&set);
871 
872 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
873 		goto badframe;
874 
875 	if (compat_restore_altstack(&frame->uc.uc_stack))
876 		goto badframe;
877 
878 	return regs->ax;
879 
880 badframe:
881 	signal_fault(regs, frame, "x32 rt_sigreturn");
882 	return 0;
883 }
884 #endif
885