signal.c 22.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3
/*
 *  Copyright (C) 1991, 1992  Linus Torvalds
4
 *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
L
Linus Torvalds 已提交
5 6 7
 *
 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
 *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
8
 *  2000-2002   x86-64 support by Andi Kleen
L
Linus Torvalds 已提交
9
 */
10 11 12

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

13
#include <linux/sched.h>
14
#include <linux/sched/task_stack.h>
15 16
#include <linux/mm.h>
#include <linux/smp.h>
L
Linus Torvalds 已提交
17 18 19
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/wait.h>
R
Roland McGrath 已提交
20
#include <linux/tracehook.h>
21 22 23 24
#include <linux/unistd.h>
#include <linux/stddef.h>
#include <linux/personality.h>
#include <linux/uaccess.h>
A
Avi Kivity 已提交
25
#include <linux/user-return-notifier.h>
26
#include <linux/uprobes.h>
27
#include <linux/context_tracking.h>
28

L
Linus Torvalds 已提交
29 30
#include <asm/processor.h>
#include <asm/ucontext.h>
31
#include <asm/fpu/internal.h>
32
#include <asm/fpu/signal.h>
R
Roland McGrath 已提交
33
#include <asm/vdso.h>
34
#include <asm/mce.h>
35
#include <asm/sighandling.h>
B
Brian Gerst 已提交
36
#include <asm/vm86.h>
37 38 39 40 41 42

#ifdef CONFIG_X86_64
#include <asm/proto.h>
#include <asm/ia32_unistd.h>
#endif /* CONFIG_X86_64 */

43
#include <asm/syscall.h>
44
#include <asm/syscalls.h>
45

46
#include <asm/sigframe.h>
47
#include <asm/signal.h>
L
Linus Torvalds 已提交
48

T
Tejun Heo 已提交
49 50 51
#define COPY(x)			do {			\
	get_user_ex(regs->x, &sc->x);			\
} while (0)
52

T
Tejun Heo 已提交
53 54 55 56 57
#define GET_SEG(seg)		({			\
	unsigned short tmp;				\
	get_user_ex(tmp, &sc->seg);			\
	tmp;						\
})
58

T
Tejun Heo 已提交
59 60 61
#define COPY_SEG(seg)		do {			\
	regs->seg = GET_SEG(seg);			\
} while (0)
62

T
Tejun Heo 已提交
63 64 65
#define COPY_SEG_CPL3(seg)	do {			\
	regs->seg = GET_SEG(seg) | 3;			\
} while (0)
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#ifdef CONFIG_X86_64
/*
 * If regs->ss will cause an IRET fault, change it.  Otherwise leave it
 * alone.  Using this generally makes no sense unless
 * user_64bit_mode(regs) would return true.
 */
static void force_valid_ss(struct pt_regs *regs)
{
	u32 ar;
	asm volatile ("lar %[old_ss], %[ar]\n\t"
		      "jz 1f\n\t"		/* If invalid: */
		      "xorl %[ar], %[ar]\n\t"	/* set ar = 0 */
		      "1:"
		      : [ar] "=r" (ar)
		      : [old_ss] "rm" ((u16)regs->ss));

	/*
	 * For a valid 64-bit user context, we need DPL 3, type
	 * read-write data or read-write exp-down data, and S and P
	 * set.  We can't use VERW because VERW doesn't check the
	 * P bit.
	 */
	ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
	if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
	    ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
		regs->ss = __USER_DS;
}
#endif

96 97 98
static int restore_sigcontext(struct pt_regs *regs,
			      struct sigcontext __user *sc,
			      unsigned long uc_flags)
99
{
100
	unsigned long buf_val;
101 102 103 104 105
	void __user *buf;
	unsigned int tmpflags;
	unsigned int err = 0;

	/* Always make any pending restarted system calls return -EINTR */
106
	current->restart_block.fn = do_no_restart_syscall;
107

108 109
	get_user_try {

110
#ifdef CONFIG_X86_32
T
Tejun Heo 已提交
111
		set_user_gs(regs, GET_SEG(gs));
112 113 114
		COPY_SEG(fs);
		COPY_SEG(es);
		COPY_SEG(ds);
115 116
#endif /* CONFIG_X86_32 */

117
		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
118
		COPY(dx); COPY(cx); COPY(ip); COPY(ax);
119 120

#ifdef CONFIG_X86_64
121 122 123 124 125 126 127 128
		COPY(r8);
		COPY(r9);
		COPY(r10);
		COPY(r11);
		COPY(r12);
		COPY(r13);
		COPY(r14);
		COPY(r15);
129 130
#endif /* CONFIG_X86_64 */

131 132
		COPY_SEG_CPL3(cs);
		COPY_SEG_CPL3(ss);
133 134 135 136 137 138 139 140 141 142

#ifdef CONFIG_X86_64
		/*
		 * Fix up SS if needed for the benefit of old DOSEMU and
		 * CRIU.
		 */
		if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
			     user_64bit_mode(regs)))
			force_valid_ss(regs);
#endif
143

144 145 146 147
		get_user_ex(tmpflags, &sc->flags);
		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
		regs->orig_ax = -1;		/* disable syscall checks */

148 149
		get_user_ex(buf_val, &sc->fpstate);
		buf = (void __user *)buf_val;
150
	} get_user_catch(err);
151

152
	err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
153

154 155
	force_iret();

156 157 158
	return err;
}

159 160
int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
		     struct pt_regs *regs, unsigned long mask)
161 162 163
{
	int err = 0;

164
	put_user_try {
165

166
#ifdef CONFIG_X86_32
T
Tejun Heo 已提交
167
		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
168 169 170
		put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
		put_user_ex(regs->es, (unsigned int __user *)&sc->es);
		put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
171 172
#endif /* CONFIG_X86_32 */

173 174 175 176 177 178 179 180
		put_user_ex(regs->di, &sc->di);
		put_user_ex(regs->si, &sc->si);
		put_user_ex(regs->bp, &sc->bp);
		put_user_ex(regs->sp, &sc->sp);
		put_user_ex(regs->bx, &sc->bx);
		put_user_ex(regs->dx, &sc->dx);
		put_user_ex(regs->cx, &sc->cx);
		put_user_ex(regs->ax, &sc->ax);
181
#ifdef CONFIG_X86_64
182 183 184 185 186 187 188 189
		put_user_ex(regs->r8, &sc->r8);
		put_user_ex(regs->r9, &sc->r9);
		put_user_ex(regs->r10, &sc->r10);
		put_user_ex(regs->r11, &sc->r11);
		put_user_ex(regs->r12, &sc->r12);
		put_user_ex(regs->r13, &sc->r13);
		put_user_ex(regs->r14, &sc->r14);
		put_user_ex(regs->r15, &sc->r15);
190 191
#endif /* CONFIG_X86_64 */

192
		put_user_ex(current->thread.trap_nr, &sc->trapno);
193 194
		put_user_ex(current->thread.error_code, &sc->err);
		put_user_ex(regs->ip, &sc->ip);
195
#ifdef CONFIG_X86_32
196 197 198 199
		put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
		put_user_ex(regs->flags, &sc->flags);
		put_user_ex(regs->sp, &sc->sp_at_signal);
		put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
200
#else /* !CONFIG_X86_32 */
201 202
		put_user_ex(regs->flags, &sc->flags);
		put_user_ex(regs->cs, &sc->cs);
L
Linus Torvalds 已提交
203 204
		put_user_ex(0, &sc->gs);
		put_user_ex(0, &sc->fs);
205
		put_user_ex(regs->ss, &sc->ss);
206 207
#endif /* CONFIG_X86_32 */

208
		put_user_ex(fpstate, &sc->fpstate);
209

210 211 212 213
		/* non-iBCS2 extensions.. */
		put_user_ex(mask, &sc->oldmask);
		put_user_ex(current->thread.cr2, &sc->cr2);
	} put_user_catch(err);
214 215 216 217

	return err;
}

L
Linus Torvalds 已提交
218
/*
219
 * Set up a signal frame.
L
Linus Torvalds 已提交
220 221 222 223 224
 */

/*
 * Determine which stack to use..
 */
225 226 227 228 229 230 231 232 233 234 235 236 237 238
static unsigned long align_sigframe(unsigned long sp)
{
#ifdef CONFIG_X86_32
	/*
	 * Align the stack pointer according to the i386 ABI,
	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
	 */
	sp = ((sp + 4) & -16ul) - 4;
#else /* !CONFIG_X86_32 */
	sp = round_down(sp, 16) - 8;
#endif
	return sp;
}

239
static void __user *
240
get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
241
	     void __user **fpstate)
L
Linus Torvalds 已提交
242 243
{
	/* Default to using normal stack */
244
	unsigned long math_size = 0;
245
	unsigned long sp = regs->sp;
246
	unsigned long buf_fx = 0;
247
	int onsigstack = on_sig_stack(sp);
248
	struct fpu *fpu = &current->thread.fpu;
249 250

	/* redzone */
251
	if (IS_ENABLED(CONFIG_X86_64))
252
		sp -= 128;
L
Linus Torvalds 已提交
253

254 255 256 257
	/* This is the X/Open sanctioned signal stack switching.  */
	if (ka->sa.sa_flags & SA_ONSTACK) {
		if (sas_ss_flags(sp) == 0)
			sp = current->sas_ss_sp + current->sas_ss_size;
258
	} else if (IS_ENABLED(CONFIG_X86_32) &&
259
		   !onsigstack &&
260
		   regs->ss != __USER_DS &&
261 262 263 264
		   !(ka->sa.sa_flags & SA_RESTORER) &&
		   ka->sa.sa_restorer) {
		/* This is the legacy signal stack switching. */
		sp = (unsigned long) ka->sa.sa_restorer;
L
Linus Torvalds 已提交
265 266
	}

267
	if (fpu->initialized) {
268
		sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
269
					  &buf_fx, &math_size);
270
		*fpstate = (void __user *)sp;
271 272
	}

273 274 275 276 277 278 279 280 281
	sp = align_sigframe(sp - frame_size);

	/*
	 * If we are on the alternate signal stack and would overflow it, don't.
	 * Return an always-bogus address instead so we will die with SIGSEGV.
	 */
	if (onsigstack && !likely(on_sig_stack(sp)))
		return (void __user *)-1L;

282
	/* save i387 and extended state */
283
	if (fpu->initialized &&
284
	    copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
285 286 287
		return (void __user *)-1L;

	return (void __user *)sp;
L
Linus Torvalds 已提交
288 289
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
#ifdef CONFIG_X86_32
static const struct {
	u16 poplmovl;
	u32 val;
	u16 int80;
} __attribute__((packed)) retcode = {
	0xb858,		/* popl %eax; movl $..., %eax */
	__NR_sigreturn,
	0x80cd,		/* int $0x80 */
};

static const struct {
	u8  movl;
	u32 val;
	u16 int80;
	u8  pad;
} __attribute__((packed)) rt_retcode = {
	0xb8,		/* movl $..., %eax */
	__NR_rt_sigreturn,
	0x80cd,		/* int $0x80 */
	0
};

313
static int
A
Al Viro 已提交
314
__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
315
	      struct pt_regs *regs)
L
Linus Torvalds 已提交
316 317
{
	struct sigframe __user *frame;
318
	void __user *restorer;
L
Linus Torvalds 已提交
319
	int err = 0;
320
	void __user *fpstate = NULL;
L
Linus Torvalds 已提交
321

A
Al Viro 已提交
322
	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
L
Linus Torvalds 已提交
323 324

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
325
		return -EFAULT;
L
Linus Torvalds 已提交
326

327
	if (__put_user(sig, &frame->sig))
328
		return -EFAULT;
L
Linus Torvalds 已提交
329

330
	if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
331
		return -EFAULT;
L
Linus Torvalds 已提交
332 333

	if (_NSIG_WORDS > 1) {
334 335
		if (__copy_to_user(&frame->extramask, &set->sig[1],
				   sizeof(frame->extramask)))
336
			return -EFAULT;
L
Linus Torvalds 已提交
337 338
	}

339
	if (current->mm->context.vdso)
340
		restorer = current->mm->context.vdso +
341
			vdso_image_32.sym___kernel_sigreturn;
342
	else
J
Jan Engelhardt 已提交
343
		restorer = &frame->retcode;
A
Al Viro 已提交
344 345
	if (ksig->ka.sa.sa_flags & SA_RESTORER)
		restorer = ksig->ka.sa.sa_restorer;
L
Linus Torvalds 已提交
346 347 348

	/* Set up to return from userspace.  */
	err |= __put_user(restorer, &frame->pretcode);
349

L
Linus Torvalds 已提交
350
	/*
351
	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
L
Linus Torvalds 已提交
352 353 354 355 356
	 *
	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
	 * reasons and because gdb uses it as a signature to notice
	 * signal handler stack frames.
	 */
357
	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
L
Linus Torvalds 已提交
358 359

	if (err)
360
		return -EFAULT;
L
Linus Torvalds 已提交
361 362

	/* Set up registers for signal handler */
363
	regs->sp = (unsigned long)frame;
A
Al Viro 已提交
364
	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
365
	regs->ax = (unsigned long)sig;
366 367
	regs->dx = 0;
	regs->cx = 0;
L
Linus Torvalds 已提交
368

369 370 371 372
	regs->ds = __USER_DS;
	regs->es = __USER_DS;
	regs->ss = __USER_DS;
	regs->cs = __USER_CS;
L
Linus Torvalds 已提交
373

374
	return 0;
L
Linus Torvalds 已提交
375 376
}

A
Al Viro 已提交
377
static int __setup_rt_frame(int sig, struct ksignal *ksig,
378
			    sigset_t *set, struct pt_regs *regs)
L
Linus Torvalds 已提交
379 380
{
	struct rt_sigframe __user *frame;
381
	void __user *restorer;
L
Linus Torvalds 已提交
382
	int err = 0;
383
	void __user *fpstate = NULL;
L
Linus Torvalds 已提交
384

A
Al Viro 已提交
385
	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
L
Linus Torvalds 已提交
386 387

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
388
		return -EFAULT;
L
Linus Torvalds 已提交
389

390 391 392 393 394 395
	put_user_try {
		put_user_ex(sig, &frame->sig);
		put_user_ex(&frame->info, &frame->pinfo);
		put_user_ex(&frame->uc, &frame->puc);

		/* Create the ucontext.  */
396
		if (boot_cpu_has(X86_FEATURE_XSAVE))
397 398 399 400
			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
		else
			put_user_ex(0, &frame->uc.uc_flags);
		put_user_ex(0, &frame->uc.uc_link);
401
		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
402 403

		/* Set up to return from userspace.  */
404
		restorer = current->mm->context.vdso +
405
			vdso_image_32.sym___kernel_rt_sigreturn;
A
Al Viro 已提交
406 407
		if (ksig->ka.sa.sa_flags & SA_RESTORER)
			restorer = ksig->ka.sa.sa_restorer;
408 409 410 411 412 413 414 415 416 417 418
		put_user_ex(restorer, &frame->pretcode);

		/*
		 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
		 *
		 * WE DO NOT USE IT ANY MORE! It's only left here for historical
		 * reasons and because gdb uses it as a signature to notice
		 * signal handler stack frames.
		 */
		put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
	} put_user_catch(err);
419
	
A
Al Viro 已提交
420
	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
421 422 423
	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
				regs, set->sig[0]);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
L
Linus Torvalds 已提交
424 425

	if (err)
426
		return -EFAULT;
L
Linus Torvalds 已提交
427 428

	/* Set up registers for signal handler */
429
	regs->sp = (unsigned long)frame;
A
Al Viro 已提交
430
	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
431
	regs->ax = (unsigned long)sig;
432 433
	regs->dx = (unsigned long)&frame->info;
	regs->cx = (unsigned long)&frame->uc;
L
Linus Torvalds 已提交
434

435 436 437 438
	regs->ds = __USER_DS;
	regs->es = __USER_DS;
	regs->ss = __USER_DS;
	regs->cs = __USER_CS;
L
Linus Torvalds 已提交
439

440
	return 0;
L
Linus Torvalds 已提交
441
}
442
#else /* !CONFIG_X86_32 */
443 444 445 446
static unsigned long frame_uc_flags(struct pt_regs *regs)
{
	unsigned long flags;

447
	if (boot_cpu_has(X86_FEATURE_XSAVE))
448 449 450 451 452 453 454 455 456 457
		flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
	else
		flags = UC_SIGCONTEXT_SS;

	if (likely(user_64bit_mode(regs)))
		flags |= UC_STRICT_RESTORE_SS;

	return flags;
}

A
Al Viro 已提交
458
static int __setup_rt_frame(int sig, struct ksignal *ksig,
459 460 461 462 463 464
			    sigset_t *set, struct pt_regs *regs)
{
	struct rt_sigframe __user *frame;
	void __user *fp = NULL;
	int err = 0;

A
Al Viro 已提交
465
	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
466 467 468 469

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
		return -EFAULT;

A
Al Viro 已提交
470 471
	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
		if (copy_siginfo_to_user(&frame->info, &ksig->info))
472 473 474
			return -EFAULT;
	}

475 476
	put_user_try {
		/* Create the ucontext.  */
477
		put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
478
		put_user_ex(0, &frame->uc.uc_link);
479
		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
480 481 482 483

		/* Set up to return from userspace.  If provided, use a stub
		   already in userspace.  */
		/* x86-64 should always use SA_RESTORER. */
A
Al Viro 已提交
484 485
		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
			put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
486 487 488 489 490
		} else {
			/* could use a vstub here */
			err |= -EFAULT;
		}
	} put_user_catch(err);
491

492 493 494
	err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));

495 496 497 498 499 500 501 502 503 504 505 506
	if (err)
		return -EFAULT;

	/* Set up registers for signal handler */
	regs->di = sig;
	/* In case the signal handler was declared without prototypes */
	regs->ax = 0;

	/* This also works for non SA_SIGINFO handlers because they expect the
	   next argument after the signal number on the stack. */
	regs->si = (unsigned long)&frame->info;
	regs->dx = (unsigned long)&frame->uc;
A
Al Viro 已提交
507
	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
508 509 510

	regs->sp = (unsigned long)frame;

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	/*
	 * Set up the CS and SS registers to run signal handlers in
	 * 64-bit mode, even if the handler happens to be interrupting
	 * 32-bit or 16-bit code.
	 *
	 * SS is subtle.  In 64-bit mode, we don't need any particular
	 * SS descriptor, but we do need SS to be valid.  It's possible
	 * that the old SS is entirely bogus -- this can happen if the
	 * signal we're trying to deliver is #GP or #SS caused by a bad
	 * SS value.  We also have a compatbility issue here: DOSEMU
	 * relies on the contents of the SS register indicating the
	 * SS value at the time of the signal, even though that code in
	 * DOSEMU predates sigreturn's ability to restore SS.  (DOSEMU
	 * avoids relying on sigreturn to restore SS; instead it uses
	 * a trampoline.)  So we do our best: if the old SS was valid,
	 * we keep it.  Otherwise we replace it.
	 */
528 529
	regs->cs = __USER_CS;

530 531 532
	if (unlikely(regs->ss != __USER_DS))
		force_valid_ss(regs);

533 534 535 536
	return 0;
}
#endif /* CONFIG_X86_32 */

A
Al Viro 已提交
537 538
static int x32_setup_rt_frame(struct ksignal *ksig,
			      compat_sigset_t *set,
539 540 541 542 543 544 545 546
			      struct pt_regs *regs)
{
#ifdef CONFIG_X86_X32_ABI
	struct rt_sigframe_x32 __user *frame;
	void __user *restorer;
	int err = 0;
	void __user *fpstate = NULL;

A
Al Viro 已提交
547
	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
548 549 550 551

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
		return -EFAULT;

A
Al Viro 已提交
552
	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
553
		if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
554 555 556 557 558
			return -EFAULT;
	}

	put_user_try {
		/* Create the ucontext.  */
559
		put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
560
		put_user_ex(0, &frame->uc.uc_link);
561
		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
562 563
		put_user_ex(0, &frame->uc.uc__pad0);

A
Al Viro 已提交
564 565
		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
			restorer = ksig->ka.sa.sa_restorer;
566 567 568 569 570 571 572 573
		} else {
			/* could use a vstub here */
			restorer = NULL;
			err |= -EFAULT;
		}
		put_user_ex(restorer, &frame->pretcode);
	} put_user_catch(err);

574 575 576 577
	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
				regs, set->sig[0]);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));

578 579 580 581 582
	if (err)
		return -EFAULT;

	/* Set up registers for signal handler */
	regs->sp = (unsigned long) frame;
A
Al Viro 已提交
583
	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
584 585

	/* We use the x32 calling convention here... */
A
Al Viro 已提交
586
	regs->di = ksig->sig;
587 588 589 590 591 592 593 594 595 596 597 598 599
	regs->si = (unsigned long) &frame->info;
	regs->dx = (unsigned long) &frame->uc;

	loadsegment(ds, __USER_DS);
	loadsegment(es, __USER_DS);

	regs->cs = __USER_CS;
	regs->ss = __USER_DS;
#endif	/* CONFIG_X86_X32_ABI */

	return 0;
}

600 601 602
/*
 * Do a signal return; undo the signal stack.
 */
603
#ifdef CONFIG_X86_32
604
asmlinkage unsigned long sys_sigreturn(void)
605
{
606
	struct pt_regs *regs = current_pt_regs();
607 608 609 610 611 612 613 614 615 616 617 618
	struct sigframe __user *frame;
	sigset_t set;

	frame = (struct sigframe __user *)(regs->sp - 8);

	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
		&& __copy_from_user(&set.sig[1], &frame->extramask,
				    sizeof(frame->extramask))))
		goto badframe;

619
	set_current_blocked(&set);
620

621 622 623 624 625
	/*
	 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
	 * Save a few cycles by skipping the __get_user.
	 */
	if (restore_sigcontext(regs, &frame->sc, 0))
626
		goto badframe;
627
	return regs->ax;
628 629

badframe:
630
	signal_fault(regs, frame, "sigreturn");
631 632 633

	return 0;
}
634
#endif /* CONFIG_X86_32 */
635

636
asmlinkage long sys_rt_sigreturn(void)
637
{
638
	struct pt_regs *regs = current_pt_regs();
639 640
	struct rt_sigframe __user *frame;
	sigset_t set;
641
	unsigned long uc_flags;
642 643 644 645 646 647

	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
		goto badframe;
648 649
	if (__get_user(uc_flags, &frame->uc.uc_flags))
		goto badframe;
650

651
	set_current_blocked(&set);
652

653
	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
654 655
		goto badframe;

656
	if (restore_altstack(&frame->uc.uc_stack))
657 658
		goto badframe;

659
	return regs->ax;
660 661 662 663 664 665

badframe:
	signal_fault(regs, frame, "rt_sigreturn");
	return 0;
}

666
static inline int is_ia32_compat_frame(struct ksignal *ksig)
667
{
668
	return IS_ENABLED(CONFIG_IA32_EMULATION) &&
669
		ksig->ka.sa.sa_flags & SA_IA32_ABI;
670 671
}

672
static inline int is_ia32_frame(struct ksignal *ksig)
673
{
674
	return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
675 676
}

677
static inline int is_x32_frame(struct ksignal *ksig)
678
{
679 680
	return IS_ENABLED(CONFIG_X86_X32_ABI) &&
		ksig->ka.sa.sa_flags & SA_X32_ABI;
681 682
}

683
static int
A
Al Viro 已提交
684
setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
685
{
686
	int usig = ksig->sig;
A
Al Viro 已提交
687
	sigset_t *set = sigmask_to_save();
688
	compat_sigset_t *cset = (compat_sigset_t *) set;
689 690

	/* Set up the stack frame */
691
	if (is_ia32_frame(ksig)) {
A
Al Viro 已提交
692 693
		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
			return ia32_setup_rt_frame(usig, ksig, cset, regs);
694
		else
A
Al Viro 已提交
695
			return ia32_setup_frame(usig, ksig, cset, regs);
696
	} else if (is_x32_frame(ksig)) {
A
Al Viro 已提交
697
		return x32_setup_rt_frame(ksig, cset, regs);
698
	} else {
A
Al Viro 已提交
699
		return __setup_rt_frame(ksig->sig, ksig, set, regs);
700
	}
701 702
}

703
static void
A
Al Viro 已提交
704
handle_signal(struct ksignal *ksig, struct pt_regs *regs)
L
Linus Torvalds 已提交
705
{
706
	bool stepping, failed;
707
	struct fpu *fpu = &current->thread.fpu;
708

709 710 711
	if (v8086_mode(regs))
		save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);

L
Linus Torvalds 已提交
712
	/* Are we from a system call? */
713
	if (syscall_get_nr(current, regs) >= 0) {
L
Linus Torvalds 已提交
714
		/* If so, check system call restarting.. */
715
		switch (syscall_get_error(current, regs)) {
716 717 718 719 720 721
		case -ERESTART_RESTARTBLOCK:
		case -ERESTARTNOHAND:
			regs->ax = -EINTR;
			break;

		case -ERESTARTSYS:
A
Al Viro 已提交
722
			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
723
				regs->ax = -EINTR;
L
Linus Torvalds 已提交
724
				break;
725 726 727 728 729 730
			}
		/* fallthrough */
		case -ERESTARTNOINTR:
			regs->ax = regs->orig_ax;
			regs->ip -= 2;
			break;
L
Linus Torvalds 已提交
731 732 733 734
		}
	}

	/*
735 736 737
	 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
	 * so that register information in the sigcontext is correct and
	 * then notify the tracer before entering the signal handler.
L
Linus Torvalds 已提交
738
	 */
739 740 741
	stepping = test_thread_flag(TIF_SINGLESTEP);
	if (stepping)
		user_disable_single_step(current);
L
Linus Torvalds 已提交
742

A
Al Viro 已提交
743 744 745 746
	failed = (setup_rt_frame(ksig, regs) < 0);
	if (!failed) {
		/*
		 * Clear the direction flag as per the ABI for function entry.
747
		 *
748 749 750
		 * Clear RF when entering the signal handler, because
		 * it might disable possible debug exception from the
		 * signal handler.
751
		 *
752 753
		 * Clear TF for the case when it wasn't set by debugger to
		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
A
Al Viro 已提交
754
		 */
755
		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
756 757 758
		/*
		 * Ensure the signal handler starts with the new fpu state.
		 */
759
		if (fpu->initialized)
760
			fpu__clear(fpu);
761
	}
762
	signal_setup_done(failed, ksig, stepping);
L
Linus Torvalds 已提交
763 764
}

765 766
static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
{
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	/*
	 * This function is fundamentally broken as currently
	 * implemented.
	 *
	 * The idea is that we want to trigger a call to the
	 * restart_block() syscall and that we want in_ia32_syscall(),
	 * in_x32_syscall(), etc. to match whatever they were in the
	 * syscall being restarted.  We assume that the syscall
	 * instruction at (regs->ip - 2) matches whatever syscall
	 * instruction we used to enter in the first place.
	 *
	 * The problem is that we can get here when ptrace pokes
	 * syscall-like values into regs even if we're not in a syscall
	 * at all.
	 *
	 * For now, we maintain historical behavior and guess based on
	 * stored state.  We could do better by saving the actual
	 * syscall arch in restart_block or (with caveats on x32) by
	 * checking if regs->ip points to 'int $0x80'.  The current
	 * behavior is incorrect if a tracer has a different bitness
	 * than the tracee.
	 */
#ifdef CONFIG_IA32_EMULATION
790
	if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
791 792 793 794 795
		return __NR_ia32_restart_syscall;
#endif
#ifdef CONFIG_X86_X32_ABI
	return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
#else
796
	return __NR_restart_syscall;
797
#endif
798
}
799

L
Linus Torvalds 已提交
800 801 802 803 804
/*
 * Note that 'init' is a special process: it doesn't get signals it doesn't
 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 * mistake.
 */
805
void do_signal(struct pt_regs *regs)
L
Linus Torvalds 已提交
806
{
A
Al Viro 已提交
807
	struct ksignal ksig;
L
Linus Torvalds 已提交
808

A
Al Viro 已提交
809
	if (get_signal(&ksig)) {
810
		/* Whee! Actually deliver the signal.  */
A
Al Viro 已提交
811
		handle_signal(&ksig, regs);
812
		return;
L
Linus Torvalds 已提交
813 814 815
	}

	/* Did we come from a system call? */
816
	if (syscall_get_nr(current, regs) >= 0) {
L
Linus Torvalds 已提交
817
		/* Restart the system call - no handlers present */
818
		switch (syscall_get_error(current, regs)) {
819 820 821
		case -ERESTARTNOHAND:
		case -ERESTARTSYS:
		case -ERESTARTNOINTR:
822 823
			regs->ax = regs->orig_ax;
			regs->ip -= 2;
824 825 826
			break;

		case -ERESTART_RESTARTBLOCK:
827
			regs->ax = get_nr_restart_syscall(regs);
828
			regs->ip -= 2;
829
			break;
L
Linus Torvalds 已提交
830 831
		}
	}
832

833 834 835 836
	/*
	 * If there's no signal to deliver, we just put the saved sigmask
	 * back.
	 */
A
Al Viro 已提交
837
	restore_saved_sigmask();
L
Linus Torvalds 已提交
838 839
}

840 841 842 843 844
void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
{
	struct task_struct *me = current;

	if (show_unhandled_signals && printk_ratelimit()) {
845
		printk("%s"
846
		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
847
		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
848 849
		       me->comm, me->pid, where, frame,
		       regs->ip, regs->sp, regs->orig_ax);
850
		print_vma_addr(KERN_CONT " in ", regs->ip);
851
		pr_cont("\n");
852 853 854 855
	}

	force_sig(SIGSEGV, me);
}
856 857

#ifdef CONFIG_X86_X32_ABI
858
asmlinkage long sys32_x32_rt_sigreturn(void)
859
{
860
	struct pt_regs *regs = current_pt_regs();
861 862
	struct rt_sigframe_x32 __user *frame;
	sigset_t set;
863
	unsigned long uc_flags;
864 865 866 867 868 869 870

	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);

	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
		goto badframe;
871 872
	if (__get_user(uc_flags, &frame->uc.uc_flags))
		goto badframe;
873 874 875

	set_current_blocked(&set);

876
	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
877 878
		goto badframe;

A
Al Viro 已提交
879
	if (compat_restore_altstack(&frame->uc.uc_stack))
880 881
		goto badframe;

882
	return regs->ax;
883 884 885 886 887 888

badframe:
	signal_fault(regs, frame, "x32 rt_sigreturn");
	return 0;
}
#endif