1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/wait.h>
18#include <linux/tracehook.h>
19#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/uaccess.h>
23#include <linux/user-return-notifier.h>
24#include <linux/uprobes.h>
25#include <linux/context_tracking.h>
26
27#include <asm/processor.h>
28#include <asm/ucontext.h>
29#include <asm/i387.h>
30#include <asm/fpu-internal.h>
31#include <asm/vdso.h>
32#include <asm/mce.h>
33#include <asm/sighandling.h>
34
35#ifdef CONFIG_X86_64
36#include <asm/proto.h>
37#include <asm/ia32_unistd.h>
38#include <asm/sys_ia32.h>
39#endif
40
41#include <asm/syscall.h>
42#include <asm/syscalls.h>
43
44#include <asm/sigframe.h>
45
46#ifdef CONFIG_X86_32
47# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
48#else
49# define FIX_EFLAGS __FIX_EFLAGS
50#endif
51
52#define COPY(x) do { \
53 get_user_ex(regs->x, &sc->x); \
54} while (0)
55
56#define GET_SEG(seg) ({ \
57 unsigned short tmp; \
58 get_user_ex(tmp, &sc->seg); \
59 tmp; \
60})
61
62#define COPY_SEG(seg) do { \
63 regs->seg = GET_SEG(seg); \
64} while (0)
65
66#define COPY_SEG_CPL3(seg) do { \
67 regs->seg = GET_SEG(seg) | 3; \
68} while (0)
69
70int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
71 unsigned long *pax)
72{
73 void __user *buf;
74 unsigned int tmpflags;
75 unsigned int err = 0;
76
77
78 current_thread_info()->restart_block.fn = do_no_restart_syscall;
79
80 get_user_try {
81
82#ifdef CONFIG_X86_32
83 set_user_gs(regs, GET_SEG(gs));
84 COPY_SEG(fs);
85 COPY_SEG(es);
86 COPY_SEG(ds);
87#endif
88
89 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
90 COPY(dx); COPY(cx); COPY(ip);
91
92#ifdef CONFIG_X86_64
93 COPY(r8);
94 COPY(r9);
95 COPY(r10);
96 COPY(r11);
97 COPY(r12);
98 COPY(r13);
99 COPY(r14);
100 COPY(r15);
101#endif
102
103#ifdef CONFIG_X86_32
104 COPY_SEG_CPL3(cs);
105 COPY_SEG_CPL3(ss);
106#else
107
108
109
110 COPY_SEG_CPL3(cs);
111#endif
112
113 get_user_ex(tmpflags, &sc->flags);
114 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
115 regs->orig_ax = -1;
116
117 get_user_ex(buf, &sc->fpstate);
118
119 get_user_ex(*pax, &sc->ax);
120 } get_user_catch(err);
121
122 err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
123
124 return err;
125}
126
127int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
128 struct pt_regs *regs, unsigned long mask)
129{
130 int err = 0;
131
132 put_user_try {
133
134#ifdef CONFIG_X86_32
135 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
136 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
137 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
138 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
139#endif
140
141 put_user_ex(regs->di, &sc->di);
142 put_user_ex(regs->si, &sc->si);
143 put_user_ex(regs->bp, &sc->bp);
144 put_user_ex(regs->sp, &sc->sp);
145 put_user_ex(regs->bx, &sc->bx);
146 put_user_ex(regs->dx, &sc->dx);
147 put_user_ex(regs->cx, &sc->cx);
148 put_user_ex(regs->ax, &sc->ax);
149#ifdef CONFIG_X86_64
150 put_user_ex(regs->r8, &sc->r8);
151 put_user_ex(regs->r9, &sc->r9);
152 put_user_ex(regs->r10, &sc->r10);
153 put_user_ex(regs->r11, &sc->r11);
154 put_user_ex(regs->r12, &sc->r12);
155 put_user_ex(regs->r13, &sc->r13);
156 put_user_ex(regs->r14, &sc->r14);
157 put_user_ex(regs->r15, &sc->r15);
158#endif
159
160 put_user_ex(current->thread.trap_nr, &sc->trapno);
161 put_user_ex(current->thread.error_code, &sc->err);
162 put_user_ex(regs->ip, &sc->ip);
163#ifdef CONFIG_X86_32
164 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
165 put_user_ex(regs->flags, &sc->flags);
166 put_user_ex(regs->sp, &sc->sp_at_signal);
167 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
168#else
169 put_user_ex(regs->flags, &sc->flags);
170 put_user_ex(regs->cs, &sc->cs);
171 put_user_ex(0, &sc->gs);
172 put_user_ex(0, &sc->fs);
173#endif
174
175 put_user_ex(fpstate, &sc->fpstate);
176
177
178 put_user_ex(mask, &sc->oldmask);
179 put_user_ex(current->thread.cr2, &sc->cr2);
180 } put_user_catch(err);
181
182 return err;
183}
184
185
186
187
188
189
190
191
192static unsigned long align_sigframe(unsigned long sp)
193{
194#ifdef CONFIG_X86_32
195
196
197
198
199 sp = ((sp + 4) & -16ul) - 4;
200#else
201 sp = round_down(sp, 16) - 8;
202#endif
203 return sp;
204}
205
206static inline void __user *
207get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
208 void __user **fpstate)
209{
210
211 unsigned long math_size = 0;
212 unsigned long sp = regs->sp;
213 unsigned long buf_fx = 0;
214 int onsigstack = on_sig_stack(sp);
215
216
217 if (config_enabled(CONFIG_X86_64))
218 sp -= 128;
219
220 if (!onsigstack) {
221
222 if (ka->sa.sa_flags & SA_ONSTACK) {
223 if (current->sas_ss_size)
224 sp = current->sas_ss_sp + current->sas_ss_size;
225 } else if (config_enabled(CONFIG_X86_32) &&
226 (regs->ss & 0xffff) != __USER_DS &&
227 !(ka->sa.sa_flags & SA_RESTORER) &&
228 ka->sa.sa_restorer) {
229
230 sp = (unsigned long) ka->sa.sa_restorer;
231 }
232 }
233
234 if (used_math()) {
235 sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
236 &buf_fx, &math_size);
237 *fpstate = (void __user *)sp;
238 }
239
240 sp = align_sigframe(sp - frame_size);
241
242
243
244
245
246 if (onsigstack && !likely(on_sig_stack(sp)))
247 return (void __user *)-1L;
248
249
250 if (used_math() &&
251 save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
252 return (void __user *)-1L;
253
254 return (void __user *)sp;
255}
256
257#ifdef CONFIG_X86_32
258static const struct {
259 u16 poplmovl;
260 u32 val;
261 u16 int80;
262} __attribute__((packed)) retcode = {
263 0xb858,
264 __NR_sigreturn,
265 0x80cd,
266};
267
268static const struct {
269 u8 movl;
270 u32 val;
271 u16 int80;
272 u8 pad;
273} __attribute__((packed)) rt_retcode = {
274 0xb8,
275 __NR_rt_sigreturn,
276 0x80cd,
277 0
278};
279
280static int
281__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
282 struct pt_regs *regs)
283{
284 struct sigframe __user *frame;
285 void __user *restorer;
286 int err = 0;
287 void __user *fpstate = NULL;
288
289 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
290
291 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
292 return -EFAULT;
293
294 if (__put_user(sig, &frame->sig))
295 return -EFAULT;
296
297 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
298 return -EFAULT;
299
300 if (_NSIG_WORDS > 1) {
301 if (__copy_to_user(&frame->extramask, &set->sig[1],
302 sizeof(frame->extramask)))
303 return -EFAULT;
304 }
305
306 if (current->mm->context.vdso)
307 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
308 else
309 restorer = &frame->retcode;
310 if (ksig->ka.sa.sa_flags & SA_RESTORER)
311 restorer = ksig->ka.sa.sa_restorer;
312
313
314 err |= __put_user(restorer, &frame->pretcode);
315
316
317
318
319
320
321
322
323 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
324
325 if (err)
326 return -EFAULT;
327
328
329 regs->sp = (unsigned long)frame;
330 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
331 regs->ax = (unsigned long)sig;
332 regs->dx = 0;
333 regs->cx = 0;
334
335 regs->ds = __USER_DS;
336 regs->es = __USER_DS;
337 regs->ss = __USER_DS;
338 regs->cs = __USER_CS;
339
340 return 0;
341}
342
343static int __setup_rt_frame(int sig, struct ksignal *ksig,
344 sigset_t *set, struct pt_regs *regs)
345{
346 struct rt_sigframe __user *frame;
347 void __user *restorer;
348 int err = 0;
349 void __user *fpstate = NULL;
350
351 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
352
353 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
354 return -EFAULT;
355
356 put_user_try {
357 put_user_ex(sig, &frame->sig);
358 put_user_ex(&frame->info, &frame->pinfo);
359 put_user_ex(&frame->uc, &frame->puc);
360
361
362 if (cpu_has_xsave)
363 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
364 else
365 put_user_ex(0, &frame->uc.uc_flags);
366 put_user_ex(0, &frame->uc.uc_link);
367 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
368
369
370 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
371 if (ksig->ka.sa.sa_flags & SA_RESTORER)
372 restorer = ksig->ka.sa.sa_restorer;
373 put_user_ex(restorer, &frame->pretcode);
374
375
376
377
378
379
380
381
382 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
383 } put_user_catch(err);
384
385 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
386 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
387 regs, set->sig[0]);
388 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
389
390 if (err)
391 return -EFAULT;
392
393
394 regs->sp = (unsigned long)frame;
395 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
396 regs->ax = (unsigned long)sig;
397 regs->dx = (unsigned long)&frame->info;
398 regs->cx = (unsigned long)&frame->uc;
399
400 regs->ds = __USER_DS;
401 regs->es = __USER_DS;
402 regs->ss = __USER_DS;
403 regs->cs = __USER_CS;
404
405 return 0;
406}
407#else
408static int __setup_rt_frame(int sig, struct ksignal *ksig,
409 sigset_t *set, struct pt_regs *regs)
410{
411 struct rt_sigframe __user *frame;
412 void __user *fp = NULL;
413 int err = 0;
414
415 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
416
417 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
418 return -EFAULT;
419
420 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
421 if (copy_siginfo_to_user(&frame->info, &ksig->info))
422 return -EFAULT;
423 }
424
425 put_user_try {
426
427 if (cpu_has_xsave)
428 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
429 else
430 put_user_ex(0, &frame->uc.uc_flags);
431 put_user_ex(0, &frame->uc.uc_link);
432 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
433
434
435
436
437 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
438 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
439 } else {
440
441 err |= -EFAULT;
442 }
443 } put_user_catch(err);
444
445 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
446 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
447
448 if (err)
449 return -EFAULT;
450
451
452 regs->di = sig;
453
454 regs->ax = 0;
455
456
457
458 regs->si = (unsigned long)&frame->info;
459 regs->dx = (unsigned long)&frame->uc;
460 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
461
462 regs->sp = (unsigned long)frame;
463
464
465
466 regs->cs = __USER_CS;
467
468 return 0;
469}
470#endif
471
472static int x32_setup_rt_frame(struct ksignal *ksig,
473 compat_sigset_t *set,
474 struct pt_regs *regs)
475{
476#ifdef CONFIG_X86_X32_ABI
477 struct rt_sigframe_x32 __user *frame;
478 void __user *restorer;
479 int err = 0;
480 void __user *fpstate = NULL;
481
482 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
483
484 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
485 return -EFAULT;
486
487 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
488 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
489 return -EFAULT;
490 }
491
492 put_user_try {
493
494 if (cpu_has_xsave)
495 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
496 else
497 put_user_ex(0, &frame->uc.uc_flags);
498 put_user_ex(0, &frame->uc.uc_link);
499 err |= __compat_save_altstack(&frame->uc.uc_stack, regs->sp);
500 put_user_ex(0, &frame->uc.uc__pad0);
501
502 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
503 restorer = ksig->ka.sa.sa_restorer;
504 } else {
505
506 restorer = NULL;
507 err |= -EFAULT;
508 }
509 put_user_ex(restorer, &frame->pretcode);
510 } put_user_catch(err);
511
512 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
513 regs, set->sig[0]);
514 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
515
516 if (err)
517 return -EFAULT;
518
519
520 regs->sp = (unsigned long) frame;
521 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
522
523
524 regs->di = ksig->sig;
525 regs->si = (unsigned long) &frame->info;
526 regs->dx = (unsigned long) &frame->uc;
527
528 loadsegment(ds, __USER_DS);
529 loadsegment(es, __USER_DS);
530
531 regs->cs = __USER_CS;
532 regs->ss = __USER_DS;
533#endif
534
535 return 0;
536}
537
538
539
540
541#ifdef CONFIG_X86_32
542unsigned long sys_sigreturn(void)
543{
544 struct pt_regs *regs = current_pt_regs();
545 struct sigframe __user *frame;
546 unsigned long ax;
547 sigset_t set;
548
549 frame = (struct sigframe __user *)(regs->sp - 8);
550
551 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
552 goto badframe;
553 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
554 && __copy_from_user(&set.sig[1], &frame->extramask,
555 sizeof(frame->extramask))))
556 goto badframe;
557
558 set_current_blocked(&set);
559
560 if (restore_sigcontext(regs, &frame->sc, &ax))
561 goto badframe;
562 return ax;
563
564badframe:
565 signal_fault(regs, frame, "sigreturn");
566
567 return 0;
568}
569#endif
570
571long sys_rt_sigreturn(void)
572{
573 struct pt_regs *regs = current_pt_regs();
574 struct rt_sigframe __user *frame;
575 unsigned long ax;
576 sigset_t set;
577
578 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
579 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
580 goto badframe;
581 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
582 goto badframe;
583
584 set_current_blocked(&set);
585
586 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
587 goto badframe;
588
589 if (restore_altstack(&frame->uc.uc_stack))
590 goto badframe;
591
592 return ax;
593
594badframe:
595 signal_fault(regs, frame, "rt_sigreturn");
596 return 0;
597}
598
599
600
601
602static int signr_convert(int sig)
603{
604#ifdef CONFIG_X86_32
605 struct thread_info *info = current_thread_info();
606
607 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
608 return info->exec_domain->signal_invmap[sig];
609#endif
610 return sig;
611}
612
613static int
614setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
615{
616 int usig = signr_convert(ksig->sig);
617 sigset_t *set = sigmask_to_save();
618 compat_sigset_t *cset = (compat_sigset_t *) set;
619
620
621 if (is_ia32_frame()) {
622 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
623 return ia32_setup_rt_frame(usig, ksig, cset, regs);
624 else
625 return ia32_setup_frame(usig, ksig, cset, regs);
626 } else if (is_x32_frame()) {
627 return x32_setup_rt_frame(ksig, cset, regs);
628 } else {
629 return __setup_rt_frame(ksig->sig, ksig, set, regs);
630 }
631}
632
633static void
634handle_signal(struct ksignal *ksig, struct pt_regs *regs)
635{
636 bool failed;
637
638 if (syscall_get_nr(current, regs) >= 0) {
639
640 switch (syscall_get_error(current, regs)) {
641 case -ERESTART_RESTARTBLOCK:
642 case -ERESTARTNOHAND:
643 regs->ax = -EINTR;
644 break;
645
646 case -ERESTARTSYS:
647 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
648 regs->ax = -EINTR;
649 break;
650 }
651
652 case -ERESTARTNOINTR:
653 regs->ax = regs->orig_ax;
654 regs->ip -= 2;
655 break;
656 }
657 }
658
659
660
661
662
663 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
664 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
665 regs->flags &= ~X86_EFLAGS_TF;
666
667 failed = (setup_rt_frame(ksig, regs) < 0);
668 if (!failed) {
669
670
671
672 regs->flags &= ~X86_EFLAGS_DF;
673
674
675
676
677
678
679 regs->flags &= ~X86_EFLAGS_TF;
680 }
681 signal_setup_done(failed, ksig, test_thread_flag(TIF_SINGLESTEP));
682}
683
684#ifdef CONFIG_X86_32
685#define NR_restart_syscall __NR_restart_syscall
686#else
687#define NR_restart_syscall \
688 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
689#endif
690
691
692
693
694
695
696static void do_signal(struct pt_regs *regs)
697{
698 struct ksignal ksig;
699
700 if (get_signal(&ksig)) {
701
702 handle_signal(&ksig, regs);
703 return;
704 }
705
706
707 if (syscall_get_nr(current, regs) >= 0) {
708
709 switch (syscall_get_error(current, regs)) {
710 case -ERESTARTNOHAND:
711 case -ERESTARTSYS:
712 case -ERESTARTNOINTR:
713 regs->ax = regs->orig_ax;
714 regs->ip -= 2;
715 break;
716
717 case -ERESTART_RESTARTBLOCK:
718 regs->ax = NR_restart_syscall;
719 regs->ip -= 2;
720 break;
721 }
722 }
723
724
725
726
727
728 restore_saved_sigmask();
729}
730
731
732
733
734
735void
736do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
737{
738 user_exit();
739
740#ifdef CONFIG_X86_MCE
741
742 if (thread_info_flags & _TIF_MCE_NOTIFY)
743 mce_notify_process();
744#endif
745
746 if (thread_info_flags & _TIF_UPROBE)
747 uprobe_notify_resume(regs);
748
749
750 if (thread_info_flags & _TIF_SIGPENDING)
751 do_signal(regs);
752
753 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
754 clear_thread_flag(TIF_NOTIFY_RESUME);
755 tracehook_notify_resume(regs);
756 }
757 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
758 fire_user_return_notifiers();
759
760 user_enter();
761}
762
763void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
764{
765 struct task_struct *me = current;
766
767 if (show_unhandled_signals && printk_ratelimit()) {
768 printk("%s"
769 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
770 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
771 me->comm, me->pid, where, frame,
772 regs->ip, regs->sp, regs->orig_ax);
773 print_vma_addr(" in ", regs->ip);
774 pr_cont("\n");
775 }
776
777 force_sig(SIGSEGV, me);
778}
779
780#ifdef CONFIG_X86_X32_ABI
781asmlinkage long sys32_x32_rt_sigreturn(void)
782{
783 struct pt_regs *regs = current_pt_regs();
784 struct rt_sigframe_x32 __user *frame;
785 sigset_t set;
786 unsigned long ax;
787
788 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
789
790 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
791 goto badframe;
792 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
793 goto badframe;
794
795 set_current_blocked(&set);
796
797 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
798 goto badframe;
799
800 if (compat_restore_altstack(&frame->uc.uc_stack))
801 goto badframe;
802
803 return ax;
804
805badframe:
806 signal_fault(regs, frame, "x32 rt_sigreturn");
807 return 0;
808}
809#endif
810