signal.c 115 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  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>
14
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
15
#include <linux/init.h>
16
#include <linux/sched/mm.h>
17
#include <linux/sched/user.h>
18
#include <linux/sched/debug.h>
19
#include <linux/sched/task.h>
20
#include <linux/sched/task_stack.h>
21
#include <linux/sched/cputime.h>
22
#include <linux/file.h>
Linus Torvalds's avatar
Linus Torvalds committed
23
#include <linux/fs.h>
24
#include <linux/proc_fs.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26
#include <linux/tty.h>
#include <linux/binfmts.h>
27
#include <linux/coredump.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30
#include <linux/security.h>
#include <linux/syscalls.h>
#include <linux/ptrace.h>
31
#include <linux/signal.h>
32
#include <linux/signalfd.h>
33
#include <linux/ratelimit.h>
34
#include <linux/tracehook.h>
35
#include <linux/capability.h>
36
#include <linux/freezer.h>
37 38
#include <linux/pid_namespace.h>
#include <linux/nsproxy.h>
39
#include <linux/user_namespace.h>
40
#include <linux/uprobes.h>
Al Viro's avatar
Al Viro committed
41
#include <linux/compat.h>
42
#include <linux/cn_proc.h>
43
#include <linux/compiler.h>
44
#include <linux/posix-timers.h>
45
#include <linux/livepatch.h>
Roman Gushchin's avatar
Roman Gushchin committed
46
#include <linux/cgroup.h>
47

48 49
#define CREATE_TRACE_POINTS
#include <trace/events/signal.h>
50

Linus Torvalds's avatar
Linus Torvalds committed
51
#include <asm/param.h>
52
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
53 54
#include <asm/unistd.h>
#include <asm/siginfo.h>
55
#include <asm/cacheflush.h>
56
#include "audit.h"	/* audit_signal_info() */
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60 61

/*
 * SLAB caches for signal bits.
 */

62
static struct kmem_cache *sigqueue_cachep;
Linus Torvalds's avatar
Linus Torvalds committed
63

64 65
int print_fatal_signals __read_mostly;

66
static void __user *sig_handler(struct task_struct *t, int sig)
67
{
68 69
	return t->sighand->action[sig - 1].sa.sa_handler;
}
70

71
static inline bool sig_handler_ignored(void __user *handler, int sig)
72
{
73 74
	/* Is it explicitly or implicitly ignored? */
	return handler == SIG_IGN ||
75
	       (handler == SIG_DFL && sig_kernel_ignore(sig));
76
}
Linus Torvalds's avatar
Linus Torvalds committed
77

78
static bool sig_task_ignored(struct task_struct *t, int sig, bool force)
Linus Torvalds's avatar
Linus Torvalds committed
79
{
80
	void __user *handler;
Linus Torvalds's avatar
Linus Torvalds committed
81

82 83
	handler = sig_handler(t, sig);

84 85 86 87
	/* SIGKILL and SIGSTOP may not be sent to the global init */
	if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
		return true;

88
	if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
89
	    handler == SIG_DFL && !(force && sig_kernel_only(sig)))
90
		return true;
91 92 93 94

	return sig_handler_ignored(handler, sig);
}

95
static bool sig_ignored(struct task_struct *t, int sig, bool force)
96
{
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100 101
	/*
	 * Blocked signals are never ignored, since the
	 * signal handler may change by the time it is
	 * unblocked.
	 */
102
	if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
103
		return false;
Linus Torvalds's avatar
Linus Torvalds committed
104

105
	/*
106 107 108
	 * Tracers may want to know about even ignored signal unless it
	 * is SIGKILL which can't be reported anyway but can be ignored
	 * by SIGNAL_UNKILLABLE task.
109
	 */
110
	if (t->ptrace && sig != SIGKILL)
111
		return false;
112 113

	return sig_task_ignored(t, sig, force);
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119
}

/*
 * Re-calculate pending state from the set of locally pending
 * signals, globally pending signals, and blocked signals.
 */
120
static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked)
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
	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))

148
static bool recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds's avatar
Linus Torvalds committed
149
{
Roman Gushchin's avatar
Roman Gushchin committed
150
	if ((t->jobctl & (JOBCTL_PENDING_MASK | JOBCTL_TRAP_FREEZE)) ||
Linus Torvalds's avatar
Linus Torvalds committed
151
	    PENDING(&t->pending, &t->blocked) ||
Roman Gushchin's avatar
Roman Gushchin committed
152 153
	    PENDING(&t->signal->shared_pending, &t->blocked) ||
	    cgroup_task_frozen(t)) {
Linus Torvalds's avatar
Linus Torvalds committed
154
		set_tsk_thread_flag(t, TIF_SIGPENDING);
155
		return true;
Roland McGrath's avatar
Roland McGrath committed
156
	}
157

158 159 160 161 162
	/*
	 * 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.
	 */
163
	return false;
Roland McGrath's avatar
Roland McGrath committed
164 165 166 167 168 169 170 171 172 173
}

/*
 * 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);
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177
}

void recalc_sigpending(void)
{
178 179
	if (!recalc_sigpending_tsk(current) && !freezing(current) &&
	    !klp_patch_pending(current))
180 181
		clear_thread_flag(TIF_SIGPENDING);

Linus Torvalds's avatar
Linus Torvalds committed
182
}
183
EXPORT_SYMBOL(recalc_sigpending);
Linus Torvalds's avatar
Linus Torvalds committed
184

185 186 187 188 189 190 191 192 193 194 195
void calculate_sigpending(void)
{
	/* Have any signals or users of TIF_SIGPENDING been delayed
	 * until after fork?
	 */
	spin_lock_irq(&current->sighand->siglock);
	set_tsk_thread_flag(current, TIF_SIGPENDING);
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);
}

Linus Torvalds's avatar
Linus Torvalds committed
196 197
/* Given the mask, find the first available signal that should be serviced. */

198 199
#define SYNCHRONOUS_MASK \
	(sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
200
	 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
201

202
int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205
{
	unsigned long i, *s, *m, x;
	int sig = 0;
206

Linus Torvalds's avatar
Linus Torvalds committed
207 208
	s = pending->signal.sig;
	m = mask->sig;
209 210 211 212 213 214 215 216 217 218 219 220 221

	/*
	 * 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;
	}

Linus Torvalds's avatar
Linus Torvalds committed
222 223
	switch (_NSIG_WORDS) {
	default:
224 225 226 227 228 229 230
		for (i = 1; i < _NSIG_WORDS; ++i) {
			x = *++s &~ *++m;
			if (!x)
				continue;
			sig = ffz(~x) + i*_NSIG_BPW + 1;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
231 232
		break;

233 234 235
	case 2:
		x = s[1] &~ m[1];
		if (!x)
Linus Torvalds's avatar
Linus Torvalds committed
236
			break;
237
		sig = ffz(~x) + _NSIG_BPW + 1;
Linus Torvalds's avatar
Linus Torvalds committed
238 239
		break;

240 241
	case 1:
		/* Nothing to do */
Linus Torvalds's avatar
Linus Torvalds committed
242 243
		break;
	}
244

Linus Torvalds's avatar
Linus Torvalds committed
245 246 247
	return sig;
}

248 249 250 251 252 253 254 255 256 257
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;

258
	pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
259 260 261
				current->comm, current->pid, sig);
}

262
/**
263
 * task_set_jobctl_pending - set jobctl pending bits
264
 * @task: target task
265
 * @mask: pending bits to set
266
 *
267 268 269 270 271 272 273 274 275 276 277 278
 * 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.
 */
279
bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
	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;
}

295
/**
296
 * task_clear_jobctl_trapping - clear jobctl trapping bit
297 298
 * @task: target task
 *
299 300 301 302
 * 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.
303 304 305 306
 *
 * CONTEXT:
 * Must be called with @task->sighand->siglock held.
 */
307
void task_clear_jobctl_trapping(struct task_struct *task)
308
{
309 310
	if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
		task->jobctl &= ~JOBCTL_TRAPPING;
311
		smp_mb();	/* advised by wake_up_bit() */
312
		wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
313 314 315
	}
}

316
/**
317
 * task_clear_jobctl_pending - clear jobctl pending bits
318
 * @task: target task
319
 * @mask: pending bits to clear
320
 *
321 322 323
 * 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.
324
 *
325 326
 * If clearing of @mask leaves no stop or trap pending, this function calls
 * task_clear_jobctl_trapping().
327 328 329 330
 *
 * CONTEXT:
 * Must be called with @task->sighand->siglock held.
 */
331
void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
332
{
333 334 335 336 337 338
	BUG_ON(mask & ~JOBCTL_PENDING_MASK);

	if (mask & JOBCTL_STOP_PENDING)
		mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;

	task->jobctl &= ~mask;
339 340 341

	if (!(task->jobctl & JOBCTL_PENDING_MASK))
		task_clear_jobctl_trapping(task);
342 343 344 345 346 347
}

/**
 * task_participate_group_stop - participate in a group stop
 * @task: task participating in a group stop
 *
348
 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
349
 * Group stop states are cleared and the group stop count is consumed if
350
 * %JOBCTL_STOP_CONSUME was set.  If the consumption completes the group
351
 * stop, the appropriate %SIGNAL_* flags are set.
352 353 354
 *
 * CONTEXT:
 * Must be called with @task->sighand->siglock held.
355 356 357 358
 *
 * RETURNS:
 * %true if group stop completion should be notified to the parent, %false
 * otherwise.
359 360 361 362
 */
static bool task_participate_group_stop(struct task_struct *task)
{
	struct signal_struct *sig = task->signal;
363
	bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
364

365
	WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
366

367
	task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
368 369 370 371 372 373 374

	if (!consume)
		return false;

	if (!WARN_ON_ONCE(sig->group_stop_count == 0))
		sig->group_stop_count--;

375 376 377 378 379
	/*
	 * 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)) {
380
		signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
381 382 383 384 385
		return true;
	}
	return false;
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399
void task_join_group_stop(struct task_struct *task)
{
	/* Have the new thread join an on-going signal group stop */
	unsigned long jobctl = current->jobctl;
	if (jobctl & JOBCTL_STOP_PENDING) {
		struct signal_struct *sig = current->signal;
		unsigned long signr = jobctl & JOBCTL_STOP_SIGMASK;
		unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
		if (task_set_jobctl_pending(task, signr | gstop)) {
			sig->group_stop_count++;
		}
	}
}

400 401 402
/*
 * allocate a new signal queue record
 * - this may be called without locks if and only if t == current, otherwise an
403
 *   appropriate lock must be held to stop the target task from exiting
404
 */
405 406
static struct sigqueue *
__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds's avatar
Linus Torvalds committed
407 408
{
	struct sigqueue *q = NULL;
409
	struct user_struct *user;
Linus Torvalds's avatar
Linus Torvalds committed
410

411
	/*
412 413
	 * Protect access to @t credentials. This can go away when all
	 * callers hold rcu read lock.
414
	 */
415
	rcu_read_lock();
416
	user = get_uid(__task_cred(t)->user);
417
	atomic_inc(&user->sigpending);
418
	rcu_read_unlock();
419

Linus Torvalds's avatar
Linus Torvalds committed
420
	if (override_rlimit ||
421
	    atomic_read(&user->sigpending) <=
422
			task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds's avatar
Linus Torvalds committed
423
		q = kmem_cache_alloc(sigqueue_cachep, flags);
424 425 426 427
	} else {
		print_dropped_signal(sig);
	}

Linus Torvalds's avatar
Linus Torvalds committed
428
	if (unlikely(q == NULL)) {
429
		atomic_dec(&user->sigpending);
430
		free_uid(user);
Linus Torvalds's avatar
Linus Torvalds committed
431 432 433
	} else {
		INIT_LIST_HEAD(&q->list);
		q->flags = 0;
434
		q->user = user;
Linus Torvalds's avatar
Linus Torvalds committed
435
	}
436 437

	return q;
Linus Torvalds's avatar
Linus Torvalds committed
438 439
}

440
static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds's avatar
Linus Torvalds committed
441 442 443 444 445 446 447 448
{
	if (q->flags & SIGQUEUE_PREALLOC)
		return;
	atomic_dec(&q->user->sigpending);
	free_uid(q->user);
	kmem_cache_free(sigqueue_cachep, q);
}

449
void flush_sigqueue(struct sigpending *queue)
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454 455 456 457 458 459 460 461
{
	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);
	}
}

/*
462
 * Flush all pending signals for this kthread.
Linus Torvalds's avatar
Linus Torvalds committed
463
 */
464
void flush_signals(struct task_struct *t)
Linus Torvalds's avatar
Linus Torvalds committed
465 466 467 468
{
	unsigned long flags;

	spin_lock_irqsave(&t->sighand->siglock, flags);
469 470 471
	clear_tsk_thread_flag(t, TIF_SIGPENDING);
	flush_sigqueue(&t->pending);
	flush_sigqueue(&t->signal->shared_pending);
Linus Torvalds's avatar
Linus Torvalds committed
472 473
	spin_unlock_irqrestore(&t->sighand->siglock, flags);
}
474
EXPORT_SYMBOL(flush_signals);
Linus Torvalds's avatar
Linus Torvalds committed
475

476
#ifdef CONFIG_POSIX_TIMERS
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static 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);
}
510
#endif
511

512 513 514 515 516 517 518 519 520 521
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);
}

Linus Torvalds's avatar
Linus Torvalds committed
522 523 524 525 526 527 528 529 530 531 532 533 534
/*
 * Flush all handlers for a task.
 */

void
flush_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;
535
#ifdef __ARCH_HAS_SA_RESTORER
536 537
		ka->sa.sa_restorer = NULL;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
538 539 540 541 542
		sigemptyset(&ka->sa.sa_mask);
		ka++;
	}
}

543
bool unhandled_signal(struct task_struct *tsk, int sig)
544
{
545
	void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
546
	if (is_global_init(tsk))
547 548
		return true;

549
	if (handler != SIG_IGN && handler != SIG_DFL)
550 551
		return false;

Tejun Heo's avatar
Tejun Heo committed
552 553
	/* if ptraced, let the tracer determine */
	return !tsk->ptrace;
554 555
}

556
static void collect_signal(int sig, struct sigpending *list, kernel_siginfo_t *info,
557
			   bool *resched_timer)
Linus Torvalds's avatar
Linus Torvalds committed
558 559 560 561 562 563 564 565 566
{
	struct sigqueue *q, *first = NULL;

	/*
	 * Collect the siginfo appropriate to this signal.  Check if
	 * there is another siginfo for the same signal.
	*/
	list_for_each_entry(q, &list->list, list) {
		if (q->info.si_signo == sig) {
567 568
			if (first)
				goto still_pending;
Linus Torvalds's avatar
Linus Torvalds committed
569 570 571
			first = q;
		}
	}
572 573 574

	sigdelset(&list->signal, sig);

Linus Torvalds's avatar
Linus Torvalds committed
575
	if (first) {
576
still_pending:
Linus Torvalds's avatar
Linus Torvalds committed
577 578
		list_del_init(&first->list);
		copy_siginfo(info, &first->info);
579 580 581 582 583 584

		*resched_timer =
			(first->flags & SIGQUEUE_PREALLOC) &&
			(info->si_code == SI_TIMER) &&
			(info->si_sys_private);

Linus Torvalds's avatar
Linus Torvalds committed
585 586
		__sigqueue_free(first);
	} else {
587 588 589 590
		/*
		 * Ok, it wasn't in the queue.  This must be
		 * a fast-pathed signal or we must have been
		 * out of queue space.  So zero out the info.
Linus Torvalds's avatar
Linus Torvalds committed
591
		 */
592
		clear_siginfo(info);
Linus Torvalds's avatar
Linus Torvalds committed
593 594
		info->si_signo = sig;
		info->si_errno = 0;
595
		info->si_code = SI_USER;
Linus Torvalds's avatar
Linus Torvalds committed
596 597 598 599 600 601
		info->si_pid = 0;
		info->si_uid = 0;
	}
}

static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
602
			kernel_siginfo_t *info, bool *resched_timer)
Linus Torvalds's avatar
Linus Torvalds committed
603
{
604
	int sig = next_signal(pending, mask);
Linus Torvalds's avatar
Linus Torvalds committed
605

606
	if (sig)
607
		collect_signal(sig, pending, info, resched_timer);
Linus Torvalds's avatar
Linus Torvalds committed
608 609 610 611
	return sig;
}

/*
612
 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds's avatar
Linus Torvalds committed
613 614 615 616
 * expected to free it.
 *
 * All callers have to hold the siglock.
 */
617
int dequeue_signal(struct task_struct *tsk, sigset_t *mask, kernel_siginfo_t *info)
Linus Torvalds's avatar
Linus Torvalds committed
618
{
619
	bool resched_timer = false;
620
	int signr;
621 622 623 624

	/* We only dequeue private signals from ourselves, we don't let
	 * signalfd steal them
	 */
625
	signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
626
	if (!signr) {
Linus Torvalds's avatar
Linus Torvalds committed
627
		signr = __dequeue_signal(&tsk->signal->shared_pending,
628
					 mask, info, &resched_timer);
629
#ifdef CONFIG_POSIX_TIMERS
630 631 632 633 634 635
		/*
		 * itimer signal ?
		 *
		 * itimers are process shared and we restart periodic
		 * itimers in the signal delivery path to prevent DoS
		 * attacks in the high resolution timer case. This is
636
		 * compliant with the old way of self-restarting
637 638 639 640 641 642 643 644 645 646
		 * itimers, as the SIGALRM is a legacy signal and only
		 * queued once. Changing the restart behaviour to
		 * restart the timer in the signal dequeue path is
		 * reducing the timer noise on heavy loaded !highres
		 * systems too.
		 */
		if (unlikely(signr == SIGALRM)) {
			struct hrtimer *tmr = &tsk->signal->real_timer;

			if (!hrtimer_is_queued(tmr) &&
647
			    tsk->signal->it_real_incr != 0) {
648 649 650 651 652
				hrtimer_forward(tmr, tmr->base->get_time(),
						tsk->signal->it_real_incr);
				hrtimer_restart(tmr);
			}
		}
653
#endif
654
	}
655

Davide Libenzi's avatar
Davide Libenzi committed
656
	recalc_sigpending();
657 658 659 660
	if (!signr)
		return 0;

	if (unlikely(sig_kernel_stop(signr))) {
661 662 663 664 665 666 667 668 669 670 671 672
		/*
		 * Set a marker that we have dequeued a stop signal.  Our
		 * caller might release the siglock and then the pending
		 * stop signal it is about to process is no longer in the
		 * pending bitmasks, but must still be cleared by a SIGCONT
		 * (and overruled by a SIGKILL).  So those cases clear this
		 * shared flag after we've set it.  Note that this flag may
		 * remain set after the signal we return is ignored or
		 * handled.  That doesn't matter because its only purpose
		 * is to alert stop-signal processing code when another
		 * processor has come along and cleared the flag.
		 */
673
		current->jobctl |= JOBCTL_STOP_DEQUEUED;
674
	}
675
#ifdef CONFIG_POSIX_TIMERS
676
	if (resched_timer) {
Linus Torvalds's avatar
Linus Torvalds committed
677 678 679 680 681 682 683
		/*
		 * Release the siglock to ensure proper locking order
		 * of timer locks outside of siglocks.  Note, we leave
		 * irqs disabled here, since the posix-timers code is
		 * about to disable them again anyway.
		 */
		spin_unlock(&tsk->sighand->siglock);
684
		posixtimer_rearm(info);
Linus Torvalds's avatar
Linus Torvalds committed
685
		spin_lock(&tsk->sighand->siglock);
686 687 688

		/* Don't expose the si_sys_private value to userspace */
		info->si_sys_private = 0;
Linus Torvalds's avatar
Linus Torvalds committed
689
	}
690
#endif
Linus Torvalds's avatar
Linus Torvalds committed
691 692
	return signr;
}
693
EXPORT_SYMBOL_GPL(dequeue_signal);
Linus Torvalds's avatar
Linus Torvalds committed
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
static int dequeue_synchronous_signal(kernel_siginfo_t *info)
{
	struct task_struct *tsk = current;
	struct sigpending *pending = &tsk->pending;
	struct sigqueue *q, *sync = NULL;

	/*
	 * Might a synchronous signal be in the queue?
	 */
	if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
		return 0;

	/*
	 * Return the first synchronous signal in the queue.
	 */
	list_for_each_entry(q, &pending->list, list) {
		/* Synchronous signals have a postive si_code */
		if ((q->info.si_code > SI_USER) &&
		    (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
			sync = q;
			goto next;
		}
	}
	return 0;
next:
	/*
	 * Check if there is another siginfo for the same signal.
	 */
	list_for_each_entry_continue(q, &pending->list, list) {
		if (q->info.si_signo == sync->info.si_signo)
			goto still_pending;
	}

	sigdelset(&pending->signal, sync->info.si_signo);
	recalc_sigpending();
still_pending:
	list_del_init(&sync->list);
	copy_siginfo(info, &sync->info);
	__sigqueue_free(sync);
	return info->si_signo;
}

Linus Torvalds's avatar
Linus Torvalds committed
737 738 739 740 741 742 743 744 745 746 747
/*
 * Tell a process that it has a new active signal..
 *
 * NOTE! we rely on the previous spin_lock to
 * lock interrupts for us! We can only be called with
 * "siglock" held, and the local interrupt must
 * have been disabled when that got acquired!
 *
 * No need to set need_resched since signal event passing
 * goes through ->blocked
 */
748
void signal_wake_up_state(struct task_struct *t, unsigned int state)
Linus Torvalds's avatar
Linus Torvalds committed
749 750 751
{
	set_tsk_thread_flag(t, TIF_SIGPENDING);
	/*
752
	 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
Matthew Wilcox's avatar
Matthew Wilcox committed
753
	 * case. We don't check t->state here because there is a race with it
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756 757
	 * executing another processor and just now entering stopped state.
	 * By using wake_up_state, we ensure the process will wake up and
	 * handle its death signal.
	 */
758
	if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
Linus Torvalds's avatar
Linus Torvalds committed
759 760 761
		kick_process(t);
}

762 763 764 765 766 767
/*
 * Remove signals in mask from the pending set and queue.
 * Returns 1 if any signals were found.
 *
 * All callers must be holding the siglock.
 */
768
static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
769 770 771 772 773 774
{
	struct sigqueue *q, *n;
	sigset_t m;

	sigandsets(&m, mask, &s->signal);
	if (sigisemptyset(&m))
775
		return;
776

777
	sigandnsets(&s->signal, &s->signal, mask);
778 779 780 781 782 783 784
	list_for_each_entry_safe(q, n, &s->list, list) {
		if (sigismember(mask, q->info.si_signo)) {
			list_del_init(&q->list);
			__sigqueue_free(q);
		}
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
785

786
static inline int is_si_special(const struct kernel_siginfo *info)
787
{
788
	return info <= SEND_SIG_PRIV;
789 790
}

791
static inline bool si_fromuser(const struct kernel_siginfo *info)
792 793 794 795 796
{
	return info == SEND_SIG_NOINFO ||
		(!is_si_special(info) && SI_FROMUSER(info));
}

797 798 799
/*
 * called with RCU read lock from check_kill_permission()
 */
800
static bool kill_ok_by_cred(struct task_struct *t)
801 802 803 804
{
	const struct cred *cred = current_cred();
	const struct cred *tcred = __task_cred(t);

805 806 807 808 809
	return uid_eq(cred->euid, tcred->suid) ||
	       uid_eq(cred->euid, tcred->uid) ||
	       uid_eq(cred->uid, tcred->suid) ||
	       uid_eq(cred->uid, tcred->uid) ||
	       ns_capable(tcred->user_ns, CAP_KILL);
810 811
}

Linus Torvalds's avatar
Linus Torvalds committed
812 813
/*
 * Bad permissions for sending the signal
814
 * - the caller must hold the RCU read lock
Linus Torvalds's avatar
Linus Torvalds committed
815
 */
816
static int check_kill_permission(int sig, struct kernel_siginfo *info,
Linus Torvalds's avatar
Linus Torvalds committed
817 818
				 struct task_struct *t)
{
819
	struct pid *sid;
820 821
	int error;

822
	if (!valid_signal(sig))
823 824
		return -EINVAL;

825
	if (!si_fromuser(info))
826
		return 0;
827

828 829
	error = audit_signal_info(sig, t); /* Let audit system see the signal */
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
830
		return error;
831

832
	if (!same_thread_group(current, t) &&
833
	    !kill_ok_by_cred(t)) {
834 835 836 837 838 839 840 841 842
		switch (sig) {
		case SIGCONT:
			sid = task_session(t);
			/*
			 * We don't return the error if sid == NULL. The
			 * task was unhashed, the caller must notice this.
			 */
			if (!sid || sid == task_session(current))
				break;
843
			/* fall through */
844 845 846 847
		default:
			return -EPERM;
		}
	}
848

849
	return security_task_kill(t, info, sig, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
850 851
}

852 853 854 855 856 857 858 859
/**
 * ptrace_trap_notify - schedule trap to notify ptracer
 * @t: tracee wanting to notify tracer
 *
 * This function schedules sticky ptrace trap which is cleared on the next
 * TRAP_STOP to notify ptracer of an event.  @t must have been seized by
 * ptracer.
 *
Tejun Heo's avatar
Tejun Heo committed
860 861 862 863 864
 * If @t is running, STOP trap will be taken.  If trapped for STOP and
 * ptracer is listening for events, tracee is woken up so that it can
 * re-trap for the new event.  If trapped otherwise, STOP trap will be
 * eventually taken without returning to userland after the existing traps
 * are finished by PTRACE_CONT.
865 866 867 868 869 870 871 872 873 874
 *
 * CONTEXT:
 * Must be called with @task->sighand->siglock held.
 */
static void ptrace_trap_notify(struct task_struct *t)
{
	WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
	assert_spin_locked(&t->sighand->siglock);

	task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
875
	ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
876 877
}

Linus Torvalds's avatar
Linus Torvalds committed
878
/*
879 880
 * Handle magic process-wide effects of stop/continue signals. Unlike
 * the signal actions, these happen immediately at signal-generation
Linus Torvalds's avatar
Linus Torvalds committed
881 882
 * time regardless of blocking, ignoring, or handling.  This does the
 * actual continuing for SIGCONT, but not the actual stopping for stop
883 884 885 886
 * signals. The process stop is done as a signal action for SIG_DFL.
 *
 * Returns true if the signal should be actually delivered, otherwise
 * it should be dropped.
Linus Torvalds's avatar
Linus Torvalds committed
887
 */
888
static bool prepare_signal(int sig, struct task_struct *p, bool force)
Linus Torvalds's avatar
Linus Torvalds committed
889
{
890
	struct signal_struct *signal = p->signal;
Linus Torvalds's avatar
Linus Torvalds committed
891
	struct task_struct *t;
892
	sigset_t flush;
Linus Torvalds's avatar
Linus Torvalds committed
893

894
	if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
895
		if (!(signal->flags & SIGNAL_GROUP_EXIT))
896
			return sig == SIGKILL;
Linus Torvalds's avatar
Linus Torvalds committed
897
		/*
898
		 * The process is in the middle of dying, nothing to do.
Linus Torvalds's avatar
Linus Torvalds committed
899
		 */
900
	} else if (sig_kernel_stop(sig)) {
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903
		/*
		 * This is a stop signal.  Remove SIGCONT from all queues.
		 */
904
		siginitset(&flush, sigmask(SIGCONT));
905
		flush_sigqueue_mask(&flush, &signal->shared_pending);
906
		for_each_thread(p, t)
907
			flush_sigqueue_mask(&flush, &t->pending);
Linus Torvalds's avatar
Linus Torvalds committed
908
	} else if (sig == SIGCONT) {
909
		unsigned int why;
Linus Torvalds's avatar
Linus Torvalds committed
910
		/*
911
		 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds's avatar
Linus Torvalds committed
912
		 */
913
		siginitset(&flush, SIG_KERNEL_STOP_MASK);
914
		flush_sigqueue_mask(&flush, &signal->shared_pending);
915
		for_each_thread(p, t) {
916
			flush_sigqueue_mask(&flush, &t->pending);
917
			task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
918 919 920 921
			if (likely(!(t->ptrace & PT_SEIZED)))
				wake_up_state(t, __TASK_STOPPED);
			else
				ptrace_trap_notify(t);
922
		}
Linus Torvalds's avatar
Linus Torvalds committed
923

924 925 926 927 928 929 930 931 932
		/*
		 * Notify the parent with CLD_CONTINUED if we were stopped.
		 *
		 * If we were in the middle of a group stop, we pretend it
		 * was already finished, and then continued. Since SIGCHLD
		 * doesn't queue we report only CLD_STOPPED, as if the next
		 * CLD_CONTINUED was dropped.
		 */
		why = 0;
933
		if (signal->flags & SIGNAL_STOP_STOPPED)
934
			why |= SIGNAL_CLD_CONTINUED;
935
		else if (signal->group_stop_count)
936 937 938
			why |= SIGNAL_CLD_STOPPED;

		if (why) {
939
			/*
940
			 * The first thread which returns from do_signal_stop()
941
			 * will take ->siglock, notice SIGNAL_CLD_MASK, and
942
			 * notify its parent. See get_signal().
943
			 */
944
			signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
945 946
			signal->group_stop_count = 0;
			signal->group_exit_code = 0;
Linus Torvalds's avatar
Linus Torvalds committed
947 948
		}
	}
949

950
	return !sig_ignored(p, sig, force);
Linus Torvalds's avatar
Linus Torvalds committed
951 952
}

953 954 955 956 957 958 959 960
/*
 * Test if P wants to take SIG.  After we've checked all threads with this,
 * it's equivalent to finding no threads not blocking SIG.  Any threads not
 * blocking SIG were ruled out because they are not running and already
 * have pending signals.  Such threads will dequeue from the shared queue
 * as soon as they're available, so putting the signal on the shared queue
 * will be equivalent to sending it to one such thread.
 */
961
static inline bool wants_signal(int sig, struct task_struct *p)
962 963
{
	if (sigismember(&p->blocked, sig))
964 965
		return false;

966
	if (p->flags & PF_EXITING)
967 968
		return false;

969
	if (sig == SIGKILL)
970 971
		return true;

972
	if (task_is_stopped_or_traced(p))
973 974
		return false;

975 976 977
	return task_curr(p) || !signal_pending(p);
}

978
static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
979 980 981 982 983 984 985 986 987 988 989 990
{
	struct signal_struct *signal = p->signal;
	struct task_struct *t;

	/*
	 * Now find a thread we can wake up to take the signal off the queue.
	 *
	 * If the main thread wants the signal, it gets first crack.
	 * Probably the least surprising to the average bear.
	 */
	if (wants_signal(sig, p))
		t = p;
991
	else if ((type == PIDTYPE_PID) || thread_group_empty(p))
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
		/*
		 * There is just one thread and it does not need to be woken.
		 * It will dequeue unblocked signals before it runs again.
		 */
		return;
	else {
		/*
		 * Otherwise try to find a suitable thread.
		 */
		t = signal->curr_target;
		while (!wants_signal(sig, t)) {
			t = next_thread(t);
			if (t == signal->curr_target)
				/*
				 * No thread needs to be woken.
				 * Any eligible threads will see
				 * the signal in the queue soon.
				 */
				return;
		}
		signal->curr_target = t;
	}

	/*
	 * Found a killable thread.  If the signal will be fatal,
	 * then start taking the whole group down immediately.
	 */
1019
	if (sig_fatal(p, sig) &&
1020
	    !(signal->flags & SIGNAL_GROUP_EXIT) &&
1021
	    !sigismember(&t->real_blocked, sig) &&
1022
	    (sig == SIGKILL || !p->ptrace)) {
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
		/*
		 * This signal will be fatal to the whole group.
		 */
		if (!sig_kernel_coredump(sig)) {
			/*
			 * Start a group exit and wake everybody up.
			 * This way we don't have other threads
			 * running and doing things after a slower
			 * thread has the fatal signal pending.
			 */
			signal->flags = SIGNAL_GROUP_EXIT;
			signal->group_exit_code = sig;
			signal->group_stop_count = 0;
			t = p;
			do {
1038
				task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
				sigaddset(&t->pending.signal, SIGKILL);
				signal_wake_up(t, 1);
			} while_each_thread(p, t);
			return;
		}
	}

	/*
	 * The signal is already in the shared-pending queue.
	 * Tell the chosen thread to wake up and dequeue it.
	 */
	signal_wake_up(t, sig == SIGKILL);
	return;
}

1054
static inline bool legacy_queue(struct sigpending *signals, int sig)
1055 1056 1057 1058
{
	return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
}

1059
#ifdef CONFIG_USER_NS
1060
static inline void userns_fixup_signal_uid(struct kernel_siginfo *info, struct task_struct *t)
1061 1062 1063 1064 1065 1066 1067
{
	if (current_user_ns() == task_cred_xxx(t, user_ns))
		return;

	if (SI_FROMKERNEL(info))
		return;

1068 1069 1070 1071
	rcu_read_lock();
	info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
					make_kuid(current_user_ns(), info->si_uid));
	rcu_read_unlock();
1072 1073
}
#else
1074
static inline void userns_fixup_signal_uid(struct kernel_siginfo *info, struct task_struct *t)
1075 1076 1077 1078 1079
{
	return;
}
#endif

1080
static int __send_signal(int sig, struct kernel_siginfo *info, struct task_struct *t,
1081
			enum pid_type type, int from_ancestor_ns)
Linus Torvalds's avatar
Linus Torvalds committed
1082
{
1083
	struct sigpending *pending;
1084
	struct sigqueue *q;
1085
	int override_rlimit;
1086
	int ret = 0, result;
1087

1088
	assert_spin_locked(&t->sighand->siglock);
1089

1090
	result = TRACE_SIGNAL_IGNORED;
1091
	if (!prepare_signal(sig, t,
1092
			from_ancestor_ns || (info == SEND_SIG_PRIV)))
1093
		goto ret;
1094

1095
	pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1096 1097 1098 1099 1100
	/*
	 * Short-circuit ignored signals and support queuing
	 * exactly one non-rt signal, so that we can get more
	 * detailed information about the cause of the signal.
	 */
1101
	result = TRACE_SIGNAL_ALREADY_PENDING;
1102
	if (legacy_queue(pending, sig))
1103 1104 1105
		goto ret;

	result = TRACE_SIGNAL_DELIVERED;
Linus Torvalds's avatar
Linus Torvalds committed
1106
	/*
1107
	 * Skip useless siginfo allocation for SIGKILL and kernel threads.
Linus Torvalds's avatar
Linus Torvalds committed
1108
	 */
1109
	if ((sig == SIGKILL) || (t->flags & PF_KTHREAD))
Linus Torvalds's avatar
Linus Torvalds committed
1110 1111
		goto out_set;

1112 1113 1114 1115 1116 1117 1118 1119 1120
	/*
	 * Real-time signals must be queued if sent by sigqueue, or
	 * some other real-time mechanism.  It is implementation
	 * defined whether kill() does so.  We attempt to do so, on
	 * the principle of least surprise, but since kill is not
	 * allowed to fail with EAGAIN when low on memory we just
	 * make sure at least one signal gets delivered and don't
	 * pass on the info struct.
	 */
1121 1122 1123 1124 1125
	if (sig < SIGRTMIN)
		override_rlimit = (is_si_special(info) || info->si_code >= 0);
	else
		override_rlimit = 0;

1126
	q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit);
Linus Torvalds's avatar
Linus Torvalds committed
1127
	if (q) {
1128
		list_add_tail(&q->list, &pending->list);
Linus Torvalds's avatar
Linus Torvalds committed
1129
		switch ((unsigned long) info) {
1130
		case (unsigned long) SEND_SIG_NOINFO:
1131
			clear_siginfo(&q->info);
Linus Torvalds's avatar
Linus Torvalds committed
1132 1133 1134
			q->info.si_signo = sig;
			q->info.si_errno = 0;
			q->info.si_code = SI_USER;
1135
			q->info.si_pid = task_tgid_nr_ns(current,
1136
							task_active_pid_ns(t));
1137
			q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds's avatar
Linus Torvalds committed
1138
			break;
1139
		case (unsigned long) SEND_SIG_PRIV:
1140
			clear_siginfo(&q->info);
Linus Torvalds's avatar
Linus Torvalds committed
1141 1142 1143 1144 1145 1146 1147 1148
			q->info.si_signo = sig;
			q->info.si_errno = 0;
			q->info.si_code = SI_KERNEL;
			q->info.si_pid = 0;
			q->info.si_uid = 0;
			break;
		default:
			copy_siginfo(&q->info, info);
1149 1150
			if (from_ancestor_ns)
				q->info.si_pid = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1151 1152
			break;
		}
1153 1154 1155

		userns_fixup_signal_uid(&q->info, t);

1156
	} else if (!is_si_special(info)) {
1157 1158 1159 1160 1161 1162
		if (sig >= SIGRTMIN && info->si_code != SI_USER) {
			/*
			 * Queue overflow, abort.  We may abort if the
			 * signal was rt and sent by user using something
			 * other than kill().
			 */
1163 1164 1165
			result = TRACE_SIGNAL_OVERFLOW_FAIL;
			ret = -EAGAIN;
			goto ret;
1166 1167 1168 1169 1170
		} else {
			/*
			 * This is a silent loss of information.  We still
			 * send the signal, but the *info bits are lost.
			 */
1171
			result = TRACE_SIGNAL_LOSE_INFO;
1172
		}
Linus Torvalds's avatar
Linus Torvalds committed
1173 1174 1175
	}

out_set:
1176
	signalfd_notify(t, sig);
1177
	sigaddset(&pending->signal, sig);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192

	/* Let multiprocess signals appear after on-going forks */
	if (type > PIDTYPE_TGID) {
		struct multiprocess_signals *delayed;
		hlist_for_each_entry(delayed, &t->signal->multiprocess, node) {
			sigset_t *signal = &delayed->signal;
			/* Can't queue both a stop and a continue signal */
			if (sig == SIGCONT)
				sigdelsetmask(signal, SIG_KERNEL_STOP_MASK);
			else if (sig_kernel_stop(sig))
				sigdelset(signal, SIGCONT);
			sigaddset(signal, sig);
		}
	}

1193
	complete_signal(sig, t, type);
1194
ret:
1195
	trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result);
1196
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1197 1198
}

1199
static int send_signal(int sig, struct kernel_siginfo *info, struct task_struct *t,
1200
			enum pid_type type)
1201
{
1202 1203 1204
	int from_ancestor_ns = 0;

#ifdef CONFIG_PID_NS
1205 1206
	from_ancestor_ns = si_fromuser(info) &&
			   !task_pid_nr_ns(current, task_active_pid_ns(t));
1207 1208
#endif

1209
	return __send_signal(sig, info, t, type, from_ancestor_ns);
1210 1211
}

1212
static void print_fatal_signal(int signr)
Ingo Molnar's avatar
Ingo Molnar committed
1213
{
1214
	struct pt_regs *regs = signal_pt_regs();
1215
	pr_info("potentially unexpected fatal signal %d.\n", signr);
Ingo Molnar's avatar
Ingo Molnar committed
1216

Al Viro's avatar
Al Viro committed
1217
#if defined(__i386__) && !defined(__arch_um__)
1218
	pr_info("code at %08lx: ", regs->ip);
Ingo Molnar's avatar
Ingo Molnar committed
1219 1220 1221 1222 1223
	{
		int i;
		for (i = 0; i < 16; i++) {
			unsigned char insn;

1224 1225
			if (get_user(insn, (unsigned char *)(regs->ip + i)))
				break;
1226
			pr_cont("%02x ", insn);
Ingo Molnar's avatar
Ingo Molnar committed
1227 1228
		}
	}
1229
	pr_cont("\n");
Ingo Molnar's avatar
Ingo Molnar committed
1230
#endif
1231
	preempt_disable();
Ingo Molnar's avatar
Ingo Molnar committed
1232
	show_regs(regs);
1233
	preempt_enable();
Ingo Molnar's avatar
Ingo Molnar committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
}

static int __init setup_print_fatal_signals(char *str)
{
	get_option (&str, &print_fatal_signals);

	return 1;
}

__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds's avatar
Linus Torvalds committed
1244

1245
int
1246
__group_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
1247
{
1248
	return send_signal(sig, info, p, PIDTYPE_TGID);
1249 1250
}

1251
int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p,
1252
			enum pid_type type)
1253 1254 1255 1256 1257
{
	unsigned long flags;
	int ret = -ESRCH;

	if (lock_task_sighand(p, &flags)) {
1258
		ret = send_signal(sig, info, p, type);
1259 1260 1261 1262 1263 1264
		unlock_task_sighand(p, &flags);
	}

	return ret;
}

Linus Torvalds's avatar
Linus Torvalds committed
1265 1266 1267
/*
 * Force a signal that the process can't ignore: if necessary
 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1268 1269 1270 1271 1272
 *
 * Note: If we unblock the signal, we always reset it to SIG_DFL,
 * since we do not want to have a signal handler that was blocked
 * be invoked when user space had explicitly blocked it.
 *
1273 1274
 * We don't want to have recursive SIGSEGV's etc, for example,
 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds's avatar
Linus Torvalds committed
1275 1276
 */
int
1277
force_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *t)
Linus Torvalds's avatar
Linus Torvalds committed
1278 1279
{
	unsigned long int flags;
1280 1281
	int ret, blocked, ignored;
	struct k_sigaction *action;
Linus Torvalds's avatar
Linus Torvalds committed
1282 1283

	spin_lock_irqsave(&t->sighand->siglock, flags);
1284 1285 1286 1287 1288 1289 1290
	action = &t->sighand->action[sig-1];
	ignored = action->sa.sa_handler == SIG_IGN;
	blocked = sigismember(&t->blocked, sig);
	if (blocked || ignored) {
		action->sa.sa_handler = SIG_DFL;
		if (blocked) {
			sigdelset(&t->blocked, sig);
Roland McGrath's avatar
Roland McGrath committed
1291
			recalc_sigpending_and_wake(t);
1292
		}
Linus Torvalds's avatar
Linus Torvalds committed
1293
	}
1294 1295 1296 1297 1298
	/*
	 * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
	 * debugging to leave init killable.
	 */
	if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
1299
		t->signal->flags &= ~SIGNAL_UNKILLABLE;
1300
	ret = send_signal(sig, info, t, PIDTYPE_PID);
Linus Torvalds's avatar
Linus Torvalds committed
1301 1302 1303 1304 1305 1306 1307 1308
	spin_unlock_irqrestore(&t->sighand->siglock, flags);

	return ret;
}

/*
 * Nuke all other threads in the group.
 */
1309
int zap_other_threads(struct task_struct *p)
Linus Torvalds's avatar
Linus Torvalds committed
1310
{
1311 1312
	struct task_struct *t = p;
	int count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1313 1314 1315

	p->signal->group_stop_count = 0;

1316
	while_each_thread(p, t) {
1317
		task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1318 1319 1320
		count++;

		/* Don't bother with already dead threads */
Linus Torvalds's avatar
Linus Torvalds committed
1321 1322 1323 1324 1325
		if (t->exit_state)
			continue;
		sigaddset(&t->pending.signal, SIGKILL);
		signal_wake_up(t, 1);
	}
1326 1327

	return count;
Linus Torvalds's avatar
Linus Torvalds committed
1328 1329
}

1330 1331
struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
					   unsigned long *flags)
1332 1333 1334
{
	struct sighand_struct *sighand;

1335
	rcu_read_lock();
1336 1337
	for (;;) {
		sighand = rcu_dereference(tsk->sighand);
1338
		if (unlikely(sighand == NULL))
1339
			break;
1340

1341 1342
		/*
		 * This sighand can be already freed and even reused, but
1343
		 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
1344 1345 1346 1347 1348 1349 1350 1351
		 * initializes ->siglock: this slab can't go away, it has
		 * the same object type, ->siglock can't be reinitialized.
		 *
		 * We need to ensure that tsk->sighand is still the same
		 * after we take the lock, we can race with de_thread() or
		 * __exit_signal(). In the latter case the next iteration
		 * must see ->sighand == NULL.
		 */
1352 1353
		spin_lock_irqsave(&sighand->siglock, *flags);
		if (likely(sighand == tsk->sighand))
1354
			break;
1355
		spin_unlock_irqrestore(&sighand->siglock, *flags);
1356
	}
1357
	rcu_read_unlock();
1358 1359 1360 1361

	return sighand;
}

1362 1363 1364
/*
 * send signal info to all the members of a group
 */
1365 1366
int group_send_sig_info(int sig, struct kernel_siginfo *info,
			struct task_struct *p, enum pid_type type)
Linus Torvalds's avatar
Linus Torvalds committed
1367
{
1368 1369 1370 1371 1372
	int ret;

	rcu_read_lock();
	ret = check_kill_permission(sig, info, p);
	rcu_read_unlock();
1373

1374
	if (!ret && sig)
1375
		ret = do_send_sig_info(sig, info, p, type);
Linus Torvalds's avatar
Linus Torvalds committed
1376 1377 1378 1379 1380

	return ret;
}

/*
1381
 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds's avatar
Linus Torvalds committed
1382
 * control characters do (^C, ^Z etc)
1383
 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds's avatar
Linus Torvalds committed
1384
 */
1385
int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp)
Linus Torvalds's avatar
Linus Torvalds committed
1386 1387 1388 1389 1390 1391
{
	struct task_struct *p = NULL;
	int retval, success;

	success = 0;
	retval = -ESRCH;
1392
	do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1393
		int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
Linus Torvalds's avatar
Linus Torvalds committed
1394 1395
		success |= !err;
		retval = err;
1396
	} while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds's avatar
Linus Torvalds committed
1397 1398 1399
	return success ? 0 : retval;
}

1400
int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid)
Linus Torvalds's avatar
Linus Torvalds committed
1401
{
1402
	int error = -ESRCH;
Linus Torvalds's avatar
Linus Torvalds committed
1403 1404
	struct task_struct *p;

1405 1406 1407 1408
	for (;;) {
		rcu_read_lock();
		p = pid_task(pid, PIDTYPE_PID);
		if (p)
1409
			error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
1410 1411 1412
		rcu_read_unlock();
		if (likely(!p || error != -ESRCH))
			return error;
1413

1414 1415 1416 1417 1418 1419
		/*
		 * The task was unhashed in between, try again.  If it
		 * is dead, pid_task() will return NULL, if we race with
		 * de_thread() it will find the new leader.
		 */
	}
Linus Torvalds's avatar
Linus Torvalds committed
1420 1421
}

1422
static int kill_proc_info(int sig, struct kernel_siginfo *info, pid_t pid)
1423 1424 1425
{
	int error;
	rcu_read_lock();
1426
	error = kill_pid_info(sig, info, find_vpid(pid));
1427 1428 1429 1430
	rcu_read_unlock();
	return error;
}

1431 1432
static inline bool kill_as_cred_perm(const struct cred *cred,
				     struct task_struct *target)
1433 1434
{
	const struct cred *pcred = __task_cred(target);
1435 1436 1437 1438 1439

	return uid_eq(cred->euid, pcred->suid) ||
	       uid_eq(cred->euid, pcred->uid) ||
	       uid_eq(cred->uid, pcred->suid) ||
	       uid_eq(cred->uid, pcred->uid);
1440 1441
}

1442
/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1443
int kill_pid_info_as_cred(int sig, struct kernel_siginfo *info, struct pid *pid,
1444
			 const struct cred *cred)
1445 1446 1447
{
	int ret = -EINVAL;
	struct task_struct *p;
1448
	unsigned long flags;
1449 1450 1451 1452

	if (!valid_signal(sig))
		return ret;

1453
	rcu_read_lock();
1454
	p = pid_task(pid, PIDTYPE_PID);
1455 1456 1457 1458
	if (!p) {
		ret = -ESRCH;
		goto out_unlock;
	}
1459
	if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
1460 1461 1462
		ret = -EPERM;
		goto out_unlock;
	}
1463
	ret = security_task_kill(p, info, sig, cred);
1464 1465
	if (ret)
		goto out_unlock;
1466 1467 1468

	if (sig) {
		if (lock_task_sighand(p, &flags)) {
1469
			ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0);
1470 1471 1472
			unlock_task_sighand(p, &flags);
		} else
			ret = -ESRCH;
1473 1474
	}
out_unlock:
1475
	rcu_read_unlock();
1476 1477
	return ret;
}
1478
EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
Linus Torvalds's avatar
Linus Torvalds committed
1479 1480 1481 1482 1483 1484 1485 1486

/*
 * kill_something_info() interprets pid in interesting ways just like kill(2).
 *
 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
 * is probably wrong.  Should make it like BSD or SYSV.
 */

1487
static int kill_something_info(int sig, struct kernel_siginfo *info, pid_t pid)
Linus Torvalds's avatar
Linus Torvalds committed
1488
{
1489
	int ret;
1490 1491 1492 1493 1494 1495 1496 1497

	if (pid > 0) {
		rcu_read_lock();
		ret = kill_pid_info(sig, info, find_vpid(pid));
		rcu_read_unlock();
		return ret;
	}

1498 1499 1500 1501
	/* -INT_MIN is undefined.  Exclude this case to avoid a UBSAN warning */
	if (pid == INT_MIN)
		return -ESRCH;

1502 1503 1504 1505 1506
	read_lock(&tasklist_lock);
	if (pid != -1) {
		ret = __kill_pgrp_info(sig, info,
				pid ? find_vpid(-pid) : task_pgrp(current));
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
1507 1508 1509 1510
		int retval = 0, count = 0;
		struct task_struct * p;

		for_each_process(p) {
1511 1512
			if (task_pid_vnr(p) > 1 &&
					!same_thread_group(p, current)) {
1513 1514
				int err = group_send_sig_info(sig, info, p,
							      PIDTYPE_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
1515 1516 1517 1518 1519
				++count;
				if (err != -EPERM)
					retval = err;
			}
		}
1520
		ret = count ? retval : -ESRCH;
Linus Torvalds's avatar
Linus Torvalds committed
1521
	}
1522 1523
	read_unlock(&tasklist_lock);

1524
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1525 1526 1527 1528 1529 1530
}

/*
 * These are for backward compatibility with the rest of the kernel source.
 */

1531
int send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
Linus Torvalds's avatar
Linus Torvalds committed
1532 1533 1534 1535 1536
{
	/*
	 * Make sure legacy kernel users don't send in bad values
	 * (normal paths check this in check_kill_permission).
	 */
1537
	if (!valid_signal(sig))
Linus Torvalds's avatar
Linus Torvalds committed
1538 1539
		return -EINVAL;

1540
	return do_send_sig_info(sig, info, p, PIDTYPE_PID);
Linus Torvalds's avatar
Linus Torvalds committed
1541
}
1542
EXPORT_SYMBOL(send_sig_info);
Linus Torvalds's avatar
Linus Torvalds committed
1543

1544 1545 1546
#define __si_special(priv) \
	((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)

Linus Torvalds's avatar
Linus Torvalds committed
1547 1548 1549
int
send_sig(int sig, struct task_struct *p, int priv)
{
1550
	return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds's avatar
Linus Torvalds committed
1551
}
1552
EXPORT_SYMBOL(send_sig);
Linus Torvalds's avatar
Linus Torvalds committed
1553

1554
void force_sig(int sig, struct task_struct *p)
Linus Torvalds's avatar
Linus Torvalds committed
1555
{
1556
	force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds's avatar
Linus Torvalds committed
1557
}
1558
EXPORT_SYMBOL(force_sig);
Linus Torvalds's avatar
Linus Torvalds committed
1559 1560 1561 1562 1563 1564 1565

/*
 * When things go south during signal handling, we
 * will force a SIGSEGV. And if the signal that caused
 * the problem was already a SIGSEGV, we'll want to
 * make sure we don't even try to deliver the signal..
 */
1566
void force_sigsegv(int sig, struct task_struct *p)
Linus Torvalds's avatar
Linus Torvalds committed
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
{
	if (sig == SIGSEGV) {
		unsigned long flags;
		spin_lock_irqsave(&p->sighand->siglock, flags);
		p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
		spin_unlock_irqrestore(&p->sighand->siglock, flags);
	}
	force_sig(SIGSEGV, p);
}

1577 1578 1579 1580 1581
int force_sig_fault(int sig, int code, void __user *addr
	___ARCH_SI_TRAPNO(int trapno)
	___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
	, struct task_struct *t)
{
1582
	struct kernel_siginfo info;
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604

	clear_siginfo(&info);
	info.si_signo = sig;
	info.si_errno = 0;
	info.si_code  = code;
	info.si_addr  = addr;
#ifdef __ARCH_SI_TRAPNO
	info.si_trapno = trapno;
#endif
#ifdef __ia64__
	info.si_imm = imm;
	info.si_flags = flags;
	info.si_isr = isr;
#endif
	return force_sig_info(info.si_signo, &info, t);
}

int send_sig_fault(int sig, int code, void __user *addr
	___ARCH_SI_TRAPNO(int trapno)
	___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
	, struct task_struct *t)
{
1605
	struct kernel_siginfo info;
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622

	clear_siginfo(&info);
	info.si_signo = sig;
	info.si_errno = 0;
	info.si_code  = code;
	info.si_addr  = addr;
#ifdef __ARCH_SI_TRAPNO
	info.si_trapno = trapno;
#endif
#ifdef __ia64__
	info.si_imm = imm;
	info.si_flags = flags;
	info.si_isr = isr;
#endif
	return send_sig_info(info.si_signo, &info, t);
}

1623 1624
int force_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
{
1625
	struct kernel_siginfo info;
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638

	WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
	clear_siginfo(&info);
	info.si_signo = SIGBUS;
	info.si_errno = 0;
	info.si_code = code;
	info.si_addr = addr;
	info.si_addr_lsb = lsb;
	return force_sig_info(info.si_signo, &info, t);
}

int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
{
1639
	struct kernel_siginfo info;
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

	WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
	clear_siginfo(&info);
	info.si_signo = SIGBUS;
	info.si_errno = 0;
	info.si_code = code;
	info.si_addr = addr;
	info.si_addr_lsb = lsb;
	return send_sig_info(info.si_signo, &info, t);
}
EXPORT_SYMBOL(send_sig_mceerr);

int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
{
1654
	struct kernel_siginfo info;
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668

	clear_siginfo(&info);
	info.si_signo = SIGSEGV;
	info.si_errno = 0;
	info.si_code  = SEGV_BNDERR;
	info.si_addr  = addr;
	info.si_lower = lower;
	info.si_upper = upper;
	return force_sig_info(info.si_signo, &info, current);
}

#ifdef SEGV_PKUERR
int force_sig_pkuerr(void __user *addr, u32 pkey)
{
1669
	struct kernel_siginfo info;
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679

	clear_siginfo(&info);
	info.si_signo = SIGSEGV;
	info.si_errno = 0;
	info.si_code  = SEGV_PKUERR;
	info.si_addr  = addr;
	info.si_pkey  = pkey;
	return force_sig_info(info.si_signo, &info, current);
}
#endif
1680

1681 1682 1683 1684 1685
/* For the crazy architectures that include trap information in
 * the errno field, instead of an actual errno value.
 */
int force_sig_ptrace_errno_trap(int errno, void __user *addr)
{
1686
	struct kernel_siginfo info;
1687 1688 1689 1690 1691 1692 1693 1694 1695

	clear_siginfo(&info);
	info.si_signo = SIGTRAP;
	info.si_errno = errno;
	info.si_code  = TRAP_HWBKPT;
	info.si_addr  = addr;
	return force_sig_info(info.si_signo, &info, current);
}

1696 1697
int kill_pgrp(struct pid *pid, int sig, int priv)
{
1698 1699 1700 1701 1702 1703 1704
	int ret;

	read_lock(&tasklist_lock);
	ret = __kill_pgrp_info(sig, __si_special(priv), pid);
	read_unlock(&tasklist_lock);

	return ret;
1705 1706 1707 1708 1709 1710 1711 1712 1713
}
EXPORT_SYMBOL(kill_pgrp);

int kill_pid(struct pid *pid, int sig, int priv)
{
	return kill_pid_info(sig, __si_special(priv), pid);
}
EXPORT_SYMBOL(kill_pid);

Linus Torvalds's avatar
Linus Torvalds committed
1714 1715 1716 1717
/*
 * These functions support sending signals using preallocated sigqueue
 * structures.  This is needed "because realtime applications cannot
 * afford to lose notifications of asynchronous events, like timer
1718
 * expirations or I/O completions".  In the case of POSIX Timers
Linus Torvalds's avatar
Linus Torvalds committed
1719 1720 1721 1722 1723 1724
 * we allocate the sigqueue structure from the timer_create.  If this
 * allocation fails we are able to report the failure to the application
 * with an EAGAIN error.
 */
struct sigqueue *sigqueue_alloc(void)
{
1725
	struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1726

1727
	if (q)
Linus Torvalds's avatar
Linus Torvalds committed
1728
		q->flags |= SIGQUEUE_PREALLOC;
1729 1730

	return q;
Linus Torvalds's avatar
Linus Torvalds committed
1731 1732 1733 1734 1735
}

void sigqueue_free(struct sigqueue *q)
{
	unsigned long flags;
1736 1737
	spinlock_t *lock = &current->sighand->siglock;

Linus Torvalds's avatar
Linus Torvalds committed
1738 1739
	BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
	/*
1740 1741
	 * We must hold ->siglock while testing q->list
	 * to serialize with collect_signal() or with
1742
	 * __exit_signal()->flush_sigqueue().
Linus Torvalds's avatar
Linus Torvalds committed
1743
	 */
1744
	spin_lock_irqsave(lock, flags);
1745 1746 1747 1748 1749
	q->flags &= ~SIGQUEUE_PREALLOC;
	/*
	 * If it is queued it will be freed when dequeued,
	 * like the "regular" sigqueue.
	 */
1750
	if (!list_empty(&q->list))
1751
		q = NULL;
1752 1753
	spin_unlock_irqrestore(lock, flags);

1754 1755
	if (q)
		__sigqueue_free(q);
Linus Torvalds's avatar
Linus Torvalds committed
1756 1757
}

1758
int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
1759
{
1760
	int sig = q->info.si_signo;
1761
	struct sigpending *pending;
1762
	struct task_struct *t;
1763
	unsigned long flags;
1764
	int ret, result;
1765

1766
	BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1767 1768

	ret = -1;
1769 1770 1771
	rcu_read_lock();
	t = pid_task(pid, type);
	if (!t || !likely(lock_task_sighand(t, &flags)))
1772 1773
		goto ret;

1774
	ret = 1; /* the signal is ignored */
1775
	result = TRACE_SIGNAL_IGNORED;
1776
	if (!prepare_signal(sig, t, false))
1777 1778 1779
		goto out;

	ret = 0;
1780 1781 1782 1783 1784 1785 1786
	if (unlikely(!list_empty(&q->list))) {
		/*
		 * If an SI_TIMER entry is already queue just increment
		 * the overrun count.
		 */
		BUG_ON(q->info.si_code != SI_TIMER);
		q->info.si_overrun++;
1787
		result = TRACE_SIGNAL_ALREADY_PENDING;
1788
		goto out;
1789
	}
1790
	q->info.si_overrun = 0;
1791 1792

	signalfd_notify(t, sig);
1793
	pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1794 1795
	list_add_tail(&q->list, &pending->list);
	sigaddset(&pending->signal, sig);
1796
	complete_signal(sig, t, type);
1797
	result = TRACE_SIGNAL_DELIVERED;
1798
out:
1799
	trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
1800 1801
	unlock_task_sighand(t, &flags);
ret:
1802
	rcu_read_unlock();
1803
	return ret;
1804 1805
}

Linus Torvalds's avatar
Linus Torvalds committed
1806 1807 1808
/*
 * Let a parent know about the death of a child.
 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath's avatar
Roland McGrath committed
1809
 *
1810 1811
 * Returns true if our parent ignored us and so we've switched to
 * self-reaping.
Linus Torvalds's avatar
Linus Torvalds committed
1812
 */
1813
bool do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds's avatar
Linus Torvalds committed
1814
{
1815
	struct kernel_siginfo info;
Linus Torvalds's avatar
Linus Torvalds committed
1816 1817
	unsigned long flags;
	struct sighand_struct *psig;
1818
	bool autoreap = false;
1819
	u64 utime, stime;
Linus Torvalds's avatar
Linus Torvalds committed
1820 1821 1822 1823

	BUG_ON(sig == -1);

 	/* do_notify_parent_cldstop should have been called instead.  */
Matthew Wilcox's avatar
Matthew Wilcox committed
1824
 	BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds's avatar
Linus Torvalds committed
1825

Tejun Heo's avatar
Tejun Heo committed
1826
	BUG_ON(!tsk->ptrace &&
Linus Torvalds's avatar
Linus Torvalds committed
1827 1828
	       (tsk->group_leader != tsk || !thread_group_empty(tsk)));

1829 1830 1831 1832 1833 1834 1835 1836 1837
	if (sig != SIGCHLD) {
		/*
		 * This is only possible if parent == real_parent.
		 * Check if it has changed security domain.
		 */
		if (tsk->parent_exec_id != tsk->parent->self_exec_id)
			sig = SIGCHLD;
	}

1838
	clear_siginfo(&info);
Linus Torvalds's avatar
Linus Torvalds committed
1839 1840
	info.si_signo = sig;
	info.si_errno = 0;
1841
	/*
1842 1843
	 * We are under tasklist_lock here so our parent is tied to
	 * us and cannot change.
1844
	 *
1845 1846
	 * task_active_pid_ns will always return the same pid namespace
	 * until a task passes through release_task.
1847 1848 1849 1850 1851 1852
	 *
	 * write_lock() currently calls preempt_disable() which is the
	 * same as rcu_read_lock(), but according to Oleg, this is not
	 * correct to rely on this
	 */
	rcu_read_lock();
1853
	info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
1854 1855
	info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
				       task_uid(tsk));
1856 1857
	rcu_read_unlock();

1858 1859 1860
	task_cputime(tsk, &utime, &stime);
	info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
	info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
Linus Torvalds's avatar
Linus Torvalds committed
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873

	info.si_status = tsk->exit_code & 0x7f;
	if (tsk->exit_code & 0x80)
		info.si_code = CLD_DUMPED;
	else if (tsk->exit_code & 0x7f)
		info.si_code = CLD_KILLED;
	else {
		info.si_code = CLD_EXITED;
		info.si_status = tsk->exit_code >> 8;
	}

	psig = tsk->parent->sighand;
	spin_lock_irqsave(&psig->siglock, flags);
Tejun Heo's avatar
Tejun Heo committed
1874
	if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds's avatar
Linus Torvalds committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	    (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
	     (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
		/*
		 * We are exiting and our parent doesn't care.  POSIX.1
		 * defines special semantics for setting SIGCHLD to SIG_IGN
		 * or setting the SA_NOCLDWAIT flag: we should be reaped
		 * automatically and not left for our parent's wait4 call.
		 * Rather than having the parent do it as a magic kind of
		 * signal handler, we just set this to tell do_exit that we
		 * can be cleaned up without becoming a zombie.  Note that
		 * we still call __wake_up_parent in this case, because a
		 * blocked sys_wait4 might now return -ECHILD.
		 *
		 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
		 * is implementation-defined: we do (if you don't want
		 * it, just use SIG_IGN instead).
		 */
1892
		autoreap = true;
Linus Torvalds's avatar
Linus Torvalds committed
1893
		if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1894
			sig = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1895
	}
1896
	if (valid_signal(sig) && sig)
Linus Torvalds's avatar
Linus Torvalds committed
1897 1898 1899
		__group_send_sig_info(sig, &info, tsk->parent);
	__wake_up_parent(tsk, tsk->parent);
	spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath's avatar
Roland McGrath committed
1900

1901
	return autoreap;
Linus Torvalds's avatar
Linus Torvalds committed
1902 1903
}

1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
/**
 * do_notify_parent_cldstop - notify parent of stopped/continued state change
 * @tsk: task reporting the state change
 * @for_ptracer: the notification is for ptracer
 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
 *
 * Notify @tsk's parent that the stopped/continued state has changed.  If
 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
 *
 * CONTEXT:
 * Must be called with tasklist_lock at least read locked.
 */
static void do_notify_parent_cldstop(struct task_struct *tsk,
				     bool for_ptracer, int why)
Linus Torvalds's avatar
Linus Torvalds committed
1919
{
1920
	struct kernel_siginfo info;
Linus Torvalds's avatar
Linus Torvalds committed
1921
	unsigned long flags;
1922
	struct task_struct *parent;
Linus Torvalds's avatar
Linus Torvalds committed
1923
	struct sighand_struct *sighand;
1924
	u64 utime, stime;
Linus Torvalds's avatar
Linus Torvalds committed
1925

1926
	if (for_ptracer) {
1927
		parent = tsk->parent;
1928
	} else {
1929 1930 1931 1932
		tsk = tsk->group_leader;
		parent = tsk->real_parent;
	}

1933
	clear_siginfo(&info);
Linus Torvalds's avatar
Linus Torvalds committed
1934 1935
	info.si_signo = SIGCHLD;
	info.si_errno = 0;
1936
	/*
1937
	 * see comment in do_notify_parent() about the following 4 lines
1938 1939
	 */
	rcu_read_lock();
1940
	info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
1941
	info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
1942 1943
	rcu_read_unlock();

1944 1945 1946
	task_cputime(tsk, &utime, &stime);
	info.si_utime = nsec_to_clock_t(utime);
	info.si_stime = nsec_to_clock_t(stime);
Linus Torvalds's avatar
Linus Torvalds committed
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

 	info.si_code = why;
 	switch (why) {
 	case CLD_CONTINUED:
 		info.si_status = SIGCONT;
 		break;
 	case CLD_STOPPED:
 		info.si_status = tsk->signal->group_exit_code & 0x7f;
 		break;
 	case CLD_TRAPPED:
 		info.si_status = tsk->exit_code & 0x7f;
 		break;
 	default:
 		BUG();
 	}

	sighand = parent->sighand;
	spin_lock_irqsave(&sighand->siglock, flags);
	if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
	    !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
		__group_send_sig_info(SIGCHLD, &info, parent);
	/*
	 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
	 */
	__wake_up_parent(tsk, parent);
	spin_unlock_irqrestore(&sighand->siglock, flags);
}

1975
static inline bool may_ptrace_stop(void)
1976
{
Tejun Heo's avatar
Tejun Heo committed
1977
	if (!likely(current->ptrace))
1978
		return false;
1979 1980 1981 1982 1983 1984
	/*
	 * Are we in the middle of do_coredump?
	 * If so and our tracer is also part of the coredump stopping
	 * is a deadlock situation, and pointless because our tracer
	 * is dead so don't allow us to stop.
	 * If SIGKILL was already sent before the caller unlocked
1985
	 * ->siglock we must see ->core_state != NULL. Otherwise it
1986
	 * is safe to enter schedule().
1987 1988 1989 1990
	 *
	 * This is almost outdated, a task with the pending SIGKILL can't
	 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
	 * after SIGKILL was already dequeued.
1991
	 */
1992
	if (unlikely(current->mm->core_state) &&
1993
	    unlikely(current->mm == current->parent->mm))
1994
		return false;
1995

1996
	return true;
1997 1998
}

Roland McGrath's avatar
Roland McGrath committed
1999
/*
2000
 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath's avatar
Roland McGrath committed
2001 2002
 * Called with the siglock held.
 */
2003
static bool sigkill_pending(struct task_struct *tsk)
Roland McGrath's avatar
Roland McGrath committed
2004
{
2005 2006
	return sigismember(&tsk->pending.signal, SIGKILL) ||
	       sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath's avatar
Roland McGrath committed
2007 2008
}

Linus Torvalds's avatar
Linus Torvalds committed
2009 2010 2011 2012 2013 2014 2015 2016
/*
 * This must be called with current->sighand->siglock held.
 *
 * This should be the path for all ptrace stops.
 * We always set current->last_siginfo while stopped here.
 * That makes it a way to test a stopped process for
 * being ptrace-stopped vs being job-control-stopped.
 *
2017 2018
 * If we actually decide not to stop at all because the tracer
 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds's avatar
Linus Torvalds committed
2019
 */
2020
static void ptrace_stop(int exit_code, int why, int clear_code, kernel_siginfo_t *info)
2021 2022
	__releases(&current->sighand->siglock)
	__acquires(&current->sighand->siglock)
Linus Torvalds's avatar
Linus Torvalds committed
2023
{
2024 2025
	bool gstop_done = false;

Roland McGrath's avatar
Roland McGrath committed
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
	if (arch_ptrace_stop_needed(exit_code, info)) {
		/*
		 * The arch code has something special to do before a
		 * ptrace stop.  This is allowed to block, e.g. for faults
		 * on user stack pages.  We can't keep the siglock while
		 * calling arch_ptrace_stop, so we must release it now.
		 * To preserve proper semantics, we must do this before
		 * any signal bookkeeping like checking group_stop_count.
		 * Meanwhile, a SIGKILL could come in before we retake the
		 * siglock.  That must prevent us from sleeping in TASK_TRACED.
		 * So after regaining the lock, we must check for SIGKILL.
		 */
		spin_unlock_irq(&current->sighand->siglock);
		arch_ptrace_stop(exit_code, info);
		spin_lock_irq(&current->sighand->siglock);
2041 2042
		if (sigkill_pending(current))
			return;
Roland McGrath's avatar
Roland McGrath committed
2043 2044
	}

2045 2046
	set_special_state(TASK_TRACED);

Linus Torvalds's avatar
Linus Torvalds committed
2047
	/*
2048 2049 2050 2051 2052
	 * We're committing to trapping.  TRACED should be visible before
	 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
	 * Also, transition to TRACED and updates to ->jobctl should be
	 * atomic with respect to siglock and should be done after the arch
	 * hook as siglock is released and regrabbed across it.
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
	 *
	 *     TRACER				    TRACEE
	 *
	 *     ptrace_attach()
	 * [L]   wait_on_bit(JOBCTL_TRAPPING)	[S] set_special_state(TRACED)
	 *     do_wait()
	 *       set_current_state()                smp_wmb();
	 *       ptrace_do_wait()
	 *         wait_task_stopped()
	 *           task_stopped_code()
	 * [L]         task_is_traced()		[S] task_clear_jobctl_trapping();
Linus Torvalds's avatar
Linus Torvalds committed
2064
	 */
2065
	smp_wmb();
Linus Torvalds's avatar
Linus Torvalds committed
2066 2067 2068 2069

	current->last_siginfo = info;
	current->exit_code = exit_code;

2070
	/*
2071 2072
	 * If @why is CLD_STOPPED, we're trapping to participate in a group
	 * stop.  Do the bookkeeping.  Note that if SIGCONT was delievered
2073 2074 2075
	 * across siglock relocks since INTERRUPT was scheduled, PENDING
	 * could be clear now.  We act as if SIGCONT is received after
	 * TASK_TRACED is entered - ignore it.
2076
	 */
2077
	if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
2078
		gstop_done = task_participate_group_stop(current);
2079

2080
	/* any trap clears pending STOP trap, STOP trap clears NOTIFY */
2081
	task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
2082 2083
	if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
		task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
2084

2085
	/* entering a trap, clear TRAPPING */
2086
	task_clear_jobctl_trapping(current);
2087

Linus Torvalds's avatar
Linus Torvalds committed
2088 2089
	spin_unlock_irq(&current->sighand->siglock);
	read_lock(&tasklist_lock);
2090
	if (may_ptrace_stop()) {
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
		/*
		 * Notify parents of the stop.
		 *
		 * While ptraced, there are two parents - the ptracer and
		 * the real_parent of the group_leader.  The ptracer should
		 * know about every stop while the real parent is only
		 * interested in the completion of group stop.  The states
		 * for the two don't interact with each other.  Notify
		 * separately unless they're gonna be duplicates.
		 */
		do_notify_parent_cldstop(current, true, why);
2102
		if (gstop_done && ptrace_reparented(current))
2103 2104
			do_notify_parent_cldstop(current, false, why);

Miklos Szeredi's avatar
Miklos Szeredi committed
2105 2106 2107 2108 2109 2110 2111
		/*
		 * Don't want to allow preemption here, because
		 * sys_ptrace() needs this task to be inactive.
		 *
		 * XXX: implement read_unlock_no_resched().
		 */
		preempt_disable();
Linus Torvalds's avatar
Linus Torvalds committed
2112
		read_unlock(&tasklist_lock);
Miklos Szeredi's avatar
Miklos Szeredi committed
2113
		preempt_enable_no_resched();
Roman Gushchin's avatar
Roman Gushchin committed
2114
		cgroup_enter_frozen();
2115
		freezable_schedule();
Linus Torvalds's avatar
Linus Torvalds committed
2116 2117 2118
	} else {
		/*
		 * By the time we got the lock, our tracer went away.
2119
		 * Don't drop the lock yet, another tracer may come.
2120 2121 2122
		 *
		 * If @gstop_done, the ptracer went away between group stop
		 * completion and here.  During detach, it would have set
2123 2124 2125
		 * JOBCTL_STOP_PENDING on us and we'll re-enter
		 * TASK_STOPPED in do_signal_stop() on return, so notifying
		 * the real parent of the group stop completion is enough.
Linus Torvalds's avatar
Linus Torvalds committed
2126
		 */
2127 2128 2129
		if (gstop_done)
			do_notify_parent_cldstop(current, false, why);

2130
		/* tasklist protects us from ptrace_freeze_traced() */
2131
		__set_current_state(TASK_RUNNING);
2132 2133
		if (clear_code)
			current->exit_code = 0;
2134
		read_unlock(&tasklist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
	}

	/*
	 * We are back.  Now reacquire the siglock before touching
	 * last_siginfo, so that we are sure to have synchronized with
	 * any signal-sending on another CPU that wants to examine it.
	 */
	spin_lock_irq(&current->sighand->siglock);
	current->last_siginfo = NULL;

Tejun Heo's avatar
Tejun Heo committed
2145 2146 2147
	/* LISTENING can be set only during STOP traps, clear it */
	current->jobctl &= ~JOBCTL_LISTENING;

Linus Torvalds's avatar
Linus Torvalds committed
2148 2149 2150
	/*
	 * Queued signals ignored us while we were stopped for tracing.
	 * So check for any that we should take before resuming user mode.
2151
	 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds's avatar
Linus Torvalds committed
2152
	 */
2153
	recalc_sigpending_tsk(current);
Linus Torvalds's avatar
Linus Torvalds committed
2154 2155
}

Tejun Heo's avatar
Tejun Heo committed
2156
static void ptrace_do_notify(int signr, int exit_code, int why)
Linus Torvalds's avatar
Linus Torvalds committed
2157
{
2158
	kernel_siginfo_t info;
Linus Torvalds's avatar
Linus Torvalds committed
2159

2160
	clear_siginfo(&info);
Tejun Heo's avatar
Tejun Heo committed
2161
	info.si_signo = signr;
Linus Torvalds's avatar
Linus Torvalds committed
2162
	info.si_code = exit_code;
2163
	info.si_pid = task_pid_vnr(current);
2164
	info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds's avatar
Linus Torvalds committed
2165 2166

	/* Let the debugger run.  */
Tejun Heo's avatar
Tejun Heo committed
2167 2168 2169 2170 2171 2172
	ptrace_stop(exit_code, why, 1, &info);
}

void ptrace_notify(int exit_code)
{
	BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
2173 2174
	if (unlikely(current->task_works))
		task_work_run();
Tejun Heo's avatar
Tejun Heo committed
2175

Linus Torvalds's avatar
Linus Torvalds committed
2176
	spin_lock_irq(&current->sighand->siglock);
Tejun Heo's avatar
Tejun Heo committed
2177
	ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
Linus Torvalds's avatar
Linus Torvalds committed
2178 2179 2180
	spin_unlock_irq(&current->sighand->siglock);
}

2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
/**
 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
 * @signr: signr causing group stop if initiating
 *
 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
 * and participate in it.  If already set, participate in the existing
 * group stop.  If participated in a group stop (and thus slept), %true is
 * returned with siglock released.
 *
 * If ptraced, this function doesn't handle stop itself.  Instead,
 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
 * untouched.  The caller must ensure that INTERRUPT trap handling takes
 * places afterwards.
 *
 * CONTEXT:
 * Must be called with @current->sighand->siglock held, which is released
 * on %true return.
 *
 * RETURNS:
 * %false if group stop is already cancelled or ptrace trap is scheduled.
 * %true if participated in group stop.
Linus Torvalds's avatar
Linus Torvalds committed
2202
 */
2203 2204
static bool do_signal_stop(int signr)
	__releases(&current->sighand->siglock)
Linus Torvalds's avatar
Linus Torvalds committed
2205 2206 2207
{
	struct signal_struct *sig = current->signal;

2208
	if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2209
		unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
2210 2211
		struct task_struct *t;

2212 2213
		/* signr will be recorded in task->jobctl for retries */
		WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
2214

2215
		if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
2216
		    unlikely(signal_group_exit(sig)))
2217
			return false;
Linus Torvalds's avatar
Linus Torvalds committed
2218
		/*
2219 2220 2221 2222 2223 2224 2225
		 * There is no group stop already in progress.  We must
		 * initiate one now.
		 *
		 * While ptraced, a task may be resumed while group stop is
		 * still in effect and then receive a stop signal and
		 * initiate another group stop.  This deviates from the
		 * usual behavior as two consecutive stop signals can't
2226 2227
		 * cause two group stops when !ptraced.  That is why we
		 * also check !task_is_stopped(t) below.
2228 2229 2230 2231 2232 2233 2234 2235
		 *
		 * The condition can be distinguished by testing whether
		 * SIGNAL_STOP_STOPPED is already set.  Don't generate
		 * group_exit_code in such case.
		 *
		 * This is not necessary for SIGNAL_STOP_CONTINUED because
		 * an intervening stop signal is required to cause two
		 * continued events regardless of ptrace.
Linus Torvalds's avatar
Linus Torvalds committed
2236
		 */
2237 2238
		if (!(sig->flags & SIGNAL_STOP_STOPPED))
			sig->group_exit_code = signr;
Linus Torvalds's avatar
Linus Torvalds committed
2239

2240 2241 2242 2243
		sig->group_stop_count = 0;

		if (task_set_jobctl_pending(current, signr | gstop))
			sig->group_stop_count++;
Linus Torvalds's avatar
Linus Torvalds committed
2244

2245 2246
		t = current;
		while_each_thread(current, t) {
Linus Torvalds's avatar
Linus Torvalds committed
2247
			/*
2248 2249 2250
			 * Setting state to TASK_STOPPED for a group
			 * stop is always done with the siglock held,
			 * so this check has no races.
Linus Torvalds's avatar
Linus Torvalds committed
2251
			 */
2252 2253
			if (!task_is_stopped(t) &&
			    task_set_jobctl_pending(t, signr | gstop)) {
2254
				sig->group_stop_count++;
2255 2256 2257 2258
				if (likely(!(t->ptrace & PT_SEIZED)))
					signal_wake_up(t, 0);
				else
					ptrace_trap_notify(t);
2259
			}
2260
		}
Linus Torvalds's avatar
Linus Torvalds committed
2261
	}
2262

Tejun Heo's avatar
Tejun Heo committed
2263
	if (likely(!current->ptrace)) {
2264
		int notify = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2265

2266 2267 2268 2269 2270 2271 2272 2273
		/*
		 * If there are no other threads in the group, or if there
		 * is a group stop in progress and we are the last to stop,
		 * report to the parent.
		 */
		if (task_participate_group_stop(current))
			notify = CLD_STOPPED;

2274
		set_special_state(TASK_STOPPED);
2275 2276
		spin_unlock_irq(&current->sighand->siglock);

2277 2278 2279 2280 2281 2282 2283 2284 2285
		/*
		 * Notify the parent of the group stop completion.  Because
		 * we're not holding either the siglock or tasklist_lock
		 * here, ptracer may attach inbetween; however, this is for
		 * group stop and should always be delivered to the real
		 * parent of the group leader.  The new ptracer will get
		 * its notification when this task transitions into
		 * TASK_TRACED.
		 */
2286 2287
		if (notify) {
			read_lock(&tasklist_lock);
2288
			do_notify_parent_cldstop(current, false, notify);
2289 2290 2291 2292
			read_unlock(&tasklist_lock);
		}

		/* Now we don't run again until woken by SIGCONT or SIGKILL */
Roman Gushchin's avatar
Roman Gushchin committed
2293
		cgroup_enter_frozen();
2294
		freezable_schedule();
2295
		return true;
2296
	} else {
2297 2298 2299 2300 2301 2302
		/*
		 * While ptraced, group stop is handled by STOP trap.
		 * Schedule it and let the caller deal with it.
		 */
		task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
		return false;
2303
	}
2304
}
Linus Torvalds's avatar
Linus Torvalds committed
2305

2306 2307 2308
/**
 * do_jobctl_trap - take care of ptrace jobctl traps
 *
Tejun Heo's avatar
Tejun Heo committed
2309 2310 2311 2312 2313 2314 2315
 * When PT_SEIZED, it's used for both group stop and explicit
 * SEIZE/INTERRUPT traps.  Both generate PTRACE_EVENT_STOP trap with
 * accompanying siginfo.  If stopped, lower eight bits of exit_code contain
 * the stop signal; otherwise, %SIGTRAP.
 *
 * When !PT_SEIZED, it's used only for group stop trap with stop signal
 * number as exit_code and no siginfo.
2316 2317 2318 2319 2320 2321 2322
 *
 * CONTEXT:
 * Must be called with @current->sighand->siglock held, which may be
 * released and re-acquired before returning with intervening sleep.
 */
static void do_jobctl_trap(void)
{
Tejun Heo's avatar
Tejun Heo committed
2323
	struct signal_struct *signal = current->signal;
2324
	int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2325

Tejun Heo's avatar
Tejun Heo committed
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
	if (current->ptrace & PT_SEIZED) {
		if (!signal->group_stop_count &&
		    !(signal->flags & SIGNAL_STOP_STOPPED))
			signr = SIGTRAP;
		WARN_ON_ONCE(!signr);
		ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
				 CLD_STOPPED);
	} else {
		WARN_ON_ONCE(!signr);
		ptrace_stop(signr, CLD_STOPPED, 0, NULL);
		current->exit_code = 0;
2337
	}
Linus Torvalds's avatar
Linus Torvalds committed
2338 2339
}

Roman Gushchin's avatar
Roman Gushchin committed
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
/**
 * do_freezer_trap - handle the freezer jobctl trap
 *
 * Puts the task into frozen state, if only the task is not about to quit.
 * In this case it drops JOBCTL_TRAP_FREEZE.
 *
 * CONTEXT:
 * Must be called with @current->sighand->siglock held,
 * which is always released before returning.
 */
static void do_freezer_trap(void)
	__releases(&current->sighand->siglock)
{
	/*
	 * If there are other trap bits pending except JOBCTL_TRAP_FREEZE,
	 * let's make another loop to give it a chance to be handled.
	 * In any case, we'll return back.
	 */
	if ((current->jobctl & (JOBCTL_PENDING_MASK | JOBCTL_TRAP_FREEZE)) !=
	     JOBCTL_TRAP_FREEZE) {
		spin_unlock_irq(&current->sighand->siglock);
		return;
	}

	/*
	 * Now we're sure that there is no pending fatal signal and no
	 * pending traps. Clear TIF_SIGPENDING to not get out of schedule()
	 * immediately (if there is a non-fatal signal pending), and
	 * put the task into sleep.
	 */
	__set_current_state(TASK_INTERRUPTIBLE);
	clear_thread_flag(TIF_SIGPENDING);
	spin_unlock_irq(&current->sighand->siglock);
	cgroup_enter_frozen();
	freezable_schedule();
}

2377
static int ptrace_signal(int signr, kernel_siginfo_t *info)
Roland McGrath's avatar
Roland McGrath committed
2378
{
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388
	/*
	 * We do not check sig_kernel_stop(signr) but set this marker
	 * unconditionally because we do not know whether debugger will
	 * change signr. This flag has no meaning unless we are going
	 * to stop after return from ptrace_stop(). In this case it will
	 * be checked in do_signal_stop(), we should only stop if it was
	 * not cleared by SIGCONT while we were sleeping. See also the
	 * comment in dequeue_signal().
	 */
	current->jobctl |= JOBCTL_STOP_DEQUEUED;
2389
	ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath's avatar
Roland McGrath committed
2390 2391 2392 2393 2394 2395 2396 2397

	/* We're back.  Did the debugger cancel the sig?  */
	signr = current->exit_code;
	if (signr == 0)
		return signr;

	current->exit_code = 0;

2398 2399 2400 2401 2402 2403
	/*
	 * Update the siginfo structure if the signal has
	 * changed.  If the debugger wanted something
	 * specific in the siginfo structure then it should
	 * have updated *info via PTRACE_SETSIGINFO.
	 */
Roland McGrath's avatar
Roland McGrath committed
2404
	if (signr != info->si_signo) {
2405
		clear_siginfo(info);
Roland McGrath's avatar
Roland McGrath committed
2406 2407 2408
		info->si_signo = signr;
		info->si_errno = 0;
		info->si_code = SI_USER;
2409
		rcu_read_lock();
Roland McGrath's avatar
Roland McGrath committed
2410
		info->si_pid = task_pid_vnr(current->parent);
2411 2412
		info->si_uid = from_kuid_munged(current_user_ns(),
						task_uid(current->parent));
2413
		rcu_read_unlock();
Roland McGrath's avatar
Roland McGrath committed
2414 2415 2416 2417
	}

	/* If the (new) signal is now blocked, requeue it.  */
	if (sigismember(&current->blocked, signr)) {
2418
		send_signal(signr, info, current, PIDTYPE_PID);
Roland McGrath's avatar
Roland McGrath committed
2419 2420 2421 2422 2423 2424
		signr = 0;
	}

	return signr;
}

2425
bool get_signal(struct ksignal *ksig)
Linus Torvalds's avatar
Linus Torvalds committed
2426
{
2427 2428 2429
	struct sighand_struct *sighand = current->sighand;
	struct signal_struct *signal = current->signal;
	int signr;
Linus Torvalds's avatar
Linus Torvalds committed
2430

2431 2432
	if (unlikely(current->task_works))
		task_work_run();
2433

2434
	if (unlikely(uprobe_deny_signal()))
2435
		return false;
2436

2437
	/*
2438 2439 2440
	 * Do this once, we can't return to user-mode if freezing() == T.
	 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
	 * thus do not need another check after return.
2441
	 */
2442 2443
	try_to_freeze();

2444
relock:
2445
	spin_lock_irq(&sighand->siglock);
2446 2447 2448 2449 2450
	/*
	 * Every stopped thread goes here after wakeup. Check to see if
	 * we should notify the parent, prepare_signal(SIGCONT) encodes
	 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
	 */
2451
	if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2452 2453 2454 2455 2456 2457 2458
		int why;

		if (signal->flags & SIGNAL_CLD_CONTINUED)
			why = CLD_CONTINUED;
		else
			why = CLD_STOPPED;

2459
		signal->flags &= ~SIGNAL_CLD_MASK;
2460

2461
		spin_unlock_irq(&sighand->siglock);
Roland McGrath's avatar
Roland McGrath committed
2462

2463 2464 2465 2466 2467 2468 2469 2470
		/*
		 * Notify the parent that we're continuing.  This event is
		 * always per-process and doesn't make whole lot of sense
		 * for ptracers, who shouldn't consume the state via
		 * wait(2) either, but, for backward compatibility, notify
		 * the ptracer of the group leader too unless it's gonna be
		 * a duplicate.
		 */
2471
		read_lock(&tasklist_lock);
2472 2473
		do_notify_parent_cldstop(current, false, why);

2474 2475 2476
		if (ptrace_reparented(current->group_leader))
			do_notify_parent_cldstop(current->group_leader,
						true, why);
2477
		read_unlock(&tasklist_lock);
2478

2479 2480 2481
		goto relock;
	}

2482
	/* Has this task already been marked for death? */
2483 2484 2485 2486
	if (signal_group_exit(signal)) {
		ksig->info.si_signo = signr = SIGKILL;
		sigdelset(&current->pending.signal, SIGKILL);
		recalc_sigpending();
2487
		goto fatal;
2488
	}
2489

Linus Torvalds's avatar
Linus Torvalds committed
2490 2491
	for (;;) {
		struct k_sigaction *ka;
2492

2493 2494
		if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
		    do_signal_stop(0))
2495
			goto relock;
2496

Roman Gushchin's avatar
Roman Gushchin committed
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
		if (unlikely(current->jobctl &
			     (JOBCTL_TRAP_MASK | JOBCTL_TRAP_FREEZE))) {
			if (current->jobctl & JOBCTL_TRAP_MASK) {
				do_jobctl_trap();
				spin_unlock_irq(&sighand->siglock);
			} else if (current->jobctl & JOBCTL_TRAP_FREEZE)
				do_freezer_trap();

			goto relock;
		}

		/*
		 * If the task is leaving the frozen state, let's update
		 * cgroup counters and reset the frozen bit.
		 */
		if (unlikely(cgroup_task_frozen(current))) {
2513
			spin_unlock_irq(&sighand->siglock);
2514
			cgroup_leave_frozen(false);
2515 2516
			goto relock;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2517

2518 2519 2520 2521 2522 2523 2524 2525 2526
		/*
		 * Signals generated by the execution of an instruction
		 * need to be delivered before any other pending signals
		 * so that the instruction pointer in the signal stack
		 * frame points to the faulting instruction.
		 */
		signr = dequeue_synchronous_signal(&ksig->info);
		if (!signr)
			signr = dequeue_signal(current, &current->blocked, &ksig->info);
2527

2528 2529
		if (!signr)
			break; /* will return 0 */
2530

2531
		if (unlikely(current->ptrace) && signr != SIGKILL) {
2532
			signr = ptrace_signal(signr, &ksig->info);
2533 2534
			if (!signr)
				continue;
Linus Torvalds's avatar
Linus Torvalds committed
2535 2536
		}

2537 2538
		ka = &sighand->action[signr-1];

2539
		/* Trace actually delivered signals. */
2540
		trace_signal_deliver(signr, &ksig->info, ka);
2541

Linus Torvalds's avatar
Linus Torvalds committed
2542 2543 2544 2545
		if (ka->sa.sa_handler == SIG_IGN) /* Do nothing.  */
			continue;
		if (ka->sa.sa_handler != SIG_DFL) {
			/* Run the handler.  */
2546
			ksig->ka = *ka;
Linus Torvalds's avatar
Linus Torvalds committed
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559

			if (ka->sa.sa_flags & SA_ONESHOT)
				ka->sa.sa_handler = SIG_DFL;

			break; /* will return non-zero "signr" value */
		}

		/*
		 * Now we are doing the default action for this signal.
		 */
		if (sig_kernel_ignore(signr)) /* Default is nothing. */
			continue;

2560
		/*
2561
		 * Global init gets no signals it doesn't want.
2562 2563 2564 2565 2566 2567 2568
		 * Container-init gets no signals it doesn't want from same
		 * container.
		 *
		 * Note that if global/container-init sees a sig_kernel_only()
		 * signal here, the signal must have been generated internally
		 * or must have come from an ancestor namespace. In either
		 * case, the signal cannot be dropped.
2569
		 */
2570
		if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2571
				!sig_kernel_only(signr))
Linus Torvalds's avatar
Linus Torvalds committed
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
			continue;

		if (sig_kernel_stop(signr)) {
			/*
			 * The default action is to stop all threads in
			 * the thread group.  The job control signals
			 * do nothing in an orphaned pgrp, but SIGSTOP
			 * always works.  Note that siglock needs to be
			 * dropped during the call to is_orphaned_pgrp()
			 * because of lock ordering with tasklist_lock.
			 * This allows an intervening SIGCONT to be posted.
			 * We need to check for that and bail out if necessary.
			 */
			if (signr != SIGSTOP) {
2586
				spin_unlock_irq(&sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
2587 2588 2589

				/* signals can be posted during this window */

2590
				if (is_current_pgrp_orphaned())
Linus Torvalds's avatar
Linus Torvalds committed
2591 2592
					goto relock;

2593
				spin_lock_irq(&sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
2594 2595
			}

2596
			if (likely(do_signal_stop(ksig->info.si_signo))) {
Linus Torvalds's avatar
Linus Torvalds committed
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
				/* It released the siglock.  */
				goto relock;
			}

			/*
			 * We didn't actually stop, due to a race
			 * with SIGCONT or something like that.
			 */
			continue;
		}

2608
	fatal:
2609
		spin_unlock_irq(&sighand->siglock);
2610 2611
		if (unlikely(cgroup_task_frozen(current)))
			cgroup_leave_frozen(true);
Linus Torvalds's avatar
Linus Torvalds committed
2612 2613 2614 2615 2616

		/*
		 * Anything else is fatal, maybe with a core dump.
		 */
		current->flags |= PF_SIGNALED;
2617

Linus Torvalds's avatar
Linus Torvalds committed
2618
		if (sig_kernel_coredump(signr)) {
2619
			if (print_fatal_signals)
2620
				print_fatal_signal(ksig->info.si_signo);
2621
			proc_coredump_connector(current);
Linus Torvalds's avatar
Linus Torvalds committed
2622 2623 2624 2625 2626 2627 2628 2629
			/*
			 * If it was able to dump core, this kills all
			 * other threads in the group and synchronizes with
			 * their demise.  If we lost the race with another
			 * thread getting here, it set group_exit_code
			 * first and our do_group_exit call below will use
			 * that value and ignore the one we pass it.
			 */
2630
			do_coredump(&ksig->info);
Linus Torvalds's avatar
Linus Torvalds committed
2631 2632 2633 2634 2635
		}

		/*
		 * Death signals, no core dump.
		 */
2636
		do_group_exit(ksig->info.si_signo);
Linus Torvalds's avatar
Linus Torvalds committed
2637 2638
		/* NOTREACHED */
	}
2639
	spin_unlock_irq(&sighand->siglock);
2640 2641 2642

	ksig->sig = signr;
	return ksig->sig > 0;
Linus Torvalds's avatar
Linus Torvalds committed
2643 2644
}

2645
/**
Al Viro's avatar
Al Viro committed
2646
 * signal_delivered - 
2647
 * @ksig:		kernel signal struct
Al Viro's avatar
Al Viro committed
2648
 * @stepping:		nonzero if debugger single-step or block-step in use
2649
 *
2650
 * This function should be called when a signal has successfully been
2651
 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
Al Viro's avatar
Al Viro committed
2652
 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2653
 * is set in @ksig->ka.sa.sa_flags.  Tracing is notified.
2654
 */
2655
static void signal_delivered(struct ksignal *ksig, int stepping)
2656 2657 2658
{
	sigset_t blocked;

2659 2660 2661 2662 2663 2664
	/* A signal was successfully delivered, and the
	   saved sigmask was stored on the signal frame,
	   and will be restored by sigreturn.  So we can
	   simply clear the restore sigmask flag.  */
	clear_restore_sigmask();

2665 2666 2667
	sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
	if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
		sigaddset(&blocked, ksig->sig);
2668
	set_current_blocked(&blocked);
2669
	tracehook_signal_handler(stepping);
2670 2671
}

Al Viro's avatar
Al Viro committed
2672 2673 2674 2675 2676
void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
{
	if (failed)
		force_sigsegv(ksig->sig, current);
	else
2677
		signal_delivered(ksig, stepping);
Al Viro's avatar
Al Viro committed
2678 2679
}

2680 2681
/*
 * It could be that complete_signal() picked us to notify about the
2682 2683
 * group-wide signal. Other threads should be notified now to take
 * the shared signals in @which since we will not.
2684
 */
2685
static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2686
{
2687
	sigset_t retarget;
2688 2689
	struct task_struct *t;

2690 2691 2692 2693
	sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
	if (sigisemptyset(&retarget))
		return;

2694 2695
	t = tsk;
	while_each_thread(tsk, t) {
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
		if (t->flags & PF_EXITING)
			continue;

		if (!has_pending_signals(&retarget, &t->blocked))
			continue;
		/* Remove the signals this thread can handle. */
		sigandsets(&retarget, &retarget, &t->blocked);

		if (!signal_pending(t))
			signal_wake_up(t, 0);

		if (sigisemptyset(&retarget))
			break;
2709 2710 2711
	}
}

2712 2713 2714
void exit_signals(struct task_struct *tsk)
{
	int group_stop = 0;
2715
	sigset_t unblocked;
2716

2717 2718 2719 2720
	/*
	 * @tsk is about to have PF_EXITING set - lock out users which
	 * expect stable threadgroup.
	 */
2721
	cgroup_threadgroup_change_begin(tsk);
2722

2723 2724
	if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
		tsk->flags |= PF_EXITING;
2725
		cgroup_threadgroup_change_end(tsk);
2726
		return;
2727 2728
	}

2729
	spin_lock_irq(&tsk->sighand->siglock);
2730 2731 2732 2733 2734
	/*
	 * From now this task is not visible for group-wide signals,
	 * see wants_signal(), do_signal_stop().
	 */
	tsk->flags |= PF_EXITING;
2735

2736
	cgroup_threadgroup_change_end(tsk);
2737

2738 2739 2740
	if (!signal_pending(tsk))
		goto out;

2741 2742 2743
	unblocked = tsk->blocked;
	signotset(&unblocked);
	retarget_shared_pending(tsk, &unblocked);
2744

2745
	if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2746
	    task_participate_group_stop(tsk))
2747
		group_stop = CLD_STOPPED;
2748
out:
2749 2750
	spin_unlock_irq(&tsk->sighand->siglock);

2751 2752 2753 2754
	/*
	 * If group stop has completed, deliver the notification.  This
	 * should always go to the real parent of the group leader.
	 */
2755
	if (unlikely(group_stop)) {
2756
		read_lock(&tasklist_lock);
2757
		do_notify_parent_cldstop(tsk, false, group_stop);
2758 2759 2760 2761
		read_unlock(&tasklist_lock);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
2762 2763 2764 2765
/*
 * System call entry points.
 */

2766 2767 2768
/**
 *  sys_restart_syscall - restart a system call
 */
2769
SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds's avatar
Linus Torvalds committed
2770
{
2771
	struct restart_block *restart = &current->restart_block;
Linus Torvalds's avatar
Linus Torvalds committed
2772 2773 2774 2775 2776 2777 2778 2779
	return restart->fn(restart);
}

long do_no_restart_syscall(struct restart_block *param)
{
	return -EINTR;
}

2780 2781 2782 2783 2784
static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
{
	if (signal_pending(tsk) && !thread_group_empty(tsk)) {
		sigset_t newblocked;
		/* A set of now blocked but previously unblocked signals. */
2785
		sigandnsets(&newblocked, newset, &current->blocked);
2786 2787 2788 2789 2790 2791
		retarget_shared_pending(tsk, &newblocked);
	}
	tsk->blocked = *newset;
	recalc_sigpending();
}

2792 2793 2794 2795 2796 2797
/**
 * set_current_blocked - change current->blocked mask
 * @newset: new mask
 *
 * It is wrong to change ->blocked directly, this helper should be used
 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds's avatar
Linus Torvalds committed
2798
 */
2799 2800 2801
void set_current_blocked(sigset_t *newset)
{
	sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
2802
	__set_current_blocked(newset);
2803 2804 2805
}

void __set_current_blocked(const sigset_t *newset)
2806 2807 2808
{
	struct task_struct *tsk = current;

2809 2810 2811 2812 2813 2814 2815
	/*
	 * In case the signal mask hasn't changed, there is nothing we need
	 * to do. The current->blocked shouldn't be modified by other task.
	 */
	if (sigequalsets(&tsk->blocked, newset))
		return;

2816
	spin_lock_irq(&tsk->sighand->siglock);
2817
	__set_task_blocked(tsk, newset);
2818 2819
	spin_unlock_irq(&tsk->sighand->siglock);
}
Linus Torvalds's avatar
Linus Torvalds committed
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830

/*
 * This is also useful for kernel threads that want to temporarily
 * (or permanently) block certain signals.
 *
 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
 * interface happily blocks "unblockable" signals like SIGKILL
 * and friends.
 */
int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
{
2831 2832
	struct task_struct *tsk = current;
	sigset_t newset;
Linus Torvalds's avatar
Linus Torvalds committed
2833

2834
	/* Lockless, only current can change ->blocked, never from irq */
2835
	if (oldset)
2836
		*oldset = tsk->blocked;
2837

Linus Torvalds's avatar
Linus Torvalds committed
2838 2839
	switch (how) {
	case SIG_BLOCK:
2840
		sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds's avatar
Linus Torvalds committed
2841 2842
		break;
	case SIG_UNBLOCK:
2843
		sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds's avatar
Linus Torvalds committed
2844 2845
		break;
	case SIG_SETMASK:
2846
		newset = *set;
Linus Torvalds's avatar
Linus Torvalds committed
2847 2848
		break;
	default:
2849
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2850
	}
2851

2852
	__set_current_blocked(&newset);
2853
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2854
}
2855
EXPORT_SYMBOL(sigprocmask);
Linus Torvalds's avatar
Linus Torvalds committed
2856

2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
/*
 * The api helps set app-provided sigmasks.
 *
 * This is useful for syscalls such as ppoll, pselect, io_pgetevents and
 * epoll_pwait where a new sigmask is passed from userland for the syscalls.
 */
int set_user_sigmask(const sigset_t __user *usigmask, sigset_t *set,
		     sigset_t *oldset, size_t sigsetsize)
{
	if (!usigmask)
		return 0;

	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;
	if (copy_from_user(set, usigmask, sizeof(sigset_t)))
		return -EFAULT;

	*oldset = current->blocked;
	set_current_blocked(set);

	return 0;
}
EXPORT_SYMBOL(set_user_sigmask);

#ifdef CONFIG_COMPAT
int set_compat_user_sigmask(const compat_sigset_t __user *usigmask,
			    sigset_t *set, sigset_t *oldset,
			    size_t sigsetsize)
{
	if (!usigmask)
		return 0;

	if (sigsetsize != sizeof(compat_sigset_t))
		return -EINVAL;
	if (get_compat_sigset(set, usigmask))
		return -EFAULT;

	*oldset = current->blocked;
	set_current_blocked(set);

	return 0;
}
EXPORT_SYMBOL(set_compat_user_sigmask);
#endif

2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
/*
 * restore_user_sigmask:
 * usigmask: sigmask passed in from userland.
 * sigsaved: saved sigmask when the syscall started and changed the sigmask to
 *           usigmask.
 *
 * This is useful for syscalls such as ppoll, pselect, io_pgetevents and
 * epoll_pwait where a new sigmask is passed in from userland for the syscalls.
 */
void restore_user_sigmask(const void __user *usigmask, sigset_t *sigsaved)
{

	if (!usigmask)
		return;
	/*
	 * When signals are pending, do not restore them here.
	 * Restoring sigmask here can lead to delivering signals that the above
	 * syscalls are intended to block because of the sigmask passed in.
	 */
	if (signal_pending(current)) {
		current->saved_sigmask = *sigsaved;
		set_restore_sigmask();
		return;
	}

	/*
	 * This is needed because the fast syscall return path does not restore
	 * saved_sigmask when signals are not pending.
	 */
	set_current_blocked(sigsaved);
}
EXPORT_SYMBOL(restore_user_sigmask);

2935 2936 2937
/**
 *  sys_rt_sigprocmask - change the list of currently blocked signals
 *  @how: whether to add, remove, or set signals
2938
 *  @nset: stores pending signals
2939 2940 2941
 *  @oset: previous value of signal mask if non-null
 *  @sigsetsize: size of sigset_t type
 */
2942
SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2943
		sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds's avatar
Linus Torvalds committed
2944 2945
{
	sigset_t old_set, new_set;
2946
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
2947 2948 2949

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
2950
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2951

2952 2953 2954 2955 2956
	old_set = current->blocked;

	if (nset) {
		if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2957 2958
		sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));

2959
		error = sigprocmask(how, &new_set, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
2960
		if (error)
2961 2962
			return error;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2963

2964 2965 2966
	if (oset) {
		if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2967
	}
2968 2969

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2970 2971
}

2972 2973 2974
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
		compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
Linus Torvalds's avatar
Linus Torvalds committed
2975
{
2976 2977 2978 2979 2980 2981 2982 2983 2984
	sigset_t old_set = current->blocked;

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

	if (nset) {
		sigset_t new_set;
		int error;
Al Viro's avatar
Al Viro committed
2985
		if (get_compat_sigset(&new_set, nset))
2986 2987 2988 2989 2990 2991 2992
			return -EFAULT;
		sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));

		error = sigprocmask(how, &new_set, NULL);
		if (error)
			return error;
	}
2993
	return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
2994 2995
}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2996

2997
static void do_sigpending(sigset_t *set)
Linus Torvalds's avatar
Linus Torvalds committed
2998 2999
{
	spin_lock_irq(&current->sighand->siglock);
Al Viro's avatar
Al Viro committed
3000
	sigorsets(set, &current->pending.signal,
Linus Torvalds's avatar
Linus Torvalds committed
3001 3002 3003 3004
		  &current->signal->shared_pending.signal);
	spin_unlock_irq(&current->sighand->siglock);

	/* Outside the lock because only this thread touches it.  */
Al Viro's avatar
Al Viro committed
3005
	sigandsets(set, &current->blocked, set);
3006
}
Linus Torvalds's avatar
Linus Torvalds committed
3007

3008 3009 3010
/**
 *  sys_rt_sigpending - examine a pending signal that has been raised
 *			while blocked
3011
 *  @uset: stores pending signals
3012 3013
 *  @sigsetsize: size of sigset_t type or larger
 */
Al Viro's avatar
Al Viro committed
3014
SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
Linus Torvalds's avatar
Linus Torvalds committed
3015
{
Al Viro's avatar
Al Viro committed
3016
	sigset_t set;
3017 3018 3019 3020

	if (sigsetsize > sizeof(*uset))
		return -EINVAL;

3021 3022 3023 3024 3025 3026
	do_sigpending(&set);

	if (copy_to_user(uset, &set, sigsetsize))
		return -EFAULT;

	return 0;
Al Viro's avatar
Al Viro committed
3027 3028 3029 3030 3031
}

#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
		compat_size_t, sigsetsize)
Linus Torvalds's avatar
Linus Torvalds committed
3032
{
Al Viro's avatar
Al Viro committed
3033
	sigset_t set;
3034 3035 3036 3037

	if (sigsetsize > sizeof(*uset))
		return -EINVAL;

3038 3039 3040
	do_sigpending(&set);

	return put_compat_sigset(uset, &set, sigsetsize);
Linus Torvalds's avatar
Linus Torvalds committed
3041
}
Al Viro's avatar
Al Viro committed
3042
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3043

3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
static const struct {
	unsigned char limit, layout;
} sig_sicodes[] = {
	[SIGILL]  = { NSIGILL,  SIL_FAULT },
	[SIGFPE]  = { NSIGFPE,  SIL_FAULT },
	[SIGSEGV] = { NSIGSEGV, SIL_FAULT },
	[SIGBUS]  = { NSIGBUS,  SIL_FAULT },
	[SIGTRAP] = { NSIGTRAP, SIL_FAULT },
#if defined(SIGEMT)
	[SIGEMT]  = { NSIGEMT,  SIL_FAULT },
#endif
	[SIGCHLD] = { NSIGCHLD, SIL_CHLD },
	[SIGPOLL] = { NSIGPOLL, SIL_POLL },
	[SIGSYS]  = { NSIGSYS,  SIL_SYS },
};

3060
static bool known_siginfo_layout(unsigned sig, int si_code)
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078
{
	if (si_code == SI_KERNEL)
		return true;
	else if ((si_code > SI_USER)) {
		if (sig_specific_sicodes(sig)) {
			if (si_code <= sig_sicodes[sig].limit)
				return true;
		}
		else if (si_code <= NSIGPOLL)
			return true;
	}
	else if (si_code >= SI_DETHREAD)
		return true;
	else if (si_code == SI_ASYNCNL)
		return true;
	return false;
}

3079
enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
3080 3081 3082
{
	enum siginfo_layout layout = SIL_KILL;
	if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
3083 3084 3085
		if ((sig < ARRAY_SIZE(sig_sicodes)) &&
		    (si_code <= sig_sicodes[sig].limit)) {
			layout = sig_sicodes[sig].layout;
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096
			/* Handle the exceptions */
			if ((sig == SIGBUS) &&
			    (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
				layout = SIL_FAULT_MCEERR;
			else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
				layout = SIL_FAULT_BNDERR;
#ifdef SEGV_PKUERR
			else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
				layout = SIL_FAULT_PKUERR;
#endif
		}
3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109
		else if (si_code <= NSIGPOLL)
			layout = SIL_POLL;
	} else {
		if (si_code == SI_TIMER)
			layout = SIL_TIMER;
		else if (si_code == SI_SIGIO)
			layout = SIL_POLL;
		else if (si_code < 0)
			layout = SIL_RT;
	}
	return layout;
}

3110 3111 3112 3113 3114
static inline char __user *si_expansion(const siginfo_t __user *info)
{
	return ((char __user *)info) + sizeof(struct kernel_siginfo);
}

3115
int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from)
Linus Torvalds's avatar
Linus Torvalds committed
3116
{
3117
	char __user *expansion = si_expansion(to);
3118
	if (copy_to_user(to, from , sizeof(struct kernel_siginfo)))
Linus Torvalds's avatar
Linus Torvalds committed
3119
		return -EFAULT;
3120
	if (clear_user(expansion, SI_EXPANSION_SIZE))
Linus Torvalds's avatar
Linus Torvalds committed
3121
		return -EFAULT;
3122
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
3123 3124
}

3125 3126
static int post_copy_siginfo_from_user(kernel_siginfo_t *info,
				       const siginfo_t __user *from)
3127
{
3128
	if (unlikely(!known_siginfo_layout(info->si_signo, info->si_code))) {
3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
		char __user *expansion = si_expansion(from);
		char buf[SI_EXPANSION_SIZE];
		int i;
		/*
		 * An unknown si_code might need more than
		 * sizeof(struct kernel_siginfo) bytes.  Verify all of the
		 * extra bytes are 0.  This guarantees copy_siginfo_to_user
		 * will return this data to userspace exactly.
		 */
		if (copy_from_user(&buf, expansion, SI_EXPANSION_SIZE))
			return -EFAULT;
		for (i = 0; i < SI_EXPANSION_SIZE; i++) {
			if (buf[i] != 0)
				return -E2BIG;
		}
	}
3145 3146 3147
	return 0;
}

3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
static int __copy_siginfo_from_user(int signo, kernel_siginfo_t *to,
				    const siginfo_t __user *from)
{
	if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
		return -EFAULT;
	to->si_signo = signo;
	return post_copy_siginfo_from_user(to, from);
}

int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from)
{
	if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
		return -EFAULT;
	return post_copy_siginfo_from_user(to, from);
}

3164
#ifdef CONFIG_COMPAT
3165
int copy_siginfo_to_user32(struct compat_siginfo __user *to,
3166
			   const struct kernel_siginfo *from)
3167 3168 3169 3170 3171
#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
{
	return __copy_siginfo_to_user32(to, from, in_x32_syscall());
}
int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
3172
			     const struct kernel_siginfo *from, bool x32_ABI)
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
#endif
{
	struct compat_siginfo new;
	memset(&new, 0, sizeof(new));

	new.si_signo = from->si_signo;
	new.si_errno = from->si_errno;
	new.si_code  = from->si_code;
	switch(siginfo_layout(from->si_signo, from->si_code)) {
	case SIL_KILL:
		new.si_pid = from->si_pid;
		new.si_uid = from->si_uid;
		break;
	case SIL_TIMER:
		new.si_tid     = from->si_tid;
		new.si_overrun = from->si_overrun;
		new.si_int     = from->si_int;
		break;
	case SIL_POLL:
		new.si_band = from->si_band;
		new.si_fd   = from->si_fd;
		break;
	case SIL_FAULT:
		new.si_addr = ptr_to_compat(from->si_addr);
#ifdef __ARCH_SI_TRAPNO
		new.si_trapno = from->si_trapno;
#endif
3200 3201 3202 3203 3204
		break;
	case SIL_FAULT_MCEERR:
		new.si_addr = ptr_to_compat(from->si_addr);
#ifdef __ARCH_SI_TRAPNO
		new.si_trapno = from->si_trapno;
3205
#endif
3206 3207 3208 3209 3210 3211
		new.si_addr_lsb = from->si_addr_lsb;
		break;
	case SIL_FAULT_BNDERR:
		new.si_addr = ptr_to_compat(from->si_addr);
#ifdef __ARCH_SI_TRAPNO
		new.si_trapno = from->si_trapno;
3212
#endif
3213 3214 3215 3216 3217 3218 3219
		new.si_lower = ptr_to_compat(from->si_lower);
		new.si_upper = ptr_to_compat(from->si_upper);
		break;
	case SIL_FAULT_PKUERR:
		new.si_addr = ptr_to_compat(from->si_addr);
#ifdef __ARCH_SI_TRAPNO
		new.si_trapno = from->si_trapno;
3220
#endif
3221
		new.si_pkey = from->si_pkey;
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
		break;
	case SIL_CHLD:
		new.si_pid    = from->si_pid;
		new.si_uid    = from->si_uid;
		new.si_status = from->si_status;
#ifdef CONFIG_X86_X32_ABI
		if (x32_ABI) {
			new._sifields._sigchld_x32._utime = from->si_utime;
			new._sifields._sigchld_x32._stime = from->si_stime;
		} else
#endif
		{
			new.si_utime = from->si_utime;
			new.si_stime = from->si_stime;
		}
		break;
	case SIL_RT:
		new.si_pid = from->si_pid;
		new.si_uid = from->si_uid;
		new.si_int = from->si_int;
		break;
	case SIL_SYS:
		new.si_call_addr = ptr_to_compat(from->si_call_addr);
		new.si_syscall   = from->si_syscall;
		new.si_arch      = from->si_arch;
		break;
	}

	if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
		return -EFAULT;

	return 0;
}

3256 3257
static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
					 const struct compat_siginfo *from)
3258 3259
{
	clear_siginfo(to);
3260 3261 3262 3263
	to->si_signo = from->si_signo;
	to->si_errno = from->si_errno;
	to->si_code  = from->si_code;
	switch(siginfo_layout(from->si_signo, from->si_code)) {
3264
	case SIL_KILL:
3265 3266
		to->si_pid = from->si_pid;
		to->si_uid = from->si_uid;
3267 3268
		break;
	case SIL_TIMER:
3269 3270 3271
		to->si_tid     = from->si_tid;
		to->si_overrun = from->si_overrun;
		to->si_int     = from->si_int;
3272 3273
		break;
	case SIL_POLL:
3274 3275
		to->si_band = from->si_band;
		to->si_fd   = from->si_fd;
3276 3277
		break;
	case SIL_FAULT:
3278
		to->si_addr = compat_ptr(from->si_addr);
3279
#ifdef __ARCH_SI_TRAPNO
3280
		to->si_trapno = from->si_trapno;
3281
#endif
3282 3283
		break;
	case SIL_FAULT_MCEERR:
3284
		to->si_addr = compat_ptr(from->si_addr);
3285
#ifdef __ARCH_SI_TRAPNO
3286
		to->si_trapno = from->si_trapno;
3287
#endif
3288
		to->si_addr_lsb = from->si_addr_lsb;
3289 3290
		break;
	case SIL_FAULT_BNDERR:
3291
		to->si_addr = compat_ptr(from->si_addr);
3292
#ifdef __ARCH_SI_TRAPNO
3293
		to->si_trapno = from->si_trapno;
3294
#endif
3295 3296
		to->si_lower = compat_ptr(from->si_lower);
		to->si_upper = compat_ptr(from->si_upper);
3297 3298
		break;
	case SIL_FAULT_PKUERR:
3299
		to->si_addr = compat_ptr(from->si_addr);
3300
#ifdef __ARCH_SI_TRAPNO
3301
		to->si_trapno = from->si_trapno;
3302
#endif
3303
		to->si_pkey = from->si_pkey;
3304 3305
		break;
	case SIL_CHLD:
3306 3307 3308
		to->si_pid    = from->si_pid;
		to->si_uid    = from->si_uid;
		to->si_status = from->si_status;
3309 3310
#ifdef CONFIG_X86_X32_ABI
		if (in_x32_syscall()) {
3311 3312
			to->si_utime = from->_sifields._sigchld_x32._utime;
			to->si_stime = from->_sifields._sigchld_x32._stime;
3313 3314 3315
		} else
#endif
		{
3316 3317
			to->si_utime = from->si_utime;
			to->si_stime = from->si_stime;
3318 3319 3320
		}
		break;
	case SIL_RT:
3321 3322 3323
		to->si_pid = from->si_pid;
		to->si_uid = from->si_uid;
		to->si_int = from->si_int;
3324 3325
		break;
	case SIL_SYS:
3326 3327 3328
		to->si_call_addr = compat_ptr(from->si_call_addr);
		to->si_syscall   = from->si_syscall;
		to->si_arch      = from->si_arch;
3329 3330 3331 3332
		break;
	}
	return 0;
}
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355

static int __copy_siginfo_from_user32(int signo, struct kernel_siginfo *to,
				      const struct compat_siginfo __user *ufrom)
{
	struct compat_siginfo from;

	if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
		return -EFAULT;

	from.si_signo = signo;
	return post_copy_siginfo_from_user32(to, &from);
}

int copy_siginfo_from_user32(struct kernel_siginfo *to,
			     const struct compat_siginfo __user *ufrom)
{
	struct compat_siginfo from;

	if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
		return -EFAULT;

	return post_copy_siginfo_from_user32(to, &from);
}
3356 3357
#endif /* CONFIG_COMPAT */

3358 3359 3360 3361 3362 3363
/**
 *  do_sigtimedwait - wait for queued signals specified in @which
 *  @which: queued signals to wait for
 *  @info: if non-null, the signal's siginfo is returned here
 *  @ts: upper bound on process time suspension
 */
3364
static int do_sigtimedwait(const sigset_t *which, kernel_siginfo_t *info,
3365
		    const struct timespec64 *ts)
3366
{
3367
	ktime_t *to = NULL, timeout = KTIME_MAX;
3368 3369
	struct task_struct *tsk = current;
	sigset_t mask = *which;
3370
	int sig, ret = 0;
3371 3372

	if (ts) {
3373
		if (!timespec64_valid(ts))
3374
			return -EINVAL;
3375
		timeout = timespec64_to_ktime(*ts);
3376
		to = &timeout;
3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
	}

	/*
	 * Invert the set of allowed signals to get those we want to block.
	 */
	sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
	signotset(&mask);

	spin_lock_irq(&tsk->sighand->siglock);
	sig = dequeue_signal(tsk, &mask, info);
3387
	if (!sig && timeout) {
3388 3389 3390
		/*
		 * None ready, temporarily unblock those we're interested
		 * while we are sleeping in so that we'll be awakened when
3391 3392
		 * they arrive. Unblocking is always fine, we can avoid
		 * set_current_blocked().
3393 3394 3395 3396 3397 3398
		 */
		tsk->real_blocked = tsk->blocked;
		sigandsets(&tsk->blocked, &tsk->blocked, &mask);
		recalc_sigpending();
		spin_unlock_irq(&tsk->sighand->siglock);

3399 3400 3401
		__set_current_state(TASK_INTERRUPTIBLE);
		ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
							 HRTIMER_MODE_REL);
3402
		spin_lock_irq(&tsk->sighand->siglock);
3403
		__set_task_blocked(tsk, &tsk->real_blocked);
3404
		sigemptyset(&tsk->real_blocked);
3405
		sig = dequeue_signal(tsk, &mask, info);
3406 3407 3408 3409 3410
	}
	spin_unlock_irq(&tsk->sighand->siglock);

	if (sig)
		return sig;
3411
	return ret ? -EINTR : -EAGAIN;
3412 3413
}

3414 3415 3416 3417 3418 3419 3420 3421
/**
 *  sys_rt_sigtimedwait - synchronously wait for queued signals specified
 *			in @uthese
 *  @uthese: queued signals to wait for
 *  @uinfo: if non-null, the signal's siginfo is returned here
 *  @uts: upper bound on process time suspension
 *  @sigsetsize: size of sigset_t type
 */
3422
SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
3423 3424
		siginfo_t __user *, uinfo,
		const struct __kernel_timespec __user *, uts,
3425
		size_t, sigsetsize)
Linus Torvalds's avatar
Linus Torvalds committed
3426 3427
{
	sigset_t these;
3428
	struct timespec64 ts;
3429
	kernel_siginfo_t info;
3430
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
3431 3432 3433 3434 3435 3436 3437

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

	if (copy_from_user(&these, uthese, sizeof(these)))
		return -EFAULT;
3438

Linus Torvalds's avatar
Linus Torvalds committed
3439
	if (uts) {
3440
		if (get_timespec64(&ts, uts))
Linus Torvalds's avatar
Linus Torvalds committed
3441 3442 3443
			return -EFAULT;
	}

3444
	ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds's avatar
Linus Torvalds committed
3445

3446 3447 3448
	if (ret > 0 && uinfo) {
		if (copy_siginfo_to_user(uinfo, &info))
			ret = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
3449 3450 3451 3452 3453
	}

	return ret;
}

3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
#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)
{
	sigset_t these;
	struct timespec64 ts;
	kernel_siginfo_t info;
	int ret;

	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

	if (copy_from_user(&these, uthese, sizeof(these)))
		return -EFAULT;

	if (uts) {
		if (get_old_timespec32(&ts, uts))
			return -EFAULT;
	}

	ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);

	if (ret > 0 && uinfo) {
		if (copy_siginfo_to_user(uinfo, &info))
			ret = -EFAULT;
	}

	return ret;
}
#endif

3487
#ifdef CONFIG_COMPAT
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
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)
{
	sigset_t s;
	struct timespec64 t;
	kernel_siginfo_t info;
	long ret;

	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

	if (get_compat_sigset(&s, uthese))
		return -EFAULT;

	if (uts) {
		if (get_timespec64(&t, uts))
			return -EFAULT;
	}

	ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);

	if (ret > 0 && uinfo) {
		if (copy_siginfo_to_user32(uinfo, &info))
			ret = -EFAULT;
	}

	return ret;
}

#ifdef CONFIG_COMPAT_32BIT_TIME
3519
COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time32, compat_sigset_t __user *, uthese,
3520
		struct compat_siginfo __user *, uinfo,
3521
		struct old_timespec32 __user *, uts, compat_size_t, sigsetsize)
3522 3523
{
	sigset_t s;
3524
	struct timespec64 t;
3525
	kernel_siginfo_t info;
3526 3527 3528 3529 3530
	long ret;

	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

Al Viro's avatar
Al Viro committed
3531
	if (get_compat_sigset(&s, uthese))
3532 3533 3534
		return -EFAULT;

	if (uts) {
3535
		if (get_old_timespec32(&t, uts))
3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
			return -EFAULT;
	}

	ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);

	if (ret > 0 && uinfo) {
		if (copy_siginfo_to_user32(uinfo, &info))
			ret = -EFAULT;
	}

	return ret;
}
#endif
3549
#endif
3550

3551 3552 3553 3554 3555 3556 3557 3558 3559 3560
static inline void prepare_kill_siginfo(int sig, struct kernel_siginfo *info)
{
	clear_siginfo(info);
	info->si_signo = sig;
	info->si_errno = 0;
	info->si_code = SI_USER;
	info->si_pid = task_tgid_vnr(current);
	info->si_uid = from_kuid_munged(current_user_ns(), current_uid());
}

3561 3562 3563 3564 3565
/**
 *  sys_kill - send a signal to a process
 *  @pid: the PID of the process
 *  @sig: signal to be sent
 */
3566
SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds's avatar
Linus Torvalds committed
3567
{
3568
	struct kernel_siginfo info;
Linus Torvalds's avatar
Linus Torvalds committed
3569

3570
	prepare_kill_siginfo(sig, &info);
Linus Torvalds's avatar
Linus Torvalds committed
3571 3572 3573 3574

	return kill_something_info(sig, &info, pid);
}

3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610
/*
 * Verify that the signaler and signalee either are in the same pid namespace
 * or that the signaler's pid namespace is an ancestor of the signalee's pid
 * namespace.
 */
static bool access_pidfd_pidns(struct pid *pid)
{
	struct pid_namespace *active = task_active_pid_ns(current);
	struct pid_namespace *p = ns_of_pid(pid);

	for (;;) {
		if (!p)
			return false;
		if (p == active)
			break;
		p = p->parent;
	}

	return true;
}

static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, siginfo_t *info)
{
#ifdef CONFIG_COMPAT
	/*
	 * Avoid hooking up compat syscalls and instead handle necessary
	 * conversions here. Note, this is a stop-gap measure and should not be
	 * considered a generic solution.
	 */
	if (in_compat_syscall())
		return copy_siginfo_from_user32(
			kinfo, (struct compat_siginfo __user *)info);
#endif
	return copy_siginfo_from_user(kinfo, info);
}

3611 3612 3613 3614 3615 3616 3617 3618
static struct pid *pidfd_to_pid(const struct file *file)
{
	if (file->f_op == &pidfd_fops)
		return file->private_data;

	return tgid_pidfd_to_pid(file);
}

3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
/**
 * sys_pidfd_send_signal - send a signal to a process through a task file
 *                          descriptor
 * @pidfd:  the file descriptor of the process
 * @sig:    signal to be sent
 * @info:   the signal info
 * @flags:  future flags to be passed
 *
 * The syscall currently only signals via PIDTYPE_PID which covers
 * kill(<positive-pid>, <signal>. It does not signal threads or process
 * groups.
 * In order to extend the syscall to threads and process groups the @flags
 * argument should be used. In essence, the @flags argument will determine
 * what is signaled and not the file descriptor itself. Put in other words,
 * grouping is a property of the flags argument not a property of the file
 * descriptor.
 *
 * Return: 0 on success, negative errno on failure
 */
SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
		siginfo_t __user *, info, unsigned int, flags)
{
	int ret;
	struct fd f;
	struct pid *pid;
	kernel_siginfo_t kinfo;

	/* Enforce flags be set to 0 until we add an extension. */
	if (flags)
		return -EINVAL;

3650
	f = fdget(pidfd);
3651 3652 3653 3654
	if (!f.file)
		return -EBADF;

	/* Is this a pidfd? */
3655
	pid = pidfd_to_pid(f.file);
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673
	if (IS_ERR(pid)) {
		ret = PTR_ERR(pid);
		goto err;
	}

	ret = -EINVAL;
	if (!access_pidfd_pidns(pid))
		goto err;

	if (info) {
		ret = copy_siginfo_from_user_any(&kinfo, info);
		if (unlikely(ret))
			goto err;

		ret = -EINVAL;
		if (unlikely(sig != kinfo.si_signo))
			goto err;

3674 3675
		/* Only allow sending arbitrary signals to yourself. */
		ret = -EPERM;
3676
		if ((task_pid(current) != pid) &&
3677 3678
		    (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
			goto err;
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689
	} else {
		prepare_kill_siginfo(sig, &kinfo);
	}

	ret = kill_pid_info(sig, &kinfo, pid);

err:
	fdput(f);
	return ret;
}

Thomas Gleixner's avatar
Thomas Gleixner committed
3690
static int
3691
do_send_specific(pid_t tgid, pid_t pid, int sig, struct kernel_siginfo *info)
Linus Torvalds's avatar
Linus Torvalds committed
3692 3693
{
	struct task_struct *p;
Thomas Gleixner's avatar
Thomas Gleixner committed
3694
	int error = -ESRCH;
Linus Torvalds's avatar
Linus Torvalds committed
3695

3696
	rcu_read_lock();
3697
	p = find_task_by_vpid(pid);
3698
	if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner's avatar
Thomas Gleixner committed
3699
		error = check_kill_permission(sig, info, p);
Linus Torvalds's avatar
Linus Torvalds committed
3700 3701 3702 3703
		/*
		 * The null signal is a permissions and process existence
		 * probe.  No signal is actually delivered.
		 */
3704
		if (!error && sig) {
3705
			error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
3706 3707 3708 3709 3710 3711 3712
			/*
			 * If lock_task_sighand() failed we pretend the task
			 * dies after receiving the signal. The window is tiny,
			 * and the signal is private anyway.
			 */
			if (unlikely(error == -ESRCH))
				error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3713 3714
		}
	}
3715
	rcu_read_unlock();
3716

Linus Torvalds's avatar
Linus Torvalds committed
3717 3718 3719
	return error;
}

Thomas Gleixner's avatar
Thomas Gleixner committed
3720 3721
static int do_tkill(pid_t tgid, pid_t pid, int sig)
{
3722
	struct kernel_siginfo info;
Thomas Gleixner's avatar
Thomas Gleixner committed
3723

3724
	clear_siginfo(&info);
Thomas Gleixner's avatar
Thomas Gleixner committed
3725 3726 3727 3728
	info.si_signo = sig;
	info.si_errno = 0;
	info.si_code = SI_TKILL;
	info.si_pid = task_tgid_vnr(current);
3729
	info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Thomas Gleixner's avatar
Thomas Gleixner committed
3730 3731 3732 3733

	return do_send_specific(tgid, pid, sig, &info);
}

3734 3735 3736 3737 3738 3739
/**
 *  sys_tgkill - send signal to one specific thread
 *  @tgid: the thread group ID of the thread
 *  @pid: the PID of the thread
 *  @sig: signal to be sent
 *
3740
 *  This syscall also checks the @tgid and returns -ESRCH even if the PID
3741 3742 3743
 *  exists but it's not belonging to the target process anymore. This
 *  method solves the problem of threads exiting and PIDs getting reused.
 */
3744
SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
3745 3746 3747 3748 3749 3750 3751 3752
{
	/* This is only valid for single tasks */
	if (pid <= 0 || tgid <= 0)
		return -EINVAL;

	return do_tkill(tgid, pid, sig);
}

3753 3754 3755 3756 3757
/**
 *  sys_tkill - send signal to one specific task
 *  @pid: the PID of the task
 *  @sig: signal to be sent
 *
Linus Torvalds's avatar
Linus Torvalds committed
3758 3759
 *  Send a signal to only one task, even if it's a CLONE_THREAD task.
 */
3760
SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds's avatar
Linus Torvalds committed
3761 3762 3763 3764 3765
{
	/* This is only valid for single tasks */
	if (pid <= 0)
		return -EINVAL;

3766
	return do_tkill(0, pid, sig);
Linus Torvalds's avatar
Linus Torvalds committed
3767 3768
}

3769
static int do_rt_sigqueueinfo(pid_t pid, int sig, kernel_siginfo_t *info)
3770 3771 3772 3773
{
	/* Not even root can pretend to send signals from the kernel.
	 * Nor can they impersonate a kill()/tgkill(), which adds source info.
	 */
3774
	if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3775
	    (task_pid_vnr(current) != pid))
3776
		return -EPERM;
3777

3778 3779 3780 3781
	/* POSIX.1b doesn't mention process groups.  */
	return kill_proc_info(sig, info, pid);
}

3782 3783 3784 3785 3786 3787
/**
 *  sys_rt_sigqueueinfo - send signal information to a signal
 *  @pid: the PID of the thread
 *  @sig: signal to be sent
 *  @uinfo: signal info to be sent
 */
3788 3789
SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
		siginfo_t __user *, uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
3790
{
3791
	kernel_siginfo_t info;
3792
	int ret = __copy_siginfo_from_user(sig, &info, uinfo);
3793 3794
	if (unlikely(ret))
		return ret;
3795 3796
	return do_rt_sigqueueinfo(pid, sig, &info);
}
Linus Torvalds's avatar
Linus Torvalds committed
3797

3798 3799 3800 3801 3802 3803
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
			compat_pid_t, pid,
			int, sig,
			struct compat_siginfo __user *, uinfo)
{
3804
	kernel_siginfo_t info;
3805
	int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
3806 3807 3808
	if (unlikely(ret))
		return ret;
	return do_rt_sigqueueinfo(pid, sig, &info);
Linus Torvalds's avatar
Linus Torvalds committed
3809
}
3810
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3811

3812
static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, kernel_siginfo_t *info)
3813 3814 3815 3816 3817 3818
{
	/* This is only valid for single tasks */
	if (pid <= 0 || tgid <= 0)
		return -EINVAL;

	/* Not even root can pretend to send signals from the kernel.
3819 3820
	 * Nor can they impersonate a kill()/tgkill(), which adds source info.
	 */
3821 3822
	if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
	    (task_pid_vnr(current) != pid))
3823
		return -EPERM;
3824

3825 3826 3827 3828 3829 3830
	return do_send_specific(tgid, pid, sig, info);
}

SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
		siginfo_t __user *, uinfo)
{
3831
	kernel_siginfo_t info;
3832
	int ret = __copy_siginfo_from_user(sig, &info, uinfo);
3833 3834
	if (unlikely(ret))
		return ret;
3835 3836 3837
	return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
}

3838 3839 3840 3841 3842 3843 3844
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
			compat_pid_t, tgid,
			compat_pid_t, pid,
			int, sig,
			struct compat_siginfo __user *, uinfo)
{
3845
	kernel_siginfo_t info;
3846
	int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
3847 3848
	if (unlikely(ret))
		return ret;
3849 3850 3851 3852
	return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
}
#endif

3853
/*
3854
 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
3855
 */
3856
void kernel_sigaction(int sig, __sighandler_t action)
3857
{
3858
	spin_lock_irq(&current->sighand->siglock);
3859 3860 3861
	current->sighand->action[sig - 1].sa.sa_handler = action;
	if (action == SIG_IGN) {
		sigset_t mask;
3862

3863 3864
		sigemptyset(&mask);
		sigaddset(&mask, sig);
3865

3866 3867 3868 3869
		flush_sigqueue_mask(&mask, &current->signal->shared_pending);
		flush_sigqueue_mask(&mask, &current->pending);
		recalc_sigpending();
	}
3870 3871
	spin_unlock_irq(&current->sighand->siglock);
}
3872
EXPORT_SYMBOL(kernel_sigaction);
3873

3874 3875 3876 3877 3878
void __weak sigaction_compat_abi(struct k_sigaction *act,
		struct k_sigaction *oact)
{
}

3879
int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds's avatar
Linus Torvalds committed
3880
{
3881
	struct task_struct *p = current, *t;
Linus Torvalds's avatar
Linus Torvalds committed
3882
	struct k_sigaction *k;
3883
	sigset_t mask;
Linus Torvalds's avatar
Linus Torvalds committed
3884

3885
	if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds's avatar
Linus Torvalds committed
3886 3887
		return -EINVAL;

3888
	k = &p->sighand->action[sig-1];
Linus Torvalds's avatar
Linus Torvalds committed
3889

3890
	spin_lock_irq(&p->sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
3891 3892 3893
	if (oact)
		*oact = *k;

3894 3895
	sigaction_compat_abi(act, oact);

Linus Torvalds's avatar
Linus Torvalds committed
3896
	if (act) {
3897 3898
		sigdelsetmask(&act->sa.sa_mask,
			      sigmask(SIGKILL) | sigmask(SIGSTOP));
3899
		*k = *act;
Linus Torvalds's avatar
Linus Torvalds committed
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910
		/*
		 * POSIX 3.3.1.3:
		 *  "Setting a signal action to SIG_IGN for a signal that is
		 *   pending shall cause the pending signal to be discarded,
		 *   whether or not it is blocked."
		 *
		 *  "Setting a signal action to SIG_DFL for a signal that is
		 *   pending and whose default action is to ignore the signal
		 *   (for example, SIGCHLD), shall cause the pending signal to
		 *   be discarded, whether or not it is blocked"
		 */
3911
		if (sig_handler_ignored(sig_handler(p, sig), sig)) {
3912 3913
			sigemptyset(&mask);
			sigaddset(&mask, sig);
3914 3915
			flush_sigqueue_mask(&mask, &p->signal->shared_pending);
			for_each_thread(p, t)
3916
				flush_sigqueue_mask(&mask, &t->pending);
Linus Torvalds's avatar
Linus Torvalds committed
3917 3918 3919
		}
	}

3920
	spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
3921 3922 3923
	return 0;
}

3924
static int
3925 3926
do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
		size_t min_ss_size)
Linus Torvalds's avatar
Linus Torvalds committed
3927
{
3928
	struct task_struct *t = current;
Linus Torvalds's avatar
Linus Torvalds committed
3929

3930 3931 3932 3933 3934 3935 3936
	if (oss) {
		memset(oss, 0, sizeof(stack_t));
		oss->ss_sp = (void __user *) t->sas_ss_sp;
		oss->ss_size = t->sas_ss_size;
		oss->ss_flags = sas_ss_flags(sp) |
			(current->sas_ss_flags & SS_FLAG_BITS);
	}
Linus Torvalds's avatar
Linus Torvalds committed
3937

3938 3939 3940 3941
	if (ss) {
		void __user *ss_sp = ss->ss_sp;
		size_t ss_size = ss->ss_size;
		unsigned ss_flags = ss->ss_flags;
3942
		int ss_mode;
Linus Torvalds's avatar
Linus Torvalds committed
3943

3944 3945
		if (unlikely(on_sig_stack(sp)))
			return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
3946

3947
		ss_mode = ss_flags & ~SS_FLAG_BITS;
3948 3949 3950
		if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
				ss_mode != 0))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
3951

3952
		if (ss_mode == SS_DISABLE) {
Linus Torvalds's avatar
Linus Torvalds committed
3953 3954 3955
			ss_size = 0;
			ss_sp = NULL;
		} else {
3956
			if (unlikely(ss_size < min_ss_size))
3957
				return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
3958 3959
		}

3960 3961 3962
		t->sas_ss_sp = (unsigned long) ss_sp;
		t->sas_ss_size = ss_size;
		t->sas_ss_flags = ss_flags;
Linus Torvalds's avatar
Linus Torvalds committed
3963
	}
3964
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
3965
}
3966

3967 3968
SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
{
3969 3970 3971 3972 3973
	stack_t new, old;
	int err;
	if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
		return -EFAULT;
	err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
3974 3975
			      current_user_stack_pointer(),
			      MINSIGSTKSZ);
3976 3977 3978
	if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
		err = -EFAULT;
	return err;
3979
}
Linus Torvalds's avatar
Linus Torvalds committed
3980

Al Viro's avatar
Al Viro committed
3981 3982
int restore_altstack(const stack_t __user *uss)
{
3983 3984 3985
	stack_t new;
	if (copy_from_user(&new, uss, sizeof(stack_t)))
		return -EFAULT;
3986 3987
	(void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
			     MINSIGSTKSZ);
Al Viro's avatar
Al Viro committed
3988
	/* squash all but EFAULT for now */
3989
	return 0;
Al Viro's avatar
Al Viro committed
3990 3991
}

3992 3993 3994
int __save_altstack(stack_t __user *uss, unsigned long sp)
{
	struct task_struct *t = current;
3995 3996
	int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
		__put_user(t->sas_ss_flags, &uss->ss_flags) |
3997
		__put_user(t->sas_ss_size, &uss->ss_size);
3998 3999 4000 4001 4002
	if (err)
		return err;
	if (t->sas_ss_flags & SS_AUTODISARM)
		sas_ss_reset(t);
	return 0;
4003 4004
}

Al Viro's avatar
Al Viro committed
4005
#ifdef CONFIG_COMPAT
4006 4007
static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
				 compat_stack_t __user *uoss_ptr)
Al Viro's avatar
Al Viro committed
4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019
{
	stack_t uss, uoss;
	int ret;

	if (uss_ptr) {
		compat_stack_t uss32;
		if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
			return -EFAULT;
		uss.ss_sp = compat_ptr(uss32.ss_sp);
		uss.ss_flags = uss32.ss_flags;
		uss.ss_size = uss32.ss_size;
	}
4020
	ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
4021 4022
			     compat_user_stack_pointer(),
			     COMPAT_MINSIGSTKSZ);
Al Viro's avatar
Al Viro committed
4023
	if (ret >= 0 && uoss_ptr)  {
4024 4025 4026 4027 4028 4029
		compat_stack_t old;
		memset(&old, 0, sizeof(old));
		old.ss_sp = ptr_to_compat(uoss.ss_sp);
		old.ss_flags = uoss.ss_flags;
		old.ss_size = uoss.ss_size;
		if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
Al Viro's avatar
Al Viro committed
4030 4031 4032 4033 4034
			ret = -EFAULT;
	}
	return ret;
}

4035 4036 4037 4038 4039 4040 4041
COMPAT_SYSCALL_DEFINE2(sigaltstack,
			const compat_stack_t __user *, uss_ptr,
			compat_stack_t __user *, uoss_ptr)
{
	return do_compat_sigaltstack(uss_ptr, uoss_ptr);
}

Al Viro's avatar
Al Viro committed
4042 4043
int compat_restore_altstack(const compat_stack_t __user *uss)
{
4044
	int err = do_compat_sigaltstack(uss, NULL);
Al Viro's avatar
Al Viro committed
4045 4046 4047
	/* squash all but -EFAULT for now */
	return err == -EFAULT ? err : 0;
}
4048 4049 4050

int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
{
4051
	int err;
4052
	struct task_struct *t = current;
4053 4054 4055
	err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
			 &uss->ss_sp) |
		__put_user(t->sas_ss_flags, &uss->ss_flags) |
4056
		__put_user(t->sas_ss_size, &uss->ss_size);
4057 4058 4059 4060 4061
	if (err)
		return err;
	if (t->sas_ss_flags & SS_AUTODISARM)
		sas_ss_reset(t);
	return 0;
4062
}
Al Viro's avatar
Al Viro committed
4063
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4064 4065 4066

#ifdef __ARCH_WANT_SYS_SIGPENDING

4067 4068
/**
 *  sys_sigpending - examine pending signals
4069
 *  @uset: where mask of pending signal is returned
4070
 */
4071
SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
Linus Torvalds's avatar
Linus Torvalds committed
4072
{
4073 4074 4075 4076 4077
	sigset_t set;

	if (sizeof(old_sigset_t) > sizeof(*uset))
		return -EINVAL;

4078 4079 4080 4081 4082 4083
	do_sigpending(&set);

	if (copy_to_user(uset, &set, sizeof(old_sigset_t)))
		return -EFAULT;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
4084 4085
}

4086 4087 4088 4089
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
{
	sigset_t set;
4090 4091 4092 4093

	do_sigpending(&set);

	return put_user(set.sig[0], set32);
4094 4095 4096
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
4097 4098 4099
#endif

#ifdef __ARCH_WANT_SYS_SIGPROCMASK
4100 4101 4102
/**
 *  sys_sigprocmask - examine and change blocked signals
 *  @how: whether to add, remove, or set signals
4103
 *  @nset: signals to add or remove (if non-null)
4104 4105
 *  @oset: previous value of signal mask if non-null
 *
4106 4107 4108
 * Some platforms have their own version with special arguments;
 * others support only sys_rt_sigprocmask.
 */
Linus Torvalds's avatar
Linus Torvalds committed
4109

4110
SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
4111
		old_sigset_t __user *, oset)
Linus Torvalds's avatar
Linus Torvalds committed
4112 4113
{
	old_sigset_t old_set, new_set;
4114
	sigset_t new_blocked;
Linus Torvalds's avatar
Linus Torvalds committed
4115

4116
	old_set = current->blocked.sig[0];
Linus Torvalds's avatar
Linus Torvalds committed
4117

4118 4119 4120
	if (nset) {
		if (copy_from_user(&new_set, nset, sizeof(*nset)))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
4121

4122
		new_blocked = current->blocked;
Linus Torvalds's avatar
Linus Torvalds committed
4123 4124 4125

		switch (how) {
		case SIG_BLOCK:
4126
			sigaddsetmask(&new_blocked, new_set);
Linus Torvalds's avatar
Linus Torvalds committed
4127 4128
			break;
		case SIG_UNBLOCK:
4129
			sigdelsetmask(&new_blocked, new_set);
Linus Torvalds's avatar
Linus Torvalds committed
4130 4131
			break;
		case SIG_SETMASK:
4132
			new_blocked.sig[0] = new_set;
Linus Torvalds's avatar
Linus Torvalds committed
4133
			break;
4134 4135
		default:
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
4136 4137
		}

4138
		set_current_blocked(&new_blocked);
4139 4140 4141
	}

	if (oset) {
Linus Torvalds's avatar
Linus Torvalds committed
4142
		if (copy_to_user(oset, &old_set, sizeof(*oset)))
4143
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
4144
	}
4145 4146

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
4147 4148 4149
}
#endif /* __ARCH_WANT_SYS_SIGPROCMASK */

4150
#ifndef CONFIG_ODD_RT_SIGACTION
4151 4152 4153
/**
 *  sys_rt_sigaction - alter an action taken by a process
 *  @sig: signal to be sent
4154 4155
 *  @act: new sigaction
 *  @oact: used to save the previous sigaction
4156 4157
 *  @sigsetsize: size of sigset_t type
 */
4158 4159 4160 4161
SYSCALL_DEFINE4(rt_sigaction, int, sig,
		const struct sigaction __user *, act,
		struct sigaction __user *, oact,
		size_t, sigsetsize)
Linus Torvalds's avatar
Linus Torvalds committed
4162 4163
{
	struct k_sigaction new_sa, old_sa;
4164
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
4165 4166 4167

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
4168
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
4169

4170 4171
	if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
4172 4173

	ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
4174 4175
	if (ret)
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
4176

4177 4178 4179 4180
	if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
		return -EFAULT;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
4181
}
Al Viro's avatar
Al Viro committed
4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
#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)
{
	struct k_sigaction new_ka, old_ka;
#ifdef __ARCH_HAS_SA_RESTORER
	compat_uptr_t restorer;
#endif
	int ret;

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(compat_sigset_t))
		return -EINVAL;

	if (act) {
		compat_uptr_t handler;
		ret = get_user(handler, &act->sa_handler);
		new_ka.sa.sa_handler = compat_ptr(handler);
#ifdef __ARCH_HAS_SA_RESTORER
		ret |= get_user(restorer, &act->sa_restorer);
		new_ka.sa.sa_restorer = compat_ptr(restorer);
#endif
Al Viro's avatar
Al Viro committed
4206
		ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
4207
		ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
Al Viro's avatar
Al Viro committed
4208 4209 4210 4211 4212 4213 4214 4215
		if (ret)
			return -EFAULT;
	}

	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
	if (!ret && oact) {
		ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), 
			       &oact->sa_handler);
4216 4217
		ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
					 sizeof(oact->sa_mask));
4218
		ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
Al Viro's avatar
Al Viro committed
4219 4220 4221 4222 4223 4224 4225 4226
#ifdef __ARCH_HAS_SA_RESTORER
		ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
				&oact->sa_restorer);
#endif
	}
	return ret;
}
#endif
4227
#endif /* !CONFIG_ODD_RT_SIGACTION */
Linus Torvalds's avatar
Linus Torvalds committed
4228

4229 4230 4231 4232 4233 4234 4235 4236 4237 4238
#ifdef CONFIG_OLD_SIGACTION
SYSCALL_DEFINE3(sigaction, int, sig,
		const struct old_sigaction __user *, act,
	        struct old_sigaction __user *, oact)
{
	struct k_sigaction new_ka, old_ka;
	int ret;

	if (act) {
		old_sigset_t mask;
4239
		if (!access_ok(act, sizeof(*act)) ||
4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253
		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
		    __get_user(mask, &act->sa_mask))
			return -EFAULT;
#ifdef __ARCH_HAS_KA_RESTORER
		new_ka.ka_restorer = NULL;
#endif
		siginitset(&new_ka.sa.sa_mask, mask);
	}

	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);

	if (!ret && oact) {
4254
		if (!access_ok(oact, sizeof(*oact)) ||
4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275
		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
			return -EFAULT;
	}

	return ret;
}
#endif
#ifdef CONFIG_COMPAT_OLD_SIGACTION
COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
		const struct compat_old_sigaction __user *, act,
	        struct compat_old_sigaction __user *, oact)
{
	struct k_sigaction new_ka, old_ka;
	int ret;
	compat_old_sigset_t mask;
	compat_uptr_t handler, restorer;

	if (act) {
4276
		if (!access_ok(act, sizeof(*act)) ||
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293
		    __get_user(handler, &act->sa_handler) ||
		    __get_user(restorer, &act->sa_restorer) ||
		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
		    __get_user(mask, &act->sa_mask))
			return -EFAULT;

#ifdef __ARCH_HAS_KA_RESTORER
		new_ka.ka_restorer = NULL;
#endif
		new_ka.sa.sa_handler = compat_ptr(handler);
		new_ka.sa.sa_restorer = compat_ptr(restorer);
		siginitset(&new_ka.sa.sa_mask, mask);
	}

	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);

	if (!ret && oact) {
4294
		if (!access_ok(oact, sizeof(*oact)) ||
4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305
		    __put_user(ptr_to_compat(old_ka.sa.sa_handler),
			       &oact->sa_handler) ||
		    __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
			       &oact->sa_restorer) ||
		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
			return -EFAULT;
	}
	return ret;
}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4306

4307
#ifdef CONFIG_SGETMASK_SYSCALL
Linus Torvalds's avatar
Linus Torvalds committed
4308 4309 4310 4311

/*
 * For backwards compatibility.  Functionality superseded by sigprocmask.
 */
4312
SYSCALL_DEFINE0(sgetmask)
Linus Torvalds's avatar
Linus Torvalds committed
4313 4314 4315 4316 4317
{
	/* SMP safe */
	return current->blocked.sig[0];
}

4318
SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds's avatar
Linus Torvalds committed
4319
{
4320 4321
	int old = current->blocked.sig[0];
	sigset_t newset;
Linus Torvalds's avatar
Linus Torvalds committed
4322

4323
	siginitset(&newset, newmask);
4324
	set_current_blocked(&newset);
Linus Torvalds's avatar
Linus Torvalds committed
4325 4326 4327

	return old;
}
4328
#endif /* CONFIG_SGETMASK_SYSCALL */
Linus Torvalds's avatar
Linus Torvalds committed
4329 4330 4331 4332 4333

#ifdef __ARCH_WANT_SYS_SIGNAL
/*
 * For backwards compatibility.  Functionality superseded by sigaction.
 */
4334
SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds's avatar
Linus Torvalds committed
4335 4336 4337 4338 4339 4340
{
	struct k_sigaction new_sa, old_sa;
	int ret;

	new_sa.sa.sa_handler = handler;
	new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
4341
	sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds's avatar
Linus Torvalds committed
4342 4343 4344 4345 4346 4347 4348 4349 4350

	ret = do_sigaction(sig, &new_sa, &old_sa);

	return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
}
#endif /* __ARCH_WANT_SYS_SIGNAL */

#ifdef __ARCH_WANT_SYS_PAUSE

4351
SYSCALL_DEFINE0(pause)
Linus Torvalds's avatar
Linus Torvalds committed
4352
{
4353
	while (!signal_pending(current)) {
4354
		__set_current_state(TASK_INTERRUPTIBLE);
4355 4356
		schedule();
	}
Linus Torvalds's avatar
Linus Torvalds committed
4357 4358 4359 4360 4361
	return -ERESTARTNOHAND;
}

#endif

4362
static int sigsuspend(sigset_t *set)
Al Viro's avatar
Al Viro committed
4363 4364 4365 4366
{
	current->saved_sigmask = current->blocked;
	set_current_blocked(set);

4367 4368 4369 4370
	while (!signal_pending(current)) {
		__set_current_state(TASK_INTERRUPTIBLE);
		schedule();
	}
Al Viro's avatar
Al Viro committed
4371 4372 4373 4374
	set_restore_sigmask();
	return -ERESTARTNOHAND;
}

4375 4376 4377 4378 4379 4380
/**
 *  sys_rt_sigsuspend - replace the signal mask for a value with the
 *	@unewset value until a signal is received
 *  @unewset: new signal mask value
 *  @sigsetsize: size of sigset_t type
 */
4381
SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
4382 4383 4384 4385 4386 4387 4388 4389 4390
{
	sigset_t newset;

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

	if (copy_from_user(&newset, unewset, sizeof(newset)))
		return -EFAULT;
Al Viro's avatar
Al Viro committed
4391
	return sigsuspend(&newset);
4392
}
Al Viro's avatar
Al Viro committed
4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
 
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
{
	sigset_t newset;

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
		return -EINVAL;

Al Viro's avatar
Al Viro committed
4403
	if (get_compat_sigset(&newset, unewset))
Al Viro's avatar
Al Viro committed
4404 4405 4406 4407
		return -EFAULT;
	return sigsuspend(&newset);
}
#endif
4408

4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424
#ifdef CONFIG_OLD_SIGSUSPEND
SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
{
	sigset_t blocked;
	siginitset(&blocked, mask);
	return sigsuspend(&blocked);
}
#endif
#ifdef CONFIG_OLD_SIGSUSPEND3
SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
{
	sigset_t blocked;
	siginitset(&blocked, mask);
	return sigsuspend(&blocked);
}
#endif
4425

4426
__weak const char *arch_vma_name(struct vm_area_struct *vma)
4427 4428 4429 4430
{
	return NULL;
}

4431
static inline void siginfo_buildtime_checks(void)
Linus Torvalds's avatar
Linus Torvalds committed
4432
{
4433
	BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
4434

4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481
	/* Verify the offsets in the two siginfos match */
#define CHECK_OFFSET(field) \
	BUILD_BUG_ON(offsetof(siginfo_t, field) != offsetof(kernel_siginfo_t, field))

	/* kill */
	CHECK_OFFSET(si_pid);
	CHECK_OFFSET(si_uid);

	/* timer */
	CHECK_OFFSET(si_tid);
	CHECK_OFFSET(si_overrun);
	CHECK_OFFSET(si_value);

	/* rt */
	CHECK_OFFSET(si_pid);
	CHECK_OFFSET(si_uid);
	CHECK_OFFSET(si_value);

	/* sigchld */
	CHECK_OFFSET(si_pid);
	CHECK_OFFSET(si_uid);
	CHECK_OFFSET(si_status);
	CHECK_OFFSET(si_utime);
	CHECK_OFFSET(si_stime);

	/* sigfault */
	CHECK_OFFSET(si_addr);
	CHECK_OFFSET(si_addr_lsb);
	CHECK_OFFSET(si_lower);
	CHECK_OFFSET(si_upper);
	CHECK_OFFSET(si_pkey);

	/* sigpoll */
	CHECK_OFFSET(si_band);
	CHECK_OFFSET(si_fd);

	/* sigsys */
	CHECK_OFFSET(si_call_addr);
	CHECK_OFFSET(si_syscall);
	CHECK_OFFSET(si_arch);
#undef CHECK_OFFSET
}

void __init signals_init(void)
{
	siginfo_buildtime_checks();

4482
	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds's avatar
Linus Torvalds committed
4483
}
4484 4485 4486 4487

#ifdef CONFIG_KGDB_KDB
#include <linux/kdb.h>
/*
4488
 * kdb_send_sig - Allows kdb to send signals without exposing
4489 4490 4491 4492
 * signal internals.  This function checks if the required locks are
 * available before calling the main signal code, to avoid kdb
 * deadlocks.
 */
4493
void kdb_send_sig(struct task_struct *t, int sig)
4494 4495
{
	static struct task_struct *kdb_prev_t;
4496
	int new_t, ret;
4497 4498 4499 4500 4501 4502 4503 4504 4505
	if (!spin_trylock(&t->sighand->siglock)) {
		kdb_printf("Can't do kill command now.\n"
			   "The sigmask lock is held somewhere else in "
			   "kernel, try again later\n");
		return;
	}
	new_t = kdb_prev_t != t;
	kdb_prev_t = t;
	if (t->state != TASK_RUNNING && new_t) {
4506
		spin_unlock(&t->sighand->siglock);
4507 4508 4509 4510 4511 4512 4513 4514
		kdb_printf("Process is not RUNNING, sending a signal from "
			   "kdb risks deadlock\n"
			   "on the run queue locks. "
			   "The signal has _not_ been sent.\n"
			   "Reissue the kill command if you want to risk "
			   "the deadlock.\n");
		return;
	}
4515
	ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
4516 4517
	spin_unlock(&t->sighand->siglock);
	if (ret)
4518 4519 4520 4521 4522 4523
		kdb_printf("Fail to deliver Signal %d to process %d.\n",
			   sig, t->pid);
	else
		kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
}
#endif	/* CONFIG_KGDB_KDB */