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