Loading...
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
46static 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
52static inline int is_ia32_frame(struct ksignal *ksig)
53{
54 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
55}
56
57static 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 * Enable all pkeys temporarily, so as to ensure that both the current
65 * execution stack as well as the alternate signal stack are writeable.
66 * The application can use any of the available pkeys to protect the
67 * alternate signal stack, and we don't know which one it is, so enable
68 * all. The PKRU register will be reset to init_pkru later in the flow,
69 * in fpu__clear_user_states(), and it is the application's responsibility
70 * to enable the appropriate pkey as the first step in the signal handler
71 * so that the handler does not segfault.
72 */
73static inline u32 sig_prepare_pkru(void)
74{
75 u32 orig_pkru = read_pkru();
76
77 write_pkru(0);
78 return orig_pkru;
79}
80
81/*
82 * Set up a signal frame.
83 */
84
85/* x86 ABI requires 16-byte alignment */
86#define FRAME_ALIGNMENT 16UL
87
88#define MAX_FRAME_PADDING (FRAME_ALIGNMENT - 1)
89
90/*
91 * Determine which stack to use..
92 */
93void __user *
94get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size,
95 void __user **fpstate)
96{
97 struct k_sigaction *ka = &ksig->ka;
98 int ia32_frame = is_ia32_frame(ksig);
99 /* Default to using normal stack */
100 bool nested_altstack = on_sig_stack(regs->sp);
101 bool entering_altstack = false;
102 unsigned long math_size = 0;
103 unsigned long sp = regs->sp;
104 unsigned long buf_fx = 0;
105 u32 pkru;
106
107 /* redzone */
108 if (!ia32_frame)
109 sp -= 128;
110
111 /* This is the X/Open sanctioned signal stack switching. */
112 if (ka->sa.sa_flags & SA_ONSTACK) {
113 /*
114 * This checks nested_altstack via sas_ss_flags(). Sensible
115 * programs use SS_AUTODISARM, which disables that check, and
116 * programs that don't use SS_AUTODISARM get compatible.
117 */
118 if (sas_ss_flags(sp) == 0) {
119 sp = current->sas_ss_sp + current->sas_ss_size;
120 entering_altstack = true;
121 }
122 } else if (ia32_frame &&
123 !nested_altstack &&
124 regs->ss != __USER_DS &&
125 !(ka->sa.sa_flags & SA_RESTORER) &&
126 ka->sa.sa_restorer) {
127 /* This is the legacy signal stack switching. */
128 sp = (unsigned long) ka->sa.sa_restorer;
129 entering_altstack = true;
130 }
131
132 sp = fpu__alloc_mathframe(sp, ia32_frame, &buf_fx, &math_size);
133 *fpstate = (void __user *)sp;
134
135 sp -= frame_size;
136
137 if (ia32_frame)
138 /*
139 * Align the stack pointer according to the i386 ABI,
140 * i.e. so that on function entry ((sp + 4) & 15) == 0.
141 */
142 sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
143 else
144 sp = round_down(sp, FRAME_ALIGNMENT) - 8;
145
146 /*
147 * If we are on the alternate signal stack and would overflow it, don't.
148 * Return an always-bogus address instead so we will die with SIGSEGV.
149 */
150 if (unlikely((nested_altstack || entering_altstack) &&
151 !__on_sig_stack(sp))) {
152
153 if (show_unhandled_signals && printk_ratelimit())
154 pr_info("%s[%d] overflowed sigaltstack\n",
155 current->comm, task_pid_nr(current));
156
157 return (void __user *)-1L;
158 }
159
160 /* Update PKRU to enable access to the alternate signal stack. */
161 pkru = sig_prepare_pkru();
162 /* save i387 and extended state */
163 if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size, pkru)) {
164 /*
165 * Restore PKRU to the original, user-defined value; disable
166 * extra pkeys enabled for the alternate signal stack, if any.
167 */
168 write_pkru(pkru);
169 return (void __user *)-1L;
170 }
171
172 return (void __user *)sp;
173}
174
175/*
176 * There are four different struct types for signal frame: sigframe_ia32,
177 * rt_sigframe_ia32, rt_sigframe_x32, and rt_sigframe. Use the worst case
178 * -- the largest size. It means the size for 64-bit apps is a bit more
179 * than needed, but this keeps the code simple.
180 */
181#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
182# define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct sigframe_ia32)
183#else
184# define MAX_FRAME_SIGINFO_UCTXT_SIZE sizeof(struct rt_sigframe)
185#endif
186
187/*
188 * The FP state frame contains an XSAVE buffer which must be 64-byte aligned.
189 * If a signal frame starts at an unaligned address, extra space is required.
190 * This is the max alignment padding, conservatively.
191 */
192#define MAX_XSAVE_PADDING 63UL
193
194/*
195 * The frame data is composed of the following areas and laid out as:
196 *
197 * -------------------------
198 * | alignment padding |
199 * -------------------------
200 * | (f)xsave frame |
201 * -------------------------
202 * | fsave header |
203 * -------------------------
204 * | alignment padding |
205 * -------------------------
206 * | siginfo + ucontext |
207 * -------------------------
208 */
209
210/* max_frame_size tells userspace the worst case signal stack size. */
211static unsigned long __ro_after_init max_frame_size;
212static unsigned int __ro_after_init fpu_default_state_size;
213
214static int __init init_sigframe_size(void)
215{
216 fpu_default_state_size = fpu__get_fpstate_size();
217
218 max_frame_size = MAX_FRAME_SIGINFO_UCTXT_SIZE + MAX_FRAME_PADDING;
219
220 max_frame_size += fpu_default_state_size + MAX_XSAVE_PADDING;
221
222 /* Userspace expects an aligned size. */
223 max_frame_size = round_up(max_frame_size, FRAME_ALIGNMENT);
224
225 pr_info("max sigframe size: %lu\n", max_frame_size);
226 return 0;
227}
228early_initcall(init_sigframe_size);
229
230unsigned long get_sigframe_size(void)
231{
232 return max_frame_size;
233}
234
235static int
236setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
237{
238 /* Perform fixup for the pre-signal frame. */
239 rseq_signal_deliver(ksig, regs);
240
241 /* Set up the stack frame */
242 if (is_ia32_frame(ksig)) {
243 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
244 return ia32_setup_rt_frame(ksig, regs);
245 else
246 return ia32_setup_frame(ksig, regs);
247 } else if (is_x32_frame(ksig)) {
248 return x32_setup_rt_frame(ksig, regs);
249 } else {
250 return x64_setup_rt_frame(ksig, regs);
251 }
252}
253
254static void
255handle_signal(struct ksignal *ksig, struct pt_regs *regs)
256{
257 bool stepping, failed;
258 struct fpu *fpu = ¤t->thread.fpu;
259
260 if (v8086_mode(regs))
261 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
262
263 /* Are we from a system call? */
264 if (syscall_get_nr(current, regs) != -1) {
265 /* If so, check system call restarting.. */
266 switch (syscall_get_error(current, regs)) {
267 case -ERESTART_RESTARTBLOCK:
268 case -ERESTARTNOHAND:
269 regs->ax = -EINTR;
270 break;
271
272 case -ERESTARTSYS:
273 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
274 regs->ax = -EINTR;
275 break;
276 }
277 fallthrough;
278 case -ERESTARTNOINTR:
279 regs->ax = regs->orig_ax;
280 regs->ip -= 2;
281 break;
282 }
283 }
284
285 /*
286 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
287 * so that register information in the sigcontext is correct and
288 * then notify the tracer before entering the signal handler.
289 */
290 stepping = test_thread_flag(TIF_SINGLESTEP);
291 if (stepping)
292 user_disable_single_step(current);
293
294 failed = (setup_rt_frame(ksig, regs) < 0);
295 if (!failed) {
296 /*
297 * Clear the direction flag as per the ABI for function entry.
298 *
299 * Clear RF when entering the signal handler, because
300 * it might disable possible debug exception from the
301 * signal handler.
302 *
303 * Clear TF for the case when it wasn't set by debugger to
304 * avoid the recursive send_sigtrap() in SIGTRAP handler.
305 */
306 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
307 /*
308 * Ensure the signal handler starts with the new fpu state.
309 */
310 fpu__clear_user_states(fpu);
311 }
312 signal_setup_done(failed, ksig, stepping);
313}
314
315static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
316{
317#ifdef CONFIG_IA32_EMULATION
318 if (current->restart_block.arch_data & TS_COMPAT)
319 return __NR_ia32_restart_syscall;
320#endif
321#ifdef CONFIG_X86_X32_ABI
322 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
323#else
324 return __NR_restart_syscall;
325#endif
326}
327
328/*
329 * Note that 'init' is a special process: it doesn't get signals it doesn't
330 * want to handle. Thus you cannot kill init even with a SIGKILL even by
331 * mistake.
332 */
333void arch_do_signal_or_restart(struct pt_regs *regs)
334{
335 struct ksignal ksig;
336
337 if (get_signal(&ksig)) {
338 /* Whee! Actually deliver the signal. */
339 handle_signal(&ksig, regs);
340 return;
341 }
342
343 /* Did we come from a system call? */
344 if (syscall_get_nr(current, regs) != -1) {
345 /* Restart the system call - no handlers present */
346 switch (syscall_get_error(current, regs)) {
347 case -ERESTARTNOHAND:
348 case -ERESTARTSYS:
349 case -ERESTARTNOINTR:
350 regs->ax = regs->orig_ax;
351 regs->ip -= 2;
352 break;
353
354 case -ERESTART_RESTARTBLOCK:
355 regs->ax = get_nr_restart_syscall(regs);
356 regs->ip -= 2;
357 break;
358 }
359 }
360
361 /*
362 * If there's no signal to deliver, we just put the saved sigmask
363 * back.
364 */
365 restore_saved_sigmask();
366}
367
368void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
369{
370 struct task_struct *me = current;
371
372 if (show_unhandled_signals && printk_ratelimit()) {
373 printk("%s"
374 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
375 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
376 me->comm, me->pid, where, frame,
377 regs->ip, regs->sp, regs->orig_ax);
378 print_vma_addr(KERN_CONT " in ", regs->ip);
379 pr_cont("\n");
380 }
381
382 force_sig(SIGSEGV);
383}
384
385#ifdef CONFIG_DYNAMIC_SIGFRAME
386#ifdef CONFIG_STRICT_SIGALTSTACK_SIZE
387static bool strict_sigaltstack_size __ro_after_init = true;
388#else
389static bool strict_sigaltstack_size __ro_after_init = false;
390#endif
391
392static int __init strict_sas_size(char *arg)
393{
394 return kstrtobool(arg, &strict_sigaltstack_size) == 0;
395}
396__setup("strict_sas_size", strict_sas_size);
397
398/*
399 * MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512
400 * exceeds that size already. As such programs might never use the
401 * sigaltstack they just continued to work. While always checking against
402 * the real size would be correct, this might be considered a regression.
403 *
404 * Therefore avoid the sanity check, unless enforced by kernel
405 * configuration or command line option.
406 *
407 * When dynamic FPU features are supported, the check is also enforced when
408 * the task has permissions to use dynamic features. Tasks which have no
409 * permission are checked against the size of the non-dynamic feature set
410 * if strict checking is enabled. This avoids forcing all tasks on the
411 * system to allocate large sigaltstacks even if they are never going
412 * to use a dynamic feature. As this is serialized via sighand::siglock
413 * any permission request for a dynamic feature either happened already
414 * or will see the newly install sigaltstack size in the permission checks.
415 */
416bool sigaltstack_size_valid(size_t ss_size)
417{
418 unsigned long fsize = max_frame_size - fpu_default_state_size;
419 u64 mask;
420
421 lockdep_assert_held(¤t->sighand->siglock);
422
423 if (!fpu_state_size_dynamic() && !strict_sigaltstack_size)
424 return true;
425
426 fsize += current->group_leader->thread.fpu.perm.__user_state_size;
427 if (likely(ss_size > fsize))
428 return true;
429
430 if (strict_sigaltstack_size)
431 return ss_size > fsize;
432
433 mask = current->group_leader->thread.fpu.perm.__state_perm;
434 if (mask & XFEATURE_MASK_USER_DYNAMIC)
435 return ss_size > fsize;
436
437 return true;
438}
439#endif /* CONFIG_DYNAMIC_SIGFRAME */
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/errno.h>
19#include <linux/wait.h>
20#include <linux/tracehook.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/internal.h>
34#include <asm/fpu/signal.h>
35#include <asm/vdso.h>
36#include <asm/mce.h>
37#include <asm/sighandling.h>
38#include <asm/vm86.h>
39
40#ifdef CONFIG_X86_64
41#include <linux/compat.h>
42#include <asm/proto.h>
43#include <asm/ia32_unistd.h>
44#endif /* CONFIG_X86_64 */
45
46#include <asm/syscall.h>
47#include <asm/sigframe.h>
48#include <asm/signal.h>
49
50#ifdef CONFIG_X86_64
51/*
52 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
53 * alone. Using this generally makes no sense unless
54 * user_64bit_mode(regs) would return true.
55 */
56static void force_valid_ss(struct pt_regs *regs)
57{
58 u32 ar;
59 asm volatile ("lar %[old_ss], %[ar]\n\t"
60 "jz 1f\n\t" /* If invalid: */
61 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
62 "1:"
63 : [ar] "=r" (ar)
64 : [old_ss] "rm" ((u16)regs->ss));
65
66 /*
67 * For a valid 64-bit user context, we need DPL 3, type
68 * read-write data or read-write exp-down data, and S and P
69 * set. We can't use VERW because VERW doesn't check the
70 * P bit.
71 */
72 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
73 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
74 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
75 regs->ss = __USER_DS;
76}
77# define CONTEXT_COPY_SIZE offsetof(struct sigcontext, reserved1)
78#else
79# define CONTEXT_COPY_SIZE sizeof(struct sigcontext)
80#endif
81
82static int restore_sigcontext(struct pt_regs *regs,
83 struct sigcontext __user *usc,
84 unsigned long uc_flags)
85{
86 struct sigcontext sc;
87
88 /* Always make any pending restarted system calls return -EINTR */
89 current->restart_block.fn = do_no_restart_syscall;
90
91 if (copy_from_user(&sc, usc, CONTEXT_COPY_SIZE))
92 return -EFAULT;
93
94#ifdef CONFIG_X86_32
95 set_user_gs(regs, sc.gs);
96 regs->fs = sc.fs;
97 regs->es = sc.es;
98 regs->ds = sc.ds;
99#endif /* CONFIG_X86_32 */
100
101 regs->bx = sc.bx;
102 regs->cx = sc.cx;
103 regs->dx = sc.dx;
104 regs->si = sc.si;
105 regs->di = sc.di;
106 regs->bp = sc.bp;
107 regs->ax = sc.ax;
108 regs->sp = sc.sp;
109 regs->ip = sc.ip;
110
111#ifdef CONFIG_X86_64
112 regs->r8 = sc.r8;
113 regs->r9 = sc.r9;
114 regs->r10 = sc.r10;
115 regs->r11 = sc.r11;
116 regs->r12 = sc.r12;
117 regs->r13 = sc.r13;
118 regs->r14 = sc.r14;
119 regs->r15 = sc.r15;
120#endif /* CONFIG_X86_64 */
121
122 /* Get CS/SS and force CPL3 */
123 regs->cs = sc.cs | 0x03;
124 regs->ss = sc.ss | 0x03;
125
126 regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
127 /* disable syscall checks */
128 regs->orig_ax = -1;
129
130#ifdef CONFIG_X86_64
131 /*
132 * Fix up SS if needed for the benefit of old DOSEMU and
133 * CRIU.
134 */
135 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
136 force_valid_ss(regs);
137#endif
138
139 return fpu__restore_sig((void __user *)sc.fpstate,
140 IS_ENABLED(CONFIG_X86_32));
141}
142
143static __always_inline int
144__unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
145 struct pt_regs *regs, unsigned long mask)
146{
147#ifdef CONFIG_X86_32
148 unsafe_put_user(get_user_gs(regs),
149 (unsigned int __user *)&sc->gs, Efault);
150 unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
151 unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
152 unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
153#endif /* CONFIG_X86_32 */
154
155 unsafe_put_user(regs->di, &sc->di, Efault);
156 unsafe_put_user(regs->si, &sc->si, Efault);
157 unsafe_put_user(regs->bp, &sc->bp, Efault);
158 unsafe_put_user(regs->sp, &sc->sp, Efault);
159 unsafe_put_user(regs->bx, &sc->bx, Efault);
160 unsafe_put_user(regs->dx, &sc->dx, Efault);
161 unsafe_put_user(regs->cx, &sc->cx, Efault);
162 unsafe_put_user(regs->ax, &sc->ax, Efault);
163#ifdef CONFIG_X86_64
164 unsafe_put_user(regs->r8, &sc->r8, Efault);
165 unsafe_put_user(regs->r9, &sc->r9, Efault);
166 unsafe_put_user(regs->r10, &sc->r10, Efault);
167 unsafe_put_user(regs->r11, &sc->r11, Efault);
168 unsafe_put_user(regs->r12, &sc->r12, Efault);
169 unsafe_put_user(regs->r13, &sc->r13, Efault);
170 unsafe_put_user(regs->r14, &sc->r14, Efault);
171 unsafe_put_user(regs->r15, &sc->r15, Efault);
172#endif /* CONFIG_X86_64 */
173
174 unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
175 unsafe_put_user(current->thread.error_code, &sc->err, Efault);
176 unsafe_put_user(regs->ip, &sc->ip, Efault);
177#ifdef CONFIG_X86_32
178 unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
179 unsafe_put_user(regs->flags, &sc->flags, Efault);
180 unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
181 unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
182#else /* !CONFIG_X86_32 */
183 unsafe_put_user(regs->flags, &sc->flags, Efault);
184 unsafe_put_user(regs->cs, &sc->cs, Efault);
185 unsafe_put_user(0, &sc->gs, Efault);
186 unsafe_put_user(0, &sc->fs, Efault);
187 unsafe_put_user(regs->ss, &sc->ss, Efault);
188#endif /* CONFIG_X86_32 */
189
190 unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
191
192 /* non-iBCS2 extensions.. */
193 unsafe_put_user(mask, &sc->oldmask, Efault);
194 unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
195 return 0;
196Efault:
197 return -EFAULT;
198}
199
200#define unsafe_put_sigcontext(sc, fp, regs, set, label) \
201do { \
202 if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0])) \
203 goto label; \
204} while(0);
205
206#define unsafe_put_sigmask(set, frame, label) \
207 unsafe_put_user(*(__u64 *)(set), \
208 (__u64 __user *)&(frame)->uc.uc_sigmask, \
209 label)
210
211/*
212 * Set up a signal frame.
213 */
214
215/*
216 * Determine which stack to use..
217 */
218static unsigned long align_sigframe(unsigned long sp)
219{
220#ifdef CONFIG_X86_32
221 /*
222 * Align the stack pointer according to the i386 ABI,
223 * i.e. so that on function entry ((sp + 4) & 15) == 0.
224 */
225 sp = ((sp + 4) & -16ul) - 4;
226#else /* !CONFIG_X86_32 */
227 sp = round_down(sp, 16) - 8;
228#endif
229 return sp;
230}
231
232static void __user *
233get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
234 void __user **fpstate)
235{
236 /* Default to using normal stack */
237 unsigned long math_size = 0;
238 unsigned long sp = regs->sp;
239 unsigned long buf_fx = 0;
240 int onsigstack = on_sig_stack(sp);
241 int ret;
242
243 /* redzone */
244 if (IS_ENABLED(CONFIG_X86_64))
245 sp -= 128;
246
247 /* This is the X/Open sanctioned signal stack switching. */
248 if (ka->sa.sa_flags & SA_ONSTACK) {
249 if (sas_ss_flags(sp) == 0)
250 sp = current->sas_ss_sp + current->sas_ss_size;
251 } else if (IS_ENABLED(CONFIG_X86_32) &&
252 !onsigstack &&
253 regs->ss != __USER_DS &&
254 !(ka->sa.sa_flags & SA_RESTORER) &&
255 ka->sa.sa_restorer) {
256 /* This is the legacy signal stack switching. */
257 sp = (unsigned long) ka->sa.sa_restorer;
258 }
259
260 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
261 &buf_fx, &math_size);
262 *fpstate = (void __user *)sp;
263
264 sp = align_sigframe(sp - frame_size);
265
266 /*
267 * If we are on the alternate signal stack and would overflow it, don't.
268 * Return an always-bogus address instead so we will die with SIGSEGV.
269 */
270 if (onsigstack && !likely(on_sig_stack(sp)))
271 return (void __user *)-1L;
272
273 /* save i387 and extended state */
274 ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
275 if (ret < 0)
276 return (void __user *)-1L;
277
278 return (void __user *)sp;
279}
280
281#ifdef CONFIG_X86_32
282static const struct {
283 u16 poplmovl;
284 u32 val;
285 u16 int80;
286} __attribute__((packed)) retcode = {
287 0xb858, /* popl %eax; movl $..., %eax */
288 __NR_sigreturn,
289 0x80cd, /* int $0x80 */
290};
291
292static const struct {
293 u8 movl;
294 u32 val;
295 u16 int80;
296 u8 pad;
297} __attribute__((packed)) rt_retcode = {
298 0xb8, /* movl $..., %eax */
299 __NR_rt_sigreturn,
300 0x80cd, /* int $0x80 */
301 0
302};
303
304static int
305__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
306 struct pt_regs *regs)
307{
308 struct sigframe __user *frame;
309 void __user *restorer;
310 void __user *fp = NULL;
311
312 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
313
314 if (!user_access_begin(frame, sizeof(*frame)))
315 return -EFAULT;
316
317 unsafe_put_user(sig, &frame->sig, Efault);
318 unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
319 unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
320 if (current->mm->context.vdso)
321 restorer = current->mm->context.vdso +
322 vdso_image_32.sym___kernel_sigreturn;
323 else
324 restorer = &frame->retcode;
325 if (ksig->ka.sa.sa_flags & SA_RESTORER)
326 restorer = ksig->ka.sa.sa_restorer;
327
328 /* Set up to return from userspace. */
329 unsafe_put_user(restorer, &frame->pretcode, Efault);
330
331 /*
332 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
333 *
334 * WE DO NOT USE IT ANY MORE! It's only left here for historical
335 * reasons and because gdb uses it as a signature to notice
336 * signal handler stack frames.
337 */
338 unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
339 user_access_end();
340
341 /* Set up registers for signal handler */
342 regs->sp = (unsigned long)frame;
343 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
344 regs->ax = (unsigned long)sig;
345 regs->dx = 0;
346 regs->cx = 0;
347
348 regs->ds = __USER_DS;
349 regs->es = __USER_DS;
350 regs->ss = __USER_DS;
351 regs->cs = __USER_CS;
352
353 return 0;
354
355Efault:
356 user_access_end();
357 return -EFAULT;
358}
359
360static int __setup_rt_frame(int sig, struct ksignal *ksig,
361 sigset_t *set, struct pt_regs *regs)
362{
363 struct rt_sigframe __user *frame;
364 void __user *restorer;
365 void __user *fp = NULL;
366
367 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
368
369 if (!user_access_begin(frame, sizeof(*frame)))
370 return -EFAULT;
371
372 unsafe_put_user(sig, &frame->sig, Efault);
373 unsafe_put_user(&frame->info, &frame->pinfo, Efault);
374 unsafe_put_user(&frame->uc, &frame->puc, Efault);
375
376 /* Create the ucontext. */
377 if (static_cpu_has(X86_FEATURE_XSAVE))
378 unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
379 else
380 unsafe_put_user(0, &frame->uc.uc_flags, Efault);
381 unsafe_put_user(0, &frame->uc.uc_link, Efault);
382 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
383
384 /* Set up to return from userspace. */
385 restorer = current->mm->context.vdso +
386 vdso_image_32.sym___kernel_rt_sigreturn;
387 if (ksig->ka.sa.sa_flags & SA_RESTORER)
388 restorer = ksig->ka.sa.sa_restorer;
389 unsafe_put_user(restorer, &frame->pretcode, Efault);
390
391 /*
392 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
393 *
394 * WE DO NOT USE IT ANY MORE! It's only left here for historical
395 * reasons and because gdb uses it as a signature to notice
396 * signal handler stack frames.
397 */
398 unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
399 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
400 unsafe_put_sigmask(set, frame, Efault);
401 user_access_end();
402
403 if (copy_siginfo_to_user(&frame->info, &ksig->info))
404 return -EFAULT;
405
406 /* Set up registers for signal handler */
407 regs->sp = (unsigned long)frame;
408 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
409 regs->ax = (unsigned long)sig;
410 regs->dx = (unsigned long)&frame->info;
411 regs->cx = (unsigned long)&frame->uc;
412
413 regs->ds = __USER_DS;
414 regs->es = __USER_DS;
415 regs->ss = __USER_DS;
416 regs->cs = __USER_CS;
417
418 return 0;
419Efault:
420 user_access_end();
421 return -EFAULT;
422}
423#else /* !CONFIG_X86_32 */
424static unsigned long frame_uc_flags(struct pt_regs *regs)
425{
426 unsigned long flags;
427
428 if (boot_cpu_has(X86_FEATURE_XSAVE))
429 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
430 else
431 flags = UC_SIGCONTEXT_SS;
432
433 if (likely(user_64bit_mode(regs)))
434 flags |= UC_STRICT_RESTORE_SS;
435
436 return flags;
437}
438
439static int __setup_rt_frame(int sig, struct ksignal *ksig,
440 sigset_t *set, struct pt_regs *regs)
441{
442 struct rt_sigframe __user *frame;
443 void __user *fp = NULL;
444 unsigned long uc_flags;
445
446 /* x86-64 should always use SA_RESTORER. */
447 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
448 return -EFAULT;
449
450 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
451 uc_flags = frame_uc_flags(regs);
452
453 if (!user_access_begin(frame, sizeof(*frame)))
454 return -EFAULT;
455
456 /* Create the ucontext. */
457 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
458 unsafe_put_user(0, &frame->uc.uc_link, Efault);
459 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
460
461 /* Set up to return from userspace. If provided, use a stub
462 already in userspace. */
463 unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
464 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
465 unsafe_put_sigmask(set, frame, Efault);
466 user_access_end();
467
468 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
469 if (copy_siginfo_to_user(&frame->info, &ksig->info))
470 return -EFAULT;
471 }
472
473 /* Set up registers for signal handler */
474 regs->di = sig;
475 /* In case the signal handler was declared without prototypes */
476 regs->ax = 0;
477
478 /* This also works for non SA_SIGINFO handlers because they expect the
479 next argument after the signal number on the stack. */
480 regs->si = (unsigned long)&frame->info;
481 regs->dx = (unsigned long)&frame->uc;
482 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
483
484 regs->sp = (unsigned long)frame;
485
486 /*
487 * Set up the CS and SS registers to run signal handlers in
488 * 64-bit mode, even if the handler happens to be interrupting
489 * 32-bit or 16-bit code.
490 *
491 * SS is subtle. In 64-bit mode, we don't need any particular
492 * SS descriptor, but we do need SS to be valid. It's possible
493 * that the old SS is entirely bogus -- this can happen if the
494 * signal we're trying to deliver is #GP or #SS caused by a bad
495 * SS value. We also have a compatbility issue here: DOSEMU
496 * relies on the contents of the SS register indicating the
497 * SS value at the time of the signal, even though that code in
498 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
499 * avoids relying on sigreturn to restore SS; instead it uses
500 * a trampoline.) So we do our best: if the old SS was valid,
501 * we keep it. Otherwise we replace it.
502 */
503 regs->cs = __USER_CS;
504
505 if (unlikely(regs->ss != __USER_DS))
506 force_valid_ss(regs);
507
508 return 0;
509
510Efault:
511 user_access_end();
512 return -EFAULT;
513}
514#endif /* CONFIG_X86_32 */
515
516#ifdef CONFIG_X86_X32_ABI
517static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
518 const struct kernel_siginfo *from)
519{
520 struct compat_siginfo new;
521
522 copy_siginfo_to_external32(&new, from);
523 if (from->si_signo == SIGCHLD) {
524 new._sifields._sigchld_x32._utime = from->si_utime;
525 new._sifields._sigchld_x32._stime = from->si_stime;
526 }
527 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
528 return -EFAULT;
529 return 0;
530}
531
532int copy_siginfo_to_user32(struct compat_siginfo __user *to,
533 const struct kernel_siginfo *from)
534{
535 if (in_x32_syscall())
536 return x32_copy_siginfo_to_user(to, from);
537 return __copy_siginfo_to_user32(to, from);
538}
539#endif /* CONFIG_X86_X32_ABI */
540
541static int x32_setup_rt_frame(struct ksignal *ksig,
542 compat_sigset_t *set,
543 struct pt_regs *regs)
544{
545#ifdef CONFIG_X86_X32_ABI
546 struct rt_sigframe_x32 __user *frame;
547 unsigned long uc_flags;
548 void __user *restorer;
549 void __user *fp = NULL;
550
551 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
552 return -EFAULT;
553
554 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
555
556 uc_flags = frame_uc_flags(regs);
557
558 if (!user_access_begin(frame, sizeof(*frame)))
559 return -EFAULT;
560
561 /* Create the ucontext. */
562 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
563 unsafe_put_user(0, &frame->uc.uc_link, Efault);
564 unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
565 unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
566 restorer = ksig->ka.sa.sa_restorer;
567 unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
568 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
569 unsafe_put_sigmask(set, frame, Efault);
570 user_access_end();
571
572 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
573 if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
574 return -EFAULT;
575 }
576
577 /* Set up registers for signal handler */
578 regs->sp = (unsigned long) frame;
579 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
580
581 /* We use the x32 calling convention here... */
582 regs->di = ksig->sig;
583 regs->si = (unsigned long) &frame->info;
584 regs->dx = (unsigned long) &frame->uc;
585
586 loadsegment(ds, __USER_DS);
587 loadsegment(es, __USER_DS);
588
589 regs->cs = __USER_CS;
590 regs->ss = __USER_DS;
591#endif /* CONFIG_X86_X32_ABI */
592
593 return 0;
594#ifdef CONFIG_X86_X32_ABI
595Efault:
596 user_access_end();
597 return -EFAULT;
598#endif
599}
600
601/*
602 * Do a signal return; undo the signal stack.
603 */
604#ifdef CONFIG_X86_32
605SYSCALL_DEFINE0(sigreturn)
606{
607 struct pt_regs *regs = current_pt_regs();
608 struct sigframe __user *frame;
609 sigset_t set;
610
611 frame = (struct sigframe __user *)(regs->sp - 8);
612
613 if (!access_ok(frame, sizeof(*frame)))
614 goto badframe;
615 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
616 __get_user(set.sig[1], &frame->extramask[0]))
617 goto badframe;
618
619 set_current_blocked(&set);
620
621 /*
622 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
623 * Save a few cycles by skipping the __get_user.
624 */
625 if (restore_sigcontext(regs, &frame->sc, 0))
626 goto badframe;
627 return regs->ax;
628
629badframe:
630 signal_fault(regs, frame, "sigreturn");
631
632 return 0;
633}
634#endif /* CONFIG_X86_32 */
635
636SYSCALL_DEFINE0(rt_sigreturn)
637{
638 struct pt_regs *regs = current_pt_regs();
639 struct rt_sigframe __user *frame;
640 sigset_t set;
641 unsigned long uc_flags;
642
643 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
644 if (!access_ok(frame, sizeof(*frame)))
645 goto badframe;
646 if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
647 goto badframe;
648 if (__get_user(uc_flags, &frame->uc.uc_flags))
649 goto badframe;
650
651 set_current_blocked(&set);
652
653 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
654 goto badframe;
655
656 if (restore_altstack(&frame->uc.uc_stack))
657 goto badframe;
658
659 return regs->ax;
660
661badframe:
662 signal_fault(regs, frame, "rt_sigreturn");
663 return 0;
664}
665
666static inline int is_ia32_compat_frame(struct ksignal *ksig)
667{
668 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
669 ksig->ka.sa.sa_flags & SA_IA32_ABI;
670}
671
672static inline int is_ia32_frame(struct ksignal *ksig)
673{
674 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
675}
676
677static inline int is_x32_frame(struct ksignal *ksig)
678{
679 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
680 ksig->ka.sa.sa_flags & SA_X32_ABI;
681}
682
683static int
684setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
685{
686 int usig = ksig->sig;
687 sigset_t *set = sigmask_to_save();
688 compat_sigset_t *cset = (compat_sigset_t *) set;
689
690 /* Perform fixup for the pre-signal frame. */
691 rseq_signal_deliver(ksig, regs);
692
693 /* Set up the stack frame */
694 if (is_ia32_frame(ksig)) {
695 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
696 return ia32_setup_rt_frame(usig, ksig, cset, regs);
697 else
698 return ia32_setup_frame(usig, ksig, cset, regs);
699 } else if (is_x32_frame(ksig)) {
700 return x32_setup_rt_frame(ksig, cset, regs);
701 } else {
702 return __setup_rt_frame(ksig->sig, ksig, set, regs);
703 }
704}
705
706static void
707handle_signal(struct ksignal *ksig, struct pt_regs *regs)
708{
709 bool stepping, failed;
710 struct fpu *fpu = ¤t->thread.fpu;
711
712 if (v8086_mode(regs))
713 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
714
715 /* Are we from a system call? */
716 if (syscall_get_nr(current, regs) >= 0) {
717 /* If so, check system call restarting.. */
718 switch (syscall_get_error(current, regs)) {
719 case -ERESTART_RESTARTBLOCK:
720 case -ERESTARTNOHAND:
721 regs->ax = -EINTR;
722 break;
723
724 case -ERESTARTSYS:
725 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
726 regs->ax = -EINTR;
727 break;
728 }
729 fallthrough;
730 case -ERESTARTNOINTR:
731 regs->ax = regs->orig_ax;
732 regs->ip -= 2;
733 break;
734 }
735 }
736
737 /*
738 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
739 * so that register information in the sigcontext is correct and
740 * then notify the tracer before entering the signal handler.
741 */
742 stepping = test_thread_flag(TIF_SINGLESTEP);
743 if (stepping)
744 user_disable_single_step(current);
745
746 failed = (setup_rt_frame(ksig, regs) < 0);
747 if (!failed) {
748 /*
749 * Clear the direction flag as per the ABI for function entry.
750 *
751 * Clear RF when entering the signal handler, because
752 * it might disable possible debug exception from the
753 * signal handler.
754 *
755 * Clear TF for the case when it wasn't set by debugger to
756 * avoid the recursive send_sigtrap() in SIGTRAP handler.
757 */
758 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
759 /*
760 * Ensure the signal handler starts with the new fpu state.
761 */
762 fpu__clear_user_states(fpu);
763 }
764 signal_setup_done(failed, ksig, stepping);
765}
766
767static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
768{
769 /*
770 * This function is fundamentally broken as currently
771 * implemented.
772 *
773 * The idea is that we want to trigger a call to the
774 * restart_block() syscall and that we want in_ia32_syscall(),
775 * in_x32_syscall(), etc. to match whatever they were in the
776 * syscall being restarted. We assume that the syscall
777 * instruction at (regs->ip - 2) matches whatever syscall
778 * instruction we used to enter in the first place.
779 *
780 * The problem is that we can get here when ptrace pokes
781 * syscall-like values into regs even if we're not in a syscall
782 * at all.
783 *
784 * For now, we maintain historical behavior and guess based on
785 * stored state. We could do better by saving the actual
786 * syscall arch in restart_block or (with caveats on x32) by
787 * checking if regs->ip points to 'int $0x80'. The current
788 * behavior is incorrect if a tracer has a different bitness
789 * than the tracee.
790 */
791#ifdef CONFIG_IA32_EMULATION
792 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
793 return __NR_ia32_restart_syscall;
794#endif
795#ifdef CONFIG_X86_X32_ABI
796 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
797#else
798 return __NR_restart_syscall;
799#endif
800}
801
802/*
803 * Note that 'init' is a special process: it doesn't get signals it doesn't
804 * want to handle. Thus you cannot kill init even with a SIGKILL even by
805 * mistake.
806 */
807void arch_do_signal(struct pt_regs *regs)
808{
809 struct ksignal ksig;
810
811 if (get_signal(&ksig)) {
812 /* Whee! Actually deliver the signal. */
813 handle_signal(&ksig, regs);
814 return;
815 }
816
817 /* Did we come from a system call? */
818 if (syscall_get_nr(current, regs) >= 0) {
819 /* Restart the system call - no handlers present */
820 switch (syscall_get_error(current, regs)) {
821 case -ERESTARTNOHAND:
822 case -ERESTARTSYS:
823 case -ERESTARTNOINTR:
824 regs->ax = regs->orig_ax;
825 regs->ip -= 2;
826 break;
827
828 case -ERESTART_RESTARTBLOCK:
829 regs->ax = get_nr_restart_syscall(regs);
830 regs->ip -= 2;
831 break;
832 }
833 }
834
835 /*
836 * If there's no signal to deliver, we just put the saved sigmask
837 * back.
838 */
839 restore_saved_sigmask();
840}
841
842void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
843{
844 struct task_struct *me = current;
845
846 if (show_unhandled_signals && printk_ratelimit()) {
847 printk("%s"
848 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
849 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
850 me->comm, me->pid, where, frame,
851 regs->ip, regs->sp, regs->orig_ax);
852 print_vma_addr(KERN_CONT " in ", regs->ip);
853 pr_cont("\n");
854 }
855
856 force_sig(SIGSEGV);
857}
858
859#ifdef CONFIG_X86_X32_ABI
860COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
861{
862 struct pt_regs *regs = current_pt_regs();
863 struct rt_sigframe_x32 __user *frame;
864 sigset_t set;
865 unsigned long uc_flags;
866
867 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
868
869 if (!access_ok(frame, sizeof(*frame)))
870 goto badframe;
871 if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
872 goto badframe;
873 if (__get_user(uc_flags, &frame->uc.uc_flags))
874 goto badframe;
875
876 set_current_blocked(&set);
877
878 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
879 goto badframe;
880
881 if (compat_restore_altstack(&frame->uc.uc_stack))
882 goto badframe;
883
884 return regs->ax;
885
886badframe:
887 signal_fault(regs, frame, "x32 rt_sigreturn");
888 return 0;
889}
890#endif