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