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