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