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