blob: 7a06651e04bcac9845c65a87a7eabd1b65e93534 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/sched.h>
17#include <linux/fs.h>
18#include <linux/tty.h>
19#include <linux/binfmts.h>
20#include <linux/security.h>
21#include <linux/syscalls.h>
22#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070024#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090025#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070026#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080029#include <linux/pid_namespace.h>
30#include <linux/nsproxy.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080031#include <linux/user_namespace.h>
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050032#define CREATE_TRACE_POINTS
33#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/param.h>
36#include <asm/uaccess.h>
37#include <asm/unistd.h>
38#include <asm/siginfo.h>
David Howellsd550bbd2012-03-28 18:30:03 +010039#include <asm/cacheflush.h>
Al Viroe1396062006-05-25 10:19:47 -040040#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * SLAB caches for signal bits.
44 */
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090048int print_fatal_signals __read_mostly;
49
Roland McGrath35de2542008-07-25 19:45:51 -070050static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070051{
Roland McGrath35de2542008-07-25 19:45:51 -070052 return t->sighand->action[sig - 1].sa.sa_handler;
53}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070054
Roland McGrath35de2542008-07-25 19:45:51 -070055static int sig_handler_ignored(void __user *handler, int sig)
56{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070057 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070058 return handler == SIG_IGN ||
59 (handler == SIG_DFL && sig_kernel_ignore(sig));
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070062static int sig_task_ignored(struct task_struct *t, int sig, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Roland McGrath35de2542008-07-25 19:45:51 -070064 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Oleg Nesterovf008faf2009-04-02 16:58:02 -070066 handler = sig_handler(t, sig);
67
68 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070069 handler == SIG_DFL && !force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070070 return 1;
71
72 return sig_handler_ignored(handler, sig);
73}
74
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070075static int sig_ignored(struct task_struct *t, int sig, bool force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070076{
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /*
78 * Blocked signals are never ignored, since the
79 * signal handler may change by the time it is
80 * unblocked.
81 */
Roland McGrath325d22d2007-11-12 15:41:55 -080082 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
84
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070085 if (!sig_task_ignored(t, sig, force))
Roland McGrath35de2542008-07-25 19:45:51 -070086 return 0;
87
88 /*
89 * Tracers may want to know about even ignored signals.
90 */
Tejun Heoa288eec2011-06-17 16:50:37 +020091 return !t->ptrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94/*
95 * Re-calculate pending state from the set of locally pending
96 * signals, globally pending signals, and blocked signals.
97 */
98static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
99{
100 unsigned long ready;
101 long i;
102
103 switch (_NSIG_WORDS) {
104 default:
105 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
106 ready |= signal->sig[i] &~ blocked->sig[i];
107 break;
108
109 case 4: ready = signal->sig[3] &~ blocked->sig[3];
110 ready |= signal->sig[2] &~ blocked->sig[2];
111 ready |= signal->sig[1] &~ blocked->sig[1];
112 ready |= signal->sig[0] &~ blocked->sig[0];
113 break;
114
115 case 2: ready = signal->sig[1] &~ blocked->sig[1];
116 ready |= signal->sig[0] &~ blocked->sig[0];
117 break;
118
119 case 1: ready = signal->sig[0] &~ blocked->sig[0];
120 }
121 return ready != 0;
122}
123
124#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
125
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700126static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200128 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700130 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700132 return 1;
133 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700134 /*
135 * We must never clear the flag in another thread, or in current
136 * when it's possible the current syscall is returning -ERESTART*.
137 * So we don't clear it here, and only callers who know they should do.
138 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700139 return 0;
140}
141
142/*
143 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
144 * This is superfluous when called on current, the wakeup is a harmless no-op.
145 */
146void recalc_sigpending_and_wake(struct task_struct *t)
147{
148 if (recalc_sigpending_tsk(t))
149 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152void recalc_sigpending(void)
153{
Tejun Heodd1d6772011-06-02 11:14:00 +0200154 if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700155 clear_thread_flag(TIF_SIGPENDING);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/* Given the mask, find the first available signal that should be serviced. */
160
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800161#define SYNCHRONOUS_MASK \
162 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
163 sigmask(SIGTRAP) | sigmask(SIGFPE))
164
Davide Libenzifba2afa2007-05-10 22:23:13 -0700165int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned long i, *s, *m, x;
168 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 s = pending->signal.sig;
171 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800172
173 /*
174 * Handle the first word specially: it contains the
175 * synchronous signals that need to be dequeued first.
176 */
177 x = *s &~ *m;
178 if (x) {
179 if (x & SYNCHRONOUS_MASK)
180 x &= SYNCHRONOUS_MASK;
181 sig = ffz(~x) + 1;
182 return sig;
183 }
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 switch (_NSIG_WORDS) {
186 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800187 for (i = 1; i < _NSIG_WORDS; ++i) {
188 x = *++s &~ *++m;
189 if (!x)
190 continue;
191 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800196 case 2:
197 x = s[1] &~ m[1];
198 if (!x)
199 break;
200 sig = ffz(~x) + _NSIG_BPW + 1;
201 break;
202
203 case 1:
204 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return sig;
209}
210
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900211static inline void print_dropped_signal(int sig)
212{
213 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
214
215 if (!print_fatal_signals)
216 return;
217
218 if (!__ratelimit(&ratelimit_state))
219 return;
220
221 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
222 current->comm, current->pid, sig);
223}
224
Tejun Heoe5c19022011-03-23 10:37:00 +0100225/**
Tejun Heo7dd3db52011-06-02 11:14:00 +0200226 * task_set_jobctl_pending - set jobctl pending bits
227 * @task: target task
228 * @mask: pending bits to set
229 *
230 * Clear @mask from @task->jobctl. @mask must be subset of
231 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
232 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
233 * cleared. If @task is already being killed or exiting, this function
234 * becomes noop.
235 *
236 * CONTEXT:
237 * Must be called with @task->sighand->siglock held.
238 *
239 * RETURNS:
240 * %true if @mask is set, %false if made noop because @task was dying.
241 */
242bool task_set_jobctl_pending(struct task_struct *task, unsigned int mask)
243{
244 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
245 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
246 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
247
248 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
249 return false;
250
251 if (mask & JOBCTL_STOP_SIGMASK)
252 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
253
254 task->jobctl |= mask;
255 return true;
256}
257
258/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200259 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100260 * @task: target task
261 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200262 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
263 * Clear it and wake up the ptracer. Note that we don't need any further
264 * locking. @task->siglock guarantees that @task->parent points to the
265 * ptracer.
Tejun Heod79fdd62011-03-23 10:37:00 +0100266 *
267 * CONTEXT:
268 * Must be called with @task->sighand->siglock held.
269 */
Tejun Heo73ddff22011-06-14 11:20:14 +0200270void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100271{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200272 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
273 task->jobctl &= ~JOBCTL_TRAPPING;
Tejun Heo62c124f2011-06-02 11:14:00 +0200274 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
Tejun Heod79fdd62011-03-23 10:37:00 +0100275 }
276}
277
278/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200279 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c19022011-03-23 10:37:00 +0100280 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200281 * @mask: pending bits to clear
Tejun Heoe5c19022011-03-23 10:37:00 +0100282 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200283 * Clear @mask from @task->jobctl. @mask must be subset of
284 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
285 * STOP bits are cleared together.
Tejun Heoe5c19022011-03-23 10:37:00 +0100286 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200287 * If clearing of @mask leaves no stop or trap pending, this function calls
288 * task_clear_jobctl_trapping().
Tejun Heoe5c19022011-03-23 10:37:00 +0100289 *
290 * CONTEXT:
291 * Must be called with @task->sighand->siglock held.
292 */
Tejun Heo3759a0d2011-06-02 11:14:00 +0200293void task_clear_jobctl_pending(struct task_struct *task, unsigned int mask)
Tejun Heoe5c19022011-03-23 10:37:00 +0100294{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200295 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
296
297 if (mask & JOBCTL_STOP_PENDING)
298 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
299
300 task->jobctl &= ~mask;
Tejun Heo6dfca322011-06-02 11:14:00 +0200301
302 if (!(task->jobctl & JOBCTL_PENDING_MASK))
303 task_clear_jobctl_trapping(task);
Tejun Heoe5c19022011-03-23 10:37:00 +0100304}
305
306/**
307 * task_participate_group_stop - participate in a group stop
308 * @task: task participating in a group stop
309 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200310 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100311 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200312 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100313 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100314 *
315 * CONTEXT:
316 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100317 *
318 * RETURNS:
319 * %true if group stop completion should be notified to the parent, %false
320 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100321 */
322static bool task_participate_group_stop(struct task_struct *task)
323{
324 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200325 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c19022011-03-23 10:37:00 +0100326
Tejun Heoa8f072c2011-06-02 11:13:59 +0200327 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100328
Tejun Heo3759a0d2011-06-02 11:14:00 +0200329 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c19022011-03-23 10:37:00 +0100330
331 if (!consume)
332 return false;
333
334 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
335 sig->group_stop_count--;
336
Tejun Heo244056f2011-03-23 10:37:01 +0100337 /*
338 * Tell the caller to notify completion iff we are entering into a
339 * fresh group stop. Read comment in do_signal_stop() for details.
340 */
341 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Tejun Heoe5c19022011-03-23 10:37:00 +0100342 sig->flags = SIGNAL_STOP_STOPPED;
343 return true;
344 }
345 return false;
346}
347
David Howellsc69e8d92008-11-14 10:39:19 +1100348/*
349 * allocate a new signal queue record
350 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700351 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100352 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900353static struct sigqueue *
354__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800357 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800359 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000360 * Protect access to @t credentials. This can go away when all
361 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800362 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000363 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100364 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800365 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000366 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800369 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800370 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900372 } else {
373 print_dropped_signal(sig);
374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800377 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100378 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else {
380 INIT_LIST_HEAD(&q->list);
381 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100382 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
David Howellsd84f4f92008-11-14 10:39:23 +1100384
385 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Andrew Morton514a01b2006-02-03 03:04:41 -0800388static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 if (q->flags & SIGQUEUE_PREALLOC)
391 return;
392 atomic_dec(&q->user->sigpending);
393 free_uid(q->user);
394 kmem_cache_free(sigqueue_cachep, q);
395}
396
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800397void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct sigqueue *q;
400
401 sigemptyset(&queue->signal);
402 while (!list_empty(&queue->list)) {
403 q = list_entry(queue->list.next, struct sigqueue , list);
404 list_del_init(&q->list);
405 __sigqueue_free(q);
406 }
407}
408
409/*
410 * Flush all pending signals for a task.
411 */
David Howells3bcac022009-04-29 13:45:05 +0100412void __flush_signals(struct task_struct *t)
413{
414 clear_tsk_thread_flag(t, TIF_SIGPENDING);
415 flush_sigqueue(&t->pending);
416 flush_sigqueue(&t->signal->shared_pending);
417}
418
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800419void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 unsigned long flags;
422
423 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100424 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_unlock_irqrestore(&t->sighand->siglock, flags);
426}
427
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400428static void __flush_itimer_signals(struct sigpending *pending)
429{
430 sigset_t signal, retain;
431 struct sigqueue *q, *n;
432
433 signal = pending->signal;
434 sigemptyset(&retain);
435
436 list_for_each_entry_safe(q, n, &pending->list, list) {
437 int sig = q->info.si_signo;
438
439 if (likely(q->info.si_code != SI_TIMER)) {
440 sigaddset(&retain, sig);
441 } else {
442 sigdelset(&signal, sig);
443 list_del_init(&q->list);
444 __sigqueue_free(q);
445 }
446 }
447
448 sigorsets(&pending->signal, &signal, &retain);
449}
450
451void flush_itimer_signals(void)
452{
453 struct task_struct *tsk = current;
454 unsigned long flags;
455
456 spin_lock_irqsave(&tsk->sighand->siglock, flags);
457 __flush_itimer_signals(&tsk->pending);
458 __flush_itimer_signals(&tsk->signal->shared_pending);
459 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
460}
461
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700462void ignore_signals(struct task_struct *t)
463{
464 int i;
465
466 for (i = 0; i < _NSIG; ++i)
467 t->sighand->action[i].sa.sa_handler = SIG_IGN;
468
469 flush_signals(t);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * Flush all handlers for a task.
474 */
475
476void
477flush_signal_handlers(struct task_struct *t, int force_default)
478{
479 int i;
480 struct k_sigaction *ka = &t->sighand->action[0];
481 for (i = _NSIG ; i != 0 ; i--) {
482 if (force_default || ka->sa.sa_handler != SIG_IGN)
483 ka->sa.sa_handler = SIG_DFL;
484 ka->sa.sa_flags = 0;
Kees Cookd8ed9cf2013-03-13 14:59:33 -0700485#ifdef SA_RESTORER
486 ka->sa.sa_restorer = NULL;
487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 sigemptyset(&ka->sa.sa_mask);
489 ka++;
490 }
491}
492
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200493int unhandled_signal(struct task_struct *tsk, int sig)
494{
Roland McGrath445a91d2008-07-25 19:45:52 -0700495 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700496 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200497 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700498 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200499 return 0;
Tejun Heoa288eec2011-06-17 16:50:37 +0200500 /* if ptraced, let the tracer determine */
501 return !tsk->ptrace;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200502}
503
Randy Dunlap5aba0852011-04-04 14:59:31 -0700504/*
505 * Notify the system that a driver wants to block all signals for this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * process, and wants to be notified if any signals at all were to be
507 * sent/acted upon. If the notifier routine returns non-zero, then the
508 * signal will be acted upon after all. If the notifier routine returns 0,
509 * then then signal will be blocked. Only one block per process is
510 * allowed. priv is a pointer to private data that the notifier routine
Randy Dunlap5aba0852011-04-04 14:59:31 -0700511 * can use to determine if the signal should be blocked or not.
512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513void
514block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
515{
516 unsigned long flags;
517
518 spin_lock_irqsave(&current->sighand->siglock, flags);
519 current->notifier_mask = mask;
520 current->notifier_data = priv;
521 current->notifier = notifier;
522 spin_unlock_irqrestore(&current->sighand->siglock, flags);
523}
524
525/* Notify the system that blocking has ended. */
526
527void
528unblock_all_signals(void)
529{
530 unsigned long flags;
531
532 spin_lock_irqsave(&current->sighand->siglock, flags);
533 current->notifier = NULL;
534 current->notifier_data = NULL;
535 recalc_sigpending();
536 spin_unlock_irqrestore(&current->sighand->siglock, flags);
537}
538
Oleg Nesterov100360f2008-07-25 01:47:29 -0700539static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Collect the siginfo appropriate to this signal. Check if
545 * there is another siginfo for the same signal.
546 */
547 list_for_each_entry(q, &list->list, list) {
548 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700549 if (first)
550 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 first = q;
552 }
553 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700554
555 sigdelset(&list->signal, sig);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700558still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 list_del_init(&first->list);
560 copy_siginfo(info, &first->info);
561 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700563 /*
564 * Ok, it wasn't in the queue. This must be
565 * a fast-pathed signal or we must have been
566 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 info->si_signo = sig;
569 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800570 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 info->si_pid = 0;
572 info->si_uid = 0;
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
577 siginfo_t *info)
578{
Roland McGrath27d91e02006-09-29 02:00:31 -0700579 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (sig) {
582 if (current->notifier) {
583 if (sigismember(current->notifier_mask, sig)) {
584 if (!(current->notifier)(current->notifier_data)) {
585 clear_thread_flag(TIF_SIGPENDING);
586 return 0;
587 }
588 }
589 }
590
Oleg Nesterov100360f2008-07-25 01:47:29 -0700591 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return sig;
595}
596
597/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700598 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * expected to free it.
600 *
601 * All callers have to hold the siglock.
602 */
603int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
604{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700605 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000606
607 /* We only dequeue private signals from ourselves, we don't let
608 * signalfd steal them
609 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700610 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800611 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 signr = __dequeue_signal(&tsk->signal->shared_pending,
613 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800614 /*
615 * itimer signal ?
616 *
617 * itimers are process shared and we restart periodic
618 * itimers in the signal delivery path to prevent DoS
619 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700620 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800621 * itimers, as the SIGALRM is a legacy signal and only
622 * queued once. Changing the restart behaviour to
623 * restart the timer in the signal dequeue path is
624 * reducing the timer noise on heavy loaded !highres
625 * systems too.
626 */
627 if (unlikely(signr == SIGALRM)) {
628 struct hrtimer *tmr = &tsk->signal->real_timer;
629
630 if (!hrtimer_is_queued(tmr) &&
631 tsk->signal->it_real_incr.tv64 != 0) {
632 hrtimer_forward(tmr, tmr->base->get_time(),
633 tsk->signal->it_real_incr);
634 hrtimer_restart(tmr);
635 }
636 }
637 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700638
Davide Libenzib8fceee2007-09-20 12:40:16 -0700639 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700640 if (!signr)
641 return 0;
642
643 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800644 /*
645 * Set a marker that we have dequeued a stop signal. Our
646 * caller might release the siglock and then the pending
647 * stop signal it is about to process is no longer in the
648 * pending bitmasks, but must still be cleared by a SIGCONT
649 * (and overruled by a SIGKILL). So those cases clear this
650 * shared flag after we've set it. Note that this flag may
651 * remain set after the signal we return is ignored or
652 * handled. That doesn't matter because its only purpose
653 * is to alert stop-signal processing code when another
654 * processor has come along and cleared the flag.
655 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200656 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800657 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700658 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Release the siglock to ensure proper locking order
661 * of timer locks outside of siglocks. Note, we leave
662 * irqs disabled here, since the posix-timers code is
663 * about to disable them again anyway.
664 */
665 spin_unlock(&tsk->sighand->siglock);
666 do_schedule_next_timer(info);
667 spin_lock(&tsk->sighand->siglock);
668 }
669 return signr;
670}
671
672/*
673 * Tell a process that it has a new active signal..
674 *
675 * NOTE! we rely on the previous spin_lock to
676 * lock interrupts for us! We can only be called with
677 * "siglock" held, and the local interrupt must
678 * have been disabled when that got acquired!
679 *
680 * No need to set need_resched since signal event passing
681 * goes through ->blocked
682 */
683void signal_wake_up(struct task_struct *t, int resume)
684{
685 unsigned int mask;
686
687 set_tsk_thread_flag(t, TIF_SIGPENDING);
688
689 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500690 * For SIGKILL, we want to wake it up in the stopped/traced/killable
691 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * executing another processor and just now entering stopped state.
693 * By using wake_up_state, we ensure the process will wake up and
694 * handle its death signal.
695 */
696 mask = TASK_INTERRUPTIBLE;
697 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500698 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!wake_up_state(t, mask))
700 kick_process(t);
701}
702
703/*
704 * Remove signals in mask from the pending set and queue.
705 * Returns 1 if any signals were found.
706 *
707 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800708 *
709 * This version takes a sigset mask and looks at all signals,
710 * not just those in the first mask word.
711 */
712static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
713{
714 struct sigqueue *q, *n;
715 sigset_t m;
716
717 sigandsets(&m, mask, &s->signal);
718 if (sigisemptyset(&m))
719 return 0;
720
Oleg Nesterov702a5072011-04-27 22:01:27 +0200721 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800722 list_for_each_entry_safe(q, n, &s->list, list) {
723 if (sigismember(mask, q->info.si_signo)) {
724 list_del_init(&q->list);
725 __sigqueue_free(q);
726 }
727 }
728 return 1;
729}
730/*
731 * Remove signals in mask from the pending set and queue.
732 * Returns 1 if any signals were found.
733 *
734 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
736static int rm_from_queue(unsigned long mask, struct sigpending *s)
737{
738 struct sigqueue *q, *n;
739
740 if (!sigtestsetmask(&s->signal, mask))
741 return 0;
742
743 sigdelsetmask(&s->signal, mask);
744 list_for_each_entry_safe(q, n, &s->list, list) {
745 if (q->info.si_signo < SIGRTMIN &&
746 (mask & sigmask(q->info.si_signo))) {
747 list_del_init(&q->list);
748 __sigqueue_free(q);
749 }
750 }
751 return 1;
752}
753
Oleg Nesterov614c5172009-12-15 16:47:22 -0800754static inline int is_si_special(const struct siginfo *info)
755{
756 return info <= SEND_SIG_FORCED;
757}
758
759static inline bool si_fromuser(const struct siginfo *info)
760{
761 return info == SEND_SIG_NOINFO ||
762 (!is_si_special(info) && SI_FROMUSER(info));
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700766 * called with RCU read lock from check_kill_permission()
767 */
768static int kill_ok_by_cred(struct task_struct *t)
769{
770 const struct cred *cred = current_cred();
771 const struct cred *tcred = __task_cred(t);
772
773 if (cred->user->user_ns == tcred->user->user_ns &&
774 (cred->euid == tcred->suid ||
775 cred->euid == tcred->uid ||
776 cred->uid == tcred->suid ||
777 cred->uid == tcred->uid))
778 return 1;
779
780 if (ns_capable(tcred->user->user_ns, CAP_KILL))
781 return 1;
782
783 return 0;
784}
785
786/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100788 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
790static int check_kill_permission(int sig, struct siginfo *info,
791 struct task_struct *t)
792{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700793 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700794 int error;
795
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700796 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700797 return -EINVAL;
798
Oleg Nesterov614c5172009-12-15 16:47:22 -0800799 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700800 return 0;
801
802 error = audit_signal_info(sig, t); /* Let audit system see the signal */
803 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400805
Oleg Nesterov065add32010-05-26 14:42:54 -0700806 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700807 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700808 switch (sig) {
809 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700810 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700811 /*
812 * We don't return the error if sid == NULL. The
813 * task was unhashed, the caller must notice this.
814 */
815 if (!sid || sid == task_session(current))
816 break;
817 default:
818 return -EPERM;
819 }
820 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100821
Amy Griffise54dc242007-03-29 18:01:04 -0400822 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Tejun Heofb1d9102011-06-14 11:20:17 +0200825/**
826 * ptrace_trap_notify - schedule trap to notify ptracer
827 * @t: tracee wanting to notify tracer
828 *
829 * This function schedules sticky ptrace trap which is cleared on the next
830 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
831 * ptracer.
832 *
Tejun Heo544b2c92011-06-14 11:20:18 +0200833 * If @t is running, STOP trap will be taken. If trapped for STOP and
834 * ptracer is listening for events, tracee is woken up so that it can
835 * re-trap for the new event. If trapped otherwise, STOP trap will be
836 * eventually taken without returning to userland after the existing traps
837 * are finished by PTRACE_CONT.
Tejun Heofb1d9102011-06-14 11:20:17 +0200838 *
839 * CONTEXT:
840 * Must be called with @task->sighand->siglock held.
841 */
842static void ptrace_trap_notify(struct task_struct *t)
843{
844 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
845 assert_spin_locked(&t->sighand->siglock);
846
847 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
Tejun Heo544b2c92011-06-14 11:20:18 +0200848 signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700852 * Handle magic process-wide effects of stop/continue signals. Unlike
853 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * time regardless of blocking, ignoring, or handling. This does the
855 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700856 * signals. The process stop is done as a signal action for SIG_DFL.
857 *
858 * Returns true if the signal should be actually delivered, otherwise
859 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Oleg Nesterovdef8cf72012-03-23 15:02:45 -0700861static int prepare_signal(int sig, struct task_struct *p, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Oleg Nesterovad16a462008-04-30 00:52:46 -0700863 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct task_struct *t;
865
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700866 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700868 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700870 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /*
872 * This is a stop signal. Remove SIGCONT from all queues.
873 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700874 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 t = p;
876 do {
877 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700878 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700880 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200882 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700884 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 t = p;
886 do {
Tejun Heo3759a0d2011-06-02 11:14:00 +0200887 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Tejun Heofb1d9102011-06-14 11:20:17 +0200889 if (likely(!(t->ptrace & PT_SEIZED)))
890 wake_up_state(t, __TASK_STOPPED);
891 else
892 ptrace_trap_notify(t);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700893 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700895 /*
896 * Notify the parent with CLD_CONTINUED if we were stopped.
897 *
898 * If we were in the middle of a group stop, we pretend it
899 * was already finished, and then continued. Since SIGCHLD
900 * doesn't queue we report only CLD_STOPPED, as if the next
901 * CLD_CONTINUED was dropped.
902 */
903 why = 0;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700904 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700905 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700906 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700907 why |= SIGNAL_CLD_STOPPED;
908
909 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700910 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700911 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700912 * will take ->siglock, notice SIGNAL_CLD_MASK, and
913 * notify its parent. See get_signal_to_deliver().
914 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700915 signal->flags = why | SIGNAL_STOP_CONTINUED;
916 signal->group_stop_count = 0;
917 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700920
Oleg Nesterovdef8cf72012-03-23 15:02:45 -0700921 return !sig_ignored(p, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700924/*
925 * Test if P wants to take SIG. After we've checked all threads with this,
926 * it's equivalent to finding no threads not blocking SIG. Any threads not
927 * blocking SIG were ruled out because they are not running and already
928 * have pending signals. Such threads will dequeue from the shared queue
929 * as soon as they're available, so putting the signal on the shared queue
930 * will be equivalent to sending it to one such thread.
931 */
932static inline int wants_signal(int sig, struct task_struct *p)
933{
934 if (sigismember(&p->blocked, sig))
935 return 0;
936 if (p->flags & PF_EXITING)
937 return 0;
938 if (sig == SIGKILL)
939 return 1;
940 if (task_is_stopped_or_traced(p))
941 return 0;
942 return task_curr(p) || !signal_pending(p);
943}
944
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700945static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700946{
947 struct signal_struct *signal = p->signal;
948 struct task_struct *t;
949
950 /*
951 * Now find a thread we can wake up to take the signal off the queue.
952 *
953 * If the main thread wants the signal, it gets first crack.
954 * Probably the least surprising to the average bear.
955 */
956 if (wants_signal(sig, p))
957 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700958 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700959 /*
960 * There is just one thread and it does not need to be woken.
961 * It will dequeue unblocked signals before it runs again.
962 */
963 return;
964 else {
965 /*
966 * Otherwise try to find a suitable thread.
967 */
968 t = signal->curr_target;
969 while (!wants_signal(sig, t)) {
970 t = next_thread(t);
971 if (t == signal->curr_target)
972 /*
973 * No thread needs to be woken.
974 * Any eligible threads will see
975 * the signal in the queue soon.
976 */
977 return;
978 }
979 signal->curr_target = t;
980 }
981
982 /*
983 * Found a killable thread. If the signal will be fatal,
984 * then start taking the whole group down immediately.
985 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700986 if (sig_fatal(p, sig) &&
987 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700988 !sigismember(&t->real_blocked, sig) &&
Tejun Heoa288eec2011-06-17 16:50:37 +0200989 (sig == SIGKILL || !t->ptrace)) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700990 /*
991 * This signal will be fatal to the whole group.
992 */
993 if (!sig_kernel_coredump(sig)) {
994 /*
995 * Start a group exit and wake everybody up.
996 * This way we don't have other threads
997 * running and doing things after a slower
998 * thread has the fatal signal pending.
999 */
1000 signal->flags = SIGNAL_GROUP_EXIT;
1001 signal->group_exit_code = sig;
1002 signal->group_stop_count = 0;
1003 t = p;
1004 do {
Tejun Heo6dfca322011-06-02 11:14:00 +02001005 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001006 sigaddset(&t->pending.signal, SIGKILL);
1007 signal_wake_up(t, 1);
1008 } while_each_thread(p, t);
1009 return;
1010 }
1011 }
1012
1013 /*
1014 * The signal is already in the shared-pending queue.
1015 * Tell the chosen thread to wake up and dequeue it.
1016 */
1017 signal_wake_up(t, sig == SIGKILL);
1018 return;
1019}
1020
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -07001021static inline int legacy_queue(struct sigpending *signals, int sig)
1022{
1023 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1024}
1025
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001026/*
1027 * map the uid in struct cred into user namespace *ns
1028 */
1029static inline uid_t map_cred_ns(const struct cred *cred,
1030 struct user_namespace *ns)
1031{
1032 return user_ns_map_uid(ns, cred, cred->uid);
1033}
1034
1035#ifdef CONFIG_USER_NS
1036static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1037{
1038 if (current_user_ns() == task_cred_xxx(t, user_ns))
1039 return;
1040
1041 if (SI_FROMKERNEL(info))
1042 return;
1043
1044 info->si_uid = user_ns_map_uid(task_cred_xxx(t, user_ns),
1045 current_cred(), info->si_uid);
1046}
1047#else
1048static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1049{
1050 return;
1051}
1052#endif
1053
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001054static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1055 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001057 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001058 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001059 int override_rlimit;
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001060 int ret = 0, result;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001061
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001062 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001063
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001064 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterov629d3622012-03-23 15:02:44 -07001065 if (!prepare_signal(sig, t,
1066 from_ancestor_ns || (info == SEND_SIG_FORCED)))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001067 goto ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001068
1069 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001071 * Short-circuit ignored signals and support queuing
1072 * exactly one non-rt signal, so that we can get more
1073 * detailed information about the cause of the signal.
1074 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001075 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001076 if (legacy_queue(pending, sig))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001077 goto ret;
1078
1079 result = TRACE_SIGNAL_DELIVERED;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001080 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * fast-pathed signals for kernel-internal things like SIGSTOP
1082 * or SIGKILL.
1083 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001084 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto out_set;
1086
Randy Dunlap5aba0852011-04-04 14:59:31 -07001087 /*
1088 * Real-time signals must be queued if sent by sigqueue, or
1089 * some other real-time mechanism. It is implementation
1090 * defined whether kill() does so. We attempt to do so, on
1091 * the principle of least surprise, but since kill is not
1092 * allowed to fail with EAGAIN when low on memory we just
1093 * make sure at least one signal gets delivered and don't
1094 * pass on the info struct.
1095 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001096 if (sig < SIGRTMIN)
1097 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1098 else
1099 override_rlimit = 0;
1100
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001101 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001102 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001104 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001106 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 q->info.si_signo = sig;
1108 q->info.si_errno = 0;
1109 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001110 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001111 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +11001112 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001114 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 q->info.si_signo = sig;
1116 q->info.si_errno = 0;
1117 q->info.si_code = SI_KERNEL;
1118 q->info.si_pid = 0;
1119 q->info.si_uid = 0;
1120 break;
1121 default:
1122 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001123 if (from_ancestor_ns)
1124 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 break;
1126 }
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001127
1128 userns_fixup_signal_uid(&q->info, t);
1129
Oleg Nesterov621d3122005-10-30 15:03:45 -08001130 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001131 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1132 /*
1133 * Queue overflow, abort. We may abort if the
1134 * signal was rt and sent by user using something
1135 * other than kill().
1136 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001137 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1138 ret = -EAGAIN;
1139 goto ret;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001140 } else {
1141 /*
1142 * This is a silent loss of information. We still
1143 * send the signal, but the *info bits are lost.
1144 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001145 result = TRACE_SIGNAL_LOSE_INFO;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
1149out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001150 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001151 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001152 complete_signal(sig, t, group);
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001153ret:
1154 trace_signal_generate(sig, info, t, group, result);
1155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001158static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1159 int group)
1160{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001161 int from_ancestor_ns = 0;
1162
1163#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001164 from_ancestor_ns = si_fromuser(info) &&
1165 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001166#endif
1167
1168 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001169}
1170
Ingo Molnar45807a12007-07-15 23:40:10 -07001171static void print_fatal_signal(struct pt_regs *regs, int signr)
1172{
1173 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001174 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001175
Al Viroca5cd872007-10-29 04:31:16 +00001176#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001177 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001178 {
1179 int i;
1180 for (i = 0; i < 16; i++) {
1181 unsigned char insn;
1182
Andi Kleenb45c6e72010-01-08 14:42:52 -08001183 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1184 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001185 printk("%02x ", insn);
1186 }
1187 }
1188#endif
1189 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001190 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001191 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001192 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001193}
1194
1195static int __init setup_print_fatal_signals(char *str)
1196{
1197 get_option (&str, &print_fatal_signals);
1198
1199 return 1;
1200}
1201
1202__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001204int
1205__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1206{
1207 return send_signal(sig, info, p, 1);
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static int
1211specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1212{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001213 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001216int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1217 bool group)
1218{
1219 unsigned long flags;
1220 int ret = -ESRCH;
1221
1222 if (lock_task_sighand(p, &flags)) {
1223 ret = send_signal(sig, info, p, group);
1224 unlock_task_sighand(p, &flags);
1225 }
1226
1227 return ret;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * Force a signal that the process can't ignore: if necessary
1232 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001233 *
1234 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1235 * since we do not want to have a signal handler that was blocked
1236 * be invoked when user space had explicitly blocked it.
1237 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001238 * We don't want to have recursive SIGSEGV's etc, for example,
1239 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241int
1242force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1243{
1244 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001245 int ret, blocked, ignored;
1246 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001249 action = &t->sighand->action[sig-1];
1250 ignored = action->sa.sa_handler == SIG_IGN;
1251 blocked = sigismember(&t->blocked, sig);
1252 if (blocked || ignored) {
1253 action->sa.sa_handler = SIG_DFL;
1254 if (blocked) {
1255 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001256 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001259 if (action->sa.sa_handler == SIG_DFL)
1260 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 ret = specific_send_sig_info(sig, info, t);
1262 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1263
1264 return ret;
1265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/*
1268 * Nuke all other threads in the group.
1269 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001270int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001272 struct task_struct *t = p;
1273 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 p->signal->group_stop_count = 0;
1276
Oleg Nesterov09faef12010-05-26 14:43:11 -07001277 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001278 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001279 count++;
1280
1281 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (t->exit_state)
1283 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 signal_wake_up(t, 1);
1286 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001287
1288 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001291struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1292 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001293{
1294 struct sighand_struct *sighand;
1295
1296 for (;;) {
Paul E. McKenneya8417962011-07-19 03:25:36 -07001297 local_irq_save(*flags);
1298 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001299 sighand = rcu_dereference(tsk->sighand);
Paul E. McKenneya8417962011-07-19 03:25:36 -07001300 if (unlikely(sighand == NULL)) {
1301 rcu_read_unlock();
1302 local_irq_restore(*flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001303 break;
Paul E. McKenneya8417962011-07-19 03:25:36 -07001304 }
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001305
Paul E. McKenneya8417962011-07-19 03:25:36 -07001306 spin_lock(&sighand->siglock);
1307 if (likely(sighand == tsk->sighand)) {
1308 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001309 break;
Paul E. McKenneya8417962011-07-19 03:25:36 -07001310 }
1311 spin_unlock(&sighand->siglock);
1312 rcu_read_unlock();
1313 local_irq_restore(*flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001314 }
1315
1316 return sighand;
1317}
1318
David Howellsc69e8d92008-11-14 10:39:19 +11001319/*
1320 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1323{
David Howells694f6902010-08-04 16:59:14 +01001324 int ret;
1325
1326 rcu_read_lock();
1327 ret = check_kill_permission(sig, info, p);
1328 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001330 if (!ret && sig)
1331 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 return ret;
1334}
1335
1336/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001337 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001339 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001341int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct task_struct *p = NULL;
1344 int retval, success;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 success = 0;
1347 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001348 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int err = group_send_sig_info(sig, info, p);
1350 success |= !err;
1351 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001352 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return success ? 0 : retval;
1354}
1355
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001356int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001358 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct task_struct *p;
1360
Ingo Molnare56d0902006-01-08 01:01:37 -08001361 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001362retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001363 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001364 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001366 if (unlikely(error == -ESRCH))
1367 /*
1368 * The task was unhashed in between, try again.
1369 * If it is dead, pid_task() will return NULL,
1370 * if we race with de_thread() it will find the
1371 * new leader.
1372 */
1373 goto retry;
1374 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001375 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return error;
1378}
1379
Randy Dunlap5aba0852011-04-04 14:59:31 -07001380int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001381{
1382 int error;
1383 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001384 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001385 rcu_read_unlock();
1386 return error;
1387}
1388
Serge Hallynd178bc32011-09-26 10:45:18 -05001389static int kill_as_cred_perm(const struct cred *cred,
1390 struct task_struct *target)
1391{
1392 const struct cred *pcred = __task_cred(target);
1393 if (cred->user_ns != pcred->user_ns)
1394 return 0;
1395 if (cred->euid != pcred->suid && cred->euid != pcred->uid &&
1396 cred->uid != pcred->suid && cred->uid != pcred->uid)
1397 return 0;
1398 return 1;
1399}
1400
Eric W. Biederman2425c082006-10-02 02:17:28 -07001401/* like kill_pid_info(), but doesn't use uid/euid of "current" */
Serge Hallynd178bc32011-09-26 10:45:18 -05001402int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1403 const struct cred *cred, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001404{
1405 int ret = -EINVAL;
1406 struct task_struct *p;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001407 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001408
1409 if (!valid_signal(sig))
1410 return ret;
1411
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001412 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001413 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001414 if (!p) {
1415 ret = -ESRCH;
1416 goto out_unlock;
1417 }
Serge Hallynd178bc32011-09-26 10:45:18 -05001418 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
Harald Welte46113832005-10-10 19:44:29 +02001419 ret = -EPERM;
1420 goto out_unlock;
1421 }
David Quigley8f95dc52006-06-30 01:55:47 -07001422 ret = security_task_kill(p, info, sig, secid);
1423 if (ret)
1424 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001425
1426 if (sig) {
1427 if (lock_task_sighand(p, &flags)) {
1428 ret = __send_signal(sig, info, p, 1, 0);
1429 unlock_task_sighand(p, &flags);
1430 } else
1431 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001432 }
1433out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001434 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001435 return ret;
1436}
Serge Hallynd178bc32011-09-26 10:45:18 -05001437EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439/*
1440 * kill_something_info() interprets pid in interesting ways just like kill(2).
1441 *
1442 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1443 * is probably wrong. Should make it like BSD or SYSV.
1444 */
1445
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001446static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001448 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001449
1450 if (pid > 0) {
1451 rcu_read_lock();
1452 ret = kill_pid_info(sig, info, find_vpid(pid));
1453 rcu_read_unlock();
1454 return ret;
1455 }
1456
1457 read_lock(&tasklist_lock);
1458 if (pid != -1) {
1459 ret = __kill_pgrp_info(sig, info,
1460 pid ? find_vpid(-pid) : task_pgrp(current));
1461 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int retval = 0, count = 0;
1463 struct task_struct * p;
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001466 if (task_pid_vnr(p) > 1 &&
1467 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int err = group_send_sig_info(sig, info, p);
1469 ++count;
1470 if (err != -EPERM)
1471 retval = err;
1472 }
1473 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001474 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001476 read_unlock(&tasklist_lock);
1477
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481/*
1482 * These are for backward compatibility with the rest of the kernel source.
1483 */
1484
Randy Dunlap5aba0852011-04-04 14:59:31 -07001485int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * Make sure legacy kernel users don't send in bad values
1489 * (normal paths check this in check_kill_permission).
1490 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001491 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return -EINVAL;
1493
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001494 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001497#define __si_special(priv) \
1498 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500int
1501send_sig(int sig, struct task_struct *p, int priv)
1502{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001503 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506void
1507force_sig(int sig, struct task_struct *p)
1508{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001509 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512/*
1513 * When things go south during signal handling, we
1514 * will force a SIGSEGV. And if the signal that caused
1515 * the problem was already a SIGSEGV, we'll want to
1516 * make sure we don't even try to deliver the signal..
1517 */
1518int
1519force_sigsegv(int sig, struct task_struct *p)
1520{
1521 if (sig == SIGSEGV) {
1522 unsigned long flags;
1523 spin_lock_irqsave(&p->sighand->siglock, flags);
1524 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1525 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1526 }
1527 force_sig(SIGSEGV, p);
1528 return 0;
1529}
1530
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001531int kill_pgrp(struct pid *pid, int sig, int priv)
1532{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001533 int ret;
1534
1535 read_lock(&tasklist_lock);
1536 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1537 read_unlock(&tasklist_lock);
1538
1539 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001540}
1541EXPORT_SYMBOL(kill_pgrp);
1542
1543int kill_pid(struct pid *pid, int sig, int priv)
1544{
1545 return kill_pid_info(sig, __si_special(priv), pid);
1546}
1547EXPORT_SYMBOL(kill_pid);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/*
1550 * These functions support sending signals using preallocated sigqueue
1551 * structures. This is needed "because realtime applications cannot
1552 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001553 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * we allocate the sigqueue structure from the timer_create. If this
1555 * allocation fails we are able to report the failure to the application
1556 * with an EAGAIN error.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558struct sigqueue *sigqueue_alloc(void)
1559{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001560 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001562 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001564
1565 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568void sigqueue_free(struct sigqueue *q)
1569{
1570 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001571 spinlock_t *lock = &current->sighand->siglock;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1574 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001575 * We must hold ->siglock while testing q->list
1576 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001577 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001579 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001580 q->flags &= ~SIGQUEUE_PREALLOC;
1581 /*
1582 * If it is queued it will be freed when dequeued,
1583 * like the "regular" sigqueue.
1584 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001585 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001586 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001587 spin_unlock_irqrestore(lock, flags);
1588
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001589 if (q)
1590 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001593int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001594{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001595 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001596 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001597 unsigned long flags;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001598 int ret, result;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001599
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001600 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001601
1602 ret = -1;
1603 if (!likely(lock_task_sighand(t, &flags)))
1604 goto ret;
1605
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001606 ret = 1; /* the signal is ignored */
Oleg Nesterov163566f2011-11-22 21:37:41 +01001607 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterovdef8cf72012-03-23 15:02:45 -07001608 if (!prepare_signal(sig, t, false))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001609 goto out;
1610
1611 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001612 if (unlikely(!list_empty(&q->list))) {
1613 /*
1614 * If an SI_TIMER entry is already queue just increment
1615 * the overrun count.
1616 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001617 BUG_ON(q->info.si_code != SI_TIMER);
1618 q->info.si_overrun++;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001619 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001620 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001621 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001622 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001623
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001624 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001625 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001626 list_add_tail(&q->list, &pending->list);
1627 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001628 complete_signal(sig, t, group);
Oleg Nesterov163566f2011-11-22 21:37:41 +01001629 result = TRACE_SIGNAL_DELIVERED;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001630out:
Oleg Nesterov163566f2011-11-22 21:37:41 +01001631 trace_signal_generate(sig, &q->info, t, group, result);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001632 unlock_task_sighand(t, &flags);
1633ret:
1634 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001635}
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * Let a parent know about the death of a child.
1639 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001640 *
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001641 * Returns true if our parent ignored us and so we've switched to
1642 * self-reaping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001644bool do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct siginfo info;
1647 unsigned long flags;
1648 struct sighand_struct *psig;
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001649 bool autoreap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 BUG_ON(sig == -1);
1652
1653 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001654 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Tejun Heod21142e2011-06-17 16:50:34 +02001656 BUG_ON(!tsk->ptrace &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1658
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01001659 if (sig != SIGCHLD) {
1660 /*
1661 * This is only possible if parent == real_parent.
1662 * Check if it has changed security domain.
1663 */
1664 if (tsk->parent_exec_id != tsk->parent->self_exec_id)
1665 sig = SIGCHLD;
1666 }
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 info.si_signo = sig;
1669 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001670 /*
1671 * we are under tasklist_lock here so our parent is tied to
1672 * us and cannot exit and release its namespace.
1673 *
1674 * the only it can is to switch its nsproxy with sys_unshare,
1675 * bu uncharing pid namespaces is not allowed, so we'll always
1676 * see relevant namespace
1677 *
1678 * write_lock() currently calls preempt_disable() which is the
1679 * same as rcu_read_lock(), but according to Oleg, this is not
1680 * correct to rely on this
1681 */
1682 rcu_read_lock();
1683 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001684 info.si_uid = map_cred_ns(__task_cred(tsk),
1685 task_cred_xxx(tsk->parent, user_ns));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001686 rcu_read_unlock();
1687
Martin Schwidefsky64861632011-12-15 14:56:09 +01001688 info.si_utime = cputime_to_clock_t(tsk->utime + tsk->signal->utime);
1689 info.si_stime = cputime_to_clock_t(tsk->stime + tsk->signal->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 info.si_status = tsk->exit_code & 0x7f;
1692 if (tsk->exit_code & 0x80)
1693 info.si_code = CLD_DUMPED;
1694 else if (tsk->exit_code & 0x7f)
1695 info.si_code = CLD_KILLED;
1696 else {
1697 info.si_code = CLD_EXITED;
1698 info.si_status = tsk->exit_code >> 8;
1699 }
1700
1701 psig = tsk->parent->sighand;
1702 spin_lock_irqsave(&psig->siglock, flags);
Tejun Heod21142e2011-06-17 16:50:34 +02001703 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1705 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1706 /*
1707 * We are exiting and our parent doesn't care. POSIX.1
1708 * defines special semantics for setting SIGCHLD to SIG_IGN
1709 * or setting the SA_NOCLDWAIT flag: we should be reaped
1710 * automatically and not left for our parent's wait4 call.
1711 * Rather than having the parent do it as a magic kind of
1712 * signal handler, we just set this to tell do_exit that we
1713 * can be cleaned up without becoming a zombie. Note that
1714 * we still call __wake_up_parent in this case, because a
1715 * blocked sys_wait4 might now return -ECHILD.
1716 *
1717 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1718 * is implementation-defined: we do (if you don't want
1719 * it, just use SIG_IGN instead).
1720 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001721 autoreap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001723 sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001725 if (valid_signal(sig) && sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 __group_send_sig_info(sig, &info, tsk->parent);
1727 __wake_up_parent(tsk, tsk->parent);
1728 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001729
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001730 return autoreap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Tejun Heo75b95952011-03-23 10:37:01 +01001733/**
1734 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1735 * @tsk: task reporting the state change
1736 * @for_ptracer: the notification is for ptracer
1737 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1738 *
1739 * Notify @tsk's parent that the stopped/continued state has changed. If
1740 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1741 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1742 *
1743 * CONTEXT:
1744 * Must be called with tasklist_lock at least read locked.
1745 */
1746static void do_notify_parent_cldstop(struct task_struct *tsk,
1747 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 struct siginfo info;
1750 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001751 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct sighand_struct *sighand;
1753
Tejun Heo75b95952011-03-23 10:37:01 +01001754 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001755 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001756 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001757 tsk = tsk->group_leader;
1758 parent = tsk->real_parent;
1759 }
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 info.si_signo = SIGCHLD;
1762 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001763 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001764 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001765 */
1766 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001767 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001768 info.si_uid = map_cred_ns(__task_cred(tsk),
1769 task_cred_xxx(parent, user_ns));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001770 rcu_read_unlock();
1771
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001772 info.si_utime = cputime_to_clock_t(tsk->utime);
1773 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 info.si_code = why;
1776 switch (why) {
1777 case CLD_CONTINUED:
1778 info.si_status = SIGCONT;
1779 break;
1780 case CLD_STOPPED:
1781 info.si_status = tsk->signal->group_exit_code & 0x7f;
1782 break;
1783 case CLD_TRAPPED:
1784 info.si_status = tsk->exit_code & 0x7f;
1785 break;
1786 default:
1787 BUG();
1788 }
1789
1790 sighand = parent->sighand;
1791 spin_lock_irqsave(&sighand->siglock, flags);
1792 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1793 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1794 __group_send_sig_info(SIGCHLD, &info, parent);
1795 /*
1796 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1797 */
1798 __wake_up_parent(tsk, parent);
1799 spin_unlock_irqrestore(&sighand->siglock, flags);
1800}
1801
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001802static inline int may_ptrace_stop(void)
1803{
Tejun Heod21142e2011-06-17 16:50:34 +02001804 if (!likely(current->ptrace))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001805 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001806 /*
1807 * Are we in the middle of do_coredump?
1808 * If so and our tracer is also part of the coredump stopping
1809 * is a deadlock situation, and pointless because our tracer
1810 * is dead so don't allow us to stop.
1811 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001812 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001813 * is safe to enter schedule().
1814 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001815 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001816 unlikely(current->mm == current->parent->mm))
1817 return 0;
1818
1819 return 1;
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001823 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001824 * Called with the siglock held.
1825 */
1826static int sigkill_pending(struct task_struct *tsk)
1827{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001828 return sigismember(&tsk->pending.signal, SIGKILL) ||
1829 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001830}
1831
1832/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * This must be called with current->sighand->siglock held.
1834 *
1835 * This should be the path for all ptrace stops.
1836 * We always set current->last_siginfo while stopped here.
1837 * That makes it a way to test a stopped process for
1838 * being ptrace-stopped vs being job-control-stopped.
1839 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001840 * If we actually decide not to stop at all because the tracer
1841 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001843static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001844 __releases(&current->sighand->siglock)
1845 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001847 bool gstop_done = false;
1848
Roland McGrath1a669c22008-02-06 01:37:37 -08001849 if (arch_ptrace_stop_needed(exit_code, info)) {
1850 /*
1851 * The arch code has something special to do before a
1852 * ptrace stop. This is allowed to block, e.g. for faults
1853 * on user stack pages. We can't keep the siglock while
1854 * calling arch_ptrace_stop, so we must release it now.
1855 * To preserve proper semantics, we must do this before
1856 * any signal bookkeeping like checking group_stop_count.
1857 * Meanwhile, a SIGKILL could come in before we retake the
1858 * siglock. That must prevent us from sleeping in TASK_TRACED.
1859 * So after regaining the lock, we must check for SIGKILL.
1860 */
1861 spin_unlock_irq(&current->sighand->siglock);
1862 arch_ptrace_stop(exit_code, info);
1863 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001864 if (sigkill_pending(current))
1865 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001866 }
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02001869 * We're committing to trapping. TRACED should be visible before
1870 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1871 * Also, transition to TRACED and updates to ->jobctl should be
1872 * atomic with respect to siglock and should be done after the arch
1873 * hook as siglock is released and regrabbed across it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Tejun Heo81be24b2011-06-02 11:13:59 +02001875 set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 current->last_siginfo = info;
1878 current->exit_code = exit_code;
1879
Tejun Heod79fdd62011-03-23 10:37:00 +01001880 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * If @why is CLD_STOPPED, we're trapping to participate in a group
1882 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
Tejun Heo73ddff22011-06-14 11:20:14 +02001883 * across siglock relocks since INTERRUPT was scheduled, PENDING
1884 * could be clear now. We act as if SIGCONT is received after
1885 * TASK_TRACED is entered - ignore it.
Tejun Heod79fdd62011-03-23 10:37:00 +01001886 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001887 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 gstop_done = task_participate_group_stop(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001889
Tejun Heofb1d9102011-06-14 11:20:17 +02001890 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
Tejun Heo73ddff22011-06-14 11:20:14 +02001891 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
Tejun Heofb1d9102011-06-14 11:20:17 +02001892 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1893 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
Tejun Heo73ddff22011-06-14 11:20:14 +02001894
Tejun Heo81be24b2011-06-02 11:13:59 +02001895 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001896 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 spin_unlock_irq(&current->sighand->siglock);
1899 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001900 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001901 /*
1902 * Notify parents of the stop.
1903 *
1904 * While ptraced, there are two parents - the ptracer and
1905 * the real_parent of the group_leader. The ptracer should
1906 * know about every stop while the real parent is only
1907 * interested in the completion of group stop. The states
1908 * for the two don't interact with each other. Notify
1909 * separately unless they're gonna be duplicates.
1910 */
1911 do_notify_parent_cldstop(current, true, why);
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02001912 if (gstop_done && ptrace_reparented(current))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001913 do_notify_parent_cldstop(current, false, why);
1914
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001915 /*
1916 * Don't want to allow preemption here, because
1917 * sys_ptrace() needs this task to be inactive.
1918 *
1919 * XXX: implement read_unlock_no_resched().
1920 */
1921 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001923 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 schedule();
1925 } else {
1926 /*
1927 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001928 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001929 *
1930 * If @gstop_done, the ptracer went away between group stop
1931 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001932 * JOBCTL_STOP_PENDING on us and we'll re-enter
1933 * TASK_STOPPED in do_signal_stop() on return, so notifying
1934 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001936 if (gstop_done)
1937 do_notify_parent_cldstop(current, false, why);
1938
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001939 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001940 if (clear_code)
1941 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001942 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
1945 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001946 * While in TASK_TRACED, we were considered "frozen enough".
1947 * Now that we woke up, it's crucial if we're supposed to be
1948 * frozen that we freeze now before running anything substantial.
1949 */
1950 try_to_freeze();
1951
1952 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * We are back. Now reacquire the siglock before touching
1954 * last_siginfo, so that we are sure to have synchronized with
1955 * any signal-sending on another CPU that wants to examine it.
1956 */
1957 spin_lock_irq(&current->sighand->siglock);
1958 current->last_siginfo = NULL;
1959
Tejun Heo544b2c92011-06-14 11:20:18 +02001960 /* LISTENING can be set only during STOP traps, clear it */
1961 current->jobctl &= ~JOBCTL_LISTENING;
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /*
1964 * Queued signals ignored us while we were stopped for tracing.
1965 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001966 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001968 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Tejun Heo3544d722011-06-14 11:20:15 +02001971static void ptrace_do_notify(int signr, int exit_code, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 siginfo_t info;
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 memset(&info, 0, sizeof info);
Tejun Heo3544d722011-06-14 11:20:15 +02001976 info.si_signo = signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001978 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001979 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /* Let the debugger run. */
Tejun Heo3544d722011-06-14 11:20:15 +02001982 ptrace_stop(exit_code, why, 1, &info);
1983}
1984
1985void ptrace_notify(int exit_code)
1986{
1987 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_lock_irq(&current->sighand->siglock);
Tejun Heo3544d722011-06-14 11:20:15 +02001990 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 spin_unlock_irq(&current->sighand->siglock);
1992}
1993
Tejun Heo73ddff22011-06-14 11:20:14 +02001994/**
1995 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
1996 * @signr: signr causing group stop if initiating
1997 *
1998 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
1999 * and participate in it. If already set, participate in the existing
2000 * group stop. If participated in a group stop (and thus slept), %true is
2001 * returned with siglock released.
2002 *
2003 * If ptraced, this function doesn't handle stop itself. Instead,
2004 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2005 * untouched. The caller must ensure that INTERRUPT trap handling takes
2006 * places afterwards.
2007 *
2008 * CONTEXT:
2009 * Must be called with @current->sighand->siglock held, which is released
2010 * on %true return.
2011 *
2012 * RETURNS:
2013 * %false if group stop is already cancelled or ptrace trap is scheduled.
2014 * %true if participated in group stop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 */
Tejun Heo73ddff22011-06-14 11:20:14 +02002016static bool do_signal_stop(int signr)
2017 __releases(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Tejun Heoa8f072c2011-06-02 11:13:59 +02002021 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2022 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08002023 struct task_struct *t;
2024
Tejun Heoa8f072c2011-06-02 11:13:59 +02002025 /* signr will be recorded in task->jobctl for retries */
2026 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01002027
Tejun Heoa8f072c2011-06-02 11:13:59 +02002028 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07002029 unlikely(signal_group_exit(sig)))
Tejun Heo73ddff22011-06-14 11:20:14 +02002030 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01002032 * There is no group stop already in progress. We must
2033 * initiate one now.
2034 *
2035 * While ptraced, a task may be resumed while group stop is
2036 * still in effect and then receive a stop signal and
2037 * initiate another group stop. This deviates from the
2038 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02002039 * cause two group stops when !ptraced. That is why we
2040 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01002041 *
2042 * The condition can be distinguished by testing whether
2043 * SIGNAL_STOP_STOPPED is already set. Don't generate
2044 * group_exit_code in such case.
2045 *
2046 * This is not necessary for SIGNAL_STOP_CONTINUED because
2047 * an intervening stop signal is required to cause two
2048 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 */
Tejun Heo408a37d2011-03-23 10:37:01 +01002050 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2051 sig->group_exit_code = signr;
Oleg Nesterova122b342006-03-28 16:11:22 -08002052
Tejun Heo7dd3db52011-06-02 11:14:00 +02002053 sig->group_stop_count = 0;
2054
2055 if (task_set_jobctl_pending(current, signr | gstop))
2056 sig->group_stop_count++;
2057
Tejun Heod79fdd62011-03-23 10:37:00 +01002058 for (t = next_thread(current); t != current;
2059 t = next_thread(t)) {
Oleg Nesterova122b342006-03-28 16:11:22 -08002060 /*
2061 * Setting state to TASK_STOPPED for a group
2062 * stop is always done with the siglock held,
2063 * so this check has no races.
2064 */
Tejun Heo7dd3db52011-06-02 11:14:00 +02002065 if (!task_is_stopped(t) &&
2066 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002067 sig->group_stop_count++;
Tejun Heofb1d9102011-06-14 11:20:17 +02002068 if (likely(!(t->ptrace & PT_SEIZED)))
2069 signal_wake_up(t, 0);
2070 else
2071 ptrace_trap_notify(t);
Oleg Nesterova122b342006-03-28 16:11:22 -08002072 }
Tejun Heod79fdd62011-03-23 10:37:00 +01002073 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002074 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002075
Tejun Heod21142e2011-06-17 16:50:34 +02002076 if (likely(!current->ptrace)) {
Tejun Heo5224fa32011-03-23 10:37:00 +01002077 int notify = 0;
2078
2079 /*
2080 * If there are no other threads in the group, or if there
2081 * is a group stop in progress and we are the last to stop,
2082 * report to the parent.
2083 */
2084 if (task_participate_group_stop(current))
2085 notify = CLD_STOPPED;
2086
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002087 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01002088 spin_unlock_irq(&current->sighand->siglock);
2089
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002090 /*
2091 * Notify the parent of the group stop completion. Because
2092 * we're not holding either the siglock or tasklist_lock
2093 * here, ptracer may attach inbetween; however, this is for
2094 * group stop and should always be delivered to the real
2095 * parent of the group leader. The new ptracer will get
2096 * its notification when this task transitions into
2097 * TASK_TRACED.
2098 */
Tejun Heo5224fa32011-03-23 10:37:00 +01002099 if (notify) {
2100 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002101 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01002102 read_unlock(&tasklist_lock);
2103 }
2104
2105 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2106 schedule();
Tejun Heo73ddff22011-06-14 11:20:14 +02002107 return true;
Tejun Heod79fdd62011-03-23 10:37:00 +01002108 } else {
Tejun Heo73ddff22011-06-14 11:20:14 +02002109 /*
2110 * While ptraced, group stop is handled by STOP trap.
2111 * Schedule it and let the caller deal with it.
2112 */
2113 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2114 return false;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002115 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002116}
Tejun Heod79fdd62011-03-23 10:37:00 +01002117
Tejun Heo73ddff22011-06-14 11:20:14 +02002118/**
2119 * do_jobctl_trap - take care of ptrace jobctl traps
2120 *
Tejun Heo3544d722011-06-14 11:20:15 +02002121 * When PT_SEIZED, it's used for both group stop and explicit
2122 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2123 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2124 * the stop signal; otherwise, %SIGTRAP.
2125 *
2126 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2127 * number as exit_code and no siginfo.
Tejun Heo73ddff22011-06-14 11:20:14 +02002128 *
2129 * CONTEXT:
2130 * Must be called with @current->sighand->siglock held, which may be
2131 * released and re-acquired before returning with intervening sleep.
2132 */
2133static void do_jobctl_trap(void)
2134{
Tejun Heo3544d722011-06-14 11:20:15 +02002135 struct signal_struct *signal = current->signal;
Tejun Heo73ddff22011-06-14 11:20:14 +02002136 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
Tejun Heod79fdd62011-03-23 10:37:00 +01002137
Tejun Heo3544d722011-06-14 11:20:15 +02002138 if (current->ptrace & PT_SEIZED) {
2139 if (!signal->group_stop_count &&
2140 !(signal->flags & SIGNAL_STOP_STOPPED))
2141 signr = SIGTRAP;
2142 WARN_ON_ONCE(!signr);
2143 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2144 CLD_STOPPED);
2145 } else {
2146 WARN_ON_ONCE(!signr);
2147 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002148 current->exit_code = 0;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Roland McGrath18c98b62008-04-17 18:44:38 -07002152static int ptrace_signal(int signr, siginfo_t *info,
2153 struct pt_regs *regs, void *cookie)
2154{
Roland McGrath18c98b62008-04-17 18:44:38 -07002155 ptrace_signal_deliver(regs, cookie);
Oleg Nesterov8a352412011-07-21 17:06:53 +02002156 /*
2157 * We do not check sig_kernel_stop(signr) but set this marker
2158 * unconditionally because we do not know whether debugger will
2159 * change signr. This flag has no meaning unless we are going
2160 * to stop after return from ptrace_stop(). In this case it will
2161 * be checked in do_signal_stop(), we should only stop if it was
2162 * not cleared by SIGCONT while we were sleeping. See also the
2163 * comment in dequeue_signal().
2164 */
2165 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Tejun Heofe1bc6a2011-03-23 10:37:00 +01002166 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002167
2168 /* We're back. Did the debugger cancel the sig? */
2169 signr = current->exit_code;
2170 if (signr == 0)
2171 return signr;
2172
2173 current->exit_code = 0;
2174
Randy Dunlap5aba0852011-04-04 14:59:31 -07002175 /*
2176 * Update the siginfo structure if the signal has
2177 * changed. If the debugger wanted something
2178 * specific in the siginfo structure then it should
2179 * have updated *info via PTRACE_SETSIGINFO.
2180 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002181 if (signr != info->si_signo) {
2182 info->si_signo = signr;
2183 info->si_errno = 0;
2184 info->si_code = SI_USER;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002185 rcu_read_lock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002186 info->si_pid = task_pid_vnr(current->parent);
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002187 info->si_uid = map_cred_ns(__task_cred(current->parent),
2188 current_user_ns());
2189 rcu_read_unlock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002190 }
2191
2192 /* If the (new) signal is now blocked, requeue it. */
2193 if (sigismember(&current->blocked, signr)) {
2194 specific_send_sig_info(signr, info, current);
2195 signr = 0;
2196 }
2197
2198 return signr;
2199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2202 struct pt_regs *regs, void *cookie)
2203{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002204 struct sighand_struct *sighand = current->sighand;
2205 struct signal_struct *signal = current->signal;
2206 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002208relock:
2209 /*
2210 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2211 * While in TASK_STOPPED, we were considered "frozen enough".
2212 * Now that we woke up, it's crucial if we're supposed to be
2213 * frozen that we freeze now before running anything substantial.
2214 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002215 try_to_freeze();
2216
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002217 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002218 /*
2219 * Every stopped thread goes here after wakeup. Check to see if
2220 * we should notify the parent, prepare_signal(SIGCONT) encodes
2221 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2222 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002223 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heoc672af32011-03-23 10:36:59 +01002224 int why;
2225
2226 if (signal->flags & SIGNAL_CLD_CONTINUED)
2227 why = CLD_CONTINUED;
2228 else
2229 why = CLD_STOPPED;
2230
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002231 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002232
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002233 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002234
Tejun Heoceb6bd62011-03-23 10:37:01 +01002235 /*
2236 * Notify the parent that we're continuing. This event is
2237 * always per-process and doesn't make whole lot of sense
2238 * for ptracers, who shouldn't consume the state via
2239 * wait(2) either, but, for backward compatibility, notify
2240 * the ptracer of the group leader too unless it's gonna be
2241 * a duplicate.
2242 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002243 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002244 do_notify_parent_cldstop(current, false, why);
2245
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02002246 if (ptrace_reparented(current->group_leader))
2247 do_notify_parent_cldstop(current->group_leader,
2248 true, why);
Tejun Heoedf2ed12011-03-23 10:37:00 +01002249 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002250
Oleg Nesterove4420552008-04-30 00:52:44 -07002251 goto relock;
2252 }
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 for (;;) {
2255 struct k_sigaction *ka;
Tejun Heodd1d6772011-06-02 11:14:00 +02002256
2257 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2258 do_signal_stop(0))
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002259 goto relock;
Oleg Nesterov1be53962009-12-15 16:47:26 -08002260
Tejun Heo73ddff22011-06-14 11:20:14 +02002261 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2262 do_jobctl_trap();
2263 spin_unlock_irq(&sighand->siglock);
2264 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266
Tejun Heodd1d6772011-06-02 11:14:00 +02002267 signr = dequeue_signal(current, &current->blocked, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Tejun Heodd1d6772011-06-02 11:14:00 +02002269 if (!signr)
2270 break; /* will return 0 */
2271
Oleg Nesterov8a352412011-07-21 17:06:53 +02002272 if (unlikely(current->ptrace) && signr != SIGKILL) {
Tejun Heodd1d6772011-06-02 11:14:00 +02002273 signr = ptrace_signal(signr, info,
2274 regs, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (!signr)
Tejun Heodd1d6772011-06-02 11:14:00 +02002276 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278
Tejun Heodd1d6772011-06-02 11:14:00 +02002279 ka = &sighand->action[signr-1];
2280
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002281 /* Trace actually delivered signals. */
2282 trace_signal_deliver(signr, info, ka);
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2285 continue;
2286 if (ka->sa.sa_handler != SIG_DFL) {
2287 /* Run the handler. */
2288 *return_ka = *ka;
2289
2290 if (ka->sa.sa_flags & SA_ONESHOT)
2291 ka->sa.sa_handler = SIG_DFL;
2292
2293 break; /* will return non-zero "signr" value */
2294 }
2295
2296 /*
2297 * Now we are doing the default action for this signal.
2298 */
2299 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2300 continue;
2301
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002302 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002303 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002304 * Container-init gets no signals it doesn't want from same
2305 * container.
2306 *
2307 * Note that if global/container-init sees a sig_kernel_only()
2308 * signal here, the signal must have been generated internally
2309 * or must have come from an ancestor namespace. In either
2310 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002311 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002312 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002313 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 continue;
2315
2316 if (sig_kernel_stop(signr)) {
2317 /*
2318 * The default action is to stop all threads in
2319 * the thread group. The job control signals
2320 * do nothing in an orphaned pgrp, but SIGSTOP
2321 * always works. Note that siglock needs to be
2322 * dropped during the call to is_orphaned_pgrp()
2323 * because of lock ordering with tasklist_lock.
2324 * This allows an intervening SIGCONT to be posted.
2325 * We need to check for that and bail out if necessary.
2326 */
2327 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002328 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 /* signals can be posted during this window */
2331
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002332 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto relock;
2334
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002335 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002338 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* It released the siglock. */
2340 goto relock;
2341 }
2342
2343 /*
2344 * We didn't actually stop, due to a race
2345 * with SIGCONT or something like that.
2346 */
2347 continue;
2348 }
2349
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002350 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 /*
2353 * Anything else is fatal, maybe with a core dump.
2354 */
2355 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002358 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002359 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /*
2361 * If it was able to dump core, this kills all
2362 * other threads in the group and synchronizes with
2363 * their demise. If we lost the race with another
2364 * thread getting here, it set group_exit_code
2365 * first and our do_group_exit call below will use
2366 * that value and ignore the one we pass it.
2367 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002368 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370
2371 /*
2372 * Death signals, no core dump.
2373 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002374 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* NOTREACHED */
2376 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002377 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return signr;
2379}
2380
Matt Fleming5e6292c2012-01-10 15:11:17 -08002381/**
2382 * block_sigmask - add @ka's signal mask to current->blocked
2383 * @ka: action for @signr
2384 * @signr: signal that has been successfully delivered
2385 *
2386 * This function should be called when a signal has succesfully been
2387 * delivered. It adds the mask of signals for @ka to current->blocked
2388 * so that they are blocked during the execution of the signal
2389 * handler. In addition, @signr will be blocked unless %SA_NODEFER is
2390 * set in @ka->sa.sa_flags.
2391 */
2392void block_sigmask(struct k_sigaction *ka, int signr)
2393{
2394 sigset_t blocked;
2395
2396 sigorsets(&blocked, &current->blocked, &ka->sa.sa_mask);
2397 if (!(ka->sa.sa_flags & SA_NODEFER))
2398 sigaddset(&blocked, signr);
2399 set_current_blocked(&blocked);
2400}
2401
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002402/*
2403 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002404 * group-wide signal. Other threads should be notified now to take
2405 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002406 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002407static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002408{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002409 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002410 struct task_struct *t;
2411
Oleg Nesterovf646e222011-04-27 19:18:39 +02002412 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2413 if (sigisemptyset(&retarget))
2414 return;
2415
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002416 t = tsk;
2417 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002418 if (t->flags & PF_EXITING)
2419 continue;
2420
2421 if (!has_pending_signals(&retarget, &t->blocked))
2422 continue;
2423 /* Remove the signals this thread can handle. */
2424 sigandsets(&retarget, &retarget, &t->blocked);
2425
2426 if (!signal_pending(t))
2427 signal_wake_up(t, 0);
2428
2429 if (sigisemptyset(&retarget))
2430 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002431 }
2432}
2433
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002434void exit_signals(struct task_struct *tsk)
2435{
2436 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002437 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002438
Tejun Heo77e4ef92011-12-12 18:12:21 -08002439 /*
2440 * @tsk is about to have PF_EXITING set - lock out users which
2441 * expect stable threadgroup.
2442 */
2443 threadgroup_change_begin(tsk);
2444
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002445 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2446 tsk->flags |= PF_EXITING;
Tejun Heo77e4ef92011-12-12 18:12:21 -08002447 threadgroup_change_end(tsk);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002448 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002449 }
2450
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002451 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002452 /*
2453 * From now this task is not visible for group-wide signals,
2454 * see wants_signal(), do_signal_stop().
2455 */
2456 tsk->flags |= PF_EXITING;
Tejun Heo77e4ef92011-12-12 18:12:21 -08002457
2458 threadgroup_change_end(tsk);
2459
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002460 if (!signal_pending(tsk))
2461 goto out;
2462
Oleg Nesterovf646e222011-04-27 19:18:39 +02002463 unblocked = tsk->blocked;
2464 signotset(&unblocked);
2465 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002466
Tejun Heoa8f072c2011-06-02 11:13:59 +02002467 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002468 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002469 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002470out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002471 spin_unlock_irq(&tsk->sighand->siglock);
2472
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002473 /*
2474 * If group stop has completed, deliver the notification. This
2475 * should always go to the real parent of the group leader.
2476 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002477 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002478 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002479 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002480 read_unlock(&tasklist_lock);
2481 }
2482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484EXPORT_SYMBOL(recalc_sigpending);
2485EXPORT_SYMBOL_GPL(dequeue_signal);
2486EXPORT_SYMBOL(flush_signals);
2487EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488EXPORT_SYMBOL(send_sig);
2489EXPORT_SYMBOL(send_sig_info);
2490EXPORT_SYMBOL(sigprocmask);
2491EXPORT_SYMBOL(block_all_signals);
2492EXPORT_SYMBOL(unblock_all_signals);
2493
2494
2495/*
2496 * System call entry points.
2497 */
2498
Randy Dunlap41c57892011-04-04 15:00:26 -07002499/**
2500 * sys_restart_syscall - restart a system call
2501 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002502SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 struct restart_block *restart = &current_thread_info()->restart_block;
2505 return restart->fn(restart);
2506}
2507
2508long do_no_restart_syscall(struct restart_block *param)
2509{
2510 return -EINTR;
2511}
2512
Oleg Nesterovb1828012011-04-27 21:56:14 +02002513static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2514{
2515 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2516 sigset_t newblocked;
2517 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002518 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002519 retarget_shared_pending(tsk, &newblocked);
2520 }
2521 tsk->blocked = *newset;
2522 recalc_sigpending();
2523}
2524
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002525/**
2526 * set_current_blocked - change current->blocked mask
2527 * @newset: new mask
2528 *
2529 * It is wrong to change ->blocked directly, this helper should be used
2530 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 */
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002532void set_current_blocked(const sigset_t *newset)
2533{
2534 struct task_struct *tsk = current;
2535
2536 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002537 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002538 spin_unlock_irq(&tsk->sighand->siglock);
2539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541/*
2542 * This is also useful for kernel threads that want to temporarily
2543 * (or permanently) block certain signals.
2544 *
2545 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2546 * interface happily blocks "unblockable" signals like SIGKILL
2547 * and friends.
2548 */
2549int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2550{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002551 struct task_struct *tsk = current;
2552 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002554 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002555 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002556 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 switch (how) {
2559 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002560 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 break;
2562 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002563 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 break;
2565 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002566 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 break;
2568 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002571
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002572 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Randy Dunlap41c57892011-04-04 15:00:26 -07002576/**
2577 * sys_rt_sigprocmask - change the list of currently blocked signals
2578 * @how: whether to add, remove, or set signals
Randy Dunlapada9c932011-06-14 15:50:11 -07002579 * @nset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002580 * @oset: previous value of signal mask if non-null
2581 * @sigsetsize: size of sigset_t type
2582 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002583SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002584 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002587 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /* XXX: Don't preclude handling different sized sigset_t's. */
2590 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002593 old_set = current->blocked;
2594
2595 if (nset) {
2596 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2597 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2599
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002600 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002602 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002604
2605 if (oset) {
2606 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2607 return -EFAULT;
2608 }
2609
2610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
2613long do_sigpending(void __user *set, unsigned long sigsetsize)
2614{
2615 long error = -EINVAL;
2616 sigset_t pending;
2617
2618 if (sigsetsize > sizeof(sigset_t))
2619 goto out;
2620
2621 spin_lock_irq(&current->sighand->siglock);
2622 sigorsets(&pending, &current->pending.signal,
2623 &current->signal->shared_pending.signal);
2624 spin_unlock_irq(&current->sighand->siglock);
2625
2626 /* Outside the lock because only this thread touches it. */
2627 sigandsets(&pending, &current->blocked, &pending);
2628
2629 error = -EFAULT;
2630 if (!copy_to_user(set, &pending, sigsetsize))
2631 error = 0;
2632
2633out:
2634 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Randy Dunlap41c57892011-04-04 15:00:26 -07002637/**
2638 * sys_rt_sigpending - examine a pending signal that has been raised
2639 * while blocked
2640 * @set: stores pending signals
2641 * @sigsetsize: size of sigset_t type or larger
2642 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002643SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 return do_sigpending(set, sigsetsize);
2646}
2647
2648#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2649
2650int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2651{
2652 int err;
2653
2654 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2655 return -EFAULT;
2656 if (from->si_code < 0)
2657 return __copy_to_user(to, from, sizeof(siginfo_t))
2658 ? -EFAULT : 0;
2659 /*
2660 * If you change siginfo_t structure, please be sure
2661 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002662 * Please remember to update the signalfd_copyinfo() function
2663 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 * It should never copy any pad contained in the structure
2665 * to avoid security leaks, but must copy the generic
2666 * 3 ints plus the relevant union member.
2667 */
2668 err = __put_user(from->si_signo, &to->si_signo);
2669 err |= __put_user(from->si_errno, &to->si_errno);
2670 err |= __put_user((short)from->si_code, &to->si_code);
2671 switch (from->si_code & __SI_MASK) {
2672 case __SI_KILL:
2673 err |= __put_user(from->si_pid, &to->si_pid);
2674 err |= __put_user(from->si_uid, &to->si_uid);
2675 break;
2676 case __SI_TIMER:
2677 err |= __put_user(from->si_tid, &to->si_tid);
2678 err |= __put_user(from->si_overrun, &to->si_overrun);
2679 err |= __put_user(from->si_ptr, &to->si_ptr);
2680 break;
2681 case __SI_POLL:
2682 err |= __put_user(from->si_band, &to->si_band);
2683 err |= __put_user(from->si_fd, &to->si_fd);
2684 break;
2685 case __SI_FAULT:
2686 err |= __put_user(from->si_addr, &to->si_addr);
2687#ifdef __ARCH_SI_TRAPNO
2688 err |= __put_user(from->si_trapno, &to->si_trapno);
2689#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002690#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002691 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002692 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002693 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002694 */
2695 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2696 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 break;
2699 case __SI_CHLD:
2700 err |= __put_user(from->si_pid, &to->si_pid);
2701 err |= __put_user(from->si_uid, &to->si_uid);
2702 err |= __put_user(from->si_status, &to->si_status);
2703 err |= __put_user(from->si_utime, &to->si_utime);
2704 err |= __put_user(from->si_stime, &to->si_stime);
2705 break;
2706 case __SI_RT: /* This is not generated by the kernel as of now. */
2707 case __SI_MESGQ: /* But this is */
2708 err |= __put_user(from->si_pid, &to->si_pid);
2709 err |= __put_user(from->si_uid, &to->si_uid);
2710 err |= __put_user(from->si_ptr, &to->si_ptr);
2711 break;
2712 default: /* this is just in case for now ... */
2713 err |= __put_user(from->si_pid, &to->si_pid);
2714 err |= __put_user(from->si_uid, &to->si_uid);
2715 break;
2716 }
2717 return err;
2718}
2719
2720#endif
2721
Randy Dunlap41c57892011-04-04 15:00:26 -07002722/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002723 * do_sigtimedwait - wait for queued signals specified in @which
2724 * @which: queued signals to wait for
2725 * @info: if non-null, the signal's siginfo is returned here
2726 * @ts: upper bound on process time suspension
2727 */
2728int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2729 const struct timespec *ts)
2730{
2731 struct task_struct *tsk = current;
2732 long timeout = MAX_SCHEDULE_TIMEOUT;
2733 sigset_t mask = *which;
2734 int sig;
2735
2736 if (ts) {
2737 if (!timespec_valid(ts))
2738 return -EINVAL;
2739 timeout = timespec_to_jiffies(ts);
2740 /*
2741 * We can be close to the next tick, add another one
2742 * to ensure we will wait at least the time asked for.
2743 */
2744 if (ts->tv_sec || ts->tv_nsec)
2745 timeout++;
2746 }
2747
2748 /*
2749 * Invert the set of allowed signals to get those we want to block.
2750 */
2751 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2752 signotset(&mask);
2753
2754 spin_lock_irq(&tsk->sighand->siglock);
2755 sig = dequeue_signal(tsk, &mask, info);
2756 if (!sig && timeout) {
2757 /*
2758 * None ready, temporarily unblock those we're interested
2759 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002760 * they arrive. Unblocking is always fine, we can avoid
2761 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002762 */
2763 tsk->real_blocked = tsk->blocked;
2764 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2765 recalc_sigpending();
2766 spin_unlock_irq(&tsk->sighand->siglock);
2767
2768 timeout = schedule_timeout_interruptible(timeout);
2769
2770 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002771 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov943df142011-04-27 21:44:14 +02002772 siginitset(&tsk->real_blocked, 0);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002773 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002774 }
2775 spin_unlock_irq(&tsk->sighand->siglock);
2776
2777 if (sig)
2778 return sig;
2779 return timeout ? -EINTR : -EAGAIN;
2780}
2781
2782/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002783 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2784 * in @uthese
2785 * @uthese: queued signals to wait for
2786 * @uinfo: if non-null, the signal's siginfo is returned here
2787 * @uts: upper bound on process time suspension
2788 * @sigsetsize: size of sigset_t type
2789 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002790SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2791 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2792 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 sigset_t these;
2795 struct timespec ts;
2796 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002797 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 /* XXX: Don't preclude handling different sized sigset_t's. */
2800 if (sigsetsize != sizeof(sigset_t))
2801 return -EINVAL;
2802
2803 if (copy_from_user(&these, uthese, sizeof(these)))
2804 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (uts) {
2807 if (copy_from_user(&ts, uts, sizeof(ts)))
2808 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
2810
Oleg Nesterov943df142011-04-27 21:44:14 +02002811 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Oleg Nesterov943df142011-04-27 21:44:14 +02002813 if (ret > 0 && uinfo) {
2814 if (copy_siginfo_to_user(uinfo, &info))
2815 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
2817
2818 return ret;
2819}
2820
Randy Dunlap41c57892011-04-04 15:00:26 -07002821/**
2822 * sys_kill - send a signal to a process
2823 * @pid: the PID of the process
2824 * @sig: signal to be sent
2825 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002826SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 struct siginfo info;
2829
2830 info.si_signo = sig;
2831 info.si_errno = 0;
2832 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002833 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002834 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 return kill_something_info(sig, &info, pid);
2837}
2838
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002839static int
2840do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002841{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002842 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002843 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002844
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002845 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002846 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002847 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002848 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002849 /*
2850 * The null signal is a permissions and process existence
2851 * probe. No signal is actually delivered.
2852 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002853 if (!error && sig) {
2854 error = do_send_sig_info(sig, info, p, false);
2855 /*
2856 * If lock_task_sighand() failed we pretend the task
2857 * dies after receiving the signal. The window is tiny,
2858 * and the signal is private anyway.
2859 */
2860 if (unlikely(error == -ESRCH))
2861 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002862 }
2863 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002864 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002865
2866 return error;
2867}
2868
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002869static int do_tkill(pid_t tgid, pid_t pid, int sig)
2870{
2871 struct siginfo info;
2872
2873 info.si_signo = sig;
2874 info.si_errno = 0;
2875 info.si_code = SI_TKILL;
2876 info.si_pid = task_tgid_vnr(current);
2877 info.si_uid = current_uid();
2878
2879 return do_send_specific(tgid, pid, sig, &info);
2880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/**
2883 * sys_tgkill - send signal to one specific thread
2884 * @tgid: the thread group ID of the thread
2885 * @pid: the PID of the thread
2886 * @sig: signal to be sent
2887 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002888 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 * exists but it's not belonging to the target process anymore. This
2890 * method solves the problem of threads exiting and PIDs getting reused.
2891 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002892SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /* This is only valid for single tasks */
2895 if (pid <= 0 || tgid <= 0)
2896 return -EINVAL;
2897
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002898 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Randy Dunlap41c57892011-04-04 15:00:26 -07002901/**
2902 * sys_tkill - send signal to one specific task
2903 * @pid: the PID of the task
2904 * @sig: signal to be sent
2905 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2907 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002908SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 /* This is only valid for single tasks */
2911 if (pid <= 0)
2912 return -EINVAL;
2913
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002914 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Randy Dunlap41c57892011-04-04 15:00:26 -07002917/**
2918 * sys_rt_sigqueueinfo - send signal information to a signal
2919 * @pid: the PID of the thread
2920 * @sig: signal to be sent
2921 * @uinfo: signal info to be sent
2922 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002923SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2924 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
2926 siginfo_t info;
2927
2928 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2929 return -EFAULT;
2930
2931 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002932 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2933 */
Roland Dreier243b4222011-03-28 14:13:35 -07002934 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002935 /* We used to allow any < 0 si_code */
2936 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 info.si_signo = sig;
2940
2941 /* POSIX.1b doesn't mention process groups. */
2942 return kill_proc_info(sig, &info, pid);
2943}
2944
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002945long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2946{
2947 /* This is only valid for single tasks */
2948 if (pid <= 0 || tgid <= 0)
2949 return -EINVAL;
2950
2951 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002952 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2953 */
Roland Dreier243b4222011-03-28 14:13:35 -07002954 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002955 /* We used to allow any < 0 si_code */
2956 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002957 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002958 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002959 info->si_signo = sig;
2960
2961 return do_send_specific(tgid, pid, sig, info);
2962}
2963
2964SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2965 siginfo_t __user *, uinfo)
2966{
2967 siginfo_t info;
2968
2969 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2970 return -EFAULT;
2971
2972 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2973}
2974
Oleg Nesterov88531f72006-03-28 16:11:24 -08002975int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002977 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002979 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002981 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return -EINVAL;
2983
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002984 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (oact)
2988 *oact = *k;
2989
2990 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002991 sigdelsetmask(&act->sa.sa_mask,
2992 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002993 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 /*
2995 * POSIX 3.3.1.3:
2996 * "Setting a signal action to SIG_IGN for a signal that is
2997 * pending shall cause the pending signal to be discarded,
2998 * whether or not it is blocked."
2999 *
3000 * "Setting a signal action to SIG_DFL for a signal that is
3001 * pending and whose default action is to ignore the signal
3002 * (for example, SIGCHLD), shall cause the pending signal to
3003 * be discarded, whether or not it is blocked"
3004 */
Roland McGrath35de2542008-07-25 19:45:51 -07003005 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08003006 sigemptyset(&mask);
3007 sigaddset(&mask, sig);
3008 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 do {
George Anzinger71fabd52006-01-08 01:02:48 -08003010 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 t = next_thread(t);
3012 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
3015
3016 spin_unlock_irq(&current->sighand->siglock);
3017 return 0;
3018}
3019
3020int
3021do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
3022{
3023 stack_t oss;
3024 int error;
3025
Linus Torvalds0083fc22009-08-01 10:34:56 -07003026 oss.ss_sp = (void __user *) current->sas_ss_sp;
3027 oss.ss_size = current->sas_ss_size;
3028 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 if (uss) {
3031 void __user *ss_sp;
3032 size_t ss_size;
3033 int ss_flags;
3034
3035 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07003036 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
3037 goto out;
3038 error = __get_user(ss_sp, &uss->ss_sp) |
3039 __get_user(ss_flags, &uss->ss_flags) |
3040 __get_user(ss_size, &uss->ss_size);
3041 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 goto out;
3043
3044 error = -EPERM;
3045 if (on_sig_stack(sp))
3046 goto out;
3047
3048 error = -EINVAL;
3049 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07003050 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 * old code may have been written using ss_flags==0
3052 * to mean ss_flags==SS_ONSTACK (as this was the only
3053 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07003054 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 */
3056 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
3057 goto out;
3058
3059 if (ss_flags == SS_DISABLE) {
3060 ss_size = 0;
3061 ss_sp = NULL;
3062 } else {
3063 error = -ENOMEM;
3064 if (ss_size < MINSIGSTKSZ)
3065 goto out;
3066 }
3067
3068 current->sas_ss_sp = (unsigned long) ss_sp;
3069 current->sas_ss_size = ss_size;
3070 }
3071
Linus Torvalds0083fc22009-08-01 10:34:56 -07003072 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (uoss) {
3074 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07003075 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07003077 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
3078 __put_user(oss.ss_size, &uoss->ss_size) |
3079 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082out:
3083 return error;
3084}
3085
3086#ifdef __ARCH_WANT_SYS_SIGPENDING
3087
Randy Dunlap41c57892011-04-04 15:00:26 -07003088/**
3089 * sys_sigpending - examine pending signals
3090 * @set: where mask of pending signal is returned
3091 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01003092SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
3094 return do_sigpending(set, sizeof(*set));
3095}
3096
3097#endif
3098
3099#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07003100/**
3101 * sys_sigprocmask - examine and change blocked signals
3102 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02003103 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07003104 * @oset: previous value of signal mask if non-null
3105 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07003106 * Some platforms have their own version with special arguments;
3107 * others support only sys_rt_sigprocmask.
3108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Oleg Nesterovb013c392011-04-28 11:36:20 +02003110SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01003111 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003114 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Oleg Nesterovb013c392011-04-28 11:36:20 +02003116 old_set = current->blocked.sig[0];
3117
3118 if (nset) {
3119 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3120 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
3122
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003123 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003127 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 break;
3129 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003130 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 break;
3132 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003133 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003135 default:
3136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
3138
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003139 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02003141
3142 if (oset) {
3143 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3144 return -EFAULT;
3145 }
3146
3147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3150
3151#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07003152/**
3153 * sys_rt_sigaction - alter an action taken by a process
3154 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07003155 * @act: new sigaction
3156 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07003157 * @sigsetsize: size of sigset_t type
3158 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003159SYSCALL_DEFINE4(rt_sigaction, int, sig,
3160 const struct sigaction __user *, act,
3161 struct sigaction __user *, oact,
3162 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 struct k_sigaction new_sa, old_sa;
3165 int ret = -EINVAL;
3166
3167 /* XXX: Don't preclude handling different sized sigset_t's. */
3168 if (sigsetsize != sizeof(sigset_t))
3169 goto out;
3170
3171 if (act) {
3172 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3173 return -EFAULT;
3174 }
3175
3176 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3177
3178 if (!ret && oact) {
3179 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3180 return -EFAULT;
3181 }
3182out:
3183 return ret;
3184}
3185#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
3186
3187#ifdef __ARCH_WANT_SYS_SGETMASK
3188
3189/*
3190 * For backwards compatibility. Functionality superseded by sigprocmask.
3191 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003192SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 /* SMP safe */
3195 return current->blocked.sig[0];
3196}
3197
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003198SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003200 int old = current->blocked.sig[0];
3201 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003203 siginitset(&newset, newmask & ~(sigmask(SIGKILL) | sigmask(SIGSTOP)));
3204 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
3206 return old;
3207}
3208#endif /* __ARCH_WANT_SGETMASK */
3209
3210#ifdef __ARCH_WANT_SYS_SIGNAL
3211/*
3212 * For backwards compatibility. Functionality superseded by sigaction.
3213 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003214SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 struct k_sigaction new_sa, old_sa;
3217 int ret;
3218
3219 new_sa.sa.sa_handler = handler;
3220 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03003221 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 ret = do_sigaction(sig, &new_sa, &old_sa);
3224
3225 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3226}
3227#endif /* __ARCH_WANT_SYS_SIGNAL */
3228
3229#ifdef __ARCH_WANT_SYS_PAUSE
3230
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003231SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003233 while (!signal_pending(current)) {
3234 current->state = TASK_INTERRUPTIBLE;
3235 schedule();
3236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return -ERESTARTNOHAND;
3238}
3239
3240#endif
3241
David Woodhouse150256d2006-01-18 17:43:57 -08003242#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003243/**
3244 * sys_rt_sigsuspend - replace the signal mask for a value with the
3245 * @unewset value until a signal is received
3246 * @unewset: new signal mask value
3247 * @sigsetsize: size of sigset_t type
3248 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003249SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003250{
3251 sigset_t newset;
3252
3253 /* XXX: Don't preclude handling different sized sigset_t's. */
3254 if (sigsetsize != sizeof(sigset_t))
3255 return -EINVAL;
3256
3257 if (copy_from_user(&newset, unewset, sizeof(newset)))
3258 return -EFAULT;
3259 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3260
David Woodhouse150256d2006-01-18 17:43:57 -08003261 current->saved_sigmask = current->blocked;
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003262 set_current_blocked(&newset);
David Woodhouse150256d2006-01-18 17:43:57 -08003263
3264 current->state = TASK_INTERRUPTIBLE;
3265 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003266 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003267 return -ERESTARTNOHAND;
3268}
3269#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3270
David Howellsf269fdd2006-09-27 01:50:23 -07003271__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3272{
3273 return NULL;
3274}
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276void __init signals_init(void)
3277{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003278 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003280
3281#ifdef CONFIG_KGDB_KDB
3282#include <linux/kdb.h>
3283/*
3284 * kdb_send_sig_info - Allows kdb to send signals without exposing
3285 * signal internals. This function checks if the required locks are
3286 * available before calling the main signal code, to avoid kdb
3287 * deadlocks.
3288 */
3289void
3290kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3291{
3292 static struct task_struct *kdb_prev_t;
3293 int sig, new_t;
3294 if (!spin_trylock(&t->sighand->siglock)) {
3295 kdb_printf("Can't do kill command now.\n"
3296 "The sigmask lock is held somewhere else in "
3297 "kernel, try again later\n");
3298 return;
3299 }
3300 spin_unlock(&t->sighand->siglock);
3301 new_t = kdb_prev_t != t;
3302 kdb_prev_t = t;
3303 if (t->state != TASK_RUNNING && new_t) {
3304 kdb_printf("Process is not RUNNING, sending a signal from "
3305 "kdb risks deadlock\n"
3306 "on the run queue locks. "
3307 "The signal has _not_ been sent.\n"
3308 "Reissue the kill command if you want to risk "
3309 "the deadlock.\n");
3310 return;
3311 }
3312 sig = info->si_signo;
3313 if (send_sig_info(sig, info, t))
3314 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3315 sig, t->pid);
3316 else
3317 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3318}
3319#endif /* CONFIG_KGDB_KDB */