blob: 4de9b1da4509c0a57ee11535a0ce514944e60447 [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>
Thomas Gleixner167fd212020-07-23 00:00:05 +020028#include <linux/entry-common.h>
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +000029#include <linux/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/processor.h>
32#include <asm/ucontext.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020033#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020034#include <asm/fpu/signal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010035#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020036#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080037#include <asm/sighandling.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040038#include <asm/vm86.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080039
40#ifdef CONFIG_X86_64
Christoph Hellwigc3b3f522020-05-05 12:12:53 +020041#include <linux/compat.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080042#include <asm/proto.h>
43#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080044#endif /* CONFIG_X86_64 */
45
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070046#include <asm/syscall.h>
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
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080050#ifdef CONFIG_X86_64
51/*
52 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
53 * alone. Using this generally makes no sense unless
54 * user_64bit_mode(regs) would return true.
55 */
56static void force_valid_ss(struct pt_regs *regs)
57{
58 u32 ar;
59 asm volatile ("lar %[old_ss], %[ar]\n\t"
60 "jz 1f\n\t" /* If invalid: */
61 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
62 "1:"
63 : [ar] "=r" (ar)
64 : [old_ss] "rm" ((u16)regs->ss));
65
66 /*
67 * For a valid 64-bit user context, we need DPL 3, type
68 * read-write data or read-write exp-down data, and S and P
69 * set. We can't use VERW because VERW doesn't check the
70 * P bit.
71 */
72 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
73 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
74 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
75 regs->ss = __USER_DS;
76}
Al Viro3add42c2020-02-15 12:56:57 -050077# define CONTEXT_COPY_SIZE offsetof(struct sigcontext, reserved1)
78#else
79# define CONTEXT_COPY_SIZE sizeof(struct sigcontext)
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080080#endif
81
Andy Lutomirski6c25da52016-02-16 15:09:03 -080082static int restore_sigcontext(struct pt_regs *regs,
Al Viro3add42c2020-02-15 12:56:57 -050083 struct sigcontext __user *usc,
Andy Lutomirski6c25da52016-02-16 15:09:03 -080084 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080085{
Al Viro3add42c2020-02-15 12:56:57 -050086 struct sigcontext sc;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080087
88 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080089 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080090
Al Viro3add42c2020-02-15 12:56:57 -050091 if (copy_from_user(&sc, usc, CONTEXT_COPY_SIZE))
92 return -EFAULT;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080093
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080094#ifdef CONFIG_X86_32
Al Viro3add42c2020-02-15 12:56:57 -050095 set_user_gs(regs, sc.gs);
96 regs->fs = sc.fs;
97 regs->es = sc.es;
98 regs->ds = sc.ds;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080099#endif /* CONFIG_X86_32 */
100
Al Viro3add42c2020-02-15 12:56:57 -0500101 regs->bx = sc.bx;
102 regs->cx = sc.cx;
103 regs->dx = sc.dx;
104 regs->si = sc.si;
105 regs->di = sc.di;
106 regs->bp = sc.bp;
107 regs->ax = sc.ax;
108 regs->sp = sc.sp;
109 regs->ip = sc.ip;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800110
111#ifdef CONFIG_X86_64
Al Viro3add42c2020-02-15 12:56:57 -0500112 regs->r8 = sc.r8;
113 regs->r9 = sc.r9;
114 regs->r10 = sc.r10;
115 regs->r11 = sc.r11;
116 regs->r12 = sc.r12;
117 regs->r13 = sc.r13;
118 regs->r14 = sc.r14;
119 regs->r15 = sc.r15;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800120#endif /* CONFIG_X86_64 */
121
Al Viro3add42c2020-02-15 12:56:57 -0500122 /* Get CS/SS and force CPL3 */
123 regs->cs = sc.cs | 0x03;
124 regs->ss = sc.ss | 0x03;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800125
Al Viro3add42c2020-02-15 12:56:57 -0500126 regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
127 /* disable syscall checks */
128 regs->orig_ax = -1;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800129
Peter Zijlstra88e47182019-04-03 09:39:48 +0200130#ifdef CONFIG_X86_64
131 /*
132 * Fix up SS if needed for the benefit of old DOSEMU and
133 * CRIU.
134 */
135 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
136 force_valid_ss(regs);
137#endif
138
Al Viro3add42c2020-02-15 12:56:57 -0500139 return fpu__restore_sig((void __user *)sc.fpstate,
140 IS_ENABLED(CONFIG_X86_32));
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800141}
142
Al Virob00d8f82020-02-15 21:12:26 -0500143static __always_inline int
144__unsafe_setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
H. Peter Anvin85139422012-02-19 07:43:09 -0800145 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800146{
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800147#ifdef CONFIG_X86_32
Al Viro9f855c02020-02-15 17:25:27 -0500148 unsafe_put_user(get_user_gs(regs),
149 (unsigned int __user *)&sc->gs, Efault);
150 unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
151 unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
152 unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800153#endif /* CONFIG_X86_32 */
154
Al Viro9f855c02020-02-15 17:25:27 -0500155 unsafe_put_user(regs->di, &sc->di, Efault);
156 unsafe_put_user(regs->si, &sc->si, Efault);
157 unsafe_put_user(regs->bp, &sc->bp, Efault);
158 unsafe_put_user(regs->sp, &sc->sp, Efault);
159 unsafe_put_user(regs->bx, &sc->bx, Efault);
160 unsafe_put_user(regs->dx, &sc->dx, Efault);
161 unsafe_put_user(regs->cx, &sc->cx, Efault);
162 unsafe_put_user(regs->ax, &sc->ax, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800163#ifdef CONFIG_X86_64
Al Viro9f855c02020-02-15 17:25:27 -0500164 unsafe_put_user(regs->r8, &sc->r8, Efault);
165 unsafe_put_user(regs->r9, &sc->r9, Efault);
166 unsafe_put_user(regs->r10, &sc->r10, Efault);
167 unsafe_put_user(regs->r11, &sc->r11, Efault);
168 unsafe_put_user(regs->r12, &sc->r12, Efault);
169 unsafe_put_user(regs->r13, &sc->r13, Efault);
170 unsafe_put_user(regs->r14, &sc->r14, Efault);
171 unsafe_put_user(regs->r15, &sc->r15, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800172#endif /* CONFIG_X86_64 */
173
Al Viro9f855c02020-02-15 17:25:27 -0500174 unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
175 unsafe_put_user(current->thread.error_code, &sc->err, Efault);
176 unsafe_put_user(regs->ip, &sc->ip, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800177#ifdef CONFIG_X86_32
Al Viro9f855c02020-02-15 17:25:27 -0500178 unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
179 unsafe_put_user(regs->flags, &sc->flags, Efault);
180 unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
181 unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800182#else /* !CONFIG_X86_32 */
Al Viro9f855c02020-02-15 17:25:27 -0500183 unsafe_put_user(regs->flags, &sc->flags, Efault);
184 unsafe_put_user(regs->cs, &sc->cs, Efault);
185 unsafe_put_user(0, &sc->gs, Efault);
186 unsafe_put_user(0, &sc->fs, Efault);
187 unsafe_put_user(regs->ss, &sc->ss, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800188#endif /* CONFIG_X86_32 */
189
Al Viro9f855c02020-02-15 17:25:27 -0500190 unsafe_put_user(fpstate, (unsigned long __user *)&sc->fpstate, Efault);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800191
Al Viro9f855c02020-02-15 17:25:27 -0500192 /* non-iBCS2 extensions.. */
193 unsafe_put_user(mask, &sc->oldmask, Efault);
194 unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
Al Viro9f855c02020-02-15 17:25:27 -0500195 return 0;
196Efault:
Al Viro9f855c02020-02-15 17:25:27 -0500197 return -EFAULT;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800198}
199
Al Virob00d8f82020-02-15 21:12:26 -0500200#define unsafe_put_sigcontext(sc, fp, regs, set, label) \
201do { \
202 if (__unsafe_setup_sigcontext(sc, fp, regs, set->sig[0])) \
203 goto label; \
204} while(0);
205
Al Virob87df652020-02-15 21:36:52 -0500206#define unsafe_put_sigmask(set, frame, label) \
207 unsafe_put_user(*(__u64 *)(set), \
208 (__u64 __user *)&(frame)->uc.uc_sigmask, \
209 label)
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * Set up a signal frame.
213 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800214
215/*
216 * Determine which stack to use..
217 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800218static unsigned long align_sigframe(unsigned long sp)
219{
220#ifdef CONFIG_X86_32
221 /*
222 * Align the stack pointer according to the i386 ABI,
223 * i.e. so that on function entry ((sp + 4) & 15) == 0.
224 */
225 sp = ((sp + 4) & -16ul) - 4;
226#else /* !CONFIG_X86_32 */
227 sp = round_down(sp, 16) - 8;
228#endif
229 return sp;
230}
231
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200232static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800233get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
234 void __user **fpstate)
235{
236 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700237 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800238 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700239 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700240 int onsigstack = on_sig_stack(sp);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200241 int ret;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800242
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800243 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700244 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700245 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800246
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300247 /* This is the X/Open sanctioned signal stack switching. */
248 if (ka->sa.sa_flags & SA_ONSTACK) {
249 if (sas_ss_flags(sp) == 0)
250 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700251 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300252 !onsigstack &&
Andy Lutomirski99504812017-07-28 06:00:32 -0700253 regs->ss != __USER_DS &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300254 !(ka->sa.sa_flags & SA_RESTORER) &&
255 ka->sa.sa_restorer) {
256 /* This is the legacy signal stack switching. */
257 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800258 }
259
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200260 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
261 &buf_fx, &math_size);
262 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800263
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700264 sp = align_sigframe(sp - frame_size);
265
266 /*
267 * If we are on the alternate signal stack and would overflow it, don't.
268 * Return an always-bogus address instead so we will die with SIGSEGV.
269 */
270 if (onsigstack && !likely(on_sig_stack(sp)))
271 return (void __user *)-1L;
272
Suresh Siddha72a671c2012-07-24 16:05:29 -0700273 /* save i387 and extended state */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200274 ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
275 if (ret < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700276 return (void __user *)-1L;
277
278 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800279}
280
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800281#ifdef CONFIG_X86_32
282static const struct {
283 u16 poplmovl;
284 u32 val;
285 u16 int80;
286} __attribute__((packed)) retcode = {
287 0xb858, /* popl %eax; movl $..., %eax */
288 __NR_sigreturn,
289 0x80cd, /* int $0x80 */
290};
291
292static const struct {
293 u8 movl;
294 u32 val;
295 u16 int80;
296 u8 pad;
297} __attribute__((packed)) rt_retcode = {
298 0xb8, /* movl $..., %eax */
299 __NR_rt_sigreturn,
300 0x80cd, /* int $0x80 */
301 0
302};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Ingo Molnar7e907f42008-03-06 10:33:08 +0100304static int
Al Viro235b8022012-11-09 23:51:47 -0500305__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700306 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100309 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500310 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Al Virob00d8f82020-02-15 21:12:26 -0500312 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Al Viro5c1f1782020-02-15 21:18:02 -0500314 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700315 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Al Viro5c1f1782020-02-15 21:18:02 -0500317 unsafe_put_user(sig, &frame->sig, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500318 unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
Al Viro5c1f1782020-02-15 21:18:02 -0500319 unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700320 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700321 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700322 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100323 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100324 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500325 if (ksig->ka.sa.sa_flags & SA_RESTORER)
326 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Set up to return from userspace. */
Al Viro5c1f1782020-02-15 21:18:02 -0500329 unsafe_put_user(restorer, &frame->pretcode, Efault);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100332 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * WE DO NOT USE IT ANY MORE! It's only left here for historical
335 * reasons and because gdb uses it as a signature to notice
336 * signal handler stack frames.
337 */
Al Viro5c1f1782020-02-15 21:18:02 -0500338 unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
339 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100342 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500343 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100344 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800345 regs->dx = 0;
346 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100348 regs->ds = __USER_DS;
349 regs->es = __USER_DS;
350 regs->ss = __USER_DS;
351 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David Howells283828f2006-01-18 17:44:00 -0800353 return 0;
Al Virob00d8f82020-02-15 21:12:26 -0500354
355Efault:
356 user_access_end();
357 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Al Viro235b8022012-11-09 23:51:47 -0500360static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700361 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100364 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500365 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Al Virob00d8f82020-02-15 21:12:26 -0500367 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Al Viro119cd592020-02-15 19:54:56 -0500369 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700370 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Al Viro119cd592020-02-15 19:54:56 -0500372 unsafe_put_user(sig, &frame->sig, Efault);
373 unsafe_put_user(&frame->info, &frame->pinfo, Efault);
374 unsafe_put_user(&frame->uc, &frame->puc, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Al Viro119cd592020-02-15 19:54:56 -0500376 /* Create the ucontext. */
377 if (static_cpu_has(X86_FEATURE_XSAVE))
378 unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
379 else
380 unsafe_put_user(0, &frame->uc.uc_flags, Efault);
381 unsafe_put_user(0, &frame->uc.uc_link, Efault);
382 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Al Viro119cd592020-02-15 19:54:56 -0500384 /* Set up to return from userspace. */
385 restorer = current->mm->context.vdso +
386 vdso_image_32.sym___kernel_rt_sigreturn;
387 if (ksig->ka.sa.sa_flags & SA_RESTORER)
388 restorer = ksig->ka.sa.sa_restorer;
389 unsafe_put_user(restorer, &frame->pretcode, Efault);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100390
Al Viro119cd592020-02-15 19:54:56 -0500391 /*
392 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
393 *
394 * WE DO NOT USE IT ANY MORE! It's only left here for historical
395 * reasons and because gdb uses it as a signature to notice
396 * signal handler stack frames.
397 */
398 unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500399 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500400 unsafe_put_sigmask(set, frame, Efault);
Al Viro119cd592020-02-15 19:54:56 -0500401 user_access_end();
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700402
Al Viroead8e4e2020-02-15 21:22:39 -0500403 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700404 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100407 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500408 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700409 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100410 regs->dx = (unsigned long)&frame->info;
411 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100413 regs->ds = __USER_DS;
414 regs->es = __USER_DS;
415 regs->ss = __USER_DS;
416 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
David Howells283828f2006-01-18 17:44:00 -0800418 return 0;
Al Viro119cd592020-02-15 19:54:56 -0500419Efault:
420 user_access_end();
421 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800423#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800424static unsigned long frame_uc_flags(struct pt_regs *regs)
425{
426 unsigned long flags;
427
Borislav Petkovd366bf72016-04-04 22:25:02 +0200428 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800429 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
430 else
431 flags = UC_SIGCONTEXT_SS;
432
433 if (likely(user_64bit_mode(regs)))
434 flags |= UC_STRICT_RESTORE_SS;
435
436 return flags;
437}
438
Al Viro235b8022012-11-09 23:51:47 -0500439static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800440 sigset_t *set, struct pt_regs *regs)
441{
442 struct rt_sigframe __user *frame;
443 void __user *fp = NULL;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200444 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800445
Al Viro119cd592020-02-15 19:54:56 -0500446 /* x86-64 should always use SA_RESTORER. */
447 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
448 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800449
Al Viro235b8022012-11-09 23:51:47 -0500450 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Peter Zijlstra88e47182019-04-03 09:39:48 +0200451 uc_flags = frame_uc_flags(regs);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800452
Al Viro119cd592020-02-15 19:54:56 -0500453 if (!user_access_begin(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800454 return -EFAULT;
455
Al Viro119cd592020-02-15 19:54:56 -0500456 /* Create the ucontext. */
457 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
458 unsafe_put_user(0, &frame->uc.uc_link, Efault);
459 unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800460
Al Viro119cd592020-02-15 19:54:56 -0500461 /* Set up to return from userspace. If provided, use a stub
462 already in userspace. */
463 unsafe_put_user(ksig->ka.sa.sa_restorer, &frame->pretcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500464 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500465 unsafe_put_sigmask(set, frame, Efault);
Al Viro119cd592020-02-15 19:54:56 -0500466 user_access_end();
H. Peter Anvin5e883532012-09-21 12:43:15 -0700467
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800468 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
469 if (copy_siginfo_to_user(&frame->info, &ksig->info))
470 return -EFAULT;
471 }
472
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800473 /* Set up registers for signal handler */
474 regs->di = sig;
475 /* In case the signal handler was declared without prototypes */
476 regs->ax = 0;
477
478 /* This also works for non SA_SIGINFO handlers because they expect the
479 next argument after the signal number on the stack. */
480 regs->si = (unsigned long)&frame->info;
481 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500482 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800483
484 regs->sp = (unsigned long)frame;
485
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800486 /*
487 * Set up the CS and SS registers to run signal handlers in
488 * 64-bit mode, even if the handler happens to be interrupting
489 * 32-bit or 16-bit code.
490 *
491 * SS is subtle. In 64-bit mode, we don't need any particular
492 * SS descriptor, but we do need SS to be valid. It's possible
493 * that the old SS is entirely bogus -- this can happen if the
494 * signal we're trying to deliver is #GP or #SS caused by a bad
Ingo Molnard9f6e122021-03-18 15:28:01 +0100495 * SS value. We also have a compatibility issue here: DOSEMU
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800496 * relies on the contents of the SS register indicating the
497 * SS value at the time of the signal, even though that code in
498 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
499 * avoids relying on sigreturn to restore SS; instead it uses
500 * a trampoline.) So we do our best: if the old SS was valid,
501 * we keep it. Otherwise we replace it.
502 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800503 regs->cs = __USER_CS;
504
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800505 if (unlikely(regs->ss != __USER_DS))
506 force_valid_ss(regs);
507
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800508 return 0;
Al Viro119cd592020-02-15 19:54:56 -0500509
510Efault:
511 user_access_end();
512 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800513}
514#endif /* CONFIG_X86_32 */
515
Christoph Hellwigc3b3f522020-05-05 12:12:53 +0200516#ifdef CONFIG_X86_X32_ABI
517static int x32_copy_siginfo_to_user(struct compat_siginfo __user *to,
518 const struct kernel_siginfo *from)
519{
520 struct compat_siginfo new;
521
522 copy_siginfo_to_external32(&new, from);
523 if (from->si_signo == SIGCHLD) {
524 new._sifields._sigchld_x32._utime = from->si_utime;
525 new._sifields._sigchld_x32._stime = from->si_stime;
526 }
527 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
528 return -EFAULT;
529 return 0;
530}
531
532int copy_siginfo_to_user32(struct compat_siginfo __user *to,
533 const struct kernel_siginfo *from)
534{
535 if (in_x32_syscall())
536 return x32_copy_siginfo_to_user(to, from);
537 return __copy_siginfo_to_user32(to, from);
538}
539#endif /* CONFIG_X86_X32_ABI */
540
Al Viro235b8022012-11-09 23:51:47 -0500541static int x32_setup_rt_frame(struct ksignal *ksig,
542 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700543 struct pt_regs *regs)
544{
545#ifdef CONFIG_X86_X32_ABI
546 struct rt_sigframe_x32 __user *frame;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200547 unsigned long uc_flags;
Suresh Siddha050902c2012-07-24 16:05:27 -0700548 void __user *restorer;
Al Virob00d8f82020-02-15 21:12:26 -0500549 void __user *fp = NULL;
Suresh Siddha050902c2012-07-24 16:05:27 -0700550
Al Viro39f16c12020-02-15 18:39:17 -0500551 if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
Suresh Siddha050902c2012-07-24 16:05:27 -0700552 return -EFAULT;
553
Al Virob00d8f82020-02-15 21:12:26 -0500554 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700555
Peter Zijlstra88e47182019-04-03 09:39:48 +0200556 uc_flags = frame_uc_flags(regs);
557
Al Viro39f16c12020-02-15 18:39:17 -0500558 if (!user_access_begin(frame, sizeof(*frame)))
559 return -EFAULT;
Suresh Siddha050902c2012-07-24 16:05:27 -0700560
Al Viro39f16c12020-02-15 18:39:17 -0500561 /* Create the ucontext. */
562 unsafe_put_user(uc_flags, &frame->uc.uc_flags, Efault);
563 unsafe_put_user(0, &frame->uc.uc_link, Efault);
564 unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
565 unsafe_put_user(0, &frame->uc.uc__pad0, Efault);
566 restorer = ksig->ka.sa.sa_restorer;
567 unsafe_put_user(restorer, (unsigned long __user *)&frame->pretcode, Efault);
Al Virob00d8f82020-02-15 21:12:26 -0500568 unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Al Virob87df652020-02-15 21:36:52 -0500569 unsafe_put_sigmask(set, frame, Efault);
Al Viro39f16c12020-02-15 18:39:17 -0500570 user_access_end();
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700571
Suresh Siddha050902c2012-07-24 16:05:27 -0700572 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Christoph Hellwigc3b3f522020-05-05 12:12:53 +0200573 if (x32_copy_siginfo_to_user(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700574 return -EFAULT;
575 }
576
Suresh Siddha050902c2012-07-24 16:05:27 -0700577 /* Set up registers for signal handler */
578 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500579 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700580
581 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500582 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700583 regs->si = (unsigned long) &frame->info;
584 regs->dx = (unsigned long) &frame->uc;
585
586 loadsegment(ds, __USER_DS);
587 loadsegment(es, __USER_DS);
588
589 regs->cs = __USER_CS;
590 regs->ss = __USER_DS;
591#endif /* CONFIG_X86_X32_ABI */
592
593 return 0;
Al Viro39f16c12020-02-15 18:39:17 -0500594#ifdef CONFIG_X86_X32_ABI
595Efault:
596 user_access_end();
597 return -EFAULT;
598#endif
Suresh Siddha050902c2012-07-24 16:05:27 -0700599}
600
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800601/*
602 * Do a signal return; undo the signal stack.
603 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800604#ifdef CONFIG_X86_32
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000605SYSCALL_DEFINE0(sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606{
Al Viro3fe26fa2012-11-12 14:32:42 -0500607 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800608 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800609 sigset_t set;
610
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800611 frame = (struct sigframe __user *)(regs->sp - 8);
612
Linus Torvalds96d4f262019-01-03 18:57:57 -0800613 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800614 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500615 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
616 __get_user(set.sig[1], &frame->extramask[0]))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800617 goto badframe;
618
Oleg Nesterov39822942011-07-10 21:27:27 +0200619 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800620
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800621 /*
622 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
623 * Save a few cycles by skipping the __get_user.
624 */
625 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800626 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400627 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800628
629badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800630 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800631
632 return 0;
633}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800634#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800635
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000636SYSCALL_DEFINE0(rt_sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800637{
Al Viro3fe26fa2012-11-12 14:32:42 -0500638 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800639 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800640 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800641 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800642
643 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800644 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800645 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500646 if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800647 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800648 if (__get_user(uc_flags, &frame->uc.uc_flags))
649 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800650
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200651 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800652
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800653 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800654 goto badframe;
655
Al Viroc40702c2012-11-20 14:24:26 -0500656 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800657 goto badframe;
658
Brian Gerst6a3713f2015-04-04 08:58:23 -0400659 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800660
661badframe:
662 signal_fault(regs, frame, "rt_sigreturn");
663 return 0;
664}
665
Dmitry Safonov68463512016-09-05 16:33:08 +0300666static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200667{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700668 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300669 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200670}
671
Dmitry Safonov68463512016-09-05 16:33:08 +0300672static inline int is_ia32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200673{
Dmitry Safonov68463512016-09-05 16:33:08 +0300674 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200675}
676
Dmitry Safonov68463512016-09-05 16:33:08 +0300677static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200678{
Dmitry Safonov68463512016-09-05 16:33:08 +0300679 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
680 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200681}
682
Roland McGrath7c1def12005-06-23 00:08:21 -0700683static int
Al Viro235b8022012-11-09 23:51:47 -0500684setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700685{
Richard Weinberger3050a352014-07-13 17:43:51 +0200686 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400687 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700688 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700689
Mathieu Desnoyersbff95042019-03-05 14:47:53 -0500690 /* Perform fixup for the pre-signal frame. */
Will Deacon784e0302018-06-22 11:45:07 +0100691 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400692
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700693 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300694 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500695 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
696 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700697 else
Al Viro235b8022012-11-09 23:51:47 -0500698 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300699 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500700 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800701 } else {
Al Viro235b8022012-11-09 23:51:47 -0500702 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800703 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700704}
705
Al Viroa610d6e2012-05-21 23:42:15 -0400706static void
Al Viro235b8022012-11-09 23:51:47 -0500707handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700709 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200710 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700711
Brian Gerst5ed92a82015-07-29 01:41:19 -0400712 if (v8086_mode(regs))
713 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700716 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700718 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800719 case -ERESTART_RESTARTBLOCK:
720 case -ERESTARTNOHAND:
721 regs->ax = -EINTR;
722 break;
723
724 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500725 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100726 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800728 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500729 fallthrough;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800730 case -ERESTARTNOINTR:
731 regs->ax = regs->orig_ax;
732 regs->ip -= 2;
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 }
736
737 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700738 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
739 * so that register information in the sigcontext is correct and
740 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700742 stepping = test_thread_flag(TIF_SINGLESTEP);
743 if (stepping)
744 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Al Viro235b8022012-11-09 23:51:47 -0500746 failed = (setup_rt_frame(ksig, regs) < 0);
747 if (!failed) {
748 /*
749 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200750 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200751 * Clear RF when entering the signal handler, because
752 * it might disable possible debug exception from the
753 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200754 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700755 * Clear TF for the case when it wasn't set by debugger to
756 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500757 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200758 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200759 /*
760 * Ensure the signal handler starts with the new fpu state.
761 */
Fenghua Yub860eb82020-05-12 07:54:39 -0700762 fpu__clear_user_states(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400763 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700764 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300767static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
768{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700769 /*
770 * This function is fundamentally broken as currently
771 * implemented.
772 *
773 * The idea is that we want to trigger a call to the
774 * restart_block() syscall and that we want in_ia32_syscall(),
775 * in_x32_syscall(), etc. to match whatever they were in the
776 * syscall being restarted. We assume that the syscall
777 * instruction at (regs->ip - 2) matches whatever syscall
778 * instruction we used to enter in the first place.
779 *
780 * The problem is that we can get here when ptrace pokes
781 * syscall-like values into regs even if we're not in a syscall
782 * at all.
783 *
784 * For now, we maintain historical behavior and guess based on
785 * stored state. We could do better by saving the actual
786 * syscall arch in restart_block or (with caveats on x32) by
787 * checking if regs->ip points to 'int $0x80'. The current
788 * behavior is incorrect if a tracer has a different bitness
789 * than the tracee.
790 */
791#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800792 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000793 return __NR_ia32_restart_syscall;
794#endif
795#ifdef CONFIG_X86_X32_ABI
796 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
797#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300798 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000799#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300800}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*
803 * Note that 'init' is a special process: it doesn't get signals it doesn't
804 * want to handle. Thus you cannot kill init even with a SIGKILL even by
805 * mistake.
806 */
Jens Axboe12db8b62020-10-26 14:32:28 -0600807void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Al Viro235b8022012-11-09 23:51:47 -0500809 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jens Axboe12db8b62020-10-26 14:32:28 -0600811 if (has_signal && get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100812 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500813 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800814 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700818 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700820 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800821 case -ERESTARTNOHAND:
822 case -ERESTARTSYS:
823 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100824 regs->ax = regs->orig_ax;
825 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800826 break;
827
828 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300829 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100830 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 }
David Howells283828f2006-01-18 17:44:00 -0800834
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800835 /*
836 * If there's no signal to deliver, we just put the saved sigmask
837 * back.
838 */
Al Viro51a7b442012-05-21 23:33:55 -0400839 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700842void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
843{
844 struct task_struct *me = current;
845
846 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800847 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700848 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800849 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700850 me->comm, me->pid, where, frame,
851 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200852 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700853 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700854 }
855
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500856 force_sig(SIGSEGV);
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700857}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800858
859#ifdef CONFIG_X86_X32_ABI
Brian Gerst27dd84f2020-03-13 15:51:31 -0400860COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800861{
Al Viro3fe26fa2012-11-12 14:32:42 -0500862 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800863 struct rt_sigframe_x32 __user *frame;
864 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800865 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800866
867 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
868
Linus Torvalds96d4f262019-01-03 18:57:57 -0800869 if (!access_ok(frame, sizeof(*frame)))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800870 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500871 if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800872 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800873 if (__get_user(uc_flags, &frame->uc.uc_flags))
874 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800875
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800876 set_current_blocked(&set);
877
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800878 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800879 goto badframe;
880
Al Viro90268432012-12-14 14:47:53 -0500881 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800882 goto badframe;
883
Brian Gerst6a3713f2015-04-04 08:58:23 -0400884 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800885
886badframe:
887 signal_fault(regs, frame, "x32 rt_sigreturn");
888 return 0;
889}
890#endif