3714 lines · 95.9 KB · C
RawDownload
/* *  linux/kernel/signal.c * *  Copyright (C) 1991, 1992  Linus Torvalds * *  1997-11-02  Modified for POSIX.1b signals by Richard Henderson * *  2003-06-02  Jim Houston - Concurrent Computer Corp. *		Changes to use preallocated sigqueue structures *		to allow signals to be sent reliably. */ #include <linux/slab.h>#include <linux/export.h>#include <linux/init.h>#include <linux/sched/mm.h>#include <linux/sched/user.h>#include <linux/sched/debug.h>#include <linux/sched/task.h>#include <linux/sched/task_stack.h>#include <linux/sched/cputime.h>#include <linux/fs.h>#include <linux/tty.h>#include <linux/binfmts.h>#include <linux/coredump.h>#include <linux/security.h>#include <linux/syscalls.h>#include <linux/ptrace.h>#include <linux/signal.h>#include <linux/signalfd.h>#include <linux/ratelimit.h>#include <linux/tracehook.h>#include <linux/capability.h>#include <linux/freezer.h>#include <linux/pid_namespace.h>#include <linux/nsproxy.h>#include <linux/user_namespace.h>#include <linux/uprobes.h>#include <linux/compat.h>#include <linux/cn_proc.h>#include <linux/compiler.h>#include <linux/posix-timers.h> #define CREATE_TRACE_POINTS#include <trace/events/signal.h> #include <asm/param.h>#include <linux/uaccess.h>#include <asm/unistd.h>#include <asm/siginfo.h>#include <asm/cacheflush.h>#include "audit.h"	/* audit_signal_info() */ /* * SLAB caches for signal bits. */ static struct kmem_cache *sigqueue_cachep; int print_fatal_signals __read_mostly; static void __user *sig_handler(struct task_struct *t, int sig){	return t->sighand->action[sig - 1].sa.sa_handler;} static int sig_handler_ignored(void __user *handler, int sig){	/* Is it explicitly or implicitly ignored? */	return handler == SIG_IGN ||		(handler == SIG_DFL && sig_kernel_ignore(sig));} static int sig_task_ignored(struct task_struct *t, int sig, bool force){	void __user *handler; 	handler = sig_handler(t, sig); 	if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&			handler == SIG_DFL && !force)		return 1; 	return sig_handler_ignored(handler, sig);} static int sig_ignored(struct task_struct *t, int sig, bool force){	/*	 * Blocked signals are never ignored, since the	 * signal handler may change by the time it is	 * unblocked.	 */	if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))		return 0; 	if (!sig_task_ignored(t, sig, force))		return 0; 	/*	 * Tracers may want to know about even ignored signals.	 */	return !t->ptrace;} /* * Re-calculate pending state from the set of locally pending * signals, globally pending signals, and blocked signals. */static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked){	unsigned long ready;	long i; 	switch (_NSIG_WORDS) {	default:		for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)			ready |= signal->sig[i] &~ blocked->sig[i];		break; 	case 4: ready  = signal->sig[3] &~ blocked->sig[3];		ready |= signal->sig[2] &~ blocked->sig[2];		ready |= signal->sig[1] &~ blocked->sig[1];		ready |= signal->sig[0] &~ blocked->sig[0];		break; 	case 2: ready  = signal->sig[1] &~ blocked->sig[1];		ready |= signal->sig[0] &~ blocked->sig[0];		break; 	case 1: ready  = signal->sig[0] &~ blocked->sig[0];	}	return ready !=	0;} #define PENDING(p,b) has_pending_signals(&(p)->signal, (b)) static int recalc_sigpending_tsk(struct task_struct *t){	if ((t->jobctl & JOBCTL_PENDING_MASK) ||	    PENDING(&t->pending, &t->blocked) ||	    PENDING(&t->signal->shared_pending, &t->blocked)) {		set_tsk_thread_flag(t, TIF_SIGPENDING);		return 1;	}	/*	 * We must never clear the flag in another thread, or in current	 * when it's possible the current syscall is returning -ERESTART*.	 * So we don't clear it here, and only callers who know they should do.	 */	return 0;} /* * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up. * This is superfluous when called on current, the wakeup is a harmless no-op. */void recalc_sigpending_and_wake(struct task_struct *t){	if (recalc_sigpending_tsk(t))		signal_wake_up(t, 0);} void recalc_sigpending(void){	if (!recalc_sigpending_tsk(current) && !freezing(current))		clear_thread_flag(TIF_SIGPENDING); } /* Given the mask, find the first available signal that should be serviced. */ #define SYNCHRONOUS_MASK \	(sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \	 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS)) int next_signal(struct sigpending *pending, sigset_t *mask){	unsigned long i, *s, *m, x;	int sig = 0; 	s = pending->signal.sig;	m = mask->sig; 	/*	 * Handle the first word specially: it contains the	 * synchronous signals that need to be dequeued first.	 */	x = *s &~ *m;	if (x) {		if (x & SYNCHRONOUS_MASK)			x &= SYNCHRONOUS_MASK;		sig = ffz(~x) + 1;		return sig;	} 	switch (_NSIG_WORDS) {	default:		for (i = 1; i < _NSIG_WORDS; ++i) {			x = *++s &~ *++m;			if (!x)				continue;			sig = ffz(~x) + i*_NSIG_BPW + 1;			break;		}		break; 	case 2:		x = s[1] &~ m[1];		if (!x)			break;		sig = ffz(~x) + _NSIG_BPW + 1;		break; 	case 1:		/* Nothing to do */		break;	} 	return sig;} static inline void print_dropped_signal(int sig){	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10); 	if (!print_fatal_signals)		return; 	if (!__ratelimit(&ratelimit_state))		return; 	pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",				current->comm, current->pid, sig);} /** * task_set_jobctl_pending - set jobctl pending bits * @task: target task * @mask: pending bits to set * * Clear @mask from @task->jobctl.  @mask must be subset of * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK | * %JOBCTL_TRAPPING.  If stop signo is being set, the existing signo is * cleared.  If @task is already being killed or exiting, this function * becomes noop. * * CONTEXT: * Must be called with @task->sighand->siglock held. * * RETURNS: * %true if @mask is set, %false if made noop because @task was dying. */bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask){	BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |			JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));	BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK)); 	if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))		return false; 	if (mask & JOBCTL_STOP_SIGMASK)		task->jobctl &= ~JOBCTL_STOP_SIGMASK; 	task->jobctl |= mask;	return true;} /** * task_clear_jobctl_trapping - clear jobctl trapping bit * @task: target task * * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED. * Clear it and wake up the ptracer.  Note that we don't need any further * locking.  @task->siglock guarantees that @task->parent points to the * ptracer. * * CONTEXT: * Must be called with @task->sighand->siglock held. */void task_clear_jobctl_trapping(struct task_struct *task){	if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {		task->jobctl &= ~JOBCTL_TRAPPING;		smp_mb();	/* advised by wake_up_bit() */		wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);	}} /** * task_clear_jobctl_pending - clear jobctl pending bits * @task: target task * @mask: pending bits to clear * * Clear @mask from @task->jobctl.  @mask must be subset of * %JOBCTL_PENDING_MASK.  If %JOBCTL_STOP_PENDING is being cleared, other * STOP bits are cleared together. * * If clearing of @mask leaves no stop or trap pending, this function calls * task_clear_jobctl_trapping(). * * CONTEXT: * Must be called with @task->sighand->siglock held. */void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask){	BUG_ON(mask & ~JOBCTL_PENDING_MASK); 	if (mask & JOBCTL_STOP_PENDING)		mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED; 	task->jobctl &= ~mask; 	if (!(task->jobctl & JOBCTL_PENDING_MASK))		task_clear_jobctl_trapping(task);} /** * task_participate_group_stop - participate in a group stop * @task: task participating in a group stop * * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop. * Group stop states are cleared and the group stop count is consumed if * %JOBCTL_STOP_CONSUME was set.  If the consumption completes the group * stop, the appropriate %SIGNAL_* flags are set. * * CONTEXT: * Must be called with @task->sighand->siglock held. * * RETURNS: * %true if group stop completion should be notified to the parent, %false * otherwise. */static bool task_participate_group_stop(struct task_struct *task){	struct signal_struct *sig = task->signal;	bool consume = task->jobctl & JOBCTL_STOP_CONSUME; 	WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING)); 	task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING); 	if (!consume)		return false; 	if (!WARN_ON_ONCE(sig->group_stop_count == 0))		sig->group_stop_count--; 	/*	 * Tell the caller to notify completion iff we are entering into a	 * fresh group stop.  Read comment in do_signal_stop() for details.	 */	if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {		signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);		return true;	}	return false;} /* * allocate a new signal queue record * - this may be called without locks if and only if t == current, otherwise an *   appropriate lock must be held to stop the target task from exiting */static struct sigqueue *__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit){	struct sigqueue *q = NULL;	struct user_struct *user; 	/*	 * Protect access to @t credentials. This can go away when all	 * callers hold rcu read lock.	 */	rcu_read_lock();	user = get_uid(__task_cred(t)->user);	atomic_inc(&user->sigpending);	rcu_read_unlock(); 	if (override_rlimit ||	    atomic_read(&user->sigpending) <=			task_rlimit(t, RLIMIT_SIGPENDING)) {		q = kmem_cache_alloc(sigqueue_cachep, flags);	} else {		print_dropped_signal(sig);	} 	if (unlikely(q == NULL)) {		atomic_dec(&user->sigpending);		free_uid(user);	} else {		INIT_LIST_HEAD(&q->list);		q->flags = 0;		q->user = user;	} 	return q;} static void __sigqueue_free(struct sigqueue *q){	if (q->flags & SIGQUEUE_PREALLOC)		return;	atomic_dec(&q->user->sigpending);	free_uid(q->user);	kmem_cache_free(sigqueue_cachep, q);} void flush_sigqueue(struct sigpending *queue){	struct sigqueue *q; 	sigemptyset(&queue->signal);	while (!list_empty(&queue->list)) {		q = list_entry(queue->list.next, struct sigqueue , list);		list_del_init(&q->list);		__sigqueue_free(q);	}} /* * Flush all pending signals for this kthread. */void flush_signals(struct task_struct *t){	unsigned long flags; 	spin_lock_irqsave(&t->sighand->siglock, flags);	clear_tsk_thread_flag(t, TIF_SIGPENDING);	flush_sigqueue(&t->pending);	flush_sigqueue(&t->signal->shared_pending);	spin_unlock_irqrestore(&t->sighand->siglock, flags);} #ifdef CONFIG_POSIX_TIMERSstatic void __flush_itimer_signals(struct sigpending *pending){	sigset_t signal, retain;	struct sigqueue *q, *n; 	signal = pending->signal;	sigemptyset(&retain); 	list_for_each_entry_safe(q, n, &pending->list, list) {		int sig = q->info.si_signo; 		if (likely(q->info.si_code != SI_TIMER)) {			sigaddset(&retain, sig);		} else {			sigdelset(&signal, sig);			list_del_init(&q->list);			__sigqueue_free(q);		}	} 	sigorsets(&pending->signal, &signal, &retain);} void flush_itimer_signals(void){	struct task_struct *tsk = current;	unsigned long flags; 	spin_lock_irqsave(&tsk->sighand->siglock, flags);	__flush_itimer_signals(&tsk->pending);	__flush_itimer_signals(&tsk->signal->shared_pending);	spin_unlock_irqrestore(&tsk->sighand->siglock, flags);}#endif void ignore_signals(struct task_struct *t){	int i; 	for (i = 0; i < _NSIG; ++i)		t->sighand->action[i].sa.sa_handler = SIG_IGN; 	flush_signals(t);} /* * Flush all handlers for a task. */ voidflush_signal_handlers(struct task_struct *t, int force_default){	int i;	struct k_sigaction *ka = &t->sighand->action[0];	for (i = _NSIG ; i != 0 ; i--) {		if (force_default || ka->sa.sa_handler != SIG_IGN)			ka->sa.sa_handler = SIG_DFL;		ka->sa.sa_flags = 0;#ifdef __ARCH_HAS_SA_RESTORER		ka->sa.sa_restorer = NULL;#endif		sigemptyset(&ka->sa.sa_mask);		ka++;	}

Lines 1–500 of 3714.