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