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