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