~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/kernel/signal.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php