]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/s390/kernel/compat_signal.c
s390/compat: fix setup_frame32
[mirror_ubuntu-bionic-kernel.git] / arch / s390 / kernel / compat_signal.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2000, 2006
4 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
5 * Gerhard Tonn (ton@de.ibm.com)
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds
8 *
9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
10 */
11
12 #include <linux/compat.h>
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/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <linux/tty.h>
25 #include <linux/personality.h>
26 #include <linux/binfmts.h>
27 #include <asm/ucontext.h>
28 #include <linux/uaccess.h>
29 #include <asm/lowcore.h>
30 #include <asm/switch_to.h>
31 #include "compat_linux.h"
32 #include "compat_ptrace.h"
33 #include "entry.h"
34
35 typedef struct
36 {
37 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
38 struct sigcontext32 sc;
39 _sigregs32 sregs;
40 int signo;
41 _sigregs_ext32 sregs_ext;
42 __u16 svc_insn; /* Offset of svc_insn is NOT fixed! */
43 } sigframe32;
44
45 typedef struct
46 {
47 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
48 __u16 svc_insn;
49 compat_siginfo_t info;
50 struct ucontext32 uc;
51 } rt_sigframe32;
52
53 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
54 {
55 int err;
56
57 /* If you change siginfo_t structure, please be sure
58 this code is fixed accordingly.
59 It should never copy any pad contained in the structure
60 to avoid security leaks, but must copy the generic
61 3 ints plus the relevant union member.
62 This routine must convert siginfo from 64bit to 32bit as well
63 at the same time. */
64 err = __put_user(from->si_signo, &to->si_signo);
65 err |= __put_user(from->si_errno, &to->si_errno);
66 err |= __put_user(from->si_code, &to->si_code);
67 if (from->si_code < 0)
68 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
69 else {
70 switch (siginfo_layout(from->si_signo, from->si_code)) {
71 case SIL_RT:
72 err |= __put_user(from->si_int, &to->si_int);
73 /* fallthrough */
74 case SIL_KILL:
75 err |= __put_user(from->si_pid, &to->si_pid);
76 err |= __put_user(from->si_uid, &to->si_uid);
77 break;
78 case SIL_CHLD:
79 err |= __put_user(from->si_pid, &to->si_pid);
80 err |= __put_user(from->si_uid, &to->si_uid);
81 err |= __put_user(from->si_utime, &to->si_utime);
82 err |= __put_user(from->si_stime, &to->si_stime);
83 err |= __put_user(from->si_status, &to->si_status);
84 break;
85 case SIL_FAULT:
86 err |= __put_user((unsigned long) from->si_addr,
87 &to->si_addr);
88 break;
89 case SIL_POLL:
90 err |= __put_user(from->si_band, &to->si_band);
91 err |= __put_user(from->si_fd, &to->si_fd);
92 break;
93 case SIL_TIMER:
94 err |= __put_user(from->si_tid, &to->si_tid);
95 err |= __put_user(from->si_overrun, &to->si_overrun);
96 err |= __put_user(from->si_int, &to->si_int);
97 break;
98 default:
99 break;
100 }
101 }
102 return err ? -EFAULT : 0;
103 }
104
105 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
106 {
107 int err;
108 u32 tmp;
109
110 err = __get_user(to->si_signo, &from->si_signo);
111 err |= __get_user(to->si_errno, &from->si_errno);
112 err |= __get_user(to->si_code, &from->si_code);
113
114 if (to->si_code < 0)
115 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
116 else {
117 switch (siginfo_layout(to->si_signo, to->si_code)) {
118 case SIL_RT:
119 err |= __get_user(to->si_int, &from->si_int);
120 /* fallthrough */
121 case SIL_KILL:
122 err |= __get_user(to->si_pid, &from->si_pid);
123 err |= __get_user(to->si_uid, &from->si_uid);
124 break;
125 case SIL_CHLD:
126 err |= __get_user(to->si_pid, &from->si_pid);
127 err |= __get_user(to->si_uid, &from->si_uid);
128 err |= __get_user(to->si_utime, &from->si_utime);
129 err |= __get_user(to->si_stime, &from->si_stime);
130 err |= __get_user(to->si_status, &from->si_status);
131 break;
132 case SIL_FAULT:
133 err |= __get_user(tmp, &from->si_addr);
134 to->si_addr = (void __force __user *)
135 (u64) (tmp & PSW32_ADDR_INSN);
136 break;
137 case SIL_POLL:
138 err |= __get_user(to->si_band, &from->si_band);
139 err |= __get_user(to->si_fd, &from->si_fd);
140 break;
141 case SIL_TIMER:
142 err |= __get_user(to->si_tid, &from->si_tid);
143 err |= __get_user(to->si_overrun, &from->si_overrun);
144 err |= __get_user(to->si_int, &from->si_int);
145 break;
146 default:
147 break;
148 }
149 }
150 return err ? -EFAULT : 0;
151 }
152
153 /* Store registers needed to create the signal frame */
154 static void store_sigregs(void)
155 {
156 save_access_regs(current->thread.acrs);
157 save_fpu_regs();
158 }
159
160 /* Load registers after signal return */
161 static void load_sigregs(void)
162 {
163 restore_access_regs(current->thread.acrs);
164 }
165
166 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
167 {
168 _sigregs32 user_sregs;
169 int i;
170
171 user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
172 user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
173 user_sregs.regs.psw.mask |= PSW32_USER_BITS;
174 user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
175 (__u32)(regs->psw.mask & PSW_MASK_BA);
176 for (i = 0; i < NUM_GPRS; i++)
177 user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
178 memcpy(&user_sregs.regs.acrs, current->thread.acrs,
179 sizeof(user_sregs.regs.acrs));
180 fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
181 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
182 return -EFAULT;
183 return 0;
184 }
185
186 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
187 {
188 _sigregs32 user_sregs;
189 int i;
190
191 /* Alwys make any pending restarted system call return -EINTR */
192 current->restart_block.fn = do_no_restart_syscall;
193
194 if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
195 return -EFAULT;
196
197 if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
198 return -EINVAL;
199
200 /* Test the floating-point-control word. */
201 if (test_fp_ctl(user_sregs.fpregs.fpc))
202 return -EINVAL;
203
204 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
205 regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
206 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
207 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
208 (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
209 /* Check for invalid user address space control. */
210 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
211 regs->psw.mask = PSW_ASC_PRIMARY |
212 (regs->psw.mask & ~PSW_MASK_ASC);
213 regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
214 for (i = 0; i < NUM_GPRS; i++)
215 regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
216 memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
217 sizeof(current->thread.acrs));
218 fpregs_load((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
219
220 clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
221 return 0;
222 }
223
224 static int save_sigregs_ext32(struct pt_regs *regs,
225 _sigregs_ext32 __user *sregs_ext)
226 {
227 __u32 gprs_high[NUM_GPRS];
228 __u64 vxrs[__NUM_VXRS_LOW];
229 int i;
230
231 /* Save high gprs to signal stack */
232 for (i = 0; i < NUM_GPRS; i++)
233 gprs_high[i] = regs->gprs[i] >> 32;
234 if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
235 sizeof(sregs_ext->gprs_high)))
236 return -EFAULT;
237
238 /* Save vector registers to signal stack */
239 if (MACHINE_HAS_VX) {
240 for (i = 0; i < __NUM_VXRS_LOW; i++)
241 vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
242 if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
243 sizeof(sregs_ext->vxrs_low)) ||
244 __copy_to_user(&sregs_ext->vxrs_high,
245 current->thread.fpu.vxrs + __NUM_VXRS_LOW,
246 sizeof(sregs_ext->vxrs_high)))
247 return -EFAULT;
248 }
249 return 0;
250 }
251
252 static int restore_sigregs_ext32(struct pt_regs *regs,
253 _sigregs_ext32 __user *sregs_ext)
254 {
255 __u32 gprs_high[NUM_GPRS];
256 __u64 vxrs[__NUM_VXRS_LOW];
257 int i;
258
259 /* Restore high gprs from signal stack */
260 if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
261 sizeof(sregs_ext->gprs_high)))
262 return -EFAULT;
263 for (i = 0; i < NUM_GPRS; i++)
264 *(__u32 *)&regs->gprs[i] = gprs_high[i];
265
266 /* Restore vector registers from signal stack */
267 if (MACHINE_HAS_VX) {
268 if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
269 sizeof(sregs_ext->vxrs_low)) ||
270 __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
271 &sregs_ext->vxrs_high,
272 sizeof(sregs_ext->vxrs_high)))
273 return -EFAULT;
274 for (i = 0; i < __NUM_VXRS_LOW; i++)
275 *((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
276 }
277 return 0;
278 }
279
280 COMPAT_SYSCALL_DEFINE0(sigreturn)
281 {
282 struct pt_regs *regs = task_pt_regs(current);
283 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
284 sigset_t set;
285
286 if (get_compat_sigset(&set, (compat_sigset_t __user *)frame->sc.oldmask))
287 goto badframe;
288 set_current_blocked(&set);
289 save_fpu_regs();
290 if (restore_sigregs32(regs, &frame->sregs))
291 goto badframe;
292 if (restore_sigregs_ext32(regs, &frame->sregs_ext))
293 goto badframe;
294 load_sigregs();
295 return regs->gprs[2];
296 badframe:
297 force_sig(SIGSEGV, current);
298 return 0;
299 }
300
301 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
302 {
303 struct pt_regs *regs = task_pt_regs(current);
304 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
305 sigset_t set;
306
307 if (get_compat_sigset(&set, &frame->uc.uc_sigmask))
308 goto badframe;
309 set_current_blocked(&set);
310 if (compat_restore_altstack(&frame->uc.uc_stack))
311 goto badframe;
312 save_fpu_regs();
313 if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
314 goto badframe;
315 if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
316 goto badframe;
317 load_sigregs();
318 return regs->gprs[2];
319 badframe:
320 force_sig(SIGSEGV, current);
321 return 0;
322 }
323
324 /*
325 * Set up a signal frame.
326 */
327
328
329 /*
330 * Determine which stack to use..
331 */
332 static inline void __user *
333 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
334 {
335 unsigned long sp;
336
337 /* Default to using normal stack */
338 sp = (unsigned long) A(regs->gprs[15]);
339
340 /* Overflow on alternate signal stack gives SIGSEGV. */
341 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
342 return (void __user *) -1UL;
343
344 /* This is the X/Open sanctioned signal stack switching. */
345 if (ka->sa.sa_flags & SA_ONSTACK) {
346 if (! sas_ss_flags(sp))
347 sp = current->sas_ss_sp + current->sas_ss_size;
348 }
349
350 return (void __user *)((sp - frame_size) & -8ul);
351 }
352
353 static int setup_frame32(struct ksignal *ksig, sigset_t *set,
354 struct pt_regs *regs)
355 {
356 int sig = ksig->sig;
357 sigframe32 __user *frame;
358 unsigned long restorer;
359 size_t frame_size;
360
361 /*
362 * gprs_high are always present for 31-bit compat tasks.
363 * The space for vector registers is only allocated if
364 * the machine supports it
365 */
366 frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
367 if (!MACHINE_HAS_VX)
368 frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
369 sizeof(frame->sregs_ext.vxrs_high);
370 frame = get_sigframe(&ksig->ka, regs, frame_size);
371 if (frame == (void __user *) -1UL)
372 return -EFAULT;
373
374 /* Set up backchain. */
375 if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
376 return -EFAULT;
377
378 /* Create struct sigcontext32 on the signal stack */
379 if (put_compat_sigset((compat_sigset_t __user *)frame->sc.oldmask,
380 set, sizeof(compat_sigset_t)))
381 return -EFAULT;
382 if (__put_user(ptr_to_compat(&frame->sregs), &frame->sc.sregs))
383 return -EFAULT;
384
385 /* Store registers needed to create the signal frame */
386 store_sigregs();
387
388 /* Create _sigregs32 on the signal stack */
389 if (save_sigregs32(regs, &frame->sregs))
390 return -EFAULT;
391
392 /* Place signal number on stack to allow backtrace from handler. */
393 if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
394 return -EFAULT;
395
396 /* Create _sigregs_ext32 on the signal stack */
397 if (save_sigregs_ext32(regs, &frame->sregs_ext))
398 return -EFAULT;
399
400 /* Set up to return from userspace. If provided, use a stub
401 already in userspace. */
402 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
403 restorer = (unsigned long __force)
404 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
405 } else {
406 /* Signal frames without vectors registers are short ! */
407 __u16 __user *svc = (void __user *) frame + frame_size - 2;
408 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
409 return -EFAULT;
410 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
411 }
412
413 /* Set up registers for signal handler */
414 regs->gprs[14] = restorer;
415 regs->gprs[15] = (__force __u64) frame;
416 /* Force 31 bit amode and default user address space control. */
417 regs->psw.mask = PSW_MASK_BA |
418 (PSW_USER_BITS & PSW_MASK_ASC) |
419 (regs->psw.mask & ~PSW_MASK_ASC);
420 regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
421
422 regs->gprs[2] = sig;
423 regs->gprs[3] = (__force __u64) &frame->sc;
424
425 /* We forgot to include these in the sigcontext.
426 To avoid breaking binary compatibility, they are passed as args. */
427 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
428 sig == SIGTRAP || sig == SIGFPE) {
429 /* set extra registers only for synchronous signals */
430 regs->gprs[4] = regs->int_code & 127;
431 regs->gprs[5] = regs->int_parm_long;
432 regs->gprs[6] = current->thread.last_break;
433 }
434
435 return 0;
436 }
437
438 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
439 struct pt_regs *regs)
440 {
441 rt_sigframe32 __user *frame;
442 unsigned long restorer;
443 size_t frame_size;
444 u32 uc_flags;
445
446 frame_size = sizeof(*frame) -
447 sizeof(frame->uc.uc_mcontext_ext.__reserved);
448 /*
449 * gprs_high are always present for 31-bit compat tasks.
450 * The space for vector registers is only allocated if
451 * the machine supports it
452 */
453 uc_flags = UC_GPRS_HIGH;
454 if (MACHINE_HAS_VX) {
455 uc_flags |= UC_VXRS;
456 } else
457 frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
458 sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
459 frame = get_sigframe(&ksig->ka, regs, frame_size);
460 if (frame == (void __user *) -1UL)
461 return -EFAULT;
462
463 /* Set up backchain. */
464 if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
465 return -EFAULT;
466
467 /* Set up to return from userspace. If provided, use a stub
468 already in userspace. */
469 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
470 restorer = (unsigned long __force)
471 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
472 } else {
473 __u16 __user *svc = &frame->svc_insn;
474 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
475 return -EFAULT;
476 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
477 }
478
479 /* Create siginfo on the signal stack */
480 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
481 return -EFAULT;
482
483 /* Store registers needed to create the signal frame */
484 store_sigregs();
485
486 /* Create ucontext on the signal stack. */
487 if (__put_user(uc_flags, &frame->uc.uc_flags) ||
488 __put_user(0, &frame->uc.uc_link) ||
489 __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
490 save_sigregs32(regs, &frame->uc.uc_mcontext) ||
491 put_compat_sigset(&frame->uc.uc_sigmask, set, sizeof(compat_sigset_t)) ||
492 save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
493 return -EFAULT;
494
495 /* Set up registers for signal handler */
496 regs->gprs[14] = restorer;
497 regs->gprs[15] = (__force __u64) frame;
498 /* Force 31 bit amode and default user address space control. */
499 regs->psw.mask = PSW_MASK_BA |
500 (PSW_USER_BITS & PSW_MASK_ASC) |
501 (regs->psw.mask & ~PSW_MASK_ASC);
502 regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
503
504 regs->gprs[2] = ksig->sig;
505 regs->gprs[3] = (__force __u64) &frame->info;
506 regs->gprs[4] = (__force __u64) &frame->uc;
507 regs->gprs[5] = current->thread.last_break;
508 return 0;
509 }
510
511 /*
512 * OK, we're invoking a handler
513 */
514
515 void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
516 struct pt_regs *regs)
517 {
518 int ret;
519
520 /* Set up the stack frame */
521 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
522 ret = setup_rt_frame32(ksig, oldset, regs);
523 else
524 ret = setup_frame32(ksig, oldset, regs);
525
526 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
527 }
528