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/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
4 *
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7 * 2000-2002 x86-64 support by Andi Kleen
8 */
9#include <linux/sched.h>
10#include <linux/mm.h>
11#include <linux/smp.h>
12#include <linux/kernel.h>
13#include <linux/signal.h>
14#include <linux/errno.h>
15#include <linux/wait.h>
16#include <linux/ptrace.h>
17#include <linux/tracehook.h>
18#include <linux/unistd.h>
19#include <linux/stddef.h>
20#include <linux/personality.h>
21#include <linux/uaccess.h>
22#include <linux/user-return-notifier.h>
23
24#include <asm/processor.h>
25#include <asm/ucontext.h>
26#include <asm/i387.h>
27#include <asm/vdso.h>
28#include <asm/mce.h>
29
30#ifdef CONFIG_X86_64
31#include <asm/proto.h>
32#include <asm/ia32_unistd.h>
33#endif /* CONFIG_X86_64 */
34
35#include <asm/syscall.h>
36#include <asm/syscalls.h>
37
38#include <asm/sigframe.h>
39
40#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
42#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
43 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
44 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
45 X86_EFLAGS_CF)
46
47#ifdef CONFIG_X86_32
48# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
49#else
50# define FIX_EFLAGS __FIX_EFLAGS
51#endif
52
53#define COPY(x) do { \
54 get_user_ex(regs->x, &sc->x); \
55} while (0)
56
57#define GET_SEG(seg) ({ \
58 unsigned short tmp; \
59 get_user_ex(tmp, &sc->seg); \
60 tmp; \
61})
62
63#define COPY_SEG(seg) do { \
64 regs->seg = GET_SEG(seg); \
65} while (0)
66
67#define COPY_SEG_CPL3(seg) do { \
68 regs->seg = GET_SEG(seg) | 3; \
69} while (0)
70
71static int
72restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
73 unsigned long *pax)
74{
75 void __user *buf;
76 unsigned int tmpflags;
77 unsigned int err = 0;
78
79 /* Always make any pending restarted system calls return -EINTR */
80 current_thread_info()->restart_block.fn = do_no_restart_syscall;
81
82 get_user_try {
83
84#ifdef CONFIG_X86_32
85 set_user_gs(regs, GET_SEG(gs));
86 COPY_SEG(fs);
87 COPY_SEG(es);
88 COPY_SEG(ds);
89#endif /* CONFIG_X86_32 */
90
91 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
92 COPY(dx); COPY(cx); COPY(ip);
93
94#ifdef CONFIG_X86_64
95 COPY(r8);
96 COPY(r9);
97 COPY(r10);
98 COPY(r11);
99 COPY(r12);
100 COPY(r13);
101 COPY(r14);
102 COPY(r15);
103#endif /* CONFIG_X86_64 */
104
105#ifdef CONFIG_X86_32
106 COPY_SEG_CPL3(cs);
107 COPY_SEG_CPL3(ss);
108#else /* !CONFIG_X86_32 */
109 /* Kernel saves and restores only the CS segment register on signals,
110 * which is the bare minimum needed to allow mixed 32/64-bit code.
111 * App's signal handler can save/restore other segments if needed. */
112 COPY_SEG_CPL3(cs);
113#endif /* CONFIG_X86_32 */
114
115 get_user_ex(tmpflags, &sc->flags);
116 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
117 regs->orig_ax = -1; /* disable syscall checks */
118
119 get_user_ex(buf, &sc->fpstate);
120 err |= restore_i387_xstate(buf);
121
122 get_user_ex(*pax, &sc->ax);
123 } get_user_catch(err);
124
125 return err;
126}
127
128static int
129setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
130 struct pt_regs *regs, unsigned long mask)
131{
132 int err = 0;
133
134 put_user_try {
135
136#ifdef CONFIG_X86_32
137 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
138 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
139 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
140 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
141#endif /* CONFIG_X86_32 */
142
143 put_user_ex(regs->di, &sc->di);
144 put_user_ex(regs->si, &sc->si);
145 put_user_ex(regs->bp, &sc->bp);
146 put_user_ex(regs->sp, &sc->sp);
147 put_user_ex(regs->bx, &sc->bx);
148 put_user_ex(regs->dx, &sc->dx);
149 put_user_ex(regs->cx, &sc->cx);
150 put_user_ex(regs->ax, &sc->ax);
151#ifdef CONFIG_X86_64
152 put_user_ex(regs->r8, &sc->r8);
153 put_user_ex(regs->r9, &sc->r9);
154 put_user_ex(regs->r10, &sc->r10);
155 put_user_ex(regs->r11, &sc->r11);
156 put_user_ex(regs->r12, &sc->r12);
157 put_user_ex(regs->r13, &sc->r13);
158 put_user_ex(regs->r14, &sc->r14);
159 put_user_ex(regs->r15, &sc->r15);
160#endif /* CONFIG_X86_64 */
161
162 put_user_ex(current->thread.trap_no, &sc->trapno);
163 put_user_ex(current->thread.error_code, &sc->err);
164 put_user_ex(regs->ip, &sc->ip);
165#ifdef CONFIG_X86_32
166 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
167 put_user_ex(regs->flags, &sc->flags);
168 put_user_ex(regs->sp, &sc->sp_at_signal);
169 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
170#else /* !CONFIG_X86_32 */
171 put_user_ex(regs->flags, &sc->flags);
172 put_user_ex(regs->cs, &sc->cs);
173 put_user_ex(0, &sc->gs);
174 put_user_ex(0, &sc->fs);
175#endif /* CONFIG_X86_32 */
176
177 put_user_ex(fpstate, &sc->fpstate);
178
179 /* non-iBCS2 extensions.. */
180 put_user_ex(mask, &sc->oldmask);
181 put_user_ex(current->thread.cr2, &sc->cr2);
182 } put_user_catch(err);
183
184 return err;
185}
186
187/*
188 * Set up a signal frame.
189 */
190
191/*
192 * Determine which stack to use..
193 */
194static unsigned long align_sigframe(unsigned long sp)
195{
196#ifdef CONFIG_X86_32
197 /*
198 * Align the stack pointer according to the i386 ABI,
199 * i.e. so that on function entry ((sp + 4) & 15) == 0.
200 */
201 sp = ((sp + 4) & -16ul) - 4;
202#else /* !CONFIG_X86_32 */
203 sp = round_down(sp, 16) - 8;
204#endif
205 return sp;
206}
207
208static inline void __user *
209get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
210 void __user **fpstate)
211{
212 /* Default to using normal stack */
213 unsigned long sp = regs->sp;
214 int onsigstack = on_sig_stack(sp);
215
216#ifdef CONFIG_X86_64
217 /* redzone */
218 sp -= 128;
219#endif /* CONFIG_X86_64 */
220
221 if (!onsigstack) {
222 /* This is the X/Open sanctioned signal stack switching. */
223 if (ka->sa.sa_flags & SA_ONSTACK) {
224 if (current->sas_ss_size)
225 sp = current->sas_ss_sp + current->sas_ss_size;
226 } else {
227#ifdef CONFIG_X86_32
228 /* This is the legacy signal stack switching. */
229 if ((regs->ss & 0xffff) != __USER_DS &&
230 !(ka->sa.sa_flags & SA_RESTORER) &&
231 ka->sa.sa_restorer)
232 sp = (unsigned long) ka->sa.sa_restorer;
233#endif /* CONFIG_X86_32 */
234 }
235 }
236
237 if (used_math()) {
238 sp -= sig_xstate_size;
239#ifdef CONFIG_X86_64
240 sp = round_down(sp, 64);
241#endif /* CONFIG_X86_64 */
242 *fpstate = (void __user *)sp;
243 }
244
245 sp = align_sigframe(sp - frame_size);
246
247 /*
248 * If we are on the alternate signal stack and would overflow it, don't.
249 * Return an always-bogus address instead so we will die with SIGSEGV.
250 */
251 if (onsigstack && !likely(on_sig_stack(sp)))
252 return (void __user *)-1L;
253
254 /* save i387 state */
255 if (used_math() && save_i387_xstate(*fpstate) < 0)
256 return (void __user *)-1L;
257
258 return (void __user *)sp;
259}
260
261#ifdef CONFIG_X86_32
262static const struct {
263 u16 poplmovl;
264 u32 val;
265 u16 int80;
266} __attribute__((packed)) retcode = {
267 0xb858, /* popl %eax; movl $..., %eax */
268 __NR_sigreturn,
269 0x80cd, /* int $0x80 */
270};
271
272static const struct {
273 u8 movl;
274 u32 val;
275 u16 int80;
276 u8 pad;
277} __attribute__((packed)) rt_retcode = {
278 0xb8, /* movl $..., %eax */
279 __NR_rt_sigreturn,
280 0x80cd, /* int $0x80 */
281 0
282};
283
284static int
285__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
286 struct pt_regs *regs)
287{
288 struct sigframe __user *frame;
289 void __user *restorer;
290 int err = 0;
291 void __user *fpstate = NULL;
292
293 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
294
295 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
296 return -EFAULT;
297
298 if (__put_user(sig, &frame->sig))
299 return -EFAULT;
300
301 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
302 return -EFAULT;
303
304 if (_NSIG_WORDS > 1) {
305 if (__copy_to_user(&frame->extramask, &set->sig[1],
306 sizeof(frame->extramask)))
307 return -EFAULT;
308 }
309
310 if (current->mm->context.vdso)
311 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
312 else
313 restorer = &frame->retcode;
314 if (ka->sa.sa_flags & SA_RESTORER)
315 restorer = ka->sa.sa_restorer;
316
317 /* Set up to return from userspace. */
318 err |= __put_user(restorer, &frame->pretcode);
319
320 /*
321 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
322 *
323 * WE DO NOT USE IT ANY MORE! It's only left here for historical
324 * reasons and because gdb uses it as a signature to notice
325 * signal handler stack frames.
326 */
327 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
328
329 if (err)
330 return -EFAULT;
331
332 /* Set up registers for signal handler */
333 regs->sp = (unsigned long)frame;
334 regs->ip = (unsigned long)ka->sa.sa_handler;
335 regs->ax = (unsigned long)sig;
336 regs->dx = 0;
337 regs->cx = 0;
338
339 regs->ds = __USER_DS;
340 regs->es = __USER_DS;
341 regs->ss = __USER_DS;
342 regs->cs = __USER_CS;
343
344 return 0;
345}
346
347static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
348 sigset_t *set, struct pt_regs *regs)
349{
350 struct rt_sigframe __user *frame;
351 void __user *restorer;
352 int err = 0;
353 void __user *fpstate = NULL;
354
355 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
356
357 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
358 return -EFAULT;
359
360 put_user_try {
361 put_user_ex(sig, &frame->sig);
362 put_user_ex(&frame->info, &frame->pinfo);
363 put_user_ex(&frame->uc, &frame->puc);
364 err |= copy_siginfo_to_user(&frame->info, info);
365
366 /* Create the ucontext. */
367 if (cpu_has_xsave)
368 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
369 else
370 put_user_ex(0, &frame->uc.uc_flags);
371 put_user_ex(0, &frame->uc.uc_link);
372 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
373 put_user_ex(sas_ss_flags(regs->sp),
374 &frame->uc.uc_stack.ss_flags);
375 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
376 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
377 regs, set->sig[0]);
378 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
379
380 /* Set up to return from userspace. */
381 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
382 if (ka->sa.sa_flags & SA_RESTORER)
383 restorer = ka->sa.sa_restorer;
384 put_user_ex(restorer, &frame->pretcode);
385
386 /*
387 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
388 *
389 * WE DO NOT USE IT ANY MORE! It's only left here for historical
390 * reasons and because gdb uses it as a signature to notice
391 * signal handler stack frames.
392 */
393 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
394 } put_user_catch(err);
395
396 if (err)
397 return -EFAULT;
398
399 /* Set up registers for signal handler */
400 regs->sp = (unsigned long)frame;
401 regs->ip = (unsigned long)ka->sa.sa_handler;
402 regs->ax = (unsigned long)sig;
403 regs->dx = (unsigned long)&frame->info;
404 regs->cx = (unsigned long)&frame->uc;
405
406 regs->ds = __USER_DS;
407 regs->es = __USER_DS;
408 regs->ss = __USER_DS;
409 regs->cs = __USER_CS;
410
411 return 0;
412}
413#else /* !CONFIG_X86_32 */
414static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
415 sigset_t *set, struct pt_regs *regs)
416{
417 struct rt_sigframe __user *frame;
418 void __user *fp = NULL;
419 int err = 0;
420 struct task_struct *me = current;
421
422 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
423
424 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
425 return -EFAULT;
426
427 if (ka->sa.sa_flags & SA_SIGINFO) {
428 if (copy_siginfo_to_user(&frame->info, info))
429 return -EFAULT;
430 }
431
432 put_user_try {
433 /* Create the ucontext. */
434 if (cpu_has_xsave)
435 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
436 else
437 put_user_ex(0, &frame->uc.uc_flags);
438 put_user_ex(0, &frame->uc.uc_link);
439 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
440 put_user_ex(sas_ss_flags(regs->sp),
441 &frame->uc.uc_stack.ss_flags);
442 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
443 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
444 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
445
446 /* Set up to return from userspace. If provided, use a stub
447 already in userspace. */
448 /* x86-64 should always use SA_RESTORER. */
449 if (ka->sa.sa_flags & SA_RESTORER) {
450 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
451 } else {
452 /* could use a vstub here */
453 err |= -EFAULT;
454 }
455 } put_user_catch(err);
456
457 if (err)
458 return -EFAULT;
459
460 /* Set up registers for signal handler */
461 regs->di = sig;
462 /* In case the signal handler was declared without prototypes */
463 regs->ax = 0;
464
465 /* This also works for non SA_SIGINFO handlers because they expect the
466 next argument after the signal number on the stack. */
467 regs->si = (unsigned long)&frame->info;
468 regs->dx = (unsigned long)&frame->uc;
469 regs->ip = (unsigned long) ka->sa.sa_handler;
470
471 regs->sp = (unsigned long)frame;
472
473 /* Set up the CS register to run signal handlers in 64-bit mode,
474 even if the handler happens to be interrupting 32-bit code. */
475 regs->cs = __USER_CS;
476
477 return 0;
478}
479#endif /* CONFIG_X86_32 */
480
481#ifdef CONFIG_X86_32
482/*
483 * Atomically swap in the new signal mask, and wait for a signal.
484 */
485asmlinkage int
486sys_sigsuspend(int history0, int history1, old_sigset_t mask)
487{
488 sigset_t blocked;
489
490 current->saved_sigmask = current->blocked;
491
492 mask &= _BLOCKABLE;
493 siginitset(&blocked, mask);
494 set_current_blocked(&blocked);
495
496 current->state = TASK_INTERRUPTIBLE;
497 schedule();
498
499 set_restore_sigmask();
500 return -ERESTARTNOHAND;
501}
502
503asmlinkage int
504sys_sigaction(int sig, const struct old_sigaction __user *act,
505 struct old_sigaction __user *oact)
506{
507 struct k_sigaction new_ka, old_ka;
508 int ret = 0;
509
510 if (act) {
511 old_sigset_t mask;
512
513 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
514 return -EFAULT;
515
516 get_user_try {
517 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
518 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
519 get_user_ex(mask, &act->sa_mask);
520 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
521 } get_user_catch(ret);
522
523 if (ret)
524 return -EFAULT;
525 siginitset(&new_ka.sa.sa_mask, mask);
526 }
527
528 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
529
530 if (!ret && oact) {
531 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
532 return -EFAULT;
533
534 put_user_try {
535 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
536 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
537 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
538 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
539 } put_user_catch(ret);
540
541 if (ret)
542 return -EFAULT;
543 }
544
545 return ret;
546}
547#endif /* CONFIG_X86_32 */
548
549long
550sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
551 struct pt_regs *regs)
552{
553 return do_sigaltstack(uss, uoss, regs->sp);
554}
555
556/*
557 * Do a signal return; undo the signal stack.
558 */
559#ifdef CONFIG_X86_32
560unsigned long sys_sigreturn(struct pt_regs *regs)
561{
562 struct sigframe __user *frame;
563 unsigned long ax;
564 sigset_t set;
565
566 frame = (struct sigframe __user *)(regs->sp - 8);
567
568 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
569 goto badframe;
570 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
571 && __copy_from_user(&set.sig[1], &frame->extramask,
572 sizeof(frame->extramask))))
573 goto badframe;
574
575 sigdelsetmask(&set, ~_BLOCKABLE);
576 set_current_blocked(&set);
577
578 if (restore_sigcontext(regs, &frame->sc, &ax))
579 goto badframe;
580 return ax;
581
582badframe:
583 signal_fault(regs, frame, "sigreturn");
584
585 return 0;
586}
587#endif /* CONFIG_X86_32 */
588
589long sys_rt_sigreturn(struct pt_regs *regs)
590{
591 struct rt_sigframe __user *frame;
592 unsigned long ax;
593 sigset_t set;
594
595 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
596 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
597 goto badframe;
598 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
599 goto badframe;
600
601 sigdelsetmask(&set, ~_BLOCKABLE);
602 set_current_blocked(&set);
603
604 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
605 goto badframe;
606
607 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
608 goto badframe;
609
610 return ax;
611
612badframe:
613 signal_fault(regs, frame, "rt_sigreturn");
614 return 0;
615}
616
617/*
618 * OK, we're invoking a handler:
619 */
620static int signr_convert(int sig)
621{
622#ifdef CONFIG_X86_32
623 struct thread_info *info = current_thread_info();
624
625 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
626 return info->exec_domain->signal_invmap[sig];
627#endif /* CONFIG_X86_32 */
628 return sig;
629}
630
631#ifdef CONFIG_X86_32
632
633#define is_ia32 1
634#define ia32_setup_frame __setup_frame
635#define ia32_setup_rt_frame __setup_rt_frame
636
637#else /* !CONFIG_X86_32 */
638
639#ifdef CONFIG_IA32_EMULATION
640#define is_ia32 test_thread_flag(TIF_IA32)
641#else /* !CONFIG_IA32_EMULATION */
642#define is_ia32 0
643#endif /* CONFIG_IA32_EMULATION */
644
645int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
646 sigset_t *set, struct pt_regs *regs);
647int ia32_setup_frame(int sig, struct k_sigaction *ka,
648 sigset_t *set, struct pt_regs *regs);
649
650#endif /* CONFIG_X86_32 */
651
652static int
653setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
654 struct pt_regs *regs)
655{
656 int usig = signr_convert(sig);
657 sigset_t *set = ¤t->blocked;
658 int ret;
659
660 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
661 set = ¤t->saved_sigmask;
662
663 /* Set up the stack frame */
664 if (is_ia32) {
665 if (ka->sa.sa_flags & SA_SIGINFO)
666 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
667 else
668 ret = ia32_setup_frame(usig, ka, set, regs);
669 } else
670 ret = __setup_rt_frame(sig, ka, info, set, regs);
671
672 if (ret) {
673 force_sigsegv(sig, current);
674 return -EFAULT;
675 }
676
677 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
678 return ret;
679}
680
681static int
682handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
683 struct pt_regs *regs)
684{
685 sigset_t blocked;
686 int ret;
687
688 /* Are we from a system call? */
689 if (syscall_get_nr(current, regs) >= 0) {
690 /* If so, check system call restarting.. */
691 switch (syscall_get_error(current, regs)) {
692 case -ERESTART_RESTARTBLOCK:
693 case -ERESTARTNOHAND:
694 regs->ax = -EINTR;
695 break;
696
697 case -ERESTARTSYS:
698 if (!(ka->sa.sa_flags & SA_RESTART)) {
699 regs->ax = -EINTR;
700 break;
701 }
702 /* fallthrough */
703 case -ERESTARTNOINTR:
704 regs->ax = regs->orig_ax;
705 regs->ip -= 2;
706 break;
707 }
708 }
709
710 /*
711 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
712 * flag so that register information in the sigcontext is correct.
713 */
714 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
715 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
716 regs->flags &= ~X86_EFLAGS_TF;
717
718 ret = setup_rt_frame(sig, ka, info, regs);
719
720 if (ret)
721 return ret;
722
723 /*
724 * Clear the direction flag as per the ABI for function entry.
725 */
726 regs->flags &= ~X86_EFLAGS_DF;
727
728 /*
729 * Clear TF when entering the signal handler, but
730 * notify any tracer that was single-stepping it.
731 * The tracer may want to single-step inside the
732 * handler too.
733 */
734 regs->flags &= ~X86_EFLAGS_TF;
735
736 sigorsets(&blocked, ¤t->blocked, &ka->sa.sa_mask);
737 if (!(ka->sa.sa_flags & SA_NODEFER))
738 sigaddset(&blocked, sig);
739 set_current_blocked(&blocked);
740
741 tracehook_signal_handler(sig, info, ka, regs,
742 test_thread_flag(TIF_SINGLESTEP));
743
744 return 0;
745}
746
747#ifdef CONFIG_X86_32
748#define NR_restart_syscall __NR_restart_syscall
749#else /* !CONFIG_X86_32 */
750#define NR_restart_syscall \
751 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
752#endif /* CONFIG_X86_32 */
753
754/*
755 * Note that 'init' is a special process: it doesn't get signals it doesn't
756 * want to handle. Thus you cannot kill init even with a SIGKILL even by
757 * mistake.
758 */
759static void do_signal(struct pt_regs *regs)
760{
761 struct k_sigaction ka;
762 siginfo_t info;
763 int signr;
764
765 /*
766 * We want the common case to go fast, which is why we may in certain
767 * cases get here from kernel mode. Just return without doing anything
768 * if so.
769 * X86_32: vm86 regs switched out by assembly code before reaching
770 * here, so testing against kernel CS suffices.
771 */
772 if (!user_mode(regs))
773 return;
774
775 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
776 if (signr > 0) {
777 /* Whee! Actually deliver the signal. */
778 handle_signal(signr, &info, &ka, regs);
779 return;
780 }
781
782 /* Did we come from a system call? */
783 if (syscall_get_nr(current, regs) >= 0) {
784 /* Restart the system call - no handlers present */
785 switch (syscall_get_error(current, regs)) {
786 case -ERESTARTNOHAND:
787 case -ERESTARTSYS:
788 case -ERESTARTNOINTR:
789 regs->ax = regs->orig_ax;
790 regs->ip -= 2;
791 break;
792
793 case -ERESTART_RESTARTBLOCK:
794 regs->ax = NR_restart_syscall;
795 regs->ip -= 2;
796 break;
797 }
798 }
799
800 /*
801 * If there's no signal to deliver, we just put the saved sigmask
802 * back.
803 */
804 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
805 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
806 set_current_blocked(¤t->saved_sigmask);
807 }
808}
809
810/*
811 * notification of userspace execution resumption
812 * - triggered by the TIF_WORK_MASK flags
813 */
814void
815do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
816{
817#ifdef CONFIG_X86_MCE
818 /* notify userspace of pending MCEs */
819 if (thread_info_flags & _TIF_MCE_NOTIFY)
820 mce_notify_process();
821#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
822
823 /* deal with pending signal delivery */
824 if (thread_info_flags & _TIF_SIGPENDING)
825 do_signal(regs);
826
827 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
828 clear_thread_flag(TIF_NOTIFY_RESUME);
829 tracehook_notify_resume(regs);
830 if (current->replacement_session_keyring)
831 key_replace_session_keyring();
832 }
833 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
834 fire_user_return_notifiers();
835
836#ifdef CONFIG_X86_32
837 clear_thread_flag(TIF_IRET);
838#endif /* CONFIG_X86_32 */
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(" in ", regs->ip);
852 printk(KERN_CONT "\n");
853 }
854
855 force_sig(SIGSEGV, me);
856}