GNU Linux-libre 4.19.242-gnu1
[releases.git] / kernel / signal.c
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
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/sched/mm.h>
17 #include <linux/sched/user.h>
18 #include <linux/sched/debug.h>
19 #include <linux/sched/task.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/sched/cputime.h>
22 #include <linux/fs.h>
23 #include <linux/tty.h>
24 #include <linux/binfmts.h>
25 #include <linux/coredump.h>
26 #include <linux/security.h>
27 #include <linux/syscalls.h>
28 #include <linux/ptrace.h>
29 #include <linux/signal.h>
30 #include <linux/signalfd.h>
31 #include <linux/ratelimit.h>
32 #include <linux/tracehook.h>
33 #include <linux/capability.h>
34 #include <linux/freezer.h>
35 #include <linux/pid_namespace.h>
36 #include <linux/nsproxy.h>
37 #include <linux/user_namespace.h>
38 #include <linux/uprobes.h>
39 #include <linux/compat.h>
40 #include <linux/cn_proc.h>
41 #include <linux/compiler.h>
42 #include <linux/posix-timers.h>
43 #include <linux/livepatch.h>
44
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/signal.h>
47
48 #include <asm/param.h>
49 #include <linux/uaccess.h>
50 #include <asm/unistd.h>
51 #include <asm/siginfo.h>
52 #include <asm/cacheflush.h>
53 #include "audit.h"      /* audit_signal_info() */
54
55 /*
56  * SLAB caches for signal bits.
57  */
58
59 static struct kmem_cache *sigqueue_cachep;
60
61 int print_fatal_signals __read_mostly;
62
63 static void __user *sig_handler(struct task_struct *t, int sig)
64 {
65         return t->sighand->action[sig - 1].sa.sa_handler;
66 }
67
68 static inline bool sig_handler_ignored(void __user *handler, int sig)
69 {
70         /* Is it explicitly or implicitly ignored? */
71         return handler == SIG_IGN ||
72                (handler == SIG_DFL && sig_kernel_ignore(sig));
73 }
74
75 static bool sig_task_ignored(struct task_struct *t, int sig, bool force)
76 {
77         void __user *handler;
78
79         handler = sig_handler(t, sig);
80
81         /* SIGKILL and SIGSTOP may not be sent to the global init */
82         if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
83                 return true;
84
85         if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
86             handler == SIG_DFL && !(force && sig_kernel_only(sig)))
87                 return true;
88
89         /* Only allow kernel generated signals to this kthread */
90         if (unlikely((t->flags & PF_KTHREAD) &&
91                      (handler == SIG_KTHREAD_KERNEL) && !force))
92                 return true;
93
94         return sig_handler_ignored(handler, sig);
95 }
96
97 static bool sig_ignored(struct task_struct *t, int sig, bool force)
98 {
99         /*
100          * Blocked signals are never ignored, since the
101          * signal handler may change by the time it is
102          * unblocked.
103          */
104         if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
105                 return false;
106
107         /*
108          * Tracers may want to know about even ignored signal unless it
109          * is SIGKILL which can't be reported anyway but can be ignored
110          * by SIGNAL_UNKILLABLE task.
111          */
112         if (t->ptrace && sig != SIGKILL)
113                 return false;
114
115         return sig_task_ignored(t, sig, force);
116 }
117
118 /*
119  * Re-calculate pending state from the set of locally pending
120  * signals, globally pending signals, and blocked signals.
121  */
122 static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked)
123 {
124         unsigned long ready;
125         long i;
126
127         switch (_NSIG_WORDS) {
128         default:
129                 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
130                         ready |= signal->sig[i] &~ blocked->sig[i];
131                 break;
132
133         case 4: ready  = signal->sig[3] &~ blocked->sig[3];
134                 ready |= signal->sig[2] &~ blocked->sig[2];
135                 ready |= signal->sig[1] &~ blocked->sig[1];
136                 ready |= signal->sig[0] &~ blocked->sig[0];
137                 break;
138
139         case 2: ready  = signal->sig[1] &~ blocked->sig[1];
140                 ready |= signal->sig[0] &~ blocked->sig[0];
141                 break;
142
143         case 1: ready  = signal->sig[0] &~ blocked->sig[0];
144         }
145         return ready != 0;
146 }
147
148 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
149
150 static bool recalc_sigpending_tsk(struct task_struct *t)
151 {
152         if ((t->jobctl & JOBCTL_PENDING_MASK) ||
153             PENDING(&t->pending, &t->blocked) ||
154             PENDING(&t->signal->shared_pending, &t->blocked)) {
155                 set_tsk_thread_flag(t, TIF_SIGPENDING);
156                 return true;
157         }
158
159         /*
160          * We must never clear the flag in another thread, or in current
161          * when it's possible the current syscall is returning -ERESTART*.
162          * So we don't clear it here, and only callers who know they should do.
163          */
164         return false;
165 }
166
167 /*
168  * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
169  * This is superfluous when called on current, the wakeup is a harmless no-op.
170  */
171 void recalc_sigpending_and_wake(struct task_struct *t)
172 {
173         if (recalc_sigpending_tsk(t))
174                 signal_wake_up(t, 0);
175 }
176
177 void recalc_sigpending(void)
178 {
179         if (!recalc_sigpending_tsk(current) && !freezing(current) &&
180             !klp_patch_pending(current))
181                 clear_thread_flag(TIF_SIGPENDING);
182
183 }
184
185 void calculate_sigpending(void)
186 {
187         /* Have any signals or users of TIF_SIGPENDING been delayed
188          * until after fork?
189          */
190         spin_lock_irq(&current->sighand->siglock);
191         set_tsk_thread_flag(current, TIF_SIGPENDING);
192         recalc_sigpending();
193         spin_unlock_irq(&current->sighand->siglock);
194 }
195
196 /* Given the mask, find the first available signal that should be serviced. */
197
198 #define SYNCHRONOUS_MASK \
199         (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
200          sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
201
202 int next_signal(struct sigpending *pending, sigset_t *mask)
203 {
204         unsigned long i, *s, *m, x;
205         int sig = 0;
206
207         s = pending->signal.sig;
208         m = mask->sig;
209
210         /*
211          * Handle the first word specially: it contains the
212          * synchronous signals that need to be dequeued first.
213          */
214         x = *s &~ *m;
215         if (x) {
216                 if (x & SYNCHRONOUS_MASK)
217                         x &= SYNCHRONOUS_MASK;
218                 sig = ffz(~x) + 1;
219                 return sig;
220         }
221
222         switch (_NSIG_WORDS) {
223         default:
224                 for (i = 1; i < _NSIG_WORDS; ++i) {
225                         x = *++s &~ *++m;
226                         if (!x)
227                                 continue;
228                         sig = ffz(~x) + i*_NSIG_BPW + 1;
229                         break;
230                 }
231                 break;
232
233         case 2:
234                 x = s[1] &~ m[1];
235                 if (!x)
236                         break;
237                 sig = ffz(~x) + _NSIG_BPW + 1;
238                 break;
239
240         case 1:
241                 /* Nothing to do */
242                 break;
243         }
244
245         return sig;
246 }
247
248 static inline void print_dropped_signal(int sig)
249 {
250         static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
251
252         if (!print_fatal_signals)
253                 return;
254
255         if (!__ratelimit(&ratelimit_state))
256                 return;
257
258         pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
259                                 current->comm, current->pid, sig);
260 }
261
262 /**
263  * task_set_jobctl_pending - set jobctl pending bits
264  * @task: target task
265  * @mask: pending bits to set
266  *
267  * Clear @mask from @task->jobctl.  @mask must be subset of
268  * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
269  * %JOBCTL_TRAPPING.  If stop signo is being set, the existing signo is
270  * cleared.  If @task is already being killed or exiting, this function
271  * becomes noop.
272  *
273  * CONTEXT:
274  * Must be called with @task->sighand->siglock held.
275  *
276  * RETURNS:
277  * %true if @mask is set, %false if made noop because @task was dying.
278  */
279 bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
280 {
281         BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
282                         JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
283         BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
284
285         if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
286                 return false;
287
288         if (mask & JOBCTL_STOP_SIGMASK)
289                 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
290
291         task->jobctl |= mask;
292         return true;
293 }
294
295 /**
296  * task_clear_jobctl_trapping - clear jobctl trapping bit
297  * @task: target task
298  *
299  * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
300  * Clear it and wake up the ptracer.  Note that we don't need any further
301  * locking.  @task->siglock guarantees that @task->parent points to the
302  * ptracer.
303  *
304  * CONTEXT:
305  * Must be called with @task->sighand->siglock held.
306  */
307 void task_clear_jobctl_trapping(struct task_struct *task)
308 {
309         if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
310                 task->jobctl &= ~JOBCTL_TRAPPING;
311                 smp_mb();       /* advised by wake_up_bit() */
312                 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
313         }
314 }
315
316 /**
317  * task_clear_jobctl_pending - clear jobctl pending bits
318  * @task: target task
319  * @mask: pending bits to clear
320  *
321  * Clear @mask from @task->jobctl.  @mask must be subset of
322  * %JOBCTL_PENDING_MASK.  If %JOBCTL_STOP_PENDING is being cleared, other
323  * STOP bits are cleared together.
324  *
325  * If clearing of @mask leaves no stop or trap pending, this function calls
326  * task_clear_jobctl_trapping().
327  *
328  * CONTEXT:
329  * Must be called with @task->sighand->siglock held.
330  */
331 void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
332 {
333         BUG_ON(mask & ~JOBCTL_PENDING_MASK);
334
335         if (mask & JOBCTL_STOP_PENDING)
336                 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
337
338         task->jobctl &= ~mask;
339
340         if (!(task->jobctl & JOBCTL_PENDING_MASK))
341                 task_clear_jobctl_trapping(task);
342 }
343
344 /**
345  * task_participate_group_stop - participate in a group stop
346  * @task: task participating in a group stop
347  *
348  * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
349  * Group stop states are cleared and the group stop count is consumed if
350  * %JOBCTL_STOP_CONSUME was set.  If the consumption completes the group
351  * stop, the appropriate %SIGNAL_* flags are set.
352  *
353  * CONTEXT:
354  * Must be called with @task->sighand->siglock held.
355  *
356  * RETURNS:
357  * %true if group stop completion should be notified to the parent, %false
358  * otherwise.
359  */
360 static bool task_participate_group_stop(struct task_struct *task)
361 {
362         struct signal_struct *sig = task->signal;
363         bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
364
365         WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
366
367         task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
368
369         if (!consume)
370                 return false;
371
372         if (!WARN_ON_ONCE(sig->group_stop_count == 0))
373                 sig->group_stop_count--;
374
375         /*
376          * Tell the caller to notify completion iff we are entering into a
377          * fresh group stop.  Read comment in do_signal_stop() for details.
378          */
379         if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
380                 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
381                 return true;
382         }
383         return false;
384 }
385
386 void task_join_group_stop(struct task_struct *task)
387 {
388         unsigned long mask = current->jobctl & JOBCTL_STOP_SIGMASK;
389         struct signal_struct *sig = current->signal;
390
391         if (sig->group_stop_count) {
392                 sig->group_stop_count++;
393                 mask |= JOBCTL_STOP_CONSUME;
394         } else if (!(sig->flags & SIGNAL_STOP_STOPPED))
395                 return;
396
397         /* Have the new thread join an on-going signal group stop */
398         task_set_jobctl_pending(task, mask | JOBCTL_STOP_PENDING);
399 }
400
401 /*
402  * allocate a new signal queue record
403  * - this may be called without locks if and only if t == current, otherwise an
404  *   appropriate lock must be held to stop the target task from exiting
405  */
406 static struct sigqueue *
407 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
408 {
409         struct sigqueue *q = NULL;
410         struct user_struct *user;
411         int sigpending;
412
413         /*
414          * Protect access to @t credentials. This can go away when all
415          * callers hold rcu read lock.
416          *
417          * NOTE! A pending signal will hold on to the user refcount,
418          * and we get/put the refcount only when the sigpending count
419          * changes from/to zero.
420          */
421         rcu_read_lock();
422         user = __task_cred(t)->user;
423         sigpending = atomic_inc_return(&user->sigpending);
424         if (sigpending == 1)
425                 get_uid(user);
426         rcu_read_unlock();
427
428         if (override_rlimit || likely(sigpending <= task_rlimit(t, RLIMIT_SIGPENDING))) {
429                 q = kmem_cache_alloc(sigqueue_cachep, flags);
430         } else {
431                 print_dropped_signal(sig);
432         }
433
434         if (unlikely(q == NULL)) {
435                 if (atomic_dec_and_test(&user->sigpending))
436                         free_uid(user);
437         } else {
438                 INIT_LIST_HEAD(&q->list);
439                 q->flags = 0;
440                 q->user = user;
441         }
442
443         return q;
444 }
445
446 static void __sigqueue_free(struct sigqueue *q)
447 {
448         if (q->flags & SIGQUEUE_PREALLOC)
449                 return;
450         if (atomic_dec_and_test(&q->user->sigpending))
451                 free_uid(q->user);
452         kmem_cache_free(sigqueue_cachep, q);
453 }
454
455 void flush_sigqueue(struct sigpending *queue)
456 {
457         struct sigqueue *q;
458
459         sigemptyset(&queue->signal);
460         while (!list_empty(&queue->list)) {
461                 q = list_entry(queue->list.next, struct sigqueue , list);
462                 list_del_init(&q->list);
463                 __sigqueue_free(q);
464         }
465 }
466
467 /*
468  * Flush all pending signals for this kthread.
469  */
470 void flush_signals(struct task_struct *t)
471 {
472         unsigned long flags;
473
474         spin_lock_irqsave(&t->sighand->siglock, flags);
475         clear_tsk_thread_flag(t, TIF_SIGPENDING);
476         flush_sigqueue(&t->pending);
477         flush_sigqueue(&t->signal->shared_pending);
478         spin_unlock_irqrestore(&t->sighand->siglock, flags);
479 }
480
481 #ifdef CONFIG_POSIX_TIMERS
482 static void __flush_itimer_signals(struct sigpending *pending)
483 {
484         sigset_t signal, retain;
485         struct sigqueue *q, *n;
486
487         signal = pending->signal;
488         sigemptyset(&retain);
489
490         list_for_each_entry_safe(q, n, &pending->list, list) {
491                 int sig = q->info.si_signo;
492
493                 if (likely(q->info.si_code != SI_TIMER)) {
494                         sigaddset(&retain, sig);
495                 } else {
496                         sigdelset(&signal, sig);
497                         list_del_init(&q->list);
498                         __sigqueue_free(q);
499                 }
500         }
501
502         sigorsets(&pending->signal, &signal, &retain);
503 }
504
505 void flush_itimer_signals(void)
506 {
507         struct task_struct *tsk = current;
508         unsigned long flags;
509
510         spin_lock_irqsave(&tsk->sighand->siglock, flags);
511         __flush_itimer_signals(&tsk->pending);
512         __flush_itimer_signals(&tsk->signal->shared_pending);
513         spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
514 }
515 #endif
516
517 void ignore_signals(struct task_struct *t)
518 {
519         int i;
520
521         for (i = 0; i < _NSIG; ++i)
522                 t->sighand->action[i].sa.sa_handler = SIG_IGN;
523
524         flush_signals(t);
525 }
526
527 /*
528  * Flush all handlers for a task.
529  */
530
531 void
532 flush_signal_handlers(struct task_struct *t, int force_default)
533 {
534         int i;
535         struct k_sigaction *ka = &t->sighand->action[0];
536         for (i = _NSIG ; i != 0 ; i--) {
537                 if (force_default || ka->sa.sa_handler != SIG_IGN)
538                         ka->sa.sa_handler = SIG_DFL;
539                 ka->sa.sa_flags = 0;
540 #ifdef __ARCH_HAS_SA_RESTORER
541                 ka->sa.sa_restorer = NULL;
542 #endif
543                 sigemptyset(&ka->sa.sa_mask);
544                 ka++;
545         }
546 }
547
548 bool unhandled_signal(struct task_struct *tsk, int sig)
549 {
550         void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
551         if (is_global_init(tsk))
552                 return true;
553
554         if (handler != SIG_IGN && handler != SIG_DFL)
555                 return false;
556
557         /* if ptraced, let the tracer determine */
558         return !tsk->ptrace;
559 }
560
561 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info,
562                            bool *resched_timer)
563 {
564         struct sigqueue *q, *first = NULL;
565
566         /*
567          * Collect the siginfo appropriate to this signal.  Check if
568          * there is another siginfo for the same signal.
569         */
570         list_for_each_entry(q, &list->list, list) {
571                 if (q->info.si_signo == sig) {
572                         if (first)
573                                 goto still_pending;
574                         first = q;
575                 }
576         }
577
578         sigdelset(&list->signal, sig);
579
580         if (first) {
581 still_pending:
582                 list_del_init(&first->list);
583                 copy_siginfo(info, &first->info);
584
585                 *resched_timer =
586                         (first->flags & SIGQUEUE_PREALLOC) &&
587                         (info->si_code == SI_TIMER) &&
588                         (info->si_sys_private);
589
590                 __sigqueue_free(first);
591         } else {
592                 /*
593                  * Ok, it wasn't in the queue.  This must be
594                  * a fast-pathed signal or we must have been
595                  * out of queue space.  So zero out the info.
596                  */
597                 clear_siginfo(info);
598                 info->si_signo = sig;
599                 info->si_errno = 0;
600                 info->si_code = SI_USER;
601                 info->si_pid = 0;
602                 info->si_uid = 0;
603         }
604 }
605
606 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
607                         siginfo_t *info, bool *resched_timer)
608 {
609         int sig = next_signal(pending, mask);
610
611         if (sig)
612                 collect_signal(sig, pending, info, resched_timer);
613         return sig;
614 }
615
616 /*
617  * Dequeue a signal and return the element to the caller, which is
618  * expected to free it.
619  *
620  * All callers have to hold the siglock.
621  */
622 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
623 {
624         bool resched_timer = false;
625         int signr;
626
627         /* We only dequeue private signals from ourselves, we don't let
628          * signalfd steal them
629          */
630         signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
631         if (!signr) {
632                 signr = __dequeue_signal(&tsk->signal->shared_pending,
633                                          mask, info, &resched_timer);
634 #ifdef CONFIG_POSIX_TIMERS
635                 /*
636                  * itimer signal ?
637                  *
638                  * itimers are process shared and we restart periodic
639                  * itimers in the signal delivery path to prevent DoS
640                  * attacks in the high resolution timer case. This is
641                  * compliant with the old way of self-restarting
642                  * itimers, as the SIGALRM is a legacy signal and only
643                  * queued once. Changing the restart behaviour to
644                  * restart the timer in the signal dequeue path is
645                  * reducing the timer noise on heavy loaded !highres
646                  * systems too.
647                  */
648                 if (unlikely(signr == SIGALRM)) {
649                         struct hrtimer *tmr = &tsk->signal->real_timer;
650
651                         if (!hrtimer_is_queued(tmr) &&
652                             tsk->signal->it_real_incr != 0) {
653                                 hrtimer_forward(tmr, tmr->base->get_time(),
654                                                 tsk->signal->it_real_incr);
655                                 hrtimer_restart(tmr);
656                         }
657                 }
658 #endif
659         }
660
661         recalc_sigpending();
662         if (!signr)
663                 return 0;
664
665         if (unlikely(sig_kernel_stop(signr))) {
666                 /*
667                  * Set a marker that we have dequeued a stop signal.  Our
668                  * caller might release the siglock and then the pending
669                  * stop signal it is about to process is no longer in the
670                  * pending bitmasks, but must still be cleared by a SIGCONT
671                  * (and overruled by a SIGKILL).  So those cases clear this
672                  * shared flag after we've set it.  Note that this flag may
673                  * remain set after the signal we return is ignored or
674                  * handled.  That doesn't matter because its only purpose
675                  * is to alert stop-signal processing code when another
676                  * processor has come along and cleared the flag.
677                  */
678                 current->jobctl |= JOBCTL_STOP_DEQUEUED;
679         }
680 #ifdef CONFIG_POSIX_TIMERS
681         if (resched_timer) {
682                 /*
683                  * Release the siglock to ensure proper locking order
684                  * of timer locks outside of siglocks.  Note, we leave
685                  * irqs disabled here, since the posix-timers code is
686                  * about to disable them again anyway.
687                  */
688                 spin_unlock(&tsk->sighand->siglock);
689                 posixtimer_rearm(info);
690                 spin_lock(&tsk->sighand->siglock);
691
692                 /* Don't expose the si_sys_private value to userspace */
693                 info->si_sys_private = 0;
694         }
695 #endif
696         return signr;
697 }
698
699 static int dequeue_synchronous_signal(siginfo_t *info)
700 {
701         struct task_struct *tsk = current;
702         struct sigpending *pending = &tsk->pending;
703         struct sigqueue *q, *sync = NULL;
704
705         /*
706          * Might a synchronous signal be in the queue?
707          */
708         if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
709                 return 0;
710
711         /*
712          * Return the first synchronous signal in the queue.
713          */
714         list_for_each_entry(q, &pending->list, list) {
715                 /* Synchronous signals have a postive si_code */
716                 if ((q->info.si_code > SI_USER) &&
717                     (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
718                         sync = q;
719                         goto next;
720                 }
721         }
722         return 0;
723 next:
724         /*
725          * Check if there is another siginfo for the same signal.
726          */
727         list_for_each_entry_continue(q, &pending->list, list) {
728                 if (q->info.si_signo == sync->info.si_signo)
729                         goto still_pending;
730         }
731
732         sigdelset(&pending->signal, sync->info.si_signo);
733         recalc_sigpending();
734 still_pending:
735         list_del_init(&sync->list);
736         copy_siginfo(info, &sync->info);
737         __sigqueue_free(sync);
738         return info->si_signo;
739 }
740
741 /*
742  * Tell a process that it has a new active signal..
743  *
744  * NOTE! we rely on the previous spin_lock to
745  * lock interrupts for us! We can only be called with
746  * "siglock" held, and the local interrupt must
747  * have been disabled when that got acquired!
748  *
749  * No need to set need_resched since signal event passing
750  * goes through ->blocked
751  */
752 void signal_wake_up_state(struct task_struct *t, unsigned int state)
753 {
754         set_tsk_thread_flag(t, TIF_SIGPENDING);
755         /*
756          * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
757          * case. We don't check t->state here because there is a race with it
758          * executing another processor and just now entering stopped state.
759          * By using wake_up_state, we ensure the process will wake up and
760          * handle its death signal.
761          */
762         if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
763                 kick_process(t);
764 }
765
766 /*
767  * Remove signals in mask from the pending set and queue.
768  * Returns 1 if any signals were found.
769  *
770  * All callers must be holding the siglock.
771  */
772 static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
773 {
774         struct sigqueue *q, *n;
775         sigset_t m;
776
777         sigandsets(&m, mask, &s->signal);
778         if (sigisemptyset(&m))
779                 return;
780
781         sigandnsets(&s->signal, &s->signal, mask);
782         list_for_each_entry_safe(q, n, &s->list, list) {
783                 if (sigismember(mask, q->info.si_signo)) {
784                         list_del_init(&q->list);
785                         __sigqueue_free(q);
786                 }
787         }
788 }
789
790 static inline int is_si_special(const struct siginfo *info)
791 {
792         return info <= SEND_SIG_FORCED;
793 }
794
795 static inline bool si_fromuser(const struct siginfo *info)
796 {
797         return info == SEND_SIG_NOINFO ||
798                 (!is_si_special(info) && SI_FROMUSER(info));
799 }
800
801 /*
802  * called with RCU read lock from check_kill_permission()
803  */
804 static bool kill_ok_by_cred(struct task_struct *t)
805 {
806         const struct cred *cred = current_cred();
807         const struct cred *tcred = __task_cred(t);
808
809         return uid_eq(cred->euid, tcred->suid) ||
810                uid_eq(cred->euid, tcred->uid) ||
811                uid_eq(cred->uid, tcred->suid) ||
812                uid_eq(cred->uid, tcred->uid) ||
813                ns_capable(tcred->user_ns, CAP_KILL);
814 }
815
816 /*
817  * Bad permissions for sending the signal
818  * - the caller must hold the RCU read lock
819  */
820 static int check_kill_permission(int sig, struct siginfo *info,
821                                  struct task_struct *t)
822 {
823         struct pid *sid;
824         int error;
825
826         if (!valid_signal(sig))
827                 return -EINVAL;
828
829         if (!si_fromuser(info))
830                 return 0;
831
832         error = audit_signal_info(sig, t); /* Let audit system see the signal */
833         if (error)
834                 return error;
835
836         if (!same_thread_group(current, t) &&
837             !kill_ok_by_cred(t)) {
838                 switch (sig) {
839                 case SIGCONT:
840                         sid = task_session(t);
841                         /*
842                          * We don't return the error if sid == NULL. The
843                          * task was unhashed, the caller must notice this.
844                          */
845                         if (!sid || sid == task_session(current))
846                                 break;
847                 default:
848                         return -EPERM;
849                 }
850         }
851
852         return security_task_kill(t, info, sig, NULL);
853 }
854
855 /**
856  * ptrace_trap_notify - schedule trap to notify ptracer
857  * @t: tracee wanting to notify tracer
858  *
859  * This function schedules sticky ptrace trap which is cleared on the next
860  * TRAP_STOP to notify ptracer of an event.  @t must have been seized by
861  * ptracer.
862  *
863  * If @t is running, STOP trap will be taken.  If trapped for STOP and
864  * ptracer is listening for events, tracee is woken up so that it can
865  * re-trap for the new event.  If trapped otherwise, STOP trap will be
866  * eventually taken without returning to userland after the existing traps
867  * are finished by PTRACE_CONT.
868  *
869  * CONTEXT:
870  * Must be called with @task->sighand->siglock held.
871  */
872 static void ptrace_trap_notify(struct task_struct *t)
873 {
874         WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
875         assert_spin_locked(&t->sighand->siglock);
876
877         task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
878         ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
879 }
880
881 /*
882  * Handle magic process-wide effects of stop/continue signals. Unlike
883  * the signal actions, these happen immediately at signal-generation
884  * time regardless of blocking, ignoring, or handling.  This does the
885  * actual continuing for SIGCONT, but not the actual stopping for stop
886  * signals. The process stop is done as a signal action for SIG_DFL.
887  *
888  * Returns true if the signal should be actually delivered, otherwise
889  * it should be dropped.
890  */
891 static bool prepare_signal(int sig, struct task_struct *p, bool force)
892 {
893         struct signal_struct *signal = p->signal;
894         struct task_struct *t;
895         sigset_t flush;
896
897         if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
898                 if (!(signal->flags & SIGNAL_GROUP_EXIT))
899                         return sig == SIGKILL;
900                 /*
901                  * The process is in the middle of dying, nothing to do.
902                  */
903         } else if (sig_kernel_stop(sig)) {
904                 /*
905                  * This is a stop signal.  Remove SIGCONT from all queues.
906                  */
907                 siginitset(&flush, sigmask(SIGCONT));
908                 flush_sigqueue_mask(&flush, &signal->shared_pending);
909                 for_each_thread(p, t)
910                         flush_sigqueue_mask(&flush, &t->pending);
911         } else if (sig == SIGCONT) {
912                 unsigned int why;
913                 /*
914                  * Remove all stop signals from all queues, wake all threads.
915                  */
916                 siginitset(&flush, SIG_KERNEL_STOP_MASK);
917                 flush_sigqueue_mask(&flush, &signal->shared_pending);
918                 for_each_thread(p, t) {
919                         flush_sigqueue_mask(&flush, &t->pending);
920                         task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
921                         if (likely(!(t->ptrace & PT_SEIZED)))
922                                 wake_up_state(t, __TASK_STOPPED);
923                         else
924                                 ptrace_trap_notify(t);
925                 }
926
927                 /*
928                  * Notify the parent with CLD_CONTINUED if we were stopped.
929                  *
930                  * If we were in the middle of a group stop, we pretend it
931                  * was already finished, and then continued. Since SIGCHLD
932                  * doesn't queue we report only CLD_STOPPED, as if the next
933                  * CLD_CONTINUED was dropped.
934                  */
935                 why = 0;
936                 if (signal->flags & SIGNAL_STOP_STOPPED)
937                         why |= SIGNAL_CLD_CONTINUED;
938                 else if (signal->group_stop_count)
939                         why |= SIGNAL_CLD_STOPPED;
940
941                 if (why) {
942                         /*
943                          * The first thread which returns from do_signal_stop()
944                          * will take ->siglock, notice SIGNAL_CLD_MASK, and
945                          * notify its parent. See get_signal_to_deliver().
946                          */
947                         signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
948                         signal->group_stop_count = 0;
949                         signal->group_exit_code = 0;
950                 }
951         }
952
953         return !sig_ignored(p, sig, force);
954 }
955
956 /*
957  * Test if P wants to take SIG.  After we've checked all threads with this,
958  * it's equivalent to finding no threads not blocking SIG.  Any threads not
959  * blocking SIG were ruled out because they are not running and already
960  * have pending signals.  Such threads will dequeue from the shared queue
961  * as soon as they're available, so putting the signal on the shared queue
962  * will be equivalent to sending it to one such thread.
963  */
964 static inline bool wants_signal(int sig, struct task_struct *p)
965 {
966         if (sigismember(&p->blocked, sig))
967                 return false;
968
969         if (p->flags & PF_EXITING)
970                 return false;
971
972         if (sig == SIGKILL)
973                 return true;
974
975         if (task_is_stopped_or_traced(p))
976                 return false;
977
978         return task_curr(p) || !signal_pending(p);
979 }
980
981 static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
982 {
983         struct signal_struct *signal = p->signal;
984         struct task_struct *t;
985
986         /*
987          * Now find a thread we can wake up to take the signal off the queue.
988          *
989          * If the main thread wants the signal, it gets first crack.
990          * Probably the least surprising to the average bear.
991          */
992         if (wants_signal(sig, p))
993                 t = p;
994         else if ((type == PIDTYPE_PID) || thread_group_empty(p))
995                 /*
996                  * There is just one thread and it does not need to be woken.
997                  * It will dequeue unblocked signals before it runs again.
998                  */
999                 return;
1000         else {
1001                 /*
1002                  * Otherwise try to find a suitable thread.
1003                  */
1004                 t = signal->curr_target;
1005                 while (!wants_signal(sig, t)) {
1006                         t = next_thread(t);
1007                         if (t == signal->curr_target)
1008                                 /*
1009                                  * No thread needs to be woken.
1010                                  * Any eligible threads will see
1011                                  * the signal in the queue soon.
1012                                  */
1013                                 return;
1014                 }
1015                 signal->curr_target = t;
1016         }
1017
1018         /*
1019          * Found a killable thread.  If the signal will be fatal,
1020          * then start taking the whole group down immediately.
1021          */
1022         if (sig_fatal(p, sig) &&
1023             !(signal->flags & SIGNAL_GROUP_EXIT) &&
1024             !sigismember(&t->real_blocked, sig) &&
1025             (sig == SIGKILL || !p->ptrace)) {
1026                 /*
1027                  * This signal will be fatal to the whole group.
1028                  */
1029                 if (!sig_kernel_coredump(sig)) {
1030                         /*
1031                          * Start a group exit and wake everybody up.
1032                          * This way we don't have other threads
1033                          * running and doing things after a slower
1034                          * thread has the fatal signal pending.
1035                          */
1036                         signal->flags = SIGNAL_GROUP_EXIT;
1037                         signal->group_exit_code = sig;
1038                         signal->group_stop_count = 0;
1039                         t = p;
1040                         do {
1041                                 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1042                                 sigaddset(&t->pending.signal, SIGKILL);
1043                                 signal_wake_up(t, 1);
1044                         } while_each_thread(p, t);
1045                         return;
1046                 }
1047         }
1048
1049         /*
1050          * The signal is already in the shared-pending queue.
1051          * Tell the chosen thread to wake up and dequeue it.
1052          */
1053         signal_wake_up(t, sig == SIGKILL);
1054         return;
1055 }
1056
1057 static inline bool legacy_queue(struct sigpending *signals, int sig)
1058 {
1059         return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1060 }
1061
1062 #ifdef CONFIG_USER_NS
1063 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1064 {
1065         if (current_user_ns() == task_cred_xxx(t, user_ns))
1066                 return;
1067
1068         if (SI_FROMKERNEL(info))
1069                 return;
1070
1071         rcu_read_lock();
1072         info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
1073                                         make_kuid(current_user_ns(), info->si_uid));
1074         rcu_read_unlock();
1075 }
1076 #else
1077 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1078 {
1079         return;
1080 }
1081 #endif
1082
1083 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1084                         enum pid_type type, int from_ancestor_ns)
1085 {
1086         struct sigpending *pending;
1087         struct sigqueue *q;
1088         int override_rlimit;
1089         int ret = 0, result;
1090
1091         assert_spin_locked(&t->sighand->siglock);
1092
1093         result = TRACE_SIGNAL_IGNORED;
1094         if (!prepare_signal(sig, t,
1095                         from_ancestor_ns || (info == SEND_SIG_PRIV) || (info == SEND_SIG_FORCED)))
1096                 goto ret;
1097
1098         pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1099         /*
1100          * Short-circuit ignored signals and support queuing
1101          * exactly one non-rt signal, so that we can get more
1102          * detailed information about the cause of the signal.
1103          */
1104         result = TRACE_SIGNAL_ALREADY_PENDING;
1105         if (legacy_queue(pending, sig))
1106                 goto ret;
1107
1108         result = TRACE_SIGNAL_DELIVERED;
1109         /*
1110          * fast-pathed signals for kernel-internal things like SIGSTOP
1111          * or SIGKILL.
1112          */
1113         if (info == SEND_SIG_FORCED)
1114                 goto out_set;
1115
1116         /*
1117          * Real-time signals must be queued if sent by sigqueue, or
1118          * some other real-time mechanism.  It is implementation
1119          * defined whether kill() does so.  We attempt to do so, on
1120          * the principle of least surprise, but since kill is not
1121          * allowed to fail with EAGAIN when low on memory we just
1122          * make sure at least one signal gets delivered and don't
1123          * pass on the info struct.
1124          */
1125         if (sig < SIGRTMIN)
1126                 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1127         else
1128                 override_rlimit = 0;
1129
1130         q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit);
1131         if (q) {
1132                 list_add_tail(&q->list, &pending->list);
1133                 switch ((unsigned long) info) {
1134                 case (unsigned long) SEND_SIG_NOINFO:
1135                         clear_siginfo(&q->info);
1136                         q->info.si_signo = sig;
1137                         q->info.si_errno = 0;
1138                         q->info.si_code = SI_USER;
1139                         q->info.si_pid = task_tgid_nr_ns(current,
1140                                                         task_active_pid_ns(t));
1141                         q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
1142                         break;
1143                 case (unsigned long) SEND_SIG_PRIV:
1144                         clear_siginfo(&q->info);
1145                         q->info.si_signo = sig;
1146                         q->info.si_errno = 0;
1147                         q->info.si_code = SI_KERNEL;
1148                         q->info.si_pid = 0;
1149                         q->info.si_uid = 0;
1150                         break;
1151                 default:
1152                         copy_siginfo(&q->info, info);
1153                         if (from_ancestor_ns)
1154                                 q->info.si_pid = 0;
1155                         break;
1156                 }
1157
1158                 userns_fixup_signal_uid(&q->info, t);
1159
1160         } else if (!is_si_special(info)) {
1161                 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1162                         /*
1163                          * Queue overflow, abort.  We may abort if the
1164                          * signal was rt and sent by user using something
1165                          * other than kill().
1166                          */
1167                         result = TRACE_SIGNAL_OVERFLOW_FAIL;
1168                         ret = -EAGAIN;
1169                         goto ret;
1170                 } else {
1171                         /*
1172                          * This is a silent loss of information.  We still
1173                          * send the signal, but the *info bits are lost.
1174                          */
1175                         result = TRACE_SIGNAL_LOSE_INFO;
1176                 }
1177         }
1178
1179 out_set:
1180         signalfd_notify(t, sig);
1181         sigaddset(&pending->signal, sig);
1182
1183         /* Let multiprocess signals appear after on-going forks */
1184         if (type > PIDTYPE_TGID) {
1185                 struct multiprocess_signals *delayed;
1186                 hlist_for_each_entry(delayed, &t->signal->multiprocess, node) {
1187                         sigset_t *signal = &delayed->signal;
1188                         /* Can't queue both a stop and a continue signal */
1189                         if (sig == SIGCONT)
1190                                 sigdelsetmask(signal, SIG_KERNEL_STOP_MASK);
1191                         else if (sig_kernel_stop(sig))
1192                                 sigdelset(signal, SIGCONT);
1193                         sigaddset(signal, sig);
1194                 }
1195         }
1196
1197         complete_signal(sig, t, type);
1198 ret:
1199         trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result);
1200         return ret;
1201 }
1202
1203 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1204                         enum pid_type type)
1205 {
1206         int from_ancestor_ns = 0;
1207
1208 #ifdef CONFIG_PID_NS
1209         from_ancestor_ns = si_fromuser(info) &&
1210                            !task_pid_nr_ns(current, task_active_pid_ns(t));
1211 #endif
1212
1213         return __send_signal(sig, info, t, type, from_ancestor_ns);
1214 }
1215
1216 static void print_fatal_signal(int signr)
1217 {
1218         struct pt_regs *regs = signal_pt_regs();
1219         pr_info("potentially unexpected fatal signal %d.\n", signr);
1220
1221 #if defined(__i386__) && !defined(__arch_um__)
1222         pr_info("code at %08lx: ", regs->ip);
1223         {
1224                 int i;
1225                 for (i = 0; i < 16; i++) {
1226                         unsigned char insn;
1227
1228                         if (get_user(insn, (unsigned char *)(regs->ip + i)))
1229                                 break;
1230                         pr_cont("%02x ", insn);
1231                 }
1232         }
1233         pr_cont("\n");
1234 #endif
1235         preempt_disable();
1236         show_regs(regs);
1237         preempt_enable();
1238 }
1239
1240 static int __init setup_print_fatal_signals(char *str)
1241 {
1242         get_option (&str, &print_fatal_signals);
1243
1244         return 1;
1245 }
1246
1247 __setup("print-fatal-signals=", setup_print_fatal_signals);
1248
1249 int
1250 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1251 {
1252         return send_signal(sig, info, p, PIDTYPE_TGID);
1253 }
1254
1255 static int
1256 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1257 {
1258         return send_signal(sig, info, t, PIDTYPE_PID);
1259 }
1260
1261 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1262                         enum pid_type type)
1263 {
1264         unsigned long flags;
1265         int ret = -ESRCH;
1266
1267         if (lock_task_sighand(p, &flags)) {
1268                 ret = send_signal(sig, info, p, type);
1269                 unlock_task_sighand(p, &flags);
1270         }
1271
1272         return ret;
1273 }
1274
1275 /*
1276  * Force a signal that the process can't ignore: if necessary
1277  * we unblock the signal and change any SIG_IGN to SIG_DFL.
1278  *
1279  * Note: If we unblock the signal, we always reset it to SIG_DFL,
1280  * since we do not want to have a signal handler that was blocked
1281  * be invoked when user space had explicitly blocked it.
1282  *
1283  * We don't want to have recursive SIGSEGV's etc, for example,
1284  * that is why we also clear SIGNAL_UNKILLABLE.
1285  */
1286 int
1287 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1288 {
1289         unsigned long int flags;
1290         int ret, blocked, ignored;
1291         struct k_sigaction *action;
1292
1293         spin_lock_irqsave(&t->sighand->siglock, flags);
1294         action = &t->sighand->action[sig-1];
1295         ignored = action->sa.sa_handler == SIG_IGN;
1296         blocked = sigismember(&t->blocked, sig);
1297         if (blocked || ignored) {
1298                 action->sa.sa_handler = SIG_DFL;
1299                 if (blocked) {
1300                         sigdelset(&t->blocked, sig);
1301                         recalc_sigpending_and_wake(t);
1302                 }
1303         }
1304         /*
1305          * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
1306          * debugging to leave init killable.
1307          */
1308         if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
1309                 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1310         ret = specific_send_sig_info(sig, info, t);
1311         spin_unlock_irqrestore(&t->sighand->siglock, flags);
1312
1313         return ret;
1314 }
1315
1316 /*
1317  * Nuke all other threads in the group.
1318  */
1319 int zap_other_threads(struct task_struct *p)
1320 {
1321         struct task_struct *t = p;
1322         int count = 0;
1323
1324         p->signal->group_stop_count = 0;
1325
1326         while_each_thread(p, t) {
1327                 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1328                 count++;
1329
1330                 /* Don't bother with already dead threads */
1331                 if (t->exit_state)
1332                         continue;
1333                 sigaddset(&t->pending.signal, SIGKILL);
1334                 signal_wake_up(t, 1);
1335         }
1336
1337         return count;
1338 }
1339
1340 struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1341                                            unsigned long *flags)
1342 {
1343         struct sighand_struct *sighand;
1344
1345         rcu_read_lock();
1346         for (;;) {
1347                 sighand = rcu_dereference(tsk->sighand);
1348                 if (unlikely(sighand == NULL))
1349                         break;
1350
1351                 /*
1352                  * This sighand can be already freed and even reused, but
1353                  * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
1354                  * initializes ->siglock: this slab can't go away, it has
1355                  * the same object type, ->siglock can't be reinitialized.
1356                  *
1357                  * We need to ensure that tsk->sighand is still the same
1358                  * after we take the lock, we can race with de_thread() or
1359                  * __exit_signal(). In the latter case the next iteration
1360                  * must see ->sighand == NULL.
1361                  */
1362                 spin_lock_irqsave(&sighand->siglock, *flags);
1363                 if (likely(sighand == tsk->sighand))
1364                         break;
1365                 spin_unlock_irqrestore(&sighand->siglock, *flags);
1366         }
1367         rcu_read_unlock();
1368
1369         return sighand;
1370 }
1371
1372 /*
1373  * send signal info to all the members of a group
1374  */
1375 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1376                         enum pid_type type)
1377 {
1378         int ret;
1379
1380         rcu_read_lock();
1381         ret = check_kill_permission(sig, info, p);
1382         rcu_read_unlock();
1383
1384         if (!ret && sig)
1385                 ret = do_send_sig_info(sig, info, p, type);
1386
1387         return ret;
1388 }
1389
1390 /*
1391  * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1392  * control characters do (^C, ^Z etc)
1393  * - the caller must hold at least a readlock on tasklist_lock
1394  */
1395 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1396 {
1397         struct task_struct *p = NULL;
1398         int retval, success;
1399
1400         success = 0;
1401         retval = -ESRCH;
1402         do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1403                 int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
1404                 success |= !err;
1405                 retval = err;
1406         } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1407         return success ? 0 : retval;
1408 }
1409
1410 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1411 {
1412         int error = -ESRCH;
1413         struct task_struct *p;
1414
1415         for (;;) {
1416                 rcu_read_lock();
1417                 p = pid_task(pid, PIDTYPE_PID);
1418                 if (p)
1419                         error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
1420                 rcu_read_unlock();
1421                 if (likely(!p || error != -ESRCH))
1422                         return error;
1423
1424                 /*
1425                  * The task was unhashed in between, try again.  If it
1426                  * is dead, pid_task() will return NULL, if we race with
1427                  * de_thread() it will find the new leader.
1428                  */
1429         }
1430 }
1431
1432 static int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1433 {
1434         int error;
1435         rcu_read_lock();
1436         error = kill_pid_info(sig, info, find_vpid(pid));
1437         rcu_read_unlock();
1438         return error;
1439 }
1440
1441 static inline bool kill_as_cred_perm(const struct cred *cred,
1442                                      struct task_struct *target)
1443 {
1444         const struct cred *pcred = __task_cred(target);
1445
1446         return uid_eq(cred->euid, pcred->suid) ||
1447                uid_eq(cred->euid, pcred->uid) ||
1448                uid_eq(cred->uid, pcred->suid) ||
1449                uid_eq(cred->uid, pcred->uid);
1450 }
1451
1452 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1453 int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1454                          const struct cred *cred)
1455 {
1456         int ret = -EINVAL;
1457         struct task_struct *p;
1458         unsigned long flags;
1459
1460         if (!valid_signal(sig))
1461                 return ret;
1462
1463         rcu_read_lock();
1464         p = pid_task(pid, PIDTYPE_PID);
1465         if (!p) {
1466                 ret = -ESRCH;
1467                 goto out_unlock;
1468         }
1469         if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
1470                 ret = -EPERM;
1471                 goto out_unlock;
1472         }
1473         ret = security_task_kill(p, info, sig, cred);
1474         if (ret)
1475                 goto out_unlock;
1476
1477         if (sig) {
1478                 if (lock_task_sighand(p, &flags)) {
1479                         ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0);
1480                         unlock_task_sighand(p, &flags);
1481                 } else
1482                         ret = -ESRCH;
1483         }
1484 out_unlock:
1485         rcu_read_unlock();
1486         return ret;
1487 }
1488 EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
1489
1490 /*
1491  * kill_something_info() interprets pid in interesting ways just like kill(2).
1492  *
1493  * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1494  * is probably wrong.  Should make it like BSD or SYSV.
1495  */
1496
1497 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1498 {
1499         int ret;
1500
1501         if (pid > 0) {
1502                 rcu_read_lock();
1503                 ret = kill_pid_info(sig, info, find_vpid(pid));
1504                 rcu_read_unlock();
1505                 return ret;
1506         }
1507
1508         /* -INT_MIN is undefined.  Exclude this case to avoid a UBSAN warning */
1509         if (pid == INT_MIN)
1510                 return -ESRCH;
1511
1512         read_lock(&tasklist_lock);
1513         if (pid != -1) {
1514                 ret = __kill_pgrp_info(sig, info,
1515                                 pid ? find_vpid(-pid) : task_pgrp(current));
1516         } else {
1517                 int retval = 0, count = 0;
1518                 struct task_struct * p;
1519
1520                 for_each_process(p) {
1521                         if (task_pid_vnr(p) > 1 &&
1522                                         !same_thread_group(p, current)) {
1523                                 int err = group_send_sig_info(sig, info, p,
1524                                                               PIDTYPE_MAX);
1525                                 ++count;
1526                                 if (err != -EPERM)
1527                                         retval = err;
1528                         }
1529                 }
1530                 ret = count ? retval : -ESRCH;
1531         }
1532         read_unlock(&tasklist_lock);
1533
1534         return ret;
1535 }
1536
1537 /*
1538  * These are for backward compatibility with the rest of the kernel source.
1539  */
1540
1541 int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1542 {
1543         /*
1544          * Make sure legacy kernel users don't send in bad values
1545          * (normal paths check this in check_kill_permission).
1546          */
1547         if (!valid_signal(sig))
1548                 return -EINVAL;
1549
1550         return do_send_sig_info(sig, info, p, PIDTYPE_PID);
1551 }
1552
1553 #define __si_special(priv) \
1554         ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1555
1556 int
1557 send_sig(int sig, struct task_struct *p, int priv)
1558 {
1559         return send_sig_info(sig, __si_special(priv), p);
1560 }
1561
1562 void force_sig(int sig, struct task_struct *p)
1563 {
1564         force_sig_info(sig, SEND_SIG_PRIV, p);
1565 }
1566
1567 /*
1568  * When things go south during signal handling, we
1569  * will force a SIGSEGV. And if the signal that caused
1570  * the problem was already a SIGSEGV, we'll want to
1571  * make sure we don't even try to deliver the signal..
1572  */
1573 void force_sigsegv(int sig, struct task_struct *p)
1574 {
1575         if (sig == SIGSEGV) {
1576                 unsigned long flags;
1577                 spin_lock_irqsave(&p->sighand->siglock, flags);
1578                 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1579                 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1580         }
1581         force_sig(SIGSEGV, p);
1582 }
1583
1584 int force_sig_fault(int sig, int code, void __user *addr
1585         ___ARCH_SI_TRAPNO(int trapno)
1586         ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1587         , struct task_struct *t)
1588 {
1589         struct siginfo info;
1590
1591         clear_siginfo(&info);
1592         info.si_signo = sig;
1593         info.si_errno = 0;
1594         info.si_code  = code;
1595         info.si_addr  = addr;
1596 #ifdef __ARCH_SI_TRAPNO
1597         info.si_trapno = trapno;
1598 #endif
1599 #ifdef __ia64__
1600         info.si_imm = imm;
1601         info.si_flags = flags;
1602         info.si_isr = isr;
1603 #endif
1604         return force_sig_info(info.si_signo, &info, t);
1605 }
1606
1607 int send_sig_fault(int sig, int code, void __user *addr
1608         ___ARCH_SI_TRAPNO(int trapno)
1609         ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1610         , struct task_struct *t)
1611 {
1612         struct siginfo info;
1613
1614         clear_siginfo(&info);
1615         info.si_signo = sig;
1616         info.si_errno = 0;
1617         info.si_code  = code;
1618         info.si_addr  = addr;
1619 #ifdef __ARCH_SI_TRAPNO
1620         info.si_trapno = trapno;
1621 #endif
1622 #ifdef __ia64__
1623         info.si_imm = imm;
1624         info.si_flags = flags;
1625         info.si_isr = isr;
1626 #endif
1627         return send_sig_info(info.si_signo, &info, t);
1628 }
1629
1630 int force_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1631 {
1632         struct siginfo info;
1633
1634         WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1635         clear_siginfo(&info);
1636         info.si_signo = SIGBUS;
1637         info.si_errno = 0;
1638         info.si_code = code;
1639         info.si_addr = addr;
1640         info.si_addr_lsb = lsb;
1641         return force_sig_info(info.si_signo, &info, t);
1642 }
1643
1644 int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1645 {
1646         struct siginfo info;
1647
1648         WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1649         clear_siginfo(&info);
1650         info.si_signo = SIGBUS;
1651         info.si_errno = 0;
1652         info.si_code = code;
1653         info.si_addr = addr;
1654         info.si_addr_lsb = lsb;
1655         return send_sig_info(info.si_signo, &info, t);
1656 }
1657 EXPORT_SYMBOL(send_sig_mceerr);
1658
1659 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
1660 {
1661         struct siginfo info;
1662
1663         clear_siginfo(&info);
1664         info.si_signo = SIGSEGV;
1665         info.si_errno = 0;
1666         info.si_code  = SEGV_BNDERR;
1667         info.si_addr  = addr;
1668         info.si_lower = lower;
1669         info.si_upper = upper;
1670         return force_sig_info(info.si_signo, &info, current);
1671 }
1672
1673 #ifdef SEGV_PKUERR
1674 int force_sig_pkuerr(void __user *addr, u32 pkey)
1675 {
1676         struct siginfo info;
1677
1678         clear_siginfo(&info);
1679         info.si_signo = SIGSEGV;
1680         info.si_errno = 0;
1681         info.si_code  = SEGV_PKUERR;
1682         info.si_addr  = addr;
1683         info.si_pkey  = pkey;
1684         return force_sig_info(info.si_signo, &info, current);
1685 }
1686 #endif
1687
1688 /* For the crazy architectures that include trap information in
1689  * the errno field, instead of an actual errno value.
1690  */
1691 int force_sig_ptrace_errno_trap(int errno, void __user *addr)
1692 {
1693         struct siginfo info;
1694
1695         clear_siginfo(&info);
1696         info.si_signo = SIGTRAP;
1697         info.si_errno = errno;
1698         info.si_code  = TRAP_HWBKPT;
1699         info.si_addr  = addr;
1700         return force_sig_info(info.si_signo, &info, current);
1701 }
1702
1703 int kill_pgrp(struct pid *pid, int sig, int priv)
1704 {
1705         int ret;
1706
1707         read_lock(&tasklist_lock);
1708         ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1709         read_unlock(&tasklist_lock);
1710
1711         return ret;
1712 }
1713 EXPORT_SYMBOL(kill_pgrp);
1714
1715 int kill_pid(struct pid *pid, int sig, int priv)
1716 {
1717         return kill_pid_info(sig, __si_special(priv), pid);
1718 }
1719 EXPORT_SYMBOL(kill_pid);
1720
1721 /*
1722  * These functions support sending signals using preallocated sigqueue
1723  * structures.  This is needed "because realtime applications cannot
1724  * afford to lose notifications of asynchronous events, like timer
1725  * expirations or I/O completions".  In the case of POSIX Timers
1726  * we allocate the sigqueue structure from the timer_create.  If this
1727  * allocation fails we are able to report the failure to the application
1728  * with an EAGAIN error.
1729  */
1730 struct sigqueue *sigqueue_alloc(void)
1731 {
1732         struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1733
1734         if (q)
1735                 q->flags |= SIGQUEUE_PREALLOC;
1736
1737         return q;
1738 }
1739
1740 void sigqueue_free(struct sigqueue *q)
1741 {
1742         unsigned long flags;
1743         spinlock_t *lock = &current->sighand->siglock;
1744
1745         BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1746         /*
1747          * We must hold ->siglock while testing q->list
1748          * to serialize with collect_signal() or with
1749          * __exit_signal()->flush_sigqueue().
1750          */
1751         spin_lock_irqsave(lock, flags);
1752         q->flags &= ~SIGQUEUE_PREALLOC;
1753         /*
1754          * If it is queued it will be freed when dequeued,
1755          * like the "regular" sigqueue.
1756          */
1757         if (!list_empty(&q->list))
1758                 q = NULL;
1759         spin_unlock_irqrestore(lock, flags);
1760
1761         if (q)
1762                 __sigqueue_free(q);
1763 }
1764
1765 int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
1766 {
1767         int sig = q->info.si_signo;
1768         struct sigpending *pending;
1769         struct task_struct *t;
1770         unsigned long flags;
1771         int ret, result;
1772
1773         BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1774
1775         ret = -1;
1776         rcu_read_lock();
1777         t = pid_task(pid, type);
1778         if (!t || !likely(lock_task_sighand(t, &flags)))
1779                 goto ret;
1780
1781         ret = 1; /* the signal is ignored */
1782         result = TRACE_SIGNAL_IGNORED;
1783         if (!prepare_signal(sig, t, false))
1784                 goto out;
1785
1786         ret = 0;
1787         if (unlikely(!list_empty(&q->list))) {
1788                 /*
1789                  * If an SI_TIMER entry is already queue just increment
1790                  * the overrun count.
1791                  */
1792                 BUG_ON(q->info.si_code != SI_TIMER);
1793                 q->info.si_overrun++;
1794                 result = TRACE_SIGNAL_ALREADY_PENDING;
1795                 goto out;
1796         }
1797         q->info.si_overrun = 0;
1798
1799         signalfd_notify(t, sig);
1800         pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1801         list_add_tail(&q->list, &pending->list);
1802         sigaddset(&pending->signal, sig);
1803         complete_signal(sig, t, type);
1804         result = TRACE_SIGNAL_DELIVERED;
1805 out:
1806         trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
1807         unlock_task_sighand(t, &flags);
1808 ret:
1809         rcu_read_unlock();
1810         return ret;
1811 }
1812
1813 /*
1814  * Let a parent know about the death of a child.
1815  * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1816  *
1817  * Returns true if our parent ignored us and so we've switched to
1818  * self-reaping.
1819  */
1820 bool do_notify_parent(struct task_struct *tsk, int sig)
1821 {
1822         struct siginfo info;
1823         unsigned long flags;
1824         struct sighand_struct *psig;
1825         bool autoreap = false;
1826         u64 utime, stime;
1827
1828         BUG_ON(sig == -1);
1829
1830         /* do_notify_parent_cldstop should have been called instead.  */
1831         BUG_ON(task_is_stopped_or_traced(tsk));
1832
1833         BUG_ON(!tsk->ptrace &&
1834                (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1835
1836         if (sig != SIGCHLD) {
1837                 /*
1838                  * This is only possible if parent == real_parent.
1839                  * Check if it has changed security domain.
1840                  */
1841                 if (tsk->parent_exec_id != READ_ONCE(tsk->parent->self_exec_id))
1842                         sig = SIGCHLD;
1843         }
1844
1845         clear_siginfo(&info);
1846         info.si_signo = sig;
1847         info.si_errno = 0;
1848         /*
1849          * We are under tasklist_lock here so our parent is tied to
1850          * us and cannot change.
1851          *
1852          * task_active_pid_ns will always return the same pid namespace
1853          * until a task passes through release_task.
1854          *
1855          * write_lock() currently calls preempt_disable() which is the
1856          * same as rcu_read_lock(), but according to Oleg, this is not
1857          * correct to rely on this
1858          */
1859         rcu_read_lock();
1860         info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
1861         info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1862                                        task_uid(tsk));
1863         rcu_read_unlock();
1864
1865         task_cputime(tsk, &utime, &stime);
1866         info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
1867         info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
1868
1869         info.si_status = tsk->exit_code & 0x7f;
1870         if (tsk->exit_code & 0x80)
1871                 info.si_code = CLD_DUMPED;
1872         else if (tsk->exit_code & 0x7f)
1873                 info.si_code = CLD_KILLED;
1874         else {
1875                 info.si_code = CLD_EXITED;
1876                 info.si_status = tsk->exit_code >> 8;
1877         }
1878
1879         psig = tsk->parent->sighand;
1880         spin_lock_irqsave(&psig->siglock, flags);
1881         if (!tsk->ptrace && sig == SIGCHLD &&
1882             (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1883              (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1884                 /*
1885                  * We are exiting and our parent doesn't care.  POSIX.1
1886                  * defines special semantics for setting SIGCHLD to SIG_IGN
1887                  * or setting the SA_NOCLDWAIT flag: we should be reaped
1888                  * automatically and not left for our parent's wait4 call.
1889                  * Rather than having the parent do it as a magic kind of
1890                  * signal handler, we just set this to tell do_exit that we
1891                  * can be cleaned up without becoming a zombie.  Note that
1892                  * we still call __wake_up_parent in this case, because a
1893                  * blocked sys_wait4 might now return -ECHILD.
1894                  *
1895                  * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1896                  * is implementation-defined: we do (if you don't want
1897                  * it, just use SIG_IGN instead).
1898                  */
1899                 autoreap = true;
1900                 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1901                         sig = 0;
1902         }
1903         if (valid_signal(sig) && sig)
1904                 __group_send_sig_info(sig, &info, tsk->parent);
1905         __wake_up_parent(tsk, tsk->parent);
1906         spin_unlock_irqrestore(&psig->siglock, flags);
1907
1908         return autoreap;
1909 }
1910
1911 /**
1912  * do_notify_parent_cldstop - notify parent of stopped/continued state change
1913  * @tsk: task reporting the state change
1914  * @for_ptracer: the notification is for ptracer
1915  * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1916  *
1917  * Notify @tsk's parent that the stopped/continued state has changed.  If
1918  * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1919  * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1920  *
1921  * CONTEXT:
1922  * Must be called with tasklist_lock at least read locked.
1923  */
1924 static void do_notify_parent_cldstop(struct task_struct *tsk,
1925                                      bool for_ptracer, int why)
1926 {
1927         struct siginfo info;
1928         unsigned long flags;
1929         struct task_struct *parent;
1930         struct sighand_struct *sighand;
1931         u64 utime, stime;
1932
1933         if (for_ptracer) {
1934                 parent = tsk->parent;
1935         } else {
1936                 tsk = tsk->group_leader;
1937                 parent = tsk->real_parent;
1938         }
1939
1940         clear_siginfo(&info);
1941         info.si_signo = SIGCHLD;
1942         info.si_errno = 0;
1943         /*
1944          * see comment in do_notify_parent() about the following 4 lines
1945          */
1946         rcu_read_lock();
1947         info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
1948         info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
1949         rcu_read_unlock();
1950
1951         task_cputime(tsk, &utime, &stime);
1952         info.si_utime = nsec_to_clock_t(utime);
1953         info.si_stime = nsec_to_clock_t(stime);
1954
1955         info.si_code = why;
1956         switch (why) {
1957         case CLD_CONTINUED:
1958                 info.si_status = SIGCONT;
1959                 break;
1960         case CLD_STOPPED:
1961                 info.si_status = tsk->signal->group_exit_code & 0x7f;
1962                 break;
1963         case CLD_TRAPPED:
1964                 info.si_status = tsk->exit_code & 0x7f;
1965                 break;
1966         default:
1967                 BUG();
1968         }
1969
1970         sighand = parent->sighand;
1971         spin_lock_irqsave(&sighand->siglock, flags);
1972         if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1973             !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1974                 __group_send_sig_info(SIGCHLD, &info, parent);
1975         /*
1976          * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1977          */
1978         __wake_up_parent(tsk, parent);
1979         spin_unlock_irqrestore(&sighand->siglock, flags);
1980 }
1981
1982 static inline bool may_ptrace_stop(void)
1983 {
1984         if (!likely(current->ptrace))
1985                 return false;
1986         /*
1987          * Are we in the middle of do_coredump?
1988          * If so and our tracer is also part of the coredump stopping
1989          * is a deadlock situation, and pointless because our tracer
1990          * is dead so don't allow us to stop.
1991          * If SIGKILL was already sent before the caller unlocked
1992          * ->siglock we must see ->core_state != NULL. Otherwise it
1993          * is safe to enter schedule().
1994          *
1995          * This is almost outdated, a task with the pending SIGKILL can't
1996          * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
1997          * after SIGKILL was already dequeued.
1998          */
1999         if (unlikely(current->mm->core_state) &&
2000             unlikely(current->mm == current->parent->mm))
2001                 return false;
2002
2003         return true;
2004 }
2005
2006
2007 /*
2008  * This must be called with current->sighand->siglock held.
2009  *
2010  * This should be the path for all ptrace stops.
2011  * We always set current->last_siginfo while stopped here.
2012  * That makes it a way to test a stopped process for
2013  * being ptrace-stopped vs being job-control-stopped.
2014  *
2015  * If we actually decide not to stop at all because the tracer
2016  * is gone, we keep current->exit_code unless clear_code.
2017  */
2018 static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
2019         __releases(&current->sighand->siglock)
2020         __acquires(&current->sighand->siglock)
2021 {
2022         bool gstop_done = false;
2023
2024         if (arch_ptrace_stop_needed(exit_code, info)) {
2025                 /*
2026                  * The arch code has something special to do before a
2027                  * ptrace stop.  This is allowed to block, e.g. for faults
2028                  * on user stack pages.  We can't keep the siglock while
2029                  * calling arch_ptrace_stop, so we must release it now.
2030                  * To preserve proper semantics, we must do this before
2031                  * any signal bookkeeping like checking group_stop_count.
2032                  */
2033                 spin_unlock_irq(&current->sighand->siglock);
2034                 arch_ptrace_stop(exit_code, info);
2035                 spin_lock_irq(&current->sighand->siglock);
2036         }
2037
2038         /*
2039          * schedule() will not sleep if there is a pending signal that
2040          * can awaken the task.
2041          */
2042         set_special_state(TASK_TRACED);
2043
2044         /*
2045          * We're committing to trapping.  TRACED should be visible before
2046          * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
2047          * Also, transition to TRACED and updates to ->jobctl should be
2048          * atomic with respect to siglock and should be done after the arch
2049          * hook as siglock is released and regrabbed across it.
2050          *
2051          *     TRACER                               TRACEE
2052          *
2053          *     ptrace_attach()
2054          * [L]   wait_on_bit(JOBCTL_TRAPPING)   [S] set_special_state(TRACED)
2055          *     do_wait()
2056          *       set_current_state()                smp_wmb();
2057          *       ptrace_do_wait()
2058          *         wait_task_stopped()
2059          *           task_stopped_code()
2060          * [L]         task_is_traced()         [S] task_clear_jobctl_trapping();
2061          */
2062         smp_wmb();
2063
2064         current->last_siginfo = info;
2065         current->exit_code = exit_code;
2066
2067         /*
2068          * If @why is CLD_STOPPED, we're trapping to participate in a group
2069          * stop.  Do the bookkeeping.  Note that if SIGCONT was delievered
2070          * across siglock relocks since INTERRUPT was scheduled, PENDING
2071          * could be clear now.  We act as if SIGCONT is received after
2072          * TASK_TRACED is entered - ignore it.
2073          */
2074         if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
2075                 gstop_done = task_participate_group_stop(current);
2076
2077         /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
2078         task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
2079         if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
2080                 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
2081
2082         /* entering a trap, clear TRAPPING */
2083         task_clear_jobctl_trapping(current);
2084
2085         spin_unlock_irq(&current->sighand->siglock);
2086         read_lock(&tasklist_lock);
2087         if (may_ptrace_stop()) {
2088                 /*
2089                  * Notify parents of the stop.
2090                  *
2091                  * While ptraced, there are two parents - the ptracer and
2092                  * the real_parent of the group_leader.  The ptracer should
2093                  * know about every stop while the real parent is only
2094                  * interested in the completion of group stop.  The states
2095                  * for the two don't interact with each other.  Notify
2096                  * separately unless they're gonna be duplicates.
2097                  */
2098                 do_notify_parent_cldstop(current, true, why);
2099                 if (gstop_done && ptrace_reparented(current))
2100                         do_notify_parent_cldstop(current, false, why);
2101
2102                 /*
2103                  * Don't want to allow preemption here, because
2104                  * sys_ptrace() needs this task to be inactive.
2105                  *
2106                  * XXX: implement read_unlock_no_resched().
2107                  */
2108                 preempt_disable();
2109                 read_unlock(&tasklist_lock);
2110                 preempt_enable_no_resched();
2111                 freezable_schedule();
2112         } else {
2113                 /*
2114                  * By the time we got the lock, our tracer went away.
2115                  * Don't drop the lock yet, another tracer may come.
2116                  *
2117                  * If @gstop_done, the ptracer went away between group stop
2118                  * completion and here.  During detach, it would have set
2119                  * JOBCTL_STOP_PENDING on us and we'll re-enter
2120                  * TASK_STOPPED in do_signal_stop() on return, so notifying
2121                  * the real parent of the group stop completion is enough.
2122                  */
2123                 if (gstop_done)
2124                         do_notify_parent_cldstop(current, false, why);
2125
2126                 /* tasklist protects us from ptrace_freeze_traced() */
2127                 __set_current_state(TASK_RUNNING);
2128                 if (clear_code)
2129                         current->exit_code = 0;
2130                 read_unlock(&tasklist_lock);
2131         }
2132
2133         /*
2134          * We are back.  Now reacquire the siglock before touching
2135          * last_siginfo, so that we are sure to have synchronized with
2136          * any signal-sending on another CPU that wants to examine it.
2137          */
2138         spin_lock_irq(&current->sighand->siglock);
2139         current->last_siginfo = NULL;
2140
2141         /* LISTENING can be set only during STOP traps, clear it */
2142         current->jobctl &= ~JOBCTL_LISTENING;
2143
2144         /*
2145          * Queued signals ignored us while we were stopped for tracing.
2146          * So check for any that we should take before resuming user mode.
2147          * This sets TIF_SIGPENDING, but never clears it.
2148          */
2149         recalc_sigpending_tsk(current);
2150 }
2151
2152 static void ptrace_do_notify(int signr, int exit_code, int why)
2153 {
2154         siginfo_t info;
2155
2156         clear_siginfo(&info);
2157         info.si_signo = signr;
2158         info.si_code = exit_code;
2159         info.si_pid = task_pid_vnr(current);
2160         info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
2161
2162         /* Let the debugger run.  */
2163         ptrace_stop(exit_code, why, 1, &info);
2164 }
2165
2166 void ptrace_notify(int exit_code)
2167 {
2168         BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
2169         if (unlikely(current->task_works))
2170                 task_work_run();
2171
2172         spin_lock_irq(&current->sighand->siglock);
2173         ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
2174         spin_unlock_irq(&current->sighand->siglock);
2175 }
2176
2177 /**
2178  * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2179  * @signr: signr causing group stop if initiating
2180  *
2181  * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2182  * and participate in it.  If already set, participate in the existing
2183  * group stop.  If participated in a group stop (and thus slept), %true is
2184  * returned with siglock released.
2185  *
2186  * If ptraced, this function doesn't handle stop itself.  Instead,
2187  * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2188  * untouched.  The caller must ensure that INTERRUPT trap handling takes
2189  * places afterwards.
2190  *
2191  * CONTEXT:
2192  * Must be called with @current->sighand->siglock held, which is released
2193  * on %true return.
2194  *
2195  * RETURNS:
2196  * %false if group stop is already cancelled or ptrace trap is scheduled.
2197  * %true if participated in group stop.
2198  */
2199 static bool do_signal_stop(int signr)
2200         __releases(&current->sighand->siglock)
2201 {
2202         struct signal_struct *sig = current->signal;
2203
2204         if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2205                 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
2206                 struct task_struct *t;
2207
2208                 /* signr will be recorded in task->jobctl for retries */
2209                 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
2210
2211                 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
2212                     unlikely(signal_group_exit(sig)))
2213                         return false;
2214                 /*
2215                  * There is no group stop already in progress.  We must
2216                  * initiate one now.
2217                  *
2218                  * While ptraced, a task may be resumed while group stop is
2219                  * still in effect and then receive a stop signal and
2220                  * initiate another group stop.  This deviates from the
2221                  * usual behavior as two consecutive stop signals can't
2222                  * cause two group stops when !ptraced.  That is why we
2223                  * also check !task_is_stopped(t) below.
2224                  *
2225                  * The condition can be distinguished by testing whether
2226                  * SIGNAL_STOP_STOPPED is already set.  Don't generate
2227                  * group_exit_code in such case.
2228                  *
2229                  * This is not necessary for SIGNAL_STOP_CONTINUED because
2230                  * an intervening stop signal is required to cause two
2231                  * continued events regardless of ptrace.
2232                  */
2233                 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2234                         sig->group_exit_code = signr;
2235
2236                 sig->group_stop_count = 0;
2237
2238                 if (task_set_jobctl_pending(current, signr | gstop))
2239                         sig->group_stop_count++;
2240
2241                 t = current;
2242                 while_each_thread(current, t) {
2243                         /*
2244                          * Setting state to TASK_STOPPED for a group
2245                          * stop is always done with the siglock held,
2246                          * so this check has no races.
2247                          */
2248                         if (!task_is_stopped(t) &&
2249                             task_set_jobctl_pending(t, signr | gstop)) {
2250                                 sig->group_stop_count++;
2251                                 if (likely(!(t->ptrace & PT_SEIZED)))
2252                                         signal_wake_up(t, 0);
2253                                 else
2254                                         ptrace_trap_notify(t);
2255                         }
2256                 }
2257         }
2258
2259         if (likely(!current->ptrace)) {
2260                 int notify = 0;
2261
2262                 /*
2263                  * If there are no other threads in the group, or if there
2264                  * is a group stop in progress and we are the last to stop,
2265                  * report to the parent.
2266                  */
2267                 if (task_participate_group_stop(current))
2268                         notify = CLD_STOPPED;
2269
2270                 set_special_state(TASK_STOPPED);
2271                 spin_unlock_irq(&current->sighand->siglock);
2272
2273                 /*
2274                  * Notify the parent of the group stop completion.  Because
2275                  * we're not holding either the siglock or tasklist_lock
2276                  * here, ptracer may attach inbetween; however, this is for
2277                  * group stop and should always be delivered to the real
2278                  * parent of the group leader.  The new ptracer will get
2279                  * its notification when this task transitions into
2280                  * TASK_TRACED.
2281                  */
2282                 if (notify) {
2283                         read_lock(&tasklist_lock);
2284                         do_notify_parent_cldstop(current, false, notify);
2285                         read_unlock(&tasklist_lock);
2286                 }
2287
2288                 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2289                 freezable_schedule();
2290                 return true;
2291         } else {
2292                 /*
2293                  * While ptraced, group stop is handled by STOP trap.
2294                  * Schedule it and let the caller deal with it.
2295                  */
2296                 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2297                 return false;
2298         }
2299 }
2300
2301 /**
2302  * do_jobctl_trap - take care of ptrace jobctl traps
2303  *
2304  * When PT_SEIZED, it's used for both group stop and explicit
2305  * SEIZE/INTERRUPT traps.  Both generate PTRACE_EVENT_STOP trap with
2306  * accompanying siginfo.  If stopped, lower eight bits of exit_code contain
2307  * the stop signal; otherwise, %SIGTRAP.
2308  *
2309  * When !PT_SEIZED, it's used only for group stop trap with stop signal
2310  * number as exit_code and no siginfo.
2311  *
2312  * CONTEXT:
2313  * Must be called with @current->sighand->siglock held, which may be
2314  * released and re-acquired before returning with intervening sleep.
2315  */
2316 static void do_jobctl_trap(void)
2317 {
2318         struct signal_struct *signal = current->signal;
2319         int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2320
2321         if (current->ptrace & PT_SEIZED) {
2322                 if (!signal->group_stop_count &&
2323                     !(signal->flags & SIGNAL_STOP_STOPPED))
2324                         signr = SIGTRAP;
2325                 WARN_ON_ONCE(!signr);
2326                 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2327                                  CLD_STOPPED);
2328         } else {
2329                 WARN_ON_ONCE(!signr);
2330                 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2331                 current->exit_code = 0;
2332         }
2333 }
2334
2335 static int ptrace_signal(int signr, siginfo_t *info)
2336 {
2337         /*
2338          * We do not check sig_kernel_stop(signr) but set this marker
2339          * unconditionally because we do not know whether debugger will
2340          * change signr. This flag has no meaning unless we are going
2341          * to stop after return from ptrace_stop(). In this case it will
2342          * be checked in do_signal_stop(), we should only stop if it was
2343          * not cleared by SIGCONT while we were sleeping. See also the
2344          * comment in dequeue_signal().
2345          */
2346         current->jobctl |= JOBCTL_STOP_DEQUEUED;
2347         ptrace_stop(signr, CLD_TRAPPED, 0, info);
2348
2349         /* We're back.  Did the debugger cancel the sig?  */
2350         signr = current->exit_code;
2351         if (signr == 0)
2352                 return signr;
2353
2354         current->exit_code = 0;
2355
2356         /*
2357          * Update the siginfo structure if the signal has
2358          * changed.  If the debugger wanted something
2359          * specific in the siginfo structure then it should
2360          * have updated *info via PTRACE_SETSIGINFO.
2361          */
2362         if (signr != info->si_signo) {
2363                 clear_siginfo(info);
2364                 info->si_signo = signr;
2365                 info->si_errno = 0;
2366                 info->si_code = SI_USER;
2367                 rcu_read_lock();
2368                 info->si_pid = task_pid_vnr(current->parent);
2369                 info->si_uid = from_kuid_munged(current_user_ns(),
2370                                                 task_uid(current->parent));
2371                 rcu_read_unlock();
2372         }
2373
2374         /* If the (new) signal is now blocked, requeue it.  */
2375         if (sigismember(&current->blocked, signr)) {
2376                 specific_send_sig_info(signr, info, current);
2377                 signr = 0;
2378         }
2379
2380         return signr;
2381 }
2382
2383 bool get_signal(struct ksignal *ksig)
2384 {
2385         struct sighand_struct *sighand = current->sighand;
2386         struct signal_struct *signal = current->signal;
2387         int signr;
2388
2389         if (unlikely(current->task_works))
2390                 task_work_run();
2391
2392         if (unlikely(uprobe_deny_signal()))
2393                 return false;
2394
2395         /*
2396          * Do this once, we can't return to user-mode if freezing() == T.
2397          * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2398          * thus do not need another check after return.
2399          */
2400         try_to_freeze();
2401
2402 relock:
2403         spin_lock_irq(&sighand->siglock);
2404         /*
2405          * Every stopped thread goes here after wakeup. Check to see if
2406          * we should notify the parent, prepare_signal(SIGCONT) encodes
2407          * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2408          */
2409         if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2410                 int why;
2411
2412                 if (signal->flags & SIGNAL_CLD_CONTINUED)
2413                         why = CLD_CONTINUED;
2414                 else
2415                         why = CLD_STOPPED;
2416
2417                 signal->flags &= ~SIGNAL_CLD_MASK;
2418
2419                 spin_unlock_irq(&sighand->siglock);
2420
2421                 /*
2422                  * Notify the parent that we're continuing.  This event is
2423                  * always per-process and doesn't make whole lot of sense
2424                  * for ptracers, who shouldn't consume the state via
2425                  * wait(2) either, but, for backward compatibility, notify
2426                  * the ptracer of the group leader too unless it's gonna be
2427                  * a duplicate.
2428                  */
2429                 read_lock(&tasklist_lock);
2430                 do_notify_parent_cldstop(current, false, why);
2431
2432                 if (ptrace_reparented(current->group_leader))
2433                         do_notify_parent_cldstop(current->group_leader,
2434                                                 true, why);
2435                 read_unlock(&tasklist_lock);
2436
2437                 goto relock;
2438         }
2439
2440         /* Has this task already been marked for death? */
2441         if (signal_group_exit(signal)) {
2442                 ksig->info.si_signo = signr = SIGKILL;
2443                 sigdelset(&current->pending.signal, SIGKILL);
2444                 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2445                                 &sighand->action[SIGKILL - 1]);
2446                 recalc_sigpending();
2447                 goto fatal;
2448         }
2449
2450         for (;;) {
2451                 struct k_sigaction *ka;
2452
2453                 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2454                     do_signal_stop(0))
2455                         goto relock;
2456
2457                 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2458                         do_jobctl_trap();
2459                         spin_unlock_irq(&sighand->siglock);
2460                         goto relock;
2461                 }
2462
2463                 /*
2464                  * Signals generated by the execution of an instruction
2465                  * need to be delivered before any other pending signals
2466                  * so that the instruction pointer in the signal stack
2467                  * frame points to the faulting instruction.
2468                  */
2469                 signr = dequeue_synchronous_signal(&ksig->info);
2470                 if (!signr)
2471                         signr = dequeue_signal(current, &current->blocked, &ksig->info);
2472
2473                 if (!signr)
2474                         break; /* will return 0 */
2475
2476                 if (unlikely(current->ptrace) && signr != SIGKILL) {
2477                         signr = ptrace_signal(signr, &ksig->info);
2478                         if (!signr)
2479                                 continue;
2480                 }
2481
2482                 ka = &sighand->action[signr-1];
2483
2484                 /* Trace actually delivered signals. */
2485                 trace_signal_deliver(signr, &ksig->info, ka);
2486
2487                 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing.  */
2488                         continue;
2489                 if (ka->sa.sa_handler != SIG_DFL) {
2490                         /* Run the handler.  */
2491                         ksig->ka = *ka;
2492
2493                         if (ka->sa.sa_flags & SA_ONESHOT)
2494                                 ka->sa.sa_handler = SIG_DFL;
2495
2496                         break; /* will return non-zero "signr" value */
2497                 }
2498
2499                 /*
2500                  * Now we are doing the default action for this signal.
2501                  */
2502                 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2503                         continue;
2504
2505                 /*
2506                  * Global init gets no signals it doesn't want.
2507                  * Container-init gets no signals it doesn't want from same
2508                  * container.
2509                  *
2510                  * Note that if global/container-init sees a sig_kernel_only()
2511                  * signal here, the signal must have been generated internally
2512                  * or must have come from an ancestor namespace. In either
2513                  * case, the signal cannot be dropped.
2514                  */
2515                 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2516                                 !sig_kernel_only(signr))
2517                         continue;
2518
2519                 if (sig_kernel_stop(signr)) {
2520                         /*
2521                          * The default action is to stop all threads in
2522                          * the thread group.  The job control signals
2523                          * do nothing in an orphaned pgrp, but SIGSTOP
2524                          * always works.  Note that siglock needs to be
2525                          * dropped during the call to is_orphaned_pgrp()
2526                          * because of lock ordering with tasklist_lock.
2527                          * This allows an intervening SIGCONT to be posted.
2528                          * We need to check for that and bail out if necessary.
2529                          */
2530                         if (signr != SIGSTOP) {
2531                                 spin_unlock_irq(&sighand->siglock);
2532
2533                                 /* signals can be posted during this window */
2534
2535                                 if (is_current_pgrp_orphaned())
2536                                         goto relock;
2537
2538                                 spin_lock_irq(&sighand->siglock);
2539                         }
2540
2541                         if (likely(do_signal_stop(ksig->info.si_signo))) {
2542                                 /* It released the siglock.  */
2543                                 goto relock;
2544                         }
2545
2546                         /*
2547                          * We didn't actually stop, due to a race
2548                          * with SIGCONT or something like that.
2549                          */
2550                         continue;
2551                 }
2552
2553         fatal:
2554                 spin_unlock_irq(&sighand->siglock);
2555
2556                 /*
2557                  * Anything else is fatal, maybe with a core dump.
2558                  */
2559                 current->flags |= PF_SIGNALED;
2560
2561                 if (sig_kernel_coredump(signr)) {
2562                         if (print_fatal_signals)
2563                                 print_fatal_signal(ksig->info.si_signo);
2564                         proc_coredump_connector(current);
2565                         /*
2566                          * If it was able to dump core, this kills all
2567                          * other threads in the group and synchronizes with
2568                          * their demise.  If we lost the race with another
2569                          * thread getting here, it set group_exit_code
2570                          * first and our do_group_exit call below will use
2571                          * that value and ignore the one we pass it.
2572                          */
2573                         do_coredump(&ksig->info);
2574                 }
2575
2576                 /*
2577                  * Death signals, no core dump.
2578                  */
2579                 do_group_exit(ksig->info.si_signo);
2580                 /* NOTREACHED */
2581         }
2582         spin_unlock_irq(&sighand->siglock);
2583
2584         ksig->sig = signr;
2585         return ksig->sig > 0;
2586 }
2587
2588 /**
2589  * signal_delivered - 
2590  * @ksig:               kernel signal struct
2591  * @stepping:           nonzero if debugger single-step or block-step in use
2592  *
2593  * This function should be called when a signal has successfully been
2594  * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
2595  * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2596  * is set in @ksig->ka.sa.sa_flags.  Tracing is notified.
2597  */
2598 static void signal_delivered(struct ksignal *ksig, int stepping)
2599 {
2600         sigset_t blocked;
2601
2602         /* A signal was successfully delivered, and the
2603            saved sigmask was stored on the signal frame,
2604            and will be restored by sigreturn.  So we can
2605            simply clear the restore sigmask flag.  */
2606         clear_restore_sigmask();
2607
2608         sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
2609         if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2610                 sigaddset(&blocked, ksig->sig);
2611         set_current_blocked(&blocked);
2612         tracehook_signal_handler(stepping);
2613 }
2614
2615 void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2616 {
2617         if (failed)
2618                 force_sigsegv(ksig->sig, current);
2619         else
2620                 signal_delivered(ksig, stepping);
2621 }
2622
2623 /*
2624  * It could be that complete_signal() picked us to notify about the
2625  * group-wide signal. Other threads should be notified now to take
2626  * the shared signals in @which since we will not.
2627  */
2628 static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2629 {
2630         sigset_t retarget;
2631         struct task_struct *t;
2632
2633         sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2634         if (sigisemptyset(&retarget))
2635                 return;
2636
2637         t = tsk;
2638         while_each_thread(tsk, t) {
2639                 if (t->flags & PF_EXITING)
2640                         continue;
2641
2642                 if (!has_pending_signals(&retarget, &t->blocked))
2643                         continue;
2644                 /* Remove the signals this thread can handle. */
2645                 sigandsets(&retarget, &retarget, &t->blocked);
2646
2647                 if (!signal_pending(t))
2648                         signal_wake_up(t, 0);
2649
2650                 if (sigisemptyset(&retarget))
2651                         break;
2652         }
2653 }
2654
2655 void exit_signals(struct task_struct *tsk)
2656 {
2657         int group_stop = 0;
2658         sigset_t unblocked;
2659
2660         /*
2661          * @tsk is about to have PF_EXITING set - lock out users which
2662          * expect stable threadgroup.
2663          */
2664         cgroup_threadgroup_change_begin(tsk);
2665
2666         if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2667                 tsk->flags |= PF_EXITING;
2668                 cgroup_threadgroup_change_end(tsk);
2669                 return;
2670         }
2671
2672         spin_lock_irq(&tsk->sighand->siglock);
2673         /*
2674          * From now this task is not visible for group-wide signals,
2675          * see wants_signal(), do_signal_stop().
2676          */
2677         tsk->flags |= PF_EXITING;
2678
2679         cgroup_threadgroup_change_end(tsk);
2680
2681         if (!signal_pending(tsk))
2682                 goto out;
2683
2684         unblocked = tsk->blocked;
2685         signotset(&unblocked);
2686         retarget_shared_pending(tsk, &unblocked);
2687
2688         if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2689             task_participate_group_stop(tsk))
2690                 group_stop = CLD_STOPPED;
2691 out:
2692         spin_unlock_irq(&tsk->sighand->siglock);
2693
2694         /*
2695          * If group stop has completed, deliver the notification.  This
2696          * should always go to the real parent of the group leader.
2697          */
2698         if (unlikely(group_stop)) {
2699                 read_lock(&tasklist_lock);
2700                 do_notify_parent_cldstop(tsk, false, group_stop);
2701                 read_unlock(&tasklist_lock);
2702         }
2703 }
2704
2705 EXPORT_SYMBOL(recalc_sigpending);
2706 EXPORT_SYMBOL_GPL(dequeue_signal);
2707 EXPORT_SYMBOL(flush_signals);
2708 EXPORT_SYMBOL(force_sig);
2709 EXPORT_SYMBOL(send_sig);
2710 EXPORT_SYMBOL(send_sig_info);
2711 EXPORT_SYMBOL(sigprocmask);
2712
2713 /*
2714  * System call entry points.
2715  */
2716
2717 /**
2718  *  sys_restart_syscall - restart a system call
2719  */
2720 SYSCALL_DEFINE0(restart_syscall)
2721 {
2722         struct restart_block *restart = &current->restart_block;
2723         return restart->fn(restart);
2724 }
2725
2726 long do_no_restart_syscall(struct restart_block *param)
2727 {
2728         return -EINTR;
2729 }
2730
2731 static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2732 {
2733         if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2734                 sigset_t newblocked;
2735                 /* A set of now blocked but previously unblocked signals. */
2736                 sigandnsets(&newblocked, newset, &current->blocked);
2737                 retarget_shared_pending(tsk, &newblocked);
2738         }
2739         tsk->blocked = *newset;
2740         recalc_sigpending();
2741 }
2742
2743 /**
2744  * set_current_blocked - change current->blocked mask
2745  * @newset: new mask
2746  *
2747  * It is wrong to change ->blocked directly, this helper should be used
2748  * to ensure the process can't miss a shared signal we are going to block.
2749  */
2750 void set_current_blocked(sigset_t *newset)
2751 {
2752         sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
2753         __set_current_blocked(newset);
2754 }
2755
2756 void __set_current_blocked(const sigset_t *newset)
2757 {
2758         struct task_struct *tsk = current;
2759
2760         /*
2761          * In case the signal mask hasn't changed, there is nothing we need
2762          * to do. The current->blocked shouldn't be modified by other task.
2763          */
2764         if (sigequalsets(&tsk->blocked, newset))
2765                 return;
2766
2767         spin_lock_irq(&tsk->sighand->siglock);
2768         __set_task_blocked(tsk, newset);
2769         spin_unlock_irq(&tsk->sighand->siglock);
2770 }
2771
2772 /*
2773  * This is also useful for kernel threads that want to temporarily
2774  * (or permanently) block certain signals.
2775  *
2776  * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2777  * interface happily blocks "unblockable" signals like SIGKILL
2778  * and friends.
2779  */
2780 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2781 {
2782         struct task_struct *tsk = current;
2783         sigset_t newset;
2784
2785         /* Lockless, only current can change ->blocked, never from irq */
2786         if (oldset)
2787                 *oldset = tsk->blocked;
2788
2789         switch (how) {
2790         case SIG_BLOCK:
2791                 sigorsets(&newset, &tsk->blocked, set);
2792                 break;
2793         case SIG_UNBLOCK:
2794                 sigandnsets(&newset, &tsk->blocked, set);
2795                 break;
2796         case SIG_SETMASK:
2797                 newset = *set;
2798                 break;
2799         default:
2800                 return -EINVAL;
2801         }
2802
2803         __set_current_blocked(&newset);
2804         return 0;
2805 }
2806
2807 /**
2808  *  sys_rt_sigprocmask - change the list of currently blocked signals
2809  *  @how: whether to add, remove, or set signals
2810  *  @nset: stores pending signals
2811  *  @oset: previous value of signal mask if non-null
2812  *  @sigsetsize: size of sigset_t type
2813  */
2814 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2815                 sigset_t __user *, oset, size_t, sigsetsize)
2816 {
2817         sigset_t old_set, new_set;
2818         int error;
2819
2820         /* XXX: Don't preclude handling different sized sigset_t's.  */
2821         if (sigsetsize != sizeof(sigset_t))
2822                 return -EINVAL;
2823
2824         old_set = current->blocked;
2825
2826         if (nset) {
2827                 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2828                         return -EFAULT;
2829                 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2830
2831                 error = sigprocmask(how, &new_set, NULL);
2832                 if (error)
2833                         return error;
2834         }
2835
2836         if (oset) {
2837                 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2838                         return -EFAULT;
2839         }
2840
2841         return 0;
2842 }
2843
2844 #ifdef CONFIG_COMPAT
2845 COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2846                 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
2847 {
2848         sigset_t old_set = current->blocked;
2849
2850         /* XXX: Don't preclude handling different sized sigset_t's.  */
2851         if (sigsetsize != sizeof(sigset_t))
2852                 return -EINVAL;
2853
2854         if (nset) {
2855                 sigset_t new_set;
2856                 int error;
2857                 if (get_compat_sigset(&new_set, nset))
2858                         return -EFAULT;
2859                 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2860
2861                 error = sigprocmask(how, &new_set, NULL);
2862                 if (error)
2863                         return error;
2864         }
2865         return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
2866 }
2867 #endif
2868
2869 static void do_sigpending(sigset_t *set)
2870 {
2871         spin_lock_irq(&current->sighand->siglock);
2872         sigorsets(set, &current->pending.signal,
2873                   &current->signal->shared_pending.signal);
2874         spin_unlock_irq(&current->sighand->siglock);
2875
2876         /* Outside the lock because only this thread touches it.  */
2877         sigandsets(set, &current->blocked, set);
2878 }
2879
2880 /**
2881  *  sys_rt_sigpending - examine a pending signal that has been raised
2882  *                      while blocked
2883  *  @uset: stores pending signals
2884  *  @sigsetsize: size of sigset_t type or larger
2885  */
2886 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
2887 {
2888         sigset_t set;
2889
2890         if (sigsetsize > sizeof(*uset))
2891                 return -EINVAL;
2892
2893         do_sigpending(&set);
2894
2895         if (copy_to_user(uset, &set, sigsetsize))
2896                 return -EFAULT;
2897
2898         return 0;
2899 }
2900
2901 #ifdef CONFIG_COMPAT
2902 COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2903                 compat_size_t, sigsetsize)
2904 {
2905         sigset_t set;
2906
2907         if (sigsetsize > sizeof(*uset))
2908                 return -EINVAL;
2909
2910         do_sigpending(&set);
2911
2912         return put_compat_sigset(uset, &set, sigsetsize);
2913 }
2914 #endif
2915
2916 enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
2917 {
2918         enum siginfo_layout layout = SIL_KILL;
2919         if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
2920                 static const struct {
2921                         unsigned char limit, layout;
2922                 } filter[] = {
2923                         [SIGILL]  = { NSIGILL,  SIL_FAULT },
2924                         [SIGFPE]  = { NSIGFPE,  SIL_FAULT },
2925                         [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
2926                         [SIGBUS]  = { NSIGBUS,  SIL_FAULT },
2927                         [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
2928 #if defined(SIGEMT) && defined(NSIGEMT)
2929                         [SIGEMT]  = { NSIGEMT,  SIL_FAULT },
2930 #endif
2931                         [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
2932                         [SIGPOLL] = { NSIGPOLL, SIL_POLL },
2933                         [SIGSYS]  = { NSIGSYS,  SIL_SYS },
2934                 };
2935                 if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit)) {
2936                         layout = filter[sig].layout;
2937                         /* Handle the exceptions */
2938                         if ((sig == SIGBUS) &&
2939                             (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
2940                                 layout = SIL_FAULT_MCEERR;
2941                         else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
2942                                 layout = SIL_FAULT_BNDERR;
2943 #ifdef SEGV_PKUERR
2944                         else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
2945                                 layout = SIL_FAULT_PKUERR;
2946 #endif
2947                 }
2948                 else if (si_code <= NSIGPOLL)
2949                         layout = SIL_POLL;
2950         } else {
2951                 if (si_code == SI_TIMER)
2952                         layout = SIL_TIMER;
2953                 else if (si_code == SI_SIGIO)
2954                         layout = SIL_POLL;
2955                 else if (si_code < 0)
2956                         layout = SIL_RT;
2957         }
2958         return layout;
2959 }
2960
2961 int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
2962 {
2963         if (copy_to_user(to, from , sizeof(struct siginfo)))
2964                 return -EFAULT;
2965         return 0;
2966 }
2967
2968 #ifdef CONFIG_COMPAT
2969 int copy_siginfo_to_user32(struct compat_siginfo __user *to,
2970                            const struct siginfo *from)
2971 #if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
2972 {
2973         return __copy_siginfo_to_user32(to, from, in_x32_syscall());
2974 }
2975 int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
2976                              const struct siginfo *from, bool x32_ABI)
2977 #endif
2978 {
2979         struct compat_siginfo new;
2980         memset(&new, 0, sizeof(new));
2981
2982         new.si_signo = from->si_signo;
2983         new.si_errno = from->si_errno;
2984         new.si_code  = from->si_code;
2985         switch(siginfo_layout(from->si_signo, from->si_code)) {
2986         case SIL_KILL:
2987                 new.si_pid = from->si_pid;
2988                 new.si_uid = from->si_uid;
2989                 break;
2990         case SIL_TIMER:
2991                 new.si_tid     = from->si_tid;
2992                 new.si_overrun = from->si_overrun;
2993                 new.si_int     = from->si_int;
2994                 break;
2995         case SIL_POLL:
2996                 new.si_band = from->si_band;
2997                 new.si_fd   = from->si_fd;
2998                 break;
2999         case SIL_FAULT:
3000                 new.si_addr = ptr_to_compat(from->si_addr);
3001 #ifdef __ARCH_SI_TRAPNO
3002                 new.si_trapno = from->si_trapno;
3003 #endif
3004                 break;
3005         case SIL_FAULT_MCEERR:
3006                 new.si_addr = ptr_to_compat(from->si_addr);
3007 #ifdef __ARCH_SI_TRAPNO
3008                 new.si_trapno = from->si_trapno;
3009 #endif
3010                 new.si_addr_lsb = from->si_addr_lsb;
3011                 break;
3012         case SIL_FAULT_BNDERR:
3013                 new.si_addr = ptr_to_compat(from->si_addr);
3014 #ifdef __ARCH_SI_TRAPNO
3015                 new.si_trapno = from->si_trapno;
3016 #endif
3017                 new.si_lower = ptr_to_compat(from->si_lower);
3018                 new.si_upper = ptr_to_compat(from->si_upper);
3019                 break;
3020         case SIL_FAULT_PKUERR:
3021                 new.si_addr = ptr_to_compat(from->si_addr);
3022 #ifdef __ARCH_SI_TRAPNO
3023                 new.si_trapno = from->si_trapno;
3024 #endif
3025                 new.si_pkey = from->si_pkey;
3026                 break;
3027         case SIL_CHLD:
3028                 new.si_pid    = from->si_pid;
3029                 new.si_uid    = from->si_uid;
3030                 new.si_status = from->si_status;
3031 #ifdef CONFIG_X86_X32_ABI
3032                 if (x32_ABI) {
3033                         new._sifields._sigchld_x32._utime = from->si_utime;
3034                         new._sifields._sigchld_x32._stime = from->si_stime;
3035                 } else
3036 #endif
3037                 {
3038                         new.si_utime = from->si_utime;
3039                         new.si_stime = from->si_stime;
3040                 }
3041                 break;
3042         case SIL_RT:
3043                 new.si_pid = from->si_pid;
3044                 new.si_uid = from->si_uid;
3045                 new.si_int = from->si_int;
3046                 break;
3047         case SIL_SYS:
3048                 new.si_call_addr = ptr_to_compat(from->si_call_addr);
3049                 new.si_syscall   = from->si_syscall;
3050                 new.si_arch      = from->si_arch;
3051                 break;
3052         }
3053
3054         if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
3055                 return -EFAULT;
3056
3057         return 0;
3058 }
3059
3060 int copy_siginfo_from_user32(struct siginfo *to,
3061                              const struct compat_siginfo __user *ufrom)
3062 {
3063         struct compat_siginfo from;
3064
3065         if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3066                 return -EFAULT;
3067
3068         clear_siginfo(to);
3069         to->si_signo = from.si_signo;
3070         to->si_errno = from.si_errno;
3071         to->si_code  = from.si_code;
3072         switch(siginfo_layout(from.si_signo, from.si_code)) {
3073         case SIL_KILL:
3074                 to->si_pid = from.si_pid;
3075                 to->si_uid = from.si_uid;
3076                 break;
3077         case SIL_TIMER:
3078                 to->si_tid     = from.si_tid;
3079                 to->si_overrun = from.si_overrun;
3080                 to->si_int     = from.si_int;
3081                 break;
3082         case SIL_POLL:
3083                 to->si_band = from.si_band;
3084                 to->si_fd   = from.si_fd;
3085                 break;
3086         case SIL_FAULT:
3087                 to->si_addr = compat_ptr(from.si_addr);
3088 #ifdef __ARCH_SI_TRAPNO
3089                 to->si_trapno = from.si_trapno;
3090 #endif
3091                 break;
3092         case SIL_FAULT_MCEERR:
3093                 to->si_addr = compat_ptr(from.si_addr);
3094 #ifdef __ARCH_SI_TRAPNO
3095                 to->si_trapno = from.si_trapno;
3096 #endif
3097                 to->si_addr_lsb = from.si_addr_lsb;
3098                 break;
3099         case SIL_FAULT_BNDERR:
3100                 to->si_addr = compat_ptr(from.si_addr);
3101 #ifdef __ARCH_SI_TRAPNO
3102                 to->si_trapno = from.si_trapno;
3103 #endif
3104                 to->si_lower = compat_ptr(from.si_lower);
3105                 to->si_upper = compat_ptr(from.si_upper);
3106                 break;
3107         case SIL_FAULT_PKUERR:
3108                 to->si_addr = compat_ptr(from.si_addr);
3109 #ifdef __ARCH_SI_TRAPNO
3110                 to->si_trapno = from.si_trapno;
3111 #endif
3112                 to->si_pkey = from.si_pkey;
3113                 break;
3114         case SIL_CHLD:
3115                 to->si_pid    = from.si_pid;
3116                 to->si_uid    = from.si_uid;
3117                 to->si_status = from.si_status;
3118 #ifdef CONFIG_X86_X32_ABI
3119                 if (in_x32_syscall()) {
3120                         to->si_utime = from._sifields._sigchld_x32._utime;
3121                         to->si_stime = from._sifields._sigchld_x32._stime;
3122                 } else
3123 #endif
3124                 {
3125                         to->si_utime = from.si_utime;
3126                         to->si_stime = from.si_stime;
3127                 }
3128                 break;
3129         case SIL_RT:
3130                 to->si_pid = from.si_pid;
3131                 to->si_uid = from.si_uid;
3132                 to->si_int = from.si_int;
3133                 break;
3134         case SIL_SYS:
3135                 to->si_call_addr = compat_ptr(from.si_call_addr);
3136                 to->si_syscall   = from.si_syscall;
3137                 to->si_arch      = from.si_arch;
3138                 break;
3139         }
3140         return 0;
3141 }
3142 #endif /* CONFIG_COMPAT */
3143
3144 /**
3145  *  do_sigtimedwait - wait for queued signals specified in @which
3146  *  @which: queued signals to wait for
3147  *  @info: if non-null, the signal's siginfo is returned here
3148  *  @ts: upper bound on process time suspension
3149  */
3150 static int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
3151                     const struct timespec *ts)
3152 {
3153         ktime_t *to = NULL, timeout = KTIME_MAX;
3154         struct task_struct *tsk = current;
3155         sigset_t mask = *which;
3156         int sig, ret = 0;
3157
3158         if (ts) {
3159                 if (!timespec_valid(ts))
3160                         return -EINVAL;
3161                 timeout = timespec_to_ktime(*ts);
3162                 to = &timeout;
3163         }
3164
3165         /*
3166          * Invert the set of allowed signals to get those we want to block.
3167          */
3168         sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
3169         signotset(&mask);
3170
3171         spin_lock_irq(&tsk->sighand->siglock);
3172         sig = dequeue_signal(tsk, &mask, info);
3173         if (!sig && timeout) {
3174                 /*
3175                  * None ready, temporarily unblock those we're interested
3176                  * while we are sleeping in so that we'll be awakened when
3177                  * they arrive. Unblocking is always fine, we can avoid
3178                  * set_current_blocked().
3179                  */
3180                 tsk->real_blocked = tsk->blocked;
3181                 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
3182                 recalc_sigpending();
3183                 spin_unlock_irq(&tsk->sighand->siglock);
3184
3185                 __set_current_state(TASK_INTERRUPTIBLE);
3186                 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
3187                                                          HRTIMER_MODE_REL);
3188                 spin_lock_irq(&tsk->sighand->siglock);
3189                 __set_task_blocked(tsk, &tsk->real_blocked);
3190                 sigemptyset(&tsk->real_blocked);
3191                 sig = dequeue_signal(tsk, &mask, info);
3192         }
3193         spin_unlock_irq(&tsk->sighand->siglock);
3194
3195         if (sig)
3196                 return sig;
3197         return ret ? -EINTR : -EAGAIN;
3198 }
3199
3200 /**
3201  *  sys_rt_sigtimedwait - synchronously wait for queued signals specified
3202  *                      in @uthese
3203  *  @uthese: queued signals to wait for
3204  *  @uinfo: if non-null, the signal's siginfo is returned here
3205  *  @uts: upper bound on process time suspension
3206  *  @sigsetsize: size of sigset_t type
3207  */
3208 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
3209                 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
3210                 size_t, sigsetsize)
3211 {
3212         sigset_t these;
3213         struct timespec ts;
3214         siginfo_t info;
3215         int ret;
3216
3217         /* XXX: Don't preclude handling different sized sigset_t's.  */
3218         if (sigsetsize != sizeof(sigset_t))
3219                 return -EINVAL;
3220
3221         if (copy_from_user(&these, uthese, sizeof(these)))
3222                 return -EFAULT;
3223
3224         if (uts) {
3225                 if (copy_from_user(&ts, uts, sizeof(ts)))
3226                         return -EFAULT;
3227         }
3228
3229         ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
3230
3231         if (ret > 0 && uinfo) {
3232                 if (copy_siginfo_to_user(uinfo, &info))
3233                         ret = -EFAULT;
3234         }
3235
3236         return ret;
3237 }
3238
3239 #ifdef CONFIG_COMPAT
3240 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
3241                 struct compat_siginfo __user *, uinfo,
3242                 struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
3243 {
3244         sigset_t s;
3245         struct timespec t;
3246         siginfo_t info;
3247         long ret;
3248
3249         if (sigsetsize != sizeof(sigset_t))
3250                 return -EINVAL;
3251
3252         if (get_compat_sigset(&s, uthese))
3253                 return -EFAULT;
3254
3255         if (uts) {
3256                 if (compat_get_timespec(&t, uts))
3257                         return -EFAULT;
3258         }
3259
3260         ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3261
3262         if (ret > 0 && uinfo) {
3263                 if (copy_siginfo_to_user32(uinfo, &info))
3264                         ret = -EFAULT;
3265         }
3266
3267         return ret;
3268 }
3269 #endif
3270
3271 /**
3272  *  sys_kill - send a signal to a process
3273  *  @pid: the PID of the process
3274  *  @sig: signal to be sent
3275  */
3276 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
3277 {
3278         struct siginfo info;
3279
3280         clear_siginfo(&info);
3281         info.si_signo = sig;
3282         info.si_errno = 0;
3283         info.si_code = SI_USER;
3284         info.si_pid = task_tgid_vnr(current);
3285         info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3286
3287         return kill_something_info(sig, &info, pid);
3288 }
3289
3290 static int
3291 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
3292 {
3293         struct task_struct *p;
3294         int error = -ESRCH;
3295
3296         rcu_read_lock();
3297         p = find_task_by_vpid(pid);
3298         if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
3299                 error = check_kill_permission(sig, info, p);
3300                 /*
3301                  * The null signal is a permissions and process existence
3302                  * probe.  No signal is actually delivered.
3303                  */
3304                 if (!error && sig) {
3305                         error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
3306                         /*
3307                          * If lock_task_sighand() failed we pretend the task
3308                          * dies after receiving the signal. The window is tiny,
3309                          * and the signal is private anyway.
3310                          */
3311                         if (unlikely(error == -ESRCH))
3312                                 error = 0;
3313                 }
3314         }
3315         rcu_read_unlock();
3316
3317         return error;
3318 }
3319
3320 static int do_tkill(pid_t tgid, pid_t pid, int sig)
3321 {
3322         struct siginfo info;
3323
3324         clear_siginfo(&info);
3325         info.si_signo = sig;
3326         info.si_errno = 0;
3327         info.si_code = SI_TKILL;
3328         info.si_pid = task_tgid_vnr(current);
3329         info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3330
3331         return do_send_specific(tgid, pid, sig, &info);
3332 }
3333
3334 /**
3335  *  sys_tgkill - send signal to one specific thread
3336  *  @tgid: the thread group ID of the thread
3337  *  @pid: the PID of the thread
3338  *  @sig: signal to be sent
3339  *
3340  *  This syscall also checks the @tgid and returns -ESRCH even if the PID
3341  *  exists but it's not belonging to the target process anymore. This
3342  *  method solves the problem of threads exiting and PIDs getting reused.
3343  */
3344 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
3345 {
3346         /* This is only valid for single tasks */
3347         if (pid <= 0 || tgid <= 0)
3348                 return -EINVAL;
3349
3350         return do_tkill(tgid, pid, sig);
3351 }
3352
3353 /**
3354  *  sys_tkill - send signal to one specific task
3355  *  @pid: the PID of the task
3356  *  @sig: signal to be sent
3357  *
3358  *  Send a signal to only one task, even if it's a CLONE_THREAD task.
3359  */
3360 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
3361 {
3362         /* This is only valid for single tasks */
3363         if (pid <= 0)
3364                 return -EINVAL;
3365
3366         return do_tkill(0, pid, sig);
3367 }
3368
3369 static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
3370 {
3371         /* Not even root can pretend to send signals from the kernel.
3372          * Nor can they impersonate a kill()/tgkill(), which adds source info.
3373          */
3374         if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3375             (task_pid_vnr(current) != pid))
3376                 return -EPERM;
3377
3378         info->si_signo = sig;
3379
3380         /* POSIX.1b doesn't mention process groups.  */
3381         return kill_proc_info(sig, info, pid);
3382 }
3383
3384 /**
3385  *  sys_rt_sigqueueinfo - send signal information to a signal
3386  *  @pid: the PID of the thread
3387  *  @sig: signal to be sent
3388  *  @uinfo: signal info to be sent
3389  */
3390 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3391                 siginfo_t __user *, uinfo)
3392 {
3393         siginfo_t info;
3394         if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3395                 return -EFAULT;
3396         return do_rt_sigqueueinfo(pid, sig, &info);
3397 }
3398
3399 #ifdef CONFIG_COMPAT
3400 COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3401                         compat_pid_t, pid,
3402                         int, sig,
3403                         struct compat_siginfo __user *, uinfo)
3404 {
3405         siginfo_t info;
3406         int ret = copy_siginfo_from_user32(&info, uinfo);
3407         if (unlikely(ret))
3408                 return ret;
3409         return do_rt_sigqueueinfo(pid, sig, &info);
3410 }
3411 #endif
3412
3413 static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
3414 {
3415         /* This is only valid for single tasks */
3416         if (pid <= 0 || tgid <= 0)
3417                 return -EINVAL;
3418
3419         /* Not even root can pretend to send signals from the kernel.
3420          * Nor can they impersonate a kill()/tgkill(), which adds source info.
3421          */
3422         if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3423             (task_pid_vnr(current) != pid))
3424                 return -EPERM;
3425
3426         info->si_signo = sig;
3427
3428         return do_send_specific(tgid, pid, sig, info);
3429 }
3430
3431 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3432                 siginfo_t __user *, uinfo)
3433 {
3434         siginfo_t info;
3435
3436         if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3437                 return -EFAULT;
3438
3439         return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3440 }
3441
3442 #ifdef CONFIG_COMPAT
3443 COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3444                         compat_pid_t, tgid,
3445                         compat_pid_t, pid,
3446                         int, sig,
3447                         struct compat_siginfo __user *, uinfo)
3448 {
3449         siginfo_t info;
3450
3451         if (copy_siginfo_from_user32(&info, uinfo))
3452                 return -EFAULT;
3453         return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3454 }
3455 #endif
3456
3457 /*
3458  * For kthreads only, must not be used if cloned with CLONE_SIGHAND
3459  */
3460 void kernel_sigaction(int sig, __sighandler_t action)
3461 {
3462         spin_lock_irq(&current->sighand->siglock);
3463         current->sighand->action[sig - 1].sa.sa_handler = action;
3464         if (action == SIG_IGN) {
3465                 sigset_t mask;
3466
3467                 sigemptyset(&mask);
3468                 sigaddset(&mask, sig);
3469
3470                 flush_sigqueue_mask(&mask, &current->signal->shared_pending);
3471                 flush_sigqueue_mask(&mask, &current->pending);
3472                 recalc_sigpending();
3473         }
3474         spin_unlock_irq(&current->sighand->siglock);
3475 }
3476 EXPORT_SYMBOL(kernel_sigaction);
3477
3478 void __weak sigaction_compat_abi(struct k_sigaction *act,
3479                 struct k_sigaction *oact)
3480 {
3481 }
3482
3483 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
3484 {
3485         struct task_struct *p = current, *t;
3486         struct k_sigaction *k;
3487         sigset_t mask;
3488
3489         if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
3490                 return -EINVAL;
3491
3492         k = &p->sighand->action[sig-1];
3493
3494         spin_lock_irq(&p->sighand->siglock);
3495         if (oact)
3496                 *oact = *k;
3497
3498         sigaction_compat_abi(act, oact);
3499
3500         if (act) {
3501                 sigdelsetmask(&act->sa.sa_mask,
3502                               sigmask(SIGKILL) | sigmask(SIGSTOP));
3503                 *k = *act;
3504                 /*
3505                  * POSIX 3.3.1.3:
3506                  *  "Setting a signal action to SIG_IGN for a signal that is
3507                  *   pending shall cause the pending signal to be discarded,
3508                  *   whether or not it is blocked."
3509                  *
3510                  *  "Setting a signal action to SIG_DFL for a signal that is
3511                  *   pending and whose default action is to ignore the signal
3512                  *   (for example, SIGCHLD), shall cause the pending signal to
3513                  *   be discarded, whether or not it is blocked"
3514                  */
3515                 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
3516                         sigemptyset(&mask);
3517                         sigaddset(&mask, sig);
3518                         flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3519                         for_each_thread(p, t)
3520                                 flush_sigqueue_mask(&mask, &t->pending);
3521                 }
3522         }
3523
3524         spin_unlock_irq(&p->sighand->siglock);
3525         return 0;
3526 }
3527
3528 static int
3529 do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
3530                 size_t min_ss_size)
3531 {
3532         struct task_struct *t = current;
3533
3534         if (oss) {
3535                 memset(oss, 0, sizeof(stack_t));
3536                 oss->ss_sp = (void __user *) t->sas_ss_sp;
3537                 oss->ss_size = t->sas_ss_size;
3538                 oss->ss_flags = sas_ss_flags(sp) |
3539                         (current->sas_ss_flags & SS_FLAG_BITS);
3540         }
3541
3542         if (ss) {
3543                 void __user *ss_sp = ss->ss_sp;
3544                 size_t ss_size = ss->ss_size;
3545                 unsigned ss_flags = ss->ss_flags;
3546                 int ss_mode;
3547
3548                 if (unlikely(on_sig_stack(sp)))
3549                         return -EPERM;
3550
3551                 ss_mode = ss_flags & ~SS_FLAG_BITS;
3552                 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3553                                 ss_mode != 0))
3554                         return -EINVAL;
3555
3556                 if (ss_mode == SS_DISABLE) {
3557                         ss_size = 0;
3558                         ss_sp = NULL;
3559                 } else {
3560                         if (unlikely(ss_size < min_ss_size))
3561                                 return -ENOMEM;
3562                 }
3563
3564                 t->sas_ss_sp = (unsigned long) ss_sp;
3565                 t->sas_ss_size = ss_size;
3566                 t->sas_ss_flags = ss_flags;
3567         }
3568         return 0;
3569 }
3570
3571 SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3572 {
3573         stack_t new, old;
3574         int err;
3575         if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
3576                 return -EFAULT;
3577         err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
3578                               current_user_stack_pointer(),
3579                               MINSIGSTKSZ);
3580         if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
3581                 err = -EFAULT;
3582         return err;
3583 }
3584
3585 int restore_altstack(const stack_t __user *uss)
3586 {
3587         stack_t new;
3588         if (copy_from_user(&new, uss, sizeof(stack_t)))
3589                 return -EFAULT;
3590         (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
3591                              MINSIGSTKSZ);
3592         /* squash all but EFAULT for now */
3593         return 0;
3594 }
3595
3596 int __save_altstack(stack_t __user *uss, unsigned long sp)
3597 {
3598         struct task_struct *t = current;
3599         int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3600                 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3601                 __put_user(t->sas_ss_size, &uss->ss_size);
3602         if (err)
3603                 return err;
3604         if (t->sas_ss_flags & SS_AUTODISARM)
3605                 sas_ss_reset(t);
3606         return 0;
3607 }
3608
3609 #ifdef CONFIG_COMPAT
3610 static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
3611                                  compat_stack_t __user *uoss_ptr)
3612 {
3613         stack_t uss, uoss;
3614         int ret;
3615
3616         if (uss_ptr) {
3617                 compat_stack_t uss32;
3618                 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3619                         return -EFAULT;
3620                 uss.ss_sp = compat_ptr(uss32.ss_sp);
3621                 uss.ss_flags = uss32.ss_flags;
3622                 uss.ss_size = uss32.ss_size;
3623         }
3624         ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
3625                              compat_user_stack_pointer(),
3626                              COMPAT_MINSIGSTKSZ);
3627         if (ret >= 0 && uoss_ptr)  {
3628                 compat_stack_t old;
3629                 memset(&old, 0, sizeof(old));
3630                 old.ss_sp = ptr_to_compat(uoss.ss_sp);
3631                 old.ss_flags = uoss.ss_flags;
3632                 old.ss_size = uoss.ss_size;
3633                 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
3634                         ret = -EFAULT;
3635         }
3636         return ret;
3637 }
3638
3639 COMPAT_SYSCALL_DEFINE2(sigaltstack,
3640                         const compat_stack_t __user *, uss_ptr,
3641                         compat_stack_t __user *, uoss_ptr)
3642 {
3643         return do_compat_sigaltstack(uss_ptr, uoss_ptr);
3644 }
3645
3646 int compat_restore_altstack(const compat_stack_t __user *uss)
3647 {
3648         int err = do_compat_sigaltstack(uss, NULL);
3649         /* squash all but -EFAULT for now */
3650         return err == -EFAULT ? err : 0;
3651 }
3652
3653 int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3654 {
3655         int err;
3656         struct task_struct *t = current;
3657         err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3658                          &uss->ss_sp) |
3659                 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3660                 __put_user(t->sas_ss_size, &uss->ss_size);
3661         if (err)
3662                 return err;
3663         if (t->sas_ss_flags & SS_AUTODISARM)
3664                 sas_ss_reset(t);
3665         return 0;
3666 }
3667 #endif
3668
3669 #ifdef __ARCH_WANT_SYS_SIGPENDING
3670
3671 /**
3672  *  sys_sigpending - examine pending signals
3673  *  @uset: where mask of pending signal is returned
3674  */
3675 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
3676 {
3677         sigset_t set;
3678
3679         if (sizeof(old_sigset_t) > sizeof(*uset))
3680                 return -EINVAL;
3681
3682         do_sigpending(&set);
3683
3684         if (copy_to_user(uset, &set, sizeof(old_sigset_t)))
3685                 return -EFAULT;
3686
3687         return 0;
3688 }
3689
3690 #ifdef CONFIG_COMPAT
3691 COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
3692 {
3693         sigset_t set;
3694
3695         do_sigpending(&set);
3696
3697         return put_user(set.sig[0], set32);
3698 }
3699 #endif
3700
3701 #endif
3702
3703 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
3704 /**
3705  *  sys_sigprocmask - examine and change blocked signals
3706  *  @how: whether to add, remove, or set signals
3707  *  @nset: signals to add or remove (if non-null)
3708  *  @oset: previous value of signal mask if non-null
3709  *
3710  * Some platforms have their own version with special arguments;
3711  * others support only sys_rt_sigprocmask.
3712  */
3713
3714 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
3715                 old_sigset_t __user *, oset)
3716 {
3717         old_sigset_t old_set, new_set;
3718         sigset_t new_blocked;
3719
3720         old_set = current->blocked.sig[0];
3721
3722         if (nset) {
3723                 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3724                         return -EFAULT;
3725
3726                 new_blocked = current->blocked;
3727
3728                 switch (how) {
3729                 case SIG_BLOCK:
3730                         sigaddsetmask(&new_blocked, new_set);
3731                         break;
3732                 case SIG_UNBLOCK:
3733                         sigdelsetmask(&new_blocked, new_set);
3734                         break;
3735                 case SIG_SETMASK:
3736                         new_blocked.sig[0] = new_set;
3737                         break;
3738                 default:
3739                         return -EINVAL;
3740                 }
3741
3742                 set_current_blocked(&new_blocked);
3743         }
3744
3745         if (oset) {
3746                 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3747                         return -EFAULT;
3748         }
3749
3750         return 0;
3751 }
3752 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3753
3754 #ifndef CONFIG_ODD_RT_SIGACTION
3755 /**
3756  *  sys_rt_sigaction - alter an action taken by a process
3757  *  @sig: signal to be sent
3758  *  @act: new sigaction
3759  *  @oact: used to save the previous sigaction
3760  *  @sigsetsize: size of sigset_t type
3761  */
3762 SYSCALL_DEFINE4(rt_sigaction, int, sig,
3763                 const struct sigaction __user *, act,
3764                 struct sigaction __user *, oact,
3765                 size_t, sigsetsize)
3766 {
3767         struct k_sigaction new_sa, old_sa;
3768         int ret;
3769
3770         /* XXX: Don't preclude handling different sized sigset_t's.  */
3771         if (sigsetsize != sizeof(sigset_t))
3772                 return -EINVAL;
3773
3774         if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3775                 return -EFAULT;
3776
3777         ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3778         if (ret)
3779                 return ret;
3780
3781         if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3782                 return -EFAULT;
3783
3784         return 0;
3785 }
3786 #ifdef CONFIG_COMPAT
3787 COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
3788                 const struct compat_sigaction __user *, act,
3789                 struct compat_sigaction __user *, oact,
3790                 compat_size_t, sigsetsize)
3791 {
3792         struct k_sigaction new_ka, old_ka;
3793 #ifdef __ARCH_HAS_SA_RESTORER
3794         compat_uptr_t restorer;
3795 #endif
3796         int ret;
3797
3798         /* XXX: Don't preclude handling different sized sigset_t's.  */
3799         if (sigsetsize != sizeof(compat_sigset_t))
3800                 return -EINVAL;
3801
3802         if (act) {
3803                 compat_uptr_t handler;
3804                 ret = get_user(handler, &act->sa_handler);
3805                 new_ka.sa.sa_handler = compat_ptr(handler);
3806 #ifdef __ARCH_HAS_SA_RESTORER
3807                 ret |= get_user(restorer, &act->sa_restorer);
3808                 new_ka.sa.sa_restorer = compat_ptr(restorer);
3809 #endif
3810                 ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
3811                 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
3812                 if (ret)
3813                         return -EFAULT;
3814         }
3815
3816         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3817         if (!ret && oact) {
3818                 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), 
3819                                &oact->sa_handler);
3820                 ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
3821                                          sizeof(oact->sa_mask));
3822                 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
3823 #ifdef __ARCH_HAS_SA_RESTORER
3824                 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3825                                 &oact->sa_restorer);
3826 #endif
3827         }
3828         return ret;
3829 }
3830 #endif
3831 #endif /* !CONFIG_ODD_RT_SIGACTION */
3832
3833 #ifdef CONFIG_OLD_SIGACTION
3834 SYSCALL_DEFINE3(sigaction, int, sig,
3835                 const struct old_sigaction __user *, act,
3836                 struct old_sigaction __user *, oact)
3837 {
3838         struct k_sigaction new_ka, old_ka;
3839         int ret;
3840
3841         if (act) {
3842                 old_sigset_t mask;
3843                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3844                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
3845                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
3846                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3847                     __get_user(mask, &act->sa_mask))
3848                         return -EFAULT;
3849 #ifdef __ARCH_HAS_KA_RESTORER
3850                 new_ka.ka_restorer = NULL;
3851 #endif
3852                 siginitset(&new_ka.sa.sa_mask, mask);
3853         }
3854
3855         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3856
3857         if (!ret && oact) {
3858                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3859                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
3860                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
3861                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3862                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3863                         return -EFAULT;
3864         }
3865
3866         return ret;
3867 }
3868 #endif
3869 #ifdef CONFIG_COMPAT_OLD_SIGACTION
3870 COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
3871                 const struct compat_old_sigaction __user *, act,
3872                 struct compat_old_sigaction __user *, oact)
3873 {
3874         struct k_sigaction new_ka, old_ka;
3875         int ret;
3876         compat_old_sigset_t mask;
3877         compat_uptr_t handler, restorer;
3878
3879         if (act) {
3880                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3881                     __get_user(handler, &act->sa_handler) ||
3882                     __get_user(restorer, &act->sa_restorer) ||
3883                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3884                     __get_user(mask, &act->sa_mask))
3885                         return -EFAULT;
3886
3887 #ifdef __ARCH_HAS_KA_RESTORER
3888                 new_ka.ka_restorer = NULL;
3889 #endif
3890                 new_ka.sa.sa_handler = compat_ptr(handler);
3891                 new_ka.sa.sa_restorer = compat_ptr(restorer);
3892                 siginitset(&new_ka.sa.sa_mask, mask);
3893         }
3894
3895         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3896
3897         if (!ret && oact) {
3898                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3899                     __put_user(ptr_to_compat(old_ka.sa.sa_handler),
3900                                &oact->sa_handler) ||
3901                     __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3902                                &oact->sa_restorer) ||
3903                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3904                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3905                         return -EFAULT;
3906         }
3907         return ret;
3908 }
3909 #endif
3910
3911 #ifdef CONFIG_SGETMASK_SYSCALL
3912
3913 /*
3914  * For backwards compatibility.  Functionality superseded by sigprocmask.
3915  */
3916 SYSCALL_DEFINE0(sgetmask)
3917 {
3918         /* SMP safe */
3919         return current->blocked.sig[0];
3920 }
3921
3922 SYSCALL_DEFINE1(ssetmask, int, newmask)
3923 {
3924         int old = current->blocked.sig[0];
3925         sigset_t newset;
3926
3927         siginitset(&newset, newmask);
3928         set_current_blocked(&newset);
3929
3930         return old;
3931 }
3932 #endif /* CONFIG_SGETMASK_SYSCALL */
3933
3934 #ifdef __ARCH_WANT_SYS_SIGNAL
3935 /*
3936  * For backwards compatibility.  Functionality superseded by sigaction.
3937  */
3938 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
3939 {
3940         struct k_sigaction new_sa, old_sa;
3941         int ret;
3942
3943         new_sa.sa.sa_handler = handler;
3944         new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
3945         sigemptyset(&new_sa.sa.sa_mask);
3946
3947         ret = do_sigaction(sig, &new_sa, &old_sa);
3948
3949         return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3950 }
3951 #endif /* __ARCH_WANT_SYS_SIGNAL */
3952
3953 #ifdef __ARCH_WANT_SYS_PAUSE
3954
3955 SYSCALL_DEFINE0(pause)
3956 {
3957         while (!signal_pending(current)) {
3958                 __set_current_state(TASK_INTERRUPTIBLE);
3959                 schedule();
3960         }
3961         return -ERESTARTNOHAND;
3962 }
3963
3964 #endif
3965
3966 static int sigsuspend(sigset_t *set)
3967 {
3968         current->saved_sigmask = current->blocked;
3969         set_current_blocked(set);
3970
3971         while (!signal_pending(current)) {
3972                 __set_current_state(TASK_INTERRUPTIBLE);
3973                 schedule();
3974         }
3975         set_restore_sigmask();
3976         return -ERESTARTNOHAND;
3977 }
3978
3979 /**
3980  *  sys_rt_sigsuspend - replace the signal mask for a value with the
3981  *      @unewset value until a signal is received
3982  *  @unewset: new signal mask value
3983  *  @sigsetsize: size of sigset_t type
3984  */
3985 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
3986 {
3987         sigset_t newset;
3988
3989         /* XXX: Don't preclude handling different sized sigset_t's.  */
3990         if (sigsetsize != sizeof(sigset_t))
3991                 return -EINVAL;
3992
3993         if (copy_from_user(&newset, unewset, sizeof(newset)))
3994                 return -EFAULT;
3995         return sigsuspend(&newset);
3996 }
3997  
3998 #ifdef CONFIG_COMPAT
3999 COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
4000 {
4001         sigset_t newset;
4002
4003         /* XXX: Don't preclude handling different sized sigset_t's.  */
4004         if (sigsetsize != sizeof(sigset_t))
4005                 return -EINVAL;
4006
4007         if (get_compat_sigset(&newset, unewset))
4008                 return -EFAULT;
4009         return sigsuspend(&newset);
4010 }
4011 #endif
4012
4013 #ifdef CONFIG_OLD_SIGSUSPEND
4014 SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
4015 {
4016         sigset_t blocked;
4017         siginitset(&blocked, mask);
4018         return sigsuspend(&blocked);
4019 }
4020 #endif
4021 #ifdef CONFIG_OLD_SIGSUSPEND3
4022 SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
4023 {
4024         sigset_t blocked;
4025         siginitset(&blocked, mask);
4026         return sigsuspend(&blocked);
4027 }
4028 #endif
4029
4030 __weak const char *arch_vma_name(struct vm_area_struct *vma)
4031 {
4032         return NULL;
4033 }
4034
4035 void __init signals_init(void)
4036 {
4037         /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
4038         BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
4039                 != offsetof(struct siginfo, _sifields._pad));
4040         BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
4041
4042         sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
4043 }
4044
4045 #ifdef CONFIG_KGDB_KDB
4046 #include <linux/kdb.h>
4047 /*
4048  * kdb_send_sig - Allows kdb to send signals without exposing
4049  * signal internals.  This function checks if the required locks are
4050  * available before calling the main signal code, to avoid kdb
4051  * deadlocks.
4052  */
4053 void kdb_send_sig(struct task_struct *t, int sig)
4054 {
4055         static struct task_struct *kdb_prev_t;
4056         int new_t, ret;
4057         if (!spin_trylock(&t->sighand->siglock)) {
4058                 kdb_printf("Can't do kill command now.\n"
4059                            "The sigmask lock is held somewhere else in "
4060                            "kernel, try again later\n");
4061                 return;
4062         }
4063         new_t = kdb_prev_t != t;
4064         kdb_prev_t = t;
4065         if (t->state != TASK_RUNNING && new_t) {
4066                 spin_unlock(&t->sighand->siglock);
4067                 kdb_printf("Process is not RUNNING, sending a signal from "
4068                            "kdb risks deadlock\n"
4069                            "on the run queue locks. "
4070                            "The signal has _not_ been sent.\n"
4071                            "Reissue the kill command if you want to risk "
4072                            "the deadlock.\n");
4073                 return;
4074         }
4075         ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
4076         spin_unlock(&t->sighand->siglock);
4077         if (ret)
4078                 kdb_printf("Fail to deliver Signal %d to process %d.\n",
4079                            sig, t->pid);
4080         else
4081                 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
4082 }
4083 #endif  /* CONFIG_KGDB_KDB */