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