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