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