blob: 364813cea6476113d888ddd8a9ca428c55c3b955 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992 Linus Torvalds
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08004 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08008 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Joe Perchesc767a542012-05-21 19:50:07 -070010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Ingo Molnar7e907f42008-03-06 10:33:08 +010013#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010014#include <linux/sched/task_stack.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010015#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080016#include <linux/smp.h>
17#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080018#include <linux/errno.h>
19#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080020#include <linux/tracehook.h>
21#include <linux/unistd.h>
22#include <linux/stddef.h>
23#include <linux/personality.h>
24#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030025#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053026#include <linux/uprobes.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010027#include <linux/context_tracking.h>
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +000028#include <linux/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/processor.h>
31#include <asm/ucontext.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020032#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020033#include <asm/fpu/signal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010034#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020035#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080036#include <asm/sighandling.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040037#include <asm/vm86.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080038
39#ifdef CONFIG_X86_64
40#include <asm/proto.h>
41#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080042#endif /* CONFIG_X86_64 */
43
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070044#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053045#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010046
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080047#include <asm/sigframe.h>
Dmitry Safonov68463512016-09-05 16:33:08 +030048#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heod9a89a22009-02-09 22:17:40 +090050#define COPY(x) do { \
51 get_user_ex(regs->x, &sc->x); \
52} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080053
Tejun Heod9a89a22009-02-09 22:17:40 +090054#define GET_SEG(seg) ({ \
55 unsigned short tmp; \
56 get_user_ex(tmp, &sc->seg); \
57 tmp; \
58})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080059
Tejun Heod9a89a22009-02-09 22:17:40 +090060#define COPY_SEG(seg) do { \
61 regs->seg = GET_SEG(seg); \
62} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080063
Tejun Heod9a89a22009-02-09 22:17:40 +090064#define COPY_SEG_CPL3(seg) do { \
65 regs->seg = GET_SEG(seg) | 3; \
66} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080067
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080068#ifdef CONFIG_X86_64
69/*
70 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
71 * alone. Using this generally makes no sense unless
72 * user_64bit_mode(regs) would return true.
73 */
74static void force_valid_ss(struct pt_regs *regs)
75{
76 u32 ar;
77 asm volatile ("lar %[old_ss], %[ar]\n\t"
78 "jz 1f\n\t" /* If invalid: */
79 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
80 "1:"
81 : [ar] "=r" (ar)
82 : [old_ss] "rm" ((u16)regs->ss));
83
84 /*
85 * For a valid 64-bit user context, we need DPL 3, type
86 * read-write data or read-write exp-down data, and S and P
87 * set. We can't use VERW because VERW doesn't check the
88 * P bit.
89 */
90 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
91 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
92 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
93 regs->ss = __USER_DS;
94}
95#endif
96
Andy Lutomirski6c25da52016-02-16 15:09:03 -080097static int restore_sigcontext(struct pt_regs *regs,
98 struct sigcontext __user *sc,
99 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100{
Ingo Molnar530e5c82015-09-05 09:32:39 +0200101 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800102 void __user *buf;
103 unsigned int tmpflags;
104 unsigned int err = 0;
105
106 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800107 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 get_user_try {
110
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800111#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900112 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 COPY_SEG(fs);
114 COPY_SEG(es);
115 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800116#endif /* CONFIG_X86_32 */
117
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -0400119 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800120
121#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800122 COPY(r8);
123 COPY(r9);
124 COPY(r10);
125 COPY(r11);
126 COPY(r12);
127 COPY(r13);
128 COPY(r14);
129 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800130#endif /* CONFIG_X86_64 */
131
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800132 COPY_SEG_CPL3(cs);
133 COPY_SEG_CPL3(ss);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800134
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800135 get_user_ex(tmpflags, &sc->flags);
136 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
137 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800138
Ingo Molnar530e5c82015-09-05 09:32:39 +0200139 get_user_ex(buf_val, &sc->fpstate);
140 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800141 } get_user_catch(err);
142
Peter Zijlstra88e47182019-04-03 09:39:48 +0200143#ifdef CONFIG_X86_64
144 /*
145 * Fix up SS if needed for the benefit of old DOSEMU and
146 * CRIU.
147 */
148 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
149 force_valid_ss(regs);
150#endif
151
Masahiro Yamada97f26452016-08-03 13:45:50 -0700152 err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700153
Brian Gerst1daeaa32015-03-21 18:54:21 -0400154 force_iret();
155
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800156 return err;
157}
158
H. Peter Anvin85139422012-02-19 07:43:09 -0800159int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
160 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800161{
162 int err = 0;
163
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800164 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800165
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800166#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900167 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800168 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
169 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
170 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800171#endif /* CONFIG_X86_32 */
172
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800173 put_user_ex(regs->di, &sc->di);
174 put_user_ex(regs->si, &sc->si);
175 put_user_ex(regs->bp, &sc->bp);
176 put_user_ex(regs->sp, &sc->sp);
177 put_user_ex(regs->bx, &sc->bx);
178 put_user_ex(regs->dx, &sc->dx);
179 put_user_ex(regs->cx, &sc->cx);
180 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800181#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800182 put_user_ex(regs->r8, &sc->r8);
183 put_user_ex(regs->r9, &sc->r9);
184 put_user_ex(regs->r10, &sc->r10);
185 put_user_ex(regs->r11, &sc->r11);
186 put_user_ex(regs->r12, &sc->r12);
187 put_user_ex(regs->r13, &sc->r13);
188 put_user_ex(regs->r14, &sc->r14);
189 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800190#endif /* CONFIG_X86_64 */
191
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530192 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800193 put_user_ex(current->thread.error_code, &sc->err);
194 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800195#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800196 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
197 put_user_ex(regs->flags, &sc->flags);
198 put_user_ex(regs->sp, &sc->sp_at_signal);
199 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800200#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800201 put_user_ex(regs->flags, &sc->flags);
202 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700203 put_user_ex(0, &sc->gs);
204 put_user_ex(0, &sc->fs);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800205 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800206#endif /* CONFIG_X86_32 */
207
Jann Horn89833fa2019-03-29 22:46:51 +0100208 put_user_ex(fpstate, (unsigned long __user *)&sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800209
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800210 /* non-iBCS2 extensions.. */
211 put_user_ex(mask, &sc->oldmask);
212 put_user_ex(current->thread.cr2, &sc->cr2);
213 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800214
215 return err;
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Set up a signal frame.
220 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800221
222/*
223 * Determine which stack to use..
224 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800225static unsigned long align_sigframe(unsigned long sp)
226{
227#ifdef CONFIG_X86_32
228 /*
229 * Align the stack pointer according to the i386 ABI,
230 * i.e. so that on function entry ((sp + 4) & 15) == 0.
231 */
232 sp = ((sp + 4) & -16ul) - 4;
233#else /* !CONFIG_X86_32 */
234 sp = round_down(sp, 16) - 8;
235#endif
236 return sp;
237}
238
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200239static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800240get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
241 void __user **fpstate)
242{
243 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700244 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800245 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700246 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700247 int onsigstack = on_sig_stack(sp);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200248 int ret;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800249
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800250 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700251 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700252 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800253
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300254 /* This is the X/Open sanctioned signal stack switching. */
255 if (ka->sa.sa_flags & SA_ONSTACK) {
256 if (sas_ss_flags(sp) == 0)
257 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700258 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300259 !onsigstack &&
Andy Lutomirski99504812017-07-28 06:00:32 -0700260 regs->ss != __USER_DS &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300261 !(ka->sa.sa_flags & SA_RESTORER) &&
262 ka->sa.sa_restorer) {
263 /* This is the legacy signal stack switching. */
264 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800265 }
266
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200267 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
268 &buf_fx, &math_size);
269 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800270
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700271 sp = align_sigframe(sp - frame_size);
272
273 /*
274 * If we are on the alternate signal stack and would overflow it, don't.
275 * Return an always-bogus address instead so we will die with SIGSEGV.
276 */
277 if (onsigstack && !likely(on_sig_stack(sp)))
278 return (void __user *)-1L;
279
Suresh Siddha72a671c2012-07-24 16:05:29 -0700280 /* save i387 and extended state */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200281 ret = copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size);
282 if (ret < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700283 return (void __user *)-1L;
284
285 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800286}
287
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800288#ifdef CONFIG_X86_32
289static const struct {
290 u16 poplmovl;
291 u32 val;
292 u16 int80;
293} __attribute__((packed)) retcode = {
294 0xb858, /* popl %eax; movl $..., %eax */
295 __NR_sigreturn,
296 0x80cd, /* int $0x80 */
297};
298
299static const struct {
300 u8 movl;
301 u32 val;
302 u16 int80;
303 u8 pad;
304} __attribute__((packed)) rt_retcode = {
305 0xb8, /* movl $..., %eax */
306 __NR_rt_sigreturn,
307 0x80cd, /* int $0x80 */
308 0
309};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Ingo Molnar7e907f42008-03-06 10:33:08 +0100311static int
Al Viro235b8022012-11-09 23:51:47 -0500312__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700313 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100316 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700318 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Al Viro235b8022012-11-09 23:51:47 -0500320 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds96d4f262019-01-03 18:57:57 -0800322 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700325 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700326 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700328 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700329 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700332 if (__copy_to_user(&frame->extramask, &set->sig[1],
333 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700334 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700337 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700338 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700339 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100340 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100341 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500342 if (ksig->ka.sa.sa_flags & SA_RESTORER)
343 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Set up to return from userspace. */
346 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100349 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
351 * WE DO NOT USE IT ANY MORE! It's only left here for historical
352 * reasons and because gdb uses it as a signature to notice
353 * signal handler stack frames.
354 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800355 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700358 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100361 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500362 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100363 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800364 regs->dx = 0;
365 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100367 regs->ds = __USER_DS;
368 regs->es = __USER_DS;
369 regs->ss = __USER_DS;
370 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David Howells283828f2006-01-18 17:44:00 -0800372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Al Viro235b8022012-11-09 23:51:47 -0500375static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700376 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100379 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700381 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Al Viro235b8022012-11-09 23:51:47 -0500383 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds96d4f262019-01-03 18:57:57 -0800385 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700386 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800388 put_user_try {
389 put_user_ex(sig, &frame->sig);
390 put_user_ex(&frame->info, &frame->pinfo);
391 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800393 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200394 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800395 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
396 else
397 put_user_ex(0, &frame->uc.uc_flags);
398 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100399 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800401 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700402 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700403 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500404 if (ksig->ka.sa.sa_flags & SA_RESTORER)
405 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800406 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100407
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800408 /*
409 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
410 *
411 * WE DO NOT USE IT ANY MORE! It's only left here for historical
412 * reasons and because gdb uses it as a signature to notice
413 * signal handler stack frames.
414 */
415 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
416 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700417
Al Viro235b8022012-11-09 23:51:47 -0500418 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700419 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
420 regs, set->sig[0]);
421 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700424 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100427 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500428 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700429 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100430 regs->dx = (unsigned long)&frame->info;
431 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100433 regs->ds = __USER_DS;
434 regs->es = __USER_DS;
435 regs->ss = __USER_DS;
436 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
David Howells283828f2006-01-18 17:44:00 -0800438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800440#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800441static unsigned long frame_uc_flags(struct pt_regs *regs)
442{
443 unsigned long flags;
444
Borislav Petkovd366bf72016-04-04 22:25:02 +0200445 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800446 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
447 else
448 flags = UC_SIGCONTEXT_SS;
449
450 if (likely(user_64bit_mode(regs)))
451 flags |= UC_STRICT_RESTORE_SS;
452
453 return flags;
454}
455
Al Viro235b8022012-11-09 23:51:47 -0500456static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800457 sigset_t *set, struct pt_regs *regs)
458{
459 struct rt_sigframe __user *frame;
460 void __user *fp = NULL;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200461 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800462 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800463
Al Viro235b8022012-11-09 23:51:47 -0500464 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800465
Linus Torvalds96d4f262019-01-03 18:57:57 -0800466 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800467 return -EFAULT;
468
Al Viro235b8022012-11-09 23:51:47 -0500469 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
470 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800471 return -EFAULT;
472 }
473
Peter Zijlstra88e47182019-04-03 09:39:48 +0200474 uc_flags = frame_uc_flags(regs);
475
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800476 put_user_try {
477 /* Create the ucontext. */
Peter Zijlstra88e47182019-04-03 09:39:48 +0200478 put_user_ex(uc_flags, &frame->uc.uc_flags);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800479 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100480 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800481
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800482 /* Set up to return from userspace. If provided, use a stub
483 already in userspace. */
484 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500485 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
486 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800487 } else {
488 /* could use a vstub here */
489 err |= -EFAULT;
490 }
491 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800492
H. Peter Anvin5e883532012-09-21 12:43:15 -0700493 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
494 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
495
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800496 if (err)
497 return -EFAULT;
498
499 /* Set up registers for signal handler */
500 regs->di = sig;
501 /* In case the signal handler was declared without prototypes */
502 regs->ax = 0;
503
504 /* This also works for non SA_SIGINFO handlers because they expect the
505 next argument after the signal number on the stack. */
506 regs->si = (unsigned long)&frame->info;
507 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500508 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800509
510 regs->sp = (unsigned long)frame;
511
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800512 /*
513 * Set up the CS and SS registers to run signal handlers in
514 * 64-bit mode, even if the handler happens to be interrupting
515 * 32-bit or 16-bit code.
516 *
517 * SS is subtle. In 64-bit mode, we don't need any particular
518 * SS descriptor, but we do need SS to be valid. It's possible
519 * that the old SS is entirely bogus -- this can happen if the
520 * signal we're trying to deliver is #GP or #SS caused by a bad
521 * SS value. We also have a compatbility issue here: DOSEMU
522 * relies on the contents of the SS register indicating the
523 * SS value at the time of the signal, even though that code in
524 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
525 * avoids relying on sigreturn to restore SS; instead it uses
526 * a trampoline.) So we do our best: if the old SS was valid,
527 * we keep it. Otherwise we replace it.
528 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800529 regs->cs = __USER_CS;
530
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800531 if (unlikely(regs->ss != __USER_DS))
532 force_valid_ss(regs);
533
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800534 return 0;
535}
536#endif /* CONFIG_X86_32 */
537
Al Viro235b8022012-11-09 23:51:47 -0500538static int x32_setup_rt_frame(struct ksignal *ksig,
539 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700540 struct pt_regs *regs)
541{
542#ifdef CONFIG_X86_X32_ABI
543 struct rt_sigframe_x32 __user *frame;
Peter Zijlstra88e47182019-04-03 09:39:48 +0200544 unsigned long uc_flags;
Suresh Siddha050902c2012-07-24 16:05:27 -0700545 void __user *restorer;
546 int err = 0;
547 void __user *fpstate = NULL;
548
Al Viro235b8022012-11-09 23:51:47 -0500549 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700550
Linus Torvalds96d4f262019-01-03 18:57:57 -0800551 if (!access_ok(frame, sizeof(*frame)))
Suresh Siddha050902c2012-07-24 16:05:27 -0700552 return -EFAULT;
553
Al Viro235b8022012-11-09 23:51:47 -0500554 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Dmitry Safonov68463512016-09-05 16:33:08 +0300555 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
Suresh Siddha050902c2012-07-24 16:05:27 -0700556 return -EFAULT;
557 }
558
Peter Zijlstra88e47182019-04-03 09:39:48 +0200559 uc_flags = frame_uc_flags(regs);
560
Suresh Siddha050902c2012-07-24 16:05:27 -0700561 put_user_try {
562 /* Create the ucontext. */
Peter Zijlstra88e47182019-04-03 09:39:48 +0200563 put_user_ex(uc_flags, &frame->uc.uc_flags);
Suresh Siddha050902c2012-07-24 16:05:27 -0700564 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100565 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700566 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700567
Al Viro235b8022012-11-09 23:51:47 -0500568 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
569 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700570 } else {
571 /* could use a vstub here */
572 restorer = NULL;
573 err |= -EFAULT;
574 }
Jann Horn89833fa2019-03-29 22:46:51 +0100575 put_user_ex(restorer, (unsigned long __user *)&frame->pretcode);
Suresh Siddha050902c2012-07-24 16:05:27 -0700576 } put_user_catch(err);
577
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700578 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
579 regs, set->sig[0]);
580 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
581
Suresh Siddha050902c2012-07-24 16:05:27 -0700582 if (err)
583 return -EFAULT;
584
585 /* Set up registers for signal handler */
586 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500587 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700588
589 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500590 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700591 regs->si = (unsigned long) &frame->info;
592 regs->dx = (unsigned long) &frame->uc;
593
594 loadsegment(ds, __USER_DS);
595 loadsegment(es, __USER_DS);
596
597 regs->cs = __USER_CS;
598 regs->ss = __USER_DS;
599#endif /* CONFIG_X86_X32_ABI */
600
601 return 0;
602}
603
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800604/*
605 * Do a signal return; undo the signal stack.
606 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800607#ifdef CONFIG_X86_32
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000608SYSCALL_DEFINE0(sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800609{
Al Viro3fe26fa2012-11-12 14:32:42 -0500610 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800611 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800612 sigset_t set;
613
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800614 frame = (struct sigframe __user *)(regs->sp - 8);
615
Linus Torvalds96d4f262019-01-03 18:57:57 -0800616 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800617 goto badframe;
618 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
619 && __copy_from_user(&set.sig[1], &frame->extramask,
620 sizeof(frame->extramask))))
621 goto badframe;
622
Oleg Nesterov39822942011-07-10 21:27:27 +0200623 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800624
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800625 /*
626 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
627 * Save a few cycles by skipping the __get_user.
628 */
629 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800630 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400631 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800632
633badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800634 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800635
636 return 0;
637}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800638#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800639
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000640SYSCALL_DEFINE0(rt_sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800641{
Al Viro3fe26fa2012-11-12 14:32:42 -0500642 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800643 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800644 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800645 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800646
647 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800648 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800649 goto badframe;
650 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
651 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800652 if (__get_user(uc_flags, &frame->uc.uc_flags))
653 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800654
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200655 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800656
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800657 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800658 goto badframe;
659
Al Viroc40702c2012-11-20 14:24:26 -0500660 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800661 goto badframe;
662
Brian Gerst6a3713f2015-04-04 08:58:23 -0400663 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800664
665badframe:
666 signal_fault(regs, frame, "rt_sigreturn");
667 return 0;
668}
669
Dmitry Safonov68463512016-09-05 16:33:08 +0300670static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200671{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700672 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300673 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200674}
675
Dmitry Safonov68463512016-09-05 16:33:08 +0300676static inline int is_ia32_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_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200679}
680
Dmitry Safonov68463512016-09-05 16:33:08 +0300681static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200682{
Dmitry Safonov68463512016-09-05 16:33:08 +0300683 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
684 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200685}
686
Roland McGrath7c1def12005-06-23 00:08:21 -0700687static int
Al Viro235b8022012-11-09 23:51:47 -0500688setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700689{
Richard Weinberger3050a352014-07-13 17:43:51 +0200690 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400691 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700692 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700693
Mathieu Desnoyersbff95042019-03-05 14:47:53 -0500694 /* Perform fixup for the pre-signal frame. */
Will Deacon784e0302018-06-22 11:45:07 +0100695 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400696
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700697 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300698 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500699 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
700 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700701 else
Al Viro235b8022012-11-09 23:51:47 -0500702 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300703 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500704 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800705 } else {
Al Viro235b8022012-11-09 23:51:47 -0500706 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800707 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700708}
709
Al Viroa610d6e2012-05-21 23:42:15 -0400710static void
Al Viro235b8022012-11-09 23:51:47 -0500711handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700713 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200714 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700715
Brian Gerst5ed92a82015-07-29 01:41:19 -0400716 if (v8086_mode(regs))
717 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700720 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700722 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800723 case -ERESTART_RESTARTBLOCK:
724 case -ERESTARTNOHAND:
725 regs->ax = -EINTR;
726 break;
727
728 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500729 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100730 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800732 }
733 /* fallthrough */
734 case -ERESTARTNOINTR:
735 regs->ax = regs->orig_ax;
736 regs->ip -= 2;
737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 }
740
741 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700742 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
743 * so that register information in the sigcontext is correct and
744 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700746 stepping = test_thread_flag(TIF_SINGLESTEP);
747 if (stepping)
748 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Al Viro235b8022012-11-09 23:51:47 -0500750 failed = (setup_rt_frame(ksig, regs) < 0);
751 if (!failed) {
752 /*
753 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200754 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200755 * Clear RF when entering the signal handler, because
756 * it might disable possible debug exception from the
757 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200758 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700759 * Clear TF for the case when it wasn't set by debugger to
760 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500761 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200762 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200763 /*
764 * Ensure the signal handler starts with the new fpu state.
765 */
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200766 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400767 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700768 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300771static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
772{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700773 /*
774 * This function is fundamentally broken as currently
775 * implemented.
776 *
777 * The idea is that we want to trigger a call to the
778 * restart_block() syscall and that we want in_ia32_syscall(),
779 * in_x32_syscall(), etc. to match whatever they were in the
780 * syscall being restarted. We assume that the syscall
781 * instruction at (regs->ip - 2) matches whatever syscall
782 * instruction we used to enter in the first place.
783 *
784 * The problem is that we can get here when ptrace pokes
785 * syscall-like values into regs even if we're not in a syscall
786 * at all.
787 *
788 * For now, we maintain historical behavior and guess based on
789 * stored state. We could do better by saving the actual
790 * syscall arch in restart_block or (with caveats on x32) by
791 * checking if regs->ip points to 'int $0x80'. The current
792 * behavior is incorrect if a tracer has a different bitness
793 * than the tracee.
794 */
795#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800796 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000797 return __NR_ia32_restart_syscall;
798#endif
799#ifdef CONFIG_X86_X32_ABI
800 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
801#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300802 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000803#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300804}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/*
807 * Note that 'init' is a special process: it doesn't get signals it doesn't
808 * want to handle. Thus you cannot kill init even with a SIGKILL even by
809 * mistake.
810 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700811void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Al Viro235b8022012-11-09 23:51:47 -0500813 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Al Viro235b8022012-11-09 23:51:47 -0500815 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100816 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500817 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800818 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700822 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700824 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800825 case -ERESTARTNOHAND:
826 case -ERESTARTSYS:
827 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100828 regs->ax = regs->orig_ax;
829 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800830 break;
831
832 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300833 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100834 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
David Howells283828f2006-01-18 17:44:00 -0800838
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800839 /*
840 * If there's no signal to deliver, we just put the saved sigmask
841 * back.
842 */
Al Viro51a7b442012-05-21 23:33:55 -0400843 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700846void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
847{
848 struct task_struct *me = current;
849
850 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800851 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700852 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800853 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700854 me->comm, me->pid, where, frame,
855 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200856 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700857 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700858 }
859
860 force_sig(SIGSEGV, me);
861}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800862
863#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500864asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800865{
Al Viro3fe26fa2012-11-12 14:32:42 -0500866 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800867 struct rt_sigframe_x32 __user *frame;
868 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800869 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800870
871 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
872
Linus Torvalds96d4f262019-01-03 18:57:57 -0800873 if (!access_ok(frame, sizeof(*frame)))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800874 goto badframe;
875 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
876 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800877 if (__get_user(uc_flags, &frame->uc.uc_flags))
878 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800879
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800880 set_current_blocked(&set);
881
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800882 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800883 goto badframe;
884
Al Viro90268432012-12-14 14:47:53 -0500885 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800886 goto badframe;
887
Brian Gerst6a3713f2015-04-04 08:58:23 -0400888 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800889
890badframe:
891 signal_fault(regs, frame, "x32 rt_sigreturn");
892 return 0;
893}
894#endif