blob: 224ed526fab4c569cdee510ad72c8d71e859e95e [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;
b.liu35419042025-06-24 15:25:00 +08001283 // Change by liubin for not reboot when App dump.
1284#if 1
1285 return;
1286#else
b.liue9582032025-04-17 19:18:16 +08001287 if (print_fatal_signals < 15)
1288 return;
b.liu35419042025-06-24 15:25:00 +08001289#endif
b.liue9582032025-04-17 19:18:16 +08001290 for (i = 0; i < K_SIG_GUIDS_MAX; i++) {
1291 if (k_sig_uid[i] < 0)
1292 break;
1293 if (k_sig_uid[i] == ((int)current_uid().val))
1294 goto call_panic;
1295 }
1296 for (i = 0; i < K_SIG_GUIDS_MAX; i++) {
1297 if (k_sig_uid[i] < 0)
1298 break;
1299 if (k_sig_gid[i] == ((int)current_gid().val))
1300 goto call_panic;
1301 }
1302 return;
1303
1304call_panic:
1305 panic("Fatal sig=%d on \'%s\' pid=%u\n",
1306 signr, current->comm, current->pid);
1307 /* should never return */
1308}
1309
1310int k_signal_panic_guid_set(int set_uid, int *sig_guid_array, int size)
1311{
1312 int i;
1313 int *p = (set_uid) ? k_sig_uid : k_sig_gid;
1314
1315 if ((size <= 0) || (size > K_SIG_GUIDS_MAX))
1316 return -K_SIG_GUIDS_MAX;
1317 for (i = 0; i < size; i++)
1318 *p++ = *sig_guid_array++;
1319 *p = -1; /* terminator */
1320 return size;
1321}
1322#endif
1323
1324static void print_fatal_signal(int signr)
1325{
1326 char *p = NULL;
1327 unsigned int eehp = 0;
1328
1329 struct pt_regs *regs = signal_pt_regs();
1330 printk(KERN_INFO "potentially unexpected fatal signal %d"
1331 ", \"%s\" pid=%u\n", signr, current->comm, current->pid);
1332
1333 p = strstr(saved_command_line, " eehP=");
1334 if (p && sscanf(p, " eehP=%x", &eehp))
1335 {
1336 if(eehp == 4)
1337 {
1338 printk(KERN_INFO "Get eehP[%u], msleep 12 second... \n", eehp);
1339 msleep(1000*12);
1340 }
1341 }
1342
1343 if (print_fatal_signals == 16) {
1344 preempt_disable();
1345 show_regs(regs);
1346 preempt_enable();
1347 if (in_atomic())
1348 return;
1349#ifdef PANIC_ON_FATAL_SIGNALS
1350 while (print_fatal_signals == 16)
1351 msleep(999);
1352 /* continue according to new print_fatal_signals */
1353#endif
1354 }
1355
1356 if (print_fatal_signals == 2)
1357 return; /* short report only */
1358
1359#if defined(__i386__) && !defined(__arch_um__)
1360 printk(KERN_INFO "code at %08lx: ", regs->ip);
1361 {
1362 int i;
1363 for (i = 0; i < 16; i++) {
1364 unsigned char insn;
1365
1366 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1367 break;
1368 printk(KERN_CONT "%02x ", insn);
1369 }
1370 }
1371 printk(KERN_CONT "\n");
1372#endif
1373 preempt_disable();
1374 show_regs(regs);
1375#ifdef PANIC_ON_FATAL_SIGNALS
1376 panic_on_fatal_signals(signr);
1377#endif
1378 preempt_enable();
1379}
1380
1381static int __init setup_print_fatal_signals(char *str)
1382{
1383 get_option (&str, &print_fatal_signals);
1384
1385 return 1;
1386}
1387
1388__setup("print-fatal-signals=", setup_print_fatal_signals);
1389
1390int
1391__group_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
1392{
1393 return send_signal(sig, info, p, PIDTYPE_TGID);
1394}
1395
1396int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p,
1397 enum pid_type type)
1398{
1399 unsigned long flags;
1400 int ret = -ESRCH;
1401
1402 if (lock_task_sighand(p, &flags)) {
1403 ret = send_signal(sig, info, p, type);
1404 unlock_task_sighand(p, &flags);
1405 }
1406
1407 return ret;
1408}
1409
1410/*
1411 * Force a signal that the process can't ignore: if necessary
1412 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1413 *
1414 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1415 * since we do not want to have a signal handler that was blocked
1416 * be invoked when user space had explicitly blocked it.
1417 *
1418 * We don't want to have recursive SIGSEGV's etc, for example,
1419 * that is why we also clear SIGNAL_UNKILLABLE.
1420 */
1421static int
1422force_sig_info_to_task(struct kernel_siginfo *info, struct task_struct *t)
1423{
1424 unsigned long int flags;
1425 int ret, blocked, ignored;
1426 struct k_sigaction *action;
1427 int sig = info->si_signo;
1428
1429 spin_lock_irqsave(&t->sighand->siglock, flags);
1430 action = &t->sighand->action[sig-1];
1431 ignored = action->sa.sa_handler == SIG_IGN;
1432 blocked = sigismember(&t->blocked, sig);
1433 if (blocked || ignored) {
1434 action->sa.sa_handler = SIG_DFL;
1435 if (blocked) {
1436 sigdelset(&t->blocked, sig);
1437 recalc_sigpending_and_wake(t);
1438 }
1439 }
1440 /*
1441 * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
1442 * debugging to leave init killable.
1443 */
1444 if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
1445 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1446 ret = send_signal(sig, info, t, PIDTYPE_PID);
1447 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1448
1449 return ret;
1450}
1451
1452int force_sig_info(struct kernel_siginfo *info)
1453{
1454 return force_sig_info_to_task(info, current);
1455}
1456
1457/*
1458 * Nuke all other threads in the group.
1459 */
1460int zap_other_threads(struct task_struct *p)
1461{
1462 struct task_struct *t = p;
1463 int count = 0;
1464
1465 p->signal->group_stop_count = 0;
1466
1467 while_each_thread(p, t) {
1468 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1469 count++;
1470
1471 /* Don't bother with already dead threads */
1472 if (t->exit_state)
1473 continue;
1474 sigaddset(&t->pending.signal, SIGKILL);
1475 signal_wake_up(t, 1);
1476 }
1477
1478 return count;
1479}
1480
1481struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1482 unsigned long *flags)
1483{
1484 struct sighand_struct *sighand;
1485
1486 rcu_read_lock();
1487 for (;;) {
1488 sighand = rcu_dereference(tsk->sighand);
1489 if (unlikely(sighand == NULL))
1490 break;
1491
1492 /*
1493 * This sighand can be already freed and even reused, but
1494 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
1495 * initializes ->siglock: this slab can't go away, it has
1496 * the same object type, ->siglock can't be reinitialized.
1497 *
1498 * We need to ensure that tsk->sighand is still the same
1499 * after we take the lock, we can race with de_thread() or
1500 * __exit_signal(). In the latter case the next iteration
1501 * must see ->sighand == NULL.
1502 */
1503 spin_lock_irqsave(&sighand->siglock, *flags);
1504 if (likely(sighand == tsk->sighand))
1505 break;
1506 spin_unlock_irqrestore(&sighand->siglock, *flags);
1507 }
1508 rcu_read_unlock();
1509
1510 return sighand;
1511}
1512EXPORT_SYMBOL_GPL(__lock_task_sighand);
1513
1514/*
1515 * send signal info to all the members of a group
1516 */
1517int group_send_sig_info(int sig, struct kernel_siginfo *info,
1518 struct task_struct *p, enum pid_type type)
1519{
1520 int ret;
1521
1522 rcu_read_lock();
1523 ret = check_kill_permission(sig, info, p);
1524 rcu_read_unlock();
1525
1526 if (!ret && sig)
1527 ret = do_send_sig_info(sig, info, p, type);
1528
1529 return ret;
1530}
1531
1532/*
1533 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1534 * control characters do (^C, ^Z etc)
1535 * - the caller must hold at least a readlock on tasklist_lock
1536 */
1537int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp)
1538{
1539 struct task_struct *p = NULL;
1540 int retval, success;
1541
1542 success = 0;
1543 retval = -ESRCH;
1544 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1545 int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
1546 success |= !err;
1547 retval = err;
1548 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1549 return success ? 0 : retval;
1550}
1551
1552int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid)
1553{
1554 int error = -ESRCH;
1555 struct task_struct *p;
1556
1557 for (;;) {
1558 rcu_read_lock();
1559 p = pid_task(pid, PIDTYPE_PID);
1560 if (p)
1561 error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
1562 rcu_read_unlock();
1563 if (likely(!p || error != -ESRCH))
1564 return error;
1565
1566 /*
1567 * The task was unhashed in between, try again. If it
1568 * is dead, pid_task() will return NULL, if we race with
1569 * de_thread() it will find the new leader.
1570 */
1571 }
1572}
1573
1574static int kill_proc_info(int sig, struct kernel_siginfo *info, pid_t pid)
1575{
1576 int error;
1577 rcu_read_lock();
1578 error = kill_pid_info(sig, info, find_vpid(pid));
1579 rcu_read_unlock();
1580 return error;
1581}
1582
1583static inline bool kill_as_cred_perm(const struct cred *cred,
1584 struct task_struct *target)
1585{
1586 const struct cred *pcred = __task_cred(target);
1587
1588 return uid_eq(cred->euid, pcred->suid) ||
1589 uid_eq(cred->euid, pcred->uid) ||
1590 uid_eq(cred->uid, pcred->suid) ||
1591 uid_eq(cred->uid, pcred->uid);
1592}
1593
1594/*
1595 * The usb asyncio usage of siginfo is wrong. The glibc support
1596 * for asyncio which uses SI_ASYNCIO assumes the layout is SIL_RT.
1597 * AKA after the generic fields:
1598 * kernel_pid_t si_pid;
1599 * kernel_uid32_t si_uid;
1600 * sigval_t si_value;
1601 *
1602 * Unfortunately when usb generates SI_ASYNCIO it assumes the layout
1603 * after the generic fields is:
1604 * void __user *si_addr;
1605 *
1606 * This is a practical problem when there is a 64bit big endian kernel
1607 * and a 32bit userspace. As the 32bit address will encoded in the low
1608 * 32bits of the pointer. Those low 32bits will be stored at higher
1609 * address than appear in a 32 bit pointer. So userspace will not
1610 * see the address it was expecting for it's completions.
1611 *
1612 * There is nothing in the encoding that can allow
1613 * copy_siginfo_to_user32 to detect this confusion of formats, so
1614 * handle this by requiring the caller of kill_pid_usb_asyncio to
1615 * notice when this situration takes place and to store the 32bit
1616 * pointer in sival_int, instead of sival_addr of the sigval_t addr
1617 * parameter.
1618 */
1619int kill_pid_usb_asyncio(int sig, int errno, sigval_t addr,
1620 struct pid *pid, const struct cred *cred)
1621{
1622 struct kernel_siginfo info;
1623 struct task_struct *p;
1624 unsigned long flags;
1625 int ret = -EINVAL;
1626
1627 if (!valid_signal(sig))
1628 return ret;
1629
1630 clear_siginfo(&info);
1631 info.si_signo = sig;
1632 info.si_errno = errno;
1633 info.si_code = SI_ASYNCIO;
1634 *((sigval_t *)&info.si_pid) = addr;
1635
1636 rcu_read_lock();
1637 p = pid_task(pid, PIDTYPE_PID);
1638 if (!p) {
1639 ret = -ESRCH;
1640 goto out_unlock;
1641 }
1642 if (!kill_as_cred_perm(cred, p)) {
1643 ret = -EPERM;
1644 goto out_unlock;
1645 }
1646 ret = security_task_kill(p, &info, sig, cred);
1647 if (ret)
1648 goto out_unlock;
1649
1650 if (sig) {
1651 if (lock_task_sighand(p, &flags)) {
1652 ret = __send_signal(sig, &info, p, PIDTYPE_TGID, false);
1653 unlock_task_sighand(p, &flags);
1654 } else
1655 ret = -ESRCH;
1656 }
1657out_unlock:
1658 rcu_read_unlock();
1659 return ret;
1660}
1661EXPORT_SYMBOL_GPL(kill_pid_usb_asyncio);
1662
1663/*
1664 * kill_something_info() interprets pid in interesting ways just like kill(2).
1665 *
1666 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1667 * is probably wrong. Should make it like BSD or SYSV.
1668 */
1669
1670static int kill_something_info(int sig, struct kernel_siginfo *info, pid_t pid)
1671{
1672 int ret;
1673
1674 if (pid > 0) {
1675 rcu_read_lock();
1676 ret = kill_pid_info(sig, info, find_vpid(pid));
1677 rcu_read_unlock();
1678 return ret;
1679 }
1680
1681 /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
1682 if (pid == INT_MIN)
1683 return -ESRCH;
1684
1685 read_lock(&tasklist_lock);
1686 if (pid != -1) {
1687 ret = __kill_pgrp_info(sig, info,
1688 pid ? find_vpid(-pid) : task_pgrp(current));
1689 } else {
1690 int retval = 0, count = 0;
1691 struct task_struct * p;
1692
1693 for_each_process(p) {
1694 if (task_pid_vnr(p) > 1 &&
1695 !same_thread_group(p, current)) {
1696 int err = group_send_sig_info(sig, info, p,
1697 PIDTYPE_MAX);
1698 ++count;
1699 if (err != -EPERM)
1700 retval = err;
1701 }
1702 }
1703 ret = count ? retval : -ESRCH;
1704 }
1705 read_unlock(&tasklist_lock);
1706
1707 return ret;
1708}
1709
1710/*
1711 * These are for backward compatibility with the rest of the kernel source.
1712 */
1713
1714int send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
1715{
1716 /*
1717 * Make sure legacy kernel users don't send in bad values
1718 * (normal paths check this in check_kill_permission).
1719 */
1720 if (!valid_signal(sig))
1721 return -EINVAL;
1722
1723 return do_send_sig_info(sig, info, p, PIDTYPE_PID);
1724}
1725EXPORT_SYMBOL(send_sig_info);
1726
1727#define __si_special(priv) \
1728 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1729
1730int
1731send_sig(int sig, struct task_struct *p, int priv)
1732{
1733 return send_sig_info(sig, __si_special(priv), p);
1734}
1735EXPORT_SYMBOL(send_sig);
1736
1737void force_sig(int sig)
1738{
1739 struct kernel_siginfo info;
1740
1741 clear_siginfo(&info);
1742 info.si_signo = sig;
1743 info.si_errno = 0;
1744 info.si_code = SI_KERNEL;
1745 info.si_pid = 0;
1746 info.si_uid = 0;
1747 force_sig_info(&info);
1748}
1749EXPORT_SYMBOL(force_sig);
1750
1751/*
1752 * When things go south during signal handling, we
1753 * will force a SIGSEGV. And if the signal that caused
1754 * the problem was already a SIGSEGV, we'll want to
1755 * make sure we don't even try to deliver the signal..
1756 */
1757void force_sigsegv(int sig)
1758{
1759 struct task_struct *p = current;
1760
1761 if (sig == SIGSEGV) {
1762 unsigned long flags;
1763 spin_lock_irqsave(&p->sighand->siglock, flags);
1764 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1765 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1766 }
1767 force_sig(SIGSEGV);
1768}
1769
1770int force_sig_fault_to_task(int sig, int code, void __user *addr
1771 ___ARCH_SI_TRAPNO(int trapno)
1772 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1773 , struct task_struct *t)
1774{
1775 struct kernel_siginfo info;
1776
1777 clear_siginfo(&info);
1778 info.si_signo = sig;
1779 info.si_errno = 0;
1780 info.si_code = code;
1781 info.si_addr = addr;
1782#ifdef __ARCH_SI_TRAPNO
1783 info.si_trapno = trapno;
1784#endif
1785#ifdef __ia64__
1786 info.si_imm = imm;
1787 info.si_flags = flags;
1788 info.si_isr = isr;
1789#endif
1790 return force_sig_info_to_task(&info, t);
1791}
1792
1793int force_sig_fault(int sig, int code, void __user *addr
1794 ___ARCH_SI_TRAPNO(int trapno)
1795 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr))
1796{
1797 return force_sig_fault_to_task(sig, code, addr
1798 ___ARCH_SI_TRAPNO(trapno)
1799 ___ARCH_SI_IA64(imm, flags, isr), current);
1800}
1801
1802int send_sig_fault(int sig, int code, void __user *addr
1803 ___ARCH_SI_TRAPNO(int trapno)
1804 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1805 , struct task_struct *t)
1806{
1807 struct kernel_siginfo info;
1808
1809 clear_siginfo(&info);
1810 info.si_signo = sig;
1811 info.si_errno = 0;
1812 info.si_code = code;
1813 info.si_addr = addr;
1814#ifdef __ARCH_SI_TRAPNO
1815 info.si_trapno = trapno;
1816#endif
1817#ifdef __ia64__
1818 info.si_imm = imm;
1819 info.si_flags = flags;
1820 info.si_isr = isr;
1821#endif
1822 return send_sig_info(info.si_signo, &info, t);
1823}
1824
1825int force_sig_mceerr(int code, void __user *addr, short lsb)
1826{
1827 struct kernel_siginfo info;
1828
1829 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1830 clear_siginfo(&info);
1831 info.si_signo = SIGBUS;
1832 info.si_errno = 0;
1833 info.si_code = code;
1834 info.si_addr = addr;
1835 info.si_addr_lsb = lsb;
1836 return force_sig_info(&info);
1837}
1838
1839int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1840{
1841 struct kernel_siginfo info;
1842
1843 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1844 clear_siginfo(&info);
1845 info.si_signo = SIGBUS;
1846 info.si_errno = 0;
1847 info.si_code = code;
1848 info.si_addr = addr;
1849 info.si_addr_lsb = lsb;
1850 return send_sig_info(info.si_signo, &info, t);
1851}
1852EXPORT_SYMBOL(send_sig_mceerr);
1853
1854int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
1855{
1856 struct kernel_siginfo info;
1857
1858 clear_siginfo(&info);
1859 info.si_signo = SIGSEGV;
1860 info.si_errno = 0;
1861 info.si_code = SEGV_BNDERR;
1862 info.si_addr = addr;
1863 info.si_lower = lower;
1864 info.si_upper = upper;
1865 return force_sig_info(&info);
1866}
1867
1868#ifdef SEGV_PKUERR
1869int force_sig_pkuerr(void __user *addr, u32 pkey)
1870{
1871 struct kernel_siginfo info;
1872
1873 clear_siginfo(&info);
1874 info.si_signo = SIGSEGV;
1875 info.si_errno = 0;
1876 info.si_code = SEGV_PKUERR;
1877 info.si_addr = addr;
1878 info.si_pkey = pkey;
1879 return force_sig_info(&info);
1880}
1881#endif
1882
1883/* For the crazy architectures that include trap information in
1884 * the errno field, instead of an actual errno value.
1885 */
1886int force_sig_ptrace_errno_trap(int errno, void __user *addr)
1887{
1888 struct kernel_siginfo info;
1889
1890 clear_siginfo(&info);
1891 info.si_signo = SIGTRAP;
1892 info.si_errno = errno;
1893 info.si_code = TRAP_HWBKPT;
1894 info.si_addr = addr;
1895 return force_sig_info(&info);
1896}
1897
1898int kill_pgrp(struct pid *pid, int sig, int priv)
1899{
1900 int ret;
1901
1902 read_lock(&tasklist_lock);
1903 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1904 read_unlock(&tasklist_lock);
1905
1906 return ret;
1907}
1908EXPORT_SYMBOL(kill_pgrp);
1909
1910int kill_pid(struct pid *pid, int sig, int priv)
1911{
1912 return kill_pid_info(sig, __si_special(priv), pid);
1913}
1914EXPORT_SYMBOL(kill_pid);
1915
1916/*
1917 * These functions support sending signals using preallocated sigqueue
1918 * structures. This is needed "because realtime applications cannot
1919 * afford to lose notifications of asynchronous events, like timer
1920 * expirations or I/O completions". In the case of POSIX Timers
1921 * we allocate the sigqueue structure from the timer_create. If this
1922 * allocation fails we are able to report the failure to the application
1923 * with an EAGAIN error.
1924 */
1925struct sigqueue *sigqueue_alloc(void)
1926{
1927 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1928
1929 if (q)
1930 q->flags |= SIGQUEUE_PREALLOC;
1931
1932 return q;
1933}
1934
1935void sigqueue_free(struct sigqueue *q)
1936{
1937 spinlock_t *lock = &current->sighand->siglock;
1938 unsigned long flags;
1939
1940 if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC)))
1941 return;
1942 /*
1943 * We must hold ->siglock while testing q->list
1944 * to serialize with collect_signal() or with
1945 * __exit_signal()->flush_sigqueue().
1946 */
1947 spin_lock_irqsave(lock, flags);
1948 q->flags &= ~SIGQUEUE_PREALLOC;
1949 /*
1950 * If it is queued it will be freed when dequeued,
1951 * like the "regular" sigqueue.
1952 */
1953 if (!list_empty(&q->list))
1954 q = NULL;
1955 spin_unlock_irqrestore(lock, flags);
1956
1957 if (q)
1958 __sigqueue_free(q);
1959}
1960
1961int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
1962{
1963 int sig = q->info.si_signo;
1964 struct sigpending *pending;
1965 struct task_struct *t;
1966 unsigned long flags;
1967 int ret, result;
1968
1969 if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC)))
1970 return 0;
1971 if (WARN_ON_ONCE(q->info.si_code != SI_TIMER))
1972 return 0;
1973
1974 ret = -1;
1975 rcu_read_lock();
1976 t = pid_task(pid, type);
1977 if (!t || !likely(lock_task_sighand(t, &flags)))
1978 goto ret;
1979
1980 ret = 1; /* the signal is ignored */
1981 result = TRACE_SIGNAL_IGNORED;
1982 if (!prepare_signal(sig, t, false))
1983 goto out;
1984
1985 ret = 0;
1986 if (unlikely(!list_empty(&q->list))) {
1987 /*
1988 * If an SI_TIMER entry is already queue just increment
1989 * the overrun count.
1990 */
1991 q->info.si_overrun++;
1992 result = TRACE_SIGNAL_ALREADY_PENDING;
1993 goto out;
1994 }
1995 q->info.si_overrun = 0;
1996
1997 signalfd_notify(t, sig);
1998 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1999 list_add_tail(&q->list, &pending->list);
2000 sigaddset(&pending->signal, sig);
2001 complete_signal(sig, t, type);
2002 result = TRACE_SIGNAL_DELIVERED;
2003out:
2004 trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
2005 unlock_task_sighand(t, &flags);
2006ret:
2007 rcu_read_unlock();
2008 return ret;
2009}
2010
2011static void do_notify_pidfd(struct task_struct *task)
2012{
2013 struct pid *pid;
2014
2015 WARN_ON(task->exit_state == 0);
2016 pid = task_pid(task);
2017 wake_up_all(&pid->wait_pidfd);
2018}
2019
2020/*
2021 * Let a parent know about the death of a child.
2022 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
2023 *
2024 * Returns true if our parent ignored us and so we've switched to
2025 * self-reaping.
2026 */
2027bool do_notify_parent(struct task_struct *tsk, int sig)
2028{
2029 struct kernel_siginfo info;
2030 unsigned long flags;
2031 struct sighand_struct *psig;
2032 bool autoreap = false;
2033 u64 utime, stime;
2034
2035 WARN_ON_ONCE(sig == -1);
2036
2037 /* do_notify_parent_cldstop should have been called instead. */
2038 WARN_ON_ONCE(task_is_stopped_or_traced(tsk));
2039
2040 WARN_ON_ONCE(!tsk->ptrace &&
2041 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
2042
2043 /* Wake up all pidfd waiters */
2044 do_notify_pidfd(tsk);
2045
2046 if (sig != SIGCHLD) {
2047 /*
2048 * This is only possible if parent == real_parent.
2049 * Check if it has changed security domain.
2050 */
2051 if (tsk->parent_exec_id != READ_ONCE(tsk->parent->self_exec_id))
2052 sig = SIGCHLD;
2053 }
2054
2055 clear_siginfo(&info);
2056 info.si_signo = sig;
2057 info.si_errno = 0;
2058 /*
2059 * We are under tasklist_lock here so our parent is tied to
2060 * us and cannot change.
2061 *
2062 * task_active_pid_ns will always return the same pid namespace
2063 * until a task passes through release_task.
2064 *
2065 * write_lock() currently calls preempt_disable() which is the
2066 * same as rcu_read_lock(), but according to Oleg, this is not
2067 * correct to rely on this
2068 */
2069 rcu_read_lock();
2070 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
2071 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
2072 task_uid(tsk));
2073 rcu_read_unlock();
2074
2075 task_cputime(tsk, &utime, &stime);
2076 info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
2077 info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
2078
2079 info.si_status = tsk->exit_code & 0x7f;
2080 if (tsk->exit_code & 0x80)
2081 info.si_code = CLD_DUMPED;
2082 else if (tsk->exit_code & 0x7f)
2083 info.si_code = CLD_KILLED;
2084 else {
2085 info.si_code = CLD_EXITED;
2086 info.si_status = tsk->exit_code >> 8;
2087 }
2088
2089 psig = tsk->parent->sighand;
2090 spin_lock_irqsave(&psig->siglock, flags);
2091 if (!tsk->ptrace && sig == SIGCHLD &&
2092 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
2093 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
2094 /*
2095 * We are exiting and our parent doesn't care. POSIX.1
2096 * defines special semantics for setting SIGCHLD to SIG_IGN
2097 * or setting the SA_NOCLDWAIT flag: we should be reaped
2098 * automatically and not left for our parent's wait4 call.
2099 * Rather than having the parent do it as a magic kind of
2100 * signal handler, we just set this to tell do_exit that we
2101 * can be cleaned up without becoming a zombie. Note that
2102 * we still call __wake_up_parent in this case, because a
2103 * blocked sys_wait4 might now return -ECHILD.
2104 *
2105 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
2106 * is implementation-defined: we do (if you don't want
2107 * it, just use SIG_IGN instead).
2108 */
2109 autoreap = true;
2110 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
2111 sig = 0;
2112 }
2113 /*
2114 * Send with __send_signal as si_pid and si_uid are in the
2115 * parent's namespaces.
2116 */
2117 if (valid_signal(sig) && sig)
2118 __send_signal(sig, &info, tsk->parent, PIDTYPE_TGID, false);
2119 __wake_up_parent(tsk, tsk->parent);
2120 spin_unlock_irqrestore(&psig->siglock, flags);
2121
2122 return autoreap;
2123}
2124
2125/**
2126 * do_notify_parent_cldstop - notify parent of stopped/continued state change
2127 * @tsk: task reporting the state change
2128 * @for_ptracer: the notification is for ptracer
2129 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
2130 *
2131 * Notify @tsk's parent that the stopped/continued state has changed. If
2132 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
2133 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
2134 *
2135 * CONTEXT:
2136 * Must be called with tasklist_lock at least read locked.
2137 */
2138static void do_notify_parent_cldstop(struct task_struct *tsk,
2139 bool for_ptracer, int why)
2140{
2141 struct kernel_siginfo info;
2142 unsigned long flags;
2143 struct task_struct *parent;
2144 struct sighand_struct *sighand;
2145 u64 utime, stime;
2146
2147 if (for_ptracer) {
2148 parent = tsk->parent;
2149 } else {
2150 tsk = tsk->group_leader;
2151 parent = tsk->real_parent;
2152 }
2153
2154 clear_siginfo(&info);
2155 info.si_signo = SIGCHLD;
2156 info.si_errno = 0;
2157 /*
2158 * see comment in do_notify_parent() about the following 4 lines
2159 */
2160 rcu_read_lock();
2161 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
2162 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
2163 rcu_read_unlock();
2164
2165 task_cputime(tsk, &utime, &stime);
2166 info.si_utime = nsec_to_clock_t(utime);
2167 info.si_stime = nsec_to_clock_t(stime);
2168
2169 info.si_code = why;
2170 switch (why) {
2171 case CLD_CONTINUED:
2172 info.si_status = SIGCONT;
2173 break;
2174 case CLD_STOPPED:
2175 info.si_status = tsk->signal->group_exit_code & 0x7f;
2176 break;
2177 case CLD_TRAPPED:
2178 info.si_status = tsk->exit_code & 0x7f;
2179 break;
2180 default:
2181 BUG();
2182 }
2183
2184 sighand = parent->sighand;
2185 spin_lock_irqsave(&sighand->siglock, flags);
2186 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
2187 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
2188 __group_send_sig_info(SIGCHLD, &info, parent);
2189 /*
2190 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
2191 */
2192 __wake_up_parent(tsk, parent);
2193 spin_unlock_irqrestore(&sighand->siglock, flags);
2194}
2195
2196static inline bool may_ptrace_stop(void)
2197{
2198 if (!likely(current->ptrace))
2199 return false;
2200 /*
2201 * Are we in the middle of do_coredump?
2202 * If so and our tracer is also part of the coredump stopping
2203 * is a deadlock situation, and pointless because our tracer
2204 * is dead so don't allow us to stop.
2205 * If SIGKILL was already sent before the caller unlocked
2206 * ->siglock we must see ->core_state != NULL. Otherwise it
2207 * is safe to enter schedule().
2208 *
2209 * This is almost outdated, a task with the pending SIGKILL can't
2210 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
2211 * after SIGKILL was already dequeued.
2212 */
2213 if (unlikely(current->mm->core_state) &&
2214 unlikely(current->mm == current->parent->mm))
2215 return false;
2216
2217 return true;
2218}
2219
2220
2221/*
2222 * This must be called with current->sighand->siglock held.
2223 *
2224 * This should be the path for all ptrace stops.
2225 * We always set current->last_siginfo while stopped here.
2226 * That makes it a way to test a stopped process for
2227 * being ptrace-stopped vs being job-control-stopped.
2228 *
2229 * If we actually decide not to stop at all because the tracer
2230 * is gone, we keep current->exit_code unless clear_code.
2231 */
2232static void ptrace_stop(int exit_code, int why, int clear_code, kernel_siginfo_t *info)
2233 __releases(&current->sighand->siglock)
2234 __acquires(&current->sighand->siglock)
2235{
2236 bool gstop_done = false;
2237
2238 if (arch_ptrace_stop_needed(exit_code, info)) {
2239 /*
2240 * The arch code has something special to do before a
2241 * ptrace stop. This is allowed to block, e.g. for faults
2242 * on user stack pages. We can't keep the siglock while
2243 * calling arch_ptrace_stop, so we must release it now.
2244 * To preserve proper semantics, we must do this before
2245 * any signal bookkeeping like checking group_stop_count.
2246 */
2247 spin_unlock_irq(&current->sighand->siglock);
2248 arch_ptrace_stop(exit_code, info);
2249 spin_lock_irq(&current->sighand->siglock);
2250 }
2251
2252 /*
2253 * schedule() will not sleep if there is a pending signal that
2254 * can awaken the task.
2255 */
2256 set_special_state(TASK_TRACED);
2257
2258 /*
2259 * We're committing to trapping. TRACED should be visible before
2260 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
2261 * Also, transition to TRACED and updates to ->jobctl should be
2262 * atomic with respect to siglock and should be done after the arch
2263 * hook as siglock is released and regrabbed across it.
2264 *
2265 * TRACER TRACEE
2266 *
2267 * ptrace_attach()
2268 * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
2269 * do_wait()
2270 * set_current_state() smp_wmb();
2271 * ptrace_do_wait()
2272 * wait_task_stopped()
2273 * task_stopped_code()
2274 * [L] task_is_traced() [S] task_clear_jobctl_trapping();
2275 */
2276 smp_wmb();
2277
2278 current->last_siginfo = info;
2279 current->exit_code = exit_code;
2280
2281 /*
2282 * If @why is CLD_STOPPED, we're trapping to participate in a group
2283 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
2284 * across siglock relocks since INTERRUPT was scheduled, PENDING
2285 * could be clear now. We act as if SIGCONT is received after
2286 * TASK_TRACED is entered - ignore it.
2287 */
2288 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
2289 gstop_done = task_participate_group_stop(current);
2290
2291 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
2292 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
2293 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
2294 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
2295
2296 /* entering a trap, clear TRAPPING */
2297 task_clear_jobctl_trapping(current);
2298
2299 spin_unlock_irq(&current->sighand->siglock);
2300 read_lock(&tasklist_lock);
2301 if (may_ptrace_stop()) {
2302 /*
2303 * Notify parents of the stop.
2304 *
2305 * While ptraced, there are two parents - the ptracer and
2306 * the real_parent of the group_leader. The ptracer should
2307 * know about every stop while the real parent is only
2308 * interested in the completion of group stop. The states
2309 * for the two don't interact with each other. Notify
2310 * separately unless they're gonna be duplicates.
2311 */
2312 do_notify_parent_cldstop(current, true, why);
2313 if (gstop_done && ptrace_reparented(current))
2314 do_notify_parent_cldstop(current, false, why);
2315
2316 /*
2317 * Don't want to allow preemption here, because
2318 * sys_ptrace() needs this task to be inactive.
2319 *
2320 * XXX: implement read_unlock_no_resched().
2321 */
2322 preempt_disable();
2323 read_unlock(&tasklist_lock);
2324 cgroup_enter_frozen();
2325 preempt_enable_no_resched();
2326 freezable_schedule();
2327 cgroup_leave_frozen(true);
2328 } else {
2329 /*
2330 * By the time we got the lock, our tracer went away.
2331 * Don't drop the lock yet, another tracer may come.
2332 *
2333 * If @gstop_done, the ptracer went away between group stop
2334 * completion and here. During detach, it would have set
2335 * JOBCTL_STOP_PENDING on us and we'll re-enter
2336 * TASK_STOPPED in do_signal_stop() on return, so notifying
2337 * the real parent of the group stop completion is enough.
2338 */
2339 if (gstop_done)
2340 do_notify_parent_cldstop(current, false, why);
2341
2342 /* tasklist protects us from ptrace_freeze_traced() */
2343 __set_current_state(TASK_RUNNING);
2344 if (clear_code)
2345 current->exit_code = 0;
2346 read_unlock(&tasklist_lock);
2347 }
2348
2349 /*
2350 * We are back. Now reacquire the siglock before touching
2351 * last_siginfo, so that we are sure to have synchronized with
2352 * any signal-sending on another CPU that wants to examine it.
2353 */
2354 spin_lock_irq(&current->sighand->siglock);
2355 current->last_siginfo = NULL;
2356
2357 /* LISTENING can be set only during STOP traps, clear it */
2358 current->jobctl &= ~JOBCTL_LISTENING;
2359
2360 /*
2361 * Queued signals ignored us while we were stopped for tracing.
2362 * So check for any that we should take before resuming user mode.
2363 * This sets TIF_SIGPENDING, but never clears it.
2364 */
2365 recalc_sigpending_tsk(current);
2366}
2367
2368static void ptrace_do_notify(int signr, int exit_code, int why)
2369{
2370 kernel_siginfo_t info;
2371
2372 clear_siginfo(&info);
2373 info.si_signo = signr;
2374 info.si_code = exit_code;
2375 info.si_pid = task_pid_vnr(current);
2376 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
2377
2378 /* Let the debugger run. */
2379 ptrace_stop(exit_code, why, 1, &info);
2380}
2381
2382void ptrace_notify(int exit_code)
2383{
2384 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
2385 if (unlikely(current->task_works))
2386 task_work_run();
2387
2388 spin_lock_irq(&current->sighand->siglock);
2389 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
2390 spin_unlock_irq(&current->sighand->siglock);
2391}
2392
2393/**
2394 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2395 * @signr: signr causing group stop if initiating
2396 *
2397 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2398 * and participate in it. If already set, participate in the existing
2399 * group stop. If participated in a group stop (and thus slept), %true is
2400 * returned with siglock released.
2401 *
2402 * If ptraced, this function doesn't handle stop itself. Instead,
2403 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2404 * untouched. The caller must ensure that INTERRUPT trap handling takes
2405 * places afterwards.
2406 *
2407 * CONTEXT:
2408 * Must be called with @current->sighand->siglock held, which is released
2409 * on %true return.
2410 *
2411 * RETURNS:
2412 * %false if group stop is already cancelled or ptrace trap is scheduled.
2413 * %true if participated in group stop.
2414 */
2415static bool do_signal_stop(int signr)
2416 __releases(&current->sighand->siglock)
2417{
2418 struct signal_struct *sig = current->signal;
2419
2420 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2421 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
2422 struct task_struct *t;
2423
2424 /* signr will be recorded in task->jobctl for retries */
2425 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
2426
2427 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
2428 unlikely(signal_group_exit(sig)))
2429 return false;
2430 /*
2431 * There is no group stop already in progress. We must
2432 * initiate one now.
2433 *
2434 * While ptraced, a task may be resumed while group stop is
2435 * still in effect and then receive a stop signal and
2436 * initiate another group stop. This deviates from the
2437 * usual behavior as two consecutive stop signals can't
2438 * cause two group stops when !ptraced. That is why we
2439 * also check !task_is_stopped(t) below.
2440 *
2441 * The condition can be distinguished by testing whether
2442 * SIGNAL_STOP_STOPPED is already set. Don't generate
2443 * group_exit_code in such case.
2444 *
2445 * This is not necessary for SIGNAL_STOP_CONTINUED because
2446 * an intervening stop signal is required to cause two
2447 * continued events regardless of ptrace.
2448 */
2449 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2450 sig->group_exit_code = signr;
2451
2452 sig->group_stop_count = 0;
2453
2454 if (task_set_jobctl_pending(current, signr | gstop))
2455 sig->group_stop_count++;
2456
2457 t = current;
2458 while_each_thread(current, t) {
2459 /*
2460 * Setting state to TASK_STOPPED for a group
2461 * stop is always done with the siglock held,
2462 * so this check has no races.
2463 */
2464 if (!task_is_stopped(t) &&
2465 task_set_jobctl_pending(t, signr | gstop)) {
2466 sig->group_stop_count++;
2467 if (likely(!(t->ptrace & PT_SEIZED)))
2468 signal_wake_up(t, 0);
2469 else
2470 ptrace_trap_notify(t);
2471 }
2472 }
2473 }
2474
2475 if (likely(!current->ptrace)) {
2476 int notify = 0;
2477
2478 /*
2479 * If there are no other threads in the group, or if there
2480 * is a group stop in progress and we are the last to stop,
2481 * report to the parent.
2482 */
2483 if (task_participate_group_stop(current))
2484 notify = CLD_STOPPED;
2485
2486 set_special_state(TASK_STOPPED);
2487 spin_unlock_irq(&current->sighand->siglock);
2488
2489 /*
2490 * Notify the parent of the group stop completion. Because
2491 * we're not holding either the siglock or tasklist_lock
2492 * here, ptracer may attach inbetween; however, this is for
2493 * group stop and should always be delivered to the real
2494 * parent of the group leader. The new ptracer will get
2495 * its notification when this task transitions into
2496 * TASK_TRACED.
2497 */
2498 if (notify) {
2499 read_lock(&tasklist_lock);
2500 do_notify_parent_cldstop(current, false, notify);
2501 read_unlock(&tasklist_lock);
2502 }
2503
2504 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2505 cgroup_enter_frozen();
2506 freezable_schedule();
2507 return true;
2508 } else {
2509 /*
2510 * While ptraced, group stop is handled by STOP trap.
2511 * Schedule it and let the caller deal with it.
2512 */
2513 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2514 return false;
2515 }
2516}
2517
2518/**
2519 * do_jobctl_trap - take care of ptrace jobctl traps
2520 *
2521 * When PT_SEIZED, it's used for both group stop and explicit
2522 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2523 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2524 * the stop signal; otherwise, %SIGTRAP.
2525 *
2526 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2527 * number as exit_code and no siginfo.
2528 *
2529 * CONTEXT:
2530 * Must be called with @current->sighand->siglock held, which may be
2531 * released and re-acquired before returning with intervening sleep.
2532 */
2533static void do_jobctl_trap(void)
2534{
2535 struct signal_struct *signal = current->signal;
2536 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2537
2538 if (current->ptrace & PT_SEIZED) {
2539 if (!signal->group_stop_count &&
2540 !(signal->flags & SIGNAL_STOP_STOPPED))
2541 signr = SIGTRAP;
2542 WARN_ON_ONCE(!signr);
2543 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2544 CLD_STOPPED);
2545 } else {
2546 WARN_ON_ONCE(!signr);
2547 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2548 current->exit_code = 0;
2549 }
2550}
2551
2552/**
2553 * do_freezer_trap - handle the freezer jobctl trap
2554 *
2555 * Puts the task into frozen state, if only the task is not about to quit.
2556 * In this case it drops JOBCTL_TRAP_FREEZE.
2557 *
2558 * CONTEXT:
2559 * Must be called with @current->sighand->siglock held,
2560 * which is always released before returning.
2561 */
2562static void do_freezer_trap(void)
2563 __releases(&current->sighand->siglock)
2564{
2565 /*
2566 * If there are other trap bits pending except JOBCTL_TRAP_FREEZE,
2567 * let's make another loop to give it a chance to be handled.
2568 * In any case, we'll return back.
2569 */
2570 if ((current->jobctl & (JOBCTL_PENDING_MASK | JOBCTL_TRAP_FREEZE)) !=
2571 JOBCTL_TRAP_FREEZE) {
2572 spin_unlock_irq(&current->sighand->siglock);
2573 return;
2574 }
2575
2576 /*
2577 * Now we're sure that there is no pending fatal signal and no
2578 * pending traps. Clear TIF_SIGPENDING to not get out of schedule()
2579 * immediately (if there is a non-fatal signal pending), and
2580 * put the task into sleep.
2581 */
2582 __set_current_state(TASK_INTERRUPTIBLE);
2583 clear_thread_flag(TIF_SIGPENDING);
2584 spin_unlock_irq(&current->sighand->siglock);
2585 cgroup_enter_frozen();
2586 freezable_schedule();
2587}
2588
2589static int ptrace_signal(int signr, kernel_siginfo_t *info)
2590{
2591 /*
2592 * We do not check sig_kernel_stop(signr) but set this marker
2593 * unconditionally because we do not know whether debugger will
2594 * change signr. This flag has no meaning unless we are going
2595 * to stop after return from ptrace_stop(). In this case it will
2596 * be checked in do_signal_stop(), we should only stop if it was
2597 * not cleared by SIGCONT while we were sleeping. See also the
2598 * comment in dequeue_signal().
2599 */
2600 current->jobctl |= JOBCTL_STOP_DEQUEUED;
2601 ptrace_stop(signr, CLD_TRAPPED, 0, info);
2602
2603 /* We're back. Did the debugger cancel the sig? */
2604 signr = current->exit_code;
2605 if (signr == 0)
2606 return signr;
2607
2608 current->exit_code = 0;
2609
2610 /*
2611 * Update the siginfo structure if the signal has
2612 * changed. If the debugger wanted something
2613 * specific in the siginfo structure then it should
2614 * have updated *info via PTRACE_SETSIGINFO.
2615 */
2616 if (signr != info->si_signo) {
2617 clear_siginfo(info);
2618 info->si_signo = signr;
2619 info->si_errno = 0;
2620 info->si_code = SI_USER;
2621 rcu_read_lock();
2622 info->si_pid = task_pid_vnr(current->parent);
2623 info->si_uid = from_kuid_munged(current_user_ns(),
2624 task_uid(current->parent));
2625 rcu_read_unlock();
2626 }
2627
2628 /* If the (new) signal is now blocked, requeue it. */
2629 if (sigismember(&current->blocked, signr)) {
2630 send_signal(signr, info, current, PIDTYPE_PID);
2631 signr = 0;
2632 }
2633
2634 return signr;
2635}
2636
2637bool get_signal(struct ksignal *ksig)
2638{
2639 struct sighand_struct *sighand = current->sighand;
2640 struct signal_struct *signal = current->signal;
2641 int signr;
2642
2643 if (unlikely(current->task_works))
2644 task_work_run();
2645
2646 if (unlikely(uprobe_deny_signal()))
2647 return false;
2648
2649 /*
2650 * Do this once, we can't return to user-mode if freezing() == T.
2651 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2652 * thus do not need another check after return.
2653 */
2654 try_to_freeze();
2655
2656relock:
2657 spin_lock_irq(&sighand->siglock);
2658 /*
2659 * Every stopped thread goes here after wakeup. Check to see if
2660 * we should notify the parent, prepare_signal(SIGCONT) encodes
2661 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2662 */
2663 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2664 int why;
2665
2666 if (signal->flags & SIGNAL_CLD_CONTINUED)
2667 why = CLD_CONTINUED;
2668 else
2669 why = CLD_STOPPED;
2670
2671 signal->flags &= ~SIGNAL_CLD_MASK;
2672
2673 spin_unlock_irq(&sighand->siglock);
2674
2675 /*
2676 * Notify the parent that we're continuing. This event is
2677 * always per-process and doesn't make whole lot of sense
2678 * for ptracers, who shouldn't consume the state via
2679 * wait(2) either, but, for backward compatibility, notify
2680 * the ptracer of the group leader too unless it's gonna be
2681 * a duplicate.
2682 */
2683 read_lock(&tasklist_lock);
2684 do_notify_parent_cldstop(current, false, why);
2685
2686 if (ptrace_reparented(current->group_leader))
2687 do_notify_parent_cldstop(current->group_leader,
2688 true, why);
2689 read_unlock(&tasklist_lock);
2690
2691 goto relock;
2692 }
2693
2694 /* Has this task already been marked for death? */
2695 if (signal_group_exit(signal)) {
2696 ksig->info.si_signo = signr = SIGKILL;
2697 sigdelset(&current->pending.signal, SIGKILL);
2698 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2699 &sighand->action[SIGKILL - 1]);
2700 recalc_sigpending();
2701 goto fatal;
2702 }
2703
2704 for (;;) {
2705 struct k_sigaction *ka;
2706
2707 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2708 do_signal_stop(0))
2709 goto relock;
2710
2711 if (unlikely(current->jobctl &
2712 (JOBCTL_TRAP_MASK | JOBCTL_TRAP_FREEZE))) {
2713 if (current->jobctl & JOBCTL_TRAP_MASK) {
2714 do_jobctl_trap();
2715 spin_unlock_irq(&sighand->siglock);
2716 } else if (current->jobctl & JOBCTL_TRAP_FREEZE)
2717 do_freezer_trap();
2718
2719 goto relock;
2720 }
2721
2722 /*
2723 * If the task is leaving the frozen state, let's update
2724 * cgroup counters and reset the frozen bit.
2725 */
2726 if (unlikely(cgroup_task_frozen(current))) {
2727 spin_unlock_irq(&sighand->siglock);
2728 cgroup_leave_frozen(false);
2729 goto relock;
2730 }
2731
2732 /*
2733 * Signals generated by the execution of an instruction
2734 * need to be delivered before any other pending signals
2735 * so that the instruction pointer in the signal stack
2736 * frame points to the faulting instruction.
2737 */
2738 signr = dequeue_synchronous_signal(&ksig->info);
2739 if (!signr)
2740 signr = dequeue_signal(current, &current->blocked, &ksig->info);
2741
2742 if (!signr)
2743 break; /* will return 0 */
2744
2745 if (unlikely(current->ptrace) && signr != SIGKILL) {
2746 signr = ptrace_signal(signr, &ksig->info);
2747 if (!signr)
2748 continue;
2749 }
2750
2751 ka = &sighand->action[signr-1];
2752
2753 /* Trace actually delivered signals. */
2754 trace_signal_deliver(signr, &ksig->info, ka);
2755
2756 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2757 continue;
2758 if (ka->sa.sa_handler != SIG_DFL) {
2759 /* Run the handler. */
2760 ksig->ka = *ka;
2761
2762 if (ka->sa.sa_flags & SA_ONESHOT)
2763 ka->sa.sa_handler = SIG_DFL;
2764
2765 break; /* will return non-zero "signr" value */
2766 }
2767
2768 /*
2769 * Now we are doing the default action for this signal.
2770 */
2771 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2772 continue;
2773
2774 /*
2775 * Global init gets no signals it doesn't want.
2776 * Container-init gets no signals it doesn't want from same
2777 * container.
2778 *
2779 * Note that if global/container-init sees a sig_kernel_only()
2780 * signal here, the signal must have been generated internally
2781 * or must have come from an ancestor namespace. In either
2782 * case, the signal cannot be dropped.
2783 */
2784 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2785 !sig_kernel_only(signr))
2786 continue;
2787
2788 if (sig_kernel_stop(signr)) {
2789 /*
2790 * The default action is to stop all threads in
2791 * the thread group. The job control signals
2792 * do nothing in an orphaned pgrp, but SIGSTOP
2793 * always works. Note that siglock needs to be
2794 * dropped during the call to is_orphaned_pgrp()
2795 * because of lock ordering with tasklist_lock.
2796 * This allows an intervening SIGCONT to be posted.
2797 * We need to check for that and bail out if necessary.
2798 */
2799 if (signr != SIGSTOP) {
2800 spin_unlock_irq(&sighand->siglock);
2801
2802 /* signals can be posted during this window */
2803
2804 if (is_current_pgrp_orphaned())
2805 goto relock;
2806
2807 spin_lock_irq(&sighand->siglock);
2808 }
2809
2810 if (likely(do_signal_stop(ksig->info.si_signo))) {
2811 /* It released the siglock. */
2812 goto relock;
2813 }
2814
2815 /*
2816 * We didn't actually stop, due to a race
2817 * with SIGCONT or something like that.
2818 */
2819 continue;
2820 }
2821
2822 fatal:
2823 spin_unlock_irq(&sighand->siglock);
2824 if (unlikely(cgroup_task_frozen(current)))
2825 cgroup_leave_frozen(true);
2826
2827 /*
2828 * Anything else is fatal, maybe with a core dump.
2829 */
2830 current->flags |= PF_SIGNALED;
2831
2832 if (sig_kernel_coredump(signr)) {
2833 if (print_fatal_signals)
2834 print_fatal_signal(ksig->info.si_signo);
2835 proc_coredump_connector(current);
2836 /*
2837 * If it was able to dump core, this kills all
2838 * other threads in the group and synchronizes with
2839 * their demise. If we lost the race with another
2840 * thread getting here, it set group_exit_code
2841 * first and our do_group_exit call below will use
2842 * that value and ignore the one we pass it.
2843 */
2844 do_coredump(&ksig->info);
2845 }
2846
2847 /*
2848 * Death signals, no core dump.
2849 */
2850 do_group_exit(ksig->info.si_signo);
2851 /* NOTREACHED */
2852 }
2853 spin_unlock_irq(&sighand->siglock);
2854
2855 ksig->sig = signr;
2856 return ksig->sig > 0;
2857}
2858
2859/**
2860 * signal_delivered -
2861 * @ksig: kernel signal struct
2862 * @stepping: nonzero if debugger single-step or block-step in use
2863 *
2864 * This function should be called when a signal has successfully been
2865 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
2866 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2867 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
2868 */
2869static void signal_delivered(struct ksignal *ksig, int stepping)
2870{
2871 sigset_t blocked;
2872
2873 /* A signal was successfully delivered, and the
2874 saved sigmask was stored on the signal frame,
2875 and will be restored by sigreturn. So we can
2876 simply clear the restore sigmask flag. */
2877 clear_restore_sigmask();
2878
2879 sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
2880 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2881 sigaddset(&blocked, ksig->sig);
2882 set_current_blocked(&blocked);
2883 tracehook_signal_handler(stepping);
2884}
2885
2886void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2887{
2888 if (failed)
2889 force_sigsegv(ksig->sig);
2890 else
2891 signal_delivered(ksig, stepping);
2892}
2893
2894/*
2895 * It could be that complete_signal() picked us to notify about the
2896 * group-wide signal. Other threads should be notified now to take
2897 * the shared signals in @which since we will not.
2898 */
2899static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2900{
2901 sigset_t retarget;
2902 struct task_struct *t;
2903
2904 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2905 if (sigisemptyset(&retarget))
2906 return;
2907
2908 t = tsk;
2909 while_each_thread(tsk, t) {
2910 if (t->flags & PF_EXITING)
2911 continue;
2912
2913 if (!has_pending_signals(&retarget, &t->blocked))
2914 continue;
2915 /* Remove the signals this thread can handle. */
2916 sigandsets(&retarget, &retarget, &t->blocked);
2917
2918 if (!signal_pending(t))
2919 signal_wake_up(t, 0);
2920
2921 if (sigisemptyset(&retarget))
2922 break;
2923 }
2924}
2925
2926void exit_signals(struct task_struct *tsk)
2927{
2928 int group_stop = 0;
2929 sigset_t unblocked;
2930
2931 /*
2932 * @tsk is about to have PF_EXITING set - lock out users which
2933 * expect stable threadgroup.
2934 */
2935 cgroup_threadgroup_change_begin(tsk);
2936
2937 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2938 tsk->flags |= PF_EXITING;
2939 cgroup_threadgroup_change_end(tsk);
2940 return;
2941 }
2942
2943 spin_lock_irq(&tsk->sighand->siglock);
2944 /*
2945 * From now this task is not visible for group-wide signals,
2946 * see wants_signal(), do_signal_stop().
2947 */
2948 tsk->flags |= PF_EXITING;
2949
2950 cgroup_threadgroup_change_end(tsk);
2951
2952 if (!signal_pending(tsk))
2953 goto out;
2954
2955 unblocked = tsk->blocked;
2956 signotset(&unblocked);
2957 retarget_shared_pending(tsk, &unblocked);
2958
2959 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2960 task_participate_group_stop(tsk))
2961 group_stop = CLD_STOPPED;
2962out:
2963 spin_unlock_irq(&tsk->sighand->siglock);
2964
2965 /*
2966 * If group stop has completed, deliver the notification. This
2967 * should always go to the real parent of the group leader.
2968 */
2969 if (unlikely(group_stop)) {
2970 read_lock(&tasklist_lock);
2971 do_notify_parent_cldstop(tsk, false, group_stop);
2972 read_unlock(&tasklist_lock);
2973 }
2974}
2975
2976/*
2977 * System call entry points.
2978 */
2979
2980/**
2981 * sys_restart_syscall - restart a system call
2982 */
2983SYSCALL_DEFINE0(restart_syscall)
2984{
2985 struct restart_block *restart = &current->restart_block;
2986 return restart->fn(restart);
2987}
2988
2989long do_no_restart_syscall(struct restart_block *param)
2990{
2991 return -EINTR;
2992}
2993
2994static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2995{
2996 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2997 sigset_t newblocked;
2998 /* A set of now blocked but previously unblocked signals. */
2999 sigandnsets(&newblocked, newset, &current->blocked);
3000 retarget_shared_pending(tsk, &newblocked);
3001 }
3002 tsk->blocked = *newset;
3003 recalc_sigpending();
3004}
3005
3006/**
3007 * set_current_blocked - change current->blocked mask
3008 * @newset: new mask
3009 *
3010 * It is wrong to change ->blocked directly, this helper should be used
3011 * to ensure the process can't miss a shared signal we are going to block.
3012 */
3013void set_current_blocked(sigset_t *newset)
3014{
3015 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
3016 __set_current_blocked(newset);
3017}
3018
3019void __set_current_blocked(const sigset_t *newset)
3020{
3021 struct task_struct *tsk = current;
3022
3023 /*
3024 * In case the signal mask hasn't changed, there is nothing we need
3025 * to do. The current->blocked shouldn't be modified by other task.
3026 */
3027 if (sigequalsets(&tsk->blocked, newset))
3028 return;
3029
3030 spin_lock_irq(&tsk->sighand->siglock);
3031 __set_task_blocked(tsk, newset);
3032 spin_unlock_irq(&tsk->sighand->siglock);
3033}
3034
3035/*
3036 * This is also useful for kernel threads that want to temporarily
3037 * (or permanently) block certain signals.
3038 *
3039 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
3040 * interface happily blocks "unblockable" signals like SIGKILL
3041 * and friends.
3042 */
3043int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
3044{
3045 struct task_struct *tsk = current;
3046 sigset_t newset;
3047
3048 /* Lockless, only current can change ->blocked, never from irq */
3049 if (oldset)
3050 *oldset = tsk->blocked;
3051
3052 switch (how) {
3053 case SIG_BLOCK:
3054 sigorsets(&newset, &tsk->blocked, set);
3055 break;
3056 case SIG_UNBLOCK:
3057 sigandnsets(&newset, &tsk->blocked, set);
3058 break;
3059 case SIG_SETMASK:
3060 newset = *set;
3061 break;
3062 default:
3063 return -EINVAL;
3064 }
3065
3066 __set_current_blocked(&newset);
3067 return 0;
3068}
3069EXPORT_SYMBOL(sigprocmask);
3070
3071/*
3072 * The api helps set app-provided sigmasks.
3073 *
3074 * This is useful for syscalls such as ppoll, pselect, io_pgetevents and
3075 * epoll_pwait where a new sigmask is passed from userland for the syscalls.
3076 *
3077 * Note that it does set_restore_sigmask() in advance, so it must be always
3078 * paired with restore_saved_sigmask_unless() before return from syscall.
3079 */
3080int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize)
3081{
3082 sigset_t kmask;
3083
3084 if (!umask)
3085 return 0;
3086 if (sigsetsize != sizeof(sigset_t))
3087 return -EINVAL;
3088 if (copy_from_user(&kmask, umask, sizeof(sigset_t)))
3089 return -EFAULT;
3090
3091 set_restore_sigmask();
3092 current->saved_sigmask = current->blocked;
3093 set_current_blocked(&kmask);
3094
3095 return 0;
3096}
3097
3098#ifdef CONFIG_COMPAT
3099int set_compat_user_sigmask(const compat_sigset_t __user *umask,
3100 size_t sigsetsize)
3101{
3102 sigset_t kmask;
3103
3104 if (!umask)
3105 return 0;
3106 if (sigsetsize != sizeof(compat_sigset_t))
3107 return -EINVAL;
3108 if (get_compat_sigset(&kmask, umask))
3109 return -EFAULT;
3110
3111 set_restore_sigmask();
3112 current->saved_sigmask = current->blocked;
3113 set_current_blocked(&kmask);
3114
3115 return 0;
3116}
3117#endif
3118
3119/**
3120 * sys_rt_sigprocmask - change the list of currently blocked signals
3121 * @how: whether to add, remove, or set signals
3122 * @nset: stores pending signals
3123 * @oset: previous value of signal mask if non-null
3124 * @sigsetsize: size of sigset_t type
3125 */
3126SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
3127 sigset_t __user *, oset, size_t, sigsetsize)
3128{
3129 sigset_t old_set, new_set;
3130 int error;
3131
3132 /* XXX: Don't preclude handling different sized sigset_t's. */
3133 if (sigsetsize != sizeof(sigset_t))
3134 return -EINVAL;
3135
3136 old_set = current->blocked;
3137
3138 if (nset) {
3139 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
3140 return -EFAULT;
3141 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
3142
3143 error = sigprocmask(how, &new_set, NULL);
3144 if (error)
3145 return error;
3146 }
3147
3148 if (oset) {
3149 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
3150 return -EFAULT;
3151 }
3152
3153 return 0;
3154}
3155
3156#ifdef CONFIG_COMPAT
3157COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
3158 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
3159{
3160 sigset_t old_set = current->blocked;
3161
3162 /* XXX: Don't preclude handling different sized sigset_t's. */
3163 if (sigsetsize != sizeof(sigset_t))
3164 return -EINVAL;
3165
3166 if (nset) {
3167 sigset_t new_set;
3168 int error;
3169 if (get_compat_sigset(&new_set, nset))
3170 return -EFAULT;
3171 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
3172
3173 error = sigprocmask(how, &new_set, NULL);
3174 if (error)
3175 return error;
3176 }
3177 return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
3178}
3179#endif
3180
3181static void do_sigpending(sigset_t *set)
3182{
3183 spin_lock_irq(&current->sighand->siglock);
3184 sigorsets(set, &current->pending.signal,
3185 &current->signal->shared_pending.signal);
3186 spin_unlock_irq(&current->sighand->siglock);
3187
3188 /* Outside the lock because only this thread touches it. */
3189 sigandsets(set, &current->blocked, set);
3190}
3191
3192/**
3193 * sys_rt_sigpending - examine a pending signal that has been raised
3194 * while blocked
3195 * @uset: stores pending signals
3196 * @sigsetsize: size of sigset_t type or larger
3197 */
3198SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
3199{
3200 sigset_t set;
3201
3202 if (sigsetsize > sizeof(*uset))
3203 return -EINVAL;
3204
3205 do_sigpending(&set);
3206
3207 if (copy_to_user(uset, &set, sigsetsize))
3208 return -EFAULT;
3209
3210 return 0;
3211}
3212
3213#ifdef CONFIG_COMPAT
3214COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
3215 compat_size_t, sigsetsize)
3216{
3217 sigset_t set;
3218
3219 if (sigsetsize > sizeof(*uset))
3220 return -EINVAL;
3221
3222 do_sigpending(&set);
3223
3224 return put_compat_sigset(uset, &set, sigsetsize);
3225}
3226#endif
3227
3228static const struct {
3229 unsigned char limit, layout;
3230} sig_sicodes[] = {
3231 [SIGILL] = { NSIGILL, SIL_FAULT },
3232 [SIGFPE] = { NSIGFPE, SIL_FAULT },
3233 [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
3234 [SIGBUS] = { NSIGBUS, SIL_FAULT },
3235 [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
3236#if defined(SIGEMT)
3237 [SIGEMT] = { NSIGEMT, SIL_FAULT },
3238#endif
3239 [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
3240 [SIGPOLL] = { NSIGPOLL, SIL_POLL },
3241 [SIGSYS] = { NSIGSYS, SIL_SYS },
3242};
3243
3244static bool known_siginfo_layout(unsigned sig, int si_code)
3245{
3246 if (si_code == SI_KERNEL)
3247 return true;
3248 else if ((si_code > SI_USER)) {
3249 if (sig_specific_sicodes(sig)) {
3250 if (si_code <= sig_sicodes[sig].limit)
3251 return true;
3252 }
3253 else if (si_code <= NSIGPOLL)
3254 return true;
3255 }
3256 else if (si_code >= SI_DETHREAD)
3257 return true;
3258 else if (si_code == SI_ASYNCNL)
3259 return true;
3260 return false;
3261}
3262
3263enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
3264{
3265 enum siginfo_layout layout = SIL_KILL;
3266 if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
3267 if ((sig < ARRAY_SIZE(sig_sicodes)) &&
3268 (si_code <= sig_sicodes[sig].limit)) {
3269 layout = sig_sicodes[sig].layout;
3270 /* Handle the exceptions */
3271 if ((sig == SIGBUS) &&
3272 (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
3273 layout = SIL_FAULT_MCEERR;
3274 else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
3275 layout = SIL_FAULT_BNDERR;
3276#ifdef SEGV_PKUERR
3277 else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
3278 layout = SIL_FAULT_PKUERR;
3279#endif
3280 }
3281 else if (si_code <= NSIGPOLL)
3282 layout = SIL_POLL;
3283 } else {
3284 if (si_code == SI_TIMER)
3285 layout = SIL_TIMER;
3286 else if (si_code == SI_SIGIO)
3287 layout = SIL_POLL;
3288 else if (si_code < 0)
3289 layout = SIL_RT;
3290 }
3291 return layout;
3292}
3293
3294static inline char __user *si_expansion(const siginfo_t __user *info)
3295{
3296 return ((char __user *)info) + sizeof(struct kernel_siginfo);
3297}
3298
3299int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from)
3300{
3301 char __user *expansion = si_expansion(to);
3302 if (copy_to_user(to, from , sizeof(struct kernel_siginfo)))
3303 return -EFAULT;
3304 if (clear_user(expansion, SI_EXPANSION_SIZE))
3305 return -EFAULT;
3306 return 0;
3307}
3308
3309static int post_copy_siginfo_from_user(kernel_siginfo_t *info,
3310 const siginfo_t __user *from)
3311{
3312 if (unlikely(!known_siginfo_layout(info->si_signo, info->si_code))) {
3313 char __user *expansion = si_expansion(from);
3314 char buf[SI_EXPANSION_SIZE];
3315 int i;
3316 /*
3317 * An unknown si_code might need more than
3318 * sizeof(struct kernel_siginfo) bytes. Verify all of the
3319 * extra bytes are 0. This guarantees copy_siginfo_to_user
3320 * will return this data to userspace exactly.
3321 */
3322 if (copy_from_user(&buf, expansion, SI_EXPANSION_SIZE))
3323 return -EFAULT;
3324 for (i = 0; i < SI_EXPANSION_SIZE; i++) {
3325 if (buf[i] != 0)
3326 return -E2BIG;
3327 }
3328 }
3329 return 0;
3330}
3331
3332static int __copy_siginfo_from_user(int signo, kernel_siginfo_t *to,
3333 const siginfo_t __user *from)
3334{
3335 if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
3336 return -EFAULT;
3337 to->si_signo = signo;
3338 return post_copy_siginfo_from_user(to, from);
3339}
3340
3341int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from)
3342{
3343 if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
3344 return -EFAULT;
3345 return post_copy_siginfo_from_user(to, from);
3346}
3347
3348#ifdef CONFIG_COMPAT
3349int copy_siginfo_to_user32(struct compat_siginfo __user *to,
3350 const struct kernel_siginfo *from)
3351#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
3352{
3353 return __copy_siginfo_to_user32(to, from, in_x32_syscall());
3354}
3355int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
3356 const struct kernel_siginfo *from, bool x32_ABI)
3357#endif
3358{
3359 struct compat_siginfo new;
3360 memset(&new, 0, sizeof(new));
3361
3362 new.si_signo = from->si_signo;
3363 new.si_errno = from->si_errno;
3364 new.si_code = from->si_code;
3365 switch(siginfo_layout(from->si_signo, from->si_code)) {
3366 case SIL_KILL:
3367 new.si_pid = from->si_pid;
3368 new.si_uid = from->si_uid;
3369 break;
3370 case SIL_TIMER:
3371 new.si_tid = from->si_tid;
3372 new.si_overrun = from->si_overrun;
3373 new.si_int = from->si_int;
3374 break;
3375 case SIL_POLL:
3376 new.si_band = from->si_band;
3377 new.si_fd = from->si_fd;
3378 break;
3379 case SIL_FAULT:
3380 new.si_addr = ptr_to_compat(from->si_addr);
3381#ifdef __ARCH_SI_TRAPNO
3382 new.si_trapno = from->si_trapno;
3383#endif
3384 break;
3385 case SIL_FAULT_MCEERR:
3386 new.si_addr = ptr_to_compat(from->si_addr);
3387#ifdef __ARCH_SI_TRAPNO
3388 new.si_trapno = from->si_trapno;
3389#endif
3390 new.si_addr_lsb = from->si_addr_lsb;
3391 break;
3392 case SIL_FAULT_BNDERR:
3393 new.si_addr = ptr_to_compat(from->si_addr);
3394#ifdef __ARCH_SI_TRAPNO
3395 new.si_trapno = from->si_trapno;
3396#endif
3397 new.si_lower = ptr_to_compat(from->si_lower);
3398 new.si_upper = ptr_to_compat(from->si_upper);
3399 break;
3400 case SIL_FAULT_PKUERR:
3401 new.si_addr = ptr_to_compat(from->si_addr);
3402#ifdef __ARCH_SI_TRAPNO
3403 new.si_trapno = from->si_trapno;
3404#endif
3405 new.si_pkey = from->si_pkey;
3406 break;
3407 case SIL_CHLD:
3408 new.si_pid = from->si_pid;
3409 new.si_uid = from->si_uid;
3410 new.si_status = from->si_status;
3411#ifdef CONFIG_X86_X32_ABI
3412 if (x32_ABI) {
3413 new._sifields._sigchld_x32._utime = from->si_utime;
3414 new._sifields._sigchld_x32._stime = from->si_stime;
3415 } else
3416#endif
3417 {
3418 new.si_utime = from->si_utime;
3419 new.si_stime = from->si_stime;
3420 }
3421 break;
3422 case SIL_RT:
3423 new.si_pid = from->si_pid;
3424 new.si_uid = from->si_uid;
3425 new.si_int = from->si_int;
3426 break;
3427 case SIL_SYS:
3428 new.si_call_addr = ptr_to_compat(from->si_call_addr);
3429 new.si_syscall = from->si_syscall;
3430 new.si_arch = from->si_arch;
3431 break;
3432 }
3433
3434 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
3435 return -EFAULT;
3436
3437 return 0;
3438}
3439
3440static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
3441 const struct compat_siginfo *from)
3442{
3443 clear_siginfo(to);
3444 to->si_signo = from->si_signo;
3445 to->si_errno = from->si_errno;
3446 to->si_code = from->si_code;
3447 switch(siginfo_layout(from->si_signo, from->si_code)) {
3448 case SIL_KILL:
3449 to->si_pid = from->si_pid;
3450 to->si_uid = from->si_uid;
3451 break;
3452 case SIL_TIMER:
3453 to->si_tid = from->si_tid;
3454 to->si_overrun = from->si_overrun;
3455 to->si_int = from->si_int;
3456 break;
3457 case SIL_POLL:
3458 to->si_band = from->si_band;
3459 to->si_fd = from->si_fd;
3460 break;
3461 case SIL_FAULT:
3462 to->si_addr = compat_ptr(from->si_addr);
3463#ifdef __ARCH_SI_TRAPNO
3464 to->si_trapno = from->si_trapno;
3465#endif
3466 break;
3467 case SIL_FAULT_MCEERR:
3468 to->si_addr = compat_ptr(from->si_addr);
3469#ifdef __ARCH_SI_TRAPNO
3470 to->si_trapno = from->si_trapno;
3471#endif
3472 to->si_addr_lsb = from->si_addr_lsb;
3473 break;
3474 case SIL_FAULT_BNDERR:
3475 to->si_addr = compat_ptr(from->si_addr);
3476#ifdef __ARCH_SI_TRAPNO
3477 to->si_trapno = from->si_trapno;
3478#endif
3479 to->si_lower = compat_ptr(from->si_lower);
3480 to->si_upper = compat_ptr(from->si_upper);
3481 break;
3482 case SIL_FAULT_PKUERR:
3483 to->si_addr = compat_ptr(from->si_addr);
3484#ifdef __ARCH_SI_TRAPNO
3485 to->si_trapno = from->si_trapno;
3486#endif
3487 to->si_pkey = from->si_pkey;
3488 break;
3489 case SIL_CHLD:
3490 to->si_pid = from->si_pid;
3491 to->si_uid = from->si_uid;
3492 to->si_status = from->si_status;
3493#ifdef CONFIG_X86_X32_ABI
3494 if (in_x32_syscall()) {
3495 to->si_utime = from->_sifields._sigchld_x32._utime;
3496 to->si_stime = from->_sifields._sigchld_x32._stime;
3497 } else
3498#endif
3499 {
3500 to->si_utime = from->si_utime;
3501 to->si_stime = from->si_stime;
3502 }
3503 break;
3504 case SIL_RT:
3505 to->si_pid = from->si_pid;
3506 to->si_uid = from->si_uid;
3507 to->si_int = from->si_int;
3508 break;
3509 case SIL_SYS:
3510 to->si_call_addr = compat_ptr(from->si_call_addr);
3511 to->si_syscall = from->si_syscall;
3512 to->si_arch = from->si_arch;
3513 break;
3514 }
3515 return 0;
3516}
3517
3518static int __copy_siginfo_from_user32(int signo, struct kernel_siginfo *to,
3519 const struct compat_siginfo __user *ufrom)
3520{
3521 struct compat_siginfo from;
3522
3523 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3524 return -EFAULT;
3525
3526 from.si_signo = signo;
3527 return post_copy_siginfo_from_user32(to, &from);
3528}
3529
3530int copy_siginfo_from_user32(struct kernel_siginfo *to,
3531 const struct compat_siginfo __user *ufrom)
3532{
3533 struct compat_siginfo from;
3534
3535 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3536 return -EFAULT;
3537
3538 return post_copy_siginfo_from_user32(to, &from);
3539}
3540#endif /* CONFIG_COMPAT */
3541
3542/**
3543 * do_sigtimedwait - wait for queued signals specified in @which
3544 * @which: queued signals to wait for
3545 * @info: if non-null, the signal's siginfo is returned here
3546 * @ts: upper bound on process time suspension
3547 */
3548static int do_sigtimedwait(const sigset_t *which, kernel_siginfo_t *info,
3549 const struct timespec64 *ts)
3550{
3551 ktime_t *to = NULL, timeout = KTIME_MAX;
3552 struct task_struct *tsk = current;
3553 sigset_t mask = *which;
3554 int sig, ret = 0;
3555
3556 if (ts) {
3557 if (!timespec64_valid(ts))
3558 return -EINVAL;
3559 timeout = timespec64_to_ktime(*ts);
3560 to = &timeout;
3561 }
3562
3563 /*
3564 * Invert the set of allowed signals to get those we want to block.
3565 */
3566 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
3567 signotset(&mask);
3568
3569 spin_lock_irq(&tsk->sighand->siglock);
3570 sig = dequeue_signal(tsk, &mask, info);
3571 if (!sig && timeout) {
3572 /*
3573 * None ready, temporarily unblock those we're interested
3574 * while we are sleeping in so that we'll be awakened when
3575 * they arrive. Unblocking is always fine, we can avoid
3576 * set_current_blocked().
3577 */
3578 tsk->real_blocked = tsk->blocked;
3579 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
3580 recalc_sigpending();
3581 spin_unlock_irq(&tsk->sighand->siglock);
3582
3583 __set_current_state(TASK_INTERRUPTIBLE);
3584 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
3585 HRTIMER_MODE_REL);
3586 spin_lock_irq(&tsk->sighand->siglock);
3587 __set_task_blocked(tsk, &tsk->real_blocked);
3588 sigemptyset(&tsk->real_blocked);
3589 sig = dequeue_signal(tsk, &mask, info);
3590 }
3591 spin_unlock_irq(&tsk->sighand->siglock);
3592
3593 if (sig)
3594 return sig;
3595 return ret ? -EINTR : -EAGAIN;
3596}
3597
3598/**
3599 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
3600 * in @uthese
3601 * @uthese: queued signals to wait for
3602 * @uinfo: if non-null, the signal's siginfo is returned here
3603 * @uts: upper bound on process time suspension
3604 * @sigsetsize: size of sigset_t type
3605 */
3606SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
3607 siginfo_t __user *, uinfo,
3608 const struct __kernel_timespec __user *, uts,
3609 size_t, sigsetsize)
3610{
3611 sigset_t these;
3612 struct timespec64 ts;
3613 kernel_siginfo_t info;
3614 int ret;
3615
3616 /* XXX: Don't preclude handling different sized sigset_t's. */
3617 if (sigsetsize != sizeof(sigset_t))
3618 return -EINVAL;
3619
3620 if (copy_from_user(&these, uthese, sizeof(these)))
3621 return -EFAULT;
3622
3623 if (uts) {
3624 if (get_timespec64(&ts, uts))
3625 return -EFAULT;
3626 }
3627
3628 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
3629
3630 if (ret > 0 && uinfo) {
3631 if (copy_siginfo_to_user(uinfo, &info))
3632 ret = -EFAULT;
3633 }
3634
3635 return ret;
3636}
3637
3638#ifdef CONFIG_COMPAT_32BIT_TIME
3639SYSCALL_DEFINE4(rt_sigtimedwait_time32, const sigset_t __user *, uthese,
3640 siginfo_t __user *, uinfo,
3641 const struct old_timespec32 __user *, uts,
3642 size_t, sigsetsize)
3643{
3644 sigset_t these;
3645 struct timespec64 ts;
3646 kernel_siginfo_t info;
3647 int ret;
3648
3649 if (sigsetsize != sizeof(sigset_t))
3650 return -EINVAL;
3651
3652 if (copy_from_user(&these, uthese, sizeof(these)))
3653 return -EFAULT;
3654
3655 if (uts) {
3656 if (get_old_timespec32(&ts, uts))
3657 return -EFAULT;
3658 }
3659
3660 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
3661
3662 if (ret > 0 && uinfo) {
3663 if (copy_siginfo_to_user(uinfo, &info))
3664 ret = -EFAULT;
3665 }
3666
3667 return ret;
3668}
3669#endif
3670
3671#ifdef CONFIG_COMPAT
3672COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time64, compat_sigset_t __user *, uthese,
3673 struct compat_siginfo __user *, uinfo,
3674 struct __kernel_timespec __user *, uts, compat_size_t, sigsetsize)
3675{
3676 sigset_t s;
3677 struct timespec64 t;
3678 kernel_siginfo_t info;
3679 long ret;
3680
3681 if (sigsetsize != sizeof(sigset_t))
3682 return -EINVAL;
3683
3684 if (get_compat_sigset(&s, uthese))
3685 return -EFAULT;
3686
3687 if (uts) {
3688 if (get_timespec64(&t, uts))
3689 return -EFAULT;
3690 }
3691
3692 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3693
3694 if (ret > 0 && uinfo) {
3695 if (copy_siginfo_to_user32(uinfo, &info))
3696 ret = -EFAULT;
3697 }
3698
3699 return ret;
3700}
3701
3702#ifdef CONFIG_COMPAT_32BIT_TIME
3703COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time32, compat_sigset_t __user *, uthese,
3704 struct compat_siginfo __user *, uinfo,
3705 struct old_timespec32 __user *, uts, compat_size_t, sigsetsize)
3706{
3707 sigset_t s;
3708 struct timespec64 t;
3709 kernel_siginfo_t info;
3710 long ret;
3711
3712 if (sigsetsize != sizeof(sigset_t))
3713 return -EINVAL;
3714
3715 if (get_compat_sigset(&s, uthese))
3716 return -EFAULT;
3717
3718 if (uts) {
3719 if (get_old_timespec32(&t, uts))
3720 return -EFAULT;
3721 }
3722
3723 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3724
3725 if (ret > 0 && uinfo) {
3726 if (copy_siginfo_to_user32(uinfo, &info))
3727 ret = -EFAULT;
3728 }
3729
3730 return ret;
3731}
3732#endif
3733#endif
3734
3735static inline void prepare_kill_siginfo(int sig, struct kernel_siginfo *info)
3736{
3737 clear_siginfo(info);
3738 info->si_signo = sig;
3739 info->si_errno = 0;
3740 info->si_code = SI_USER;
3741 info->si_pid = task_tgid_vnr(current);
3742 info->si_uid = from_kuid_munged(current_user_ns(), current_uid());
3743}
3744
3745/**
3746 * sys_kill - send a signal to a process
3747 * @pid: the PID of the process
3748 * @sig: signal to be sent
3749 */
3750SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
3751{
3752 struct kernel_siginfo info;
3753
3754 prepare_kill_siginfo(sig, &info);
3755
3756 return kill_something_info(sig, &info, pid);
3757}
3758
3759/*
3760 * Verify that the signaler and signalee either are in the same pid namespace
3761 * or that the signaler's pid namespace is an ancestor of the signalee's pid
3762 * namespace.
3763 */
3764static bool access_pidfd_pidns(struct pid *pid)
3765{
3766 struct pid_namespace *active = task_active_pid_ns(current);
3767 struct pid_namespace *p = ns_of_pid(pid);
3768
3769 for (;;) {
3770 if (!p)
3771 return false;
3772 if (p == active)
3773 break;
3774 p = p->parent;
3775 }
3776
3777 return true;
3778}
3779
3780static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, siginfo_t *info)
3781{
3782#ifdef CONFIG_COMPAT
3783 /*
3784 * Avoid hooking up compat syscalls and instead handle necessary
3785 * conversions here. Note, this is a stop-gap measure and should not be
3786 * considered a generic solution.
3787 */
3788 if (in_compat_syscall())
3789 return copy_siginfo_from_user32(
3790 kinfo, (struct compat_siginfo __user *)info);
3791#endif
3792 return copy_siginfo_from_user(kinfo, info);
3793}
3794
3795static struct pid *pidfd_to_pid(const struct file *file)
3796{
3797 struct pid *pid;
3798
3799 pid = pidfd_pid(file);
3800 if (!IS_ERR(pid))
3801 return pid;
3802
3803 return tgid_pidfd_to_pid(file);
3804}
3805
3806/**
3807 * sys_pidfd_send_signal - Signal a process through a pidfd
3808 * @pidfd: file descriptor of the process
3809 * @sig: signal to send
3810 * @info: signal info
3811 * @flags: future flags
3812 *
3813 * The syscall currently only signals via PIDTYPE_PID which covers
3814 * kill(<positive-pid>, <signal>. It does not signal threads or process
3815 * groups.
3816 * In order to extend the syscall to threads and process groups the @flags
3817 * argument should be used. In essence, the @flags argument will determine
3818 * what is signaled and not the file descriptor itself. Put in other words,
3819 * grouping is a property of the flags argument not a property of the file
3820 * descriptor.
3821 *
3822 * Return: 0 on success, negative errno on failure
3823 */
3824SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
3825 siginfo_t __user *, info, unsigned int, flags)
3826{
3827 int ret;
3828 struct fd f;
3829 struct pid *pid;
3830 kernel_siginfo_t kinfo;
3831
3832 /* Enforce flags be set to 0 until we add an extension. */
3833 if (flags)
3834 return -EINVAL;
3835
3836 f = fdget(pidfd);
3837 if (!f.file)
3838 return -EBADF;
3839
3840 /* Is this a pidfd? */
3841 pid = pidfd_to_pid(f.file);
3842 if (IS_ERR(pid)) {
3843 ret = PTR_ERR(pid);
3844 goto err;
3845 }
3846
3847 ret = -EINVAL;
3848 if (!access_pidfd_pidns(pid))
3849 goto err;
3850
3851 if (info) {
3852 ret = copy_siginfo_from_user_any(&kinfo, info);
3853 if (unlikely(ret))
3854 goto err;
3855
3856 ret = -EINVAL;
3857 if (unlikely(sig != kinfo.si_signo))
3858 goto err;
3859
3860 /* Only allow sending arbitrary signals to yourself. */
3861 ret = -EPERM;
3862 if ((task_pid(current) != pid) &&
3863 (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
3864 goto err;
3865 } else {
3866 prepare_kill_siginfo(sig, &kinfo);
3867 }
3868
3869 ret = kill_pid_info(sig, &kinfo, pid);
3870
3871err:
3872 fdput(f);
3873 return ret;
3874}
3875
3876static int
3877do_send_specific(pid_t tgid, pid_t pid, int sig, struct kernel_siginfo *info)
3878{
3879 struct task_struct *p;
3880 int error = -ESRCH;
3881
3882 rcu_read_lock();
3883 p = find_task_by_vpid(pid);
3884 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
3885 error = check_kill_permission(sig, info, p);
3886 /*
3887 * The null signal is a permissions and process existence
3888 * probe. No signal is actually delivered.
3889 */
3890 if (!error && sig) {
3891 error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
3892 /*
3893 * If lock_task_sighand() failed we pretend the task
3894 * dies after receiving the signal. The window is tiny,
3895 * and the signal is private anyway.
3896 */
3897 if (unlikely(error == -ESRCH))
3898 error = 0;
3899 }
3900 }
3901 rcu_read_unlock();
3902
3903 return error;
3904}
3905
3906static int do_tkill(pid_t tgid, pid_t pid, int sig)
3907{
3908 struct kernel_siginfo info;
3909
3910 clear_siginfo(&info);
3911 info.si_signo = sig;
3912 info.si_errno = 0;
3913 info.si_code = SI_TKILL;
3914 info.si_pid = task_tgid_vnr(current);
3915 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3916
3917 return do_send_specific(tgid, pid, sig, &info);
3918}
3919
3920/**
3921 * sys_tgkill - send signal to one specific thread
3922 * @tgid: the thread group ID of the thread
3923 * @pid: the PID of the thread
3924 * @sig: signal to be sent
3925 *
3926 * This syscall also checks the @tgid and returns -ESRCH even if the PID
3927 * exists but it's not belonging to the target process anymore. This
3928 * method solves the problem of threads exiting and PIDs getting reused.
3929 */
3930SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
3931{
3932 /* This is only valid for single tasks */
3933 if (pid <= 0 || tgid <= 0)
3934 return -EINVAL;
3935
3936 return do_tkill(tgid, pid, sig);
3937}
3938
3939/**
3940 * sys_tkill - send signal to one specific task
3941 * @pid: the PID of the task
3942 * @sig: signal to be sent
3943 *
3944 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3945 */
3946SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
3947{
3948 /* This is only valid for single tasks */
3949 if (pid <= 0)
3950 return -EINVAL;
3951
3952 return do_tkill(0, pid, sig);
3953}
3954
3955static int do_rt_sigqueueinfo(pid_t pid, int sig, kernel_siginfo_t *info)
3956{
3957 /* Not even root can pretend to send signals from the kernel.
3958 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3959 */
3960 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3961 (task_pid_vnr(current) != pid))
3962 return -EPERM;
3963
3964 /* POSIX.1b doesn't mention process groups. */
3965 return kill_proc_info(sig, info, pid);
3966}
3967
3968/**
3969 * sys_rt_sigqueueinfo - send signal information to a signal
3970 * @pid: the PID of the thread
3971 * @sig: signal to be sent
3972 * @uinfo: signal info to be sent
3973 */
3974SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3975 siginfo_t __user *, uinfo)
3976{
3977 kernel_siginfo_t info;
3978 int ret = __copy_siginfo_from_user(sig, &info, uinfo);
3979 if (unlikely(ret))
3980 return ret;
3981 return do_rt_sigqueueinfo(pid, sig, &info);
3982}
3983
3984#ifdef CONFIG_COMPAT
3985COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3986 compat_pid_t, pid,
3987 int, sig,
3988 struct compat_siginfo __user *, uinfo)
3989{
3990 kernel_siginfo_t info;
3991 int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
3992 if (unlikely(ret))
3993 return ret;
3994 return do_rt_sigqueueinfo(pid, sig, &info);
3995}
3996#endif
3997
3998static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, kernel_siginfo_t *info)
3999{
4000 /* This is only valid for single tasks */
4001 if (pid <= 0 || tgid <= 0)
4002 return -EINVAL;
4003
4004 /* Not even root can pretend to send signals from the kernel.
4005 * Nor can they impersonate a kill()/tgkill(), which adds source info.
4006 */
4007 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
4008 (task_pid_vnr(current) != pid))
4009 return -EPERM;
4010
4011 return do_send_specific(tgid, pid, sig, info);
4012}
4013
4014SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
4015 siginfo_t __user *, uinfo)
4016{
4017 kernel_siginfo_t info;
4018 int ret = __copy_siginfo_from_user(sig, &info, uinfo);
4019 if (unlikely(ret))
4020 return ret;
4021 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
4022}
4023
4024#ifdef CONFIG_COMPAT
4025COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
4026 compat_pid_t, tgid,
4027 compat_pid_t, pid,
4028 int, sig,
4029 struct compat_siginfo __user *, uinfo)
4030{
4031 kernel_siginfo_t info;
4032 int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
4033 if (unlikely(ret))
4034 return ret;
4035 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
4036}
4037#endif
4038
4039/*
4040 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
4041 */
4042void kernel_sigaction(int sig, __sighandler_t action)
4043{
4044 spin_lock_irq(&current->sighand->siglock);
4045 current->sighand->action[sig - 1].sa.sa_handler = action;
4046 if (action == SIG_IGN) {
4047 sigset_t mask;
4048
4049 sigemptyset(&mask);
4050 sigaddset(&mask, sig);
4051
4052 flush_sigqueue_mask(&mask, &current->signal->shared_pending);
4053 flush_sigqueue_mask(&mask, &current->pending);
4054 recalc_sigpending();
4055 }
4056 spin_unlock_irq(&current->sighand->siglock);
4057}
4058EXPORT_SYMBOL(kernel_sigaction);
4059
4060void __weak sigaction_compat_abi(struct k_sigaction *act,
4061 struct k_sigaction *oact)
4062{
4063}
4064
4065int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
4066{
4067 struct task_struct *p = current, *t;
4068 struct k_sigaction *k;
4069 sigset_t mask;
4070
4071 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
4072 return -EINVAL;
4073
4074 k = &p->sighand->action[sig-1];
4075
4076 spin_lock_irq(&p->sighand->siglock);
4077 if (oact)
4078 *oact = *k;
4079
4080 sigaction_compat_abi(act, oact);
4081
4082 if (act) {
4083 sigdelsetmask(&act->sa.sa_mask,
4084 sigmask(SIGKILL) | sigmask(SIGSTOP));
4085 *k = *act;
4086 /*
4087 * POSIX 3.3.1.3:
4088 * "Setting a signal action to SIG_IGN for a signal that is
4089 * pending shall cause the pending signal to be discarded,
4090 * whether or not it is blocked."
4091 *
4092 * "Setting a signal action to SIG_DFL for a signal that is
4093 * pending and whose default action is to ignore the signal
4094 * (for example, SIGCHLD), shall cause the pending signal to
4095 * be discarded, whether or not it is blocked"
4096 */
4097 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
4098 sigemptyset(&mask);
4099 sigaddset(&mask, sig);
4100 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
4101 for_each_thread(p, t)
4102 flush_sigqueue_mask(&mask, &t->pending);
4103 }
4104 }
4105
4106 spin_unlock_irq(&p->sighand->siglock);
4107 return 0;
4108}
4109
4110static int
4111do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
4112 size_t min_ss_size)
4113{
4114 struct task_struct *t = current;
4115
4116 if (oss) {
4117 memset(oss, 0, sizeof(stack_t));
4118 oss->ss_sp = (void __user *) t->sas_ss_sp;
4119 oss->ss_size = t->sas_ss_size;
4120 oss->ss_flags = sas_ss_flags(sp) |
4121 (current->sas_ss_flags & SS_FLAG_BITS);
4122 }
4123
4124 if (ss) {
4125 void __user *ss_sp = ss->ss_sp;
4126 size_t ss_size = ss->ss_size;
4127 unsigned ss_flags = ss->ss_flags;
4128 int ss_mode;
4129
4130 if (unlikely(on_sig_stack(sp)))
4131 return -EPERM;
4132
4133 ss_mode = ss_flags & ~SS_FLAG_BITS;
4134 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
4135 ss_mode != 0))
4136 return -EINVAL;
4137
4138 if (ss_mode == SS_DISABLE) {
4139 ss_size = 0;
4140 ss_sp = NULL;
4141 } else {
4142 if (unlikely(ss_size < min_ss_size))
4143 return -ENOMEM;
4144 }
4145
4146 t->sas_ss_sp = (unsigned long) ss_sp;
4147 t->sas_ss_size = ss_size;
4148 t->sas_ss_flags = ss_flags;
4149 }
4150 return 0;
4151}
4152
4153SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
4154{
4155 stack_t new, old;
4156 int err;
4157 if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
4158 return -EFAULT;
4159 err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
4160 current_user_stack_pointer(),
4161 MINSIGSTKSZ);
4162 if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
4163 err = -EFAULT;
4164 return err;
4165}
4166
4167int restore_altstack(const stack_t __user *uss)
4168{
4169 stack_t new;
4170 if (copy_from_user(&new, uss, sizeof(stack_t)))
4171 return -EFAULT;
4172 (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
4173 MINSIGSTKSZ);
4174 /* squash all but EFAULT for now */
4175 return 0;
4176}
4177
4178int __save_altstack(stack_t __user *uss, unsigned long sp)
4179{
4180 struct task_struct *t = current;
4181 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
4182 __put_user(t->sas_ss_flags, &uss->ss_flags) |
4183 __put_user(t->sas_ss_size, &uss->ss_size);
4184 if (err)
4185 return err;
4186 if (t->sas_ss_flags & SS_AUTODISARM)
4187 sas_ss_reset(t);
4188 return 0;
4189}
4190
4191#ifdef CONFIG_COMPAT
4192static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
4193 compat_stack_t __user *uoss_ptr)
4194{
4195 stack_t uss, uoss;
4196 int ret;
4197
4198 if (uss_ptr) {
4199 compat_stack_t uss32;
4200 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
4201 return -EFAULT;
4202 uss.ss_sp = compat_ptr(uss32.ss_sp);
4203 uss.ss_flags = uss32.ss_flags;
4204 uss.ss_size = uss32.ss_size;
4205 }
4206 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
4207 compat_user_stack_pointer(),
4208 COMPAT_MINSIGSTKSZ);
4209 if (ret >= 0 && uoss_ptr) {
4210 compat_stack_t old;
4211 memset(&old, 0, sizeof(old));
4212 old.ss_sp = ptr_to_compat(uoss.ss_sp);
4213 old.ss_flags = uoss.ss_flags;
4214 old.ss_size = uoss.ss_size;
4215 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
4216 ret = -EFAULT;
4217 }
4218 return ret;
4219}
4220
4221COMPAT_SYSCALL_DEFINE2(sigaltstack,
4222 const compat_stack_t __user *, uss_ptr,
4223 compat_stack_t __user *, uoss_ptr)
4224{
4225 return do_compat_sigaltstack(uss_ptr, uoss_ptr);
4226}
4227
4228int compat_restore_altstack(const compat_stack_t __user *uss)
4229{
4230 int err = do_compat_sigaltstack(uss, NULL);
4231 /* squash all but -EFAULT for now */
4232 return err == -EFAULT ? err : 0;
4233}
4234
4235int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
4236{
4237 int err;
4238 struct task_struct *t = current;
4239 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
4240 &uss->ss_sp) |
4241 __put_user(t->sas_ss_flags, &uss->ss_flags) |
4242 __put_user(t->sas_ss_size, &uss->ss_size);
4243 if (err)
4244 return err;
4245 if (t->sas_ss_flags & SS_AUTODISARM)
4246 sas_ss_reset(t);
4247 return 0;
4248}
4249#endif
4250
4251#ifdef __ARCH_WANT_SYS_SIGPENDING
4252
4253/**
4254 * sys_sigpending - examine pending signals
4255 * @uset: where mask of pending signal is returned
4256 */
4257SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
4258{
4259 sigset_t set;
4260
4261 if (sizeof(old_sigset_t) > sizeof(*uset))
4262 return -EINVAL;
4263
4264 do_sigpending(&set);
4265
4266 if (copy_to_user(uset, &set, sizeof(old_sigset_t)))
4267 return -EFAULT;
4268
4269 return 0;
4270}
4271
4272#ifdef CONFIG_COMPAT
4273COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
4274{
4275 sigset_t set;
4276
4277 do_sigpending(&set);
4278
4279 return put_user(set.sig[0], set32);
4280}
4281#endif
4282
4283#endif
4284
4285#ifdef __ARCH_WANT_SYS_SIGPROCMASK
4286/**
4287 * sys_sigprocmask - examine and change blocked signals
4288 * @how: whether to add, remove, or set signals
4289 * @nset: signals to add or remove (if non-null)
4290 * @oset: previous value of signal mask if non-null
4291 *
4292 * Some platforms have their own version with special arguments;
4293 * others support only sys_rt_sigprocmask.
4294 */
4295
4296SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
4297 old_sigset_t __user *, oset)
4298{
4299 old_sigset_t old_set, new_set;
4300 sigset_t new_blocked;
4301
4302 old_set = current->blocked.sig[0];
4303
4304 if (nset) {
4305 if (copy_from_user(&new_set, nset, sizeof(*nset)))
4306 return -EFAULT;
4307
4308 new_blocked = current->blocked;
4309
4310 switch (how) {
4311 case SIG_BLOCK:
4312 sigaddsetmask(&new_blocked, new_set);
4313 break;
4314 case SIG_UNBLOCK:
4315 sigdelsetmask(&new_blocked, new_set);
4316 break;
4317 case SIG_SETMASK:
4318 new_blocked.sig[0] = new_set;
4319 break;
4320 default:
4321 return -EINVAL;
4322 }
4323
4324 set_current_blocked(&new_blocked);
4325 }
4326
4327 if (oset) {
4328 if (copy_to_user(oset, &old_set, sizeof(*oset)))
4329 return -EFAULT;
4330 }
4331
4332 return 0;
4333}
4334#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
4335
4336#ifndef CONFIG_ODD_RT_SIGACTION
4337/**
4338 * sys_rt_sigaction - alter an action taken by a process
4339 * @sig: signal to be sent
4340 * @act: new sigaction
4341 * @oact: used to save the previous sigaction
4342 * @sigsetsize: size of sigset_t type
4343 */
4344SYSCALL_DEFINE4(rt_sigaction, int, sig,
4345 const struct sigaction __user *, act,
4346 struct sigaction __user *, oact,
4347 size_t, sigsetsize)
4348{
4349 struct k_sigaction new_sa, old_sa;
4350 int ret;
4351
4352 /* XXX: Don't preclude handling different sized sigset_t's. */
4353 if (sigsetsize != sizeof(sigset_t))
4354 return -EINVAL;
4355
4356 if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
4357 return -EFAULT;
4358
4359 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
4360 if (ret)
4361 return ret;
4362
4363 if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
4364 return -EFAULT;
4365
4366 return 0;
4367}
4368#ifdef CONFIG_COMPAT
4369COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
4370 const struct compat_sigaction __user *, act,
4371 struct compat_sigaction __user *, oact,
4372 compat_size_t, sigsetsize)
4373{
4374 struct k_sigaction new_ka, old_ka;
4375#ifdef __ARCH_HAS_SA_RESTORER
4376 compat_uptr_t restorer;
4377#endif
4378 int ret;
4379
4380 /* XXX: Don't preclude handling different sized sigset_t's. */
4381 if (sigsetsize != sizeof(compat_sigset_t))
4382 return -EINVAL;
4383
4384 if (act) {
4385 compat_uptr_t handler;
4386 ret = get_user(handler, &act->sa_handler);
4387 new_ka.sa.sa_handler = compat_ptr(handler);
4388#ifdef __ARCH_HAS_SA_RESTORER
4389 ret |= get_user(restorer, &act->sa_restorer);
4390 new_ka.sa.sa_restorer = compat_ptr(restorer);
4391#endif
4392 ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
4393 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
4394 if (ret)
4395 return -EFAULT;
4396 }
4397
4398 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4399 if (!ret && oact) {
4400 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
4401 &oact->sa_handler);
4402 ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
4403 sizeof(oact->sa_mask));
4404 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
4405#ifdef __ARCH_HAS_SA_RESTORER
4406 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
4407 &oact->sa_restorer);
4408#endif
4409 }
4410 return ret;
4411}
4412#endif
4413#endif /* !CONFIG_ODD_RT_SIGACTION */
4414
4415#ifdef CONFIG_OLD_SIGACTION
4416SYSCALL_DEFINE3(sigaction, int, sig,
4417 const struct old_sigaction __user *, act,
4418 struct old_sigaction __user *, oact)
4419{
4420 struct k_sigaction new_ka, old_ka;
4421 int ret;
4422
4423 if (act) {
4424 old_sigset_t mask;
4425 if (!access_ok(act, sizeof(*act)) ||
4426 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
4427 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
4428 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
4429 __get_user(mask, &act->sa_mask))
4430 return -EFAULT;
4431#ifdef __ARCH_HAS_KA_RESTORER
4432 new_ka.ka_restorer = NULL;
4433#endif
4434 siginitset(&new_ka.sa.sa_mask, mask);
4435 }
4436
4437 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4438
4439 if (!ret && oact) {
4440 if (!access_ok(oact, sizeof(*oact)) ||
4441 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
4442 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
4443 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
4444 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
4445 return -EFAULT;
4446 }
4447
4448 return ret;
4449}
4450#endif
4451#ifdef CONFIG_COMPAT_OLD_SIGACTION
4452COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
4453 const struct compat_old_sigaction __user *, act,
4454 struct compat_old_sigaction __user *, oact)
4455{
4456 struct k_sigaction new_ka, old_ka;
4457 int ret;
4458 compat_old_sigset_t mask;
4459 compat_uptr_t handler, restorer;
4460
4461 if (act) {
4462 if (!access_ok(act, sizeof(*act)) ||
4463 __get_user(handler, &act->sa_handler) ||
4464 __get_user(restorer, &act->sa_restorer) ||
4465 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
4466 __get_user(mask, &act->sa_mask))
4467 return -EFAULT;
4468
4469#ifdef __ARCH_HAS_KA_RESTORER
4470 new_ka.ka_restorer = NULL;
4471#endif
4472 new_ka.sa.sa_handler = compat_ptr(handler);
4473 new_ka.sa.sa_restorer = compat_ptr(restorer);
4474 siginitset(&new_ka.sa.sa_mask, mask);
4475 }
4476
4477 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4478
4479 if (!ret && oact) {
4480 if (!access_ok(oact, sizeof(*oact)) ||
4481 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
4482 &oact->sa_handler) ||
4483 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
4484 &oact->sa_restorer) ||
4485 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
4486 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
4487 return -EFAULT;
4488 }
4489 return ret;
4490}
4491#endif
4492
4493#ifdef CONFIG_SGETMASK_SYSCALL
4494
4495/*
4496 * For backwards compatibility. Functionality superseded by sigprocmask.
4497 */
4498SYSCALL_DEFINE0(sgetmask)
4499{
4500 /* SMP safe */
4501 return current->blocked.sig[0];
4502}
4503
4504SYSCALL_DEFINE1(ssetmask, int, newmask)
4505{
4506 int old = current->blocked.sig[0];
4507 sigset_t newset;
4508
4509 siginitset(&newset, newmask);
4510 set_current_blocked(&newset);
4511
4512 return old;
4513}
4514#endif /* CONFIG_SGETMASK_SYSCALL */
4515
4516#ifdef __ARCH_WANT_SYS_SIGNAL
4517/*
4518 * For backwards compatibility. Functionality superseded by sigaction.
4519 */
4520SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
4521{
4522 struct k_sigaction new_sa, old_sa;
4523 int ret;
4524
4525 new_sa.sa.sa_handler = handler;
4526 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
4527 sigemptyset(&new_sa.sa.sa_mask);
4528
4529 ret = do_sigaction(sig, &new_sa, &old_sa);
4530
4531 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
4532}
4533#endif /* __ARCH_WANT_SYS_SIGNAL */
4534
4535#ifdef __ARCH_WANT_SYS_PAUSE
4536
4537SYSCALL_DEFINE0(pause)
4538{
4539 while (!signal_pending(current)) {
4540 __set_current_state(TASK_INTERRUPTIBLE);
4541 schedule();
4542 }
4543 return -ERESTARTNOHAND;
4544}
4545
4546#endif
4547
4548static int sigsuspend(sigset_t *set)
4549{
4550 current->saved_sigmask = current->blocked;
4551 set_current_blocked(set);
4552
4553 while (!signal_pending(current)) {
4554 __set_current_state(TASK_INTERRUPTIBLE);
4555 schedule();
4556 }
4557 set_restore_sigmask();
4558 return -ERESTARTNOHAND;
4559}
4560
4561/**
4562 * sys_rt_sigsuspend - replace the signal mask for a value with the
4563 * @unewset value until a signal is received
4564 * @unewset: new signal mask value
4565 * @sigsetsize: size of sigset_t type
4566 */
4567SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
4568{
4569 sigset_t newset;
4570
4571 /* XXX: Don't preclude handling different sized sigset_t's. */
4572 if (sigsetsize != sizeof(sigset_t))
4573 return -EINVAL;
4574
4575 if (copy_from_user(&newset, unewset, sizeof(newset)))
4576 return -EFAULT;
4577 return sigsuspend(&newset);
4578}
4579
4580#ifdef CONFIG_COMPAT
4581COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
4582{
4583 sigset_t newset;
4584
4585 /* XXX: Don't preclude handling different sized sigset_t's. */
4586 if (sigsetsize != sizeof(sigset_t))
4587 return -EINVAL;
4588
4589 if (get_compat_sigset(&newset, unewset))
4590 return -EFAULT;
4591 return sigsuspend(&newset);
4592}
4593#endif
4594
4595#ifdef CONFIG_OLD_SIGSUSPEND
4596SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
4597{
4598 sigset_t blocked;
4599 siginitset(&blocked, mask);
4600 return sigsuspend(&blocked);
4601}
4602#endif
4603#ifdef CONFIG_OLD_SIGSUSPEND3
4604SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
4605{
4606 sigset_t blocked;
4607 siginitset(&blocked, mask);
4608 return sigsuspend(&blocked);
4609}
4610#endif
4611
4612__weak const char *arch_vma_name(struct vm_area_struct *vma)
4613{
4614 return NULL;
4615}
4616
4617static inline void siginfo_buildtime_checks(void)
4618{
4619 BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
4620
4621 /* Verify the offsets in the two siginfos match */
4622#define CHECK_OFFSET(field) \
4623 BUILD_BUG_ON(offsetof(siginfo_t, field) != offsetof(kernel_siginfo_t, field))
4624
4625 /* kill */
4626 CHECK_OFFSET(si_pid);
4627 CHECK_OFFSET(si_uid);
4628
4629 /* timer */
4630 CHECK_OFFSET(si_tid);
4631 CHECK_OFFSET(si_overrun);
4632 CHECK_OFFSET(si_value);
4633
4634 /* rt */
4635 CHECK_OFFSET(si_pid);
4636 CHECK_OFFSET(si_uid);
4637 CHECK_OFFSET(si_value);
4638
4639 /* sigchld */
4640 CHECK_OFFSET(si_pid);
4641 CHECK_OFFSET(si_uid);
4642 CHECK_OFFSET(si_status);
4643 CHECK_OFFSET(si_utime);
4644 CHECK_OFFSET(si_stime);
4645
4646 /* sigfault */
4647 CHECK_OFFSET(si_addr);
4648 CHECK_OFFSET(si_addr_lsb);
4649 CHECK_OFFSET(si_lower);
4650 CHECK_OFFSET(si_upper);
4651 CHECK_OFFSET(si_pkey);
4652
4653 /* sigpoll */
4654 CHECK_OFFSET(si_band);
4655 CHECK_OFFSET(si_fd);
4656
4657 /* sigsys */
4658 CHECK_OFFSET(si_call_addr);
4659 CHECK_OFFSET(si_syscall);
4660 CHECK_OFFSET(si_arch);
4661#undef CHECK_OFFSET
4662
4663 /* usb asyncio */
4664 BUILD_BUG_ON(offsetof(struct siginfo, si_pid) !=
4665 offsetof(struct siginfo, si_addr));
4666 if (sizeof(int) == sizeof(void __user *)) {
4667 BUILD_BUG_ON(sizeof_field(struct siginfo, si_pid) !=
4668 sizeof(void __user *));
4669 } else {
4670 BUILD_BUG_ON((sizeof_field(struct siginfo, si_pid) +
4671 sizeof_field(struct siginfo, si_uid)) !=
4672 sizeof(void __user *));
4673 BUILD_BUG_ON(offsetofend(struct siginfo, si_pid) !=
4674 offsetof(struct siginfo, si_uid));
4675 }
4676#ifdef CONFIG_COMPAT
4677 BUILD_BUG_ON(offsetof(struct compat_siginfo, si_pid) !=
4678 offsetof(struct compat_siginfo, si_addr));
4679 BUILD_BUG_ON(sizeof_field(struct compat_siginfo, si_pid) !=
4680 sizeof(compat_uptr_t));
4681 BUILD_BUG_ON(sizeof_field(struct compat_siginfo, si_pid) !=
4682 sizeof_field(struct siginfo, si_pid));
4683#endif
4684}
4685
4686void __init signals_init(void)
4687{
4688 siginfo_buildtime_checks();
4689
4690 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
4691}
4692
4693#ifdef CONFIG_KGDB_KDB
4694#include <linux/kdb.h>
4695/*
4696 * kdb_send_sig - Allows kdb to send signals without exposing
4697 * signal internals. This function checks if the required locks are
4698 * available before calling the main signal code, to avoid kdb
4699 * deadlocks.
4700 */
4701void kdb_send_sig(struct task_struct *t, int sig)
4702{
4703 static struct task_struct *kdb_prev_t;
4704 int new_t, ret;
4705 if (!spin_trylock(&t->sighand->siglock)) {
4706 kdb_printf("Can't do kill command now.\n"
4707 "The sigmask lock is held somewhere else in "
4708 "kernel, try again later\n");
4709 return;
4710 }
4711 new_t = kdb_prev_t != t;
4712 kdb_prev_t = t;
4713 if (t->state != TASK_RUNNING && new_t) {
4714 spin_unlock(&t->sighand->siglock);
4715 kdb_printf("Process is not RUNNING, sending a signal from "
4716 "kdb risks deadlock\n"
4717 "on the run queue locks. "
4718 "The signal has _not_ been sent.\n"
4719 "Reissue the kill command if you want to risk "
4720 "the deadlock.\n");
4721 return;
4722 }
4723 ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
4724 spin_unlock(&t->sighand->siglock);
4725 if (ret)
4726 kdb_printf("Fail to deliver Signal %d to process %d.\n",
4727 sig, t->pid);
4728 else
4729 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
4730}
4731#endif /* CONFIG_KGDB_KDB */