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