f56141e3e2
If an attacker can cause a controlled kernel stack overflow, overwriting the restart block is a very juicy exploit target. This is because the restart_block is held in the same memory allocation as the kernel stack. Moving the restart block to struct task_struct prevents this exploit by making the restart_block harder to locate. Note that there are other fields in thread_info that are also easy targets, at least on some architectures. It's also a decent simplification, since the restart code is more or less identical on all architectures. [james.hogan@imgtec.com: metag: align thread_info::supervisor_stack] Signed-off-by: Andy Lutomirski <luto@amacapital.net> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Kees Cook <keescook@chromium.org> Cc: David Miller <davem@davemloft.net> Acked-by: Richard Weinberger <richard@nod.at> Cc: Richard Henderson <rth@twiddle.net> Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru> Cc: Matt Turner <mattst88@gmail.com> Cc: Vineet Gupta <vgupta@synopsys.com> Cc: Russell King <rmk@arm.linux.org.uk> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Haavard Skinnemoen <hskinnemoen@gmail.com> Cc: Hans-Christian Egtvedt <egtvedt@samfundet.no> Cc: Steven Miao <realmz6@gmail.com> Cc: Mark Salter <msalter@redhat.com> Cc: Aurelien Jacquiot <a-jacquiot@ti.com> Cc: Mikael Starvik <starvik@axis.com> Cc: Jesper Nilsson <jesper.nilsson@axis.com> Cc: David Howells <dhowells@redhat.com> Cc: Richard Kuo <rkuo@codeaurora.org> Cc: "Luck, Tony" <tony.luck@intel.com> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Michal Simek <monstr@monstr.eu> Cc: Ralf Baechle <ralf@linux-mips.org> Cc: Jonas Bonn <jonas@southpole.se> Cc: "James E.J. Bottomley" <jejb@parisc-linux.org> Cc: Helge Deller <deller@gmx.de> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Paul Mackerras <paulus@samba.org> Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc) Tested-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc) Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Chen Liqin <liqin.linux@gmail.com> Cc: Lennox Wu <lennox.wu@gmail.com> Cc: Chris Metcalf <cmetcalf@ezchip.com> Cc: Guan Xuetao <gxt@mprc.pku.edu.cn> Cc: Chris Zankel <chris@zankel.net> Cc: Max Filippov <jcmvbkbc@gmail.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Guenter Roeck <linux@roeck-us.net> Signed-off-by: James Hogan <james.hogan@imgtec.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
289 lines
6.0 KiB
C
289 lines
6.0 KiB
C
/*
|
|
* Copyright (C) 2004-2006 Atmel Corporation
|
|
*
|
|
* Based on linux/arch/sh/kernel/signal.c
|
|
* Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/sched.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/ptrace.h>
|
|
#include <linux/unistd.h>
|
|
#include <linux/tracehook.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
#include <asm/ucontext.h>
|
|
#include <asm/syscalls.h>
|
|
|
|
struct rt_sigframe
|
|
{
|
|
struct siginfo info;
|
|
struct ucontext uc;
|
|
unsigned long retcode;
|
|
};
|
|
|
|
static int
|
|
restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
|
|
{
|
|
int err = 0;
|
|
|
|
#define COPY(x) err |= __get_user(regs->x, &sc->x)
|
|
COPY(sr);
|
|
COPY(pc);
|
|
COPY(lr);
|
|
COPY(sp);
|
|
COPY(r12);
|
|
COPY(r11);
|
|
COPY(r10);
|
|
COPY(r9);
|
|
COPY(r8);
|
|
COPY(r7);
|
|
COPY(r6);
|
|
COPY(r5);
|
|
COPY(r4);
|
|
COPY(r3);
|
|
COPY(r2);
|
|
COPY(r1);
|
|
COPY(r0);
|
|
#undef COPY
|
|
|
|
/*
|
|
* Don't allow anyone to pretend they're running in supervisor
|
|
* mode or something...
|
|
*/
|
|
err |= !valid_user_regs(regs);
|
|
|
|
return err;
|
|
}
|
|
|
|
|
|
asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
|
|
{
|
|
struct rt_sigframe __user *frame;
|
|
sigset_t set;
|
|
|
|
/* Always make any pending restarted system calls return -EINTR */
|
|
current->restart_block.fn = do_no_restart_syscall;
|
|
|
|
frame = (struct rt_sigframe __user *)regs->sp;
|
|
pr_debug("SIG return: frame = %p\n", frame);
|
|
|
|
if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
|
|
goto badframe;
|
|
|
|
if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
|
|
goto badframe;
|
|
|
|
set_current_blocked(&set);
|
|
|
|
if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
|
|
goto badframe;
|
|
|
|
if (restore_altstack(&frame->uc.uc_stack))
|
|
goto badframe;
|
|
|
|
pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
|
|
regs->pc, regs->lr, regs->sp);
|
|
|
|
return regs->r12;
|
|
|
|
badframe:
|
|
force_sig(SIGSEGV, current);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
|
|
{
|
|
int err = 0;
|
|
|
|
#define COPY(x) err |= __put_user(regs->x, &sc->x)
|
|
COPY(sr);
|
|
COPY(pc);
|
|
COPY(lr);
|
|
COPY(sp);
|
|
COPY(r12);
|
|
COPY(r11);
|
|
COPY(r10);
|
|
COPY(r9);
|
|
COPY(r8);
|
|
COPY(r7);
|
|
COPY(r6);
|
|
COPY(r5);
|
|
COPY(r4);
|
|
COPY(r3);
|
|
COPY(r2);
|
|
COPY(r1);
|
|
COPY(r0);
|
|
#undef COPY
|
|
|
|
return err;
|
|
}
|
|
|
|
static inline void __user *
|
|
get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
|
|
{
|
|
unsigned long sp = sigsp(regs->sp, ksig);
|
|
|
|
return (void __user *)((sp - framesize) & ~3);
|
|
}
|
|
|
|
static int
|
|
setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
|
|
{
|
|
struct rt_sigframe __user *frame;
|
|
int err = 0;
|
|
|
|
frame = get_sigframe(ksig, regs, sizeof(*frame));
|
|
err = -EFAULT;
|
|
if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
|
|
goto out;
|
|
|
|
/*
|
|
* Set up the return code:
|
|
*
|
|
* mov r8, __NR_rt_sigreturn
|
|
* scall
|
|
*
|
|
* Note: This will blow up since we're using a non-executable
|
|
* stack. Better use SA_RESTORER.
|
|
*/
|
|
#if __NR_rt_sigreturn > 127
|
|
# error __NR_rt_sigreturn must be < 127 to fit in a short mov
|
|
#endif
|
|
err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
|
|
&frame->retcode);
|
|
|
|
err |= copy_siginfo_to_user(&frame->info, &ksig->info);
|
|
|
|
/* Set up the ucontext */
|
|
err |= __put_user(0, &frame->uc.uc_flags);
|
|
err |= __put_user(NULL, &frame->uc.uc_link);
|
|
err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
|
|
err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
|
|
err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
|
|
|
|
if (err)
|
|
goto out;
|
|
|
|
regs->r12 = ksig->sig;
|
|
regs->r11 = (unsigned long) &frame->info;
|
|
regs->r10 = (unsigned long) &frame->uc;
|
|
regs->sp = (unsigned long) frame;
|
|
if (ksig->ka.sa.sa_flags & SA_RESTORER)
|
|
regs->lr = (unsigned long)ksig->ka.sa.sa_restorer;
|
|
else {
|
|
printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
|
|
current->comm, current->pid);
|
|
regs->lr = (unsigned long) &frame->retcode;
|
|
}
|
|
|
|
pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
|
|
current->comm, current->pid, ksig->sig, regs->sp,
|
|
regs->pc, ksig->ka.sa.sa_handler, regs->lr);
|
|
|
|
regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
|
|
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static inline void setup_syscall_restart(struct pt_regs *regs)
|
|
{
|
|
if (regs->r12 == -ERESTART_RESTARTBLOCK)
|
|
regs->r8 = __NR_restart_syscall;
|
|
else
|
|
regs->r12 = regs->r12_orig;
|
|
regs->pc -= 2;
|
|
}
|
|
|
|
static inline void
|
|
handle_signal(struct ksignal *ksig, struct pt_regs *regs, int syscall)
|
|
{
|
|
int ret;
|
|
|
|
/*
|
|
* Set up the stack frame
|
|
*/
|
|
ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
|
|
|
|
/*
|
|
* Check that the resulting registers are sane
|
|
*/
|
|
ret |= !valid_user_regs(regs);
|
|
|
|
/*
|
|
* Block the signal if we were successful.
|
|
*/
|
|
signal_setup_done(ret, ksig, 0);
|
|
}
|
|
|
|
/*
|
|
* Note that 'init' is a special process: it doesn't get signals it
|
|
* doesn't want to handle. Thus you cannot kill init even with a
|
|
* SIGKILL even by mistake.
|
|
*/
|
|
static void do_signal(struct pt_regs *regs, int syscall)
|
|
{
|
|
struct ksignal ksig;
|
|
|
|
/*
|
|
* We want the common case to go fast, which is why we may in
|
|
* certain cases get here from kernel mode. Just return
|
|
* without doing anything if so.
|
|
*/
|
|
if (!user_mode(regs))
|
|
return;
|
|
|
|
get_signal(&ksig);
|
|
if (syscall) {
|
|
switch (regs->r12) {
|
|
case -ERESTART_RESTARTBLOCK:
|
|
case -ERESTARTNOHAND:
|
|
if (ksig.sig > 0) {
|
|
regs->r12 = -EINTR;
|
|
break;
|
|
}
|
|
/* fall through */
|
|
case -ERESTARTSYS:
|
|
if (ksig.sig > 0 && !(ksig.ka.sa.sa_flags & SA_RESTART)) {
|
|
regs->r12 = -EINTR;
|
|
break;
|
|
}
|
|
/* fall through */
|
|
case -ERESTARTNOINTR:
|
|
setup_syscall_restart(regs);
|
|
}
|
|
}
|
|
|
|
if (!ksig.sig) {
|
|
/* No signal to deliver -- put the saved sigmask back */
|
|
restore_saved_sigmask();
|
|
return;
|
|
}
|
|
|
|
handle_signal(&ksig, regs, syscall);
|
|
}
|
|
|
|
asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
|
|
{
|
|
int syscall = 0;
|
|
|
|
if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
|
|
syscall = 1;
|
|
|
|
if (ti->flags & _TIF_SIGPENDING)
|
|
do_signal(regs, syscall);
|
|
|
|
if (ti->flags & _TIF_NOTIFY_RESUME) {
|
|
clear_thread_flag(TIF_NOTIFY_RESUME);
|
|
tracehook_notify_resume(regs);
|
|
}
|
|
}
|