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