Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * linux/arch/x86_64/ia32/ia32_signal.c
4 : *
5 : * Copyright (C) 1991, 1992 Linus Torvalds
6 : *
7 : * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 : * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
9 : * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen
10 : */
11 :
12 : #include <linux/sched.h>
13 : #include <linux/sched/task_stack.h>
14 : #include <linux/mm.h>
15 : #include <linux/smp.h>
16 : #include <linux/kernel.h>
17 : #include <linux/errno.h>
18 : #include <linux/wait.h>
19 : #include <linux/unistd.h>
20 : #include <linux/stddef.h>
21 : #include <linux/personality.h>
22 : #include <linux/compat.h>
23 : #include <linux/binfmts.h>
24 : #include <linux/syscalls.h>
25 : #include <asm/ucontext.h>
26 : #include <linux/uaccess.h>
27 : #include <asm/fpu/internal.h>
28 : #include <asm/fpu/signal.h>
29 : #include <asm/ptrace.h>
30 : #include <asm/ia32_unistd.h>
31 : #include <asm/user32.h>
32 : #include <uapi/asm/sigcontext.h>
33 : #include <asm/proto.h>
34 : #include <asm/vdso.h>
35 : #include <asm/sigframe.h>
36 : #include <asm/sighandling.h>
37 : #include <asm/smap.h>
38 :
39 0 : static inline void reload_segments(struct sigcontext_32 *sc)
40 : {
41 0 : unsigned int cur;
42 :
43 0 : savesegment(gs, cur);
44 0 : if ((sc->gs | 0x03) != cur)
45 0 : load_gs_index(sc->gs | 0x03);
46 0 : savesegment(fs, cur);
47 0 : if ((sc->fs | 0x03) != cur)
48 0 : loadsegment(fs, sc->fs | 0x03);
49 0 : savesegment(ds, cur);
50 0 : if ((sc->ds | 0x03) != cur)
51 0 : loadsegment(ds, sc->ds | 0x03);
52 0 : savesegment(es, cur);
53 0 : if ((sc->es | 0x03) != cur)
54 0 : loadsegment(es, sc->es | 0x03);
55 0 : }
56 :
57 : /*
58 : * Do a signal return; undo the signal stack.
59 : */
60 0 : static int ia32_restore_sigcontext(struct pt_regs *regs,
61 : struct sigcontext_32 __user *usc)
62 : {
63 0 : struct sigcontext_32 sc;
64 :
65 : /* Always make any pending restarted system calls return -EINTR */
66 0 : current->restart_block.fn = do_no_restart_syscall;
67 :
68 0 : if (unlikely(copy_from_user(&sc, usc, sizeof(sc))))
69 : return -EFAULT;
70 :
71 : /* Get only the ia32 registers. */
72 0 : regs->bx = sc.bx;
73 0 : regs->cx = sc.cx;
74 0 : regs->dx = sc.dx;
75 0 : regs->si = sc.si;
76 0 : regs->di = sc.di;
77 0 : regs->bp = sc.bp;
78 0 : regs->ax = sc.ax;
79 0 : regs->sp = sc.sp;
80 0 : regs->ip = sc.ip;
81 :
82 : /* Get CS/SS and force CPL3 */
83 0 : regs->cs = sc.cs | 0x03;
84 0 : regs->ss = sc.ss | 0x03;
85 :
86 0 : regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
87 : /* disable syscall checks */
88 0 : regs->orig_ax = -1;
89 :
90 : /*
91 : * Reload fs and gs if they have changed in the signal
92 : * handler. This does not handle long fs/gs base changes in
93 : * the handler, but does not clobber them at least in the
94 : * normal case.
95 : */
96 0 : reload_segments(&sc);
97 0 : return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
98 : }
99 :
100 0 : COMPAT_SYSCALL_DEFINE0(sigreturn)
101 : {
102 0 : struct pt_regs *regs = current_pt_regs();
103 0 : struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
104 0 : sigset_t set;
105 :
106 0 : if (!access_ok(frame, sizeof(*frame)))
107 0 : goto badframe;
108 0 : if (__get_user(set.sig[0], &frame->sc.oldmask)
109 0 : || __get_user(((__u32 *)&set)[1], &frame->extramask[0]))
110 0 : goto badframe;
111 :
112 0 : set_current_blocked(&set);
113 :
114 0 : if (ia32_restore_sigcontext(regs, &frame->sc))
115 0 : goto badframe;
116 0 : return regs->ax;
117 :
118 0 : badframe:
119 0 : signal_fault(regs, frame, "32bit sigreturn");
120 0 : return 0;
121 : }
122 :
123 0 : COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
124 : {
125 0 : struct pt_regs *regs = current_pt_regs();
126 0 : struct rt_sigframe_ia32 __user *frame;
127 0 : sigset_t set;
128 :
129 0 : frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
130 :
131 0 : if (!access_ok(frame, sizeof(*frame)))
132 0 : goto badframe;
133 0 : if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
134 0 : goto badframe;
135 :
136 0 : set_current_blocked(&set);
137 :
138 0 : if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
139 0 : goto badframe;
140 :
141 0 : if (compat_restore_altstack(&frame->uc.uc_stack))
142 0 : goto badframe;
143 :
144 0 : return regs->ax;
145 :
146 0 : badframe:
147 0 : signal_fault(regs, frame, "32bit rt sigreturn");
148 0 : return 0;
149 : }
150 :
151 : /*
152 : * Set up a signal frame.
153 : */
154 :
155 : #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
156 :
157 : static __always_inline int
158 0 : __unsafe_setup_sigcontext32(struct sigcontext_32 __user *sc,
159 : void __user *fpstate,
160 : struct pt_regs *regs, unsigned int mask)
161 : {
162 0 : unsafe_put_user(get_user_seg(gs), (unsigned int __user *)&sc->gs, Efault);
163 0 : unsafe_put_user(get_user_seg(fs), (unsigned int __user *)&sc->fs, Efault);
164 0 : unsafe_put_user(get_user_seg(ds), (unsigned int __user *)&sc->ds, Efault);
165 0 : unsafe_put_user(get_user_seg(es), (unsigned int __user *)&sc->es, Efault);
166 :
167 0 : unsafe_put_user(regs->di, &sc->di, Efault);
168 0 : unsafe_put_user(regs->si, &sc->si, Efault);
169 0 : unsafe_put_user(regs->bp, &sc->bp, Efault);
170 0 : unsafe_put_user(regs->sp, &sc->sp, Efault);
171 0 : unsafe_put_user(regs->bx, &sc->bx, Efault);
172 0 : unsafe_put_user(regs->dx, &sc->dx, Efault);
173 0 : unsafe_put_user(regs->cx, &sc->cx, Efault);
174 0 : unsafe_put_user(regs->ax, &sc->ax, Efault);
175 0 : unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
176 0 : unsafe_put_user(current->thread.error_code, &sc->err, Efault);
177 0 : unsafe_put_user(regs->ip, &sc->ip, Efault);
178 0 : unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
179 0 : unsafe_put_user(regs->flags, &sc->flags, Efault);
180 0 : unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
181 0 : unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
182 :
183 0 : unsafe_put_user(ptr_to_compat(fpstate), &sc->fpstate, Efault);
184 :
185 : /* non-iBCS2 extensions.. */
186 0 : unsafe_put_user(mask, &sc->oldmask, Efault);
187 0 : unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
188 0 : return 0;
189 :
190 : Efault:
191 : return -EFAULT;
192 : }
193 :
194 : #define unsafe_put_sigcontext32(sc, fp, regs, set, label) \
195 : do { \
196 : if (__unsafe_setup_sigcontext32(sc, fp, regs, set->sig[0])) \
197 : goto label; \
198 : } while(0)
199 :
200 : /*
201 : * Determine which stack to use..
202 : */
203 0 : static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
204 : size_t frame_size,
205 : void __user **fpstate)
206 : {
207 0 : unsigned long sp, fx_aligned, math_size;
208 :
209 : /* Default to using normal stack */
210 0 : sp = regs->sp;
211 :
212 : /* This is the X/Open sanctioned signal stack switching. */
213 0 : if (ksig->ka.sa.sa_flags & SA_ONSTACK)
214 0 : sp = sigsp(sp, ksig);
215 : /* This is the legacy signal stack switching. */
216 0 : else if (regs->ss != __USER32_DS &&
217 0 : !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
218 0 : ksig->ka.sa.sa_restorer)
219 0 : sp = (unsigned long) ksig->ka.sa.sa_restorer;
220 :
221 0 : sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
222 0 : *fpstate = (struct _fpstate_32 __user *) sp;
223 0 : if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
224 : math_size) < 0)
225 : return (void __user *) -1L;
226 :
227 0 : sp -= frame_size;
228 : /* Align the stack pointer according to the i386 ABI,
229 : * i.e. so that on function entry ((sp + 4) & 15) == 0. */
230 0 : sp = ((sp + 4) & -16ul) - 4;
231 0 : return (void __user *) sp;
232 : }
233 :
234 0 : int ia32_setup_frame(int sig, struct ksignal *ksig,
235 : compat_sigset_t *set, struct pt_regs *regs)
236 : {
237 0 : struct sigframe_ia32 __user *frame;
238 0 : void __user *restorer;
239 0 : void __user *fp = NULL;
240 :
241 : /* copy_to_user optimizes that into a single 8 byte store */
242 0 : static const struct {
243 : u16 poplmovl;
244 : u32 val;
245 : u16 int80;
246 : } __attribute__((packed)) code = {
247 : 0xb858, /* popl %eax ; movl $...,%eax */
248 : __NR_ia32_sigreturn,
249 : 0x80cd, /* int $0x80 */
250 : };
251 :
252 0 : frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
253 :
254 0 : if (ksig->ka.sa.sa_flags & SA_RESTORER) {
255 0 : restorer = ksig->ka.sa.sa_restorer;
256 : } else {
257 : /* Return stub is in 32bit vsyscall page */
258 0 : if (current->mm->context.vdso)
259 0 : restorer = current->mm->context.vdso +
260 0 : vdso_image_32.sym___kernel_sigreturn;
261 : else
262 0 : restorer = &frame->retcode;
263 : }
264 :
265 0 : if (!user_access_begin(frame, sizeof(*frame)))
266 : return -EFAULT;
267 :
268 0 : unsafe_put_user(sig, &frame->sig, Efault);
269 0 : unsafe_put_sigcontext32(&frame->sc, fp, regs, set, Efault);
270 0 : unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
271 0 : unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
272 : /*
273 : * These are actually not used anymore, but left because some
274 : * gdb versions depend on them as a marker.
275 : */
276 0 : unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
277 0 : user_access_end();
278 :
279 : /* Set up registers for signal handler */
280 0 : regs->sp = (unsigned long) frame;
281 0 : regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
282 :
283 : /* Make -mregparm=3 work */
284 0 : regs->ax = sig;
285 0 : regs->dx = 0;
286 0 : regs->cx = 0;
287 :
288 0 : loadsegment(ds, __USER32_DS);
289 0 : loadsegment(es, __USER32_DS);
290 :
291 0 : regs->cs = __USER32_CS;
292 0 : regs->ss = __USER32_DS;
293 :
294 0 : return 0;
295 : Efault:
296 : user_access_end();
297 : return -EFAULT;
298 : }
299 :
300 0 : int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
301 : compat_sigset_t *set, struct pt_regs *regs)
302 : {
303 0 : struct rt_sigframe_ia32 __user *frame;
304 0 : void __user *restorer;
305 0 : void __user *fp = NULL;
306 :
307 : /* unsafe_put_user optimizes that into a single 8 byte store */
308 0 : static const struct {
309 : u8 movl;
310 : u32 val;
311 : u16 int80;
312 : u8 pad;
313 : } __attribute__((packed)) code = {
314 : 0xb8,
315 : __NR_ia32_rt_sigreturn,
316 : 0x80cd,
317 : 0,
318 : };
319 :
320 0 : frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
321 :
322 0 : if (!user_access_begin(frame, sizeof(*frame)))
323 : return -EFAULT;
324 :
325 0 : unsafe_put_user(sig, &frame->sig, Efault);
326 0 : unsafe_put_user(ptr_to_compat(&frame->info), &frame->pinfo, Efault);
327 0 : unsafe_put_user(ptr_to_compat(&frame->uc), &frame->puc, Efault);
328 :
329 : /* Create the ucontext. */
330 0 : if (static_cpu_has(X86_FEATURE_XSAVE))
331 0 : unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
332 : else
333 0 : unsafe_put_user(0, &frame->uc.uc_flags, Efault);
334 0 : unsafe_put_user(0, &frame->uc.uc_link, Efault);
335 0 : unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
336 :
337 0 : if (ksig->ka.sa.sa_flags & SA_RESTORER)
338 0 : restorer = ksig->ka.sa.sa_restorer;
339 : else
340 0 : restorer = current->mm->context.vdso +
341 0 : vdso_image_32.sym___kernel_rt_sigreturn;
342 0 : unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
343 :
344 : /*
345 : * Not actually used anymore, but left because some gdb
346 : * versions need it.
347 : */
348 0 : unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
349 0 : unsafe_put_sigcontext32(&frame->uc.uc_mcontext, fp, regs, set, Efault);
350 0 : unsafe_put_user(*(__u64 *)set, (__u64 __user *)&frame->uc.uc_sigmask, Efault);
351 0 : user_access_end();
352 :
353 0 : if (__copy_siginfo_to_user32(&frame->info, &ksig->info))
354 : return -EFAULT;
355 :
356 : /* Set up registers for signal handler */
357 0 : regs->sp = (unsigned long) frame;
358 0 : regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
359 :
360 : /* Make -mregparm=3 work */
361 0 : regs->ax = sig;
362 0 : regs->dx = (unsigned long) &frame->info;
363 0 : regs->cx = (unsigned long) &frame->uc;
364 :
365 0 : loadsegment(ds, __USER32_DS);
366 0 : loadsegment(es, __USER32_DS);
367 :
368 0 : regs->cs = __USER32_CS;
369 0 : regs->ss = __USER32_DS;
370 :
371 0 : return 0;
372 : Efault:
373 : user_access_end();
374 : return -EFAULT;
375 : }
|