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