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