xref: /linux/arch/arm64/kernel/signal32.c (revision 303bff3637d13f0ecbb280c7cb403470c440a2a6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/kernel/signal.c
4  *
5  * Copyright (C) 1995-2009 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  * Modified by Will Deacon <will.deacon@arm.com>
8  */
9 
10 #include <linux/compat.h>
11 #include <linux/signal.h>
12 #include <linux/syscalls.h>
13 #include <linux/ratelimit.h>
14 
15 #include <asm/esr.h>
16 #include <asm/fpsimd.h>
17 #include <asm/signal32.h>
18 #include <asm/traps.h>
19 #include <linux/uaccess.h>
20 #include <asm/unistd_compat_32.h>
21 #include <asm/vdso.h>
22 
23 struct compat_vfp_sigframe {
24 	compat_ulong_t	magic;
25 	compat_ulong_t	size;
26 	struct compat_user_vfp {
27 		compat_u64	fpregs[32];
28 		compat_ulong_t	fpscr;
29 	} ufp;
30 	struct compat_user_vfp_exc {
31 		compat_ulong_t	fpexc;
32 		compat_ulong_t	fpinst;
33 		compat_ulong_t	fpinst2;
34 	} ufp_exc;
35 } __attribute__((__aligned__(8)));
36 
37 #define VFP_MAGIC		0x56465001
38 #define VFP_STORAGE_SIZE	sizeof(struct compat_vfp_sigframe)
39 
40 #define FSR_WRITE_SHIFT		(11)
41 
42 struct compat_aux_sigframe {
43 	struct compat_vfp_sigframe	vfp;
44 
45 	/* Something that isn't a valid magic number for any coprocessor.  */
46 	unsigned long			end_magic;
47 } __attribute__((__aligned__(8)));
48 
put_sigset_t(compat_sigset_t __user * uset,sigset_t * set)49 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
50 {
51 	compat_sigset_t	cset;
52 
53 	cset.sig[0] = set->sig[0] & 0xffffffffull;
54 	cset.sig[1] = set->sig[0] >> 32;
55 
56 	return copy_to_user(uset, &cset, sizeof(*uset));
57 }
58 
get_sigset_t(sigset_t * set,const compat_sigset_t __user * uset)59 static inline int get_sigset_t(sigset_t *set,
60 			       const compat_sigset_t __user *uset)
61 {
62 	compat_sigset_t s32;
63 
64 	if (copy_from_user(&s32, uset, sizeof(*uset)))
65 		return -EFAULT;
66 
67 	set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
68 	return 0;
69 }
70 
71 /*
72  * VFP save/restore code.
73  */
74 union __fpsimd_vreg {
75 	__uint128_t	raw;
76 	struct {
77 		u64	lo;
78 		u64	hi;
79 	};
80 };
81 
compat_preserve_vfp_context(struct compat_vfp_sigframe __user * frame)82 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
83 {
84 	struct user_fpsimd_state const *fpsimd =
85 		&current->thread.uw.fpsimd_state;
86 	compat_ulong_t magic = VFP_MAGIC;
87 	compat_ulong_t size = VFP_STORAGE_SIZE;
88 	compat_ulong_t fpscr, fpexc;
89 	int i, err = 0;
90 
91 	/*
92 	 * Save the hardware registers to the fpsimd_state structure.
93 	 * Note that this also saves V16-31, which aren't visible
94 	 * in AArch32.
95 	 */
96 	fpsimd_save_and_flush_current_state();
97 
98 	/* Place structure header on the stack */
99 	__put_user_error(magic, &frame->magic, err);
100 	__put_user_error(size, &frame->size, err);
101 
102 	/*
103 	 * Now copy the FP registers. Since the registers are packed,
104 	 * we can copy the prefix we want (V0-V15) as it is.
105 	 */
106 	for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
107 		union __fpsimd_vreg vreg = {
108 			.raw = fpsimd->vregs[i >> 1],
109 		};
110 
111 		__put_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
112 		__put_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
113 	}
114 
115 	/* Create an AArch32 fpscr from the fpsr and the fpcr. */
116 	fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
117 		(fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
118 	__put_user_error(fpscr, &frame->ufp.fpscr, err);
119 
120 	/*
121 	 * The exception register aren't available so we fake up a
122 	 * basic FPEXC and zero everything else.
123 	 */
124 	fpexc = (1 << 30);
125 	__put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
126 	__put_user_error(0, &frame->ufp_exc.fpinst, err);
127 	__put_user_error(0, &frame->ufp_exc.fpinst2, err);
128 
129 	return err ? -EFAULT : 0;
130 }
131 
compat_restore_vfp_context(struct compat_vfp_sigframe __user * frame)132 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
133 {
134 	struct user_fpsimd_state fpsimd;
135 	compat_ulong_t magic = VFP_MAGIC;
136 	compat_ulong_t size = VFP_STORAGE_SIZE;
137 	compat_ulong_t fpscr;
138 	int i, err = 0;
139 
140 	__get_user_error(magic, &frame->magic, err);
141 	__get_user_error(size, &frame->size, err);
142 
143 	if (err)
144 		return -EFAULT;
145 	if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
146 		return -EINVAL;
147 
148 	/* Copy the FP registers into the start of the fpsimd_state. */
149 	for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
150 		union __fpsimd_vreg vreg;
151 
152 		__get_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
153 		__get_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
154 		fpsimd.vregs[i >> 1] = vreg.raw;
155 	}
156 
157 	/* Extract the fpsr and the fpcr from the fpscr */
158 	__get_user_error(fpscr, &frame->ufp.fpscr, err);
159 	fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
160 	fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
161 
162 	if (err)
163 		return -EFAULT;
164 
165 	/*
166 	 * We don't need to touch the exception register, so
167 	 * reload the hardware state.
168 	 */
169 	fpsimd_save_and_flush_current_state();
170 	current->thread.uw.fpsimd_state = fpsimd;
171 
172 	return 0;
173 }
174 
compat_restore_sigframe(struct pt_regs * regs,struct compat_sigframe __user * sf)175 static int compat_restore_sigframe(struct pt_regs *regs,
176 				   struct compat_sigframe __user *sf)
177 {
178 	int err;
179 	sigset_t set;
180 	struct compat_aux_sigframe __user *aux;
181 	unsigned long psr;
182 
183 	err = get_sigset_t(&set, &sf->uc.uc_sigmask);
184 	if (err == 0)
185 		set_current_blocked(&set);
186 
187 	__get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
188 	__get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
189 	__get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
190 	__get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
191 	__get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
192 	__get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
193 	__get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
194 	__get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
195 	__get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
196 	__get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
197 	__get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
198 	__get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
199 	__get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
200 	__get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
201 	__get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
202 	__get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
203 	__get_user_error(psr, &sf->uc.uc_mcontext.arm_cpsr, err);
204 
205 	regs->pstate = compat_psr_to_pstate(psr);
206 
207 	/*
208 	 * Avoid compat_sys_sigreturn() restarting.
209 	 */
210 	forget_syscall(regs);
211 
212 	err |= !valid_user_regs(&regs->user_regs, current);
213 
214 	aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
215 	if (err == 0 && system_supports_fpsimd())
216 		err |= compat_restore_vfp_context(&aux->vfp);
217 
218 	return err;
219 }
220 
COMPAT_SYSCALL_DEFINE0(sigreturn)221 COMPAT_SYSCALL_DEFINE0(sigreturn)
222 {
223 	struct pt_regs *regs = current_pt_regs();
224 	struct compat_sigframe __user *frame;
225 
226 	/* Always make any pending restarted system calls return -EINTR */
227 	current->restart_block.fn = do_no_restart_syscall;
228 
229 	/*
230 	 * Since we stacked the signal on a 64-bit boundary,
231 	 * then 'sp' should be word aligned here.  If it's
232 	 * not, then the user is trying to mess with us.
233 	 */
234 	if (regs->compat_sp & 7)
235 		goto badframe;
236 
237 	frame = (struct compat_sigframe __user *)regs->compat_sp;
238 
239 	if (!access_ok(frame, sizeof (*frame)))
240 		goto badframe;
241 
242 	if (compat_restore_sigframe(regs, frame))
243 		goto badframe;
244 
245 	return regs->regs[0];
246 
247 badframe:
248 	arm64_notify_segfault(regs->compat_sp);
249 	return 0;
250 }
251 
COMPAT_SYSCALL_DEFINE0(rt_sigreturn)252 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
253 {
254 	struct pt_regs *regs = current_pt_regs();
255 	struct compat_rt_sigframe __user *frame;
256 
257 	/* Always make any pending restarted system calls return -EINTR */
258 	current->restart_block.fn = do_no_restart_syscall;
259 
260 	/*
261 	 * Since we stacked the signal on a 64-bit boundary,
262 	 * then 'sp' should be word aligned here.  If it's
263 	 * not, then the user is trying to mess with us.
264 	 */
265 	if (regs->compat_sp & 7)
266 		goto badframe;
267 
268 	frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
269 
270 	if (!access_ok(frame, sizeof (*frame)))
271 		goto badframe;
272 
273 	if (compat_restore_sigframe(regs, &frame->sig))
274 		goto badframe;
275 
276 	if (compat_restore_altstack(&frame->sig.uc.uc_stack))
277 		goto badframe;
278 
279 	return regs->regs[0];
280 
281 badframe:
282 	arm64_notify_segfault(regs->compat_sp);
283 	return 0;
284 }
285 
compat_get_sigframe(struct ksignal * ksig,struct pt_regs * regs,int framesize)286 static void __user *compat_get_sigframe(struct ksignal *ksig,
287 					struct pt_regs *regs,
288 					int framesize)
289 {
290 	compat_ulong_t sp = sigsp(regs->compat_sp, ksig);
291 	void __user *frame;
292 
293 	/*
294 	 * ATPCS B01 mandates 8-byte alignment
295 	 */
296 	frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
297 
298 	/*
299 	 * Check that we can actually write to the signal frame.
300 	 */
301 	if (!access_ok(frame, framesize))
302 		frame = NULL;
303 
304 	return frame;
305 }
306 
compat_setup_return(struct pt_regs * regs,struct k_sigaction * ka,compat_ulong_t __user * rc,void __user * frame,int usig)307 static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
308 				compat_ulong_t __user *rc, void __user *frame,
309 				int usig)
310 {
311 	compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
312 	compat_ulong_t retcode;
313 	compat_ulong_t spsr = regs->pstate & ~(PSR_f | PSR_AA32_E_BIT);
314 	int thumb;
315 
316 	/* Check if the handler is written for ARM or Thumb */
317 	thumb = handler & 1;
318 
319 	if (thumb)
320 		spsr |= PSR_AA32_T_BIT;
321 	else
322 		spsr &= ~PSR_AA32_T_BIT;
323 
324 	/* The IT state must be cleared for both ARM and Thumb-2 */
325 	spsr &= ~PSR_AA32_IT_MASK;
326 
327 	if (ka->sa.sa_flags & SA_RESTORER) {
328 		retcode = ptr_to_compat(ka->sa.sa_restorer);
329 	} else {
330 		/* Set up sigreturn pointer */
331 		unsigned int idx = thumb << 1;
332 
333 		if (ka->sa.sa_flags & SA_SIGINFO)
334 			idx += 3;
335 
336 		retcode = (unsigned long)current->mm->context.sigpage +
337 			  (idx << 2) + thumb;
338 	}
339 
340 	regs->regs[0]	= usig;
341 	regs->compat_sp	= ptr_to_compat(frame);
342 	regs->compat_lr	= retcode;
343 	regs->pc	= handler;
344 	regs->pstate	= spsr;
345 }
346 
compat_setup_sigframe(struct compat_sigframe __user * sf,struct pt_regs * regs,sigset_t * set)347 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
348 				 struct pt_regs *regs, sigset_t *set)
349 {
350 	struct compat_aux_sigframe __user *aux;
351 	unsigned long psr = pstate_to_compat_psr(regs->pstate);
352 	int err = 0;
353 
354 	__put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
355 	__put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
356 	__put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
357 	__put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
358 	__put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
359 	__put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
360 	__put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
361 	__put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
362 	__put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
363 	__put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
364 	__put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
365 	__put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
366 	__put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
367 	__put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
368 	__put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
369 	__put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
370 	__put_user_error(psr, &sf->uc.uc_mcontext.arm_cpsr, err);
371 
372 	__put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
373 	/* set the compat FSR WnR */
374 	__put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) <<
375 			 FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
376 	__put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
377 	__put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
378 
379 	err |= put_sigset_t(&sf->uc.uc_sigmask, set);
380 
381 	aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
382 
383 	if (err == 0 && system_supports_fpsimd())
384 		err |= compat_preserve_vfp_context(&aux->vfp);
385 	__put_user_error(0, &aux->end_magic, err);
386 
387 	return err;
388 }
389 
390 /*
391  * 32-bit signal handling routines called from signal.c
392  */
compat_setup_rt_frame(int usig,struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)393 int compat_setup_rt_frame(int usig, struct ksignal *ksig,
394 			  sigset_t *set, struct pt_regs *regs)
395 {
396 	struct compat_rt_sigframe __user *frame;
397 	int err = 0;
398 
399 	frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
400 
401 	if (!frame)
402 		return 1;
403 
404 	err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
405 
406 	__put_user_error(0, &frame->sig.uc.uc_flags, err);
407 	__put_user_error(0, &frame->sig.uc.uc_link, err);
408 
409 	err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
410 
411 	err |= compat_setup_sigframe(&frame->sig, regs, set);
412 
413 	if (err == 0) {
414 		compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
415 		regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
416 		regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
417 	}
418 
419 	return err;
420 }
421 
compat_setup_frame(int usig,struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)422 int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
423 		       struct pt_regs *regs)
424 {
425 	struct compat_sigframe __user *frame;
426 	int err = 0;
427 
428 	frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
429 
430 	if (!frame)
431 		return 1;
432 
433 	__put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
434 
435 	err |= compat_setup_sigframe(frame, regs, set);
436 	if (err == 0)
437 		compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
438 
439 	return err;
440 }
441 
compat_setup_restart_syscall(struct pt_regs * regs)442 void compat_setup_restart_syscall(struct pt_regs *regs)
443 {
444        regs->regs[7] = __NR_compat32_restart_syscall;
445 }
446 
447 /*
448  * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
449  * changes likely come with new fields that should be added below.
450  */
451 static_assert(NSIGILL	== 11);
452 static_assert(NSIGFPE	== 15);
453 static_assert(NSIGSEGV	== 10);
454 static_assert(NSIGBUS	== 5);
455 static_assert(NSIGTRAP	== 6);
456 static_assert(NSIGCHLD	== 6);
457 static_assert(NSIGSYS	== 2);
458 static_assert(sizeof(compat_siginfo_t) == 128);
459 static_assert(__alignof__(compat_siginfo_t) == 4);
460 static_assert(offsetof(compat_siginfo_t, si_signo)	== 0x00);
461 static_assert(offsetof(compat_siginfo_t, si_errno)	== 0x04);
462 static_assert(offsetof(compat_siginfo_t, si_code)	== 0x08);
463 static_assert(offsetof(compat_siginfo_t, si_pid)	== 0x0c);
464 static_assert(offsetof(compat_siginfo_t, si_uid)	== 0x10);
465 static_assert(offsetof(compat_siginfo_t, si_tid)	== 0x0c);
466 static_assert(offsetof(compat_siginfo_t, si_overrun)	== 0x10);
467 static_assert(offsetof(compat_siginfo_t, si_status)	== 0x14);
468 static_assert(offsetof(compat_siginfo_t, si_utime)	== 0x18);
469 static_assert(offsetof(compat_siginfo_t, si_stime)	== 0x1c);
470 static_assert(offsetof(compat_siginfo_t, si_value)	== 0x14);
471 static_assert(offsetof(compat_siginfo_t, si_int)	== 0x14);
472 static_assert(offsetof(compat_siginfo_t, si_ptr)	== 0x14);
473 static_assert(offsetof(compat_siginfo_t, si_addr)	== 0x0c);
474 static_assert(offsetof(compat_siginfo_t, si_addr_lsb)	== 0x10);
475 static_assert(offsetof(compat_siginfo_t, si_lower)	== 0x14);
476 static_assert(offsetof(compat_siginfo_t, si_upper)	== 0x18);
477 static_assert(offsetof(compat_siginfo_t, si_pkey)	== 0x14);
478 static_assert(offsetof(compat_siginfo_t, si_perf_data)	== 0x10);
479 static_assert(offsetof(compat_siginfo_t, si_perf_type)	== 0x14);
480 static_assert(offsetof(compat_siginfo_t, si_perf_flags)	== 0x18);
481 static_assert(offsetof(compat_siginfo_t, si_band)	== 0x0c);
482 static_assert(offsetof(compat_siginfo_t, si_fd)		== 0x10);
483 static_assert(offsetof(compat_siginfo_t, si_call_addr)	== 0x0c);
484 static_assert(offsetof(compat_siginfo_t, si_syscall)	== 0x10);
485 static_assert(offsetof(compat_siginfo_t, si_arch)	== 0x14);
486