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