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