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