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