Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  Copyright (C) 1991, 1992  Linus Torvalds
0004  *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
0005  *
0006  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
0007  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
0008  *  2000-2002   x86-64 support by Andi Kleen
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/sched.h>
0014 #include <linux/sched/task_stack.h>
0015 #include <linux/mm.h>
0016 #include <linux/smp.h>
0017 #include <linux/kernel.h>
0018 #include <linux/kstrtox.h>
0019 #include <linux/errno.h>
0020 #include <linux/wait.h>
0021 #include <linux/unistd.h>
0022 #include <linux/stddef.h>
0023 #include <linux/personality.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/user-return-notifier.h>
0026 #include <linux/uprobes.h>
0027 #include <linux/context_tracking.h>
0028 #include <linux/entry-common.h>
0029 #include <linux/syscalls.h>
0030 
0031 #include <asm/processor.h>
0032 #include <asm/ucontext.h>
0033 #include <asm/fpu/signal.h>
0034 #include <asm/fpu/xstate.h>
0035 #include <asm/vdso.h>
0036 #include <asm/mce.h>
0037 #include <asm/sighandling.h>
0038 #include <asm/vm86.h>
0039 
0040 #ifdef CONFIG_X86_64
0041 #include <linux/compat.h>
0042 #include <asm/proto.h>
0043 #include <asm/ia32_unistd.h>
0044 #include <asm/fpu/xstate.h>
0045 #endif /* CONFIG_X86_64 */
0046 
0047 #include <asm/syscall.h>
0048 #include <asm/sigframe.h>
0049 #include <asm/signal.h>
0050 
0051 #ifdef CONFIG_X86_64
0052 /*
0053  * If regs->ss will cause an IRET fault, change it.  Otherwise leave it
0054  * alone.  Using this generally makes no sense unless
0055  * user_64bit_mode(regs) would return true.
0056  */
0057 static void force_valid_ss(struct pt_regs *regs)
0058 {
0059     u32 ar;
0060     asm volatile ("lar %[old_ss], %[ar]\n\t"
0061               "jz 1f\n\t"       /* If invalid: */
0062               "xorl %[ar], %[ar]\n\t"   /* set ar = 0 */
0063               "1:"
0064               : [ar] "=r" (ar)
0065               : [old_ss] "rm" ((u16)regs->ss));
0066 
0067     /*
0068      * For a valid 64-bit user context, we need DPL 3, type
0069      * read-write data or read-write exp-down data, and S and P
0070      * set.  We can't use VERW because VERW doesn't check the
0071      * P bit.
0072      */
0073     ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
0074     if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
0075         ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
0076         regs->ss = __USER_DS;
0077 }
0078 # define CONTEXT_COPY_SIZE  offsetof(struct sigcontext, reserved1)
0079 #else
0080 # define CONTEXT_COPY_SIZE  sizeof(struct sigcontext)
0081 #endif
0082 
0083 static bool restore_sigcontext(struct pt_regs *regs,
0084                    struct sigcontext __user *usc,
0085                    unsigned long uc_flags)
0086 {
0087     struct sigcontext sc;
0088 
0089     /* Always make any pending restarted system calls return -EINTR */
0090     current->restart_block.fn = do_no_restart_syscall;
0091 
0092     if (copy_from_user(&sc, usc, CONTEXT_COPY_SIZE))
0093         return false;
0094 
0095 #ifdef CONFIG_X86_32
0096     loadsegment(gs, sc.gs);
0097     regs->fs = sc.fs;
0098     regs->es = sc.es;
0099     regs->ds = sc.ds;
0100 #endif /* CONFIG_X86_32 */
0101 
0102     regs->bx = sc.bx;
0103     regs->cx = sc.cx;
0104     regs->dx = sc.dx;
0105     regs->si = sc.si;
0106     regs->di = sc.di;
0107     regs->bp = sc.bp;
0108     regs->ax = sc.ax;
0109     regs->sp = sc.sp;
0110     regs->ip = sc.ip;
0111 
0112 #ifdef CONFIG_X86_64
0113     regs->r8 = sc.r8;
0114     regs->r9 = sc.r9;
0115     regs->r10 = sc.r10;
0116     regs->r11 = sc.r11;
0117     regs->r12 = sc.r12;
0118     regs->r13 = sc.r13;
0119     regs->r14 = sc.r14;
0120     regs->r15 = sc.r15;
0121 #endif /* CONFIG_X86_64 */
0122 
0123     /* Get CS/SS and force CPL3 */
0124     regs->cs = sc.cs | 0x03;
0125     regs->ss = sc.ss | 0x03;
0126 
0127     regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
0128     /* disable syscall checks */
0129     regs->orig_ax = -1;
0130 
0131 #ifdef CONFIG_X86_64
0132     /*
0133      * Fix up SS if needed for the benefit of old DOSEMU and
0134      * CRIU.
0135      */
0136     if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
0137         force_valid_ss(regs);
0138 #endif
0139 
0140     return fpu__restore_sig((void __user *)sc.fpstate,
0141                    IS_ENABLED(CONFIG_X86_32));
0142 }
0143 
0144 static __always_inline int
0145 __unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
0146              struct pt_regs *regs, unsigned long mask)
0147 {
0148 #ifdef CONFIG_X86_32
0149     unsigned int gs;
0150     savesegment(gs, gs);
0151 
0152     unsafe_put_user(gs,   (unsigned int __user *)&sc->gs, Efault);
0153     unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
0154     unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
0155     unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
0156 #endif /* CONFIG_X86_32 */
0157 
0158     unsafe_put_user(regs->di, &sc->di, Efault);
0159     unsafe_put_user(regs->si, &sc->si, Efault);
0160     unsafe_put_user(regs->bp, &sc->bp, Efault);
0161     unsafe_put_user(regs->sp, &sc->sp, Efault);
0162     unsafe_put_user(regs->bx, &sc->bx, Efault);
0163     unsafe_put_user(regs->dx, &sc->dx, Efault);
0164     unsafe_put_user(regs->cx, &sc->cx, Efault);
0165     unsafe_put_user(regs->ax, &sc->ax, Efault);
0166 #ifdef CONFIG_X86_64
0167     unsafe_put_user(regs->r8, &sc->r8, Efault);
0168     unsafe_put_user(regs->r9, &sc->r9, Efault);
0169     unsafe_put_user(regs->r10, &sc->r10, Efault);
0170     unsafe_put_user(regs->r11, &sc->r11, Efault);
0171     unsafe_put_user(regs->r12, &sc->r12, Efault);
0172     unsafe_put_user(regs->r13, &sc->r13, Efault);
0173     unsafe_put_user(regs->r14, &sc->r14, Efault);
0174     unsafe_put_user(regs->r15, &sc->r15, Efault);
0175 #endif /* CONFIG_X86_64 */
0176 
0177     unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
0178     unsafe_put_user(current->thread.error_code, &sc->err, Efault);
0179     unsafe_put_user(regs->ip, &sc->ip, Efault);
0180 #ifdef CONFIG_X86_32
0181     unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
0182     unsafe_put_user(regs->flags, &sc->flags, Efault);
0183     unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
0184     unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
0185 #else /* !CONFIG_X86_32 */
0186     unsafe_put_user(regs->flags, &sc->flags, Efault);
0187     unsafe_put_user(regs->cs, &sc->cs, Efault);
0188     unsafe_put_user(0, &sc->gs, Efault);
0189     unsafe_put_user(0, &sc->fs, Efault);
0190     unsafe_put_user(regs->ss, &sc->ss, Efault);
0191 #endif /* CONFIG_X86_32 */
0192 
0193     unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
0194 
0195     /* non-iBCS2 extensions.. */
0196     unsafe_put_user(mask, &sc->oldmask, Efault);
0197     unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
0198     return 0;
0199 Efault:
0200     return -EFAULT;
0201 }
0202 
0203 #define unsafe_put_sigcontext(sc, fp, regs, set, label)         \
0204 do {                                    \
0205     if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0]))   \
0206         goto label;                     \
0207 } while(0);
0208 
0209 #define unsafe_put_sigmask(set, frame, label) \
0210     unsafe_put_user(*(__u64 *)(set), \
0211             (__u64 __user *)&(frame)->uc.uc_sigmask, \
0212             label)
0213 
0214 /*
0215  * Set up a signal frame.
0216  */
0217 
0218 /* x86 ABI requires 16-byte alignment */
0219 #define FRAME_ALIGNMENT 16UL
0220 
0221 #define MAX_FRAME_PADDING   (FRAME_ALIGNMENT - 1)
0222 
0223 /*
0224  * Determine which stack to use..
0225  */
0226 static unsigned long align_sigframe(unsigned long sp)
0227 {
0228 #ifdef CONFIG_X86_32
0229     /*
0230      * Align the stack pointer according to the i386 ABI,
0231      * i.e. so that on function entry ((sp + 4) & 15) == 0.
0232      */
0233     sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
0234 #else /* !CONFIG_X86_32 */
0235     sp = round_down(sp, FRAME_ALIGNMENT) - 8;
0236 #endif
0237     return sp;
0238 }
0239 
0240 static void __user *
0241 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
0242          void __user **fpstate)
0243 {
0244     /* Default to using normal stack */
0245     bool nested_altstack = on_sig_stack(regs->sp);
0246     bool entering_altstack = false;
0247     unsigned long math_size = 0;
0248     unsigned long sp = regs->sp;
0249     unsigned long buf_fx = 0;
0250 
0251     /* redzone */
0252     if (IS_ENABLED(CONFIG_X86_64))
0253         sp -= 128;
0254 
0255     /* This is the X/Open sanctioned signal stack switching.  */
0256     if (ka->sa.sa_flags & SA_ONSTACK) {
0257         /*
0258          * This checks nested_altstack via sas_ss_flags(). Sensible
0259          * programs use SS_AUTODISARM, which disables that check, and
0260          * programs that don't use SS_AUTODISARM get compatible.
0261          */
0262         if (sas_ss_flags(sp) == 0) {
0263             sp = current->sas_ss_sp + current->sas_ss_size;
0264             entering_altstack = true;
0265         }
0266     } else if (IS_ENABLED(CONFIG_X86_32) &&
0267            !nested_altstack &&
0268            regs->ss != __USER_DS &&
0269            !(ka->sa.sa_flags & SA_RESTORER) &&
0270            ka->sa.sa_restorer) {
0271         /* This is the legacy signal stack switching. */
0272         sp = (unsigned long) ka->sa.sa_restorer;
0273         entering_altstack = true;
0274     }
0275 
0276     sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
0277                   &buf_fx, &math_size);
0278     *fpstate = (void __user *)sp;
0279 
0280     sp = align_sigframe(sp - frame_size);
0281 
0282     /*
0283      * If we are on the alternate signal stack and would overflow it, don't.
0284      * Return an always-bogus address instead so we will die with SIGSEGV.
0285      */
0286     if (unlikely((nested_altstack || entering_altstack) &&
0287              !__on_sig_stack(sp))) {
0288 
0289         if (show_unhandled_signals && printk_ratelimit())
0290             pr_info("%s[%d] overflowed sigaltstack\n",
0291                 current->comm, task_pid_nr(current));
0292 
0293         return (void __user *)-1L;
0294     }
0295 
0296     /* save i387 and extended state */
0297     if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size))
0298         return (void __user *)-1L;
0299 
0300     return (void __user *)sp;
0301 }
0302 
0303 #ifdef CONFIG_X86_32
0304 static const struct {
0305     u16 poplmovl;
0306     u32 val;
0307     u16 int80;
0308 } __attribute__((packed)) retcode = {
0309     0xb858,     /* popl %eax; movl $..., %eax */
0310     __NR_sigreturn,
0311     0x80cd,     /* int $0x80 */
0312 };
0313 
0314 static const struct {
0315     u8  movl;
0316     u32 val;
0317     u16 int80;
0318     u8  pad;
0319 } __attribute__((packed)) rt_retcode = {
0320     0xb8,       /* movl $..., %eax */
0321     __NR_rt_sigreturn,
0322     0x80cd,     /* int $0x80 */
0323     0
0324 };
0325 
0326 static int
0327 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
0328           struct pt_regs *regs)
0329 {
0330     struct sigframe __user *frame;
0331     void __user *restorer;
0332     void __user *fp = NULL;
0333 
0334     frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
0335 
0336     if (!user_access_begin(frame, sizeof(*frame)))
0337         return -EFAULT;
0338 
0339     unsafe_put_user(sig, &frame->sig, Efault);
0340     unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
0341     unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
0342     if (current->mm->context.vdso)
0343         restorer = current->mm->context.vdso +
0344             vdso_image_32.sym___kernel_sigreturn;
0345     else
0346         restorer = &frame->retcode;
0347     if (ksig->ka.sa.sa_flags & SA_RESTORER)
0348         restorer = ksig->ka.sa.sa_restorer;
0349 
0350     /* Set up to return from userspace.  */
0351     unsafe_put_user(restorer, &frame->pretcode, Efault);
0352 
0353     /*
0354      * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
0355      *
0356      * WE DO NOT USE IT ANY MORE! It's only left here for historical
0357      * reasons and because gdb uses it as a signature to notice
0358      * signal handler stack frames.
0359      */
0360     unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
0361     user_access_end();
0362 
0363     /* Set up registers for signal handler */
0364     regs->sp = (unsigned long)frame;
0365     regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
0366     regs->ax = (unsigned long)sig;
0367     regs->dx = 0;
0368     regs->cx = 0;
0369 
0370     regs->ds = __USER_DS;
0371     regs->es = __USER_DS;
0372     regs->ss = __USER_DS;
0373     regs->cs = __USER_CS;
0374 
0375     return 0;
0376 
0377 Efault:
0378     user_access_end();
0379     return -EFAULT;
0380 }
0381 
0382 static int __setup_rt_frame(int sig, struct ksignal *ksig,
0383                 sigset_t *set, struct pt_regs *regs)
0384 {
0385     struct rt_sigframe __user *frame;
0386     void __user *restorer;
0387     void __user *fp = NULL;
0388 
0389     frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
0390 
0391     if (!user_access_begin(frame, sizeof(*frame)))
0392         return -EFAULT;
0393 
0394     unsafe_put_user(sig, &frame->sig, Efault);
0395     unsafe_put_user(&frame->info, &frame->pinfo, Efault);
0396     unsafe_put_user(&frame->uc, &frame->puc, Efault);
0397 
0398     /* Create the ucontext.  */
0399     if (static_cpu_has(X86_FEATURE_XSAVE))
0400         unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
0401     else
0402         unsafe_put_user(0, &frame->uc.uc_flags, Efault);
0403     unsafe_put_user(0, &frame->uc.uc_link, Efault);
0404     unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
0405 
0406     /* Set up to return from userspace.  */
0407     restorer = current->mm->context.vdso +
0408         vdso_image_32.sym___kernel_rt_sigreturn;
0409     if (ksig->ka.sa.sa_flags & SA_RESTORER)
0410         restorer = ksig->ka.sa.sa_restorer;
0411     unsafe_put_user(restorer, &frame->pretcode, Efault);
0412 
0413     /*
0414      * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
0415      *
0416      * WE DO NOT USE IT ANY MORE! It's only left here for historical
0417      * reasons and because gdb uses it as a signature to notice
0418      * signal handler stack frames.
0419      */
0420     unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
0421     unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
0422     unsafe_put_sigmask(set, frame, Efault);
0423     user_access_end();
0424     
0425     if (copy_siginfo_to_user(&frame->info, &ksig->info))
0426         return -EFAULT;
0427 
0428     /* Set up registers for signal handler */
0429     regs->sp = (unsigned long)frame;
0430     regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
0431     regs->ax = (unsigned long)sig;
0432     regs->dx = (unsigned long)&frame->info;
0433     regs->cx = (unsigned long)&frame->uc;
0434 
0435     regs->ds = __USER_DS;
0436     regs->es = __USER_DS;
0437     regs->ss = __USER_DS;
0438     regs->cs = __USER_CS;
0439 
0440     return 0;
0441 Efault:
0442     user_access_end();
0443     return -EFAULT;
0444 }
0445 #else /* !CONFIG_X86_32 */
0446 static unsigned long frame_uc_flags(struct pt_regs *regs)
0447 {
0448     unsigned long flags;
0449 
0450     if (boot_cpu_has(X86_FEATURE_XSAVE))
0451         flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
0452     else
0453         flags = UC_SIGCONTEXT_SS;
0454 
0455     if (likely(user_64bit_mode(regs)))
0456         flags |= UC_STRICT_RESTORE_SS;
0457 
0458     return flags;
0459 }
0460 
0461 static int __setup_rt_frame(int sig, struct ksignal *ksig,
0462                 sigset_t *set, struct pt_regs *regs)
0463 {
0464     struct rt_sigframe __user *frame;
0465     void __user *fp = NULL;
0466     unsigned long uc_flags;
0467 
0468     /* x86-64 should always use SA_RESTORER. */
0469     if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
0470         return -EFAULT;
0471 
0472     frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
0473     uc_flags = frame_uc_flags(regs);
0474 
0475     if (!user_access_begin(frame, sizeof(*frame)))
0476         return -EFAULT;
0477 
0478     /* Create the ucontext.  */
0479     unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
0480     unsafe_put_user(0, &frame->uc.uc_link, Efault);
0481     unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
0482 
0483     /* Set up to return from userspace.  If provided, use a stub
0484        already in userspace.  */
0485     unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
0486     unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
0487     unsafe_put_sigmask(set, frame, Efault);
0488     user_access_end();
0489 
0490     if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
0491         if (copy_siginfo_to_user(&frame->info, &ksig->info))
0492             return -EFAULT;
0493     }
0494 
0495     /* Set up registers for signal handler */
0496     regs->di = sig;
0497     /* In case the signal handler was declared without prototypes */
0498     regs->ax = 0;
0499 
0500     /* This also works for non SA_SIGINFO handlers because they expect the
0501        next argument after the signal number on the stack. */
0502     regs->si = (unsigned long)&frame->info;
0503     regs->dx = (unsigned long)&frame->uc;
0504     regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
0505 
0506     regs->sp = (unsigned long)frame;
0507 
0508     /*
0509      * Set up the CS and SS registers to run signal handlers in
0510      * 64-bit mode, even if the handler happens to be interrupting
0511      * 32-bit or 16-bit code.
0512      *
0513      * SS is subtle.  In 64-bit mode, we don't need any particular
0514      * SS descriptor, but we do need SS to be valid.  It's possible
0515      * that the old SS is entirely bogus -- this can happen if the
0516      * signal we're trying to deliver is #GP or #SS caused by a bad
0517      * SS value.  We also have a compatibility issue here: DOSEMU
0518      * relies on the contents of the SS register indicating the
0519      * SS value at the time of the signal, even though that code in
0520      * DOSEMU predates sigreturn's ability to restore SS.  (DOSEMU
0521      * avoids relying on sigreturn to restore SS; instead it uses
0522      * a trampoline.)  So we do our best: if the old SS was valid,
0523      * we keep it.  Otherwise we replace it.
0524      */
0525     regs->cs = __USER_CS;
0526 
0527     if (unlikely(regs->ss != __USER_DS))
0528         force_valid_ss(regs);
0529 
0530     return 0;
0531 
0532 Efault:
0533     user_access_end();
0534     return -EFAULT;
0535 }
0536 #endif /* CONFIG_X86_32 */
0537 
0538 #ifdef CONFIG_X86_X32_ABI
0539 static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
0540         const struct kernel_siginfo *from)
0541 {
0542     struct compat_siginfo new;
0543 
0544     copy_siginfo_to_external32(&new, from);
0545     if (from->si_signo == SIGCHLD) {
0546         new._sifields._sigchld_x32._utime = from->si_utime;
0547         new._sifields._sigchld_x32._stime = from->si_stime;
0548     }
0549     if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
0550         return -EFAULT;
0551     return 0;
0552 }
0553 
0554 int copy_siginfo_to_user32(struct compat_siginfo __user *to,
0555                const struct kernel_siginfo *from)
0556 {
0557     if (in_x32_syscall())
0558         return x32_copy_siginfo_to_user(to, from);
0559     return __copy_siginfo_to_user32(to, from);
0560 }
0561 #endif /* CONFIG_X86_X32_ABI */
0562 
0563 static int x32_setup_rt_frame(struct ksignal *ksig,
0564                   compat_sigset_t *set,
0565                   struct pt_regs *regs)
0566 {
0567 #ifdef CONFIG_X86_X32_ABI
0568     struct rt_sigframe_x32 __user *frame;
0569     unsigned long uc_flags;
0570     void __user *restorer;
0571     void __user *fp = NULL;
0572 
0573     if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
0574         return -EFAULT;
0575 
0576     frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
0577 
0578     uc_flags = frame_uc_flags(regs);
0579 
0580     if (!user_access_begin(frame, sizeof(*frame)))
0581         return -EFAULT;
0582 
0583     /* Create the ucontext.  */
0584     unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
0585     unsafe_put_user(0, &frame->uc.uc_link, Efault);
0586     unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
0587     unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
0588     restorer = ksig->ka.sa.sa_restorer;
0589     unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
0590     unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
0591     unsafe_put_sigmask(set, frame, Efault);
0592     user_access_end();
0593 
0594     if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
0595         if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
0596             return -EFAULT;
0597     }
0598 
0599     /* Set up registers for signal handler */
0600     regs->sp = (unsigned long) frame;
0601     regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
0602 
0603     /* We use the x32 calling convention here... */
0604     regs->di = ksig->sig;
0605     regs->si = (unsigned long) &frame->info;
0606     regs->dx = (unsigned long) &frame->uc;
0607 
0608     loadsegment(ds, __USER_DS);
0609     loadsegment(es, __USER_DS);
0610 
0611     regs->cs = __USER_CS;
0612     regs->ss = __USER_DS;
0613 #endif  /* CONFIG_X86_X32_ABI */
0614 
0615     return 0;
0616 #ifdef CONFIG_X86_X32_ABI
0617 Efault:
0618     user_access_end();
0619     return -EFAULT;
0620 #endif
0621 }
0622 
0623 /*
0624  * Do a signal return; undo the signal stack.
0625  */
0626 #ifdef CONFIG_X86_32
0627 SYSCALL_DEFINE0(sigreturn)
0628 {
0629     struct pt_regs *regs = current_pt_regs();
0630     struct sigframe __user *frame;
0631     sigset_t set;
0632 
0633     frame = (struct sigframe __user *)(regs->sp - 8);
0634 
0635     if (!access_ok(frame, sizeof(*frame)))
0636         goto badframe;
0637     if (__get_user(set.sig[0], &frame->sc.oldmask) ||
0638         __get_user(set.sig[1], &frame->extramask[0]))
0639         goto badframe;
0640 
0641     set_current_blocked(&set);
0642 
0643     /*
0644      * x86_32 has no uc_flags bits relevant to restore_sigcontext.
0645      * Save a few cycles by skipping the __get_user.
0646      */
0647     if (!restore_sigcontext(regs, &frame->sc, 0))
0648         goto badframe;
0649     return regs->ax;
0650 
0651 badframe:
0652     signal_fault(regs, frame, "sigreturn");
0653 
0654     return 0;
0655 }
0656 #endif /* CONFIG_X86_32 */
0657 
0658 SYSCALL_DEFINE0(rt_sigreturn)
0659 {
0660     struct pt_regs *regs = current_pt_regs();
0661     struct rt_sigframe __user *frame;
0662     sigset_t set;
0663     unsigned long uc_flags;
0664 
0665     frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
0666     if (!access_ok(frame, sizeof(*frame)))
0667         goto badframe;
0668     if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
0669         goto badframe;
0670     if (__get_user(uc_flags, &frame->uc.uc_flags))
0671         goto badframe;
0672 
0673     set_current_blocked(&set);
0674 
0675     if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
0676         goto badframe;
0677 
0678     if (restore_altstack(&frame->uc.uc_stack))
0679         goto badframe;
0680 
0681     return regs->ax;
0682 
0683 badframe:
0684     signal_fault(regs, frame, "rt_sigreturn");
0685     return 0;
0686 }
0687 
0688 /*
0689  * There are four different struct types for signal frame: sigframe_ia32,
0690  * rt_sigframe_ia32, rt_sigframe_x32, and rt_sigframe. Use the worst case
0691  * -- the largest size. It means the size for 64-bit apps is a bit more
0692  * than needed, but this keeps the code simple.
0693  */
0694 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
0695 # define MAX_FRAME_SIGINFO_UCTXT_SIZE   sizeof(struct sigframe_ia32)
0696 #else
0697 # define MAX_FRAME_SIGINFO_UCTXT_SIZE   sizeof(struct rt_sigframe)
0698 #endif
0699 
0700 /*
0701  * The FP state frame contains an XSAVE buffer which must be 64-byte aligned.
0702  * If a signal frame starts at an unaligned address, extra space is required.
0703  * This is the max alignment padding, conservatively.
0704  */
0705 #define MAX_XSAVE_PADDING   63UL
0706 
0707 /*
0708  * The frame data is composed of the following areas and laid out as:
0709  *
0710  * -------------------------
0711  * | alignment padding     |
0712  * -------------------------
0713  * | (f)xsave frame        |
0714  * -------------------------
0715  * | fsave header          |
0716  * -------------------------
0717  * | alignment padding     |
0718  * -------------------------
0719  * | siginfo + ucontext    |
0720  * -------------------------
0721  */
0722 
0723 /* max_frame_size tells userspace the worst case signal stack size. */
0724 static unsigned long __ro_after_init max_frame_size;
0725 static unsigned int __ro_after_init fpu_default_state_size;
0726 
0727 void __init init_sigframe_size(void)
0728 {
0729     fpu_default_state_size = fpu__get_fpstate_size();
0730 
0731     max_frame_size = MAX_FRAME_SIGINFO_UCTXT_SIZE + MAX_FRAME_PADDING;
0732 
0733     max_frame_size += fpu_default_state_size + MAX_XSAVE_PADDING;
0734 
0735     /* Userspace expects an aligned size. */
0736     max_frame_size = round_up(max_frame_size, FRAME_ALIGNMENT);
0737 
0738     pr_info("max sigframe size: %lu\n", max_frame_size);
0739 }
0740 
0741 unsigned long get_sigframe_size(void)
0742 {
0743     return max_frame_size;
0744 }
0745 
0746 static inline int is_ia32_compat_frame(struct ksignal *ksig)
0747 {
0748     return IS_ENABLED(CONFIG_IA32_EMULATION) &&
0749         ksig->ka.sa.sa_flags & SA_IA32_ABI;
0750 }
0751 
0752 static inline int is_ia32_frame(struct ksignal *ksig)
0753 {
0754     return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
0755 }
0756 
0757 static inline int is_x32_frame(struct ksignal *ksig)
0758 {
0759     return IS_ENABLED(CONFIG_X86_X32_ABI) &&
0760         ksig->ka.sa.sa_flags & SA_X32_ABI;
0761 }
0762 
0763 static int
0764 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
0765 {
0766     int usig = ksig->sig;
0767     sigset_t *set = sigmask_to_save();
0768     compat_sigset_t *cset = (compat_sigset_t *) set;
0769 
0770     /* Perform fixup for the pre-signal frame. */
0771     rseq_signal_deliver(ksig, regs);
0772 
0773     /* Set up the stack frame */
0774     if (is_ia32_frame(ksig)) {
0775         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
0776             return ia32_setup_rt_frame(usig, ksig, cset, regs);
0777         else
0778             return ia32_setup_frame(usig, ksig, cset, regs);
0779     } else if (is_x32_frame(ksig)) {
0780         return x32_setup_rt_frame(ksig, cset, regs);
0781     } else {
0782         return __setup_rt_frame(ksig->sig, ksig, set, regs);
0783     }
0784 }
0785 
0786 static void
0787 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
0788 {
0789     bool stepping, failed;
0790     struct fpu *fpu = &current->thread.fpu;
0791 
0792     if (v8086_mode(regs))
0793         save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
0794 
0795     /* Are we from a system call? */
0796     if (syscall_get_nr(current, regs) != -1) {
0797         /* If so, check system call restarting.. */
0798         switch (syscall_get_error(current, regs)) {
0799         case -ERESTART_RESTARTBLOCK:
0800         case -ERESTARTNOHAND:
0801             regs->ax = -EINTR;
0802             break;
0803 
0804         case -ERESTARTSYS:
0805             if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
0806                 regs->ax = -EINTR;
0807                 break;
0808             }
0809             fallthrough;
0810         case -ERESTARTNOINTR:
0811             regs->ax = regs->orig_ax;
0812             regs->ip -= 2;
0813             break;
0814         }
0815     }
0816 
0817     /*
0818      * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
0819      * so that register information in the sigcontext is correct and
0820      * then notify the tracer before entering the signal handler.
0821      */
0822     stepping = test_thread_flag(TIF_SINGLESTEP);
0823     if (stepping)
0824         user_disable_single_step(current);
0825 
0826     failed = (setup_rt_frame(ksig, regs) < 0);
0827     if (!failed) {
0828         /*
0829          * Clear the direction flag as per the ABI for function entry.
0830          *
0831          * Clear RF when entering the signal handler, because
0832          * it might disable possible debug exception from the
0833          * signal handler.
0834          *
0835          * Clear TF for the case when it wasn't set by debugger to
0836          * avoid the recursive send_sigtrap() in SIGTRAP handler.
0837          */
0838         regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
0839         /*
0840          * Ensure the signal handler starts with the new fpu state.
0841          */
0842         fpu__clear_user_states(fpu);
0843     }
0844     signal_setup_done(failed, ksig, stepping);
0845 }
0846 
0847 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
0848 {
0849 #ifdef CONFIG_IA32_EMULATION
0850     if (current->restart_block.arch_data & TS_COMPAT)
0851         return __NR_ia32_restart_syscall;
0852 #endif
0853 #ifdef CONFIG_X86_X32_ABI
0854     return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
0855 #else
0856     return __NR_restart_syscall;
0857 #endif
0858 }
0859 
0860 /*
0861  * Note that 'init' is a special process: it doesn't get signals it doesn't
0862  * want to handle. Thus you cannot kill init even with a SIGKILL even by
0863  * mistake.
0864  */
0865 void arch_do_signal_or_restart(struct pt_regs *regs)
0866 {
0867     struct ksignal ksig;
0868 
0869     if (get_signal(&ksig)) {
0870         /* Whee! Actually deliver the signal.  */
0871         handle_signal(&ksig, regs);
0872         return;
0873     }
0874 
0875     /* Did we come from a system call? */
0876     if (syscall_get_nr(current, regs) != -1) {
0877         /* Restart the system call - no handlers present */
0878         switch (syscall_get_error(current, regs)) {
0879         case -ERESTARTNOHAND:
0880         case -ERESTARTSYS:
0881         case -ERESTARTNOINTR:
0882             regs->ax = regs->orig_ax;
0883             regs->ip -= 2;
0884             break;
0885 
0886         case -ERESTART_RESTARTBLOCK:
0887             regs->ax = get_nr_restart_syscall(regs);
0888             regs->ip -= 2;
0889             break;
0890         }
0891     }
0892 
0893     /*
0894      * If there's no signal to deliver, we just put the saved sigmask
0895      * back.
0896      */
0897     restore_saved_sigmask();
0898 }
0899 
0900 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
0901 {
0902     struct task_struct *me = current;
0903 
0904     if (show_unhandled_signals && printk_ratelimit()) {
0905         printk("%s"
0906                "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
0907                task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
0908                me->comm, me->pid, where, frame,
0909                regs->ip, regs->sp, regs->orig_ax);
0910         print_vma_addr(KERN_CONT " in ", regs->ip);
0911         pr_cont("\n");
0912     }
0913 
0914     force_sig(SIGSEGV);
0915 }
0916 
0917 #ifdef CONFIG_DYNAMIC_SIGFRAME
0918 #ifdef CONFIG_STRICT_SIGALTSTACK_SIZE
0919 static bool strict_sigaltstack_size __ro_after_init = true;
0920 #else
0921 static bool strict_sigaltstack_size __ro_after_init = false;
0922 #endif
0923 
0924 static int __init strict_sas_size(char *arg)
0925 {
0926     return kstrtobool(arg, &strict_sigaltstack_size);
0927 }
0928 __setup("strict_sas_size", strict_sas_size);
0929 
0930 /*
0931  * MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512
0932  * exceeds that size already. As such programs might never use the
0933  * sigaltstack they just continued to work. While always checking against
0934  * the real size would be correct, this might be considered a regression.
0935  *
0936  * Therefore avoid the sanity check, unless enforced by kernel
0937  * configuration or command line option.
0938  *
0939  * When dynamic FPU features are supported, the check is also enforced when
0940  * the task has permissions to use dynamic features. Tasks which have no
0941  * permission are checked against the size of the non-dynamic feature set
0942  * if strict checking is enabled. This avoids forcing all tasks on the
0943  * system to allocate large sigaltstacks even if they are never going
0944  * to use a dynamic feature. As this is serialized via sighand::siglock
0945  * any permission request for a dynamic feature either happened already
0946  * or will see the newly install sigaltstack size in the permission checks.
0947  */
0948 bool sigaltstack_size_valid(size_t ss_size)
0949 {
0950     unsigned long fsize = max_frame_size - fpu_default_state_size;
0951     u64 mask;
0952 
0953     lockdep_assert_held(&current->sighand->siglock);
0954 
0955     if (!fpu_state_size_dynamic() && !strict_sigaltstack_size)
0956         return true;
0957 
0958     fsize += current->group_leader->thread.fpu.perm.__user_state_size;
0959     if (likely(ss_size > fsize))
0960         return true;
0961 
0962     if (strict_sigaltstack_size)
0963         return ss_size > fsize;
0964 
0965     mask = current->group_leader->thread.fpu.perm.__state_perm;
0966     if (mask & XFEATURE_MASK_USER_DYNAMIC)
0967         return ss_size > fsize;
0968 
0969     return true;
0970 }
0971 #endif /* CONFIG_DYNAMIC_SIGFRAME */
0972 
0973 #ifdef CONFIG_X86_X32_ABI
0974 COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
0975 {
0976     struct pt_regs *regs = current_pt_regs();
0977     struct rt_sigframe_x32 __user *frame;
0978     sigset_t set;
0979     unsigned long uc_flags;
0980 
0981     frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
0982 
0983     if (!access_ok(frame, sizeof(*frame)))
0984         goto badframe;
0985     if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
0986         goto badframe;
0987     if (__get_user(uc_flags, &frame->uc.uc_flags))
0988         goto badframe;
0989 
0990     set_current_blocked(&set);
0991 
0992     if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
0993         goto badframe;
0994 
0995     if (compat_restore_altstack(&frame->uc.uc_stack))
0996         goto badframe;
0997 
0998     return regs->ax;
0999 
1000 badframe:
1001     signal_fault(regs, frame, "x32 rt_sigreturn");
1002     return 0;
1003 }
1004 #endif