arm64: Provide read/write fault information in compat signal handlers
[cascardo/linux.git] / arch / arm64 / kernel / signal32.c
1 /*
2  * Based on arch/arm/kernel/signal.c
3  *
4  * Copyright (C) 1995-2009 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  * Modified by Will Deacon <will.deacon@arm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <linux/compat.h>
22 #include <linux/signal.h>
23 #include <linux/syscalls.h>
24 #include <linux/ratelimit.h>
25
26 #include <asm/esr.h>
27 #include <asm/fpsimd.h>
28 #include <asm/signal32.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd32.h>
31
32 struct compat_sigcontext {
33         /* We always set these two fields to 0 */
34         compat_ulong_t                  trap_no;
35         compat_ulong_t                  error_code;
36
37         compat_ulong_t                  oldmask;
38         compat_ulong_t                  arm_r0;
39         compat_ulong_t                  arm_r1;
40         compat_ulong_t                  arm_r2;
41         compat_ulong_t                  arm_r3;
42         compat_ulong_t                  arm_r4;
43         compat_ulong_t                  arm_r5;
44         compat_ulong_t                  arm_r6;
45         compat_ulong_t                  arm_r7;
46         compat_ulong_t                  arm_r8;
47         compat_ulong_t                  arm_r9;
48         compat_ulong_t                  arm_r10;
49         compat_ulong_t                  arm_fp;
50         compat_ulong_t                  arm_ip;
51         compat_ulong_t                  arm_sp;
52         compat_ulong_t                  arm_lr;
53         compat_ulong_t                  arm_pc;
54         compat_ulong_t                  arm_cpsr;
55         compat_ulong_t                  fault_address;
56 };
57
58 struct compat_ucontext {
59         compat_ulong_t                  uc_flags;
60         compat_uptr_t                   uc_link;
61         compat_stack_t                  uc_stack;
62         struct compat_sigcontext        uc_mcontext;
63         compat_sigset_t                 uc_sigmask;
64         int             __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
65         compat_ulong_t  uc_regspace[128] __attribute__((__aligned__(8)));
66 };
67
68 struct compat_vfp_sigframe {
69         compat_ulong_t  magic;
70         compat_ulong_t  size;
71         struct compat_user_vfp {
72                 compat_u64      fpregs[32];
73                 compat_ulong_t  fpscr;
74         } ufp;
75         struct compat_user_vfp_exc {
76                 compat_ulong_t  fpexc;
77                 compat_ulong_t  fpinst;
78                 compat_ulong_t  fpinst2;
79         } ufp_exc;
80 } __attribute__((__aligned__(8)));
81
82 #define VFP_MAGIC               0x56465001
83 #define VFP_STORAGE_SIZE        sizeof(struct compat_vfp_sigframe)
84
85 #define FSR_WRITE_SHIFT         (11)
86
87 struct compat_aux_sigframe {
88         struct compat_vfp_sigframe      vfp;
89
90         /* Something that isn't a valid magic number for any coprocessor.  */
91         unsigned long                   end_magic;
92 } __attribute__((__aligned__(8)));
93
94 struct compat_sigframe {
95         struct compat_ucontext  uc;
96         compat_ulong_t          retcode[2];
97 };
98
99 struct compat_rt_sigframe {
100         struct compat_siginfo info;
101         struct compat_sigframe sig;
102 };
103
104 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
105
106 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
107 {
108         compat_sigset_t cset;
109
110         cset.sig[0] = set->sig[0] & 0xffffffffull;
111         cset.sig[1] = set->sig[0] >> 32;
112
113         return copy_to_user(uset, &cset, sizeof(*uset));
114 }
115
116 static inline int get_sigset_t(sigset_t *set,
117                                const compat_sigset_t __user *uset)
118 {
119         compat_sigset_t s32;
120
121         if (copy_from_user(&s32, uset, sizeof(*uset)))
122                 return -EFAULT;
123
124         set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
125         return 0;
126 }
127
128 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
129 {
130         int err;
131
132         if (!access_ok(VERIFY_WRITE, to, sizeof(*to)))
133                 return -EFAULT;
134
135         /* If you change siginfo_t structure, please be sure
136          * this code is fixed accordingly.
137          * It should never copy any pad contained in the structure
138          * to avoid security leaks, but must copy the generic
139          * 3 ints plus the relevant union member.
140          * This routine must convert siginfo from 64bit to 32bit as well
141          * at the same time.
142          */
143         err = __put_user(from->si_signo, &to->si_signo);
144         err |= __put_user(from->si_errno, &to->si_errno);
145         err |= __put_user((short)from->si_code, &to->si_code);
146         if (from->si_code < 0)
147                 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad,
148                                       SI_PAD_SIZE);
149         else switch (from->si_code & __SI_MASK) {
150         case __SI_KILL:
151                 err |= __put_user(from->si_pid, &to->si_pid);
152                 err |= __put_user(from->si_uid, &to->si_uid);
153                 break;
154         case __SI_TIMER:
155                  err |= __put_user(from->si_tid, &to->si_tid);
156                  err |= __put_user(from->si_overrun, &to->si_overrun);
157                  err |= __put_user((compat_uptr_t)(unsigned long)from->si_ptr,
158                                    &to->si_ptr);
159                 break;
160         case __SI_POLL:
161                 err |= __put_user(from->si_band, &to->si_band);
162                 err |= __put_user(from->si_fd, &to->si_fd);
163                 break;
164         case __SI_FAULT:
165                 err |= __put_user((compat_uptr_t)(unsigned long)from->si_addr,
166                                   &to->si_addr);
167 #ifdef BUS_MCEERR_AO
168                 /*
169                  * Other callers might not initialize the si_lsb field,
170                  * so check explicitely for the right codes here.
171                  */
172                 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
173                         err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
174 #endif
175                 break;
176         case __SI_CHLD:
177                 err |= __put_user(from->si_pid, &to->si_pid);
178                 err |= __put_user(from->si_uid, &to->si_uid);
179                 err |= __put_user(from->si_status, &to->si_status);
180                 err |= __put_user(from->si_utime, &to->si_utime);
181                 err |= __put_user(from->si_stime, &to->si_stime);
182                 break;
183         case __SI_RT: /* This is not generated by the kernel as of now. */
184         case __SI_MESGQ: /* But this is */
185                 err |= __put_user(from->si_pid, &to->si_pid);
186                 err |= __put_user(from->si_uid, &to->si_uid);
187                 err |= __put_user((compat_uptr_t)(unsigned long)from->si_ptr, &to->si_ptr);
188                 break;
189         default: /* this is just in case for now ... */
190                 err |= __put_user(from->si_pid, &to->si_pid);
191                 err |= __put_user(from->si_uid, &to->si_uid);
192                 break;
193         }
194         return err;
195 }
196
197 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
198 {
199         memset(to, 0, sizeof *to);
200
201         if (copy_from_user(to, from, __ARCH_SI_PREAMBLE_SIZE) ||
202             copy_from_user(to->_sifields._pad,
203                            from->_sifields._pad, SI_PAD_SIZE))
204                 return -EFAULT;
205
206         return 0;
207 }
208
209 /*
210  * VFP save/restore code.
211  */
212 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
213 {
214         struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
215         compat_ulong_t magic = VFP_MAGIC;
216         compat_ulong_t size = VFP_STORAGE_SIZE;
217         compat_ulong_t fpscr, fpexc;
218         int err = 0;
219
220         /*
221          * Save the hardware registers to the fpsimd_state structure.
222          * Note that this also saves V16-31, which aren't visible
223          * in AArch32.
224          */
225         fpsimd_save_state(fpsimd);
226
227         /* Place structure header on the stack */
228         __put_user_error(magic, &frame->magic, err);
229         __put_user_error(size, &frame->size, err);
230
231         /*
232          * Now copy the FP registers. Since the registers are packed,
233          * we can copy the prefix we want (V0-V15) as it is.
234          * FIXME: Won't work if big endian.
235          */
236         err |= __copy_to_user(&frame->ufp.fpregs, fpsimd->vregs,
237                               sizeof(frame->ufp.fpregs));
238
239         /* Create an AArch32 fpscr from the fpsr and the fpcr. */
240         fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
241                 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
242         __put_user_error(fpscr, &frame->ufp.fpscr, err);
243
244         /*
245          * The exception register aren't available so we fake up a
246          * basic FPEXC and zero everything else.
247          */
248         fpexc = (1 << 30);
249         __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
250         __put_user_error(0, &frame->ufp_exc.fpinst, err);
251         __put_user_error(0, &frame->ufp_exc.fpinst2, err);
252
253         return err ? -EFAULT : 0;
254 }
255
256 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
257 {
258         struct fpsimd_state fpsimd;
259         compat_ulong_t magic = VFP_MAGIC;
260         compat_ulong_t size = VFP_STORAGE_SIZE;
261         compat_ulong_t fpscr;
262         int err = 0;
263
264         __get_user_error(magic, &frame->magic, err);
265         __get_user_error(size, &frame->size, err);
266
267         if (err)
268                 return -EFAULT;
269         if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
270                 return -EINVAL;
271
272         /*
273          * Copy the FP registers into the start of the fpsimd_state.
274          * FIXME: Won't work if big endian.
275          */
276         err |= __copy_from_user(fpsimd.vregs, frame->ufp.fpregs,
277                                 sizeof(frame->ufp.fpregs));
278
279         /* Extract the fpsr and the fpcr from the fpscr */
280         __get_user_error(fpscr, &frame->ufp.fpscr, err);
281         fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
282         fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
283
284         /*
285          * We don't need to touch the exception register, so
286          * reload the hardware state.
287          */
288         if (!err) {
289                 preempt_disable();
290                 fpsimd_load_state(&fpsimd);
291                 preempt_enable();
292         }
293
294         return err ? -EFAULT : 0;
295 }
296
297 static int compat_restore_sigframe(struct pt_regs *regs,
298                                    struct compat_sigframe __user *sf)
299 {
300         int err;
301         sigset_t set;
302         struct compat_aux_sigframe __user *aux;
303
304         err = get_sigset_t(&set, &sf->uc.uc_sigmask);
305         if (err == 0) {
306                 sigdelsetmask(&set, ~_BLOCKABLE);
307                 set_current_blocked(&set);
308         }
309
310         __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
311         __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
312         __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
313         __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
314         __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
315         __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
316         __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
317         __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
318         __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
319         __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
320         __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
321         __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
322         __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
323         __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
324         __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
325         __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
326         __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
327
328         /*
329          * Avoid compat_sys_sigreturn() restarting.
330          */
331         regs->syscallno = ~0UL;
332
333         err |= !valid_user_regs(&regs->user_regs);
334
335         aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
336         if (err == 0)
337                 err |= compat_restore_vfp_context(&aux->vfp);
338
339         return err;
340 }
341
342 asmlinkage int compat_sys_sigreturn(struct pt_regs *regs)
343 {
344         struct compat_sigframe __user *frame;
345
346         /* Always make any pending restarted system calls return -EINTR */
347         current_thread_info()->restart_block.fn = do_no_restart_syscall;
348
349         /*
350          * Since we stacked the signal on a 64-bit boundary,
351          * then 'sp' should be word aligned here.  If it's
352          * not, then the user is trying to mess with us.
353          */
354         if (regs->compat_sp & 7)
355                 goto badframe;
356
357         frame = (struct compat_sigframe __user *)regs->compat_sp;
358
359         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
360                 goto badframe;
361
362         if (compat_restore_sigframe(regs, frame))
363                 goto badframe;
364
365         return regs->regs[0];
366
367 badframe:
368         if (show_unhandled_signals)
369                 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
370                                     current->comm, task_pid_nr(current), __func__,
371                                     regs->pc, regs->sp);
372         force_sig(SIGSEGV, current);
373         return 0;
374 }
375
376 asmlinkage int compat_sys_rt_sigreturn(struct pt_regs *regs)
377 {
378         struct compat_rt_sigframe __user *frame;
379
380         /* Always make any pending restarted system calls return -EINTR */
381         current_thread_info()->restart_block.fn = do_no_restart_syscall;
382
383         /*
384          * Since we stacked the signal on a 64-bit boundary,
385          * then 'sp' should be word aligned here.  If it's
386          * not, then the user is trying to mess with us.
387          */
388         if (regs->compat_sp & 7)
389                 goto badframe;
390
391         frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
392
393         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
394                 goto badframe;
395
396         if (compat_restore_sigframe(regs, &frame->sig))
397                 goto badframe;
398
399         if (compat_restore_altstack(&frame->sig.uc.uc_stack))
400                 goto badframe;
401
402         return regs->regs[0];
403
404 badframe:
405         if (show_unhandled_signals)
406                 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
407                                     current->comm, task_pid_nr(current), __func__,
408                                     regs->pc, regs->sp);
409         force_sig(SIGSEGV, current);
410         return 0;
411 }
412
413 static void __user *compat_get_sigframe(struct k_sigaction *ka,
414                                         struct pt_regs *regs,
415                                         int framesize)
416 {
417         compat_ulong_t sp = regs->compat_sp;
418         void __user *frame;
419
420         /*
421          * This is the X/Open sanctioned signal stack switching.
422          */
423         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
424                 sp = current->sas_ss_sp + current->sas_ss_size;
425
426         /*
427          * ATPCS B01 mandates 8-byte alignment
428          */
429         frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
430
431         /*
432          * Check that we can actually write to the signal frame.
433          */
434         if (!access_ok(VERIFY_WRITE, frame, framesize))
435                 frame = NULL;
436
437         return frame;
438 }
439
440 static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
441                                 compat_ulong_t __user *rc, void __user *frame,
442                                 int usig)
443 {
444         compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
445         compat_ulong_t retcode;
446         compat_ulong_t spsr = regs->pstate & ~PSR_f;
447         int thumb;
448
449         /* Check if the handler is written for ARM or Thumb */
450         thumb = handler & 1;
451
452         if (thumb)
453                 spsr |= COMPAT_PSR_T_BIT;
454         else
455                 spsr &= ~COMPAT_PSR_T_BIT;
456
457         /* The IT state must be cleared for both ARM and Thumb-2 */
458         spsr &= ~COMPAT_PSR_IT_MASK;
459
460         if (ka->sa.sa_flags & SA_RESTORER) {
461                 retcode = ptr_to_compat(ka->sa.sa_restorer);
462         } else {
463                 /* Set up sigreturn pointer */
464                 unsigned int idx = thumb << 1;
465
466                 if (ka->sa.sa_flags & SA_SIGINFO)
467                         idx += 3;
468
469                 retcode = AARCH32_VECTORS_BASE +
470                           AARCH32_KERN_SIGRET_CODE_OFFSET +
471                           (idx << 2) + thumb;
472         }
473
474         regs->regs[0]   = usig;
475         regs->compat_sp = ptr_to_compat(frame);
476         regs->compat_lr = retcode;
477         regs->pc        = handler;
478         regs->pstate    = spsr;
479 }
480
481 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
482                                  struct pt_regs *regs, sigset_t *set)
483 {
484         struct compat_aux_sigframe __user *aux;
485         int err = 0;
486
487         __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
488         __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
489         __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
490         __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
491         __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
492         __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
493         __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
494         __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
495         __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
496         __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
497         __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
498         __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
499         __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
500         __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
501         __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
502         __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
503         __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
504
505         __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
506         /* set the compat FSR WnR */
507         __put_user_error(!!(current->thread.fault_code & ESR_EL1_WRITE) <<
508                          FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
509         __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
510         __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
511
512         err |= put_sigset_t(&sf->uc.uc_sigmask, set);
513
514         aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
515
516         if (err == 0)
517                 err |= compat_preserve_vfp_context(&aux->vfp);
518         __put_user_error(0, &aux->end_magic, err);
519
520         return err;
521 }
522
523 /*
524  * 32-bit signal handling routines called from signal.c
525  */
526 int compat_setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
527                           sigset_t *set, struct pt_regs *regs)
528 {
529         struct compat_rt_sigframe __user *frame;
530         int err = 0;
531
532         frame = compat_get_sigframe(ka, regs, sizeof(*frame));
533
534         if (!frame)
535                 return 1;
536
537         err |= copy_siginfo_to_user32(&frame->info, info);
538
539         __put_user_error(0, &frame->sig.uc.uc_flags, err);
540         __put_user_error(0, &frame->sig.uc.uc_link, err);
541
542         err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
543
544         err |= compat_setup_sigframe(&frame->sig, regs, set);
545
546         if (err == 0) {
547                 compat_setup_return(regs, ka, frame->sig.retcode, frame, usig);
548                 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
549                 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
550         }
551
552         return err;
553 }
554
555 int compat_setup_frame(int usig, struct k_sigaction *ka, sigset_t *set,
556                        struct pt_regs *regs)
557 {
558         struct compat_sigframe __user *frame;
559         int err = 0;
560
561         frame = compat_get_sigframe(ka, regs, sizeof(*frame));
562
563         if (!frame)
564                 return 1;
565
566         __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
567
568         err |= compat_setup_sigframe(frame, regs, set);
569         if (err == 0)
570                 compat_setup_return(regs, ka, frame->retcode, frame, usig);
571
572         return err;
573 }
574
575 void compat_setup_restart_syscall(struct pt_regs *regs)
576 {
577        regs->regs[7] = __NR_compat_restart_syscall;
578 }