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