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