cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

ia32_signal.c (10257B)


      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/signal.h>
     28#include <asm/ptrace.h>
     29#include <asm/ia32_unistd.h>
     30#include <asm/user32.h>
     31#include <uapi/asm/sigcontext.h>
     32#include <asm/proto.h>
     33#include <asm/vdso.h>
     34#include <asm/sigframe.h>
     35#include <asm/sighandling.h>
     36#include <asm/smap.h>
     37
     38static 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/*
     57 * Do a signal return; undo the signal stack.
     58 */
     59static bool ia32_restore_sigcontext(struct pt_regs *regs,
     60				    struct sigcontext_32 __user *usc)
     61{
     62	struct sigcontext_32 sc;
     63
     64	/* Always make any pending restarted system calls return -EINTR */
     65	current->restart_block.fn = do_no_restart_syscall;
     66
     67	if (unlikely(copy_from_user(&sc, usc, sizeof(sc))))
     68		return false;
     69
     70	/* Get only the ia32 registers. */
     71	regs->bx = sc.bx;
     72	regs->cx = sc.cx;
     73	regs->dx = sc.dx;
     74	regs->si = sc.si;
     75	regs->di = sc.di;
     76	regs->bp = sc.bp;
     77	regs->ax = sc.ax;
     78	regs->sp = sc.sp;
     79	regs->ip = sc.ip;
     80
     81	/* Get CS/SS and force CPL3 */
     82	regs->cs = sc.cs | 0x03;
     83	regs->ss = sc.ss | 0x03;
     84
     85	regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
     86	/* disable syscall checks */
     87	regs->orig_ax = -1;
     88
     89	/*
     90	 * Reload fs and gs if they have changed in the signal
     91	 * handler.  This does not handle long fs/gs base changes in
     92	 * the handler, but does not clobber them at least in the
     93	 * normal case.
     94	 */
     95	reload_segments(&sc);
     96	return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
     97}
     98
     99COMPAT_SYSCALL_DEFINE0(sigreturn)
    100{
    101	struct pt_regs *regs = current_pt_regs();
    102	struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
    103	sigset_t set;
    104
    105	if (!access_ok(frame, sizeof(*frame)))
    106		goto badframe;
    107	if (__get_user(set.sig[0], &frame->sc.oldmask)
    108	    || __get_user(((__u32 *)&set)[1], &frame->extramask[0]))
    109		goto badframe;
    110
    111	set_current_blocked(&set);
    112
    113	if (!ia32_restore_sigcontext(regs, &frame->sc))
    114		goto badframe;
    115	return regs->ax;
    116
    117badframe:
    118	signal_fault(regs, frame, "32bit sigreturn");
    119	return 0;
    120}
    121
    122COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
    123{
    124	struct pt_regs *regs = current_pt_regs();
    125	struct rt_sigframe_ia32 __user *frame;
    126	sigset_t set;
    127
    128	frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
    129
    130	if (!access_ok(frame, sizeof(*frame)))
    131		goto badframe;
    132	if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
    133		goto badframe;
    134
    135	set_current_blocked(&set);
    136
    137	if (!ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
    138		goto badframe;
    139
    140	if (compat_restore_altstack(&frame->uc.uc_stack))
    141		goto badframe;
    142
    143	return regs->ax;
    144
    145badframe:
    146	signal_fault(regs, frame, "32bit rt sigreturn");
    147	return 0;
    148}
    149
    150/*
    151 * Set up a signal frame.
    152 */
    153
    154#define get_user_seg(seg)	({ unsigned int v; savesegment(seg, v); v; })
    155
    156static __always_inline int
    157__unsafe_setup_sigcontext32(struct sigcontext_32 __user *sc,
    158			    void __user *fpstate,
    159			    struct pt_regs *regs, unsigned int mask)
    160{
    161	unsafe_put_user(get_user_seg(gs), (unsigned int __user *)&sc->gs, Efault);
    162	unsafe_put_user(get_user_seg(fs), (unsigned int __user *)&sc->fs, Efault);
    163	unsafe_put_user(get_user_seg(ds), (unsigned int __user *)&sc->ds, Efault);
    164	unsafe_put_user(get_user_seg(es), (unsigned int __user *)&sc->es, Efault);
    165
    166	unsafe_put_user(regs->di, &sc->di, Efault);
    167	unsafe_put_user(regs->si, &sc->si, Efault);
    168	unsafe_put_user(regs->bp, &sc->bp, Efault);
    169	unsafe_put_user(regs->sp, &sc->sp, Efault);
    170	unsafe_put_user(regs->bx, &sc->bx, Efault);
    171	unsafe_put_user(regs->dx, &sc->dx, Efault);
    172	unsafe_put_user(regs->cx, &sc->cx, Efault);
    173	unsafe_put_user(regs->ax, &sc->ax, Efault);
    174	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);
    177	unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
    178	unsafe_put_user(regs->flags, &sc->flags, Efault);
    179	unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
    180	unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
    181
    182	unsafe_put_user(ptr_to_compat(fpstate), &sc->fpstate, Efault);
    183
    184	/* non-iBCS2 extensions.. */
    185	unsafe_put_user(mask, &sc->oldmask, Efault);
    186	unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
    187	return 0;
    188
    189Efault:
    190	return -EFAULT;
    191}
    192
    193#define unsafe_put_sigcontext32(sc, fp, regs, set, label)		\
    194do {									\
    195	if (__unsafe_setup_sigcontext32(sc, fp, regs, set->sig[0]))	\
    196		goto label;						\
    197} while(0)
    198
    199/*
    200 * Determine which stack to use..
    201 */
    202static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
    203				 size_t frame_size,
    204				 void __user **fpstate)
    205{
    206	unsigned long sp, fx_aligned, math_size;
    207
    208	/* Default to using normal stack */
    209	sp = regs->sp;
    210
    211	/* This is the X/Open sanctioned signal stack switching.  */
    212	if (ksig->ka.sa.sa_flags & SA_ONSTACK)
    213		sp = sigsp(sp, ksig);
    214	/* This is the legacy signal stack switching. */
    215	else if (regs->ss != __USER32_DS &&
    216		!(ksig->ka.sa.sa_flags & SA_RESTORER) &&
    217		 ksig->ka.sa.sa_restorer)
    218		sp = (unsigned long) ksig->ka.sa.sa_restorer;
    219
    220	sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
    221	*fpstate = (struct _fpstate_32 __user *) sp;
    222	if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
    223				      math_size))
    224		return (void __user *) -1L;
    225
    226	sp -= frame_size;
    227	/* Align the stack pointer according to the i386 ABI,
    228	 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
    229	sp = ((sp + 4) & -16ul) - 4;
    230	return (void __user *) sp;
    231}
    232
    233int ia32_setup_frame(int sig, struct ksignal *ksig,
    234		     compat_sigset_t *set, struct pt_regs *regs)
    235{
    236	struct sigframe_ia32 __user *frame;
    237	void __user *restorer;
    238	void __user *fp = NULL;
    239
    240	/* copy_to_user optimizes that into a single 8 byte store */
    241	static const struct {
    242		u16 poplmovl;
    243		u32 val;
    244		u16 int80;
    245	} __attribute__((packed)) code = {
    246		0xb858,		 /* popl %eax ; movl $...,%eax */
    247		__NR_ia32_sigreturn,
    248		0x80cd,		/* int $0x80 */
    249	};
    250
    251	frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
    252
    253	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
    254		restorer = ksig->ka.sa.sa_restorer;
    255	} else {
    256		/* Return stub is in 32bit vsyscall page */
    257		if (current->mm->context.vdso)
    258			restorer = current->mm->context.vdso +
    259				vdso_image_32.sym___kernel_sigreturn;
    260		else
    261			restorer = &frame->retcode;
    262	}
    263
    264	if (!user_access_begin(frame, sizeof(*frame)))
    265		return -EFAULT;
    266
    267	unsafe_put_user(sig, &frame->sig, Efault);
    268	unsafe_put_sigcontext32(&frame->sc, fp, regs, set, Efault);
    269	unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
    270	unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
    271	/*
    272	 * These are actually not used anymore, but left because some
    273	 * gdb versions depend on them as a marker.
    274	 */
    275	unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
    276	user_access_end();
    277
    278	/* Set up registers for signal handler */
    279	regs->sp = (unsigned long) frame;
    280	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
    281
    282	/* Make -mregparm=3 work */
    283	regs->ax = sig;
    284	regs->dx = 0;
    285	regs->cx = 0;
    286
    287	loadsegment(ds, __USER32_DS);
    288	loadsegment(es, __USER32_DS);
    289
    290	regs->cs = __USER32_CS;
    291	regs->ss = __USER32_DS;
    292
    293	return 0;
    294Efault:
    295	user_access_end();
    296	return -EFAULT;
    297}
    298
    299int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
    300			compat_sigset_t *set, struct pt_regs *regs)
    301{
    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(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_compat_save_altstack(&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 = sig;
    361	regs->dx = (unsigned long) &frame->info;
    362	regs->cx = (unsigned long) &frame->uc;
    363
    364	loadsegment(ds, __USER32_DS);
    365	loadsegment(es, __USER32_DS);
    366
    367	regs->cs = __USER32_CS;
    368	regs->ss = __USER32_DS;
    369
    370	return 0;
    371Efault:
    372	user_access_end();
    373	return -EFAULT;
    374}