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