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

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

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ 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.75 ] ~ [ 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 
 29 #include <asm/processor.h>
 30 #include <asm/ucontext.h>
 31 #include <asm/fpu/internal.h>
 32 #include <asm/fpu/signal.h>
 33 #include <asm/vdso.h>
 34 #include <asm/mce.h>
 35 #include <asm/sighandling.h>
 36 #include <asm/vm86.h>
 37 
 38 #ifdef CONFIG_X86_64
 39 #include <asm/proto.h>
 40 #include <asm/ia32_unistd.h>
 41 #endif /* CONFIG_X86_64 */
 42 
 43 #include <asm/syscall.h>
 44 #include <asm/syscalls.h>
 45 
 46 #include <asm/sigframe.h>
 47 #include <asm/signal.h>
 48 
 49 #define COPY(x)                 do {                    \
 50         get_user_ex(regs->x, &sc->x);                   \
 51 } while (0)
 52 
 53 #define GET_SEG(seg)            ({                      \
 54         unsigned short tmp;                             \
 55         get_user_ex(tmp, &sc->seg);                     \
 56         tmp;                                            \
 57 })
 58 
 59 #define COPY_SEG(seg)           do {                    \
 60         regs->seg = GET_SEG(seg);                       \
 61 } while (0)
 62 
 63 #define COPY_SEG_CPL3(seg)      do {                    \
 64         regs->seg = GET_SEG(seg) | 3;                   \
 65 } while (0)
 66 
 67 #ifdef CONFIG_X86_64
 68 /*
 69  * If regs->ss will cause an IRET fault, change it.  Otherwise leave it
 70  * alone.  Using this generally makes no sense unless
 71  * user_64bit_mode(regs) would return true.
 72  */
 73 static void force_valid_ss(struct pt_regs *regs)
 74 {
 75         u32 ar;
 76         asm volatile ("lar %[old_ss], %[ar]\n\t"
 77                       "jz 1f\n\t"               /* If invalid: */
 78                       "xorl %[ar], %[ar]\n\t"   /* set ar = 0 */
 79                       "1:"
 80                       : [ar] "=r" (ar)
 81                       : [old_ss] "rm" ((u16)regs->ss));
 82 
 83         /*
 84          * For a valid 64-bit user context, we need DPL 3, type
 85          * read-write data or read-write exp-down data, and S and P
 86          * set.  We can't use VERW because VERW doesn't check the
 87          * P bit.
 88          */
 89         ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
 90         if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
 91             ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
 92                 regs->ss = __USER_DS;
 93 }
 94 #endif
 95 
 96 static int restore_sigcontext(struct pt_regs *regs,
 97                               struct sigcontext __user *sc,
 98                               unsigned long uc_flags)
 99 {
100         unsigned long buf_val;
101         void __user *buf;
102         unsigned int tmpflags;
103         unsigned int err = 0;
104 
105         /* Always make any pending restarted system calls return -EINTR */
106         current->restart_block.fn = do_no_restart_syscall;
107 
108         get_user_try {
109 
110 #ifdef CONFIG_X86_32
111                 set_user_gs(regs, GET_SEG(gs));
112                 COPY_SEG(fs);
113                 COPY_SEG(es);
114                 COPY_SEG(ds);
115 #endif /* CONFIG_X86_32 */
116 
117                 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
118                 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
119 
120 #ifdef CONFIG_X86_64
121                 COPY(r8);
122                 COPY(r9);
123                 COPY(r10);
124                 COPY(r11);
125                 COPY(r12);
126                 COPY(r13);
127                 COPY(r14);
128                 COPY(r15);
129 #endif /* CONFIG_X86_64 */
130 
131                 COPY_SEG_CPL3(cs);
132                 COPY_SEG_CPL3(ss);
133 
134 #ifdef CONFIG_X86_64
135                 /*
136                  * Fix up SS if needed for the benefit of old DOSEMU and
137                  * CRIU.
138                  */
139                 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
140                              user_64bit_mode(regs)))
141                         force_valid_ss(regs);
142 #endif
143 
144                 get_user_ex(tmpflags, &sc->flags);
145                 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
146                 regs->orig_ax = -1;             /* disable syscall checks */
147 
148                 get_user_ex(buf_val, &sc->fpstate);
149                 buf = (void __user *)buf_val;
150         } get_user_catch(err);
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(VERIFY_WRITE, 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(VERIFY_WRITE, 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         int err = 0;
464 
465         frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
466 
467         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
468                 return -EFAULT;
469 
470         if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
471                 if (copy_siginfo_to_user(&frame->info, &ksig->info))
472                         return -EFAULT;
473         }
474 
475         put_user_try {
476                 /* Create the ucontext.  */
477                 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
478                 put_user_ex(0, &frame->uc.uc_link);
479                 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
480 
481                 /* Set up to return from userspace.  If provided, use a stub
482                    already in userspace.  */
483                 /* x86-64 should always use SA_RESTORER. */
484                 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
485                         put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
486                 } else {
487                         /* could use a vstub here */
488                         err |= -EFAULT;
489                 }
490         } put_user_catch(err);
491 
492         err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
493         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
494 
495         if (err)
496                 return -EFAULT;
497 
498         /* Set up registers for signal handler */
499         regs->di = sig;
500         /* In case the signal handler was declared without prototypes */
501         regs->ax = 0;
502 
503         /* This also works for non SA_SIGINFO handlers because they expect the
504            next argument after the signal number on the stack. */
505         regs->si = (unsigned long)&frame->info;
506         regs->dx = (unsigned long)&frame->uc;
507         regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
508 
509         regs->sp = (unsigned long)frame;
510 
511         /*
512          * Set up the CS and SS registers to run signal handlers in
513          * 64-bit mode, even if the handler happens to be interrupting
514          * 32-bit or 16-bit code.
515          *
516          * SS is subtle.  In 64-bit mode, we don't need any particular
517          * SS descriptor, but we do need SS to be valid.  It's possible
518          * that the old SS is entirely bogus -- this can happen if the
519          * signal we're trying to deliver is #GP or #SS caused by a bad
520          * SS value.  We also have a compatbility issue here: DOSEMU
521          * relies on the contents of the SS register indicating the
522          * SS value at the time of the signal, even though that code in
523          * DOSEMU predates sigreturn's ability to restore SS.  (DOSEMU
524          * avoids relying on sigreturn to restore SS; instead it uses
525          * a trampoline.)  So we do our best: if the old SS was valid,
526          * we keep it.  Otherwise we replace it.
527          */
528         regs->cs = __USER_CS;
529 
530         if (unlikely(regs->ss != __USER_DS))
531                 force_valid_ss(regs);
532 
533         return 0;
534 }
535 #endif /* CONFIG_X86_32 */
536 
537 static int x32_setup_rt_frame(struct ksignal *ksig,
538                               compat_sigset_t *set,
539                               struct pt_regs *regs)
540 {
541 #ifdef CONFIG_X86_X32_ABI
542         struct rt_sigframe_x32 __user *frame;
543         void __user *restorer;
544         int err = 0;
545         void __user *fpstate = NULL;
546 
547         frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
548 
549         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
550                 return -EFAULT;
551 
552         if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
553                 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
554                         return -EFAULT;
555         }
556 
557         put_user_try {
558                 /* Create the ucontext.  */
559                 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
560                 put_user_ex(0, &frame->uc.uc_link);
561                 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
562                 put_user_ex(0, &frame->uc.uc__pad0);
563 
564                 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
565                         restorer = ksig->ka.sa.sa_restorer;
566                 } else {
567                         /* could use a vstub here */
568                         restorer = NULL;
569                         err |= -EFAULT;
570                 }
571                 put_user_ex(restorer, &frame->pretcode);
572         } put_user_catch(err);
573 
574         err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
575                                 regs, set->sig[0]);
576         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
577 
578         if (err)
579                 return -EFAULT;
580 
581         /* Set up registers for signal handler */
582         regs->sp = (unsigned long) frame;
583         regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
584 
585         /* We use the x32 calling convention here... */
586         regs->di = ksig->sig;
587         regs->si = (unsigned long) &frame->info;
588         regs->dx = (unsigned long) &frame->uc;
589 
590         loadsegment(ds, __USER_DS);
591         loadsegment(es, __USER_DS);
592 
593         regs->cs = __USER_CS;
594         regs->ss = __USER_DS;
595 #endif  /* CONFIG_X86_X32_ABI */
596 
597         return 0;
598 }
599 
600 /*
601  * Do a signal return; undo the signal stack.
602  */
603 #ifdef CONFIG_X86_32
604 asmlinkage unsigned long sys_sigreturn(void)
605 {
606         struct pt_regs *regs = current_pt_regs();
607         struct sigframe __user *frame;
608         sigset_t set;
609 
610         frame = (struct sigframe __user *)(regs->sp - 8);
611 
612         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
613                 goto badframe;
614         if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
615                 && __copy_from_user(&set.sig[1], &frame->extramask,
616                                     sizeof(frame->extramask))))
617                 goto badframe;
618 
619         set_current_blocked(&set);
620 
621         /*
622          * x86_32 has no uc_flags bits relevant to restore_sigcontext.
623          * Save a few cycles by skipping the __get_user.
624          */
625         if (restore_sigcontext(regs, &frame->sc, 0))
626                 goto badframe;
627         return regs->ax;
628 
629 badframe:
630         signal_fault(regs, frame, "sigreturn");
631 
632         return 0;
633 }
634 #endif /* CONFIG_X86_32 */
635 
636 asmlinkage long sys_rt_sigreturn(void)
637 {
638         struct pt_regs *regs = current_pt_regs();
639         struct rt_sigframe __user *frame;
640         sigset_t set;
641         unsigned long uc_flags;
642 
643         frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
644         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
645                 goto badframe;
646         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
647                 goto badframe;
648         if (__get_user(uc_flags, &frame->uc.uc_flags))
649                 goto badframe;
650 
651         set_current_blocked(&set);
652 
653         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
654                 goto badframe;
655 
656         if (restore_altstack(&frame->uc.uc_stack))
657                 goto badframe;
658 
659         return regs->ax;
660 
661 badframe:
662         signal_fault(regs, frame, "rt_sigreturn");
663         return 0;
664 }
665 
666 static inline int is_ia32_compat_frame(struct ksignal *ksig)
667 {
668         return IS_ENABLED(CONFIG_IA32_EMULATION) &&
669                 ksig->ka.sa.sa_flags & SA_IA32_ABI;
670 }
671 
672 static inline int is_ia32_frame(struct ksignal *ksig)
673 {
674         return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
675 }
676 
677 static inline int is_x32_frame(struct ksignal *ksig)
678 {
679         return IS_ENABLED(CONFIG_X86_X32_ABI) &&
680                 ksig->ka.sa.sa_flags & SA_X32_ABI;
681 }
682 
683 static int
684 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
685 {
686         int usig = ksig->sig;
687         sigset_t *set = sigmask_to_save();
688         compat_sigset_t *cset = (compat_sigset_t *) set;
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 = &current->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                 if (fpu->initialized)
760                         fpu__clear(fpu);
761         }
762         signal_setup_done(failed, ksig, stepping);
763 }
764 
765 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
766 {
767         /*
768          * This function is fundamentally broken as currently
769          * implemented.
770          *
771          * The idea is that we want to trigger a call to the
772          * restart_block() syscall and that we want in_ia32_syscall(),
773          * in_x32_syscall(), etc. to match whatever they were in the
774          * syscall being restarted.  We assume that the syscall
775          * instruction at (regs->ip - 2) matches whatever syscall
776          * instruction we used to enter in the first place.
777          *
778          * The problem is that we can get here when ptrace pokes
779          * syscall-like values into regs even if we're not in a syscall
780          * at all.
781          *
782          * For now, we maintain historical behavior and guess based on
783          * stored state.  We could do better by saving the actual
784          * syscall arch in restart_block or (with caveats on x32) by
785          * checking if regs->ip points to 'int $0x80'.  The current
786          * behavior is incorrect if a tracer has a different bitness
787          * than the tracee.
788          */
789 #ifdef CONFIG_IA32_EMULATION
790         if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
791                 return __NR_ia32_restart_syscall;
792 #endif
793 #ifdef CONFIG_X86_X32_ABI
794         return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
795 #else
796         return __NR_restart_syscall;
797 #endif
798 }
799 
800 /*
801  * Note that 'init' is a special process: it doesn't get signals it doesn't
802  * want to handle. Thus you cannot kill init even with a SIGKILL even by
803  * mistake.
804  */
805 void do_signal(struct pt_regs *regs)
806 {
807         struct ksignal ksig;
808 
809         if (get_signal(&ksig)) {
810                 /* Whee! Actually deliver the signal.  */
811                 handle_signal(&ksig, regs);
812                 return;
813         }
814 
815         /* Did we come from a system call? */
816         if (syscall_get_nr(current, regs) >= 0) {
817                 /* Restart the system call - no handlers present */
818                 switch (syscall_get_error(current, regs)) {
819                 case -ERESTARTNOHAND:
820                 case -ERESTARTSYS:
821                 case -ERESTARTNOINTR:
822                         regs->ax = regs->orig_ax;
823                         regs->ip -= 2;
824                         break;
825 
826                 case -ERESTART_RESTARTBLOCK:
827                         regs->ax = get_nr_restart_syscall(regs);
828                         regs->ip -= 2;
829                         break;
830                 }
831         }
832 
833         /*
834          * If there's no signal to deliver, we just put the saved sigmask
835          * back.
836          */
837         restore_saved_sigmask();
838 }
839 
840 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
841 {
842         struct task_struct *me = current;
843 
844         if (show_unhandled_signals && printk_ratelimit()) {
845                 printk("%s"
846                        "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
847                        task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
848                        me->comm, me->pid, where, frame,
849                        regs->ip, regs->sp, regs->orig_ax);
850                 print_vma_addr(KERN_CONT " in ", regs->ip);
851                 pr_cont("\n");
852         }
853 
854         force_sig(SIGSEGV, me);
855 }
856 
857 #ifdef CONFIG_X86_X32_ABI
858 asmlinkage long sys32_x32_rt_sigreturn(void)
859 {
860         struct pt_regs *regs = current_pt_regs();
861         struct rt_sigframe_x32 __user *frame;
862         sigset_t set;
863         unsigned long uc_flags;
864 
865         frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
866 
867         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
868                 goto badframe;
869         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
870                 goto badframe;
871         if (__get_user(uc_flags, &frame->uc.uc_flags))
872                 goto badframe;
873 
874         set_current_blocked(&set);
875 
876         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
877                 goto badframe;
878 
879         if (compat_restore_altstack(&frame->uc.uc_stack))
880                 goto badframe;
881 
882         return regs->ax;
883 
884 badframe:
885         signal_fault(regs, frame, "x32 rt_sigreturn");
886         return 0;
887 }
888 #endif
889 

~ [ 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