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