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