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