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