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