blob: 87b327c6cb104db8b59f0f1df4f24eb5f82c6df5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992 Linus Torvalds
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08004 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08008 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Joe Perchesc767a542012-05-21 19:50:07 -070010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Ingo Molnar7e907f42008-03-06 10:33:08 +010013#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010014#include <linux/sched/task_stack.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010015#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080016#include <linux/smp.h>
17#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080018#include <linux/errno.h>
19#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080020#include <linux/tracehook.h>
21#include <linux/unistd.h>
22#include <linux/stddef.h>
23#include <linux/personality.h>
24#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030025#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053026#include <linux/uprobes.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010027#include <linux/context_tracking.h>
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +000028#include <linux/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/processor.h>
31#include <asm/ucontext.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020032#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020033#include <asm/fpu/signal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010034#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020035#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080036#include <asm/sighandling.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040037#include <asm/vm86.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080038
39#ifdef CONFIG_X86_64
40#include <asm/proto.h>
41#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080042#endif /* CONFIG_X86_64 */
43
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070044#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053045#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010046
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080047#include <asm/sigframe.h>
Dmitry Safonov68463512016-09-05 16:33:08 +030048#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heod9a89a22009-02-09 22:17:40 +090050#define COPY(x) do { \
51 get_user_ex(regs->x, &sc->x); \
52} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080053
Tejun Heod9a89a22009-02-09 22:17:40 +090054#define GET_SEG(seg) ({ \
55 unsigned short tmp; \
56 get_user_ex(tmp, &sc->seg); \
57 tmp; \
58})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080059
Tejun Heod9a89a22009-02-09 22:17:40 +090060#define COPY_SEG(seg) do { \
61 regs->seg = GET_SEG(seg); \
62} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080063
Tejun Heod9a89a22009-02-09 22:17:40 +090064#define COPY_SEG_CPL3(seg) do { \
65 regs->seg = GET_SEG(seg) | 3; \
66} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080067
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080068#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
Andy Lutomirski6c25da52016-02-16 15:09:03 -080097static int restore_sigcontext(struct pt_regs *regs,
98 struct sigcontext __user *sc,
99 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100{
Ingo Molnar530e5c82015-09-05 09:32:39 +0200101 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800102 void __user *buf;
103 unsigned int tmpflags;
104 unsigned int err = 0;
105
106 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800107 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 get_user_try {
110
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800111#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900112 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 COPY_SEG(fs);
114 COPY_SEG(es);
115 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800116#endif /* CONFIG_X86_32 */
117
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -0400119 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800120
121#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800122 COPY(r8);
123 COPY(r9);
124 COPY(r10);
125 COPY(r11);
126 COPY(r12);
127 COPY(r13);
128 COPY(r14);
129 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800130#endif /* CONFIG_X86_64 */
131
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800132 COPY_SEG_CPL3(cs);
133 COPY_SEG_CPL3(ss);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800134
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
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800145 get_user_ex(tmpflags, &sc->flags);
146 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
147 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800148
Ingo Molnar530e5c82015-09-05 09:32:39 +0200149 get_user_ex(buf_val, &sc->fpstate);
150 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800151 } get_user_catch(err);
152
Masahiro Yamada97f26452016-08-03 13:45:50 -0700153 err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700154
Brian Gerst1daeaa32015-03-21 18:54:21 -0400155 force_iret();
156
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800157 return err;
158}
159
H. Peter Anvin85139422012-02-19 07:43:09 -0800160int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
161 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162{
163 int err = 0;
164
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800165 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800166
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800167#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900168 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800169 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800172#endif /* CONFIG_X86_32 */
173
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800174 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800182#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800183 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800191#endif /* CONFIG_X86_64 */
192
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530193 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800194 put_user_ex(current->thread.error_code, &sc->err);
195 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800196#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800197 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);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800201#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800202 put_user_ex(regs->flags, &sc->flags);
203 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700204 put_user_ex(0, &sc->gs);
205 put_user_ex(0, &sc->fs);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800206 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800207#endif /* CONFIG_X86_32 */
208
Jann Horn89833fa2019-03-29 22:46:51 +0100209 put_user_ex(fpstate, (unsigned long __user *)&sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800210
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800211 /* non-iBCS2 extensions.. */
212 put_user_ex(mask, &sc->oldmask);
213 put_user_ex(current->thread.cr2, &sc->cr2);
214 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800215
216 return err;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * Set up a signal frame.
221 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800222
223/*
224 * Determine which stack to use..
225 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800226static 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
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200240static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800241get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
242 void __user **fpstate)
243{
244 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700245 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800246 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700247 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700248 int onsigstack = on_sig_stack(sp);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200249 int ret;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800250
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800251 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700252 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700253 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800254
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300255 /* 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;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700259 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300260 !onsigstack &&
Andy Lutomirski99504812017-07-28 06:00:32 -0700261 regs->ss != __USER_DS &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300262 !(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;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800266 }
267
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200268 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
269 &buf_fx, &math_size);
270 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800271
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700272 sp = align_sigframe(sp - frame_size);
273
274 /*
275 * If we are on the alternate signal stack and would overflow it, don't.
276 * Return an always-bogus address instead so we will die with SIGSEGV.
277 */
278 if (onsigstack && !likely(on_sig_stack(sp)))
279 return (void __user *)-1L;
280
Suresh Siddha72a671c2012-07-24 16:05:29 -0700281 /* save i387 and extended state */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200282 ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
283 if (ret < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700284 return (void __user *)-1L;
285
286 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800287}
288
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800289#ifdef CONFIG_X86_32
290static const struct {
291 u16 poplmovl;
292 u32 val;
293 u16 int80;
294} __attribute__((packed)) retcode = {
295 0xb858, /* popl %eax; movl $..., %eax */
296 __NR_sigreturn,
297 0x80cd, /* int $0x80 */
298};
299
300static const struct {
301 u8 movl;
302 u32 val;
303 u16 int80;
304 u8 pad;
305} __attribute__((packed)) rt_retcode = {
306 0xb8, /* movl $..., %eax */
307 __NR_rt_sigreturn,
308 0x80cd, /* int $0x80 */
309 0
310};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Ingo Molnar7e907f42008-03-06 10:33:08 +0100312static int
Al Viro235b8022012-11-09 23:51:47 -0500313__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700314 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100317 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700319 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Al Viro235b8022012-11-09 23:51:47 -0500321 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds96d4f262019-01-03 18:57:57 -0800323 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700324 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700326 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700327 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700329 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700330 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700333 if (__copy_to_user(&frame->extramask, &set->sig[1],
334 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700335 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700338 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700339 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700340 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100341 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100342 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500343 if (ksig->ka.sa.sa_flags & SA_RESTORER)
344 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Set up to return from userspace. */
347 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100350 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
352 * WE DO NOT USE IT ANY MORE! It's only left here for historical
353 * reasons and because gdb uses it as a signature to notice
354 * signal handler stack frames.
355 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800356 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700359 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100362 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500363 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100364 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800365 regs->dx = 0;
366 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100368 regs->ds = __USER_DS;
369 regs->es = __USER_DS;
370 regs->ss = __USER_DS;
371 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Howells283828f2006-01-18 17:44:00 -0800373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Al Viro235b8022012-11-09 23:51:47 -0500376static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700377 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100380 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700382 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Al Viro235b8022012-11-09 23:51:47 -0500384 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds96d4f262019-01-03 18:57:57 -0800386 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700387 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800389 put_user_try {
390 put_user_ex(sig, &frame->sig);
391 put_user_ex(&frame->info, &frame->pinfo);
392 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800394 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200395 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800396 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
397 else
398 put_user_ex(0, &frame->uc.uc_flags);
399 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100400 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800402 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700403 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700404 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500405 if (ksig->ka.sa.sa_flags & SA_RESTORER)
406 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800407 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100408
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800409 /*
410 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
411 *
412 * WE DO NOT USE IT ANY MORE! It's only left here for historical
413 * reasons and because gdb uses it as a signature to notice
414 * signal handler stack frames.
415 */
416 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
417 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700418
Al Viro235b8022012-11-09 23:51:47 -0500419 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700420 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
421 regs, set->sig[0]);
422 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700425 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100428 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500429 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700430 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100431 regs->dx = (unsigned long)&frame->info;
432 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100434 regs->ds = __USER_DS;
435 regs->es = __USER_DS;
436 regs->ss = __USER_DS;
437 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David Howells283828f2006-01-18 17:44:00 -0800439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800441#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800442static unsigned long frame_uc_flags(struct pt_regs *regs)
443{
444 unsigned long flags;
445
Borislav Petkovd366bf72016-04-04 22:25:02 +0200446 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800447 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
448 else
449 flags = UC_SIGCONTEXT_SS;
450
451 if (likely(user_64bit_mode(regs)))
452 flags |= UC_STRICT_RESTORE_SS;
453
454 return flags;
455}
456
Al Viro235b8022012-11-09 23:51:47 -0500457static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800458 sigset_t *set, struct pt_regs *regs)
459{
460 struct rt_sigframe __user *frame;
461 void __user *fp = NULL;
462 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800463
Al Viro235b8022012-11-09 23:51:47 -0500464 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800465
Linus Torvalds96d4f262019-01-03 18:57:57 -0800466 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800467 return -EFAULT;
468
Al Viro235b8022012-11-09 23:51:47 -0500469 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
470 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800471 return -EFAULT;
472 }
473
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800474 put_user_try {
475 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800476 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800477 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100478 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800479
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800480 /* Set up to return from userspace. If provided, use a stub
481 already in userspace. */
482 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500483 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
484 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800485 } else {
486 /* could use a vstub here */
487 err |= -EFAULT;
488 }
489 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800490
H. Peter Anvin5e883532012-09-21 12:43:15 -0700491 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
492 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
493
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800494 if (err)
495 return -EFAULT;
496
497 /* Set up registers for signal handler */
498 regs->di = sig;
499 /* In case the signal handler was declared without prototypes */
500 regs->ax = 0;
501
502 /* This also works for non SA_SIGINFO handlers because they expect the
503 next argument after the signal number on the stack. */
504 regs->si = (unsigned long)&frame->info;
505 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500506 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800507
508 regs->sp = (unsigned long)frame;
509
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800510 /*
511 * Set up the CS and SS registers to run signal handlers in
512 * 64-bit mode, even if the handler happens to be interrupting
513 * 32-bit or 16-bit code.
514 *
515 * SS is subtle. In 64-bit mode, we don't need any particular
516 * SS descriptor, but we do need SS to be valid. It's possible
517 * that the old SS is entirely bogus -- this can happen if the
518 * signal we're trying to deliver is #GP or #SS caused by a bad
519 * SS value. We also have a compatbility issue here: DOSEMU
520 * relies on the contents of the SS register indicating the
521 * SS value at the time of the signal, even though that code in
522 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
523 * avoids relying on sigreturn to restore SS; instead it uses
524 * a trampoline.) So we do our best: if the old SS was valid,
525 * we keep it. Otherwise we replace it.
526 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800527 regs->cs = __USER_CS;
528
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800529 if (unlikely(regs->ss != __USER_DS))
530 force_valid_ss(regs);
531
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800532 return 0;
533}
534#endif /* CONFIG_X86_32 */
535
Al Viro235b8022012-11-09 23:51:47 -0500536static int x32_setup_rt_frame(struct ksignal *ksig,
537 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700538 struct pt_regs *regs)
539{
540#ifdef CONFIG_X86_X32_ABI
541 struct rt_sigframe_x32 __user *frame;
542 void __user *restorer;
543 int err = 0;
544 void __user *fpstate = NULL;
545
Al Viro235b8022012-11-09 23:51:47 -0500546 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700547
Linus Torvalds96d4f262019-01-03 18:57:57 -0800548 if (!access_ok(frame, sizeof(*frame)))
Suresh Siddha050902c2012-07-24 16:05:27 -0700549 return -EFAULT;
550
Al Viro235b8022012-11-09 23:51:47 -0500551 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Dmitry Safonov68463512016-09-05 16:33:08 +0300552 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
Suresh Siddha050902c2012-07-24 16:05:27 -0700553 return -EFAULT;
554 }
555
556 put_user_try {
557 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800558 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Suresh Siddha050902c2012-07-24 16:05:27 -0700559 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100560 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700561 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700562
Al Viro235b8022012-11-09 23:51:47 -0500563 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
564 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700565 } else {
566 /* could use a vstub here */
567 restorer = NULL;
568 err |= -EFAULT;
569 }
Jann Horn89833fa2019-03-29 22:46:51 +0100570 put_user_ex(restorer, (unsigned long __user *)&frame->pretcode);
Suresh Siddha050902c2012-07-24 16:05:27 -0700571 } put_user_catch(err);
572
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700573 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
574 regs, set->sig[0]);
575 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
576
Suresh Siddha050902c2012-07-24 16:05:27 -0700577 if (err)
578 return -EFAULT;
579
580 /* Set up registers for signal handler */
581 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500582 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700583
584 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500585 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700586 regs->si = (unsigned long) &frame->info;
587 regs->dx = (unsigned long) &frame->uc;
588
589 loadsegment(ds, __USER_DS);
590 loadsegment(es, __USER_DS);
591
592 regs->cs = __USER_CS;
593 regs->ss = __USER_DS;
594#endif /* CONFIG_X86_X32_ABI */
595
596 return 0;
597}
598
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800599/*
600 * Do a signal return; undo the signal stack.
601 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800602#ifdef CONFIG_X86_32
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000603SYSCALL_DEFINE0(sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800604{
Al Viro3fe26fa2012-11-12 14:32:42 -0500605 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800607 sigset_t set;
608
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800609 frame = (struct sigframe __user *)(regs->sp - 8);
610
Linus Torvalds96d4f262019-01-03 18:57:57 -0800611 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800612 goto badframe;
613 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
614 && __copy_from_user(&set.sig[1], &frame->extramask,
615 sizeof(frame->extramask))))
616 goto badframe;
617
Oleg Nesterov39822942011-07-10 21:27:27 +0200618 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800619
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800620 /*
621 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
622 * Save a few cycles by skipping the __get_user.
623 */
624 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800625 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400626 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800627
628badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800629 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800630
631 return 0;
632}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800633#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800634
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000635SYSCALL_DEFINE0(rt_sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800636{
Al Viro3fe26fa2012-11-12 14:32:42 -0500637 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800638 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800639 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800640 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800641
642 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800643 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800644 goto badframe;
645 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
646 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800647 if (__get_user(uc_flags, &frame->uc.uc_flags))
648 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800649
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200650 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800651
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800652 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800653 goto badframe;
654
Al Viroc40702c2012-11-20 14:24:26 -0500655 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800656 goto badframe;
657
Brian Gerst6a3713f2015-04-04 08:58:23 -0400658 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800659
660badframe:
661 signal_fault(regs, frame, "rt_sigreturn");
662 return 0;
663}
664
Dmitry Safonov68463512016-09-05 16:33:08 +0300665static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200666{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700667 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300668 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200669}
670
Dmitry Safonov68463512016-09-05 16:33:08 +0300671static inline int is_ia32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200672{
Dmitry Safonov68463512016-09-05 16:33:08 +0300673 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200674}
675
Dmitry Safonov68463512016-09-05 16:33:08 +0300676static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200677{
Dmitry Safonov68463512016-09-05 16:33:08 +0300678 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
679 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200680}
681
Roland McGrath7c1def12005-06-23 00:08:21 -0700682static int
Al Viro235b8022012-11-09 23:51:47 -0500683setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700684{
Richard Weinberger3050a352014-07-13 17:43:51 +0200685 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400686 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700687 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700688
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400689 /*
690 * Increment event counter and perform fixup for the pre-signal
691 * frame.
692 */
Will Deacon784e0302018-06-22 11:45:07 +0100693 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400694
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700695 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300696 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500697 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
698 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700699 else
Al Viro235b8022012-11-09 23:51:47 -0500700 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300701 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500702 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800703 } else {
Al Viro235b8022012-11-09 23:51:47 -0500704 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800705 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700706}
707
Al Viroa610d6e2012-05-21 23:42:15 -0400708static void
Al Viro235b8022012-11-09 23:51:47 -0500709handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700711 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200712 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700713
Brian Gerst5ed92a82015-07-29 01:41:19 -0400714 if (v8086_mode(regs))
715 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700718 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700720 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800721 case -ERESTART_RESTARTBLOCK:
722 case -ERESTARTNOHAND:
723 regs->ax = -EINTR;
724 break;
725
726 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500727 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100728 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800730 }
731 /* fallthrough */
732 case -ERESTARTNOINTR:
733 regs->ax = regs->orig_ax;
734 regs->ip -= 2;
735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 }
738
739 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700740 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
741 * so that register information in the sigcontext is correct and
742 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700744 stepping = test_thread_flag(TIF_SINGLESTEP);
745 if (stepping)
746 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Al Viro235b8022012-11-09 23:51:47 -0500748 failed = (setup_rt_frame(ksig, regs) < 0);
749 if (!failed) {
750 /*
751 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200752 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200753 * Clear RF when entering the signal handler, because
754 * it might disable possible debug exception from the
755 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200756 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700757 * Clear TF for the case when it wasn't set by debugger to
758 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500759 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200760 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200761 /*
762 * Ensure the signal handler starts with the new fpu state.
763 */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200764 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400765 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700766 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300769static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
770{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700771 /*
772 * This function is fundamentally broken as currently
773 * implemented.
774 *
775 * The idea is that we want to trigger a call to the
776 * restart_block() syscall and that we want in_ia32_syscall(),
777 * in_x32_syscall(), etc. to match whatever they were in the
778 * syscall being restarted. We assume that the syscall
779 * instruction at (regs->ip - 2) matches whatever syscall
780 * instruction we used to enter in the first place.
781 *
782 * The problem is that we can get here when ptrace pokes
783 * syscall-like values into regs even if we're not in a syscall
784 * at all.
785 *
786 * For now, we maintain historical behavior and guess based on
787 * stored state. We could do better by saving the actual
788 * syscall arch in restart_block or (with caveats on x32) by
789 * checking if regs->ip points to 'int $0x80'. The current
790 * behavior is incorrect if a tracer has a different bitness
791 * than the tracee.
792 */
793#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800794 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000795 return __NR_ia32_restart_syscall;
796#endif
797#ifdef CONFIG_X86_X32_ABI
798 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
799#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300800 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000801#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300802}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/*
805 * Note that 'init' is a special process: it doesn't get signals it doesn't
806 * want to handle. Thus you cannot kill init even with a SIGKILL even by
807 * mistake.
808 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700809void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Al Viro235b8022012-11-09 23:51:47 -0500811 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Al Viro235b8022012-11-09 23:51:47 -0500813 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100814 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500815 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800816 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700820 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700822 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800823 case -ERESTARTNOHAND:
824 case -ERESTARTSYS:
825 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100826 regs->ax = regs->orig_ax;
827 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800828 break;
829
830 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300831 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100832 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 }
David Howells283828f2006-01-18 17:44:00 -0800836
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800837 /*
838 * If there's no signal to deliver, we just put the saved sigmask
839 * back.
840 */
Al Viro51a7b442012-05-21 23:33:55 -0400841 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700844void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
845{
846 struct task_struct *me = current;
847
848 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800849 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700850 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800851 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700852 me->comm, me->pid, where, frame,
853 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200854 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700855 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700856 }
857
858 force_sig(SIGSEGV, me);
859}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800860
861#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500862asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800863{
Al Viro3fe26fa2012-11-12 14:32:42 -0500864 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800865 struct rt_sigframe_x32 __user *frame;
866 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800867 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800868
869 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
870
Linus Torvalds96d4f262019-01-03 18:57:57 -0800871 if (!access_ok(frame, sizeof(*frame)))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800872 goto badframe;
873 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
874 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800875 if (__get_user(uc_flags, &frame->uc.uc_flags))
876 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800877
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800878 set_current_blocked(&set);
879
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800880 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800881 goto badframe;
882
Al Viro90268432012-12-14 14:47:53 -0500883 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800884 goto badframe;
885
Brian Gerst6a3713f2015-04-04 08:58:23 -0400886 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800887
888badframe:
889 signal_fault(regs, frame, "x32 rt_sigreturn");
890 return 0;
891}
892#endif