]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/tile/kernel/signal.c
arch/tile: don't allow user code to set the PL via ptrace or signal return
[net-next-2.6.git] / arch / tile / kernel / signal.c
1 /*
2  * Copyright (C) 1991, 1992  Linus Torvalds
3  * Copyright 2010 Tilera Corporation. All Rights Reserved.
4  *
5  *   This program is free software; you can redistribute it and/or
6  *   modify it under the terms of the GNU General Public License
7  *   as published by the Free Software Foundation, version 2.
8  *
9  *   This program is distributed in the hope that it will be useful, but
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12  *   NON INFRINGEMENT.  See the GNU General Public License for
13  *   more details.
14  */
15
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/unistd.h>
25 #include <linux/stddef.h>
26 #include <linux/personality.h>
27 #include <linux/suspend.h>
28 #include <linux/ptrace.h>
29 #include <linux/elf.h>
30 #include <linux/compat.h>
31 #include <linux/syscalls.h>
32 #include <linux/uaccess.h>
33 #include <asm/processor.h>
34 #include <asm/ucontext.h>
35 #include <asm/sigframe.h>
36 #include <asm/syscalls.h>
37 #include <arch/interrupts.h>
38
39 #define DEBUG_SIG 0
40
41 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
42
43
44 SYSCALL_DEFINE3(sigaltstack, const stack_t __user *, uss,
45                 stack_t __user *, uoss, struct pt_regs *, regs)
46 {
47         return do_sigaltstack(uss, uoss, regs->sp);
48 }
49
50
51 /*
52  * Do a signal return; undo the signal stack.
53  */
54
55 int restore_sigcontext(struct pt_regs *regs,
56                        struct sigcontext __user *sc, long *pr0)
57 {
58         int err = 0;
59         int i;
60
61         /* Always make any pending restarted system calls return -EINTR */
62         current_thread_info()->restart_block.fn = do_no_restart_syscall;
63
64         /*
65          * Enforce that sigcontext is like pt_regs, and doesn't mess
66          * up our stack alignment rules.
67          */
68         BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
69         BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
70
71         for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
72                 err |= __get_user(regs->regs[i], &sc->gregs[i]);
73
74         /* Ensure that the PL is always set to USER_PL. */
75         regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
76
77         regs->faultnum = INT_SWINT_1_SIGRETURN;
78
79         err |= __get_user(*pr0, &sc->gregs[0]);
80         return err;
81 }
82
83 /* sigreturn() returns long since it restores r0 in the interrupted code. */
84 SYSCALL_DEFINE1(rt_sigreturn, struct pt_regs *, regs)
85 {
86         struct rt_sigframe __user *frame =
87                 (struct rt_sigframe __user *)(regs->sp);
88         sigset_t set;
89         long r0;
90
91         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
92                 goto badframe;
93         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
94                 goto badframe;
95
96         sigdelsetmask(&set, ~_BLOCKABLE);
97         spin_lock_irq(&current->sighand->siglock);
98         current->blocked = set;
99         recalc_sigpending();
100         spin_unlock_irq(&current->sighand->siglock);
101
102         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
103                 goto badframe;
104
105         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
106                 goto badframe;
107
108         return r0;
109
110 badframe:
111         force_sig(SIGSEGV, current);
112         return 0;
113 }
114
115 /*
116  * Set up a signal frame.
117  */
118
119 int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
120 {
121         int i, err = 0;
122
123         for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
124                 err |= __put_user(regs->regs[i], &sc->gregs[i]);
125
126         return err;
127 }
128
129 /*
130  * Determine which stack to use..
131  */
132 static inline void __user *get_sigframe(struct k_sigaction *ka,
133                                         struct pt_regs *regs,
134                                         size_t frame_size)
135 {
136         unsigned long sp;
137
138         /* Default to using normal stack */
139         sp = regs->sp;
140
141         /*
142          * If we are on the alternate signal stack and would overflow
143          * it, don't.  Return an always-bogus address instead so we
144          * will die with SIGSEGV.
145          */
146         if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
147                 return (void __user __force *)-1UL;
148
149         /* This is the X/Open sanctioned signal stack switching.  */
150         if (ka->sa.sa_flags & SA_ONSTACK) {
151                 if (sas_ss_flags(sp) == 0)
152                         sp = current->sas_ss_sp + current->sas_ss_size;
153         }
154
155         sp -= frame_size;
156         /*
157          * Align the stack pointer according to the TILE ABI,
158          * i.e. so that on function entry (sp & 15) == 0.
159          */
160         sp &= -16UL;
161         return (void __user *) sp;
162 }
163
164 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
165                            sigset_t *set, struct pt_regs *regs)
166 {
167         unsigned long restorer;
168         struct rt_sigframe __user *frame;
169         int err = 0;
170         int usig;
171
172         frame = get_sigframe(ka, regs, sizeof(*frame));
173
174         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
175                 goto give_sigsegv;
176
177         usig = current_thread_info()->exec_domain
178                 && current_thread_info()->exec_domain->signal_invmap
179                 && sig < 32
180                 ? current_thread_info()->exec_domain->signal_invmap[sig]
181                 : sig;
182
183         /* Always write at least the signal number for the stack backtracer. */
184         if (ka->sa.sa_flags & SA_SIGINFO) {
185                 /* At sigreturn time, restore the callee-save registers too. */
186                 err |= copy_siginfo_to_user(&frame->info, info);
187                 regs->flags |= PT_FLAGS_RESTORE_REGS;
188         } else {
189                 err |= __put_user(info->si_signo, &frame->info.si_signo);
190         }
191
192         /* Create the ucontext.  */
193         err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
194         err |= __put_user(0, &frame->uc.uc_flags);
195         err |= __put_user(NULL, &frame->uc.uc_link);
196         err |= __put_user((void __user *)(current->sas_ss_sp),
197                           &frame->uc.uc_stack.ss_sp);
198         err |= __put_user(sas_ss_flags(regs->sp),
199                           &frame->uc.uc_stack.ss_flags);
200         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
201         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
202         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
203         if (err)
204                 goto give_sigsegv;
205
206         restorer = VDSO_BASE;
207         if (ka->sa.sa_flags & SA_RESTORER)
208                 restorer = (unsigned long) ka->sa.sa_restorer;
209
210         /*
211          * Set up registers for signal handler.
212          * Registers that we don't modify keep the value they had from
213          * user-space at the time we took the signal.
214          * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
215          * since some things rely on this (e.g. glibc's debug/segfault.c).
216          */
217         regs->pc = (unsigned long) ka->sa.sa_handler;
218         regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
219         regs->sp = (unsigned long) frame;
220         regs->lr = restorer;
221         regs->regs[0] = (unsigned long) usig;
222         regs->regs[1] = (unsigned long) &frame->info;
223         regs->regs[2] = (unsigned long) &frame->uc;
224         regs->flags |= PT_FLAGS_CALLER_SAVES;
225
226         /*
227          * Notify any tracer that was single-stepping it.
228          * The tracer may want to single-step inside the
229          * handler too.
230          */
231         if (test_thread_flag(TIF_SINGLESTEP))
232                 ptrace_notify(SIGTRAP);
233
234         return 0;
235
236 give_sigsegv:
237         force_sigsegv(sig, current);
238         return -EFAULT;
239 }
240
241 /*
242  * OK, we're invoking a handler
243  */
244
245 static int handle_signal(unsigned long sig, siginfo_t *info,
246                          struct k_sigaction *ka, sigset_t *oldset,
247                          struct pt_regs *regs)
248 {
249         int ret;
250
251
252         /* Are we from a system call? */
253         if (regs->faultnum == INT_SWINT_1) {
254                 /* If so, check system call restarting.. */
255                 switch (regs->regs[0]) {
256                 case -ERESTART_RESTARTBLOCK:
257                 case -ERESTARTNOHAND:
258                         regs->regs[0] = -EINTR;
259                         break;
260
261                 case -ERESTARTSYS:
262                         if (!(ka->sa.sa_flags & SA_RESTART)) {
263                                 regs->regs[0] = -EINTR;
264                                 break;
265                         }
266                         /* fallthrough */
267                 case -ERESTARTNOINTR:
268                         /* Reload caller-saves to restore r0..r5 and r10. */
269                         regs->flags |= PT_FLAGS_CALLER_SAVES;
270                         regs->regs[0] = regs->orig_r0;
271                         regs->pc -= 8;
272                 }
273         }
274
275         /* Set up the stack frame */
276 #ifdef CONFIG_COMPAT
277         if (is_compat_task())
278                 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
279         else
280 #endif
281                 ret = setup_rt_frame(sig, ka, info, oldset, regs);
282         if (ret == 0) {
283                 /* This code is only called from system calls or from
284                  * the work_pending path in the return-to-user code, and
285                  * either way we can re-enable interrupts unconditionally.
286                  */
287                 spin_lock_irq(&current->sighand->siglock);
288                 sigorsets(&current->blocked,
289                           &current->blocked, &ka->sa.sa_mask);
290                 if (!(ka->sa.sa_flags & SA_NODEFER))
291                         sigaddset(&current->blocked, sig);
292                 recalc_sigpending();
293                 spin_unlock_irq(&current->sighand->siglock);
294         }
295
296         return ret;
297 }
298
299 /*
300  * Note that 'init' is a special process: it doesn't get signals it doesn't
301  * want to handle. Thus you cannot kill init even with a SIGKILL even by
302  * mistake.
303  */
304 void do_signal(struct pt_regs *regs)
305 {
306         siginfo_t info;
307         int signr;
308         struct k_sigaction ka;
309         sigset_t *oldset;
310
311         /*
312          * i386 will check if we're coming from kernel mode and bail out
313          * here.  In my experience this just turns weird crashes into
314          * weird spin-hangs.  But if we find a case where this seems
315          * helpful, we can reinstate the check on "!user_mode(regs)".
316          */
317
318         if (current_thread_info()->status & TS_RESTORE_SIGMASK)
319                 oldset = &current->saved_sigmask;
320         else
321                 oldset = &current->blocked;
322
323         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
324         if (signr > 0) {
325                 /* Whee! Actually deliver the signal.  */
326                 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
327                         /*
328                          * A signal was successfully delivered; the saved
329                          * sigmask will have been stored in the signal frame,
330                          * and will be restored by sigreturn, so we can simply
331                          * clear the TS_RESTORE_SIGMASK flag.
332                          */
333                         current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
334                 }
335
336                 goto done;
337         }
338
339         /* Did we come from a system call? */
340         if (regs->faultnum == INT_SWINT_1) {
341                 /* Restart the system call - no handlers present */
342                 switch (regs->regs[0]) {
343                 case -ERESTARTNOHAND:
344                 case -ERESTARTSYS:
345                 case -ERESTARTNOINTR:
346                         regs->flags |= PT_FLAGS_CALLER_SAVES;
347                         regs->regs[0] = regs->orig_r0;
348                         regs->pc -= 8;
349                         break;
350
351                 case -ERESTART_RESTARTBLOCK:
352                         regs->flags |= PT_FLAGS_CALLER_SAVES;
353                         regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
354                         regs->pc -= 8;
355                         break;
356                 }
357         }
358
359         /* If there's no signal to deliver, just put the saved sigmask back. */
360         if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
361                 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
362                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
363         }
364
365 done:
366         /* Avoid double syscall restart if there are nested signals. */
367         regs->faultnum = INT_SWINT_1_SIGRETURN;
368 }