arm64/syscalls: Check address limit on user-mode return
[sfrench/cifs-2.6.git] / arch / arm / kernel / signal.c
1 /*
2  *  linux/arch/arm/kernel/signal.c
3  *
4  *  Copyright (C) 1995-2009 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/errno.h>
11 #include <linux/random.h>
12 #include <linux/signal.h>
13 #include <linux/personality.h>
14 #include <linux/uaccess.h>
15 #include <linux/tracehook.h>
16 #include <linux/uprobes.h>
17 #include <linux/syscalls.h>
18
19 #include <asm/elf.h>
20 #include <asm/cacheflush.h>
21 #include <asm/traps.h>
22 #include <asm/ucontext.h>
23 #include <asm/unistd.h>
24 #include <asm/vfp.h>
25
26 extern const unsigned long sigreturn_codes[7];
27
28 static unsigned long signal_return_offset;
29
30 #ifdef CONFIG_CRUNCH
31 static int preserve_crunch_context(struct crunch_sigframe __user *frame)
32 {
33         char kbuf[sizeof(*frame) + 8];
34         struct crunch_sigframe *kframe;
35
36         /* the crunch context must be 64 bit aligned */
37         kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
38         kframe->magic = CRUNCH_MAGIC;
39         kframe->size = CRUNCH_STORAGE_SIZE;
40         crunch_task_copy(current_thread_info(), &kframe->storage);
41         return __copy_to_user(frame, kframe, sizeof(*frame));
42 }
43
44 static int restore_crunch_context(struct crunch_sigframe __user *frame)
45 {
46         char kbuf[sizeof(*frame) + 8];
47         struct crunch_sigframe *kframe;
48
49         /* the crunch context must be 64 bit aligned */
50         kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
51         if (__copy_from_user(kframe, frame, sizeof(*frame)))
52                 return -1;
53         if (kframe->magic != CRUNCH_MAGIC ||
54             kframe->size != CRUNCH_STORAGE_SIZE)
55                 return -1;
56         crunch_task_restore(current_thread_info(), &kframe->storage);
57         return 0;
58 }
59 #endif
60
61 #ifdef CONFIG_IWMMXT
62
63 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
64 {
65         char kbuf[sizeof(*frame) + 8];
66         struct iwmmxt_sigframe *kframe;
67
68         /* the iWMMXt context must be 64 bit aligned */
69         kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
70         kframe->magic = IWMMXT_MAGIC;
71         kframe->size = IWMMXT_STORAGE_SIZE;
72         iwmmxt_task_copy(current_thread_info(), &kframe->storage);
73         return __copy_to_user(frame, kframe, sizeof(*frame));
74 }
75
76 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
77 {
78         char kbuf[sizeof(*frame) + 8];
79         struct iwmmxt_sigframe *kframe;
80
81         /* the iWMMXt context must be 64 bit aligned */
82         kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
83         if (__copy_from_user(kframe, frame, sizeof(*frame)))
84                 return -1;
85         if (kframe->magic != IWMMXT_MAGIC ||
86             kframe->size != IWMMXT_STORAGE_SIZE)
87                 return -1;
88         iwmmxt_task_restore(current_thread_info(), &kframe->storage);
89         return 0;
90 }
91
92 #endif
93
94 #ifdef CONFIG_VFP
95
96 static int preserve_vfp_context(struct vfp_sigframe __user *frame)
97 {
98         const unsigned long magic = VFP_MAGIC;
99         const unsigned long size = VFP_STORAGE_SIZE;
100         int err = 0;
101
102         __put_user_error(magic, &frame->magic, err);
103         __put_user_error(size, &frame->size, err);
104
105         if (err)
106                 return -EFAULT;
107
108         return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
109 }
110
111 static int restore_vfp_context(struct vfp_sigframe __user *frame)
112 {
113         unsigned long magic;
114         unsigned long size;
115         int err = 0;
116
117         __get_user_error(magic, &frame->magic, err);
118         __get_user_error(size, &frame->size, err);
119
120         if (err)
121                 return -EFAULT;
122         if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
123                 return -EINVAL;
124
125         return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
126 }
127
128 #endif
129
130 /*
131  * Do a signal return; undo the signal stack.  These are aligned to 64-bit.
132  */
133 struct sigframe {
134         struct ucontext uc;
135         unsigned long retcode[2];
136 };
137
138 struct rt_sigframe {
139         struct siginfo info;
140         struct sigframe sig;
141 };
142
143 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
144 {
145         struct aux_sigframe __user *aux;
146         sigset_t set;
147         int err;
148
149         err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
150         if (err == 0)
151                 set_current_blocked(&set);
152
153         __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
154         __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
155         __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
156         __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
157         __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
158         __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
159         __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
160         __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
161         __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
162         __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
163         __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
164         __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
165         __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
166         __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
167         __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
168         __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
169         __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
170
171         err |= !valid_user_regs(regs);
172
173         aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
174 #ifdef CONFIG_CRUNCH
175         if (err == 0)
176                 err |= restore_crunch_context(&aux->crunch);
177 #endif
178 #ifdef CONFIG_IWMMXT
179         if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
180                 err |= restore_iwmmxt_context(&aux->iwmmxt);
181 #endif
182 #ifdef CONFIG_VFP
183         if (err == 0)
184                 err |= restore_vfp_context(&aux->vfp);
185 #endif
186
187         return err;
188 }
189
190 asmlinkage int sys_sigreturn(struct pt_regs *regs)
191 {
192         struct sigframe __user *frame;
193
194         /* Always make any pending restarted system calls return -EINTR */
195         current->restart_block.fn = do_no_restart_syscall;
196
197         /*
198          * Since we stacked the signal on a 64-bit boundary,
199          * then 'sp' should be word aligned here.  If it's
200          * not, then the user is trying to mess with us.
201          */
202         if (regs->ARM_sp & 7)
203                 goto badframe;
204
205         frame = (struct sigframe __user *)regs->ARM_sp;
206
207         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
208                 goto badframe;
209
210         if (restore_sigframe(regs, frame))
211                 goto badframe;
212
213         return regs->ARM_r0;
214
215 badframe:
216         force_sig(SIGSEGV, current);
217         return 0;
218 }
219
220 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
221 {
222         struct rt_sigframe __user *frame;
223
224         /* Always make any pending restarted system calls return -EINTR */
225         current->restart_block.fn = do_no_restart_syscall;
226
227         /*
228          * Since we stacked the signal on a 64-bit boundary,
229          * then 'sp' should be word aligned here.  If it's
230          * not, then the user is trying to mess with us.
231          */
232         if (regs->ARM_sp & 7)
233                 goto badframe;
234
235         frame = (struct rt_sigframe __user *)regs->ARM_sp;
236
237         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
238                 goto badframe;
239
240         if (restore_sigframe(regs, &frame->sig))
241                 goto badframe;
242
243         if (restore_altstack(&frame->sig.uc.uc_stack))
244                 goto badframe;
245
246         return regs->ARM_r0;
247
248 badframe:
249         force_sig(SIGSEGV, current);
250         return 0;
251 }
252
253 static int
254 setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
255 {
256         struct aux_sigframe __user *aux;
257         int err = 0;
258
259         __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
260         __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
261         __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
262         __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
263         __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
264         __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
265         __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
266         __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
267         __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
268         __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
269         __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
270         __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
271         __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
272         __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
273         __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
274         __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
275         __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
276
277         __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
278         __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
279         __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
280         __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
281
282         err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
283
284         aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
285 #ifdef CONFIG_CRUNCH
286         if (err == 0)
287                 err |= preserve_crunch_context(&aux->crunch);
288 #endif
289 #ifdef CONFIG_IWMMXT
290         if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
291                 err |= preserve_iwmmxt_context(&aux->iwmmxt);
292 #endif
293 #ifdef CONFIG_VFP
294         if (err == 0)
295                 err |= preserve_vfp_context(&aux->vfp);
296 #endif
297         __put_user_error(0, &aux->end_magic, err);
298
299         return err;
300 }
301
302 static inline void __user *
303 get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
304 {
305         unsigned long sp = sigsp(regs->ARM_sp, ksig);
306         void __user *frame;
307
308         /*
309          * ATPCS B01 mandates 8-byte alignment
310          */
311         frame = (void __user *)((sp - framesize) & ~7);
312
313         /*
314          * Check that we can actually write to the signal frame.
315          */
316         if (!access_ok(VERIFY_WRITE, frame, framesize))
317                 frame = NULL;
318
319         return frame;
320 }
321
322 static int
323 setup_return(struct pt_regs *regs, struct ksignal *ksig,
324              unsigned long __user *rc, void __user *frame)
325 {
326         unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
327         unsigned long retcode;
328         int thumb = 0;
329         unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
330
331         cpsr |= PSR_ENDSTATE;
332
333         /*
334          * Maybe we need to deliver a 32-bit signal to a 26-bit task.
335          */
336         if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
337                 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
338
339 #ifdef CONFIG_ARM_THUMB
340         if (elf_hwcap & HWCAP_THUMB) {
341                 /*
342                  * The LSB of the handler determines if we're going to
343                  * be using THUMB or ARM mode for this signal handler.
344                  */
345                 thumb = handler & 1;
346
347                 /*
348                  * Clear the If-Then Thumb-2 execution state.  ARM spec
349                  * requires this to be all 000s in ARM mode.  Snapdragon
350                  * S4/Krait misbehaves on a Thumb=>ARM signal transition
351                  * without this.
352                  *
353                  * We must do this whenever we are running on a Thumb-2
354                  * capable CPU, which includes ARMv6T2.  However, we elect
355                  * to always do this to simplify the code; this field is
356                  * marked UNK/SBZP for older architectures.
357                  */
358                 cpsr &= ~PSR_IT_MASK;
359
360                 if (thumb) {
361                         cpsr |= PSR_T_BIT;
362                 } else
363                         cpsr &= ~PSR_T_BIT;
364         }
365 #endif
366
367         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
368                 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
369         } else {
370                 unsigned int idx = thumb << 1;
371
372                 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
373                         idx += 3;
374
375                 /*
376                  * Put the sigreturn code on the stack no matter which return
377                  * mechanism we use in order to remain ABI compliant
378                  */
379                 if (__put_user(sigreturn_codes[idx],   rc) ||
380                     __put_user(sigreturn_codes[idx+1], rc+1))
381                         return 1;
382
383 #ifdef CONFIG_MMU
384                 if (cpsr & MODE32_BIT) {
385                         struct mm_struct *mm = current->mm;
386
387                         /*
388                          * 32-bit code can use the signal return page
389                          * except when the MPU has protected the vectors
390                          * page from PL0
391                          */
392                         retcode = mm->context.sigpage + signal_return_offset +
393                                   (idx << 2) + thumb;
394                 } else
395 #endif
396                 {
397                         /*
398                          * Ensure that the instruction cache sees
399                          * the return code written onto the stack.
400                          */
401                         flush_icache_range((unsigned long)rc,
402                                            (unsigned long)(rc + 2));
403
404                         retcode = ((unsigned long)rc) + thumb;
405                 }
406         }
407
408         regs->ARM_r0 = ksig->sig;
409         regs->ARM_sp = (unsigned long)frame;
410         regs->ARM_lr = retcode;
411         regs->ARM_pc = handler;
412         regs->ARM_cpsr = cpsr;
413
414         return 0;
415 }
416
417 static int
418 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
419 {
420         struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
421         int err = 0;
422
423         if (!frame)
424                 return 1;
425
426         /*
427          * Set uc.uc_flags to a value which sc.trap_no would never have.
428          */
429         __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
430
431         err |= setup_sigframe(frame, regs, set);
432         if (err == 0)
433                 err = setup_return(regs, ksig, frame->retcode, frame);
434
435         return err;
436 }
437
438 static int
439 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
440 {
441         struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
442         int err = 0;
443
444         if (!frame)
445                 return 1;
446
447         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
448
449         __put_user_error(0, &frame->sig.uc.uc_flags, err);
450         __put_user_error(NULL, &frame->sig.uc.uc_link, err);
451
452         err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
453         err |= setup_sigframe(&frame->sig, regs, set);
454         if (err == 0)
455                 err = setup_return(regs, ksig, frame->sig.retcode, frame);
456
457         if (err == 0) {
458                 /*
459                  * For realtime signals we must also set the second and third
460                  * arguments for the signal handler.
461                  *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
462                  */
463                 regs->ARM_r1 = (unsigned long)&frame->info;
464                 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
465         }
466
467         return err;
468 }
469
470 /*
471  * OK, we're invoking a handler
472  */     
473 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
474 {
475         sigset_t *oldset = sigmask_to_save();
476         int ret;
477
478         /*
479          * Set up the stack frame
480          */
481         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
482                 ret = setup_rt_frame(ksig, oldset, regs);
483         else
484                 ret = setup_frame(ksig, oldset, regs);
485
486         /*
487          * Check that the resulting registers are actually sane.
488          */
489         ret |= !valid_user_regs(regs);
490
491         signal_setup_done(ret, ksig, 0);
492 }
493
494 /*
495  * Note that 'init' is a special process: it doesn't get signals it doesn't
496  * want to handle. Thus you cannot kill init even with a SIGKILL even by
497  * mistake.
498  *
499  * Note that we go through the signals twice: once to check the signals that
500  * the kernel can handle, and then we build all the user-level signal handling
501  * stack-frames in one go after that.
502  */
503 static int do_signal(struct pt_regs *regs, int syscall)
504 {
505         unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
506         struct ksignal ksig;
507         int restart = 0;
508
509         /*
510          * If we were from a system call, check for system call restarting...
511          */
512         if (syscall) {
513                 continue_addr = regs->ARM_pc;
514                 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
515                 retval = regs->ARM_r0;
516
517                 /*
518                  * Prepare for system call restart.  We do this here so that a
519                  * debugger will see the already changed PSW.
520                  */
521                 switch (retval) {
522                 case -ERESTART_RESTARTBLOCK:
523                         restart -= 2;
524                 case -ERESTARTNOHAND:
525                 case -ERESTARTSYS:
526                 case -ERESTARTNOINTR:
527                         restart++;
528                         regs->ARM_r0 = regs->ARM_ORIG_r0;
529                         regs->ARM_pc = restart_addr;
530                         break;
531                 }
532         }
533
534         /*
535          * Get the signal to deliver.  When running under ptrace, at this
536          * point the debugger may change all our registers ...
537          */
538         /*
539          * Depending on the signal settings we may need to revert the
540          * decision to restart the system call.  But skip this if a
541          * debugger has chosen to restart at a different PC.
542          */
543         if (get_signal(&ksig)) {
544                 /* handler */
545                 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
546                         if (retval == -ERESTARTNOHAND ||
547                             retval == -ERESTART_RESTARTBLOCK
548                             || (retval == -ERESTARTSYS
549                                 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
550                                 regs->ARM_r0 = -EINTR;
551                                 regs->ARM_pc = continue_addr;
552                         }
553                 }
554                 handle_signal(&ksig, regs);
555         } else {
556                 /* no handler */
557                 restore_saved_sigmask();
558                 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
559                         regs->ARM_pc = continue_addr;
560                         return restart;
561                 }
562         }
563         return 0;
564 }
565
566 asmlinkage int
567 do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
568 {
569         /*
570          * The assembly code enters us with IRQs off, but it hasn't
571          * informed the tracing code of that for efficiency reasons.
572          * Update the trace code with the current status.
573          */
574         trace_hardirqs_off();
575
576         /* Check valid user FS if needed */
577         addr_limit_user_check();
578
579         do {
580                 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
581                         schedule();
582                 } else {
583                         if (unlikely(!user_mode(regs)))
584                                 return 0;
585                         local_irq_enable();
586                         if (thread_flags & _TIF_SIGPENDING) {
587                                 int restart = do_signal(regs, syscall);
588                                 if (unlikely(restart)) {
589                                         /*
590                                          * Restart without handlers.
591                                          * Deal with it without leaving
592                                          * the kernel space.
593                                          */
594                                         return restart;
595                                 }
596                                 syscall = 0;
597                         } else if (thread_flags & _TIF_UPROBE) {
598                                 uprobe_notify_resume(regs);
599                         } else {
600                                 clear_thread_flag(TIF_NOTIFY_RESUME);
601                                 tracehook_notify_resume(regs);
602                         }
603                 }
604                 local_irq_disable();
605                 thread_flags = current_thread_info()->flags;
606         } while (thread_flags & _TIF_WORK_MASK);
607         return 0;
608 }
609
610 struct page *get_signal_page(void)
611 {
612         unsigned long ptr;
613         unsigned offset;
614         struct page *page;
615         void *addr;
616
617         page = alloc_pages(GFP_KERNEL, 0);
618
619         if (!page)
620                 return NULL;
621
622         addr = page_address(page);
623
624         /* Give the signal return code some randomness */
625         offset = 0x200 + (get_random_int() & 0x7fc);
626         signal_return_offset = offset;
627
628         /*
629          * Copy signal return handlers into the vector page, and
630          * set sigreturn to be a pointer to these.
631          */
632         memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
633
634         ptr = (unsigned long)addr + offset;
635         flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
636
637         return page;
638 }