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 <linux/uaccess.h> 30 #include <asm/unistd.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(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 (siginfo_layout(from->si_signo, from->si_code)) { 150 case SIL_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 SIL_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(from->si_int, &to->si_int); 158 break; 159 case SIL_POLL: 160 err |= __put_user(from->si_band, &to->si_band); 161 err |= __put_user(from->si_fd, &to->si_fd); 162 break; 163 case SIL_FAULT: 164 err |= __put_user((compat_uptr_t)(unsigned long)from->si_addr, 165 &to->si_addr); 166 #ifdef BUS_MCEERR_AO 167 /* 168 * Other callers might not initialize the si_lsb field, 169 * so check explicitly for the right codes here. 170 */ 171 if (from->si_signo == SIGBUS && 172 (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 SIL_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 SIL_RT: 184 err |= __put_user(from->si_pid, &to->si_pid); 185 err |= __put_user(from->si_uid, &to->si_uid); 186 err |= __put_user(from->si_int, &to->si_int); 187 break; 188 case SIL_SYS: 189 err |= __put_user((compat_uptr_t)(unsigned long) 190 from->si_call_addr, &to->si_call_addr); 191 err |= __put_user(from->si_syscall, &to->si_syscall); 192 err |= __put_user(from->si_arch, &to->si_arch); 193 break; 194 } 195 return err; 196 } 197 198 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from) 199 { 200 if (copy_from_user(to, from, __ARCH_SI_PREAMBLE_SIZE) || 201 copy_from_user(to->_sifields._pad, 202 from->_sifields._pad, SI_PAD_SIZE)) 203 return -EFAULT; 204 205 return 0; 206 } 207 208 /* 209 * VFP save/restore code. 210 * 211 * We have to be careful with endianness, since the fpsimd context-switch 212 * code operates on 128-bit (Q) register values whereas the compat ABI 213 * uses an array of 64-bit (D) registers. Consequently, we need to swap 214 * the two halves of each Q register when running on a big-endian CPU. 215 */ 216 union __fpsimd_vreg { 217 __uint128_t raw; 218 struct { 219 #ifdef __AARCH64EB__ 220 u64 hi; 221 u64 lo; 222 #else 223 u64 lo; 224 u64 hi; 225 #endif 226 }; 227 }; 228 229 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame) 230 { 231 struct fpsimd_state *fpsimd = ¤t->thread.fpsimd_state; 232 compat_ulong_t magic = VFP_MAGIC; 233 compat_ulong_t size = VFP_STORAGE_SIZE; 234 compat_ulong_t fpscr, fpexc; 235 int i, err = 0; 236 237 /* 238 * Save the hardware registers to the fpsimd_state structure. 239 * Note that this also saves V16-31, which aren't visible 240 * in AArch32. 241 */ 242 fpsimd_signal_preserve_current_state(); 243 244 /* Place structure header on the stack */ 245 __put_user_error(magic, &frame->magic, err); 246 __put_user_error(size, &frame->size, err); 247 248 /* 249 * Now copy the FP registers. Since the registers are packed, 250 * we can copy the prefix we want (V0-V15) as it is. 251 */ 252 for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) { 253 union __fpsimd_vreg vreg = { 254 .raw = fpsimd->vregs[i >> 1], 255 }; 256 257 __put_user_error(vreg.lo, &frame->ufp.fpregs[i], err); 258 __put_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err); 259 } 260 261 /* Create an AArch32 fpscr from the fpsr and the fpcr. */ 262 fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) | 263 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK); 264 __put_user_error(fpscr, &frame->ufp.fpscr, err); 265 266 /* 267 * The exception register aren't available so we fake up a 268 * basic FPEXC and zero everything else. 269 */ 270 fpexc = (1 << 30); 271 __put_user_error(fpexc, &frame->ufp_exc.fpexc, err); 272 __put_user_error(0, &frame->ufp_exc.fpinst, err); 273 __put_user_error(0, &frame->ufp_exc.fpinst2, err); 274 275 return err ? -EFAULT : 0; 276 } 277 278 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame) 279 { 280 struct fpsimd_state fpsimd; 281 compat_ulong_t magic = VFP_MAGIC; 282 compat_ulong_t size = VFP_STORAGE_SIZE; 283 compat_ulong_t fpscr; 284 int i, err = 0; 285 286 __get_user_error(magic, &frame->magic, err); 287 __get_user_error(size, &frame->size, err); 288 289 if (err) 290 return -EFAULT; 291 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE) 292 return -EINVAL; 293 294 /* Copy the FP registers into the start of the fpsimd_state. */ 295 for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) { 296 union __fpsimd_vreg vreg; 297 298 __get_user_error(vreg.lo, &frame->ufp.fpregs[i], err); 299 __get_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err); 300 fpsimd.vregs[i >> 1] = vreg.raw; 301 } 302 303 /* Extract the fpsr and the fpcr from the fpscr */ 304 __get_user_error(fpscr, &frame->ufp.fpscr, err); 305 fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK; 306 fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK; 307 308 /* 309 * We don't need to touch the exception register, so 310 * reload the hardware state. 311 */ 312 if (!err) 313 fpsimd_update_current_state(&fpsimd); 314 315 return err ? -EFAULT : 0; 316 } 317 318 static int compat_restore_sigframe(struct pt_regs *regs, 319 struct compat_sigframe __user *sf) 320 { 321 int err; 322 sigset_t set; 323 struct compat_aux_sigframe __user *aux; 324 325 err = get_sigset_t(&set, &sf->uc.uc_sigmask); 326 if (err == 0) { 327 sigdelsetmask(&set, ~_BLOCKABLE); 328 set_current_blocked(&set); 329 } 330 331 __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err); 332 __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err); 333 __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err); 334 __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err); 335 __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err); 336 __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err); 337 __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err); 338 __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err); 339 __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err); 340 __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err); 341 __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err); 342 __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err); 343 __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err); 344 __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err); 345 __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err); 346 __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err); 347 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err); 348 349 /* 350 * Avoid compat_sys_sigreturn() restarting. 351 */ 352 forget_syscall(regs); 353 354 err |= !valid_user_regs(®s->user_regs, current); 355 356 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace; 357 if (err == 0) 358 err |= compat_restore_vfp_context(&aux->vfp); 359 360 return err; 361 } 362 363 asmlinkage int compat_sys_sigreturn(struct pt_regs *regs) 364 { 365 struct compat_sigframe __user *frame; 366 367 /* Always make any pending restarted system calls return -EINTR */ 368 current->restart_block.fn = do_no_restart_syscall; 369 370 /* 371 * Since we stacked the signal on a 64-bit boundary, 372 * then 'sp' should be word aligned here. If it's 373 * not, then the user is trying to mess with us. 374 */ 375 if (regs->compat_sp & 7) 376 goto badframe; 377 378 frame = (struct compat_sigframe __user *)regs->compat_sp; 379 380 if (!access_ok(VERIFY_READ, frame, sizeof (*frame))) 381 goto badframe; 382 383 if (compat_restore_sigframe(regs, frame)) 384 goto badframe; 385 386 return regs->regs[0]; 387 388 badframe: 389 if (show_unhandled_signals) 390 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n", 391 current->comm, task_pid_nr(current), __func__, 392 regs->pc, regs->compat_sp); 393 force_sig(SIGSEGV, current); 394 return 0; 395 } 396 397 asmlinkage int compat_sys_rt_sigreturn(struct pt_regs *regs) 398 { 399 struct compat_rt_sigframe __user *frame; 400 401 /* Always make any pending restarted system calls return -EINTR */ 402 current->restart_block.fn = do_no_restart_syscall; 403 404 /* 405 * Since we stacked the signal on a 64-bit boundary, 406 * then 'sp' should be word aligned here. If it's 407 * not, then the user is trying to mess with us. 408 */ 409 if (regs->compat_sp & 7) 410 goto badframe; 411 412 frame = (struct compat_rt_sigframe __user *)regs->compat_sp; 413 414 if (!access_ok(VERIFY_READ, frame, sizeof (*frame))) 415 goto badframe; 416 417 if (compat_restore_sigframe(regs, &frame->sig)) 418 goto badframe; 419 420 if (compat_restore_altstack(&frame->sig.uc.uc_stack)) 421 goto badframe; 422 423 return regs->regs[0]; 424 425 badframe: 426 if (show_unhandled_signals) 427 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n", 428 current->comm, task_pid_nr(current), __func__, 429 regs->pc, regs->compat_sp); 430 force_sig(SIGSEGV, current); 431 return 0; 432 } 433 434 static void __user *compat_get_sigframe(struct ksignal *ksig, 435 struct pt_regs *regs, 436 int framesize) 437 { 438 compat_ulong_t sp = sigsp(regs->compat_sp, ksig); 439 void __user *frame; 440 441 /* 442 * ATPCS B01 mandates 8-byte alignment 443 */ 444 frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7)); 445 446 /* 447 * Check that we can actually write to the signal frame. 448 */ 449 if (!access_ok(VERIFY_WRITE, frame, framesize)) 450 frame = NULL; 451 452 return frame; 453 } 454 455 static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka, 456 compat_ulong_t __user *rc, void __user *frame, 457 int usig) 458 { 459 compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler); 460 compat_ulong_t retcode; 461 compat_ulong_t spsr = regs->pstate & ~(PSR_f | COMPAT_PSR_E_BIT); 462 int thumb; 463 464 /* Check if the handler is written for ARM or Thumb */ 465 thumb = handler & 1; 466 467 if (thumb) 468 spsr |= COMPAT_PSR_T_BIT; 469 else 470 spsr &= ~COMPAT_PSR_T_BIT; 471 472 /* The IT state must be cleared for both ARM and Thumb-2 */ 473 spsr &= ~COMPAT_PSR_IT_MASK; 474 475 /* Restore the original endianness */ 476 spsr |= COMPAT_PSR_ENDSTATE; 477 478 if (ka->sa.sa_flags & SA_RESTORER) { 479 retcode = ptr_to_compat(ka->sa.sa_restorer); 480 } else { 481 /* Set up sigreturn pointer */ 482 unsigned int idx = thumb << 1; 483 484 if (ka->sa.sa_flags & SA_SIGINFO) 485 idx += 3; 486 487 retcode = AARCH32_VECTORS_BASE + 488 AARCH32_KERN_SIGRET_CODE_OFFSET + 489 (idx << 2) + thumb; 490 } 491 492 regs->regs[0] = usig; 493 regs->compat_sp = ptr_to_compat(frame); 494 regs->compat_lr = retcode; 495 regs->pc = handler; 496 regs->pstate = spsr; 497 } 498 499 static int compat_setup_sigframe(struct compat_sigframe __user *sf, 500 struct pt_regs *regs, sigset_t *set) 501 { 502 struct compat_aux_sigframe __user *aux; 503 int err = 0; 504 505 __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err); 506 __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err); 507 __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err); 508 __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err); 509 __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err); 510 __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err); 511 __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err); 512 __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err); 513 __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err); 514 __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err); 515 __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err); 516 __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err); 517 __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err); 518 __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err); 519 __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err); 520 __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err); 521 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err); 522 523 __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err); 524 /* set the compat FSR WnR */ 525 __put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) << 526 FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err); 527 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err); 528 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err); 529 530 err |= put_sigset_t(&sf->uc.uc_sigmask, set); 531 532 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace; 533 534 if (err == 0) 535 err |= compat_preserve_vfp_context(&aux->vfp); 536 __put_user_error(0, &aux->end_magic, err); 537 538 return err; 539 } 540 541 /* 542 * 32-bit signal handling routines called from signal.c 543 */ 544 int compat_setup_rt_frame(int usig, struct ksignal *ksig, 545 sigset_t *set, struct pt_regs *regs) 546 { 547 struct compat_rt_sigframe __user *frame; 548 int err = 0; 549 550 frame = compat_get_sigframe(ksig, regs, sizeof(*frame)); 551 552 if (!frame) 553 return 1; 554 555 err |= copy_siginfo_to_user32(&frame->info, &ksig->info); 556 557 __put_user_error(0, &frame->sig.uc.uc_flags, err); 558 __put_user_error(0, &frame->sig.uc.uc_link, err); 559 560 err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp); 561 562 err |= compat_setup_sigframe(&frame->sig, regs, set); 563 564 if (err == 0) { 565 compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig); 566 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info; 567 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc; 568 } 569 570 return err; 571 } 572 573 int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set, 574 struct pt_regs *regs) 575 { 576 struct compat_sigframe __user *frame; 577 int err = 0; 578 579 frame = compat_get_sigframe(ksig, regs, sizeof(*frame)); 580 581 if (!frame) 582 return 1; 583 584 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err); 585 586 err |= compat_setup_sigframe(frame, regs, set); 587 if (err == 0) 588 compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig); 589 590 return err; 591 } 592 593 void compat_setup_restart_syscall(struct pt_regs *regs) 594 { 595 regs->regs[7] = __NR_compat_restart_syscall; 596 } 597