blob: f3df262e370b328e1bd963e8d54321acc83918c0 [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
Christoph Hellwigc3b3f522020-05-05 12:12:53 +020040#include <linux/compat.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080041#include <asm/proto.h>
42#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080043#endif /* CONFIG_X86_64 */
44
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070045#include <asm/syscall.h>
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080046#include <asm/sigframe.h>
Dmitry Safonov68463512016-09-05 16:33:08 +030047#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080049#ifdef CONFIG_X86_64
50/*
51 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
52 * alone. Using this generally makes no sense unless
53 * user_64bit_mode(regs) would return true.
54 */
55static void force_valid_ss(struct pt_regs *regs)
56{
57 u32 ar;
58 asm volatile ("lar %[old_ss], %[ar]\n\t"
59 "jz 1f\n\t" /* If invalid: */
60 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
61 "1:"
62 : [ar] "=r" (ar)
63 : [old_ss] "rm" ((u16)regs->ss));
64
65 /*
66 * For a valid 64-bit user context, we need DPL 3, type
67 * read-write data or read-write exp-down data, and S and P
68 * set. We can't use VERW because VERW doesn't check the
69 * P bit.
70 */
71 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
72 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
73 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
74 regs->ss = __USER_DS;
75}
Al Viro3add42c2020-02-15 12:56:57 -050076# define CONTEXT_COPY_SIZE offsetof(struct sigcontext, reserved1)
77#else
78# define CONTEXT_COPY_SIZE sizeof(struct sigcontext)
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080079#endif
80
Andy Lutomirski6c25da52016-02-16 15:09:03 -080081static int restore_sigcontext(struct pt_regs *regs,
Al Viro3add42c2020-02-15 12:56:57 -050082 struct sigcontext __user *usc,
Andy Lutomirski6c25da52016-02-16 15:09:03 -080083 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080084{
Al Viro3add42c2020-02-15 12:56:57 -050085 struct sigcontext sc;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080086
87 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080088 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080089
Al Viro3add42c2020-02-15 12:56:57 -050090 if (copy_from_user(&sc, usc, CONTEXT_COPY_SIZE))
91 return -EFAULT;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080092
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080093#ifdef CONFIG_X86_32
Al Viro3add42c2020-02-15 12:56:57 -050094 set_user_gs(regs, sc.gs);
95 regs->fs = sc.fs;
96 regs->es = sc.es;
97 regs->ds = sc.ds;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080098#endif /* CONFIG_X86_32 */
99
Al Viro3add42c2020-02-15 12:56:57 -0500100 regs->bx = sc.bx;
101 regs->cx = sc.cx;
102 regs->dx = sc.dx;
103 regs->si = sc.si;
104 regs->di = sc.di;
105 regs->bp = sc.bp;
106 regs->ax = sc.ax;
107 regs->sp = sc.sp;
108 regs->ip = sc.ip;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800109
110#ifdef CONFIG_X86_64
Al Viro3add42c2020-02-15 12:56:57 -0500111 regs->r8 = sc.r8;
112 regs->r9 = sc.r9;
113 regs->r10 = sc.r10;
114 regs->r11 = sc.r11;
115 regs->r12 = sc.r12;
116 regs->r13 = sc.r13;
117 regs->r14 = sc.r14;
118 regs->r15 = sc.r15;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800119#endif /* CONFIG_X86_64 */
120
Al Viro3add42c2020-02-15 12:56:57 -0500121 /* Get CS/SS and force CPL3 */
122 regs->cs = sc.cs | 0x03;
123 regs->ss = sc.ss | 0x03;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800124
Al Viro3add42c2020-02-15 12:56:57 -0500125 regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
126 /* disable syscall checks */
127 regs->orig_ax = -1;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800128
Peter Zijlstra88e47182019-04-03 09:39:48 +0200129#ifdef CONFIG_X86_64
130 /*
131 * Fix up SS if needed for the benefit of old DOSEMU and
132 * CRIU.
133 */
134 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
135 force_valid_ss(regs);
136#endif
137
Al Viro3add42c2020-02-15 12:56:57 -0500138 return fpu__restore_sig((void __user *)sc.fpstate,
139 IS_ENABLED(CONFIG_X86_32));
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800140}
141
Al Virob00d8f82020-02-15 21:12:26 -0500142static __always_inline int
143__unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
H. Peter Anvin85139422012-02-19 07:43:09 -0800144 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800145{
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800146#ifdef CONFIG_X86_32
Al Viro9f855c02020-02-15 17:25:27 -0500147 unsafe_put_user(get_user_gs(regs),
148 (unsigned int __user *)&sc->gs, Efault);
149 unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
150 unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
151 unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800152#endif /* CONFIG_X86_32 */
153
Al Viro9f855c02020-02-15 17:25:27 -0500154 unsafe_put_user(regs->di, &sc->di, Efault);
155 unsafe_put_user(regs->si, &sc->si, Efault);
156 unsafe_put_user(regs->bp, &sc->bp, Efault);
157 unsafe_put_user(regs->sp, &sc->sp, Efault);
158 unsafe_put_user(regs->bx, &sc->bx, Efault);
159 unsafe_put_user(regs->dx, &sc->dx, Efault);
160 unsafe_put_user(regs->cx, &sc->cx, Efault);
161 unsafe_put_user(regs->ax, &sc->ax, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162#ifdef CONFIG_X86_64
Al Viro9f855c02020-02-15 17:25:27 -0500163 unsafe_put_user(regs->r8, &sc->r8, Efault);
164 unsafe_put_user(regs->r9, &sc->r9, Efault);
165 unsafe_put_user(regs->r10, &sc->r10, Efault);
166 unsafe_put_user(regs->r11, &sc->r11, Efault);
167 unsafe_put_user(regs->r12, &sc->r12, Efault);
168 unsafe_put_user(regs->r13, &sc->r13, Efault);
169 unsafe_put_user(regs->r14, &sc->r14, Efault);
170 unsafe_put_user(regs->r15, &sc->r15, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800171#endif /* CONFIG_X86_64 */
172
Al Viro9f855c02020-02-15 17:25:27 -0500173 unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
174 unsafe_put_user(current->thread.error_code, &sc->err, Efault);
175 unsafe_put_user(regs->ip, &sc->ip, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800176#ifdef CONFIG_X86_32
Al Viro9f855c02020-02-15 17:25:27 -0500177 unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
178 unsafe_put_user(regs->flags, &sc->flags, Efault);
179 unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
180 unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800181#else /* !CONFIG_X86_32 */
Al Viro9f855c02020-02-15 17:25:27 -0500182 unsafe_put_user(regs->flags, &sc->flags, Efault);
183 unsafe_put_user(regs->cs, &sc->cs, Efault);
184 unsafe_put_user(0, &sc->gs, Efault);
185 unsafe_put_user(0, &sc->fs, Efault);
186 unsafe_put_user(regs->ss, &sc->ss, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800187#endif /* CONFIG_X86_32 */
188
Al Viro9f855c02020-02-15 17:25:27 -0500189 unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800190
Al Viro9f855c02020-02-15 17:25:27 -0500191 /* non-iBCS2 extensions.. */
192 unsafe_put_user(mask, &sc->oldmask, Efault);
193 unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
Al Viro9f855c02020-02-15 17:25:27 -0500194 return 0;
195Efault:
Al Viro9f855c02020-02-15 17:25:27 -0500196 return -EFAULT;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800197}
198
Al Virob00d8f82020-02-15 21:12:26 -0500199#define unsafe_put_sigcontext(sc, fp, regs, set, label) \
200do { \
201 if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0])) \
202 goto label; \
203} while(0);
204
Al Virob87df652020-02-15 21:36:52 -0500205#define unsafe_put_sigmask(set, frame, label) \
206 unsafe_put_user(*(__u64 *)(set), \
207 (__u64 __user *)&(frame)->uc.uc_sigmask, \
208 label)
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * Set up a signal frame.
212 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800213
214/*
215 * Determine which stack to use..
216 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800217static unsigned long align_sigframe(unsigned long sp)
218{
219#ifdef CONFIG_X86_32
220 /*
221 * Align the stack pointer according to the i386 ABI,
222 * i.e. so that on function entry ((sp + 4) & 15) == 0.
223 */
224 sp = ((sp + 4) & -16ul) - 4;
225#else /* !CONFIG_X86_32 */
226 sp = round_down(sp, 16) - 8;
227#endif
228 return sp;
229}
230
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200231static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800232get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
233 void __user **fpstate)
234{
235 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700236 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800237 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700238 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700239 int onsigstack = on_sig_stack(sp);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200240 int ret;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800241
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800242 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700243 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700244 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800245
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300246 /* This is the X/Open sanctioned signal stack switching. */
247 if (ka->sa.sa_flags & SA_ONSTACK) {
248 if (sas_ss_flags(sp) == 0)
249 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700250 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300251 !onsigstack &&
Andy Lutomirski99504812017-07-28 06:00:32 -0700252 regs->ss != __USER_DS &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300253 !(ka->sa.sa_flags & SA_RESTORER) &&
254 ka->sa.sa_restorer) {
255 /* This is the legacy signal stack switching. */
256 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800257 }
258
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200259 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
260 &buf_fx, &math_size);
261 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800262
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700263 sp = align_sigframe(sp - frame_size);
264
265 /*
266 * If we are on the alternate signal stack and would overflow it, don't.
267 * Return an always-bogus address instead so we will die with SIGSEGV.
268 */
269 if (onsigstack && !likely(on_sig_stack(sp)))
270 return (void __user *)-1L;
271
Suresh Siddha72a671c2012-07-24 16:05:29 -0700272 /* save i387 and extended state */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200273 ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
274 if (ret < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700275 return (void __user *)-1L;
276
277 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800278}
279
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800280#ifdef CONFIG_X86_32
281static const struct {
282 u16 poplmovl;
283 u32 val;
284 u16 int80;
285} __attribute__((packed)) retcode = {
286 0xb858, /* popl %eax; movl $..., %eax */
287 __NR_sigreturn,
288 0x80cd, /* int $0x80 */
289};
290
291static const struct {
292 u8 movl;
293 u32 val;
294 u16 int80;
295 u8 pad;
296} __attribute__((packed)) rt_retcode = {
297 0xb8, /* movl $..., %eax */
298 __NR_rt_sigreturn,
299 0x80cd, /* int $0x80 */
300 0
301};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Ingo Molnar7e907f42008-03-06 10:33:08 +0100303static int
Al Viro235b8022012-11-09 23:51:47 -0500304__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700305 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100308 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500309 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Al Virob00d8f82020-02-15 21:12:26 -0500311 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Al Viro5c1f1782020-02-15 21:18:02 -0500313 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700314 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Al Viro5c1f1782020-02-15 21:18:02 -0500316 unsafe_put_user(sig, &frame->sig, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500317 unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
Al Viro5c1f1782020-02-15 21:18:02 -0500318 unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700319 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700320 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700321 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100322 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100323 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500324 if (ksig->ka.sa.sa_flags & SA_RESTORER)
325 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Set up to return from userspace. */
Al Viro5c1f1782020-02-15 21:18:02 -0500328 unsafe_put_user(restorer, &frame->pretcode, Efault);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100331 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
333 * WE DO NOT USE IT ANY MORE! It's only left here for historical
334 * reasons and because gdb uses it as a signature to notice
335 * signal handler stack frames.
336 */
Al Viro5c1f1782020-02-15 21:18:02 -0500337 unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
338 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100341 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500342 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100343 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800344 regs->dx = 0;
345 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100347 regs->ds = __USER_DS;
348 regs->es = __USER_DS;
349 regs->ss = __USER_DS;
350 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
David Howells283828f2006-01-18 17:44:00 -0800352 return 0;
Al Virob00d8f82020-02-15 21:12:26 -0500353
354Efault:
355 user_access_end();
356 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Al Viro235b8022012-11-09 23:51:47 -0500359static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700360 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100363 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500364 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Al Virob00d8f82020-02-15 21:12:26 -0500366 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Al Viro119cd592020-02-15 19:54:56 -0500368 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700369 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Al Viro119cd592020-02-15 19:54:56 -0500371 unsafe_put_user(sig, &frame->sig, Efault);
372 unsafe_put_user(&frame->info, &frame->pinfo, Efault);
373 unsafe_put_user(&frame->uc, &frame->puc, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Al Viro119cd592020-02-15 19:54:56 -0500375 /* Create the ucontext. */
376 if (static_cpu_has(X86_FEATURE_XSAVE))
377 unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
378 else
379 unsafe_put_user(0, &frame->uc.uc_flags, Efault);
380 unsafe_put_user(0, &frame->uc.uc_link, Efault);
381 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Al Viro119cd592020-02-15 19:54:56 -0500383 /* Set up to return from userspace. */
384 restorer = current->mm->context.vdso +
385 vdso_image_32.sym___kernel_rt_sigreturn;
386 if (ksig->ka.sa.sa_flags & SA_RESTORER)
387 restorer = ksig->ka.sa.sa_restorer;
388 unsafe_put_user(restorer, &frame->pretcode, Efault);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100389
Al Viro119cd592020-02-15 19:54:56 -0500390 /*
391 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
392 *
393 * WE DO NOT USE IT ANY MORE! It's only left here for historical
394 * reasons and because gdb uses it as a signature to notice
395 * signal handler stack frames.
396 */
397 unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500398 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500399 unsafe_put_sigmask(set, frame, Efault);
Al Viro119cd592020-02-15 19:54:56 -0500400 user_access_end();
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700401
Al Viroead8e4e2020-02-15 21:22:39 -0500402 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700403 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100406 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500407 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700408 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100409 regs->dx = (unsigned long)&frame->info;
410 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 regs->ds = __USER_DS;
413 regs->es = __USER_DS;
414 regs->ss = __USER_DS;
415 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells283828f2006-01-18 17:44:00 -0800417 return 0;
Al Viro119cd592020-02-15 19:54:56 -0500418Efault:
419 user_access_end();
420 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800422#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800423static unsigned long frame_uc_flags(struct pt_regs *regs)
424{
425 unsigned long flags;
426
Borislav Petkovd366bf72016-04-04 22:25:02 +0200427 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800428 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
429 else
430 flags = UC_SIGCONTEXT_SS;
431
432 if (likely(user_64bit_mode(regs)))
433 flags |= UC_STRICT_RESTORE_SS;
434
435 return flags;
436}
437
Al Viro235b8022012-11-09 23:51:47 -0500438static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800439 sigset_t *set, struct pt_regs *regs)
440{
441 struct rt_sigframe __user *frame;
442 void __user *fp = NULL;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200443 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800444
Al Viro119cd592020-02-15 19:54:56 -0500445 /* x86-64 should always use SA_RESTORER. */
446 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
447 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800448
Al Viro235b8022012-11-09 23:51:47 -0500449 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Peter Zijlstra88e47182019-04-03 09:39:48 +0200450 uc_flags = frame_uc_flags(regs);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800451
Al Viro119cd592020-02-15 19:54:56 -0500452 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800453 return -EFAULT;
454
Al Viro119cd592020-02-15 19:54:56 -0500455 /* Create the ucontext. */
456 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
457 unsafe_put_user(0, &frame->uc.uc_link, Efault);
458 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800459
Al Viro119cd592020-02-15 19:54:56 -0500460 /* Set up to return from userspace. If provided, use a stub
461 already in userspace. */
462 unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500463 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500464 unsafe_put_sigmask(set, frame, Efault);
Al Viro119cd592020-02-15 19:54:56 -0500465 user_access_end();
H. Peter Anvin5e883532012-09-21 12:43:15 -0700466
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800467 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
468 if (copy_siginfo_to_user(&frame->info, &ksig->info))
469 return -EFAULT;
470 }
471
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800472 /* Set up registers for signal handler */
473 regs->di = sig;
474 /* In case the signal handler was declared without prototypes */
475 regs->ax = 0;
476
477 /* This also works for non SA_SIGINFO handlers because they expect the
478 next argument after the signal number on the stack. */
479 regs->si = (unsigned long)&frame->info;
480 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500481 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800482
483 regs->sp = (unsigned long)frame;
484
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800485 /*
486 * Set up the CS and SS registers to run signal handlers in
487 * 64-bit mode, even if the handler happens to be interrupting
488 * 32-bit or 16-bit code.
489 *
490 * SS is subtle. In 64-bit mode, we don't need any particular
491 * SS descriptor, but we do need SS to be valid. It's possible
492 * that the old SS is entirely bogus -- this can happen if the
493 * signal we're trying to deliver is #GP or #SS caused by a bad
494 * SS value. We also have a compatbility issue here: DOSEMU
495 * relies on the contents of the SS register indicating the
496 * SS value at the time of the signal, even though that code in
497 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
498 * avoids relying on sigreturn to restore SS; instead it uses
499 * a trampoline.) So we do our best: if the old SS was valid,
500 * we keep it. Otherwise we replace it.
501 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800502 regs->cs = __USER_CS;
503
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800504 if (unlikely(regs->ss != __USER_DS))
505 force_valid_ss(regs);
506
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800507 return 0;
Al Viro119cd592020-02-15 19:54:56 -0500508
509Efault:
510 user_access_end();
511 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800512}
513#endif /* CONFIG_X86_32 */
514
Christoph Hellwigc3b3f522020-05-05 12:12:53 +0200515#ifdef CONFIG_X86_X32_ABI
516static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
517 const struct kernel_siginfo *from)
518{
519 struct compat_siginfo new;
520
521 copy_siginfo_to_external32(&new, from);
522 if (from->si_signo == SIGCHLD) {
523 new._sifields._sigchld_x32._utime = from->si_utime;
524 new._sifields._sigchld_x32._stime = from->si_stime;
525 }
526 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
527 return -EFAULT;
528 return 0;
529}
530
531int copy_siginfo_to_user32(struct compat_siginfo __user *to,
532 const struct kernel_siginfo *from)
533{
534 if (in_x32_syscall())
535 return x32_copy_siginfo_to_user(to, from);
536 return __copy_siginfo_to_user32(to, from);
537}
538#endif /* CONFIG_X86_X32_ABI */
539
Al Viro235b8022012-11-09 23:51:47 -0500540static int x32_setup_rt_frame(struct ksignal *ksig,
541 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700542 struct pt_regs *regs)
543{
544#ifdef CONFIG_X86_X32_ABI
545 struct rt_sigframe_x32 __user *frame;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200546 unsigned long uc_flags;
Suresh Siddha050902c2012-07-24 16:05:27 -0700547 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500548 void __user *fp = NULL;
Suresh Siddha050902c2012-07-24 16:05:27 -0700549
Al Viro39f16c12020-02-15 18:39:17 -0500550 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
Suresh Siddha050902c2012-07-24 16:05:27 -0700551 return -EFAULT;
552
Al Virob00d8f82020-02-15 21:12:26 -0500553 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700554
Peter Zijlstra88e47182019-04-03 09:39:48 +0200555 uc_flags = frame_uc_flags(regs);
556
Al Viro39f16c12020-02-15 18:39:17 -0500557 if (!user_access_begin(frame, sizeof(*frame)))
558 return -EFAULT;
Suresh Siddha050902c2012-07-24 16:05:27 -0700559
Al Viro39f16c12020-02-15 18:39:17 -0500560 /* Create the ucontext. */
561 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
562 unsafe_put_user(0, &frame->uc.uc_link, Efault);
563 unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
564 unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
565 restorer = ksig->ka.sa.sa_restorer;
566 unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500567 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500568 unsafe_put_sigmask(set, frame, Efault);
Al Viro39f16c12020-02-15 18:39:17 -0500569 user_access_end();
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700570
Suresh Siddha050902c2012-07-24 16:05:27 -0700571 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Christoph Hellwigc3b3f522020-05-05 12:12:53 +0200572 if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700573 return -EFAULT;
574 }
575
Suresh Siddha050902c2012-07-24 16:05:27 -0700576 /* Set up registers for signal handler */
577 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500578 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700579
580 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500581 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700582 regs->si = (unsigned long) &frame->info;
583 regs->dx = (unsigned long) &frame->uc;
584
585 loadsegment(ds, __USER_DS);
586 loadsegment(es, __USER_DS);
587
588 regs->cs = __USER_CS;
589 regs->ss = __USER_DS;
590#endif /* CONFIG_X86_X32_ABI */
591
592 return 0;
Al Viro39f16c12020-02-15 18:39:17 -0500593#ifdef CONFIG_X86_X32_ABI
594Efault:
595 user_access_end();
596 return -EFAULT;
597#endif
Suresh Siddha050902c2012-07-24 16:05:27 -0700598}
599
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800600/*
601 * Do a signal return; undo the signal stack.
602 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800603#ifdef CONFIG_X86_32
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000604SYSCALL_DEFINE0(sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800605{
Al Viro3fe26fa2012-11-12 14:32:42 -0500606 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800607 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800608 sigset_t set;
609
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800610 frame = (struct sigframe __user *)(regs->sp - 8);
611
Linus Torvalds96d4f262019-01-03 18:57:57 -0800612 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800613 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500614 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
615 __get_user(set.sig[1], &frame->extramask[0]))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800616 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;
Al Viro71c33132020-02-15 11:43:18 -0500645 if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800646 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 Desnoyersbff95042019-03-05 14:47:53 -0500689 /* Perform fixup for the pre-signal frame. */
Will Deacon784e0302018-06-22 11:45:07 +0100690 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400691
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700692 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300693 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500694 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
695 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700696 else
Al Viro235b8022012-11-09 23:51:47 -0500697 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300698 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500699 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800700 } else {
Al Viro235b8022012-11-09 23:51:47 -0500701 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800702 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700703}
704
Al Viroa610d6e2012-05-21 23:42:15 -0400705static void
Al Viro235b8022012-11-09 23:51:47 -0500706handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700708 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200709 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700710
Brian Gerst5ed92a82015-07-29 01:41:19 -0400711 if (v8086_mode(regs))
712 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700715 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700717 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800718 case -ERESTART_RESTARTBLOCK:
719 case -ERESTARTNOHAND:
720 regs->ax = -EINTR;
721 break;
722
723 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500724 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100725 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800727 }
728 /* fallthrough */
729 case -ERESTARTNOINTR:
730 regs->ax = regs->orig_ax;
731 regs->ip -= 2;
732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735
736 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700737 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
738 * so that register information in the sigcontext is correct and
739 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700741 stepping = test_thread_flag(TIF_SINGLESTEP);
742 if (stepping)
743 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Al Viro235b8022012-11-09 23:51:47 -0500745 failed = (setup_rt_frame(ksig, regs) < 0);
746 if (!failed) {
747 /*
748 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200749 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200750 * Clear RF when entering the signal handler, because
751 * it might disable possible debug exception from the
752 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200753 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700754 * Clear TF for the case when it wasn't set by debugger to
755 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500756 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200757 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200758 /*
759 * Ensure the signal handler starts with the new fpu state.
760 */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200761 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400762 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700763 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300766static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
767{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700768 /*
769 * This function is fundamentally broken as currently
770 * implemented.
771 *
772 * The idea is that we want to trigger a call to the
773 * restart_block() syscall and that we want in_ia32_syscall(),
774 * in_x32_syscall(), etc. to match whatever they were in the
775 * syscall being restarted. We assume that the syscall
776 * instruction at (regs->ip - 2) matches whatever syscall
777 * instruction we used to enter in the first place.
778 *
779 * The problem is that we can get here when ptrace pokes
780 * syscall-like values into regs even if we're not in a syscall
781 * at all.
782 *
783 * For now, we maintain historical behavior and guess based on
784 * stored state. We could do better by saving the actual
785 * syscall arch in restart_block or (with caveats on x32) by
786 * checking if regs->ip points to 'int $0x80'. The current
787 * behavior is incorrect if a tracer has a different bitness
788 * than the tracee.
789 */
790#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800791 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000792 return __NR_ia32_restart_syscall;
793#endif
794#ifdef CONFIG_X86_X32_ABI
795 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
796#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300797 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000798#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300799}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * Note that 'init' is a special process: it doesn't get signals it doesn't
803 * want to handle. Thus you cannot kill init even with a SIGKILL even by
804 * mistake.
805 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700806void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Al Viro235b8022012-11-09 23:51:47 -0500808 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Al Viro235b8022012-11-09 23:51:47 -0500810 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100811 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500812 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800813 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700817 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700819 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800820 case -ERESTARTNOHAND:
821 case -ERESTARTSYS:
822 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100823 regs->ax = regs->orig_ax;
824 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800825 break;
826
827 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300828 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100829 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 }
David Howells283828f2006-01-18 17:44:00 -0800833
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800834 /*
835 * If there's no signal to deliver, we just put the saved sigmask
836 * back.
837 */
Al Viro51a7b442012-05-21 23:33:55 -0400838 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700841void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
842{
843 struct task_struct *me = current;
844
845 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800846 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700847 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800848 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700849 me->comm, me->pid, where, frame,
850 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200851 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700852 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700853 }
854
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500855 force_sig(SIGSEGV);
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700856}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800857
858#ifdef CONFIG_X86_X32_ABI
Brian Gerst27dd84f2020-03-13 15:51:31 -0400859COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800860{
Al Viro3fe26fa2012-11-12 14:32:42 -0500861 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800862 struct rt_sigframe_x32 __user *frame;
863 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800864 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800865
866 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
867
Linus Torvalds96d4f262019-01-03 18:57:57 -0800868 if (!access_ok(frame, sizeof(*frame)))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800869 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500870 if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800871 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800872 if (__get_user(uc_flags, &frame->uc.uc_flags))
873 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800874
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800875 set_current_blocked(&set);
876
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800877 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800878 goto badframe;
879
Al Viro90268432012-12-14 14:47:53 -0500880 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800881 goto badframe;
882
Brian Gerst6a3713f2015-04-04 08:58:23 -0400883 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800884
885badframe:
886 signal_fault(regs, frame, "x32 rt_sigreturn");
887 return 0;
888}
889#endif