1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs 5 * 6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes 8 * 2000-2002 x86-64 support by Andi Kleen 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 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/kstrtox.h> 19 #include <linux/errno.h> 20 #include <linux/wait.h> 21 #include <linux/unistd.h> 22 #include <linux/stddef.h> 23 #include <linux/personality.h> 24 #include <linux/uaccess.h> 25 #include <linux/user-return-notifier.h> 26 #include <linux/uprobes.h> 27 #include <linux/context_tracking.h> 28 #include <linux/entry-common.h> 29 #include <linux/syscalls.h> 30 31 #include <asm/processor.h> 32 #include <asm/ucontext.h> 33 #include <asm/fpu/signal.h> 34 #include <asm/fpu/xstate.h> 35 #include <asm/vdso.h> 36 #include <asm/mce.h> 37 #include <asm/sighandling.h> 38 #include <asm/vm86.h> 39 40 #include <asm/syscall.h> 41 #include <asm/sigframe.h> 42 #include <asm/signal.h> 43 44 static inline int is_ia32_compat_frame(struct ksignal *ksig) 45 { 46 return IS_ENABLED(CONFIG_IA32_EMULATION) && 47 ksig->ka.sa.sa_flags & SA_IA32_ABI; 48 } 49 50 static inline int is_ia32_frame(struct ksignal *ksig) 51 { 52 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig); 53 } 54 55 static inline int is_x32_frame(struct ksignal *ksig) 56 { 57 return IS_ENABLED(CONFIG_X86_X32_ABI) && 58 ksig->ka.sa.sa_flags & SA_X32_ABI; 59 } 60 61 /* 62 * Set up a signal frame. 63 */ 64 65 /* x86 ABI requires 16-byte alignment */ 66 #define FRAME_ALIGNMENT 16UL 67 68 #define MAX_FRAME_PADDING (FRAME_ALIGNMENT - 1) 69 70 /* 71 * Determine which stack to use.. 72 */ 73 void __user * 74 get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size, 75 void __user **fpstate) 76 { 77 struct k_sigaction *ka = &ksig->ka; 78 int ia32_frame = is_ia32_frame(ksig); 79 /* Default to using normal stack */ 80 bool nested_altstack = on_sig_stack(regs->sp); 81 bool entering_altstack = false; 82 unsigned long math_size = 0; 83 unsigned long sp = regs->sp; 84 unsigned long buf_fx = 0; 85 86 /* redzone */ 87 if (!ia32_frame) 88 sp -= 128; 89 90 /* This is the X/Open sanctioned signal stack switching. */ 91 if (ka->sa.sa_flags & SA_ONSTACK) { 92 /* 93 * This checks nested_altstack via sas_ss_flags(). Sensible 94 * programs use SS_AUTODISARM, which disables that check, and 95 * programs that don't use SS_AUTODISARM get compatible. 96 */ 97 if (sas_ss_flags(sp) == 0) { 98 sp = current->sas_ss_sp + current->sas_ss_size; 99 entering_altstack = true; 100 } 101 } else if (ia32_frame && 102 !nested_altstack && 103 regs->ss != __USER_DS && 104 !(ka->sa.sa_flags & SA_RESTORER) && 105 ka->sa.sa_restorer) { 106 /* This is the legacy signal stack switching. */ 107 sp = (unsigned long) ka->sa.sa_restorer; 108 entering_altstack = true; 109 } 110 111 sp = fpu__alloc_mathframe(sp, ia32_frame, &buf_fx, &math_size); 112 *fpstate = (void __user *)sp; 113 114 sp -= frame_size; 115 116 if (ia32_frame) 117 /* 118 * Align the stack pointer according to the i386 ABI, 119 * i.e. so that on function entry ((sp + 4) & 15) == 0. 120 */ 121 sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4; 122 else 123 sp = round_down(sp, FRAME_ALIGNMENT) - 8; 124 125 /* 126 * If we are on the alternate signal stack and would overflow it, don't. 127 * Return an always-bogus address instead so we will die with SIGSEGV. 128 */ 129 if (unlikely((nested_altstack || entering_altstack) && 130 !__on_sig_stack(sp))) { 131 132 if (show_unhandled_signals && printk_ratelimit()) 133 pr_info("%s[%d] overflowed sigaltstack\n", 134 current->comm, task_pid_nr(current)); 135 136 return (void __user *)-1L; 137 } 138 139 /* save i387 and extended state */ 140 if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size)) 141 return (void __user *)-1L; 142 143 return (void __user *)sp; 144 } 145 146 /* 147 * There are four different struct types for signal frame: sigframe_ia32, 148 * rt_sigframe_ia32, rt_sigframe_x32, and rt_sigframe. Use the worst case 149 * -- the largest size. It means the size for 64-bit apps is a bit more 150 * than needed, but this keeps the code simple. 151 */ 152 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION) 153 # define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct sigframe_ia32) 154 #else 155 # define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct rt_sigframe) 156 #endif 157 158 /* 159 * The FP state frame contains an XSAVE buffer which must be 64-byte aligned. 160 * If a signal frame starts at an unaligned address, extra space is required. 161 * This is the max alignment padding, conservatively. 162 */ 163 #define MAX_XSAVE_PADDING 63UL 164 165 /* 166 * The frame data is composed of the following areas and laid out as: 167 * 168 * ------------------------- 169 * | alignment padding | 170 * ------------------------- 171 * | (f)xsave frame | 172 * ------------------------- 173 * | fsave header | 174 * ------------------------- 175 * | alignment padding | 176 * ------------------------- 177 * | siginfo + ucontext | 178 * ------------------------- 179 */ 180 181 /* max_frame_size tells userspace the worst case signal stack size. */ 182 static unsigned long __ro_after_init max_frame_size; 183 static unsigned int __ro_after_init fpu_default_state_size; 184 185 static int __init init_sigframe_size(void) 186 { 187 fpu_default_state_size = fpu__get_fpstate_size(); 188 189 max_frame_size = MAX_FRAME_SIGINFO_UCTXT_SIZE + MAX_FRAME_PADDING; 190 191 max_frame_size += fpu_default_state_size + MAX_XSAVE_PADDING; 192 193 /* Userspace expects an aligned size. */ 194 max_frame_size = round_up(max_frame_size, FRAME_ALIGNMENT); 195 196 pr_info("max sigframe size: %lu\n", max_frame_size); 197 return 0; 198 } 199 early_initcall(init_sigframe_size); 200 201 unsigned long get_sigframe_size(void) 202 { 203 return max_frame_size; 204 } 205 206 static int 207 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs) 208 { 209 /* Perform fixup for the pre-signal frame. */ 210 rseq_signal_deliver(ksig, regs); 211 212 /* Set up the stack frame */ 213 if (is_ia32_frame(ksig)) { 214 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 215 return ia32_setup_rt_frame(ksig, regs); 216 else 217 return ia32_setup_frame(ksig, regs); 218 } else if (is_x32_frame(ksig)) { 219 return x32_setup_rt_frame(ksig, regs); 220 } else { 221 return x64_setup_rt_frame(ksig, regs); 222 } 223 } 224 225 static void 226 handle_signal(struct ksignal *ksig, struct pt_regs *regs) 227 { 228 bool stepping, failed; 229 struct fpu *fpu = ¤t->thread.fpu; 230 231 if (v8086_mode(regs)) 232 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL); 233 234 /* Are we from a system call? */ 235 if (syscall_get_nr(current, regs) != -1) { 236 /* If so, check system call restarting.. */ 237 switch (syscall_get_error(current, regs)) { 238 case -ERESTART_RESTARTBLOCK: 239 case -ERESTARTNOHAND: 240 regs->ax = -EINTR; 241 break; 242 243 case -ERESTARTSYS: 244 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { 245 regs->ax = -EINTR; 246 break; 247 } 248 fallthrough; 249 case -ERESTARTNOINTR: 250 regs->ax = regs->orig_ax; 251 regs->ip -= 2; 252 break; 253 } 254 } 255 256 /* 257 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now 258 * so that register information in the sigcontext is correct and 259 * then notify the tracer before entering the signal handler. 260 */ 261 stepping = test_thread_flag(TIF_SINGLESTEP); 262 if (stepping) 263 user_disable_single_step(current); 264 265 failed = (setup_rt_frame(ksig, regs) < 0); 266 if (!failed) { 267 /* 268 * Clear the direction flag as per the ABI for function entry. 269 * 270 * Clear RF when entering the signal handler, because 271 * it might disable possible debug exception from the 272 * signal handler. 273 * 274 * Clear TF for the case when it wasn't set by debugger to 275 * avoid the recursive send_sigtrap() in SIGTRAP handler. 276 */ 277 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF); 278 /* 279 * Ensure the signal handler starts with the new fpu state. 280 */ 281 fpu__clear_user_states(fpu); 282 } 283 signal_setup_done(failed, ksig, stepping); 284 } 285 286 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs) 287 { 288 #ifdef CONFIG_IA32_EMULATION 289 if (current->restart_block.arch_data & TS_COMPAT) 290 return __NR_ia32_restart_syscall; 291 #endif 292 #ifdef CONFIG_X86_X32_ABI 293 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT); 294 #else 295 return __NR_restart_syscall; 296 #endif 297 } 298 299 /* 300 * Note that 'init' is a special process: it doesn't get signals it doesn't 301 * want to handle. Thus you cannot kill init even with a SIGKILL even by 302 * mistake. 303 */ 304 void arch_do_signal_or_restart(struct pt_regs *regs) 305 { 306 struct ksignal ksig; 307 308 if (get_signal(&ksig)) { 309 /* Whee! Actually deliver the signal. */ 310 handle_signal(&ksig, regs); 311 return; 312 } 313 314 /* Did we come from a system call? */ 315 if (syscall_get_nr(current, regs) != -1) { 316 /* Restart the system call - no handlers present */ 317 switch (syscall_get_error(current, regs)) { 318 case -ERESTARTNOHAND: 319 case -ERESTARTSYS: 320 case -ERESTARTNOINTR: 321 regs->ax = regs->orig_ax; 322 regs->ip -= 2; 323 break; 324 325 case -ERESTART_RESTARTBLOCK: 326 regs->ax = get_nr_restart_syscall(regs); 327 regs->ip -= 2; 328 break; 329 } 330 } 331 332 /* 333 * If there's no signal to deliver, we just put the saved sigmask 334 * back. 335 */ 336 restore_saved_sigmask(); 337 } 338 339 void signal_fault(struct pt_regs *regs, void __user *frame, char *where) 340 { 341 struct task_struct *me = current; 342 343 if (show_unhandled_signals && printk_ratelimit()) { 344 printk("%s" 345 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx", 346 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG, 347 me->comm, me->pid, where, frame, 348 regs->ip, regs->sp, regs->orig_ax); 349 print_vma_addr(KERN_CONT " in ", regs->ip); 350 pr_cont("\n"); 351 } 352 353 force_sig(SIGSEGV); 354 } 355 356 #ifdef CONFIG_DYNAMIC_SIGFRAME 357 #ifdef CONFIG_STRICT_SIGALTSTACK_SIZE 358 static bool strict_sigaltstack_size __ro_after_init = true; 359 #else 360 static bool strict_sigaltstack_size __ro_after_init = false; 361 #endif 362 363 static int __init strict_sas_size(char *arg) 364 { 365 return kstrtobool(arg, &strict_sigaltstack_size) == 0; 366 } 367 __setup("strict_sas_size", strict_sas_size); 368 369 /* 370 * MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512 371 * exceeds that size already. As such programs might never use the 372 * sigaltstack they just continued to work. While always checking against 373 * the real size would be correct, this might be considered a regression. 374 * 375 * Therefore avoid the sanity check, unless enforced by kernel 376 * configuration or command line option. 377 * 378 * When dynamic FPU features are supported, the check is also enforced when 379 * the task has permissions to use dynamic features. Tasks which have no 380 * permission are checked against the size of the non-dynamic feature set 381 * if strict checking is enabled. This avoids forcing all tasks on the 382 * system to allocate large sigaltstacks even if they are never going 383 * to use a dynamic feature. As this is serialized via sighand::siglock 384 * any permission request for a dynamic feature either happened already 385 * or will see the newly install sigaltstack size in the permission checks. 386 */ 387 bool sigaltstack_size_valid(size_t ss_size) 388 { 389 unsigned long fsize = max_frame_size - fpu_default_state_size; 390 u64 mask; 391 392 lockdep_assert_held(¤t->sighand->siglock); 393 394 if (!fpu_state_size_dynamic() && !strict_sigaltstack_size) 395 return true; 396 397 fsize += current->group_leader->thread.fpu.perm.__user_state_size; 398 if (likely(ss_size > fsize)) 399 return true; 400 401 if (strict_sigaltstack_size) 402 return ss_size > fsize; 403 404 mask = current->group_leader->thread.fpu.perm.__state_perm; 405 if (mask & XFEATURE_MASK_USER_DYNAMIC) 406 return ss_size > fsize; 407 408 return true; 409 } 410 #endif /* CONFIG_DYNAMIC_SIGFRAME */ 411