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