David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/kernel/signal.c |
| 4 | * |
| 5 | * Copyright (C) 1995-2009 Russell King |
| 6 | * Copyright (C) 2012 ARM Ltd. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/cache.h> |
| 10 | #include <linux/compat.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/signal.h> |
| 14 | #include <linux/personality.h> |
| 15 | #include <linux/freezer.h> |
| 16 | #include <linux/stddef.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/sizes.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/tracehook.h> |
| 21 | #include <linux/ratelimit.h> |
| 22 | #include <linux/syscalls.h> |
| 23 | |
| 24 | #include <asm/daifflags.h> |
| 25 | #include <asm/debug-monitors.h> |
| 26 | #include <asm/elf.h> |
| 27 | #include <asm/cacheflush.h> |
| 28 | #include <asm/ucontext.h> |
| 29 | #include <asm/unistd.h> |
| 30 | #include <asm/fpsimd.h> |
| 31 | #include <asm/ptrace.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 32 | #include <asm/syscall.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 33 | #include <asm/signal32.h> |
| 34 | #include <asm/traps.h> |
| 35 | #include <asm/vdso.h> |
| 36 | |
| 37 | /* |
| 38 | * Do a signal return; undo the signal stack. These are aligned to 128-bit. |
| 39 | */ |
| 40 | struct rt_sigframe { |
| 41 | struct siginfo info; |
| 42 | struct ucontext uc; |
| 43 | }; |
| 44 | |
| 45 | struct frame_record { |
| 46 | u64 fp; |
| 47 | u64 lr; |
| 48 | }; |
| 49 | |
| 50 | struct rt_sigframe_user_layout { |
| 51 | struct rt_sigframe __user *sigframe; |
| 52 | struct frame_record __user *next_frame; |
| 53 | |
| 54 | unsigned long size; /* size of allocated sigframe data */ |
| 55 | unsigned long limit; /* largest allowed size */ |
| 56 | |
| 57 | unsigned long fpsimd_offset; |
| 58 | unsigned long esr_offset; |
| 59 | unsigned long sve_offset; |
| 60 | unsigned long extra_offset; |
| 61 | unsigned long end_offset; |
| 62 | }; |
| 63 | |
| 64 | #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16) |
| 65 | #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16) |
| 66 | #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16) |
| 67 | |
| 68 | static void init_user_layout(struct rt_sigframe_user_layout *user) |
| 69 | { |
| 70 | const size_t reserved_size = |
| 71 | sizeof(user->sigframe->uc.uc_mcontext.__reserved); |
| 72 | |
| 73 | memset(user, 0, sizeof(*user)); |
| 74 | user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved); |
| 75 | |
| 76 | user->limit = user->size + reserved_size; |
| 77 | |
| 78 | user->limit -= TERMINATOR_SIZE; |
| 79 | user->limit -= EXTRA_CONTEXT_SIZE; |
| 80 | /* Reserve space for extension and terminator ^ */ |
| 81 | } |
| 82 | |
| 83 | static size_t sigframe_size(struct rt_sigframe_user_layout const *user) |
| 84 | { |
| 85 | return round_up(max(user->size, sizeof(struct rt_sigframe)), 16); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Sanity limit on the approximate maximum size of signal frame we'll |
| 90 | * try to generate. Stack alignment padding and the frame record are |
| 91 | * not taken into account. This limit is not a guarantee and is |
| 92 | * NOT ABI. |
| 93 | */ |
| 94 | #define SIGFRAME_MAXSZ SZ_64K |
| 95 | |
| 96 | static int __sigframe_alloc(struct rt_sigframe_user_layout *user, |
| 97 | unsigned long *offset, size_t size, bool extend) |
| 98 | { |
| 99 | size_t padded_size = round_up(size, 16); |
| 100 | |
| 101 | if (padded_size > user->limit - user->size && |
| 102 | !user->extra_offset && |
| 103 | extend) { |
| 104 | int ret; |
| 105 | |
| 106 | user->limit += EXTRA_CONTEXT_SIZE; |
| 107 | ret = __sigframe_alloc(user, &user->extra_offset, |
| 108 | sizeof(struct extra_context), false); |
| 109 | if (ret) { |
| 110 | user->limit -= EXTRA_CONTEXT_SIZE; |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | /* Reserve space for the __reserved[] terminator */ |
| 115 | user->size += TERMINATOR_SIZE; |
| 116 | |
| 117 | /* |
| 118 | * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for |
| 119 | * the terminator: |
| 120 | */ |
| 121 | user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE; |
| 122 | } |
| 123 | |
| 124 | /* Still not enough space? Bad luck! */ |
| 125 | if (padded_size > user->limit - user->size) |
| 126 | return -ENOMEM; |
| 127 | |
| 128 | *offset = user->size; |
| 129 | user->size += padded_size; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Allocate space for an optional record of <size> bytes in the user |
| 136 | * signal frame. The offset from the signal frame base address to the |
| 137 | * allocated block is assigned to *offset. |
| 138 | */ |
| 139 | static int sigframe_alloc(struct rt_sigframe_user_layout *user, |
| 140 | unsigned long *offset, size_t size) |
| 141 | { |
| 142 | return __sigframe_alloc(user, offset, size, true); |
| 143 | } |
| 144 | |
| 145 | /* Allocate the null terminator record and prevent further allocations */ |
| 146 | static int sigframe_alloc_end(struct rt_sigframe_user_layout *user) |
| 147 | { |
| 148 | int ret; |
| 149 | |
| 150 | /* Un-reserve the space reserved for the terminator: */ |
| 151 | user->limit += TERMINATOR_SIZE; |
| 152 | |
| 153 | ret = sigframe_alloc(user, &user->end_offset, |
| 154 | sizeof(struct _aarch64_ctx)); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | |
| 158 | /* Prevent further allocation: */ |
| 159 | user->limit = user->size; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static void __user *apply_user_offset( |
| 164 | struct rt_sigframe_user_layout const *user, unsigned long offset) |
| 165 | { |
| 166 | char __user *base = (char __user *)user->sigframe; |
| 167 | |
| 168 | return base + offset; |
| 169 | } |
| 170 | |
| 171 | static int preserve_fpsimd_context(struct fpsimd_context __user *ctx) |
| 172 | { |
| 173 | struct user_fpsimd_state const *fpsimd = |
| 174 | ¤t->thread.uw.fpsimd_state; |
| 175 | int err; |
| 176 | |
| 177 | /* copy the FP and status/control registers */ |
| 178 | err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs)); |
| 179 | __put_user_error(fpsimd->fpsr, &ctx->fpsr, err); |
| 180 | __put_user_error(fpsimd->fpcr, &ctx->fpcr, err); |
| 181 | |
| 182 | /* copy the magic/size information */ |
| 183 | __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err); |
| 184 | __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err); |
| 185 | |
| 186 | return err ? -EFAULT : 0; |
| 187 | } |
| 188 | |
| 189 | static int restore_fpsimd_context(struct fpsimd_context __user *ctx) |
| 190 | { |
| 191 | struct user_fpsimd_state fpsimd; |
| 192 | __u32 magic, size; |
| 193 | int err = 0; |
| 194 | |
| 195 | /* check the magic/size information */ |
| 196 | __get_user_error(magic, &ctx->head.magic, err); |
| 197 | __get_user_error(size, &ctx->head.size, err); |
| 198 | if (err) |
| 199 | return -EFAULT; |
| 200 | if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context)) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | /* copy the FP and status/control registers */ |
| 204 | err = __copy_from_user(fpsimd.vregs, ctx->vregs, |
| 205 | sizeof(fpsimd.vregs)); |
| 206 | __get_user_error(fpsimd.fpsr, &ctx->fpsr, err); |
| 207 | __get_user_error(fpsimd.fpcr, &ctx->fpcr, err); |
| 208 | |
| 209 | clear_thread_flag(TIF_SVE); |
| 210 | |
| 211 | /* load the hardware registers from the fpsimd_state structure */ |
| 212 | if (!err) |
| 213 | fpsimd_update_current_state(&fpsimd); |
| 214 | |
| 215 | return err ? -EFAULT : 0; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | struct user_ctxs { |
| 220 | struct fpsimd_context __user *fpsimd; |
| 221 | struct sve_context __user *sve; |
| 222 | }; |
| 223 | |
| 224 | #ifdef CONFIG_ARM64_SVE |
| 225 | |
| 226 | static int preserve_sve_context(struct sve_context __user *ctx) |
| 227 | { |
| 228 | int err = 0; |
| 229 | u16 reserved[ARRAY_SIZE(ctx->__reserved)]; |
| 230 | unsigned int vl = current->thread.sve_vl; |
| 231 | unsigned int vq = 0; |
| 232 | |
| 233 | if (test_thread_flag(TIF_SVE)) |
| 234 | vq = sve_vq_from_vl(vl); |
| 235 | |
| 236 | memset(reserved, 0, sizeof(reserved)); |
| 237 | |
| 238 | __put_user_error(SVE_MAGIC, &ctx->head.magic, err); |
| 239 | __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16), |
| 240 | &ctx->head.size, err); |
| 241 | __put_user_error(vl, &ctx->vl, err); |
| 242 | BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved)); |
| 243 | err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved)); |
| 244 | |
| 245 | if (vq) { |
| 246 | /* |
| 247 | * This assumes that the SVE state has already been saved to |
| 248 | * the task struct by calling preserve_fpsimd_context(). |
| 249 | */ |
| 250 | err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET, |
| 251 | current->thread.sve_state, |
| 252 | SVE_SIG_REGS_SIZE(vq)); |
| 253 | } |
| 254 | |
| 255 | return err ? -EFAULT : 0; |
| 256 | } |
| 257 | |
| 258 | static int restore_sve_fpsimd_context(struct user_ctxs *user) |
| 259 | { |
| 260 | int err; |
| 261 | unsigned int vq; |
| 262 | struct user_fpsimd_state fpsimd; |
| 263 | struct sve_context sve; |
| 264 | |
| 265 | if (__copy_from_user(&sve, user->sve, sizeof(sve))) |
| 266 | return -EFAULT; |
| 267 | |
| 268 | if (sve.vl != current->thread.sve_vl) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | if (sve.head.size <= sizeof(*user->sve)) { |
| 272 | clear_thread_flag(TIF_SVE); |
| 273 | goto fpsimd_only; |
| 274 | } |
| 275 | |
| 276 | vq = sve_vq_from_vl(sve.vl); |
| 277 | |
| 278 | if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq)) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | /* |
| 282 | * Careful: we are about __copy_from_user() directly into |
| 283 | * thread.sve_state with preemption enabled, so protection is |
| 284 | * needed to prevent a racing context switch from writing stale |
| 285 | * registers back over the new data. |
| 286 | */ |
| 287 | |
| 288 | fpsimd_flush_task_state(current); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 289 | /* From now, fpsimd_thread_switch() won't touch thread.sve_state */ |
| 290 | |
| 291 | sve_alloc(current); |
| 292 | err = __copy_from_user(current->thread.sve_state, |
| 293 | (char __user const *)user->sve + |
| 294 | SVE_SIG_REGS_OFFSET, |
| 295 | SVE_SIG_REGS_SIZE(vq)); |
| 296 | if (err) |
| 297 | return -EFAULT; |
| 298 | |
| 299 | set_thread_flag(TIF_SVE); |
| 300 | |
| 301 | fpsimd_only: |
| 302 | /* copy the FP and status/control registers */ |
| 303 | /* restore_sigframe() already checked that user->fpsimd != NULL. */ |
| 304 | err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs, |
| 305 | sizeof(fpsimd.vregs)); |
| 306 | __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err); |
| 307 | __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err); |
| 308 | |
| 309 | /* load the hardware registers from the fpsimd_state structure */ |
| 310 | if (!err) |
| 311 | fpsimd_update_current_state(&fpsimd); |
| 312 | |
| 313 | return err ? -EFAULT : 0; |
| 314 | } |
| 315 | |
| 316 | #else /* ! CONFIG_ARM64_SVE */ |
| 317 | |
| 318 | /* Turn any non-optimised out attempts to use these into a link error: */ |
| 319 | extern int preserve_sve_context(void __user *ctx); |
| 320 | extern int restore_sve_fpsimd_context(struct user_ctxs *user); |
| 321 | |
| 322 | #endif /* ! CONFIG_ARM64_SVE */ |
| 323 | |
| 324 | |
| 325 | static int parse_user_sigframe(struct user_ctxs *user, |
| 326 | struct rt_sigframe __user *sf) |
| 327 | { |
| 328 | struct sigcontext __user *const sc = &sf->uc.uc_mcontext; |
| 329 | struct _aarch64_ctx __user *head; |
| 330 | char __user *base = (char __user *)&sc->__reserved; |
| 331 | size_t offset = 0; |
| 332 | size_t limit = sizeof(sc->__reserved); |
| 333 | bool have_extra_context = false; |
| 334 | char const __user *const sfp = (char const __user *)sf; |
| 335 | |
| 336 | user->fpsimd = NULL; |
| 337 | user->sve = NULL; |
| 338 | |
| 339 | if (!IS_ALIGNED((unsigned long)base, 16)) |
| 340 | goto invalid; |
| 341 | |
| 342 | while (1) { |
| 343 | int err = 0; |
| 344 | u32 magic, size; |
| 345 | char const __user *userp; |
| 346 | struct extra_context const __user *extra; |
| 347 | u64 extra_datap; |
| 348 | u32 extra_size; |
| 349 | struct _aarch64_ctx const __user *end; |
| 350 | u32 end_magic, end_size; |
| 351 | |
| 352 | if (limit - offset < sizeof(*head)) |
| 353 | goto invalid; |
| 354 | |
| 355 | if (!IS_ALIGNED(offset, 16)) |
| 356 | goto invalid; |
| 357 | |
| 358 | head = (struct _aarch64_ctx __user *)(base + offset); |
| 359 | __get_user_error(magic, &head->magic, err); |
| 360 | __get_user_error(size, &head->size, err); |
| 361 | if (err) |
| 362 | return err; |
| 363 | |
| 364 | if (limit - offset < size) |
| 365 | goto invalid; |
| 366 | |
| 367 | switch (magic) { |
| 368 | case 0: |
| 369 | if (size) |
| 370 | goto invalid; |
| 371 | |
| 372 | goto done; |
| 373 | |
| 374 | case FPSIMD_MAGIC: |
| 375 | if (user->fpsimd) |
| 376 | goto invalid; |
| 377 | |
| 378 | if (size < sizeof(*user->fpsimd)) |
| 379 | goto invalid; |
| 380 | |
| 381 | user->fpsimd = (struct fpsimd_context __user *)head; |
| 382 | break; |
| 383 | |
| 384 | case ESR_MAGIC: |
| 385 | /* ignore */ |
| 386 | break; |
| 387 | |
| 388 | case SVE_MAGIC: |
| 389 | if (!system_supports_sve()) |
| 390 | goto invalid; |
| 391 | |
| 392 | if (user->sve) |
| 393 | goto invalid; |
| 394 | |
| 395 | if (size < sizeof(*user->sve)) |
| 396 | goto invalid; |
| 397 | |
| 398 | user->sve = (struct sve_context __user *)head; |
| 399 | break; |
| 400 | |
| 401 | case EXTRA_MAGIC: |
| 402 | if (have_extra_context) |
| 403 | goto invalid; |
| 404 | |
| 405 | if (size < sizeof(*extra)) |
| 406 | goto invalid; |
| 407 | |
| 408 | userp = (char const __user *)head; |
| 409 | |
| 410 | extra = (struct extra_context const __user *)userp; |
| 411 | userp += size; |
| 412 | |
| 413 | __get_user_error(extra_datap, &extra->datap, err); |
| 414 | __get_user_error(extra_size, &extra->size, err); |
| 415 | if (err) |
| 416 | return err; |
| 417 | |
| 418 | /* Check for the dummy terminator in __reserved[]: */ |
| 419 | |
| 420 | if (limit - offset - size < TERMINATOR_SIZE) |
| 421 | goto invalid; |
| 422 | |
| 423 | end = (struct _aarch64_ctx const __user *)userp; |
| 424 | userp += TERMINATOR_SIZE; |
| 425 | |
| 426 | __get_user_error(end_magic, &end->magic, err); |
| 427 | __get_user_error(end_size, &end->size, err); |
| 428 | if (err) |
| 429 | return err; |
| 430 | |
| 431 | if (end_magic || end_size) |
| 432 | goto invalid; |
| 433 | |
| 434 | /* Prevent looping/repeated parsing of extra_context */ |
| 435 | have_extra_context = true; |
| 436 | |
| 437 | base = (__force void __user *)extra_datap; |
| 438 | if (!IS_ALIGNED((unsigned long)base, 16)) |
| 439 | goto invalid; |
| 440 | |
| 441 | if (!IS_ALIGNED(extra_size, 16)) |
| 442 | goto invalid; |
| 443 | |
| 444 | if (base != userp) |
| 445 | goto invalid; |
| 446 | |
| 447 | /* Reject "unreasonably large" frames: */ |
| 448 | if (extra_size > sfp + SIGFRAME_MAXSZ - userp) |
| 449 | goto invalid; |
| 450 | |
| 451 | /* |
| 452 | * Ignore trailing terminator in __reserved[] |
| 453 | * and start parsing extra data: |
| 454 | */ |
| 455 | offset = 0; |
| 456 | limit = extra_size; |
| 457 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 458 | if (!access_ok(base, limit)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 459 | goto invalid; |
| 460 | |
| 461 | continue; |
| 462 | |
| 463 | default: |
| 464 | goto invalid; |
| 465 | } |
| 466 | |
| 467 | if (size < sizeof(*head)) |
| 468 | goto invalid; |
| 469 | |
| 470 | if (limit - offset < size) |
| 471 | goto invalid; |
| 472 | |
| 473 | offset += size; |
| 474 | } |
| 475 | |
| 476 | done: |
| 477 | return 0; |
| 478 | |
| 479 | invalid: |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | static int restore_sigframe(struct pt_regs *regs, |
| 484 | struct rt_sigframe __user *sf) |
| 485 | { |
| 486 | sigset_t set; |
| 487 | int i, err; |
| 488 | struct user_ctxs user; |
| 489 | |
| 490 | err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set)); |
| 491 | if (err == 0) |
| 492 | set_current_blocked(&set); |
| 493 | |
| 494 | for (i = 0; i < 31; i++) |
| 495 | __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], |
| 496 | err); |
| 497 | __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); |
| 498 | __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); |
| 499 | __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); |
| 500 | |
| 501 | /* |
| 502 | * Avoid sys_rt_sigreturn() restarting. |
| 503 | */ |
| 504 | forget_syscall(regs); |
| 505 | |
| 506 | err |= !valid_user_regs(®s->user_regs, current); |
| 507 | if (err == 0) |
| 508 | err = parse_user_sigframe(&user, sf); |
| 509 | |
| 510 | if (err == 0) { |
| 511 | if (!user.fpsimd) |
| 512 | return -EINVAL; |
| 513 | |
| 514 | if (user.sve) { |
| 515 | if (!system_supports_sve()) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | err = restore_sve_fpsimd_context(&user); |
| 519 | } else { |
| 520 | err = restore_fpsimd_context(user.fpsimd); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return err; |
| 525 | } |
| 526 | |
| 527 | SYSCALL_DEFINE0(rt_sigreturn) |
| 528 | { |
| 529 | struct pt_regs *regs = current_pt_regs(); |
| 530 | struct rt_sigframe __user *frame; |
| 531 | |
| 532 | /* Always make any pending restarted system calls return -EINTR */ |
| 533 | current->restart_block.fn = do_no_restart_syscall; |
| 534 | |
| 535 | /* |
| 536 | * Since we stacked the signal on a 128-bit boundary, then 'sp' should |
| 537 | * be word aligned here. |
| 538 | */ |
| 539 | if (regs->sp & 15) |
| 540 | goto badframe; |
| 541 | |
| 542 | frame = (struct rt_sigframe __user *)regs->sp; |
| 543 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 544 | if (!access_ok(frame, sizeof (*frame))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 545 | goto badframe; |
| 546 | |
| 547 | if (restore_sigframe(regs, frame)) |
| 548 | goto badframe; |
| 549 | |
| 550 | if (restore_altstack(&frame->uc.uc_stack)) |
| 551 | goto badframe; |
| 552 | |
| 553 | return regs->regs[0]; |
| 554 | |
| 555 | badframe: |
| 556 | arm64_notify_segfault(regs->sp); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Determine the layout of optional records in the signal frame |
| 562 | * |
| 563 | * add_all: if true, lays out the biggest possible signal frame for |
| 564 | * this task; otherwise, generates a layout for the current state |
| 565 | * of the task. |
| 566 | */ |
| 567 | static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, |
| 568 | bool add_all) |
| 569 | { |
| 570 | int err; |
| 571 | |
| 572 | err = sigframe_alloc(user, &user->fpsimd_offset, |
| 573 | sizeof(struct fpsimd_context)); |
| 574 | if (err) |
| 575 | return err; |
| 576 | |
| 577 | /* fault information, if valid */ |
| 578 | if (add_all || current->thread.fault_code) { |
| 579 | err = sigframe_alloc(user, &user->esr_offset, |
| 580 | sizeof(struct esr_context)); |
| 581 | if (err) |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | if (system_supports_sve()) { |
| 586 | unsigned int vq = 0; |
| 587 | |
| 588 | if (add_all || test_thread_flag(TIF_SVE)) { |
| 589 | int vl = sve_max_vl; |
| 590 | |
| 591 | if (!add_all) |
| 592 | vl = current->thread.sve_vl; |
| 593 | |
| 594 | vq = sve_vq_from_vl(vl); |
| 595 | } |
| 596 | |
| 597 | err = sigframe_alloc(user, &user->sve_offset, |
| 598 | SVE_SIG_CONTEXT_SIZE(vq)); |
| 599 | if (err) |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | return sigframe_alloc_end(user); |
| 604 | } |
| 605 | |
| 606 | static int setup_sigframe(struct rt_sigframe_user_layout *user, |
| 607 | struct pt_regs *regs, sigset_t *set) |
| 608 | { |
| 609 | int i, err = 0; |
| 610 | struct rt_sigframe __user *sf = user->sigframe; |
| 611 | |
| 612 | /* set up the stack frame for unwinding */ |
| 613 | __put_user_error(regs->regs[29], &user->next_frame->fp, err); |
| 614 | __put_user_error(regs->regs[30], &user->next_frame->lr, err); |
| 615 | |
| 616 | for (i = 0; i < 31; i++) |
| 617 | __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], |
| 618 | err); |
| 619 | __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); |
| 620 | __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); |
| 621 | __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); |
| 622 | |
| 623 | __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err); |
| 624 | |
| 625 | err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set)); |
| 626 | |
| 627 | if (err == 0) { |
| 628 | struct fpsimd_context __user *fpsimd_ctx = |
| 629 | apply_user_offset(user, user->fpsimd_offset); |
| 630 | err |= preserve_fpsimd_context(fpsimd_ctx); |
| 631 | } |
| 632 | |
| 633 | /* fault information, if valid */ |
| 634 | if (err == 0 && user->esr_offset) { |
| 635 | struct esr_context __user *esr_ctx = |
| 636 | apply_user_offset(user, user->esr_offset); |
| 637 | |
| 638 | __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err); |
| 639 | __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err); |
| 640 | __put_user_error(current->thread.fault_code, &esr_ctx->esr, err); |
| 641 | } |
| 642 | |
| 643 | /* Scalable Vector Extension state, if present */ |
| 644 | if (system_supports_sve() && err == 0 && user->sve_offset) { |
| 645 | struct sve_context __user *sve_ctx = |
| 646 | apply_user_offset(user, user->sve_offset); |
| 647 | err |= preserve_sve_context(sve_ctx); |
| 648 | } |
| 649 | |
| 650 | if (err == 0 && user->extra_offset) { |
| 651 | char __user *sfp = (char __user *)user->sigframe; |
| 652 | char __user *userp = |
| 653 | apply_user_offset(user, user->extra_offset); |
| 654 | |
| 655 | struct extra_context __user *extra; |
| 656 | struct _aarch64_ctx __user *end; |
| 657 | u64 extra_datap; |
| 658 | u32 extra_size; |
| 659 | |
| 660 | extra = (struct extra_context __user *)userp; |
| 661 | userp += EXTRA_CONTEXT_SIZE; |
| 662 | |
| 663 | end = (struct _aarch64_ctx __user *)userp; |
| 664 | userp += TERMINATOR_SIZE; |
| 665 | |
| 666 | /* |
| 667 | * extra_datap is just written to the signal frame. |
| 668 | * The value gets cast back to a void __user * |
| 669 | * during sigreturn. |
| 670 | */ |
| 671 | extra_datap = (__force u64)userp; |
| 672 | extra_size = sfp + round_up(user->size, 16) - userp; |
| 673 | |
| 674 | __put_user_error(EXTRA_MAGIC, &extra->head.magic, err); |
| 675 | __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err); |
| 676 | __put_user_error(extra_datap, &extra->datap, err); |
| 677 | __put_user_error(extra_size, &extra->size, err); |
| 678 | |
| 679 | /* Add the terminator */ |
| 680 | __put_user_error(0, &end->magic, err); |
| 681 | __put_user_error(0, &end->size, err); |
| 682 | } |
| 683 | |
| 684 | /* set the "end" magic */ |
| 685 | if (err == 0) { |
| 686 | struct _aarch64_ctx __user *end = |
| 687 | apply_user_offset(user, user->end_offset); |
| 688 | |
| 689 | __put_user_error(0, &end->magic, err); |
| 690 | __put_user_error(0, &end->size, err); |
| 691 | } |
| 692 | |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | static int get_sigframe(struct rt_sigframe_user_layout *user, |
| 697 | struct ksignal *ksig, struct pt_regs *regs) |
| 698 | { |
| 699 | unsigned long sp, sp_top; |
| 700 | int err; |
| 701 | |
| 702 | init_user_layout(user); |
| 703 | err = setup_sigframe_layout(user, false); |
| 704 | if (err) |
| 705 | return err; |
| 706 | |
| 707 | sp = sp_top = sigsp(regs->sp, ksig); |
| 708 | |
| 709 | sp = round_down(sp - sizeof(struct frame_record), 16); |
| 710 | user->next_frame = (struct frame_record __user *)sp; |
| 711 | |
| 712 | sp = round_down(sp, 16) - sigframe_size(user); |
| 713 | user->sigframe = (struct rt_sigframe __user *)sp; |
| 714 | |
| 715 | /* |
| 716 | * Check that we can actually write to the signal frame. |
| 717 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 718 | if (!access_ok(user->sigframe, sp_top - sp)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 719 | return -EFAULT; |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, |
| 725 | struct rt_sigframe_user_layout *user, int usig) |
| 726 | { |
| 727 | __sigrestore_t sigtramp; |
| 728 | |
| 729 | regs->regs[0] = usig; |
| 730 | regs->sp = (unsigned long)user->sigframe; |
| 731 | regs->regs[29] = (unsigned long)&user->next_frame->fp; |
| 732 | regs->pc = (unsigned long)ka->sa.sa_handler; |
| 733 | |
| 734 | if (ka->sa.sa_flags & SA_RESTORER) |
| 735 | sigtramp = ka->sa.sa_restorer; |
| 736 | else |
| 737 | sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp); |
| 738 | |
| 739 | regs->regs[30] = (unsigned long)sigtramp; |
| 740 | } |
| 741 | |
| 742 | static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, |
| 743 | struct pt_regs *regs) |
| 744 | { |
| 745 | struct rt_sigframe_user_layout user; |
| 746 | struct rt_sigframe __user *frame; |
| 747 | int err = 0; |
| 748 | |
| 749 | fpsimd_signal_preserve_current_state(); |
| 750 | |
| 751 | if (get_sigframe(&user, ksig, regs)) |
| 752 | return 1; |
| 753 | |
| 754 | frame = user.sigframe; |
| 755 | |
| 756 | __put_user_error(0, &frame->uc.uc_flags, err); |
| 757 | __put_user_error(NULL, &frame->uc.uc_link, err); |
| 758 | |
| 759 | err |= __save_altstack(&frame->uc.uc_stack, regs->sp); |
| 760 | err |= setup_sigframe(&user, regs, set); |
| 761 | if (err == 0) { |
| 762 | setup_return(regs, &ksig->ka, &user, usig); |
| 763 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) { |
| 764 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
| 765 | regs->regs[1] = (unsigned long)&frame->info; |
| 766 | regs->regs[2] = (unsigned long)&frame->uc; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | return err; |
| 771 | } |
| 772 | |
| 773 | static void setup_restart_syscall(struct pt_regs *regs) |
| 774 | { |
| 775 | if (is_compat_task()) |
| 776 | compat_setup_restart_syscall(regs); |
| 777 | else |
| 778 | regs->regs[8] = __NR_restart_syscall; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * OK, we're invoking a handler |
| 783 | */ |
| 784 | static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
| 785 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 786 | sigset_t *oldset = sigmask_to_save(); |
| 787 | int usig = ksig->sig; |
| 788 | int ret; |
| 789 | |
| 790 | rseq_signal_deliver(ksig, regs); |
| 791 | |
| 792 | /* |
| 793 | * Set up the stack frame |
| 794 | */ |
| 795 | if (is_compat_task()) { |
| 796 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
| 797 | ret = compat_setup_rt_frame(usig, ksig, oldset, regs); |
| 798 | else |
| 799 | ret = compat_setup_frame(usig, ksig, oldset, regs); |
| 800 | } else { |
| 801 | ret = setup_rt_frame(usig, ksig, oldset, regs); |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Check that the resulting registers are actually sane. |
| 806 | */ |
| 807 | ret |= !valid_user_regs(®s->user_regs, current); |
| 808 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 809 | /* Step into the signal handler if we are stepping */ |
| 810 | signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 815 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 816 | * mistake. |
| 817 | * |
| 818 | * Note that we go through the signals twice: once to check the signals that |
| 819 | * the kernel can handle, and then we build all the user-level signal handling |
| 820 | * stack-frames in one go after that. |
| 821 | */ |
| 822 | static void do_signal(struct pt_regs *regs) |
| 823 | { |
| 824 | unsigned long continue_addr = 0, restart_addr = 0; |
| 825 | int retval = 0; |
| 826 | struct ksignal ksig; |
| 827 | bool syscall = in_syscall(regs); |
| 828 | |
| 829 | /* |
| 830 | * If we were from a system call, check for system call restarting... |
| 831 | */ |
| 832 | if (syscall) { |
| 833 | continue_addr = regs->pc; |
| 834 | restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4); |
| 835 | retval = regs->regs[0]; |
| 836 | |
| 837 | /* |
| 838 | * Avoid additional syscall restarting via ret_to_user. |
| 839 | */ |
| 840 | forget_syscall(regs); |
| 841 | |
| 842 | /* |
| 843 | * Prepare for system call restart. We do this here so that a |
| 844 | * debugger will see the already changed PC. |
| 845 | */ |
| 846 | switch (retval) { |
| 847 | case -ERESTARTNOHAND: |
| 848 | case -ERESTARTSYS: |
| 849 | case -ERESTARTNOINTR: |
| 850 | case -ERESTART_RESTARTBLOCK: |
| 851 | regs->regs[0] = regs->orig_x0; |
| 852 | regs->pc = restart_addr; |
| 853 | break; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Get the signal to deliver. When running under ptrace, at this point |
| 859 | * the debugger may change all of our registers. |
| 860 | */ |
| 861 | if (get_signal(&ksig)) { |
| 862 | /* |
| 863 | * Depending on the signal settings, we may need to revert the |
| 864 | * decision to restart the system call, but skip this if a |
| 865 | * debugger has chosen to restart at a different PC. |
| 866 | */ |
| 867 | if (regs->pc == restart_addr && |
| 868 | (retval == -ERESTARTNOHAND || |
| 869 | retval == -ERESTART_RESTARTBLOCK || |
| 870 | (retval == -ERESTARTSYS && |
| 871 | !(ksig.ka.sa.sa_flags & SA_RESTART)))) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 872 | syscall_set_return_value(current, regs, -EINTR, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 873 | regs->pc = continue_addr; |
| 874 | } |
| 875 | |
| 876 | handle_signal(&ksig, regs); |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * Handle restarting a different system call. As above, if a debugger |
| 882 | * has chosen to restart at a different PC, ignore the restart. |
| 883 | */ |
| 884 | if (syscall && regs->pc == restart_addr) { |
| 885 | if (retval == -ERESTART_RESTARTBLOCK) |
| 886 | setup_restart_syscall(regs); |
| 887 | user_rewind_single_step(current); |
| 888 | } |
| 889 | |
| 890 | restore_saved_sigmask(); |
| 891 | } |
| 892 | |
| 893 | asmlinkage void do_notify_resume(struct pt_regs *regs, |
| 894 | unsigned long thread_flags) |
| 895 | { |
| 896 | /* |
| 897 | * The assembly code enters us with IRQs off, but it hasn't |
| 898 | * informed the tracing code of that for efficiency reasons. |
| 899 | * Update the trace code with the current status. |
| 900 | */ |
| 901 | trace_hardirqs_off(); |
| 902 | |
| 903 | do { |
| 904 | /* Check valid user FS if needed */ |
| 905 | addr_limit_user_check(); |
| 906 | |
| 907 | if (thread_flags & _TIF_NEED_RESCHED) { |
| 908 | /* Unmask Debug and SError for the next task */ |
| 909 | local_daif_restore(DAIF_PROCCTX_NOIRQ); |
| 910 | |
| 911 | schedule(); |
| 912 | } else { |
| 913 | local_daif_restore(DAIF_PROCCTX); |
| 914 | |
| 915 | if (thread_flags & _TIF_UPROBE) |
| 916 | uprobe_notify_resume(regs); |
| 917 | |
| 918 | if (thread_flags & _TIF_SIGPENDING) |
| 919 | do_signal(regs); |
| 920 | |
| 921 | if (thread_flags & _TIF_NOTIFY_RESUME) { |
| 922 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 923 | tracehook_notify_resume(regs); |
| 924 | rseq_handle_notify_resume(NULL, regs); |
| 925 | } |
| 926 | |
| 927 | if (thread_flags & _TIF_FOREIGN_FPSTATE) |
| 928 | fpsimd_restore_current_state(); |
| 929 | } |
| 930 | |
| 931 | local_daif_mask(); |
| 932 | thread_flags = READ_ONCE(current_thread_info()->flags); |
| 933 | } while (thread_flags & _TIF_WORK_MASK); |
| 934 | } |
| 935 | |
| 936 | unsigned long __ro_after_init signal_minsigstksz; |
| 937 | |
| 938 | /* |
| 939 | * Determine the stack space required for guaranteed signal devliery. |
| 940 | * This function is used to populate AT_MINSIGSTKSZ at process startup. |
| 941 | * cpufeatures setup is assumed to be complete. |
| 942 | */ |
| 943 | void __init minsigstksz_setup(void) |
| 944 | { |
| 945 | struct rt_sigframe_user_layout user; |
| 946 | |
| 947 | init_user_layout(&user); |
| 948 | |
| 949 | /* |
| 950 | * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't |
| 951 | * be big enough, but it's our best guess: |
| 952 | */ |
| 953 | if (WARN_ON(setup_sigframe_layout(&user, true))) |
| 954 | return; |
| 955 | |
| 956 | signal_minsigstksz = sigframe_size(&user) + |
| 957 | round_up(sizeof(struct frame_record), 16) + |
| 958 | 16; /* max alignment padding */ |
| 959 | } |