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