Path: blob/master/arch/tile/kernel/compat_signal.c
10817 views
/*1* Copyright 2010 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*/1314#include <linux/sched.h>15#include <linux/mm.h>16#include <linux/smp.h>17#include <linux/kernel.h>18#include <linux/signal.h>19#include <linux/errno.h>20#include <linux/wait.h>21#include <linux/unistd.h>22#include <linux/stddef.h>23#include <linux/personality.h>24#include <linux/suspend.h>25#include <linux/ptrace.h>26#include <linux/elf.h>27#include <linux/compat.h>28#include <linux/syscalls.h>29#include <linux/uaccess.h>30#include <asm/processor.h>31#include <asm/ucontext.h>32#include <asm/sigframe.h>33#include <asm/syscalls.h>34#include <arch/interrupts.h>3536struct compat_sigaction {37compat_uptr_t sa_handler;38compat_ulong_t sa_flags;39compat_uptr_t sa_restorer;40sigset_t sa_mask __packed;41};4243struct compat_sigaltstack {44compat_uptr_t ss_sp;45int ss_flags;46compat_size_t ss_size;47};4849struct compat_ucontext {50compat_ulong_t uc_flags;51compat_uptr_t uc_link;52struct compat_sigaltstack uc_stack;53struct sigcontext uc_mcontext;54sigset_t uc_sigmask; /* mask last for extensibility */55};5657#define COMPAT_SI_PAD_SIZE ((SI_MAX_SIZE - 3 * sizeof(int)) / sizeof(int))5859struct compat_siginfo {60int si_signo;61int si_errno;62int si_code;6364union {65int _pad[COMPAT_SI_PAD_SIZE];6667/* kill() */68struct {69unsigned int _pid; /* sender's pid */70unsigned int _uid; /* sender's uid */71} _kill;7273/* POSIX.1b timers */74struct {75compat_timer_t _tid; /* timer id */76int _overrun; /* overrun count */77compat_sigval_t _sigval; /* same as below */78int _sys_private; /* not to be passed to user */79int _overrun_incr; /* amount to add to overrun */80} _timer;8182/* POSIX.1b signals */83struct {84unsigned int _pid; /* sender's pid */85unsigned int _uid; /* sender's uid */86compat_sigval_t _sigval;87} _rt;8889/* SIGCHLD */90struct {91unsigned int _pid; /* which child */92unsigned int _uid; /* sender's uid */93int _status; /* exit code */94compat_clock_t _utime;95compat_clock_t _stime;96} _sigchld;9798/* SIGILL, SIGFPE, SIGSEGV, SIGBUS */99struct {100unsigned int _addr; /* faulting insn/memory ref. */101#ifdef __ARCH_SI_TRAPNO102int _trapno; /* TRAP # which caused the signal */103#endif104} _sigfault;105106/* SIGPOLL */107struct {108int _band; /* POLL_IN, POLL_OUT, POLL_MSG */109int _fd;110} _sigpoll;111} _sifields;112};113114struct compat_rt_sigframe {115unsigned char save_area[C_ABI_SAVE_AREA_SIZE]; /* caller save area */116struct compat_siginfo info;117struct compat_ucontext uc;118};119120#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))121122long compat_sys_rt_sigaction(int sig, struct compat_sigaction __user *act,123struct compat_sigaction __user *oact,124size_t sigsetsize)125{126struct k_sigaction new_sa, old_sa;127int ret = -EINVAL;128129/* XXX: Don't preclude handling different sized sigset_t's. */130if (sigsetsize != sizeof(sigset_t))131goto out;132133if (act) {134compat_uptr_t handler, restorer;135136if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||137__get_user(handler, &act->sa_handler) ||138__get_user(new_sa.sa.sa_flags, &act->sa_flags) ||139__get_user(restorer, &act->sa_restorer) ||140__copy_from_user(&new_sa.sa.sa_mask, &act->sa_mask,141sizeof(sigset_t)))142return -EFAULT;143new_sa.sa.sa_handler = compat_ptr(handler);144new_sa.sa.sa_restorer = compat_ptr(restorer);145}146147ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);148149if (!ret && oact) {150if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||151__put_user(ptr_to_compat(old_sa.sa.sa_handler),152&oact->sa_handler) ||153__put_user(ptr_to_compat(old_sa.sa.sa_restorer),154&oact->sa_restorer) ||155__put_user(old_sa.sa.sa_flags, &oact->sa_flags) ||156__copy_to_user(&oact->sa_mask, &old_sa.sa.sa_mask,157sizeof(sigset_t)))158return -EFAULT;159}160out:161return ret;162}163164long compat_sys_rt_sigqueueinfo(int pid, int sig,165struct compat_siginfo __user *uinfo)166{167siginfo_t info;168int ret;169mm_segment_t old_fs = get_fs();170171if (copy_siginfo_from_user32(&info, uinfo))172return -EFAULT;173set_fs(KERNEL_DS);174ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __force __user *)&info);175set_fs(old_fs);176return ret;177}178179int copy_siginfo_to_user32(struct compat_siginfo __user *to, siginfo_t *from)180{181int err;182183if (!access_ok(VERIFY_WRITE, to, sizeof(struct compat_siginfo)))184return -EFAULT;185186/* If you change siginfo_t structure, please make sure that187this code is fixed accordingly.188It should never copy any pad contained in the structure189to avoid security leaks, but must copy the generic1903 ints plus the relevant union member. */191err = __put_user(from->si_signo, &to->si_signo);192err |= __put_user(from->si_errno, &to->si_errno);193err |= __put_user((short)from->si_code, &to->si_code);194195if (from->si_code < 0) {196err |= __put_user(from->si_pid, &to->si_pid);197err |= __put_user(from->si_uid, &to->si_uid);198err |= __put_user(ptr_to_compat(from->si_ptr), &to->si_ptr);199} else {200/*201* First 32bits of unions are always present:202* si_pid === si_band === si_tid === si_addr(LS half)203*/204err |= __put_user(from->_sifields._pad[0],205&to->_sifields._pad[0]);206switch (from->si_code >> 16) {207case __SI_FAULT >> 16:208break;209case __SI_CHLD >> 16:210err |= __put_user(from->si_utime, &to->si_utime);211err |= __put_user(from->si_stime, &to->si_stime);212err |= __put_user(from->si_status, &to->si_status);213/* FALL THROUGH */214default:215case __SI_KILL >> 16:216err |= __put_user(from->si_uid, &to->si_uid);217break;218case __SI_POLL >> 16:219err |= __put_user(from->si_fd, &to->si_fd);220break;221case __SI_TIMER >> 16:222err |= __put_user(from->si_overrun, &to->si_overrun);223err |= __put_user(ptr_to_compat(from->si_ptr),224&to->si_ptr);225break;226/* This is not generated by the kernel as of now. */227case __SI_RT >> 16:228case __SI_MESGQ >> 16:229err |= __put_user(from->si_uid, &to->si_uid);230err |= __put_user(from->si_int, &to->si_int);231break;232}233}234return err;235}236237int copy_siginfo_from_user32(siginfo_t *to, struct compat_siginfo __user *from)238{239int err;240u32 ptr32;241242if (!access_ok(VERIFY_READ, from, sizeof(struct compat_siginfo)))243return -EFAULT;244245err = __get_user(to->si_signo, &from->si_signo);246err |= __get_user(to->si_errno, &from->si_errno);247err |= __get_user(to->si_code, &from->si_code);248249err |= __get_user(to->si_pid, &from->si_pid);250err |= __get_user(to->si_uid, &from->si_uid);251err |= __get_user(ptr32, &from->si_ptr);252to->si_ptr = compat_ptr(ptr32);253254return err;255}256257long compat_sys_sigaltstack(const struct compat_sigaltstack __user *uss_ptr,258struct compat_sigaltstack __user *uoss_ptr,259struct pt_regs *regs)260{261stack_t uss, uoss;262int ret;263mm_segment_t seg;264265if (uss_ptr) {266u32 ptr;267268memset(&uss, 0, sizeof(stack_t));269if (!access_ok(VERIFY_READ, uss_ptr, sizeof(*uss_ptr)) ||270__get_user(ptr, &uss_ptr->ss_sp) ||271__get_user(uss.ss_flags, &uss_ptr->ss_flags) ||272__get_user(uss.ss_size, &uss_ptr->ss_size))273return -EFAULT;274uss.ss_sp = compat_ptr(ptr);275}276seg = get_fs();277set_fs(KERNEL_DS);278ret = do_sigaltstack(uss_ptr ? (stack_t __user __force *)&uss : NULL,279(stack_t __user __force *)&uoss,280(unsigned long)compat_ptr(regs->sp));281set_fs(seg);282if (ret >= 0 && uoss_ptr) {283if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(*uoss_ptr)) ||284__put_user(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp) ||285__put_user(uoss.ss_flags, &uoss_ptr->ss_flags) ||286__put_user(uoss.ss_size, &uoss_ptr->ss_size))287ret = -EFAULT;288}289return ret;290}291292/* The assembly shim for this function arranges to ignore the return value. */293long compat_sys_rt_sigreturn(struct pt_regs *regs)294{295struct compat_rt_sigframe __user *frame =296(struct compat_rt_sigframe __user *) compat_ptr(regs->sp);297sigset_t set;298299if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))300goto badframe;301if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))302goto badframe;303304sigdelsetmask(&set, ~_BLOCKABLE);305spin_lock_irq(¤t->sighand->siglock);306current->blocked = set;307recalc_sigpending();308spin_unlock_irq(¤t->sighand->siglock);309310if (restore_sigcontext(regs, &frame->uc.uc_mcontext))311goto badframe;312313if (compat_sys_sigaltstack(&frame->uc.uc_stack, NULL, regs) != 0)314goto badframe;315316return 0;317318badframe:319signal_fault("bad sigreturn frame", regs, frame, 0);320return 0;321}322323/*324* Determine which stack to use..325*/326static inline void __user *compat_get_sigframe(struct k_sigaction *ka,327struct pt_regs *regs,328size_t frame_size)329{330unsigned long sp;331332/* Default to using normal stack */333sp = (unsigned long)compat_ptr(regs->sp);334335/*336* If we are on the alternate signal stack and would overflow337* it, don't. Return an always-bogus address instead so we338* will die with SIGSEGV.339*/340if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))341return (void __user __force *)-1UL;342343/* This is the X/Open sanctioned signal stack switching. */344if (ka->sa.sa_flags & SA_ONSTACK) {345if (sas_ss_flags(sp) == 0)346sp = current->sas_ss_sp + current->sas_ss_size;347}348349sp -= frame_size;350/*351* Align the stack pointer according to the TILE ABI,352* i.e. so that on function entry (sp & 15) == 0.353*/354sp &= -16UL;355return (void __user *) sp;356}357358int compat_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,359sigset_t *set, struct pt_regs *regs)360{361unsigned long restorer;362struct compat_rt_sigframe __user *frame;363int err = 0;364int usig;365366frame = compat_get_sigframe(ka, regs, sizeof(*frame));367368if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))369goto give_sigsegv;370371usig = current_thread_info()->exec_domain372&& current_thread_info()->exec_domain->signal_invmap373&& sig < 32374? current_thread_info()->exec_domain->signal_invmap[sig]375: sig;376377/* Always write at least the signal number for the stack backtracer. */378if (ka->sa.sa_flags & SA_SIGINFO) {379/* At sigreturn time, restore the callee-save registers too. */380err |= copy_siginfo_to_user32(&frame->info, info);381regs->flags |= PT_FLAGS_RESTORE_REGS;382} else {383err |= __put_user(info->si_signo, &frame->info.si_signo);384}385386/* Create the ucontext. */387err |= __clear_user(&frame->save_area, sizeof(frame->save_area));388err |= __put_user(0, &frame->uc.uc_flags);389err |= __put_user(0, &frame->uc.uc_link);390err |= __put_user(ptr_to_compat((void *)(current->sas_ss_sp)),391&frame->uc.uc_stack.ss_sp);392err |= __put_user(sas_ss_flags(regs->sp),393&frame->uc.uc_stack.ss_flags);394err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);395err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);396err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));397if (err)398goto give_sigsegv;399400restorer = VDSO_BASE;401if (ka->sa.sa_flags & SA_RESTORER)402restorer = ptr_to_compat_reg(ka->sa.sa_restorer);403404/*405* Set up registers for signal handler.406* Registers that we don't modify keep the value they had from407* user-space at the time we took the signal.408*/409regs->pc = ptr_to_compat_reg(ka->sa.sa_handler);410regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */411regs->sp = ptr_to_compat_reg(frame);412regs->lr = restorer;413regs->regs[0] = (unsigned long) usig;414415if (ka->sa.sa_flags & SA_SIGINFO) {416/* Need extra arguments, so mark to restore caller-saves. */417regs->regs[1] = ptr_to_compat_reg(&frame->info);418regs->regs[2] = ptr_to_compat_reg(&frame->uc);419regs->flags |= PT_FLAGS_CALLER_SAVES;420}421422/*423* Notify any tracer that was single-stepping it.424* The tracer may want to single-step inside the425* handler too.426*/427if (test_thread_flag(TIF_SINGLESTEP))428ptrace_notify(SIGTRAP);429430return 0;431432give_sigsegv:433signal_fault("bad setup frame", regs, frame, sig);434return -EFAULT;435}436437438