blob: 0052bbe5dfd43367aa2e7143ed4b33daa1e9779e [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>
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080045#include <asm/sigframe.h>
Dmitry Safonov68463512016-09-05 16:33:08 +030046#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080048#ifdef CONFIG_X86_64
49/*
50 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
51 * alone. Using this generally makes no sense unless
52 * user_64bit_mode(regs) would return true.
53 */
54static void force_valid_ss(struct pt_regs *regs)
55{
56 u32 ar;
57 asm volatile ("lar %[old_ss], %[ar]\n\t"
58 "jz 1f\n\t" /* If invalid: */
59 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
60 "1:"
61 : [ar] "=r" (ar)
62 : [old_ss] "rm" ((u16)regs->ss));
63
64 /*
65 * For a valid 64-bit user context, we need DPL 3, type
66 * read-write data or read-write exp-down data, and S and P
67 * set. We can't use VERW because VERW doesn't check the
68 * P bit.
69 */
70 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
71 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
72 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
73 regs->ss = __USER_DS;
74}
Al Viro3add42c2020-02-15 12:56:57 -050075# define CONTEXT_COPY_SIZE offsetof(struct sigcontext, reserved1)
76#else
77# define CONTEXT_COPY_SIZE sizeof(struct sigcontext)
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080078#endif
79
Andy Lutomirski6c25da52016-02-16 15:09:03 -080080static int restore_sigcontext(struct pt_regs *regs,
Al Viro3add42c2020-02-15 12:56:57 -050081 struct sigcontext __user *usc,
Andy Lutomirski6c25da52016-02-16 15:09:03 -080082 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080083{
Al Viro3add42c2020-02-15 12:56:57 -050084 struct sigcontext sc;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080085
86 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080087 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080088
Al Viro3add42c2020-02-15 12:56:57 -050089 if (copy_from_user(&sc, usc, CONTEXT_COPY_SIZE))
90 return -EFAULT;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080091
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080092#ifdef CONFIG_X86_32
Al Viro3add42c2020-02-15 12:56:57 -050093 set_user_gs(regs, sc.gs);
94 regs->fs = sc.fs;
95 regs->es = sc.es;
96 regs->ds = sc.ds;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080097#endif /* CONFIG_X86_32 */
98
Al Viro3add42c2020-02-15 12:56:57 -050099 regs->bx = sc.bx;
100 regs->cx = sc.cx;
101 regs->dx = sc.dx;
102 regs->si = sc.si;
103 regs->di = sc.di;
104 regs->bp = sc.bp;
105 regs->ax = sc.ax;
106 regs->sp = sc.sp;
107 regs->ip = sc.ip;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108
109#ifdef CONFIG_X86_64
Al Viro3add42c2020-02-15 12:56:57 -0500110 regs->r8 = sc.r8;
111 regs->r9 = sc.r9;
112 regs->r10 = sc.r10;
113 regs->r11 = sc.r11;
114 regs->r12 = sc.r12;
115 regs->r13 = sc.r13;
116 regs->r14 = sc.r14;
117 regs->r15 = sc.r15;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118#endif /* CONFIG_X86_64 */
119
Al Viro3add42c2020-02-15 12:56:57 -0500120 /* Get CS/SS and force CPL3 */
121 regs->cs = sc.cs | 0x03;
122 regs->ss = sc.ss | 0x03;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800123
Al Viro3add42c2020-02-15 12:56:57 -0500124 regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
125 /* disable syscall checks */
126 regs->orig_ax = -1;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800127
Peter Zijlstra88e47182019-04-03 09:39:48 +0200128#ifdef CONFIG_X86_64
129 /*
130 * Fix up SS if needed for the benefit of old DOSEMU and
131 * CRIU.
132 */
133 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
134 force_valid_ss(regs);
135#endif
136
Al Viro3add42c2020-02-15 12:56:57 -0500137 return fpu__restore_sig((void __user *)sc.fpstate,
138 IS_ENABLED(CONFIG_X86_32));
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800139}
140
Al Virob00d8f82020-02-15 21:12:26 -0500141static __always_inline int
142__unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
H. Peter Anvin85139422012-02-19 07:43:09 -0800143 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144{
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800145#ifdef CONFIG_X86_32
Al Viro9f855c02020-02-15 17:25:27 -0500146 unsafe_put_user(get_user_gs(regs),
147 (unsigned int __user *)&sc->gs, Efault);
148 unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
149 unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
150 unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800151#endif /* CONFIG_X86_32 */
152
Al Viro9f855c02020-02-15 17:25:27 -0500153 unsafe_put_user(regs->di, &sc->di, Efault);
154 unsafe_put_user(regs->si, &sc->si, Efault);
155 unsafe_put_user(regs->bp, &sc->bp, Efault);
156 unsafe_put_user(regs->sp, &sc->sp, Efault);
157 unsafe_put_user(regs->bx, &sc->bx, Efault);
158 unsafe_put_user(regs->dx, &sc->dx, Efault);
159 unsafe_put_user(regs->cx, &sc->cx, Efault);
160 unsafe_put_user(regs->ax, &sc->ax, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800161#ifdef CONFIG_X86_64
Al Viro9f855c02020-02-15 17:25:27 -0500162 unsafe_put_user(regs->r8, &sc->r8, Efault);
163 unsafe_put_user(regs->r9, &sc->r9, Efault);
164 unsafe_put_user(regs->r10, &sc->r10, Efault);
165 unsafe_put_user(regs->r11, &sc->r11, Efault);
166 unsafe_put_user(regs->r12, &sc->r12, Efault);
167 unsafe_put_user(regs->r13, &sc->r13, Efault);
168 unsafe_put_user(regs->r14, &sc->r14, Efault);
169 unsafe_put_user(regs->r15, &sc->r15, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800170#endif /* CONFIG_X86_64 */
171
Al Viro9f855c02020-02-15 17:25:27 -0500172 unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
173 unsafe_put_user(current->thread.error_code, &sc->err, Efault);
174 unsafe_put_user(regs->ip, &sc->ip, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175#ifdef CONFIG_X86_32
Al Viro9f855c02020-02-15 17:25:27 -0500176 unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
177 unsafe_put_user(regs->flags, &sc->flags, Efault);
178 unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
179 unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800180#else /* !CONFIG_X86_32 */
Al Viro9f855c02020-02-15 17:25:27 -0500181 unsafe_put_user(regs->flags, &sc->flags, Efault);
182 unsafe_put_user(regs->cs, &sc->cs, Efault);
183 unsafe_put_user(0, &sc->gs, Efault);
184 unsafe_put_user(0, &sc->fs, Efault);
185 unsafe_put_user(regs->ss, &sc->ss, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800186#endif /* CONFIG_X86_32 */
187
Al Viro9f855c02020-02-15 17:25:27 -0500188 unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800189
Al Viro9f855c02020-02-15 17:25:27 -0500190 /* non-iBCS2 extensions.. */
191 unsafe_put_user(mask, &sc->oldmask, Efault);
192 unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
Al Viro9f855c02020-02-15 17:25:27 -0500193 return 0;
194Efault:
Al Viro9f855c02020-02-15 17:25:27 -0500195 return -EFAULT;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800196}
197
Al Virob00d8f82020-02-15 21:12:26 -0500198#define unsafe_put_sigcontext(sc, fp, regs, set, label) \
199do { \
200 if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0])) \
201 goto label; \
202} while(0);
203
Al Virob87df652020-02-15 21:36:52 -0500204#define unsafe_put_sigmask(set, frame, label) \
205 unsafe_put_user(*(__u64 *)(set), \
206 (__u64 __user *)&(frame)->uc.uc_sigmask, \
207 label)
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * Set up a signal frame.
211 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800212
213/*
214 * Determine which stack to use..
215 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800216static unsigned long align_sigframe(unsigned long sp)
217{
218#ifdef CONFIG_X86_32
219 /*
220 * Align the stack pointer according to the i386 ABI,
221 * i.e. so that on function entry ((sp + 4) & 15) == 0.
222 */
223 sp = ((sp + 4) & -16ul) - 4;
224#else /* !CONFIG_X86_32 */
225 sp = round_down(sp, 16) - 8;
226#endif
227 return sp;
228}
229
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200230static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800231get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
232 void __user **fpstate)
233{
234 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700235 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800236 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700237 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700238 int onsigstack = on_sig_stack(sp);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200239 int ret;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800240
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800241 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700242 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700243 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800244
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300245 /* This is the X/Open sanctioned signal stack switching. */
246 if (ka->sa.sa_flags & SA_ONSTACK) {
247 if (sas_ss_flags(sp) == 0)
248 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700249 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300250 !onsigstack &&
Andy Lutomirski99504812017-07-28 06:00:32 -0700251 regs->ss != __USER_DS &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300252 !(ka->sa.sa_flags & SA_RESTORER) &&
253 ka->sa.sa_restorer) {
254 /* This is the legacy signal stack switching. */
255 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800256 }
257
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200258 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
259 &buf_fx, &math_size);
260 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800261
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700262 sp = align_sigframe(sp - frame_size);
263
264 /*
265 * If we are on the alternate signal stack and would overflow it, don't.
266 * Return an always-bogus address instead so we will die with SIGSEGV.
267 */
268 if (onsigstack && !likely(on_sig_stack(sp)))
269 return (void __user *)-1L;
270
Suresh Siddha72a671c2012-07-24 16:05:29 -0700271 /* save i387 and extended state */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200272 ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
273 if (ret < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700274 return (void __user *)-1L;
275
276 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800277}
278
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800279#ifdef CONFIG_X86_32
280static const struct {
281 u16 poplmovl;
282 u32 val;
283 u16 int80;
284} __attribute__((packed)) retcode = {
285 0xb858, /* popl %eax; movl $..., %eax */
286 __NR_sigreturn,
287 0x80cd, /* int $0x80 */
288};
289
290static const struct {
291 u8 movl;
292 u32 val;
293 u16 int80;
294 u8 pad;
295} __attribute__((packed)) rt_retcode = {
296 0xb8, /* movl $..., %eax */
297 __NR_rt_sigreturn,
298 0x80cd, /* int $0x80 */
299 0
300};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Ingo Molnar7e907f42008-03-06 10:33:08 +0100302static int
Al Viro235b8022012-11-09 23:51:47 -0500303__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700304 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100307 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500308 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Al Virob00d8f82020-02-15 21:12:26 -0500310 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Al Viro5c1f1782020-02-15 21:18:02 -0500312 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700313 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Al Viro5c1f1782020-02-15 21:18:02 -0500315 unsafe_put_user(sig, &frame->sig, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500316 unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
Al Viro5c1f1782020-02-15 21:18:02 -0500317 unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700318 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700319 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700320 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100321 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100322 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500323 if (ksig->ka.sa.sa_flags & SA_RESTORER)
324 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Set up to return from userspace. */
Al Viro5c1f1782020-02-15 21:18:02 -0500327 unsafe_put_user(restorer, &frame->pretcode, Efault);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100330 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
332 * WE DO NOT USE IT ANY MORE! It's only left here for historical
333 * reasons and because gdb uses it as a signature to notice
334 * signal handler stack frames.
335 */
Al Viro5c1f1782020-02-15 21:18:02 -0500336 unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
337 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100340 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500341 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100342 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800343 regs->dx = 0;
344 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100346 regs->ds = __USER_DS;
347 regs->es = __USER_DS;
348 regs->ss = __USER_DS;
349 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David Howells283828f2006-01-18 17:44:00 -0800351 return 0;
Al Virob00d8f82020-02-15 21:12:26 -0500352
353Efault:
354 user_access_end();
355 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Al Viro235b8022012-11-09 23:51:47 -0500358static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700359 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100362 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500363 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Al Virob00d8f82020-02-15 21:12:26 -0500365 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Al Viro119cd592020-02-15 19:54:56 -0500367 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700368 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Al Viro119cd592020-02-15 19:54:56 -0500370 unsafe_put_user(sig, &frame->sig, Efault);
371 unsafe_put_user(&frame->info, &frame->pinfo, Efault);
372 unsafe_put_user(&frame->uc, &frame->puc, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Al Viro119cd592020-02-15 19:54:56 -0500374 /* Create the ucontext. */
375 if (static_cpu_has(X86_FEATURE_XSAVE))
376 unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
377 else
378 unsafe_put_user(0, &frame->uc.uc_flags, Efault);
379 unsafe_put_user(0, &frame->uc.uc_link, Efault);
380 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Al Viro119cd592020-02-15 19:54:56 -0500382 /* Set up to return from userspace. */
383 restorer = current->mm->context.vdso +
384 vdso_image_32.sym___kernel_rt_sigreturn;
385 if (ksig->ka.sa.sa_flags & SA_RESTORER)
386 restorer = ksig->ka.sa.sa_restorer;
387 unsafe_put_user(restorer, &frame->pretcode, Efault);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100388
Al Viro119cd592020-02-15 19:54:56 -0500389 /*
390 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
391 *
392 * WE DO NOT USE IT ANY MORE! It's only left here for historical
393 * reasons and because gdb uses it as a signature to notice
394 * signal handler stack frames.
395 */
396 unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500397 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500398 unsafe_put_sigmask(set, frame, Efault);
Al Viro119cd592020-02-15 19:54:56 -0500399 user_access_end();
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700400
Al Viroead8e4e2020-02-15 21:22:39 -0500401 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700402 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100405 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500406 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700407 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100408 regs->dx = (unsigned long)&frame->info;
409 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100411 regs->ds = __USER_DS;
412 regs->es = __USER_DS;
413 regs->ss = __USER_DS;
414 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
David Howells283828f2006-01-18 17:44:00 -0800416 return 0;
Al Viro119cd592020-02-15 19:54:56 -0500417Efault:
418 user_access_end();
419 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800421#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800422static unsigned long frame_uc_flags(struct pt_regs *regs)
423{
424 unsigned long flags;
425
Borislav Petkovd366bf72016-04-04 22:25:02 +0200426 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800427 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
428 else
429 flags = UC_SIGCONTEXT_SS;
430
431 if (likely(user_64bit_mode(regs)))
432 flags |= UC_STRICT_RESTORE_SS;
433
434 return flags;
435}
436
Al Viro235b8022012-11-09 23:51:47 -0500437static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800438 sigset_t *set, struct pt_regs *regs)
439{
440 struct rt_sigframe __user *frame;
441 void __user *fp = NULL;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200442 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800443
Al Viro119cd592020-02-15 19:54:56 -0500444 /* x86-64 should always use SA_RESTORER. */
445 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
446 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800447
Al Viro235b8022012-11-09 23:51:47 -0500448 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Peter Zijlstra88e47182019-04-03 09:39:48 +0200449 uc_flags = frame_uc_flags(regs);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800450
Al Viro119cd592020-02-15 19:54:56 -0500451 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800452 return -EFAULT;
453
Al Viro119cd592020-02-15 19:54:56 -0500454 /* Create the ucontext. */
455 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
456 unsafe_put_user(0, &frame->uc.uc_link, Efault);
457 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800458
Al Viro119cd592020-02-15 19:54:56 -0500459 /* Set up to return from userspace. If provided, use a stub
460 already in userspace. */
461 unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500462 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500463 unsafe_put_sigmask(set, frame, Efault);
Al Viro119cd592020-02-15 19:54:56 -0500464 user_access_end();
H. Peter Anvin5e883532012-09-21 12:43:15 -0700465
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800466 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
467 if (copy_siginfo_to_user(&frame->info, &ksig->info))
468 return -EFAULT;
469 }
470
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800471 /* Set up registers for signal handler */
472 regs->di = sig;
473 /* In case the signal handler was declared without prototypes */
474 regs->ax = 0;
475
476 /* This also works for non SA_SIGINFO handlers because they expect the
477 next argument after the signal number on the stack. */
478 regs->si = (unsigned long)&frame->info;
479 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500480 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800481
482 regs->sp = (unsigned long)frame;
483
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800484 /*
485 * Set up the CS and SS registers to run signal handlers in
486 * 64-bit mode, even if the handler happens to be interrupting
487 * 32-bit or 16-bit code.
488 *
489 * SS is subtle. In 64-bit mode, we don't need any particular
490 * SS descriptor, but we do need SS to be valid. It's possible
491 * that the old SS is entirely bogus -- this can happen if the
492 * signal we're trying to deliver is #GP or #SS caused by a bad
493 * SS value. We also have a compatbility issue here: DOSEMU
494 * relies on the contents of the SS register indicating the
495 * SS value at the time of the signal, even though that code in
496 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
497 * avoids relying on sigreturn to restore SS; instead it uses
498 * a trampoline.) So we do our best: if the old SS was valid,
499 * we keep it. Otherwise we replace it.
500 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800501 regs->cs = __USER_CS;
502
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800503 if (unlikely(regs->ss != __USER_DS))
504 force_valid_ss(regs);
505
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800506 return 0;
Al Viro119cd592020-02-15 19:54:56 -0500507
508Efault:
509 user_access_end();
510 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800511}
512#endif /* CONFIG_X86_32 */
513
Al Viro235b8022012-11-09 23:51:47 -0500514static int x32_setup_rt_frame(struct ksignal *ksig,
515 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700516 struct pt_regs *regs)
517{
518#ifdef CONFIG_X86_X32_ABI
519 struct rt_sigframe_x32 __user *frame;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200520 unsigned long uc_flags;
Suresh Siddha050902c2012-07-24 16:05:27 -0700521 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500522 void __user *fp = NULL;
Suresh Siddha050902c2012-07-24 16:05:27 -0700523
Al Viro39f16c12020-02-15 18:39:17 -0500524 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
Suresh Siddha050902c2012-07-24 16:05:27 -0700525 return -EFAULT;
526
Al Virob00d8f82020-02-15 21:12:26 -0500527 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700528
Peter Zijlstra88e47182019-04-03 09:39:48 +0200529 uc_flags = frame_uc_flags(regs);
530
Al Viro39f16c12020-02-15 18:39:17 -0500531 if (!user_access_begin(frame, sizeof(*frame)))
532 return -EFAULT;
Suresh Siddha050902c2012-07-24 16:05:27 -0700533
Al Viro39f16c12020-02-15 18:39:17 -0500534 /* Create the ucontext. */
535 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
536 unsafe_put_user(0, &frame->uc.uc_link, Efault);
537 unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
538 unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
539 restorer = ksig->ka.sa.sa_restorer;
540 unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500541 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500542 unsafe_put_sigmask(set, frame, Efault);
Al Viro39f16c12020-02-15 18:39:17 -0500543 user_access_end();
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700544
Suresh Siddha050902c2012-07-24 16:05:27 -0700545 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
546 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
547 return -EFAULT;
548 }
549
Suresh Siddha050902c2012-07-24 16:05:27 -0700550 /* Set up registers for signal handler */
551 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500552 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700553
554 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500555 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700556 regs->si = (unsigned long) &frame->info;
557 regs->dx = (unsigned long) &frame->uc;
558
559 loadsegment(ds, __USER_DS);
560 loadsegment(es, __USER_DS);
561
562 regs->cs = __USER_CS;
563 regs->ss = __USER_DS;
564#endif /* CONFIG_X86_X32_ABI */
565
566 return 0;
Al Viro39f16c12020-02-15 18:39:17 -0500567#ifdef CONFIG_X86_X32_ABI
568Efault:
569 user_access_end();
570 return -EFAULT;
571#endif
Suresh Siddha050902c2012-07-24 16:05:27 -0700572}
573
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800574/*
575 * Do a signal return; undo the signal stack.
576 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800577#ifdef CONFIG_X86_32
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000578SYSCALL_DEFINE0(sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800579{
Al Viro3fe26fa2012-11-12 14:32:42 -0500580 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800581 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800582 sigset_t set;
583
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800584 frame = (struct sigframe __user *)(regs->sp - 8);
585
Linus Torvalds96d4f262019-01-03 18:57:57 -0800586 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800587 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500588 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
589 __get_user(set.sig[1], &frame->extramask[0]))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800590 goto badframe;
591
Oleg Nesterov39822942011-07-10 21:27:27 +0200592 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800593
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800594 /*
595 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
596 * Save a few cycles by skipping the __get_user.
597 */
598 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800599 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400600 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800601
602badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800603 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800604
605 return 0;
606}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800607#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800608
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000609SYSCALL_DEFINE0(rt_sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800610{
Al Viro3fe26fa2012-11-12 14:32:42 -0500611 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800612 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800613 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800614 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800615
616 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800617 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800618 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500619 if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800620 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800621 if (__get_user(uc_flags, &frame->uc.uc_flags))
622 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800623
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200624 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800625
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800626 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800627 goto badframe;
628
Al Viroc40702c2012-11-20 14:24:26 -0500629 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800630 goto badframe;
631
Brian Gerst6a3713f2015-04-04 08:58:23 -0400632 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800633
634badframe:
635 signal_fault(regs, frame, "rt_sigreturn");
636 return 0;
637}
638
Dmitry Safonov68463512016-09-05 16:33:08 +0300639static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200640{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700641 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300642 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200643}
644
Dmitry Safonov68463512016-09-05 16:33:08 +0300645static inline int is_ia32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200646{
Dmitry Safonov68463512016-09-05 16:33:08 +0300647 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200648}
649
Dmitry Safonov68463512016-09-05 16:33:08 +0300650static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200651{
Dmitry Safonov68463512016-09-05 16:33:08 +0300652 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
653 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200654}
655
Roland McGrath7c1def12005-06-23 00:08:21 -0700656static int
Al Viro235b8022012-11-09 23:51:47 -0500657setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700658{
Richard Weinberger3050a352014-07-13 17:43:51 +0200659 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400660 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700661 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700662
Mathieu Desnoyersbff95042019-03-05 14:47:53 -0500663 /* Perform fixup for the pre-signal frame. */
Will Deacon784e0302018-06-22 11:45:07 +0100664 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400665
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700666 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300667 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500668 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
669 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700670 else
Al Viro235b8022012-11-09 23:51:47 -0500671 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300672 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500673 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800674 } else {
Al Viro235b8022012-11-09 23:51:47 -0500675 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800676 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700677}
678
Al Viroa610d6e2012-05-21 23:42:15 -0400679static void
Al Viro235b8022012-11-09 23:51:47 -0500680handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700682 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200683 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700684
Brian Gerst5ed92a82015-07-29 01:41:19 -0400685 if (v8086_mode(regs))
686 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700689 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700691 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800692 case -ERESTART_RESTARTBLOCK:
693 case -ERESTARTNOHAND:
694 regs->ax = -EINTR;
695 break;
696
697 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500698 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100699 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800701 }
702 /* fallthrough */
703 case -ERESTARTNOINTR:
704 regs->ax = regs->orig_ax;
705 regs->ip -= 2;
706 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 }
709
710 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700711 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
712 * so that register information in the sigcontext is correct and
713 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700715 stepping = test_thread_flag(TIF_SINGLESTEP);
716 if (stepping)
717 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Al Viro235b8022012-11-09 23:51:47 -0500719 failed = (setup_rt_frame(ksig, regs) < 0);
720 if (!failed) {
721 /*
722 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200723 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200724 * Clear RF when entering the signal handler, because
725 * it might disable possible debug exception from the
726 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200727 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700728 * Clear TF for the case when it wasn't set by debugger to
729 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500730 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200731 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200732 /*
733 * Ensure the signal handler starts with the new fpu state.
734 */
Fenghua Yub860eb82020-05-12 07:54:39 -0700735 fpu__clear_user_states(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400736 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700737 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300740static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
741{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700742 /*
743 * This function is fundamentally broken as currently
744 * implemented.
745 *
746 * The idea is that we want to trigger a call to the
747 * restart_block() syscall and that we want in_ia32_syscall(),
748 * in_x32_syscall(), etc. to match whatever they were in the
749 * syscall being restarted. We assume that the syscall
750 * instruction at (regs->ip - 2) matches whatever syscall
751 * instruction we used to enter in the first place.
752 *
753 * The problem is that we can get here when ptrace pokes
754 * syscall-like values into regs even if we're not in a syscall
755 * at all.
756 *
757 * For now, we maintain historical behavior and guess based on
758 * stored state. We could do better by saving the actual
759 * syscall arch in restart_block or (with caveats on x32) by
760 * checking if regs->ip points to 'int $0x80'. The current
761 * behavior is incorrect if a tracer has a different bitness
762 * than the tracee.
763 */
764#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800765 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000766 return __NR_ia32_restart_syscall;
767#endif
768#ifdef CONFIG_X86_X32_ABI
769 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
770#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300771 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000772#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300773}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/*
776 * Note that 'init' is a special process: it doesn't get signals it doesn't
777 * want to handle. Thus you cannot kill init even with a SIGKILL even by
778 * mistake.
779 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700780void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Al Viro235b8022012-11-09 23:51:47 -0500782 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Al Viro235b8022012-11-09 23:51:47 -0500784 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100785 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500786 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800787 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700791 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700793 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800794 case -ERESTARTNOHAND:
795 case -ERESTARTSYS:
796 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100797 regs->ax = regs->orig_ax;
798 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800799 break;
800
801 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300802 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100803 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 }
David Howells283828f2006-01-18 17:44:00 -0800807
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800808 /*
809 * If there's no signal to deliver, we just put the saved sigmask
810 * back.
811 */
Al Viro51a7b442012-05-21 23:33:55 -0400812 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700815void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
816{
817 struct task_struct *me = current;
818
819 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800820 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700821 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800822 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700823 me->comm, me->pid, where, frame,
824 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200825 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700826 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700827 }
828
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500829 force_sig(SIGSEGV);
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700830}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800831
832#ifdef CONFIG_X86_X32_ABI
Brian Gerst27dd84f2020-03-13 15:51:31 -0400833COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800834{
Al Viro3fe26fa2012-11-12 14:32:42 -0500835 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800836 struct rt_sigframe_x32 __user *frame;
837 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800838 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800839
840 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
841
Linus Torvalds96d4f262019-01-03 18:57:57 -0800842 if (!access_ok(frame, sizeof(*frame)))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800843 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500844 if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800845 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800846 if (__get_user(uc_flags, &frame->uc.uc_flags))
847 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800848
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800849 set_current_blocked(&set);
850
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800851 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800852 goto badframe;
853
Al Viro90268432012-12-14 14:47:53 -0500854 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800855 goto badframe;
856
Brian Gerst6a3713f2015-04-04 08:58:23 -0400857 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800858
859badframe:
860 signal_fault(regs, frame, "x32 rt_sigreturn");
861 return 0;
862}
863#endif