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