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