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