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