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