]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/signal.c
signal: Fix premature completion of group stop when interfered by ptrace
[mirror_ubuntu-artful-kernel.git] / kernel / signal.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
13#include <linux/slab.h>
14#include <linux/module.h>
1da177e4
LT
15#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>
7ed20e1a 23#include <linux/signal.h>
fba2afaa 24#include <linux/signalfd.h>
f84d49b2 25#include <linux/ratelimit.h>
35de254d 26#include <linux/tracehook.h>
c59ede7b 27#include <linux/capability.h>
7dfb7103 28#include <linux/freezer.h>
84d73786
SB
29#include <linux/pid_namespace.h>
30#include <linux/nsproxy.h>
d1eb650f
MH
31#define CREATE_TRACE_POINTS
32#include <trace/events/signal.h>
84d73786 33
1da177e4
LT
34#include <asm/param.h>
35#include <asm/uaccess.h>
36#include <asm/unistd.h>
37#include <asm/siginfo.h>
e1396065 38#include "audit.h" /* audit_signal_info() */
1da177e4
LT
39
40/*
41 * SLAB caches for signal bits.
42 */
43
e18b890b 44static struct kmem_cache *sigqueue_cachep;
1da177e4 45
f84d49b2
NO
46int print_fatal_signals __read_mostly;
47
35de254d 48static void __user *sig_handler(struct task_struct *t, int sig)
93585eea 49{
35de254d
RM
50 return t->sighand->action[sig - 1].sa.sa_handler;
51}
93585eea 52
35de254d
RM
53static int sig_handler_ignored(void __user *handler, int sig)
54{
93585eea 55 /* Is it explicitly or implicitly ignored? */
93585eea
PE
56 return handler == SIG_IGN ||
57 (handler == SIG_DFL && sig_kernel_ignore(sig));
58}
1da177e4 59
921cf9f6
SB
60static int sig_task_ignored(struct task_struct *t, int sig,
61 int from_ancestor_ns)
1da177e4 62{
35de254d 63 void __user *handler;
1da177e4 64
f008faff
ON
65 handler = sig_handler(t, sig);
66
67 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
921cf9f6 68 handler == SIG_DFL && !from_ancestor_ns)
f008faff
ON
69 return 1;
70
71 return sig_handler_ignored(handler, sig);
72}
73
921cf9f6 74static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
f008faff 75{
1da177e4
LT
76 /*
77 * Blocked signals are never ignored, since the
78 * signal handler may change by the time it is
79 * unblocked.
80 */
325d22df 81 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
1da177e4
LT
82 return 0;
83
921cf9f6 84 if (!sig_task_ignored(t, sig, from_ancestor_ns))
35de254d
RM
85 return 0;
86
87 /*
88 * Tracers may want to know about even ignored signals.
89 */
43918f2b 90 return !tracehook_consider_ignored_signal(t, sig);
1da177e4
LT
91}
92
93/*
94 * Re-calculate pending state from the set of locally pending
95 * signals, globally pending signals, and blocked signals.
96 */
97static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
98{
99 unsigned long ready;
100 long i;
101
102 switch (_NSIG_WORDS) {
103 default:
104 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
105 ready |= signal->sig[i] &~ blocked->sig[i];
106 break;
107
108 case 4: ready = signal->sig[3] &~ blocked->sig[3];
109 ready |= signal->sig[2] &~ blocked->sig[2];
110 ready |= signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
113
114 case 2: ready = signal->sig[1] &~ blocked->sig[1];
115 ready |= signal->sig[0] &~ blocked->sig[0];
116 break;
117
118 case 1: ready = signal->sig[0] &~ blocked->sig[0];
119 }
120 return ready != 0;
121}
122
123#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
124
7bb44ade 125static int recalc_sigpending_tsk(struct task_struct *t)
1da177e4
LT
126{
127 if (t->signal->group_stop_count > 0 ||
128 PENDING(&t->pending, &t->blocked) ||
7bb44ade 129 PENDING(&t->signal->shared_pending, &t->blocked)) {
1da177e4 130 set_tsk_thread_flag(t, TIF_SIGPENDING);
7bb44ade
RM
131 return 1;
132 }
b74d0deb
RM
133 /*
134 * We must never clear the flag in another thread, or in current
135 * when it's possible the current syscall is returning -ERESTART*.
136 * So we don't clear it here, and only callers who know they should do.
137 */
7bb44ade
RM
138 return 0;
139}
140
141/*
142 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
143 * This is superfluous when called on current, the wakeup is a harmless no-op.
144 */
145void recalc_sigpending_and_wake(struct task_struct *t)
146{
147 if (recalc_sigpending_tsk(t))
148 signal_wake_up(t, 0);
1da177e4
LT
149}
150
151void recalc_sigpending(void)
152{
b787f7ba
RM
153 if (unlikely(tracehook_force_sigpending()))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
b74d0deb
RM
156 clear_thread_flag(TIF_SIGPENDING);
157
1da177e4
LT
158}
159
160/* Given the mask, find the first available signal that should be serviced. */
161
a27341cd
LT
162#define SYNCHRONOUS_MASK \
163 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
164 sigmask(SIGTRAP) | sigmask(SIGFPE))
165
fba2afaa 166int next_signal(struct sigpending *pending, sigset_t *mask)
1da177e4
LT
167{
168 unsigned long i, *s, *m, x;
169 int sig = 0;
f84d49b2 170
1da177e4
LT
171 s = pending->signal.sig;
172 m = mask->sig;
a27341cd
LT
173
174 /*
175 * Handle the first word specially: it contains the
176 * synchronous signals that need to be dequeued first.
177 */
178 x = *s &~ *m;
179 if (x) {
180 if (x & SYNCHRONOUS_MASK)
181 x &= SYNCHRONOUS_MASK;
182 sig = ffz(~x) + 1;
183 return sig;
184 }
185
1da177e4
LT
186 switch (_NSIG_WORDS) {
187 default:
a27341cd
LT
188 for (i = 1; i < _NSIG_WORDS; ++i) {
189 x = *++s &~ *++m;
190 if (!x)
191 continue;
192 sig = ffz(~x) + i*_NSIG_BPW + 1;
193 break;
194 }
1da177e4
LT
195 break;
196
a27341cd
LT
197 case 2:
198 x = s[1] &~ m[1];
199 if (!x)
1da177e4 200 break;
a27341cd 201 sig = ffz(~x) + _NSIG_BPW + 1;
1da177e4
LT
202 break;
203
a27341cd
LT
204 case 1:
205 /* Nothing to do */
1da177e4
LT
206 break;
207 }
f84d49b2 208
1da177e4
LT
209 return sig;
210}
211
f84d49b2
NO
212static inline void print_dropped_signal(int sig)
213{
214 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
215
216 if (!print_fatal_signals)
217 return;
218
219 if (!__ratelimit(&ratelimit_state))
220 return;
221
222 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
223 current->comm, current->pid, sig);
224}
225
e5c1902e
TH
226/**
227 * task_clear_group_stop_pending - clear pending group stop
228 * @task: target task
229 *
230 * Clear group stop states for @task.
231 *
232 * CONTEXT:
233 * Must be called with @task->sighand->siglock held.
234 */
235static void task_clear_group_stop_pending(struct task_struct *task)
236{
237 task->group_stop &= ~GROUP_STOP_CONSUME;
238}
239
240/**
241 * task_participate_group_stop - participate in a group stop
242 * @task: task participating in a group stop
243 *
244 * @task is participating in a group stop. Group stop states are cleared
245 * and the group stop count is consumed if %GROUP_STOP_CONSUME was set. If
246 * the consumption completes the group stop, the appropriate %SIGNAL_*
247 * flags are set.
248 *
249 * CONTEXT:
250 * Must be called with @task->sighand->siglock held.
251 */
252static bool task_participate_group_stop(struct task_struct *task)
253{
254 struct signal_struct *sig = task->signal;
255 bool consume = task->group_stop & GROUP_STOP_CONSUME;
256
257 task_clear_group_stop_pending(task);
258
259 if (!consume)
260 return false;
261
262 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
263 sig->group_stop_count--;
264
265 if (!sig->group_stop_count) {
266 sig->flags = SIGNAL_STOP_STOPPED;
267 return true;
268 }
269 return false;
270}
271
c69e8d9c
DH
272/*
273 * allocate a new signal queue record
274 * - this may be called without locks if and only if t == current, otherwise an
d84f4f99 275 * appopriate lock must be held to stop the target task from exiting
c69e8d9c 276 */
f84d49b2
NO
277static struct sigqueue *
278__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
1da177e4
LT
279{
280 struct sigqueue *q = NULL;
10b1fbdb 281 struct user_struct *user;
1da177e4 282
10b1fbdb 283 /*
7cf7db8d
TG
284 * Protect access to @t credentials. This can go away when all
285 * callers hold rcu read lock.
10b1fbdb 286 */
7cf7db8d 287 rcu_read_lock();
d84f4f99 288 user = get_uid(__task_cred(t)->user);
10b1fbdb 289 atomic_inc(&user->sigpending);
7cf7db8d 290 rcu_read_unlock();
f84d49b2 291
1da177e4 292 if (override_rlimit ||
10b1fbdb 293 atomic_read(&user->sigpending) <=
78d7d407 294 task_rlimit(t, RLIMIT_SIGPENDING)) {
1da177e4 295 q = kmem_cache_alloc(sigqueue_cachep, flags);
f84d49b2
NO
296 } else {
297 print_dropped_signal(sig);
298 }
299
1da177e4 300 if (unlikely(q == NULL)) {
10b1fbdb 301 atomic_dec(&user->sigpending);
d84f4f99 302 free_uid(user);
1da177e4
LT
303 } else {
304 INIT_LIST_HEAD(&q->list);
305 q->flags = 0;
d84f4f99 306 q->user = user;
1da177e4 307 }
d84f4f99
DH
308
309 return q;
1da177e4
LT
310}
311
514a01b8 312static void __sigqueue_free(struct sigqueue *q)
1da177e4
LT
313{
314 if (q->flags & SIGQUEUE_PREALLOC)
315 return;
316 atomic_dec(&q->user->sigpending);
317 free_uid(q->user);
318 kmem_cache_free(sigqueue_cachep, q);
319}
320
6a14c5c9 321void flush_sigqueue(struct sigpending *queue)
1da177e4
LT
322{
323 struct sigqueue *q;
324
325 sigemptyset(&queue->signal);
326 while (!list_empty(&queue->list)) {
327 q = list_entry(queue->list.next, struct sigqueue , list);
328 list_del_init(&q->list);
329 __sigqueue_free(q);
330 }
331}
332
333/*
334 * Flush all pending signals for a task.
335 */
3bcac026
DH
336void __flush_signals(struct task_struct *t)
337{
338 clear_tsk_thread_flag(t, TIF_SIGPENDING);
339 flush_sigqueue(&t->pending);
340 flush_sigqueue(&t->signal->shared_pending);
341}
342
c81addc9 343void flush_signals(struct task_struct *t)
1da177e4
LT
344{
345 unsigned long flags;
346
347 spin_lock_irqsave(&t->sighand->siglock, flags);
3bcac026 348 __flush_signals(t);
1da177e4
LT
349 spin_unlock_irqrestore(&t->sighand->siglock, flags);
350}
351
cbaffba1
ON
352static void __flush_itimer_signals(struct sigpending *pending)
353{
354 sigset_t signal, retain;
355 struct sigqueue *q, *n;
356
357 signal = pending->signal;
358 sigemptyset(&retain);
359
360 list_for_each_entry_safe(q, n, &pending->list, list) {
361 int sig = q->info.si_signo;
362
363 if (likely(q->info.si_code != SI_TIMER)) {
364 sigaddset(&retain, sig);
365 } else {
366 sigdelset(&signal, sig);
367 list_del_init(&q->list);
368 __sigqueue_free(q);
369 }
370 }
371
372 sigorsets(&pending->signal, &signal, &retain);
373}
374
375void flush_itimer_signals(void)
376{
377 struct task_struct *tsk = current;
378 unsigned long flags;
379
380 spin_lock_irqsave(&tsk->sighand->siglock, flags);
381 __flush_itimer_signals(&tsk->pending);
382 __flush_itimer_signals(&tsk->signal->shared_pending);
383 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
384}
385
10ab825b
ON
386void ignore_signals(struct task_struct *t)
387{
388 int i;
389
390 for (i = 0; i < _NSIG; ++i)
391 t->sighand->action[i].sa.sa_handler = SIG_IGN;
392
393 flush_signals(t);
394}
395
1da177e4
LT
396/*
397 * Flush all handlers for a task.
398 */
399
400void
401flush_signal_handlers(struct task_struct *t, int force_default)
402{
403 int i;
404 struct k_sigaction *ka = &t->sighand->action[0];
405 for (i = _NSIG ; i != 0 ; i--) {
406 if (force_default || ka->sa.sa_handler != SIG_IGN)
407 ka->sa.sa_handler = SIG_DFL;
408 ka->sa.sa_flags = 0;
409 sigemptyset(&ka->sa.sa_mask);
410 ka++;
411 }
412}
413
abd4f750
MAS
414int unhandled_signal(struct task_struct *tsk, int sig)
415{
445a91d2 416 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
b460cbc5 417 if (is_global_init(tsk))
abd4f750 418 return 1;
445a91d2 419 if (handler != SIG_IGN && handler != SIG_DFL)
abd4f750 420 return 0;
43918f2b 421 return !tracehook_consider_fatal_signal(tsk, sig);
abd4f750
MAS
422}
423
1da177e4
LT
424
425/* Notify the system that a driver wants to block all signals for this
426 * process, and wants to be notified if any signals at all were to be
427 * sent/acted upon. If the notifier routine returns non-zero, then the
428 * signal will be acted upon after all. If the notifier routine returns 0,
429 * then then signal will be blocked. Only one block per process is
430 * allowed. priv is a pointer to private data that the notifier routine
431 * can use to determine if the signal should be blocked or not. */
432
433void
434block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
435{
436 unsigned long flags;
437
438 spin_lock_irqsave(&current->sighand->siglock, flags);
439 current->notifier_mask = mask;
440 current->notifier_data = priv;
441 current->notifier = notifier;
442 spin_unlock_irqrestore(&current->sighand->siglock, flags);
443}
444
445/* Notify the system that blocking has ended. */
446
447void
448unblock_all_signals(void)
449{
450 unsigned long flags;
451
452 spin_lock_irqsave(&current->sighand->siglock, flags);
453 current->notifier = NULL;
454 current->notifier_data = NULL;
455 recalc_sigpending();
456 spin_unlock_irqrestore(&current->sighand->siglock, flags);
457}
458
100360f0 459static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
1da177e4
LT
460{
461 struct sigqueue *q, *first = NULL;
1da177e4 462
1da177e4
LT
463 /*
464 * Collect the siginfo appropriate to this signal. Check if
465 * there is another siginfo for the same signal.
466 */
467 list_for_each_entry(q, &list->list, list) {
468 if (q->info.si_signo == sig) {
d4434207
ON
469 if (first)
470 goto still_pending;
1da177e4
LT
471 first = q;
472 }
473 }
d4434207
ON
474
475 sigdelset(&list->signal, sig);
476
1da177e4 477 if (first) {
d4434207 478still_pending:
1da177e4
LT
479 list_del_init(&first->list);
480 copy_siginfo(info, &first->info);
481 __sigqueue_free(first);
1da177e4 482 } else {
1da177e4
LT
483 /* Ok, it wasn't in the queue. This must be
484 a fast-pathed signal or we must have been
485 out of queue space. So zero out the info.
486 */
1da177e4
LT
487 info->si_signo = sig;
488 info->si_errno = 0;
7486e5d9 489 info->si_code = SI_USER;
1da177e4
LT
490 info->si_pid = 0;
491 info->si_uid = 0;
492 }
1da177e4
LT
493}
494
495static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
496 siginfo_t *info)
497{
27d91e07 498 int sig = next_signal(pending, mask);
1da177e4 499
1da177e4
LT
500 if (sig) {
501 if (current->notifier) {
502 if (sigismember(current->notifier_mask, sig)) {
503 if (!(current->notifier)(current->notifier_data)) {
504 clear_thread_flag(TIF_SIGPENDING);
505 return 0;
506 }
507 }
508 }
509
100360f0 510 collect_signal(sig, pending, info);
1da177e4 511 }
1da177e4
LT
512
513 return sig;
514}
515
516/*
517 * Dequeue a signal and return the element to the caller, which is
518 * expected to free it.
519 *
520 * All callers have to hold the siglock.
521 */
522int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
523{
c5363d03 524 int signr;
caec4e8d
BH
525
526 /* We only dequeue private signals from ourselves, we don't let
527 * signalfd steal them
528 */
b8fceee1 529 signr = __dequeue_signal(&tsk->pending, mask, info);
8bfd9a7a 530 if (!signr) {
1da177e4
LT
531 signr = __dequeue_signal(&tsk->signal->shared_pending,
532 mask, info);
8bfd9a7a
TG
533 /*
534 * itimer signal ?
535 *
536 * itimers are process shared and we restart periodic
537 * itimers in the signal delivery path to prevent DoS
538 * attacks in the high resolution timer case. This is
539 * compliant with the old way of self restarting
540 * itimers, as the SIGALRM is a legacy signal and only
541 * queued once. Changing the restart behaviour to
542 * restart the timer in the signal dequeue path is
543 * reducing the timer noise on heavy loaded !highres
544 * systems too.
545 */
546 if (unlikely(signr == SIGALRM)) {
547 struct hrtimer *tmr = &tsk->signal->real_timer;
548
549 if (!hrtimer_is_queued(tmr) &&
550 tsk->signal->it_real_incr.tv64 != 0) {
551 hrtimer_forward(tmr, tmr->base->get_time(),
552 tsk->signal->it_real_incr);
553 hrtimer_restart(tmr);
554 }
555 }
556 }
c5363d03 557
b8fceee1 558 recalc_sigpending();
c5363d03
PE
559 if (!signr)
560 return 0;
561
562 if (unlikely(sig_kernel_stop(signr))) {
8bfd9a7a
TG
563 /*
564 * Set a marker that we have dequeued a stop signal. Our
565 * caller might release the siglock and then the pending
566 * stop signal it is about to process is no longer in the
567 * pending bitmasks, but must still be cleared by a SIGCONT
568 * (and overruled by a SIGKILL). So those cases clear this
569 * shared flag after we've set it. Note that this flag may
570 * remain set after the signal we return is ignored or
571 * handled. That doesn't matter because its only purpose
572 * is to alert stop-signal processing code when another
573 * processor has come along and cleared the flag.
574 */
92413d77 575 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
8bfd9a7a 576 }
c5363d03 577 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
1da177e4
LT
578 /*
579 * Release the siglock to ensure proper locking order
580 * of timer locks outside of siglocks. Note, we leave
581 * irqs disabled here, since the posix-timers code is
582 * about to disable them again anyway.
583 */
584 spin_unlock(&tsk->sighand->siglock);
585 do_schedule_next_timer(info);
586 spin_lock(&tsk->sighand->siglock);
587 }
588 return signr;
589}
590
591/*
592 * Tell a process that it has a new active signal..
593 *
594 * NOTE! we rely on the previous spin_lock to
595 * lock interrupts for us! We can only be called with
596 * "siglock" held, and the local interrupt must
597 * have been disabled when that got acquired!
598 *
599 * No need to set need_resched since signal event passing
600 * goes through ->blocked
601 */
602void signal_wake_up(struct task_struct *t, int resume)
603{
604 unsigned int mask;
605
606 set_tsk_thread_flag(t, TIF_SIGPENDING);
607
608 /*
f021a3c2
MW
609 * For SIGKILL, we want to wake it up in the stopped/traced/killable
610 * case. We don't check t->state here because there is a race with it
1da177e4
LT
611 * executing another processor and just now entering stopped state.
612 * By using wake_up_state, we ensure the process will wake up and
613 * handle its death signal.
614 */
615 mask = TASK_INTERRUPTIBLE;
616 if (resume)
f021a3c2 617 mask |= TASK_WAKEKILL;
1da177e4
LT
618 if (!wake_up_state(t, mask))
619 kick_process(t);
620}
621
71fabd5e
GA
622/*
623 * Remove signals in mask from the pending set and queue.
624 * Returns 1 if any signals were found.
625 *
626 * All callers must be holding the siglock.
627 *
628 * This version takes a sigset mask and looks at all signals,
629 * not just those in the first mask word.
630 */
631static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
632{
633 struct sigqueue *q, *n;
634 sigset_t m;
635
636 sigandsets(&m, mask, &s->signal);
637 if (sigisemptyset(&m))
638 return 0;
639
640 signandsets(&s->signal, &s->signal, mask);
641 list_for_each_entry_safe(q, n, &s->list, list) {
642 if (sigismember(mask, q->info.si_signo)) {
643 list_del_init(&q->list);
644 __sigqueue_free(q);
645 }
646 }
647 return 1;
648}
1da177e4
LT
649/*
650 * Remove signals in mask from the pending set and queue.
651 * Returns 1 if any signals were found.
652 *
653 * All callers must be holding the siglock.
654 */
655static int rm_from_queue(unsigned long mask, struct sigpending *s)
656{
657 struct sigqueue *q, *n;
658
659 if (!sigtestsetmask(&s->signal, mask))
660 return 0;
661
662 sigdelsetmask(&s->signal, mask);
663 list_for_each_entry_safe(q, n, &s->list, list) {
664 if (q->info.si_signo < SIGRTMIN &&
665 (mask & sigmask(q->info.si_signo))) {
666 list_del_init(&q->list);
667 __sigqueue_free(q);
668 }
669 }
670 return 1;
671}
672
614c517d
ON
673static inline int is_si_special(const struct siginfo *info)
674{
675 return info <= SEND_SIG_FORCED;
676}
677
678static inline bool si_fromuser(const struct siginfo *info)
679{
680 return info == SEND_SIG_NOINFO ||
681 (!is_si_special(info) && SI_FROMUSER(info));
682}
683
1da177e4
LT
684/*
685 * Bad permissions for sending the signal
694f690d 686 * - the caller must hold the RCU read lock
1da177e4
LT
687 */
688static int check_kill_permission(int sig, struct siginfo *info,
689 struct task_struct *t)
690{
065add39 691 const struct cred *cred, *tcred;
2e2ba22e 692 struct pid *sid;
3b5e9e53
ON
693 int error;
694
7ed20e1a 695 if (!valid_signal(sig))
3b5e9e53
ON
696 return -EINVAL;
697
614c517d 698 if (!si_fromuser(info))
3b5e9e53 699 return 0;
e54dc243 700
3b5e9e53
ON
701 error = audit_signal_info(sig, t); /* Let audit system see the signal */
702 if (error)
1da177e4 703 return error;
3b5e9e53 704
065add39 705 cred = current_cred();
c69e8d9c 706 tcred = __task_cred(t);
065add39
ON
707 if (!same_thread_group(current, t) &&
708 (cred->euid ^ tcred->suid) &&
c69e8d9c
DH
709 (cred->euid ^ tcred->uid) &&
710 (cred->uid ^ tcred->suid) &&
711 (cred->uid ^ tcred->uid) &&
2e2ba22e
ON
712 !capable(CAP_KILL)) {
713 switch (sig) {
714 case SIGCONT:
2e2ba22e 715 sid = task_session(t);
2e2ba22e
ON
716 /*
717 * We don't return the error if sid == NULL. The
718 * task was unhashed, the caller must notice this.
719 */
720 if (!sid || sid == task_session(current))
721 break;
722 default:
723 return -EPERM;
724 }
725 }
c2f0c7c3 726
e54dc243 727 return security_task_kill(t, info, sig, 0);
1da177e4
LT
728}
729
1da177e4 730/*
7e695a5e
ON
731 * Handle magic process-wide effects of stop/continue signals. Unlike
732 * the signal actions, these happen immediately at signal-generation
1da177e4
LT
733 * time regardless of blocking, ignoring, or handling. This does the
734 * actual continuing for SIGCONT, but not the actual stopping for stop
7e695a5e
ON
735 * signals. The process stop is done as a signal action for SIG_DFL.
736 *
737 * Returns true if the signal should be actually delivered, otherwise
738 * it should be dropped.
1da177e4 739 */
921cf9f6 740static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
1da177e4 741{
ad16a460 742 struct signal_struct *signal = p->signal;
1da177e4
LT
743 struct task_struct *t;
744
7e695a5e 745 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
1da177e4 746 /*
7e695a5e 747 * The process is in the middle of dying, nothing to do.
1da177e4 748 */
7e695a5e 749 } else if (sig_kernel_stop(sig)) {
1da177e4
LT
750 /*
751 * This is a stop signal. Remove SIGCONT from all queues.
752 */
ad16a460 753 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
1da177e4
LT
754 t = p;
755 do {
756 rm_from_queue(sigmask(SIGCONT), &t->pending);
ad16a460 757 } while_each_thread(p, t);
1da177e4 758 } else if (sig == SIGCONT) {
fc321d2e 759 unsigned int why;
1da177e4
LT
760 /*
761 * Remove all stop signals from all queues,
762 * and wake all threads.
763 */
ad16a460 764 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
1da177e4
LT
765 t = p;
766 do {
767 unsigned int state;
768 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1da177e4
LT
769 /*
770 * If there is a handler for SIGCONT, we must make
771 * sure that no thread returns to user mode before
772 * we post the signal, in case it was the only
773 * thread eligible to run the signal handler--then
774 * it must not do anything between resuming and
775 * running the handler. With the TIF_SIGPENDING
776 * flag set, the thread will pause and acquire the
777 * siglock that we hold now and until we've queued
fc321d2e 778 * the pending signal.
1da177e4
LT
779 *
780 * Wake up the stopped thread _after_ setting
781 * TIF_SIGPENDING
782 */
f021a3c2 783 state = __TASK_STOPPED;
1da177e4
LT
784 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
785 set_tsk_thread_flag(t, TIF_SIGPENDING);
786 state |= TASK_INTERRUPTIBLE;
787 }
788 wake_up_state(t, state);
ad16a460 789 } while_each_thread(p, t);
1da177e4 790
fc321d2e
ON
791 /*
792 * Notify the parent with CLD_CONTINUED if we were stopped.
793 *
794 * If we were in the middle of a group stop, we pretend it
795 * was already finished, and then continued. Since SIGCHLD
796 * doesn't queue we report only CLD_STOPPED, as if the next
797 * CLD_CONTINUED was dropped.
798 */
799 why = 0;
ad16a460 800 if (signal->flags & SIGNAL_STOP_STOPPED)
fc321d2e 801 why |= SIGNAL_CLD_CONTINUED;
ad16a460 802 else if (signal->group_stop_count)
fc321d2e
ON
803 why |= SIGNAL_CLD_STOPPED;
804
805 if (why) {
021e1ae3 806 /*
ae6d2ed7 807 * The first thread which returns from do_signal_stop()
021e1ae3
ON
808 * will take ->siglock, notice SIGNAL_CLD_MASK, and
809 * notify its parent. See get_signal_to_deliver().
810 */
ad16a460
ON
811 signal->flags = why | SIGNAL_STOP_CONTINUED;
812 signal->group_stop_count = 0;
813 signal->group_exit_code = 0;
1da177e4
LT
814 } else {
815 /*
816 * We are not stopped, but there could be a stop
817 * signal in the middle of being processed after
818 * being removed from the queue. Clear that too.
819 */
ad16a460 820 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
1da177e4 821 }
1da177e4 822 }
7e695a5e 823
921cf9f6 824 return !sig_ignored(p, sig, from_ancestor_ns);
1da177e4
LT
825}
826
71f11dc0
ON
827/*
828 * Test if P wants to take SIG. After we've checked all threads with this,
829 * it's equivalent to finding no threads not blocking SIG. Any threads not
830 * blocking SIG were ruled out because they are not running and already
831 * have pending signals. Such threads will dequeue from the shared queue
832 * as soon as they're available, so putting the signal on the shared queue
833 * will be equivalent to sending it to one such thread.
834 */
835static inline int wants_signal(int sig, struct task_struct *p)
836{
837 if (sigismember(&p->blocked, sig))
838 return 0;
839 if (p->flags & PF_EXITING)
840 return 0;
841 if (sig == SIGKILL)
842 return 1;
843 if (task_is_stopped_or_traced(p))
844 return 0;
845 return task_curr(p) || !signal_pending(p);
846}
847
5fcd835b 848static void complete_signal(int sig, struct task_struct *p, int group)
71f11dc0
ON
849{
850 struct signal_struct *signal = p->signal;
851 struct task_struct *t;
852
853 /*
854 * Now find a thread we can wake up to take the signal off the queue.
855 *
856 * If the main thread wants the signal, it gets first crack.
857 * Probably the least surprising to the average bear.
858 */
859 if (wants_signal(sig, p))
860 t = p;
5fcd835b 861 else if (!group || thread_group_empty(p))
71f11dc0
ON
862 /*
863 * There is just one thread and it does not need to be woken.
864 * It will dequeue unblocked signals before it runs again.
865 */
866 return;
867 else {
868 /*
869 * Otherwise try to find a suitable thread.
870 */
871 t = signal->curr_target;
872 while (!wants_signal(sig, t)) {
873 t = next_thread(t);
874 if (t == signal->curr_target)
875 /*
876 * No thread needs to be woken.
877 * Any eligible threads will see
878 * the signal in the queue soon.
879 */
880 return;
881 }
882 signal->curr_target = t;
883 }
884
885 /*
886 * Found a killable thread. If the signal will be fatal,
887 * then start taking the whole group down immediately.
888 */
fae5fa44
ON
889 if (sig_fatal(p, sig) &&
890 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
71f11dc0 891 !sigismember(&t->real_blocked, sig) &&
445a91d2 892 (sig == SIGKILL ||
43918f2b 893 !tracehook_consider_fatal_signal(t, sig))) {
71f11dc0
ON
894 /*
895 * This signal will be fatal to the whole group.
896 */
897 if (!sig_kernel_coredump(sig)) {
898 /*
899 * Start a group exit and wake everybody up.
900 * This way we don't have other threads
901 * running and doing things after a slower
902 * thread has the fatal signal pending.
903 */
904 signal->flags = SIGNAL_GROUP_EXIT;
905 signal->group_exit_code = sig;
906 signal->group_stop_count = 0;
907 t = p;
908 do {
909 sigaddset(&t->pending.signal, SIGKILL);
910 signal_wake_up(t, 1);
911 } while_each_thread(p, t);
912 return;
913 }
914 }
915
916 /*
917 * The signal is already in the shared-pending queue.
918 * Tell the chosen thread to wake up and dequeue it.
919 */
920 signal_wake_up(t, sig == SIGKILL);
921 return;
922}
923
af7fff9c
PE
924static inline int legacy_queue(struct sigpending *signals, int sig)
925{
926 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
927}
928
7978b567
SB
929static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
930 int group, int from_ancestor_ns)
1da177e4 931{
2ca3515a 932 struct sigpending *pending;
6e65acba 933 struct sigqueue *q;
7a0aeb14 934 int override_rlimit;
1da177e4 935
d1eb650f 936 trace_signal_generate(sig, info, t);
0a16b607 937
6e65acba 938 assert_spin_locked(&t->sighand->siglock);
921cf9f6
SB
939
940 if (!prepare_signal(sig, t, from_ancestor_ns))
7e695a5e 941 return 0;
2ca3515a
ON
942
943 pending = group ? &t->signal->shared_pending : &t->pending;
2acb024d
PE
944 /*
945 * Short-circuit ignored signals and support queuing
946 * exactly one non-rt signal, so that we can get more
947 * detailed information about the cause of the signal.
948 */
7e695a5e 949 if (legacy_queue(pending, sig))
2acb024d 950 return 0;
1da177e4
LT
951 /*
952 * fast-pathed signals for kernel-internal things like SIGSTOP
953 * or SIGKILL.
954 */
b67a1b9e 955 if (info == SEND_SIG_FORCED)
1da177e4
LT
956 goto out_set;
957
958 /* Real-time signals must be queued if sent by sigqueue, or
959 some other real-time mechanism. It is implementation
960 defined whether kill() does so. We attempt to do so, on
961 the principle of least surprise, but since kill is not
962 allowed to fail with EAGAIN when low on memory we just
963 make sure at least one signal gets delivered and don't
964 pass on the info struct. */
965
7a0aeb14
VN
966 if (sig < SIGRTMIN)
967 override_rlimit = (is_si_special(info) || info->si_code >= 0);
968 else
969 override_rlimit = 0;
970
f84d49b2 971 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
7a0aeb14 972 override_rlimit);
1da177e4 973 if (q) {
2ca3515a 974 list_add_tail(&q->list, &pending->list);
1da177e4 975 switch ((unsigned long) info) {
b67a1b9e 976 case (unsigned long) SEND_SIG_NOINFO:
1da177e4
LT
977 q->info.si_signo = sig;
978 q->info.si_errno = 0;
979 q->info.si_code = SI_USER;
9cd4fd10 980 q->info.si_pid = task_tgid_nr_ns(current,
09bca05c 981 task_active_pid_ns(t));
76aac0e9 982 q->info.si_uid = current_uid();
1da177e4 983 break;
b67a1b9e 984 case (unsigned long) SEND_SIG_PRIV:
1da177e4
LT
985 q->info.si_signo = sig;
986 q->info.si_errno = 0;
987 q->info.si_code = SI_KERNEL;
988 q->info.si_pid = 0;
989 q->info.si_uid = 0;
990 break;
991 default:
992 copy_siginfo(&q->info, info);
6588c1e3
SB
993 if (from_ancestor_ns)
994 q->info.si_pid = 0;
1da177e4
LT
995 break;
996 }
621d3121 997 } else if (!is_si_special(info)) {
ba005e1f
MH
998 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
999 /*
1000 * Queue overflow, abort. We may abort if the
1001 * signal was rt and sent by user using something
1002 * other than kill().
1003 */
1004 trace_signal_overflow_fail(sig, group, info);
1da177e4 1005 return -EAGAIN;
ba005e1f
MH
1006 } else {
1007 /*
1008 * This is a silent loss of information. We still
1009 * send the signal, but the *info bits are lost.
1010 */
1011 trace_signal_lose_info(sig, group, info);
1012 }
1da177e4
LT
1013 }
1014
1015out_set:
53c30337 1016 signalfd_notify(t, sig);
2ca3515a 1017 sigaddset(&pending->signal, sig);
4cd4b6d4
PE
1018 complete_signal(sig, t, group);
1019 return 0;
1da177e4
LT
1020}
1021
7978b567
SB
1022static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1023 int group)
1024{
921cf9f6
SB
1025 int from_ancestor_ns = 0;
1026
1027#ifdef CONFIG_PID_NS
dd34200a
ON
1028 from_ancestor_ns = si_fromuser(info) &&
1029 !task_pid_nr_ns(current, task_active_pid_ns(t));
921cf9f6
SB
1030#endif
1031
1032 return __send_signal(sig, info, t, group, from_ancestor_ns);
7978b567
SB
1033}
1034
45807a1d
IM
1035static void print_fatal_signal(struct pt_regs *regs, int signr)
1036{
1037 printk("%s/%d: potentially unexpected fatal signal %d.\n",
ba25f9dc 1038 current->comm, task_pid_nr(current), signr);
45807a1d 1039
ca5cd877 1040#if defined(__i386__) && !defined(__arch_um__)
65ea5b03 1041 printk("code at %08lx: ", regs->ip);
45807a1d
IM
1042 {
1043 int i;
1044 for (i = 0; i < 16; i++) {
1045 unsigned char insn;
1046
b45c6e76
AK
1047 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1048 break;
45807a1d
IM
1049 printk("%02x ", insn);
1050 }
1051 }
1052#endif
1053 printk("\n");
3a9f84d3 1054 preempt_disable();
45807a1d 1055 show_regs(regs);
3a9f84d3 1056 preempt_enable();
45807a1d
IM
1057}
1058
1059static int __init setup_print_fatal_signals(char *str)
1060{
1061 get_option (&str, &print_fatal_signals);
1062
1063 return 1;
1064}
1065
1066__setup("print-fatal-signals=", setup_print_fatal_signals);
1da177e4 1067
4cd4b6d4
PE
1068int
1069__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1070{
1071 return send_signal(sig, info, p, 1);
1072}
1073
1da177e4
LT
1074static int
1075specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1076{
4cd4b6d4 1077 return send_signal(sig, info, t, 0);
1da177e4
LT
1078}
1079
4a30debf
ON
1080int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1081 bool group)
1082{
1083 unsigned long flags;
1084 int ret = -ESRCH;
1085
1086 if (lock_task_sighand(p, &flags)) {
1087 ret = send_signal(sig, info, p, group);
1088 unlock_task_sighand(p, &flags);
1089 }
1090
1091 return ret;
1092}
1093
1da177e4
LT
1094/*
1095 * Force a signal that the process can't ignore: if necessary
1096 * we unblock the signal and change any SIG_IGN to SIG_DFL.
ae74c3b6
LT
1097 *
1098 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1099 * since we do not want to have a signal handler that was blocked
1100 * be invoked when user space had explicitly blocked it.
1101 *
80fe728d
ON
1102 * We don't want to have recursive SIGSEGV's etc, for example,
1103 * that is why we also clear SIGNAL_UNKILLABLE.
1da177e4 1104 */
1da177e4
LT
1105int
1106force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1107{
1108 unsigned long int flags;
ae74c3b6
LT
1109 int ret, blocked, ignored;
1110 struct k_sigaction *action;
1da177e4
LT
1111
1112 spin_lock_irqsave(&t->sighand->siglock, flags);
ae74c3b6
LT
1113 action = &t->sighand->action[sig-1];
1114 ignored = action->sa.sa_handler == SIG_IGN;
1115 blocked = sigismember(&t->blocked, sig);
1116 if (blocked || ignored) {
1117 action->sa.sa_handler = SIG_DFL;
1118 if (blocked) {
1119 sigdelset(&t->blocked, sig);
7bb44ade 1120 recalc_sigpending_and_wake(t);
ae74c3b6 1121 }
1da177e4 1122 }
80fe728d
ON
1123 if (action->sa.sa_handler == SIG_DFL)
1124 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1da177e4
LT
1125 ret = specific_send_sig_info(sig, info, t);
1126 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1127
1128 return ret;
1129}
1130
1da177e4
LT
1131/*
1132 * Nuke all other threads in the group.
1133 */
09faef11 1134int zap_other_threads(struct task_struct *p)
1da177e4 1135{
09faef11
ON
1136 struct task_struct *t = p;
1137 int count = 0;
1da177e4 1138
1da177e4
LT
1139 p->signal->group_stop_count = 0;
1140
09faef11
ON
1141 while_each_thread(p, t) {
1142 count++;
1143
1144 /* Don't bother with already dead threads */
1da177e4
LT
1145 if (t->exit_state)
1146 continue;
1da177e4 1147 sigaddset(&t->pending.signal, SIGKILL);
1da177e4
LT
1148 signal_wake_up(t, 1);
1149 }
09faef11
ON
1150
1151 return count;
1da177e4
LT
1152}
1153
b8ed374e
NK
1154struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1155 unsigned long *flags)
f63ee72e
ON
1156{
1157 struct sighand_struct *sighand;
1158
1406f2d3 1159 rcu_read_lock();
f63ee72e
ON
1160 for (;;) {
1161 sighand = rcu_dereference(tsk->sighand);
1162 if (unlikely(sighand == NULL))
1163 break;
1164
1165 spin_lock_irqsave(&sighand->siglock, *flags);
1166 if (likely(sighand == tsk->sighand))
1167 break;
1168 spin_unlock_irqrestore(&sighand->siglock, *flags);
1169 }
1406f2d3 1170 rcu_read_unlock();
f63ee72e
ON
1171
1172 return sighand;
1173}
1174
c69e8d9c
DH
1175/*
1176 * send signal info to all the members of a group
c69e8d9c 1177 */
1da177e4
LT
1178int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1179{
694f690d
DH
1180 int ret;
1181
1182 rcu_read_lock();
1183 ret = check_kill_permission(sig, info, p);
1184 rcu_read_unlock();
f63ee72e 1185
4a30debf
ON
1186 if (!ret && sig)
1187 ret = do_send_sig_info(sig, info, p, true);
1da177e4
LT
1188
1189 return ret;
1190}
1191
1192/*
146a505d 1193 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1da177e4 1194 * control characters do (^C, ^Z etc)
c69e8d9c 1195 * - the caller must hold at least a readlock on tasklist_lock
1da177e4 1196 */
c4b92fc1 1197int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1da177e4
LT
1198{
1199 struct task_struct *p = NULL;
1200 int retval, success;
1201
1da177e4
LT
1202 success = 0;
1203 retval = -ESRCH;
c4b92fc1 1204 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4
LT
1205 int err = group_send_sig_info(sig, info, p);
1206 success |= !err;
1207 retval = err;
c4b92fc1 1208 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
1209 return success ? 0 : retval;
1210}
1211
c4b92fc1 1212int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1da177e4 1213{
d36174bc 1214 int error = -ESRCH;
1da177e4
LT
1215 struct task_struct *p;
1216
e56d0903 1217 rcu_read_lock();
d36174bc 1218retry:
c4b92fc1 1219 p = pid_task(pid, PIDTYPE_PID);
d36174bc 1220 if (p) {
1da177e4 1221 error = group_send_sig_info(sig, info, p);
d36174bc
ON
1222 if (unlikely(error == -ESRCH))
1223 /*
1224 * The task was unhashed in between, try again.
1225 * If it is dead, pid_task() will return NULL,
1226 * if we race with de_thread() it will find the
1227 * new leader.
1228 */
1229 goto retry;
1230 }
e56d0903 1231 rcu_read_unlock();
6ca25b55 1232
1da177e4
LT
1233 return error;
1234}
1235
c3de4b38
MW
1236int
1237kill_proc_info(int sig, struct siginfo *info, pid_t pid)
c4b92fc1
EB
1238{
1239 int error;
1240 rcu_read_lock();
b488893a 1241 error = kill_pid_info(sig, info, find_vpid(pid));
c4b92fc1
EB
1242 rcu_read_unlock();
1243 return error;
1244}
1245
2425c08b
EB
1246/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1247int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
8f95dc58 1248 uid_t uid, uid_t euid, u32 secid)
46113830
HW
1249{
1250 int ret = -EINVAL;
1251 struct task_struct *p;
c69e8d9c 1252 const struct cred *pcred;
14d8c9f3 1253 unsigned long flags;
46113830
HW
1254
1255 if (!valid_signal(sig))
1256 return ret;
1257
14d8c9f3 1258 rcu_read_lock();
2425c08b 1259 p = pid_task(pid, PIDTYPE_PID);
46113830
HW
1260 if (!p) {
1261 ret = -ESRCH;
1262 goto out_unlock;
1263 }
c69e8d9c 1264 pcred = __task_cred(p);
614c517d 1265 if (si_fromuser(info) &&
c69e8d9c
DH
1266 euid != pcred->suid && euid != pcred->uid &&
1267 uid != pcred->suid && uid != pcred->uid) {
46113830
HW
1268 ret = -EPERM;
1269 goto out_unlock;
1270 }
8f95dc58
DQ
1271 ret = security_task_kill(p, info, sig, secid);
1272 if (ret)
1273 goto out_unlock;
14d8c9f3
TG
1274
1275 if (sig) {
1276 if (lock_task_sighand(p, &flags)) {
1277 ret = __send_signal(sig, info, p, 1, 0);
1278 unlock_task_sighand(p, &flags);
1279 } else
1280 ret = -ESRCH;
46113830
HW
1281 }
1282out_unlock:
14d8c9f3 1283 rcu_read_unlock();
46113830
HW
1284 return ret;
1285}
2425c08b 1286EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1da177e4
LT
1287
1288/*
1289 * kill_something_info() interprets pid in interesting ways just like kill(2).
1290 *
1291 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1292 * is probably wrong. Should make it like BSD or SYSV.
1293 */
1294
bc64efd2 1295static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1da177e4 1296{
8d42db18 1297 int ret;
d5df763b
PE
1298
1299 if (pid > 0) {
1300 rcu_read_lock();
1301 ret = kill_pid_info(sig, info, find_vpid(pid));
1302 rcu_read_unlock();
1303 return ret;
1304 }
1305
1306 read_lock(&tasklist_lock);
1307 if (pid != -1) {
1308 ret = __kill_pgrp_info(sig, info,
1309 pid ? find_vpid(-pid) : task_pgrp(current));
1310 } else {
1da177e4
LT
1311 int retval = 0, count = 0;
1312 struct task_struct * p;
1313
1da177e4 1314 for_each_process(p) {
d25141a8
SB
1315 if (task_pid_vnr(p) > 1 &&
1316 !same_thread_group(p, current)) {
1da177e4
LT
1317 int err = group_send_sig_info(sig, info, p);
1318 ++count;
1319 if (err != -EPERM)
1320 retval = err;
1321 }
1322 }
8d42db18 1323 ret = count ? retval : -ESRCH;
1da177e4 1324 }
d5df763b
PE
1325 read_unlock(&tasklist_lock);
1326
8d42db18 1327 return ret;
1da177e4
LT
1328}
1329
1330/*
1331 * These are for backward compatibility with the rest of the kernel source.
1332 */
1333
1da177e4
LT
1334int
1335send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1336{
1da177e4
LT
1337 /*
1338 * Make sure legacy kernel users don't send in bad values
1339 * (normal paths check this in check_kill_permission).
1340 */
7ed20e1a 1341 if (!valid_signal(sig))
1da177e4
LT
1342 return -EINVAL;
1343
4a30debf 1344 return do_send_sig_info(sig, info, p, false);
1da177e4
LT
1345}
1346
b67a1b9e
ON
1347#define __si_special(priv) \
1348 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1349
1da177e4
LT
1350int
1351send_sig(int sig, struct task_struct *p, int priv)
1352{
b67a1b9e 1353 return send_sig_info(sig, __si_special(priv), p);
1da177e4
LT
1354}
1355
1da177e4
LT
1356void
1357force_sig(int sig, struct task_struct *p)
1358{
b67a1b9e 1359 force_sig_info(sig, SEND_SIG_PRIV, p);
1da177e4
LT
1360}
1361
1362/*
1363 * When things go south during signal handling, we
1364 * will force a SIGSEGV. And if the signal that caused
1365 * the problem was already a SIGSEGV, we'll want to
1366 * make sure we don't even try to deliver the signal..
1367 */
1368int
1369force_sigsegv(int sig, struct task_struct *p)
1370{
1371 if (sig == SIGSEGV) {
1372 unsigned long flags;
1373 spin_lock_irqsave(&p->sighand->siglock, flags);
1374 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1375 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1376 }
1377 force_sig(SIGSEGV, p);
1378 return 0;
1379}
1380
c4b92fc1
EB
1381int kill_pgrp(struct pid *pid, int sig, int priv)
1382{
146a505d
PE
1383 int ret;
1384
1385 read_lock(&tasklist_lock);
1386 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1387 read_unlock(&tasklist_lock);
1388
1389 return ret;
c4b92fc1
EB
1390}
1391EXPORT_SYMBOL(kill_pgrp);
1392
1393int kill_pid(struct pid *pid, int sig, int priv)
1394{
1395 return kill_pid_info(sig, __si_special(priv), pid);
1396}
1397EXPORT_SYMBOL(kill_pid);
1398
1da177e4
LT
1399/*
1400 * These functions support sending signals using preallocated sigqueue
1401 * structures. This is needed "because realtime applications cannot
1402 * afford to lose notifications of asynchronous events, like timer
f84d49b2 1403 * expirations or I/O completions". In the case of Posix Timers
1da177e4
LT
1404 * we allocate the sigqueue structure from the timer_create. If this
1405 * allocation fails we are able to report the failure to the application
1406 * with an EAGAIN error.
1407 */
1da177e4
LT
1408struct sigqueue *sigqueue_alloc(void)
1409{
f84d49b2 1410 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1da177e4 1411
f84d49b2 1412 if (q)
1da177e4 1413 q->flags |= SIGQUEUE_PREALLOC;
f84d49b2
NO
1414
1415 return q;
1da177e4
LT
1416}
1417
1418void sigqueue_free(struct sigqueue *q)
1419{
1420 unsigned long flags;
60187d27
ON
1421 spinlock_t *lock = &current->sighand->siglock;
1422
1da177e4
LT
1423 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1424 /*
c8e85b4f
ON
1425 * We must hold ->siglock while testing q->list
1426 * to serialize with collect_signal() or with
da7978b0 1427 * __exit_signal()->flush_sigqueue().
1da177e4 1428 */
60187d27 1429 spin_lock_irqsave(lock, flags);
c8e85b4f
ON
1430 q->flags &= ~SIGQUEUE_PREALLOC;
1431 /*
1432 * If it is queued it will be freed when dequeued,
1433 * like the "regular" sigqueue.
1434 */
60187d27 1435 if (!list_empty(&q->list))
c8e85b4f 1436 q = NULL;
60187d27
ON
1437 spin_unlock_irqrestore(lock, flags);
1438
c8e85b4f
ON
1439 if (q)
1440 __sigqueue_free(q);
1da177e4
LT
1441}
1442
ac5c2153 1443int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
9e3bd6c3 1444{
e62e6650 1445 int sig = q->info.si_signo;
2ca3515a 1446 struct sigpending *pending;
e62e6650
ON
1447 unsigned long flags;
1448 int ret;
2ca3515a 1449
4cd4b6d4 1450 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
e62e6650
ON
1451
1452 ret = -1;
1453 if (!likely(lock_task_sighand(t, &flags)))
1454 goto ret;
1455
7e695a5e 1456 ret = 1; /* the signal is ignored */
921cf9f6 1457 if (!prepare_signal(sig, t, 0))
e62e6650
ON
1458 goto out;
1459
1460 ret = 0;
9e3bd6c3
PE
1461 if (unlikely(!list_empty(&q->list))) {
1462 /*
1463 * If an SI_TIMER entry is already queue just increment
1464 * the overrun count.
1465 */
9e3bd6c3
PE
1466 BUG_ON(q->info.si_code != SI_TIMER);
1467 q->info.si_overrun++;
e62e6650 1468 goto out;
9e3bd6c3 1469 }
ba661292 1470 q->info.si_overrun = 0;
9e3bd6c3 1471
9e3bd6c3 1472 signalfd_notify(t, sig);
2ca3515a 1473 pending = group ? &t->signal->shared_pending : &t->pending;
9e3bd6c3
PE
1474 list_add_tail(&q->list, &pending->list);
1475 sigaddset(&pending->signal, sig);
4cd4b6d4 1476 complete_signal(sig, t, group);
e62e6650
ON
1477out:
1478 unlock_task_sighand(t, &flags);
1479ret:
1480 return ret;
9e3bd6c3
PE
1481}
1482
1da177e4
LT
1483/*
1484 * Let a parent know about the death of a child.
1485 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
2b2a1ff6
RM
1486 *
1487 * Returns -1 if our parent ignored us and so we've switched to
1488 * self-reaping, or else @sig.
1da177e4 1489 */
2b2a1ff6 1490int do_notify_parent(struct task_struct *tsk, int sig)
1da177e4
LT
1491{
1492 struct siginfo info;
1493 unsigned long flags;
1494 struct sighand_struct *psig;
1b04624f 1495 int ret = sig;
1da177e4
LT
1496
1497 BUG_ON(sig == -1);
1498
1499 /* do_notify_parent_cldstop should have been called instead. */
e1abb39c 1500 BUG_ON(task_is_stopped_or_traced(tsk));
1da177e4 1501
5cb11446 1502 BUG_ON(!task_ptrace(tsk) &&
1da177e4
LT
1503 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1504
1505 info.si_signo = sig;
1506 info.si_errno = 0;
b488893a
PE
1507 /*
1508 * we are under tasklist_lock here so our parent is tied to
1509 * us and cannot exit and release its namespace.
1510 *
1511 * the only it can is to switch its nsproxy with sys_unshare,
1512 * bu uncharing pid namespaces is not allowed, so we'll always
1513 * see relevant namespace
1514 *
1515 * write_lock() currently calls preempt_disable() which is the
1516 * same as rcu_read_lock(), but according to Oleg, this is not
1517 * correct to rely on this
1518 */
1519 rcu_read_lock();
1520 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
c69e8d9c 1521 info.si_uid = __task_cred(tsk)->uid;
b488893a
PE
1522 rcu_read_unlock();
1523
32bd671d
PZ
1524 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1525 tsk->signal->utime));
1526 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1527 tsk->signal->stime));
1da177e4
LT
1528
1529 info.si_status = tsk->exit_code & 0x7f;
1530 if (tsk->exit_code & 0x80)
1531 info.si_code = CLD_DUMPED;
1532 else if (tsk->exit_code & 0x7f)
1533 info.si_code = CLD_KILLED;
1534 else {
1535 info.si_code = CLD_EXITED;
1536 info.si_status = tsk->exit_code >> 8;
1537 }
1538
1539 psig = tsk->parent->sighand;
1540 spin_lock_irqsave(&psig->siglock, flags);
5cb11446 1541 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1da177e4
LT
1542 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1543 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1544 /*
1545 * We are exiting and our parent doesn't care. POSIX.1
1546 * defines special semantics for setting SIGCHLD to SIG_IGN
1547 * or setting the SA_NOCLDWAIT flag: we should be reaped
1548 * automatically and not left for our parent's wait4 call.
1549 * Rather than having the parent do it as a magic kind of
1550 * signal handler, we just set this to tell do_exit that we
1551 * can be cleaned up without becoming a zombie. Note that
1552 * we still call __wake_up_parent in this case, because a
1553 * blocked sys_wait4 might now return -ECHILD.
1554 *
1555 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1556 * is implementation-defined: we do (if you don't want
1557 * it, just use SIG_IGN instead).
1558 */
1b04624f 1559 ret = tsk->exit_signal = -1;
1da177e4 1560 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
2b2a1ff6 1561 sig = -1;
1da177e4 1562 }
7ed20e1a 1563 if (valid_signal(sig) && sig > 0)
1da177e4
LT
1564 __group_send_sig_info(sig, &info, tsk->parent);
1565 __wake_up_parent(tsk, tsk->parent);
1566 spin_unlock_irqrestore(&psig->siglock, flags);
2b2a1ff6 1567
1b04624f 1568 return ret;
1da177e4
LT
1569}
1570
a1d5e21e 1571static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1da177e4
LT
1572{
1573 struct siginfo info;
1574 unsigned long flags;
bc505a47 1575 struct task_struct *parent;
1da177e4
LT
1576 struct sighand_struct *sighand;
1577
5cb11446 1578 if (task_ptrace(tsk))
bc505a47
ON
1579 parent = tsk->parent;
1580 else {
1581 tsk = tsk->group_leader;
1582 parent = tsk->real_parent;
1583 }
1584
1da177e4
LT
1585 info.si_signo = SIGCHLD;
1586 info.si_errno = 0;
b488893a
PE
1587 /*
1588 * see comment in do_notify_parent() abot the following 3 lines
1589 */
1590 rcu_read_lock();
d9265663 1591 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
c69e8d9c 1592 info.si_uid = __task_cred(tsk)->uid;
b488893a
PE
1593 rcu_read_unlock();
1594
d8878ba3
MK
1595 info.si_utime = cputime_to_clock_t(tsk->utime);
1596 info.si_stime = cputime_to_clock_t(tsk->stime);
1da177e4
LT
1597
1598 info.si_code = why;
1599 switch (why) {
1600 case CLD_CONTINUED:
1601 info.si_status = SIGCONT;
1602 break;
1603 case CLD_STOPPED:
1604 info.si_status = tsk->signal->group_exit_code & 0x7f;
1605 break;
1606 case CLD_TRAPPED:
1607 info.si_status = tsk->exit_code & 0x7f;
1608 break;
1609 default:
1610 BUG();
1611 }
1612
1613 sighand = parent->sighand;
1614 spin_lock_irqsave(&sighand->siglock, flags);
1615 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1616 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1617 __group_send_sig_info(SIGCHLD, &info, parent);
1618 /*
1619 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1620 */
1621 __wake_up_parent(tsk, parent);
1622 spin_unlock_irqrestore(&sighand->siglock, flags);
1623}
1624
d5f70c00
ON
1625static inline int may_ptrace_stop(void)
1626{
5cb11446 1627 if (!likely(task_ptrace(current)))
d5f70c00 1628 return 0;
d5f70c00
ON
1629 /*
1630 * Are we in the middle of do_coredump?
1631 * If so and our tracer is also part of the coredump stopping
1632 * is a deadlock situation, and pointless because our tracer
1633 * is dead so don't allow us to stop.
1634 * If SIGKILL was already sent before the caller unlocked
999d9fc1 1635 * ->siglock we must see ->core_state != NULL. Otherwise it
d5f70c00
ON
1636 * is safe to enter schedule().
1637 */
999d9fc1 1638 if (unlikely(current->mm->core_state) &&
d5f70c00
ON
1639 unlikely(current->mm == current->parent->mm))
1640 return 0;
1641
1642 return 1;
1643}
1644
1a669c2f
RM
1645/*
1646 * Return nonzero if there is a SIGKILL that should be waking us up.
1647 * Called with the siglock held.
1648 */
1649static int sigkill_pending(struct task_struct *tsk)
1650{
3d749b9e
ON
1651 return sigismember(&tsk->pending.signal, SIGKILL) ||
1652 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1a669c2f
RM
1653}
1654
1da177e4
LT
1655/*
1656 * This must be called with current->sighand->siglock held.
1657 *
1658 * This should be the path for all ptrace stops.
1659 * We always set current->last_siginfo while stopped here.
1660 * That makes it a way to test a stopped process for
1661 * being ptrace-stopped vs being job-control-stopped.
1662 *
20686a30
ON
1663 * If we actually decide not to stop at all because the tracer
1664 * is gone, we keep current->exit_code unless clear_code.
1da177e4 1665 */
fe1bc6a0 1666static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
b8401150
NK
1667 __releases(&current->sighand->siglock)
1668 __acquires(&current->sighand->siglock)
1da177e4 1669{
1a669c2f
RM
1670 if (arch_ptrace_stop_needed(exit_code, info)) {
1671 /*
1672 * The arch code has something special to do before a
1673 * ptrace stop. This is allowed to block, e.g. for faults
1674 * on user stack pages. We can't keep the siglock while
1675 * calling arch_ptrace_stop, so we must release it now.
1676 * To preserve proper semantics, we must do this before
1677 * any signal bookkeeping like checking group_stop_count.
1678 * Meanwhile, a SIGKILL could come in before we retake the
1679 * siglock. That must prevent us from sleeping in TASK_TRACED.
1680 * So after regaining the lock, we must check for SIGKILL.
1681 */
1682 spin_unlock_irq(&current->sighand->siglock);
1683 arch_ptrace_stop(exit_code, info);
1684 spin_lock_irq(&current->sighand->siglock);
3d749b9e
ON
1685 if (sigkill_pending(current))
1686 return;
1a669c2f
RM
1687 }
1688
1da177e4
LT
1689 /*
1690 * If there is a group stop in progress,
1691 * we must participate in the bookkeeping.
1692 */
1693 if (current->signal->group_stop_count > 0)
e5c1902e 1694 task_participate_group_stop(current);
1da177e4
LT
1695
1696 current->last_siginfo = info;
1697 current->exit_code = exit_code;
1698
1699 /* Let the debugger run. */
d9ae90ac 1700 __set_current_state(TASK_TRACED);
1da177e4
LT
1701 spin_unlock_irq(&current->sighand->siglock);
1702 read_lock(&tasklist_lock);
3d749b9e 1703 if (may_ptrace_stop()) {
fe1bc6a0 1704 do_notify_parent_cldstop(current, why);
53da1d94
MS
1705 /*
1706 * Don't want to allow preemption here, because
1707 * sys_ptrace() needs this task to be inactive.
1708 *
1709 * XXX: implement read_unlock_no_resched().
1710 */
1711 preempt_disable();
1da177e4 1712 read_unlock(&tasklist_lock);
53da1d94 1713 preempt_enable_no_resched();
1da177e4
LT
1714 schedule();
1715 } else {
1716 /*
1717 * By the time we got the lock, our tracer went away.
6405f7f4 1718 * Don't drop the lock yet, another tracer may come.
1da177e4 1719 */
6405f7f4 1720 __set_current_state(TASK_RUNNING);
20686a30
ON
1721 if (clear_code)
1722 current->exit_code = 0;
6405f7f4 1723 read_unlock(&tasklist_lock);
1da177e4
LT
1724 }
1725
13b1c3d4
RM
1726 /*
1727 * While in TASK_TRACED, we were considered "frozen enough".
1728 * Now that we woke up, it's crucial if we're supposed to be
1729 * frozen that we freeze now before running anything substantial.
1730 */
1731 try_to_freeze();
1732
1da177e4
LT
1733 /*
1734 * We are back. Now reacquire the siglock before touching
1735 * last_siginfo, so that we are sure to have synchronized with
1736 * any signal-sending on another CPU that wants to examine it.
1737 */
1738 spin_lock_irq(&current->sighand->siglock);
1739 current->last_siginfo = NULL;
1740
1741 /*
1742 * Queued signals ignored us while we were stopped for tracing.
1743 * So check for any that we should take before resuming user mode.
b74d0deb 1744 * This sets TIF_SIGPENDING, but never clears it.
1da177e4 1745 */
b74d0deb 1746 recalc_sigpending_tsk(current);
1da177e4
LT
1747}
1748
1749void ptrace_notify(int exit_code)
1750{
1751 siginfo_t info;
1752
1753 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1754
1755 memset(&info, 0, sizeof info);
1756 info.si_signo = SIGTRAP;
1757 info.si_code = exit_code;
b488893a 1758 info.si_pid = task_pid_vnr(current);
76aac0e9 1759 info.si_uid = current_uid();
1da177e4
LT
1760
1761 /* Let the debugger run. */
1762 spin_lock_irq(&current->sighand->siglock);
fe1bc6a0 1763 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
1da177e4
LT
1764 spin_unlock_irq(&current->sighand->siglock);
1765}
1766
1da177e4
LT
1767/*
1768 * This performs the stopping for SIGSTOP and other stop signals.
1769 * We have to stop all threads in the thread group.
1770 * Returns nonzero if we've actually stopped and released the siglock.
1771 * Returns zero if we didn't stop and still hold the siglock.
1772 */
a122b341 1773static int do_signal_stop(int signr)
1da177e4
LT
1774{
1775 struct signal_struct *sig = current->signal;
edf2ed15 1776 int notify = 0;
1da177e4 1777
ae6d2ed7 1778 if (!sig->group_stop_count) {
e5c1902e 1779 unsigned int gstop = GROUP_STOP_CONSUME;
f558b7e4
ON
1780 struct task_struct *t;
1781
2b201a9e 1782 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
573cf9ad 1783 unlikely(signal_group_exit(sig)))
f558b7e4 1784 return 0;
1da177e4
LT
1785 /*
1786 * There is no group stop already in progress.
a122b341 1787 * We must initiate one now.
1da177e4 1788 */
a122b341 1789 sig->group_exit_code = signr;
1da177e4 1790
e5c1902e 1791 current->group_stop = gstop;
ae6d2ed7 1792 sig->group_stop_count = 1;
a122b341 1793 for (t = next_thread(current); t != current; t = next_thread(t))
1da177e4 1794 /*
a122b341
ON
1795 * Setting state to TASK_STOPPED for a group
1796 * stop is always done with the siglock held,
1797 * so this check has no races.
1da177e4 1798 */
d12619b5 1799 if (!(t->flags & PF_EXITING) &&
e1abb39c 1800 !task_is_stopped_or_traced(t)) {
e5c1902e 1801 t->group_stop = gstop;
ae6d2ed7 1802 sig->group_stop_count++;
a122b341 1803 signal_wake_up(t, 0);
e5c1902e
TH
1804 } else
1805 task_clear_group_stop_pending(t);
1da177e4 1806 }
ae6d2ed7
RM
1807 /*
1808 * If there are no other threads in the group, or if there is
1809 * a group stop in progress and we are the last to stop, report
1810 * to the parent. When ptraced, every thread reports itself.
1811 */
e5c1902e 1812 if (task_participate_group_stop(current))
edf2ed15 1813 notify = CLD_STOPPED;
edf2ed15
TH
1814 if (task_ptrace(current))
1815 notify = CLD_STOPPED;
1816
1817 current->exit_code = sig->group_exit_code;
1818 __set_current_state(TASK_STOPPED);
1819
ae6d2ed7 1820 spin_unlock_irq(&current->sighand->siglock);
1da177e4 1821
ae6d2ed7
RM
1822 if (notify) {
1823 read_lock(&tasklist_lock);
1824 do_notify_parent_cldstop(current, notify);
1825 read_unlock(&tasklist_lock);
1826 }
1827
1828 /* Now we don't run again until woken by SIGCONT or SIGKILL */
71db5eb9 1829 schedule();
ae6d2ed7
RM
1830
1831 tracehook_finish_jctl();
1832 current->exit_code = 0;
dac27f4a 1833
1da177e4
LT
1834 return 1;
1835}
1836
18c98b65
RM
1837static int ptrace_signal(int signr, siginfo_t *info,
1838 struct pt_regs *regs, void *cookie)
1839{
5cb11446 1840 if (!task_ptrace(current))
18c98b65
RM
1841 return signr;
1842
1843 ptrace_signal_deliver(regs, cookie);
1844
1845 /* Let the debugger run. */
fe1bc6a0 1846 ptrace_stop(signr, CLD_TRAPPED, 0, info);
18c98b65
RM
1847
1848 /* We're back. Did the debugger cancel the sig? */
1849 signr = current->exit_code;
1850 if (signr == 0)
1851 return signr;
1852
1853 current->exit_code = 0;
1854
1855 /* Update the siginfo structure if the signal has
1856 changed. If the debugger wanted something
1857 specific in the siginfo structure then it should
1858 have updated *info via PTRACE_SETSIGINFO. */
1859 if (signr != info->si_signo) {
1860 info->si_signo = signr;
1861 info->si_errno = 0;
1862 info->si_code = SI_USER;
1863 info->si_pid = task_pid_vnr(current->parent);
c69e8d9c 1864 info->si_uid = task_uid(current->parent);
18c98b65
RM
1865 }
1866
1867 /* If the (new) signal is now blocked, requeue it. */
1868 if (sigismember(&current->blocked, signr)) {
1869 specific_send_sig_info(signr, info, current);
1870 signr = 0;
1871 }
1872
1873 return signr;
1874}
1875
1da177e4
LT
1876int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1877 struct pt_regs *regs, void *cookie)
1878{
f6b76d4f
ON
1879 struct sighand_struct *sighand = current->sighand;
1880 struct signal_struct *signal = current->signal;
1881 int signr;
1da177e4 1882
13b1c3d4
RM
1883relock:
1884 /*
1885 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1886 * While in TASK_STOPPED, we were considered "frozen enough".
1887 * Now that we woke up, it's crucial if we're supposed to be
1888 * frozen that we freeze now before running anything substantial.
1889 */
fc558a74
RW
1890 try_to_freeze();
1891
f6b76d4f 1892 spin_lock_irq(&sighand->siglock);
021e1ae3
ON
1893 /*
1894 * Every stopped thread goes here after wakeup. Check to see if
1895 * we should notify the parent, prepare_signal(SIGCONT) encodes
1896 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1897 */
f6b76d4f 1898 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
c672af35
TH
1899 int why;
1900
1901 if (signal->flags & SIGNAL_CLD_CONTINUED)
1902 why = CLD_CONTINUED;
1903 else
1904 why = CLD_STOPPED;
1905
f6b76d4f 1906 signal->flags &= ~SIGNAL_CLD_MASK;
e4420551 1907
ae6d2ed7 1908 spin_unlock_irq(&sighand->siglock);
fa00b80b 1909
edf2ed15
TH
1910 read_lock(&tasklist_lock);
1911 do_notify_parent_cldstop(current->group_leader, why);
1912 read_unlock(&tasklist_lock);
e4420551
ON
1913 goto relock;
1914 }
1915
1da177e4
LT
1916 for (;;) {
1917 struct k_sigaction *ka;
7bcf6a2c
RM
1918 /*
1919 * Tracing can induce an artifical signal and choose sigaction.
1920 * The return value in @signr determines the default action,
1921 * but @info->si_signo is the signal number we will report.
1922 */
1923 signr = tracehook_get_signal(current, regs, info, return_ka);
1924 if (unlikely(signr < 0))
1925 goto relock;
1926 if (unlikely(signr != 0))
1927 ka = return_ka;
1928 else {
1be53963
ON
1929 if (unlikely(signal->group_stop_count > 0) &&
1930 do_signal_stop(0))
1931 goto relock;
1932
7bcf6a2c
RM
1933 signr = dequeue_signal(current, &current->blocked,
1934 info);
1da177e4 1935
18c98b65 1936 if (!signr)
7bcf6a2c
RM
1937 break; /* will return 0 */
1938
1939 if (signr != SIGKILL) {
1940 signr = ptrace_signal(signr, info,
1941 regs, cookie);
1942 if (!signr)
1943 continue;
1944 }
1945
1946 ka = &sighand->action[signr-1];
1da177e4
LT
1947 }
1948
f9d4257e
MH
1949 /* Trace actually delivered signals. */
1950 trace_signal_deliver(signr, info, ka);
1951
1da177e4
LT
1952 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1953 continue;
1954 if (ka->sa.sa_handler != SIG_DFL) {
1955 /* Run the handler. */
1956 *return_ka = *ka;
1957
1958 if (ka->sa.sa_flags & SA_ONESHOT)
1959 ka->sa.sa_handler = SIG_DFL;
1960
1961 break; /* will return non-zero "signr" value */
1962 }
1963
1964 /*
1965 * Now we are doing the default action for this signal.
1966 */
1967 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1968 continue;
1969
84d73786 1970 /*
0fbc26a6 1971 * Global init gets no signals it doesn't want.
b3bfa0cb
SB
1972 * Container-init gets no signals it doesn't want from same
1973 * container.
1974 *
1975 * Note that if global/container-init sees a sig_kernel_only()
1976 * signal here, the signal must have been generated internally
1977 * or must have come from an ancestor namespace. In either
1978 * case, the signal cannot be dropped.
84d73786 1979 */
fae5fa44 1980 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
b3bfa0cb 1981 !sig_kernel_only(signr))
1da177e4
LT
1982 continue;
1983
1984 if (sig_kernel_stop(signr)) {
1985 /*
1986 * The default action is to stop all threads in
1987 * the thread group. The job control signals
1988 * do nothing in an orphaned pgrp, but SIGSTOP
1989 * always works. Note that siglock needs to be
1990 * dropped during the call to is_orphaned_pgrp()
1991 * because of lock ordering with tasklist_lock.
1992 * This allows an intervening SIGCONT to be posted.
1993 * We need to check for that and bail out if necessary.
1994 */
1995 if (signr != SIGSTOP) {
f6b76d4f 1996 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
1997
1998 /* signals can be posted during this window */
1999
3e7cd6c4 2000 if (is_current_pgrp_orphaned())
1da177e4
LT
2001 goto relock;
2002
f6b76d4f 2003 spin_lock_irq(&sighand->siglock);
1da177e4
LT
2004 }
2005
7bcf6a2c 2006 if (likely(do_signal_stop(info->si_signo))) {
1da177e4
LT
2007 /* It released the siglock. */
2008 goto relock;
2009 }
2010
2011 /*
2012 * We didn't actually stop, due to a race
2013 * with SIGCONT or something like that.
2014 */
2015 continue;
2016 }
2017
f6b76d4f 2018 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
2019
2020 /*
2021 * Anything else is fatal, maybe with a core dump.
2022 */
2023 current->flags |= PF_SIGNALED;
2dce81bf 2024
1da177e4 2025 if (sig_kernel_coredump(signr)) {
2dce81bf 2026 if (print_fatal_signals)
7bcf6a2c 2027 print_fatal_signal(regs, info->si_signo);
1da177e4
LT
2028 /*
2029 * If it was able to dump core, this kills all
2030 * other threads in the group and synchronizes with
2031 * their demise. If we lost the race with another
2032 * thread getting here, it set group_exit_code
2033 * first and our do_group_exit call below will use
2034 * that value and ignore the one we pass it.
2035 */
7bcf6a2c 2036 do_coredump(info->si_signo, info->si_signo, regs);
1da177e4
LT
2037 }
2038
2039 /*
2040 * Death signals, no core dump.
2041 */
7bcf6a2c 2042 do_group_exit(info->si_signo);
1da177e4
LT
2043 /* NOTREACHED */
2044 }
f6b76d4f 2045 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
2046 return signr;
2047}
2048
d12619b5
ON
2049void exit_signals(struct task_struct *tsk)
2050{
2051 int group_stop = 0;
5dee1707 2052 struct task_struct *t;
d12619b5 2053
5dee1707
ON
2054 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2055 tsk->flags |= PF_EXITING;
2056 return;
d12619b5
ON
2057 }
2058
5dee1707 2059 spin_lock_irq(&tsk->sighand->siglock);
d12619b5
ON
2060 /*
2061 * From now this task is not visible for group-wide signals,
2062 * see wants_signal(), do_signal_stop().
2063 */
2064 tsk->flags |= PF_EXITING;
5dee1707
ON
2065 if (!signal_pending(tsk))
2066 goto out;
2067
2068 /* It could be that __group_complete_signal() choose us to
2069 * notify about group-wide signal. Another thread should be
2070 * woken now to take the signal since we will not.
2071 */
2072 for (t = tsk; (t = next_thread(t)) != tsk; )
2073 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2074 recalc_sigpending_and_wake(t);
2075
2076 if (unlikely(tsk->signal->group_stop_count) &&
e5c1902e 2077 task_participate_group_stop(tsk))
edf2ed15 2078 group_stop = CLD_STOPPED;
5dee1707 2079out:
d12619b5
ON
2080 spin_unlock_irq(&tsk->sighand->siglock);
2081
ae6d2ed7 2082 if (unlikely(group_stop)) {
d12619b5 2083 read_lock(&tasklist_lock);
ae6d2ed7 2084 do_notify_parent_cldstop(tsk, group_stop);
d12619b5
ON
2085 read_unlock(&tasklist_lock);
2086 }
2087}
2088
1da177e4
LT
2089EXPORT_SYMBOL(recalc_sigpending);
2090EXPORT_SYMBOL_GPL(dequeue_signal);
2091EXPORT_SYMBOL(flush_signals);
2092EXPORT_SYMBOL(force_sig);
1da177e4
LT
2093EXPORT_SYMBOL(send_sig);
2094EXPORT_SYMBOL(send_sig_info);
2095EXPORT_SYMBOL(sigprocmask);
2096EXPORT_SYMBOL(block_all_signals);
2097EXPORT_SYMBOL(unblock_all_signals);
2098
2099
2100/*
2101 * System call entry points.
2102 */
2103
754fe8d2 2104SYSCALL_DEFINE0(restart_syscall)
1da177e4
LT
2105{
2106 struct restart_block *restart = &current_thread_info()->restart_block;
2107 return restart->fn(restart);
2108}
2109
2110long do_no_restart_syscall(struct restart_block *param)
2111{
2112 return -EINTR;
2113}
2114
2115/*
2116 * We don't need to get the kernel lock - this is all local to this
2117 * particular thread.. (and that's good, because this is _heavily_
2118 * used by various programs)
2119 */
2120
2121/*
2122 * This is also useful for kernel threads that want to temporarily
2123 * (or permanently) block certain signals.
2124 *
2125 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2126 * interface happily blocks "unblockable" signals like SIGKILL
2127 * and friends.
2128 */
2129int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2130{
2131 int error;
1da177e4
LT
2132
2133 spin_lock_irq(&current->sighand->siglock);
a26fd335
ON
2134 if (oldset)
2135 *oldset = current->blocked;
2136
1da177e4
LT
2137 error = 0;
2138 switch (how) {
2139 case SIG_BLOCK:
2140 sigorsets(&current->blocked, &current->blocked, set);
2141 break;
2142 case SIG_UNBLOCK:
2143 signandsets(&current->blocked, &current->blocked, set);
2144 break;
2145 case SIG_SETMASK:
2146 current->blocked = *set;
2147 break;
2148 default:
2149 error = -EINVAL;
2150 }
2151 recalc_sigpending();
2152 spin_unlock_irq(&current->sighand->siglock);
a26fd335 2153
1da177e4
LT
2154 return error;
2155}
2156
17da2bd9
HC
2157SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2158 sigset_t __user *, oset, size_t, sigsetsize)
1da177e4
LT
2159{
2160 int error = -EINVAL;
2161 sigset_t old_set, new_set;
2162
2163 /* XXX: Don't preclude handling different sized sigset_t's. */
2164 if (sigsetsize != sizeof(sigset_t))
2165 goto out;
2166
2167 if (set) {
2168 error = -EFAULT;
2169 if (copy_from_user(&new_set, set, sizeof(*set)))
2170 goto out;
2171 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2172
2173 error = sigprocmask(how, &new_set, &old_set);
2174 if (error)
2175 goto out;
2176 if (oset)
2177 goto set_old;
2178 } else if (oset) {
2179 spin_lock_irq(&current->sighand->siglock);
2180 old_set = current->blocked;
2181 spin_unlock_irq(&current->sighand->siglock);
2182
2183 set_old:
2184 error = -EFAULT;
2185 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2186 goto out;
2187 }
2188 error = 0;
2189out:
2190 return error;
2191}
2192
2193long do_sigpending(void __user *set, unsigned long sigsetsize)
2194{
2195 long error = -EINVAL;
2196 sigset_t pending;
2197
2198 if (sigsetsize > sizeof(sigset_t))
2199 goto out;
2200
2201 spin_lock_irq(&current->sighand->siglock);
2202 sigorsets(&pending, &current->pending.signal,
2203 &current->signal->shared_pending.signal);
2204 spin_unlock_irq(&current->sighand->siglock);
2205
2206 /* Outside the lock because only this thread touches it. */
2207 sigandsets(&pending, &current->blocked, &pending);
2208
2209 error = -EFAULT;
2210 if (!copy_to_user(set, &pending, sigsetsize))
2211 error = 0;
2212
2213out:
2214 return error;
2215}
2216
17da2bd9 2217SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
1da177e4
LT
2218{
2219 return do_sigpending(set, sigsetsize);
2220}
2221
2222#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2223
2224int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2225{
2226 int err;
2227
2228 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2229 return -EFAULT;
2230 if (from->si_code < 0)
2231 return __copy_to_user(to, from, sizeof(siginfo_t))
2232 ? -EFAULT : 0;
2233 /*
2234 * If you change siginfo_t structure, please be sure
2235 * this code is fixed accordingly.
fba2afaa
DL
2236 * Please remember to update the signalfd_copyinfo() function
2237 * inside fs/signalfd.c too, in case siginfo_t changes.
1da177e4
LT
2238 * It should never copy any pad contained in the structure
2239 * to avoid security leaks, but must copy the generic
2240 * 3 ints plus the relevant union member.
2241 */
2242 err = __put_user(from->si_signo, &to->si_signo);
2243 err |= __put_user(from->si_errno, &to->si_errno);
2244 err |= __put_user((short)from->si_code, &to->si_code);
2245 switch (from->si_code & __SI_MASK) {
2246 case __SI_KILL:
2247 err |= __put_user(from->si_pid, &to->si_pid);
2248 err |= __put_user(from->si_uid, &to->si_uid);
2249 break;
2250 case __SI_TIMER:
2251 err |= __put_user(from->si_tid, &to->si_tid);
2252 err |= __put_user(from->si_overrun, &to->si_overrun);
2253 err |= __put_user(from->si_ptr, &to->si_ptr);
2254 break;
2255 case __SI_POLL:
2256 err |= __put_user(from->si_band, &to->si_band);
2257 err |= __put_user(from->si_fd, &to->si_fd);
2258 break;
2259 case __SI_FAULT:
2260 err |= __put_user(from->si_addr, &to->si_addr);
2261#ifdef __ARCH_SI_TRAPNO
2262 err |= __put_user(from->si_trapno, &to->si_trapno);
a337fdac
AK
2263#endif
2264#ifdef BUS_MCEERR_AO
2265 /*
2266 * Other callers might not initialize the si_lsb field,
2267 * so check explicitely for the right codes here.
2268 */
2269 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2270 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
1da177e4
LT
2271#endif
2272 break;
2273 case __SI_CHLD:
2274 err |= __put_user(from->si_pid, &to->si_pid);
2275 err |= __put_user(from->si_uid, &to->si_uid);
2276 err |= __put_user(from->si_status, &to->si_status);
2277 err |= __put_user(from->si_utime, &to->si_utime);
2278 err |= __put_user(from->si_stime, &to->si_stime);
2279 break;
2280 case __SI_RT: /* This is not generated by the kernel as of now. */
2281 case __SI_MESGQ: /* But this is */
2282 err |= __put_user(from->si_pid, &to->si_pid);
2283 err |= __put_user(from->si_uid, &to->si_uid);
2284 err |= __put_user(from->si_ptr, &to->si_ptr);
2285 break;
2286 default: /* this is just in case for now ... */
2287 err |= __put_user(from->si_pid, &to->si_pid);
2288 err |= __put_user(from->si_uid, &to->si_uid);
2289 break;
2290 }
2291 return err;
2292}
2293
2294#endif
2295
17da2bd9
HC
2296SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2297 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2298 size_t, sigsetsize)
1da177e4
LT
2299{
2300 int ret, sig;
2301 sigset_t these;
2302 struct timespec ts;
2303 siginfo_t info;
2304 long timeout = 0;
2305
2306 /* XXX: Don't preclude handling different sized sigset_t's. */
2307 if (sigsetsize != sizeof(sigset_t))
2308 return -EINVAL;
2309
2310 if (copy_from_user(&these, uthese, sizeof(these)))
2311 return -EFAULT;
2312
2313 /*
2314 * Invert the set of allowed signals to get those we
2315 * want to block.
2316 */
2317 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2318 signotset(&these);
2319
2320 if (uts) {
2321 if (copy_from_user(&ts, uts, sizeof(ts)))
2322 return -EFAULT;
2323 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2324 || ts.tv_sec < 0)
2325 return -EINVAL;
2326 }
2327
2328 spin_lock_irq(&current->sighand->siglock);
2329 sig = dequeue_signal(current, &these, &info);
2330 if (!sig) {
2331 timeout = MAX_SCHEDULE_TIMEOUT;
2332 if (uts)
2333 timeout = (timespec_to_jiffies(&ts)
2334 + (ts.tv_sec || ts.tv_nsec));
2335
2336 if (timeout) {
2337 /* None ready -- temporarily unblock those we're
2338 * interested while we are sleeping in so that we'll
2339 * be awakened when they arrive. */
2340 current->real_blocked = current->blocked;
2341 sigandsets(&current->blocked, &current->blocked, &these);
2342 recalc_sigpending();
2343 spin_unlock_irq(&current->sighand->siglock);
2344
75bcc8c5 2345 timeout = schedule_timeout_interruptible(timeout);
1da177e4 2346
1da177e4
LT
2347 spin_lock_irq(&current->sighand->siglock);
2348 sig = dequeue_signal(current, &these, &info);
2349 current->blocked = current->real_blocked;
2350 siginitset(&current->real_blocked, 0);
2351 recalc_sigpending();
2352 }
2353 }
2354 spin_unlock_irq(&current->sighand->siglock);
2355
2356 if (sig) {
2357 ret = sig;
2358 if (uinfo) {
2359 if (copy_siginfo_to_user(uinfo, &info))
2360 ret = -EFAULT;
2361 }
2362 } else {
2363 ret = -EAGAIN;
2364 if (timeout)
2365 ret = -EINTR;
2366 }
2367
2368 return ret;
2369}
2370
17da2bd9 2371SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
1da177e4
LT
2372{
2373 struct siginfo info;
2374
2375 info.si_signo = sig;
2376 info.si_errno = 0;
2377 info.si_code = SI_USER;
b488893a 2378 info.si_pid = task_tgid_vnr(current);
76aac0e9 2379 info.si_uid = current_uid();
1da177e4
LT
2380
2381 return kill_something_info(sig, &info, pid);
2382}
2383
30b4ae8a
TG
2384static int
2385do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
1da177e4 2386{
1da177e4 2387 struct task_struct *p;
30b4ae8a 2388 int error = -ESRCH;
1da177e4 2389
3547ff3a 2390 rcu_read_lock();
228ebcbe 2391 p = find_task_by_vpid(pid);
b488893a 2392 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
30b4ae8a 2393 error = check_kill_permission(sig, info, p);
1da177e4
LT
2394 /*
2395 * The null signal is a permissions and process existence
2396 * probe. No signal is actually delivered.
2397 */
4a30debf
ON
2398 if (!error && sig) {
2399 error = do_send_sig_info(sig, info, p, false);
2400 /*
2401 * If lock_task_sighand() failed we pretend the task
2402 * dies after receiving the signal. The window is tiny,
2403 * and the signal is private anyway.
2404 */
2405 if (unlikely(error == -ESRCH))
2406 error = 0;
1da177e4
LT
2407 }
2408 }
3547ff3a 2409 rcu_read_unlock();
6dd69f10 2410
1da177e4
LT
2411 return error;
2412}
2413
30b4ae8a
TG
2414static int do_tkill(pid_t tgid, pid_t pid, int sig)
2415{
2416 struct siginfo info;
2417
2418 info.si_signo = sig;
2419 info.si_errno = 0;
2420 info.si_code = SI_TKILL;
2421 info.si_pid = task_tgid_vnr(current);
2422 info.si_uid = current_uid();
2423
2424 return do_send_specific(tgid, pid, sig, &info);
2425}
2426
6dd69f10
VL
2427/**
2428 * sys_tgkill - send signal to one specific thread
2429 * @tgid: the thread group ID of the thread
2430 * @pid: the PID of the thread
2431 * @sig: signal to be sent
2432 *
72fd4a35 2433 * This syscall also checks the @tgid and returns -ESRCH even if the PID
6dd69f10
VL
2434 * exists but it's not belonging to the target process anymore. This
2435 * method solves the problem of threads exiting and PIDs getting reused.
2436 */
a5f8fa9e 2437SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
6dd69f10
VL
2438{
2439 /* This is only valid for single tasks */
2440 if (pid <= 0 || tgid <= 0)
2441 return -EINVAL;
2442
2443 return do_tkill(tgid, pid, sig);
2444}
2445
1da177e4
LT
2446/*
2447 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2448 */
a5f8fa9e 2449SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
1da177e4 2450{
1da177e4
LT
2451 /* This is only valid for single tasks */
2452 if (pid <= 0)
2453 return -EINVAL;
2454
6dd69f10 2455 return do_tkill(0, pid, sig);
1da177e4
LT
2456}
2457
a5f8fa9e
HC
2458SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2459 siginfo_t __user *, uinfo)
1da177e4
LT
2460{
2461 siginfo_t info;
2462
2463 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2464 return -EFAULT;
2465
2466 /* Not even root can pretend to send signals from the kernel.
da48524e
JT
2467 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2468 */
2469 if (info.si_code != SI_QUEUE) {
2470 /* We used to allow any < 0 si_code */
2471 WARN_ON_ONCE(info.si_code < 0);
1da177e4 2472 return -EPERM;
da48524e 2473 }
1da177e4
LT
2474 info.si_signo = sig;
2475
2476 /* POSIX.1b doesn't mention process groups. */
2477 return kill_proc_info(sig, &info, pid);
2478}
2479
62ab4505
TG
2480long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2481{
2482 /* This is only valid for single tasks */
2483 if (pid <= 0 || tgid <= 0)
2484 return -EINVAL;
2485
2486 /* Not even root can pretend to send signals from the kernel.
da48524e
JT
2487 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2488 */
2489 if (info->si_code != SI_QUEUE) {
2490 /* We used to allow any < 0 si_code */
2491 WARN_ON_ONCE(info->si_code < 0);
62ab4505 2492 return -EPERM;
da48524e 2493 }
62ab4505
TG
2494 info->si_signo = sig;
2495
2496 return do_send_specific(tgid, pid, sig, info);
2497}
2498
2499SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2500 siginfo_t __user *, uinfo)
2501{
2502 siginfo_t info;
2503
2504 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2505 return -EFAULT;
2506
2507 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2508}
2509
88531f72 2510int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
1da177e4 2511{
93585eea 2512 struct task_struct *t = current;
1da177e4 2513 struct k_sigaction *k;
71fabd5e 2514 sigset_t mask;
1da177e4 2515
7ed20e1a 2516 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
1da177e4
LT
2517 return -EINVAL;
2518
93585eea 2519 k = &t->sighand->action[sig-1];
1da177e4
LT
2520
2521 spin_lock_irq(&current->sighand->siglock);
1da177e4
LT
2522 if (oact)
2523 *oact = *k;
2524
2525 if (act) {
9ac95f2f
ON
2526 sigdelsetmask(&act->sa.sa_mask,
2527 sigmask(SIGKILL) | sigmask(SIGSTOP));
88531f72 2528 *k = *act;
1da177e4
LT
2529 /*
2530 * POSIX 3.3.1.3:
2531 * "Setting a signal action to SIG_IGN for a signal that is
2532 * pending shall cause the pending signal to be discarded,
2533 * whether or not it is blocked."
2534 *
2535 * "Setting a signal action to SIG_DFL for a signal that is
2536 * pending and whose default action is to ignore the signal
2537 * (for example, SIGCHLD), shall cause the pending signal to
2538 * be discarded, whether or not it is blocked"
2539 */
35de254d 2540 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
71fabd5e
GA
2541 sigemptyset(&mask);
2542 sigaddset(&mask, sig);
2543 rm_from_queue_full(&mask, &t->signal->shared_pending);
1da177e4 2544 do {
71fabd5e 2545 rm_from_queue_full(&mask, &t->pending);
1da177e4
LT
2546 t = next_thread(t);
2547 } while (t != current);
1da177e4 2548 }
1da177e4
LT
2549 }
2550
2551 spin_unlock_irq(&current->sighand->siglock);
2552 return 0;
2553}
2554
2555int
2556do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2557{
2558 stack_t oss;
2559 int error;
2560
0083fc2c
LT
2561 oss.ss_sp = (void __user *) current->sas_ss_sp;
2562 oss.ss_size = current->sas_ss_size;
2563 oss.ss_flags = sas_ss_flags(sp);
1da177e4
LT
2564
2565 if (uss) {
2566 void __user *ss_sp;
2567 size_t ss_size;
2568 int ss_flags;
2569
2570 error = -EFAULT;
0dd8486b
LT
2571 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2572 goto out;
2573 error = __get_user(ss_sp, &uss->ss_sp) |
2574 __get_user(ss_flags, &uss->ss_flags) |
2575 __get_user(ss_size, &uss->ss_size);
2576 if (error)
1da177e4
LT
2577 goto out;
2578
2579 error = -EPERM;
2580 if (on_sig_stack(sp))
2581 goto out;
2582
2583 error = -EINVAL;
2584 /*
2585 *
2586 * Note - this code used to test ss_flags incorrectly
2587 * old code may have been written using ss_flags==0
2588 * to mean ss_flags==SS_ONSTACK (as this was the only
2589 * way that worked) - this fix preserves that older
2590 * mechanism
2591 */
2592 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2593 goto out;
2594
2595 if (ss_flags == SS_DISABLE) {
2596 ss_size = 0;
2597 ss_sp = NULL;
2598 } else {
2599 error = -ENOMEM;
2600 if (ss_size < MINSIGSTKSZ)
2601 goto out;
2602 }
2603
2604 current->sas_ss_sp = (unsigned long) ss_sp;
2605 current->sas_ss_size = ss_size;
2606 }
2607
0083fc2c 2608 error = 0;
1da177e4
LT
2609 if (uoss) {
2610 error = -EFAULT;
0083fc2c 2611 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
1da177e4 2612 goto out;
0083fc2c
LT
2613 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2614 __put_user(oss.ss_size, &uoss->ss_size) |
2615 __put_user(oss.ss_flags, &uoss->ss_flags);
1da177e4
LT
2616 }
2617
1da177e4
LT
2618out:
2619 return error;
2620}
2621
2622#ifdef __ARCH_WANT_SYS_SIGPENDING
2623
b290ebe2 2624SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
1da177e4
LT
2625{
2626 return do_sigpending(set, sizeof(*set));
2627}
2628
2629#endif
2630
2631#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2632/* Some platforms have their own version with special arguments others
2633 support only sys_rt_sigprocmask. */
2634
b290ebe2
HC
2635SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2636 old_sigset_t __user *, oset)
1da177e4
LT
2637{
2638 int error;
2639 old_sigset_t old_set, new_set;
2640
2641 if (set) {
2642 error = -EFAULT;
2643 if (copy_from_user(&new_set, set, sizeof(*set)))
2644 goto out;
2645 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2646
2647 spin_lock_irq(&current->sighand->siglock);
2648 old_set = current->blocked.sig[0];
2649
2650 error = 0;
2651 switch (how) {
2652 default:
2653 error = -EINVAL;
2654 break;
2655 case SIG_BLOCK:
2656 sigaddsetmask(&current->blocked, new_set);
2657 break;
2658 case SIG_UNBLOCK:
2659 sigdelsetmask(&current->blocked, new_set);
2660 break;
2661 case SIG_SETMASK:
2662 current->blocked.sig[0] = new_set;
2663 break;
2664 }
2665
2666 recalc_sigpending();
2667 spin_unlock_irq(&current->sighand->siglock);
2668 if (error)
2669 goto out;
2670 if (oset)
2671 goto set_old;
2672 } else if (oset) {
2673 old_set = current->blocked.sig[0];
2674 set_old:
2675 error = -EFAULT;
2676 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2677 goto out;
2678 }
2679 error = 0;
2680out:
2681 return error;
2682}
2683#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2684
2685#ifdef __ARCH_WANT_SYS_RT_SIGACTION
d4e82042
HC
2686SYSCALL_DEFINE4(rt_sigaction, int, sig,
2687 const struct sigaction __user *, act,
2688 struct sigaction __user *, oact,
2689 size_t, sigsetsize)
1da177e4
LT
2690{
2691 struct k_sigaction new_sa, old_sa;
2692 int ret = -EINVAL;
2693
2694 /* XXX: Don't preclude handling different sized sigset_t's. */
2695 if (sigsetsize != sizeof(sigset_t))
2696 goto out;
2697
2698 if (act) {
2699 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2700 return -EFAULT;
2701 }
2702
2703 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2704
2705 if (!ret && oact) {
2706 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2707 return -EFAULT;
2708 }
2709out:
2710 return ret;
2711}
2712#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2713
2714#ifdef __ARCH_WANT_SYS_SGETMASK
2715
2716/*
2717 * For backwards compatibility. Functionality superseded by sigprocmask.
2718 */
a5f8fa9e 2719SYSCALL_DEFINE0(sgetmask)
1da177e4
LT
2720{
2721 /* SMP safe */
2722 return current->blocked.sig[0];
2723}
2724
a5f8fa9e 2725SYSCALL_DEFINE1(ssetmask, int, newmask)
1da177e4
LT
2726{
2727 int old;
2728
2729 spin_lock_irq(&current->sighand->siglock);
2730 old = current->blocked.sig[0];
2731
2732 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2733 sigmask(SIGSTOP)));
2734 recalc_sigpending();
2735 spin_unlock_irq(&current->sighand->siglock);
2736
2737 return old;
2738}
2739#endif /* __ARCH_WANT_SGETMASK */
2740
2741#ifdef __ARCH_WANT_SYS_SIGNAL
2742/*
2743 * For backwards compatibility. Functionality superseded by sigaction.
2744 */
a5f8fa9e 2745SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
1da177e4
LT
2746{
2747 struct k_sigaction new_sa, old_sa;
2748 int ret;
2749
2750 new_sa.sa.sa_handler = handler;
2751 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
c70d3d70 2752 sigemptyset(&new_sa.sa.sa_mask);
1da177e4
LT
2753
2754 ret = do_sigaction(sig, &new_sa, &old_sa);
2755
2756 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2757}
2758#endif /* __ARCH_WANT_SYS_SIGNAL */
2759
2760#ifdef __ARCH_WANT_SYS_PAUSE
2761
a5f8fa9e 2762SYSCALL_DEFINE0(pause)
1da177e4
LT
2763{
2764 current->state = TASK_INTERRUPTIBLE;
2765 schedule();
2766 return -ERESTARTNOHAND;
2767}
2768
2769#endif
2770
150256d8 2771#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
d4e82042 2772SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
150256d8
DW
2773{
2774 sigset_t newset;
2775
2776 /* XXX: Don't preclude handling different sized sigset_t's. */
2777 if (sigsetsize != sizeof(sigset_t))
2778 return -EINVAL;
2779
2780 if (copy_from_user(&newset, unewset, sizeof(newset)))
2781 return -EFAULT;
2782 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2783
2784 spin_lock_irq(&current->sighand->siglock);
2785 current->saved_sigmask = current->blocked;
2786 current->blocked = newset;
2787 recalc_sigpending();
2788 spin_unlock_irq(&current->sighand->siglock);
2789
2790 current->state = TASK_INTERRUPTIBLE;
2791 schedule();
4e4c22c7 2792 set_restore_sigmask();
150256d8
DW
2793 return -ERESTARTNOHAND;
2794}
2795#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2796
f269fdd1
DH
2797__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2798{
2799 return NULL;
2800}
2801
1da177e4
LT
2802void __init signals_init(void)
2803{
0a31bd5f 2804 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
1da177e4 2805}
67fc4e0c
JW
2806
2807#ifdef CONFIG_KGDB_KDB
2808#include <linux/kdb.h>
2809/*
2810 * kdb_send_sig_info - Allows kdb to send signals without exposing
2811 * signal internals. This function checks if the required locks are
2812 * available before calling the main signal code, to avoid kdb
2813 * deadlocks.
2814 */
2815void
2816kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
2817{
2818 static struct task_struct *kdb_prev_t;
2819 int sig, new_t;
2820 if (!spin_trylock(&t->sighand->siglock)) {
2821 kdb_printf("Can't do kill command now.\n"
2822 "The sigmask lock is held somewhere else in "
2823 "kernel, try again later\n");
2824 return;
2825 }
2826 spin_unlock(&t->sighand->siglock);
2827 new_t = kdb_prev_t != t;
2828 kdb_prev_t = t;
2829 if (t->state != TASK_RUNNING && new_t) {
2830 kdb_printf("Process is not RUNNING, sending a signal from "
2831 "kdb risks deadlock\n"
2832 "on the run queue locks. "
2833 "The signal has _not_ been sent.\n"
2834 "Reissue the kill command if you want to risk "
2835 "the deadlock.\n");
2836 return;
2837 }
2838 sig = info->si_signo;
2839 if (send_sig_info(sig, info, t))
2840 kdb_printf("Fail to deliver Signal %d to process %d.\n",
2841 sig, t->pid);
2842 else
2843 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
2844}
2845#endif /* CONFIG_KGDB_KDB */