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