~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/kernel/signal.c

Version: ~ [ linux-5.6-rc1 ] ~ [ linux-5.5.2 ] ~ [ linux-5.4.17 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.102 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.170 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.213 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.213 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp