blob: 7e59ebc2c25e669ef60f54d6e3ae6839c34d2f2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010016#include <linux/sched/mm.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010017#include <linux/sched/user.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010018#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010019#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010020#include <linux/sched/task_stack.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010021#include <linux/sched/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/fs.h>
23#include <linux/tty.h>
24#include <linux/binfmts.h>
Alex Kelly179899f2012-10-04 17:15:24 -070025#include <linux/coredump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/security.h>
27#include <linux/syscalls.h>
28#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070030#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090031#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070032#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080033#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080034#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080035#include <linux/pid_namespace.h>
36#include <linux/nsproxy.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080037#include <linux/user_namespace.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053038#include <linux/uprobes.h>
Al Viro90268432012-12-14 14:47:53 -050039#include <linux/compat.h>
Jesper Derehag2b5faa42013-03-19 20:50:05 +000040#include <linux/cn_proc.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070041#include <linux/compiler.h>
42
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050043#define CREATE_TRACE_POINTS
44#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/param.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/unistd.h>
49#include <asm/siginfo.h>
David Howellsd550bbd2012-03-28 18:30:03 +010050#include <asm/cacheflush.h>
Al Viroe1396062006-05-25 10:19:47 -040051#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * SLAB caches for signal bits.
55 */
56
Christoph Lametere18b8902006-12-06 20:33:20 -080057static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090059int print_fatal_signals __read_mostly;
60
Roland McGrath35de2542008-07-25 19:45:51 -070061static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070062{
Roland McGrath35de2542008-07-25 19:45:51 -070063 return t->sighand->action[sig - 1].sa.sa_handler;
64}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070065
Roland McGrath35de2542008-07-25 19:45:51 -070066static int sig_handler_ignored(void __user *handler, int sig)
67{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070068 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070069 return handler == SIG_IGN ||
70 (handler == SIG_DFL && sig_kernel_ignore(sig));
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070073static int sig_task_ignored(struct task_struct *t, int sig, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Roland McGrath35de2542008-07-25 19:45:51 -070075 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Oleg Nesterovf008faf2009-04-02 16:58:02 -070077 handler = sig_handler(t, sig);
78
79 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070080 handler == SIG_DFL && !force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070081 return 1;
82
83 return sig_handler_ignored(handler, sig);
84}
85
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070086static int sig_ignored(struct task_struct *t, int sig, bool force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070087{
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /*
89 * Blocked signals are never ignored, since the
90 * signal handler may change by the time it is
91 * unblocked.
92 */
Roland McGrath325d22d2007-11-12 15:41:55 -080093 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070096 if (!sig_task_ignored(t, sig, force))
Roland McGrath35de2542008-07-25 19:45:51 -070097 return 0;
98
99 /*
100 * Tracers may want to know about even ignored signals.
101 */
Tejun Heoa288eec2011-06-17 16:50:37 +0200102 return !t->ptrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * Re-calculate pending state from the set of locally pending
107 * signals, globally pending signals, and blocked signals.
108 */
109static 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 McGrath7bb44ad2007-05-23 13:57:44 -0700137static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200139 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700141 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700143 return 1;
144 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700145 /*
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 McGrath7bb44ad2007-05-23 13:57:44 -0700150 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 */
157void recalc_sigpending_and_wake(struct task_struct *t)
158{
159 if (recalc_sigpending_tsk(t))
160 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163void recalc_sigpending(void)
164{
Tejun Heodd1d6772011-06-02 11:14:00 +0200165 if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700166 clear_thread_flag(TIF_SIGPENDING);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/* Given the mask, find the first available signal that should be serviced. */
171
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800172#define SYNCHRONOUS_MASK \
173 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
Will Drewrya0727e82012-04-12 16:48:00 -0500174 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800175
Davide Libenzifba2afa2007-05-10 22:23:13 -0700176int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unsigned long i, *s, *m, x;
179 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 s = pending->signal.sig;
182 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800183
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 Torvalds1da177e2005-04-16 15:20:36 -0700196 switch (_NSIG_WORDS) {
197 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800198 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 Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800207 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 Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return sig;
220}
221
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900222static 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 Xiaoqiang747800e2016-05-23 16:23:59 -0700232 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900233 current->comm, current->pid, sig);
234}
235
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100236/**
Tejun Heo7dd3db52011-06-02 11:14:00 +0200237 * 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 Dabbeltb76808e2015-04-30 21:19:57 -0700253bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heo7dd3db52011-06-02 11:14:00 +0200254{
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 Heoa8f072c2011-06-02 11:13:59 +0200270 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100271 * @task: target task
272 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200273 * 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 Heod79fdd62011-03-23 10:37:00 +0100277 *
278 * CONTEXT:
279 * Must be called with @task->sighand->siglock held.
280 */
Tejun Heo73ddff22011-06-14 11:20:14 +0200281void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100282{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200283 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
284 task->jobctl &= ~JOBCTL_TRAPPING;
Oleg Nesterov650226b2014-06-06 14:36:44 -0700285 smp_mb(); /* advised by wake_up_bit() */
Tejun Heo62c124f2011-06-02 11:14:00 +0200286 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
Tejun Heod79fdd62011-03-23 10:37:00 +0100287 }
288}
289
290/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200291 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100292 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200293 * @mask: pending bits to clear
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100294 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200295 * 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 Heoe5c1902e2011-03-23 10:37:00 +0100298 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200299 * If clearing of @mask leaves no stop or trap pending, this function calls
300 * task_clear_jobctl_trapping().
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100301 *
302 * CONTEXT:
303 * Must be called with @task->sighand->siglock held.
304 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700305void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100306{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200307 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 Heo6dfca322011-06-02 11:14:00 +0200313
314 if (!(task->jobctl & JOBCTL_PENDING_MASK))
315 task_clear_jobctl_trapping(task);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100316}
317
318/**
319 * task_participate_group_stop - participate in a group stop
320 * @task: task participating in a group stop
321 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200322 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100323 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200324 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100325 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100326 *
327 * CONTEXT:
328 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100329 *
330 * RETURNS:
331 * %true if group stop completion should be notified to the parent, %false
332 * otherwise.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100333 */
334static bool task_participate_group_stop(struct task_struct *task)
335{
336 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200337 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100338
Tejun Heoa8f072c2011-06-02 11:13:59 +0200339 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100340
Tejun Heo3759a0d2011-06-02 11:14:00 +0200341 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100342
343 if (!consume)
344 return false;
345
346 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
347 sig->group_stop_count--;
348
Tejun Heo244056f2011-03-23 10:37:01 +0100349 /*
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 Iles2d39b3c2017-01-10 16:57:54 -0800354 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100355 return true;
356 }
357 return false;
358}
359
David Howellsc69e8d92008-11-14 10:39:19 +1100360/*
361 * allocate a new signal queue record
362 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700363 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100364 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900365static struct sigqueue *
366__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800369 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800371 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000372 * Protect access to @t credentials. This can go away when all
373 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800374 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000375 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100376 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800377 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000378 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800381 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800382 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900384 } else {
385 print_dropped_signal(sig);
386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800389 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100390 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 } else {
392 INIT_LIST_HEAD(&q->list);
393 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100394 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
David Howellsd84f4f92008-11-14 10:39:23 +1100396
397 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Andrew Morton514a01b2006-02-03 03:04:41 -0800400static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
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 Nesterov6a14c5c2006-03-28 16:11:18 -0800409void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
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 Nesterov9e7c8f82015-06-04 16:22:16 -0400422 * Flush all pending signals for this kthread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800424void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 unsigned long flags;
427
428 spin_lock_irqsave(&t->sighand->siglock, flags);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400429 clear_tsk_thread_flag(t, TIF_SIGPENDING);
430 flush_sigqueue(&t->pending);
431 flush_sigqueue(&t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_unlock_irqrestore(&t->sighand->siglock, flags);
433}
434
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500435#ifdef CONFIG_POSIX_TIMERS
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400436static 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
459void 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 Pitrebaa73d92016-11-11 00:10:10 -0500469#endif
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400470
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700471void 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 Torvalds1da177e2005-04-16 15:20:36 -0700481/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * Flush all handlers for a task.
483 */
484
485void
486flush_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 Morton522cff12013-03-13 14:59:34 -0700494#ifdef __ARCH_HAS_SA_RESTORER
Kees Cook2ca39522013-03-13 14:59:33 -0700495 ka->sa.sa_restorer = NULL;
496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 sigemptyset(&ka->sa.sa_mask);
498 ka++;
499 }
500}
501
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200502int unhandled_signal(struct task_struct *tsk, int sig)
503{
Roland McGrath445a91d2008-07-25 19:45:52 -0700504 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700505 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200506 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700507 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200508 return 0;
Tejun Heoa288eec2011-06-17 16:50:37 +0200509 /* if ptraced, let the tracer determine */
510 return !tsk->ptrace;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200511}
512
Oleg Nesterov100360f2008-07-25 01:47:29 -0700513static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
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 Nesterovd4434202008-07-25 01:47:28 -0700523 if (first)
524 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 first = q;
526 }
527 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700528
529 sigdelset(&list->signal, sig);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700532still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 list_del_init(&first->list);
534 copy_siginfo(info, &first->info);
535 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700537 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 info->si_signo = sig;
543 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800544 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 info->si_pid = 0;
546 info->si_uid = 0;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
551 siginfo_t *info)
552{
Roland McGrath27d91e02006-09-29 02:00:31 -0700553 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Oleg Nesterov2e01fab2015-11-06 16:32:19 -0800555 if (sig)
Oleg Nesterov100360f2008-07-25 01:47:29 -0700556 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return sig;
558}
559
560/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700561 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * expected to free it.
563 *
564 * All callers have to hold the siglock.
565 */
566int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
567{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700568 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000569
570 /* We only dequeue private signals from ourselves, we don't let
571 * signalfd steal them
572 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700573 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800574 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 signr = __dequeue_signal(&tsk->signal->shared_pending,
576 mask, info);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500577#ifdef CONFIG_POSIX_TIMERS
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800578 /*
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 Dunlap5aba0852011-04-04 14:59:31 -0700584 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800585 * 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 Gleixner2456e852016-12-25 11:38:40 +0100595 tsk->signal->it_real_incr != 0) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800596 hrtimer_forward(tmr, tmr->base->get_time(),
597 tsk->signal->it_real_incr);
598 hrtimer_restart(tmr);
599 }
600 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500601#endif
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800602 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700603
Davide Libenzib8fceee2007-09-20 12:40:16 -0700604 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700605 if (!signr)
606 return 0;
607
608 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800609 /*
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 Heoa8f072c2011-06-02 11:13:59 +0200621 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800622 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500623#ifdef CONFIG_POSIX_TIMERS
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700624 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /*
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 Pitrebaa73d92016-11-11 00:10:10 -0500635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 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 Nesterov910ffdb2013-01-21 20:47:41 +0100650void signal_wake_up_state(struct task_struct *t, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 set_tsk_thread_flag(t, TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100654 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500655 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * 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 Nesterov910ffdb2013-01-21 20:47:41 +0100660 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 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 Anzinger71fabd52006-01-08 01:02:48 -0800669 */
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700670static int flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
George Anzinger71fabd52006-01-08 01:02:48 -0800671{
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 Nesterov702a5072011-04-27 22:01:27 +0200679 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800680 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 Torvalds1da177e2005-04-16 15:20:36 -0700688
Oleg Nesterov614c5172009-12-15 16:47:22 -0800689static inline int is_si_special(const struct siginfo *info)
690{
691 return info <= SEND_SIG_FORCED;
692}
693
694static 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 Torvalds1da177e2005-04-16 15:20:36 -0700700/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700701 * called with RCU read lock from check_kill_permission()
702 */
703static 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. Biederman5af66202012-03-03 20:21:47 -0800708 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. Hallyn39fd3392011-03-23 16:43:19 -0700712 return 1;
713
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800714 if (ns_capable(tcred->user_ns, CAP_KILL))
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700715 return 1;
716
717 return 0;
718}
719
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100722 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
724static int check_kill_permission(int sig, struct siginfo *info,
725 struct task_struct *t)
726{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700727 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700728 int error;
729
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700730 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700731 return -EINVAL;
732
Oleg Nesterov614c5172009-12-15 16:47:22 -0800733 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700734 return 0;
735
736 error = audit_signal_info(sig, t); /* Let audit system see the signal */
737 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400739
Oleg Nesterov065add32010-05-26 14:42:54 -0700740 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700741 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700742 switch (sig) {
743 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700744 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700745 /*
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 Grubbc2f0c7c2005-05-06 12:38:39 +0100755
Amy Griffise54dc242007-03-29 18:01:04 -0400756 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Tejun Heofb1d9102011-06-14 11:20:17 +0200759/**
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 Heo544b2c92011-06-14 11:20:18 +0200767 * 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 Heofb1d9102011-06-14 11:20:17 +0200772 *
773 * CONTEXT:
774 * Must be called with @task->sighand->siglock held.
775 */
776static 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 Nesterov910ffdb2013-01-21 20:47:41 +0100782 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700786 * Handle magic process-wide effects of stop/continue signals. Unlike
787 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 * time regardless of blocking, ignoring, or handling. This does the
789 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700790 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Oleg Nesterov403bad72013-04-30 15:28:10 -0700795static bool prepare_signal(int sig, struct task_struct *p, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700797 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct task_struct *t;
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700799 sigset_t flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Oleg Nesterov403bad72013-04-30 15:28:10 -0700801 if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
Oleg Nesterov5fa534c2015-11-06 16:32:31 -0800802 if (!(signal->flags & SIGNAL_GROUP_EXIT))
Oleg Nesterov403bad72013-04-30 15:28:10 -0700803 return sig == SIGKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700805 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700807 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * This is a stop signal. Remove SIGCONT from all queues.
810 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700811 siginitset(&flush, sigmask(SIGCONT));
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700812 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700813 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700814 flush_sigqueue_mask(&flush, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700816 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200818 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700820 siginitset(&flush, SIG_KERNEL_STOP_MASK);
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700821 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700822 for_each_thread(p, t) {
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700823 flush_sigqueue_mask(&flush, &t->pending);
Tejun Heo3759a0d2011-06-02 11:14:00 +0200824 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200825 if (likely(!(t->ptrace & PT_SEIZED)))
826 wake_up_state(t, __TASK_STOPPED);
827 else
828 ptrace_trap_notify(t);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700831 /*
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 Nesterovad16a4602008-04-30 00:52:46 -0700840 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700841 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700842 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700843 why |= SIGNAL_CLD_STOPPED;
844
845 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700846 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700847 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700848 * will take ->siglock, notice SIGNAL_CLD_MASK, and
849 * notify its parent. See get_signal_to_deliver().
850 */
Jamie Iles2d39b3c2017-01-10 16:57:54 -0800851 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700852 signal->group_stop_count = 0;
853 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700856
Oleg Nesterovdef8cf72012-03-23 15:02:45 -0700857 return !sig_ignored(p, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700860/*
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 */
868static 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 Nesterov5fcd8352008-04-30 00:52:55 -0700881static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700882{
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 Nesterov5fcd8352008-04-30 00:52:55 -0700894 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700895 /*
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 Nesterovfae5fa42008-04-30 00:53:03 -0700922 if (sig_fatal(p, sig) &&
923 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700924 !sigismember(&t->real_blocked, sig) &&
Tejun Heoa288eec2011-06-17 16:50:37 +0200925 (sig == SIGKILL || !t->ptrace)) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700926 /*
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 Heo6dfca322011-06-02 11:14:00 +0200941 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700942 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 Emelyanovaf7fff92008-04-30 00:52:34 -0700957static inline int legacy_queue(struct sigpending *signals, int sig)
958{
959 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
960}
961
Serge E. Hallyn6b550f92012-01-10 15:11:37 -0800962#ifdef CONFIG_USER_NS
963static 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. Biederman078de5f2012-02-08 07:00:08 -0800971 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. Hallyn6b550f92012-01-10 15:11:37 -0800975}
976#else
977static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
978{
979 return;
980}
981#endif
982
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700983static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
984 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700986 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700987 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200988 int override_rlimit;
Oleg Nesterov6c303d32011-11-22 21:13:48 +0100989 int ret = 0, result;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400990
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700991 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700992
Oleg Nesterov6c303d32011-11-22 21:13:48 +0100993 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterov629d3622012-03-23 15:02:44 -0700994 if (!prepare_signal(sig, t,
995 from_ancestor_ns || (info == SEND_SIG_FORCED)))
Oleg Nesterov6c303d32011-11-22 21:13:48 +0100996 goto ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700997
998 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001000 * 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 Nesterov6c303d32011-11-22 21:13:48 +01001004 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001005 if (legacy_queue(pending, sig))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001006 goto ret;
1007
1008 result = TRACE_SIGNAL_DELIVERED;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001009 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * fast-pathed signals for kernel-internal things like SIGSTOP
1011 * or SIGKILL.
1012 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001013 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto out_set;
1015
Randy Dunlap5aba0852011-04-04 14:59:31 -07001016 /*
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 Nossum7a0aeb12009-05-16 11:28:33 +02001025 if (sig < SIGRTMIN)
1026 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1027 else
1028 override_rlimit = 0;
1029
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001030 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001031 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001033 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001035 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 q->info.si_signo = sig;
1037 q->info.si_errno = 0;
1038 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001039 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001040 task_active_pid_ns(t));
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001041 q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001043 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 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 Bhattiprolu6588c1e2009-04-02 16:58:09 -07001052 if (from_ancestor_ns)
1053 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 break;
1055 }
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001056
1057 userns_fixup_signal_uid(&q->info, t);
1058
Oleg Nesterov621d3122005-10-30 15:03:45 -08001059 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001060 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 Nesterov6c303d32011-11-22 21:13:48 +01001066 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1067 ret = -EAGAIN;
1068 goto ret;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001069 } else {
1070 /*
1071 * This is a silent loss of information. We still
1072 * send the signal, but the *info bits are lost.
1073 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001074 result = TRACE_SIGNAL_LOSE_INFO;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001079 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001080 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001081 complete_signal(sig, t, group);
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001082ret:
1083 trace_signal_generate(sig, info, t, group, result);
1084 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001087static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1088 int group)
1089{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001090 int from_ancestor_ns = 0;
1091
1092#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001093 from_ancestor_ns = si_fromuser(info) &&
1094 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001095#endif
1096
1097 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001098}
1099
Al Viro4aaefee2012-11-05 13:09:56 -05001100static void print_fatal_signal(int signr)
Ingo Molnar45807a12007-07-15 23:40:10 -07001101{
Al Viro4aaefee2012-11-05 13:09:56 -05001102 struct pt_regs *regs = signal_pt_regs();
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001103 pr_info("potentially unexpected fatal signal %d.\n", signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001104
Al Viroca5cd872007-10-29 04:31:16 +00001105#if defined(__i386__) && !defined(__arch_um__)
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001106 pr_info("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001107 {
1108 int i;
1109 for (i = 0; i < 16; i++) {
1110 unsigned char insn;
1111
Andi Kleenb45c6e72010-01-08 14:42:52 -08001112 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1113 break;
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001114 pr_cont("%02x ", insn);
Ingo Molnar45807a12007-07-15 23:40:10 -07001115 }
1116 }
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001117 pr_cont("\n");
Ingo Molnar45807a12007-07-15 23:40:10 -07001118#endif
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001119 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001120 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001121 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001122}
1123
1124static 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 Torvalds1da177e2005-04-16 15:20:36 -07001132
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001133int
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 Torvalds1da177e2005-04-16 15:20:36 -07001139static int
1140specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1141{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001142 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001145int 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 Torvalds1da177e2005-04-16 15:20:36 -07001159/*
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 Torvaldsae74c3b2006-08-02 20:17:49 -07001162 *
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 Nesterov80fe7282008-04-30 00:53:05 -07001167 * We don't want to have recursive SIGSEGV's etc, for example,
1168 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170int
1171force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1172{
1173 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001174 int ret, blocked, ignored;
1175 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001178 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 McGrath7bb44ad2007-05-23 13:57:44 -07001185 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001188 if (action->sa.sa_handler == SIG_DFL)
1189 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ret = specific_send_sig_info(sig, info, t);
1191 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1192
1193 return ret;
1194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/*
1197 * Nuke all other threads in the group.
1198 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001199int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001201 struct task_struct *t = p;
1202 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 p->signal->group_stop_count = 0;
1205
Oleg Nesterov09faef12010-05-26 14:43:11 -07001206 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001207 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001208 count++;
1209
1210 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (t->exit_state)
1212 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 signal_wake_up(t, 1);
1215 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001216
1217 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001220struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1221 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001222{
1223 struct sighand_struct *sighand;
1224
1225 for (;;) {
Paul E. McKenneyc41247e2014-05-05 08:18:30 -07001226 /*
1227 * Disable interrupts early to avoid deadlocks.
1228 * See rcu_read_unlock() comment header for details.
1229 */
Paul E. McKenneya8417962011-07-19 03:25:36 -07001230 local_irq_save(*flags);
1231 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001232 sighand = rcu_dereference(tsk->sighand);
Paul E. McKenneya8417962011-07-19 03:25:36 -07001233 if (unlikely(sighand == NULL)) {
1234 rcu_read_unlock();
1235 local_irq_restore(*flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001236 break;
Paul E. McKenneya8417962011-07-19 03:25:36 -07001237 }
Oleg Nesterov392809b2014-09-28 23:44:18 +02001238 /*
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. McKenneya8417962011-07-19 03:25:36 -07001249 spin_lock(&sighand->siglock);
1250 if (likely(sighand == tsk->sighand)) {
1251 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001252 break;
Paul E. McKenneya8417962011-07-19 03:25:36 -07001253 }
1254 spin_unlock(&sighand->siglock);
1255 rcu_read_unlock();
1256 local_irq_restore(*flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001257 }
1258
1259 return sighand;
1260}
1261
David Howellsc69e8d92008-11-14 10:39:19 +11001262/*
1263 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1266{
David Howells694f6902010-08-04 16:59:14 +01001267 int ret;
1268
1269 rcu_read_lock();
1270 ret = check_kill_permission(sig, info, p);
1271 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001273 if (!ret && sig)
1274 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 return ret;
1277}
1278
1279/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001280 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001282 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001284int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 struct task_struct *p = NULL;
1287 int retval, success;
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 success = 0;
1290 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001291 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int err = group_send_sig_info(sig, info, p);
1293 success |= !err;
1294 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001295 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return success ? 0 : retval;
1297}
1298
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001299int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001301 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct task_struct *p;
1303
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001304 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 Nesterov6ca25b52008-04-30 00:52:45 -07001312
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001313 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Randy Dunlap5aba0852011-04-04 14:59:31 -07001321int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001322{
1323 int error;
1324 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001325 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001326 rcu_read_unlock();
1327 return error;
1328}
1329
Serge Hallynd178bc32011-09-26 10:45:18 -05001330static 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. Biederman5af66202012-03-03 20:21:47 -08001334 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 Hallynd178bc32011-09-26 10:45:18 -05001336 return 0;
1337 return 1;
1338}
1339
Eric W. Biederman2425c082006-10-02 02:17:28 -07001340/* like kill_pid_info(), but doesn't use uid/euid of "current" */
Serge Hallynd178bc32011-09-26 10:45:18 -05001341int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1342 const struct cred *cred, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001343{
1344 int ret = -EINVAL;
1345 struct task_struct *p;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001346 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001347
1348 if (!valid_signal(sig))
1349 return ret;
1350
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001351 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001352 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001353 if (!p) {
1354 ret = -ESRCH;
1355 goto out_unlock;
1356 }
Serge Hallynd178bc32011-09-26 10:45:18 -05001357 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
Harald Welte46113832005-10-10 19:44:29 +02001358 ret = -EPERM;
1359 goto out_unlock;
1360 }
David Quigley8f95dc52006-06-30 01:55:47 -07001361 ret = security_task_kill(p, info, sig, secid);
1362 if (ret)
1363 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001364
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 Welte46113832005-10-10 19:44:29 +02001371 }
1372out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001373 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001374 return ret;
1375}
Serge Hallynd178bc32011-09-26 10:45:18 -05001376EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
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 Padovanbc64efd2008-07-25 01:47:33 -07001385static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001387 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001388
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 Torvalds1da177e2005-04-16 15:20:36 -07001401 int retval = 0, count = 0;
1402 struct task_struct * p;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001405 if (task_pid_vnr(p) > 1 &&
1406 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int err = group_send_sig_info(sig, info, p);
1408 ++count;
1409 if (err != -EPERM)
1410 retval = err;
1411 }
1412 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001413 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001415 read_unlock(&tasklist_lock);
1416
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420/*
1421 * These are for backward compatibility with the rest of the kernel source.
1422 */
1423
Randy Dunlap5aba0852011-04-04 14:59:31 -07001424int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /*
1427 * Make sure legacy kernel users don't send in bad values
1428 * (normal paths check this in check_kill_permission).
1429 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001430 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -EINVAL;
1432
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001433 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001436#define __si_special(priv) \
1437 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439int
1440send_sig(int sig, struct task_struct *p, int priv)
1441{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001442 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445void
1446force_sig(int sig, struct task_struct *p)
1447{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001448 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
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 */
1457int
1458force_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. Biedermanc4b92fc2006-10-02 02:17:10 -07001470int kill_pgrp(struct pid *pid, int sig, int priv)
1471{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001472 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. Biedermanc4b92fc2006-10-02 02:17:10 -07001479}
1480EXPORT_SYMBOL(kill_pgrp);
1481
1482int kill_pid(struct pid *pid, int sig, int priv)
1483{
1484 return kill_pid_info(sig, __si_special(priv), pid);
1485}
1486EXPORT_SYMBOL(kill_pid);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488/*
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 Dunlap5aba0852011-04-04 14:59:31 -07001492 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001497struct sigqueue *sigqueue_alloc(void)
1498{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001499 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001501 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001503
1504 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507void sigqueue_free(struct sigqueue *q)
1508{
1509 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001510 spinlock_t *lock = &current->sighand->siglock;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1513 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001514 * We must hold ->siglock while testing q->list
1515 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001516 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001518 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001519 q->flags &= ~SIGQUEUE_PREALLOC;
1520 /*
1521 * If it is queued it will be freed when dequeued,
1522 * like the "regular" sigqueue.
1523 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001524 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001525 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001526 spin_unlock_irqrestore(lock, flags);
1527
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001528 if (q)
1529 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001532int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001533{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001534 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001535 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001536 unsigned long flags;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001537 int ret, result;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001538
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001539 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001540
1541 ret = -1;
1542 if (!likely(lock_task_sighand(t, &flags)))
1543 goto ret;
1544
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001545 ret = 1; /* the signal is ignored */
Oleg Nesterov163566f2011-11-22 21:37:41 +01001546 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterovdef8cf72012-03-23 15:02:45 -07001547 if (!prepare_signal(sig, t, false))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001548 goto out;
1549
1550 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001551 if (unlikely(!list_empty(&q->list))) {
1552 /*
1553 * If an SI_TIMER entry is already queue just increment
1554 * the overrun count.
1555 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001556 BUG_ON(q->info.si_code != SI_TIMER);
1557 q->info.si_overrun++;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001558 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001559 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001560 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001561 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001562
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001563 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001564 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001565 list_add_tail(&q->list, &pending->list);
1566 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001567 complete_signal(sig, t, group);
Oleg Nesterov163566f2011-11-22 21:37:41 +01001568 result = TRACE_SIGNAL_DELIVERED;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001569out:
Oleg Nesterov163566f2011-11-22 21:37:41 +01001570 trace_signal_generate(sig, &q->info, t, group, result);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001571 unlock_task_sighand(t, &flags);
1572ret:
1573 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * Let a parent know about the death of a child.
1578 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001579 *
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001580 * Returns true if our parent ignored us and so we've switched to
1581 * self-reaping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001583bool do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct siginfo info;
1586 unsigned long flags;
1587 struct sighand_struct *psig;
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001588 bool autoreap = false;
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001589 u64 utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 BUG_ON(sig == -1);
1592
1593 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001594 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Tejun Heod21142e2011-06-17 16:50:34 +02001596 BUG_ON(!tsk->ptrace &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1598
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01001599 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 Torvalds1da177e2005-04-16 15:20:36 -07001608 info.si_signo = sig;
1609 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001610 /*
Eric W. Biederman32084502012-05-31 16:26:39 -07001611 * We are under tasklist_lock here so our parent is tied to
1612 * us and cannot change.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001613 *
Eric W. Biederman32084502012-05-31 16:26:39 -07001614 * task_active_pid_ns will always return the same pid namespace
1615 * until a task passes through release_task.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001616 *
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. Biederman32084502012-05-31 16:26:39 -07001622 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07001623 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1624 task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001625 rcu_read_unlock();
1626
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001627 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 Torvalds1da177e2005-04-16 15:20:36 -07001630
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 Heod21142e2011-06-17 16:50:34 +02001643 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 (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 Nesterov53c8f9f2011-06-22 23:08:18 +02001661 autoreap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001663 sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001665 if (valid_signal(sig) && sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 __group_send_sig_info(sig, &info, tsk->parent);
1667 __wake_up_parent(tsk, tsk->parent);
1668 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001669
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001670 return autoreap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Tejun Heo75b95952011-03-23 10:37:01 +01001673/**
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 */
1686static void do_notify_parent_cldstop(struct task_struct *tsk,
1687 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct siginfo info;
1690 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001691 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct sighand_struct *sighand;
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001693 u64 utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Tejun Heo75b95952011-03-23 10:37:01 +01001695 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001696 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001697 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001698 tsk = tsk->group_leader;
1699 parent = tsk->real_parent;
1700 }
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 info.si_signo = SIGCHLD;
1703 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001704 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001705 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001706 */
1707 rcu_read_lock();
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08001708 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07001709 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001710 rcu_read_unlock();
1711
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001712 task_cputime(tsk, &utime, &stime);
1713 info.si_utime = nsec_to_clock_t(utime);
1714 info.si_stime = nsec_to_clock_t(stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
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 Nesterovd5f70c02006-06-26 00:26:07 -07001743static inline int may_ptrace_stop(void)
1744{
Tejun Heod21142e2011-06-17 16:50:34 +02001745 if (!likely(current->ptrace))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001746 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001747 /*
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 Nesterov999d9fc2008-07-25 01:47:41 -07001753 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001754 * is safe to enter schedule().
Oleg Nesterov9899d112013-01-21 20:48:00 +01001755 *
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 Nesterovd5f70c02006-06-26 00:26:07 -07001759 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001760 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001761 unlikely(current->mm == current->parent->mm))
1762 return 0;
1763
1764 return 1;
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001768 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001769 * Called with the siglock held.
1770 */
1771static int sigkill_pending(struct task_struct *tsk)
1772{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001773 return sigismember(&tsk->pending.signal, SIGKILL) ||
1774 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001775}
1776
1777/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * 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 Nesterov20686a32008-02-08 04:19:03 -08001785 * If we actually decide not to stop at all because the tracer
1786 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001788static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001789 __releases(&current->sighand->siglock)
1790 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001792 bool gstop_done = false;
1793
Roland McGrath1a669c22008-02-06 01:37:37 -08001794 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(&current->sighand->siglock);
1807 arch_ptrace_stop(exit_code, info);
1808 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001809 if (sigkill_pending(current))
1810 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02001814 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001819 */
Tejun Heo81be24b2011-06-02 11:13:59 +02001820 set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 current->last_siginfo = info;
1823 current->exit_code = exit_code;
1824
Tejun Heod79fdd62011-03-23 10:37:00 +01001825 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 * 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 Heo73ddff22011-06-14 11:20:14 +02001828 * 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 Heod79fdd62011-03-23 10:37:00 +01001831 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001832 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 gstop_done = task_participate_group_stop(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001834
Tejun Heofb1d9102011-06-14 11:20:17 +02001835 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
Tejun Heo73ddff22011-06-14 11:20:14 +02001836 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
Tejun Heofb1d9102011-06-14 11:20:17 +02001837 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1838 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
Tejun Heo73ddff22011-06-14 11:20:14 +02001839
Tejun Heo81be24b2011-06-02 11:13:59 +02001840 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001841 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 spin_unlock_irq(&current->sighand->siglock);
1844 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001845 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001846 /*
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 Nesterovbb3696d2011-06-24 17:34:23 +02001857 if (gstop_done && ptrace_reparented(current))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001858 do_notify_parent_cldstop(current, false, why);
1859
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001860 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001867 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001868 preempt_enable_no_resched();
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02001869 freezable_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 } else {
1871 /*
1872 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001873 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001874 *
1875 * If @gstop_done, the ptracer went away between group stop
1876 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001877 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001880 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001881 if (gstop_done)
1882 do_notify_parent_cldstop(current, false, why);
1883
Oleg Nesterov9899d112013-01-21 20:48:00 +01001884 /* tasklist protects us from ptrace_freeze_traced() */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001885 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001886 if (clear_code)
1887 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001888 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
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(&current->sighand->siglock);
1897 current->last_siginfo = NULL;
1898
Tejun Heo544b2c92011-06-14 11:20:18 +02001899 /* LISTENING can be set only during STOP traps, clear it */
1900 current->jobctl &= ~JOBCTL_LISTENING;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
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 McGrathb74d0de2007-06-06 03:59:00 -07001905 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001907 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Tejun Heo3544d722011-06-14 11:20:15 +02001910static void ptrace_do_notify(int signr, int exit_code, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 siginfo_t info;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 memset(&info, 0, sizeof info);
Tejun Heo3544d722011-06-14 11:20:15 +02001915 info.si_signo = signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001917 info.si_pid = task_pid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001918 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* Let the debugger run. */
Tejun Heo3544d722011-06-14 11:20:15 +02001921 ptrace_stop(exit_code, why, 1, &info);
1922}
1923
1924void ptrace_notify(int exit_code)
1925{
1926 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02001927 if (unlikely(current->task_works))
1928 task_work_run();
Tejun Heo3544d722011-06-14 11:20:15 +02001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 spin_lock_irq(&current->sighand->siglock);
Tejun Heo3544d722011-06-14 11:20:15 +02001931 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 spin_unlock_irq(&current->sighand->siglock);
1933}
1934
Tejun Heo73ddff22011-06-14 11:20:14 +02001935/**
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 Torvalds1da177e2005-04-16 15:20:36 -07001956 */
Tejun Heo73ddff22011-06-14 11:20:14 +02001957static bool do_signal_stop(int signr)
1958 __releases(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Tejun Heoa8f072c2011-06-02 11:13:59 +02001962 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07001963 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001964 struct task_struct *t;
1965
Tejun Heoa8f072c2011-06-02 11:13:59 +02001966 /* signr will be recorded in task->jobctl for retries */
1967 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01001968
Tejun Heoa8f072c2011-06-02 11:13:59 +02001969 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001970 unlikely(signal_group_exit(sig)))
Tejun Heo73ddff22011-06-14 11:20:14 +02001971 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001973 * 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 Nesterov780006eac2011-04-01 20:12:16 +02001980 * cause two group stops when !ptraced. That is why we
1981 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001982 *
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 Torvalds1da177e2005-04-16 15:20:36 -07001990 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001991 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1992 sig->group_exit_code = signr;
Oleg Nesterova122b342006-03-28 16:11:22 -08001993
Tejun Heo7dd3db52011-06-02 11:14:00 +02001994 sig->group_stop_count = 0;
1995
1996 if (task_set_jobctl_pending(current, signr | gstop))
1997 sig->group_stop_count++;
1998
Oleg Nesterov8d38f202014-01-23 15:55:56 -08001999 t = current;
2000 while_each_thread(current, t) {
Oleg Nesterova122b342006-03-28 16:11:22 -08002001 /*
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 Heo7dd3db52011-06-02 11:14:00 +02002006 if (!task_is_stopped(t) &&
2007 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002008 sig->group_stop_count++;
Tejun Heofb1d9102011-06-14 11:20:17 +02002009 if (likely(!(t->ptrace & PT_SEIZED)))
2010 signal_wake_up(t, 0);
2011 else
2012 ptrace_trap_notify(t);
Oleg Nesterova122b342006-03-28 16:11:22 -08002013 }
Tejun Heod79fdd62011-03-23 10:37:00 +01002014 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002015 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002016
Tejun Heod21142e2011-06-17 16:50:34 +02002017 if (likely(!current->ptrace)) {
Tejun Heo5224fa32011-03-23 10:37:00 +01002018 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 McGrathae6d2ed2009-09-23 15:56:53 -07002028 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01002029 spin_unlock_irq(&current->sighand->siglock);
2030
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002031 /*
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 Heo5224fa32011-03-23 10:37:00 +01002040 if (notify) {
2041 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002042 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01002043 read_unlock(&tasklist_lock);
2044 }
2045
2046 /* Now we don't run again until woken by SIGCONT or SIGKILL */
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002047 freezable_schedule();
Tejun Heo73ddff22011-06-14 11:20:14 +02002048 return true;
Tejun Heod79fdd62011-03-23 10:37:00 +01002049 } else {
Tejun Heo73ddff22011-06-14 11:20:14 +02002050 /*
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 McGrathae6d2ed2009-09-23 15:56:53 -07002056 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002057}
Tejun Heod79fdd62011-03-23 10:37:00 +01002058
Tejun Heo73ddff22011-06-14 11:20:14 +02002059/**
2060 * do_jobctl_trap - take care of ptrace jobctl traps
2061 *
Tejun Heo3544d722011-06-14 11:20:15 +02002062 * 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 Heo73ddff22011-06-14 11:20:14 +02002069 *
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 */
2074static void do_jobctl_trap(void)
2075{
Tejun Heo3544d722011-06-14 11:20:15 +02002076 struct signal_struct *signal = current->signal;
Tejun Heo73ddff22011-06-14 11:20:14 +02002077 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
Tejun Heod79fdd62011-03-23 10:37:00 +01002078
Tejun Heo3544d722011-06-14 11:20:15 +02002079 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 McGrathae6d2ed2009-09-23 15:56:53 -07002089 current->exit_code = 0;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Al Viro94eb22d2012-11-05 13:08:06 -05002093static int ptrace_signal(int signr, siginfo_t *info)
Roland McGrath18c98b62008-04-17 18:44:38 -07002094{
Al Virob7f95912012-11-05 13:06:22 -05002095 ptrace_signal_deliver();
Oleg Nesterov8a352412011-07-21 17:06:53 +02002096 /*
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 Heofe1bc6a2011-03-23 10:37:00 +01002106 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002107
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 Dunlap5aba0852011-04-04 14:59:31 -07002115 /*
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 McGrath18c98b62008-04-17 18:44:38 -07002121 if (signr != info->si_signo) {
2122 info->si_signo = signr;
2123 info->si_errno = 0;
2124 info->si_code = SI_USER;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002125 rcu_read_lock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002126 info->si_pid = task_pid_vnr(current->parent);
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07002127 info->si_uid = from_kuid_munged(current_user_ns(),
2128 task_uid(current->parent));
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002129 rcu_read_unlock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002130 }
2131
2132 /* If the (new) signal is now blocked, requeue it. */
2133 if (sigismember(&current->blocked, signr)) {
2134 specific_send_sig_info(signr, info, current);
2135 signr = 0;
2136 }
2137
2138 return signr;
2139}
2140
Richard Weinberger828b1f62013-10-07 15:26:57 +02002141int get_signal(struct ksignal *ksig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002143 struct sighand_struct *sighand = current->sighand;
2144 struct signal_struct *signal = current->signal;
2145 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02002147 if (unlikely(current->task_works))
2148 task_work_run();
Al Viro72667022012-07-15 14:10:52 +04002149
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302150 if (unlikely(uprobe_deny_signal()))
2151 return 0;
2152
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002153 /*
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002154 * 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 McGrath13b1c3d2008-03-03 20:22:05 -08002157 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002158 try_to_freeze();
2159
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002160relock:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002161 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002162 /*
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 Nesterovf6b76d42008-04-30 00:52:47 -07002167 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heoc672af32011-03-23 10:36:59 +01002168 int why;
2169
2170 if (signal->flags & SIGNAL_CLD_CONTINUED)
2171 why = CLD_CONTINUED;
2172 else
2173 why = CLD_STOPPED;
2174
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002175 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002176
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002177 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002178
Tejun Heoceb6bd62011-03-23 10:37:01 +01002179 /*
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 Heoedf2ed12011-03-23 10:37:00 +01002187 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002188 do_notify_parent_cldstop(current, false, why);
2189
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02002190 if (ptrace_reparented(current->group_leader))
2191 do_notify_parent_cldstop(current->group_leader,
2192 true, why);
Tejun Heoedf2ed12011-03-23 10:37:00 +01002193 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002194
Oleg Nesterove4420552008-04-30 00:52:44 -07002195 goto relock;
2196 }
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 for (;;) {
2199 struct k_sigaction *ka;
Tejun Heodd1d6772011-06-02 11:14:00 +02002200
2201 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2202 do_signal_stop(0))
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002203 goto relock;
Oleg Nesterov1be53962009-12-15 16:47:26 -08002204
Tejun Heo73ddff22011-06-14 11:20:14 +02002205 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2206 do_jobctl_trap();
2207 spin_unlock_irq(&sighand->siglock);
2208 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210
Richard Weinberger828b1f62013-10-07 15:26:57 +02002211 signr = dequeue_signal(current, &current->blocked, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Tejun Heodd1d6772011-06-02 11:14:00 +02002213 if (!signr)
2214 break; /* will return 0 */
2215
Oleg Nesterov8a352412011-07-21 17:06:53 +02002216 if (unlikely(current->ptrace) && signr != SIGKILL) {
Richard Weinberger828b1f62013-10-07 15:26:57 +02002217 signr = ptrace_signal(signr, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (!signr)
Tejun Heodd1d6772011-06-02 11:14:00 +02002219 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221
Tejun Heodd1d6772011-06-02 11:14:00 +02002222 ka = &sighand->action[signr-1];
2223
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002224 /* Trace actually delivered signals. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002225 trace_signal_deliver(signr, &ksig->info, ka);
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2228 continue;
2229 if (ka->sa.sa_handler != SIG_DFL) {
2230 /* Run the handler. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002231 ksig->ka = *ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
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 Bhattiprolu84d73782006-12-08 02:38:01 -08002245 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002246 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002247 * 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 Bhattiprolu84d73782006-12-08 02:38:01 -08002254 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002255 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002256 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 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 Nesterovf6b76d42008-04-30 00:52:47 -07002271 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* signals can be posted during this window */
2274
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002275 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto relock;
2277
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002278 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280
Richard Weinberger828b1f62013-10-07 15:26:57 +02002281 if (likely(do_signal_stop(ksig->info.si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 /* 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 Nesterovf6b76d42008-04-30 00:52:47 -07002293 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /*
2296 * Anything else is fatal, maybe with a core dump.
2297 */
2298 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002301 if (print_fatal_signals)
Richard Weinberger828b1f62013-10-07 15:26:57 +02002302 print_fatal_signal(ksig->info.si_signo);
Jesper Derehag2b5faa42013-03-19 20:50:05 +00002303 proc_coredump_connector(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /*
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 Weinberger828b1f62013-10-07 15:26:57 +02002312 do_coredump(&ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314
2315 /*
2316 * Death signals, no core dump.
2317 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002318 do_group_exit(ksig->info.si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /* NOTREACHED */
2320 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002321 spin_unlock_irq(&sighand->siglock);
Richard Weinberger828b1f62013-10-07 15:26:57 +02002322
2323 ksig->sig = signr;
2324 return ksig->sig > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Matt Fleming5e6292c2012-01-10 15:11:17 -08002327/**
Al Viroefee9842012-04-28 02:04:15 -04002328 * signal_delivered -
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002329 * @ksig: kernel signal struct
Al Viroefee9842012-04-28 02:04:15 -04002330 * @stepping: nonzero if debugger single-step or block-step in use
Matt Fleming5e6292c2012-01-10 15:11:17 -08002331 *
Masanari Iidae2278672014-02-18 22:54:36 +09002332 * This function should be called when a signal has successfully been
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002333 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
Al Viroefee9842012-04-28 02:04:15 -04002334 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002335 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
Matt Fleming5e6292c2012-01-10 15:11:17 -08002336 */
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002337static void signal_delivered(struct ksignal *ksig, int stepping)
Matt Fleming5e6292c2012-01-10 15:11:17 -08002338{
2339 sigset_t blocked;
2340
Al Viroa610d6e2012-05-21 23:42:15 -04002341 /* 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 Weinberger10b1c7a2014-07-13 13:36:04 +02002347 sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
2348 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2349 sigaddset(&blocked, ksig->sig);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002350 set_current_blocked(&blocked);
Richard Weinbergerdf5601f2013-10-07 15:37:19 +02002351 tracehook_signal_handler(stepping);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002352}
2353
Al Viro2ce5da12012-11-07 15:11:25 -05002354void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2355{
2356 if (failed)
2357 force_sigsegv(ksig->sig, current);
2358 else
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002359 signal_delivered(ksig, stepping);
Al Viro2ce5da12012-11-07 15:11:25 -05002360}
2361
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002362/*
2363 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002364 * group-wide signal. Other threads should be notified now to take
2365 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002366 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002367static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002368{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002369 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002370 struct task_struct *t;
2371
Oleg Nesterovf646e222011-04-27 19:18:39 +02002372 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2373 if (sigisemptyset(&retarget))
2374 return;
2375
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002376 t = tsk;
2377 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002378 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 Nesterov0edceb7bc2011-04-27 19:17:37 +02002391 }
2392}
2393
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002394void exit_signals(struct task_struct *tsk)
2395{
2396 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002397 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002398
Tejun Heo77e4ef92011-12-12 18:12:21 -08002399 /*
2400 * @tsk is about to have PF_EXITING set - lock out users which
2401 * expect stable threadgroup.
2402 */
Ingo Molnar780de9d2017-02-02 11:50:56 +01002403 cgroup_threadgroup_change_begin(tsk);
Tejun Heo77e4ef92011-12-12 18:12:21 -08002404
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002405 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2406 tsk->flags |= PF_EXITING;
Ingo Molnar780de9d2017-02-02 11:50:56 +01002407 cgroup_threadgroup_change_end(tsk);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002408 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002409 }
2410
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002411 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002412 /*
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 Heo77e4ef92011-12-12 18:12:21 -08002417
Ingo Molnar780de9d2017-02-02 11:50:56 +01002418 cgroup_threadgroup_change_end(tsk);
Tejun Heo77e4ef92011-12-12 18:12:21 -08002419
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002420 if (!signal_pending(tsk))
2421 goto out;
2422
Oleg Nesterovf646e222011-04-27 19:18:39 +02002423 unblocked = tsk->blocked;
2424 signotset(&unblocked);
2425 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002426
Tejun Heoa8f072c2011-06-02 11:13:59 +02002427 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c1902e2011-03-23 10:37:00 +01002428 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002429 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002430out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002431 spin_unlock_irq(&tsk->sighand->siglock);
2432
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002433 /*
2434 * If group stop has completed, deliver the notification. This
2435 * should always go to the real parent of the group leader.
2436 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002437 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002438 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002439 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002440 read_unlock(&tasklist_lock);
2441 }
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444EXPORT_SYMBOL(recalc_sigpending);
2445EXPORT_SYMBOL_GPL(dequeue_signal);
2446EXPORT_SYMBOL(flush_signals);
2447EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448EXPORT_SYMBOL(send_sig);
2449EXPORT_SYMBOL(send_sig_info);
2450EXPORT_SYMBOL(sigprocmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452/*
2453 * System call entry points.
2454 */
2455
Randy Dunlap41c57892011-04-04 15:00:26 -07002456/**
2457 * sys_restart_syscall - restart a system call
2458 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002459SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002461 struct restart_block *restart = &current->restart_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return restart->fn(restart);
2463}
2464
2465long do_no_restart_syscall(struct restart_block *param)
2466{
2467 return -EINTR;
2468}
2469
Oleg Nesterovb1828012011-04-27 21:56:14 +02002470static 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 Nesterov702a5072011-04-27 22:01:27 +02002475 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002476 retarget_shared_pending(tsk, &newblocked);
2477 }
2478 tsk->blocked = *newset;
2479 recalc_sigpending();
2480}
2481
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002482/**
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 Torvalds1da177e2005-04-16 15:20:36 -07002488 */
Al Viro77097ae2012-04-27 13:58:59 -04002489void set_current_blocked(sigset_t *newset)
2490{
Al Viro77097ae2012-04-27 13:58:59 -04002491 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01002492 __set_current_blocked(newset);
Al Viro77097ae2012-04-27 13:58:59 -04002493}
2494
2495void __set_current_blocked(const sigset_t *newset)
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002496{
2497 struct task_struct *tsk = current;
2498
Waiman Longc7be96a2016-12-14 15:04:10 -08002499 /*
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 Nesterove6fa16a2011-04-27 20:59:41 +02002506 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002507 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002508 spin_unlock_irq(&tsk->sighand->siglock);
2509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
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 */
2519int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2520{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002521 struct task_struct *tsk = current;
2522 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002524 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002525 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002526 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 switch (how) {
2529 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002530 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 break;
2532 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002533 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 break;
2535 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002536 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 break;
2538 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002541
Al Viro77097ae2012-04-27 13:58:59 -04002542 __set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
Randy Dunlap41c57892011-04-04 15:00:26 -07002546/**
2547 * sys_rt_sigprocmask - change the list of currently blocked signals
2548 * @how: whether to add, remove, or set signals
Randy Dunlapada9c932011-06-14 15:50:11 -07002549 * @nset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002550 * @oset: previous value of signal mask if non-null
2551 * @sigsetsize: size of sigset_t type
2552 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002553SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002554 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002557 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /* XXX: Don't preclude handling different sized sigset_t's. */
2560 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002563 old_set = current->blocked;
2564
2565 if (nset) {
2566 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2567 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2569
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002570 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002572 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002574
2575 if (oset) {
2576 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2577 return -EFAULT;
2578 }
2579
2580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Al Viro322a56c2012-12-25 13:32:58 -05002583#ifdef CONFIG_COMPAT
Al Viro322a56c2012-12-25 13:32:58 -05002584COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2585 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Al Viro322a56c2012-12-25 13:32:58 -05002587#ifdef __BIG_ENDIAN
2588 sigset_t old_set = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Al Viro322a56c2012-12-25 13:32:58 -05002590 /* 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 Virodb61ec22013-03-02 20:39:15 -05002611 if (copy_to_user(oset, &old32, sizeof(compat_sigset_t)))
Al Viro322a56c2012-12-25 13:32:58 -05002612 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 Viro322a56c2012-12-25 13:32:58 -05002621
Al Virofe9c1db2012-12-25 14:31:38 -05002622static int do_sigpending(void *set, unsigned long sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (sigsetsize > sizeof(sigset_t))
Al Virofe9c1db2012-12-25 14:31:38 -05002625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 spin_lock_irq(&current->sighand->siglock);
Al Virofe9c1db2012-12-25 14:31:38 -05002628 sigorsets(set, &current->pending.signal,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 &current->signal->shared_pending.signal);
2630 spin_unlock_irq(&current->sighand->siglock);
2631
2632 /* Outside the lock because only this thread touches it. */
Al Virofe9c1db2012-12-25 14:31:38 -05002633 sigandsets(set, &current->blocked, set);
2634 return 0;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Randy Dunlap41c57892011-04-04 15:00:26 -07002637/**
2638 * sys_rt_sigpending - examine a pending signal that has been raised
2639 * while blocked
Randy Dunlap20f22ab2013-03-04 14:32:59 -08002640 * @uset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002641 * @sigsetsize: size of sigset_t type or larger
2642 */
Al Virofe9c1db2012-12-25 14:31:38 -05002643SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Al Virofe9c1db2012-12-25 14:31:38 -05002645 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 Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
Al Virofe9c1db2012-12-25 14:31:38 -05002652#ifdef CONFIG_COMPAT
Al Virofe9c1db2012-12-25 14:31:38 -05002653COMPAT_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 Virofe9c1db2012-12-25 14:31:38 -05002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2674
Al Viroce395962013-10-13 17:23:53 -04002675int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
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 Libenzifba2afa2007-05-10 22:23:13 -07002687 * Please remember to update the signalfd_copyinfo() function
2688 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 * 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 Kleena337fda2010-09-27 20:32:19 +02002715#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002716 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002717 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002718 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002719 */
Amanieu d'Antras26135022015-08-06 15:46:29 -07002720 if (from->si_signo == SIGBUS &&
2721 (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO))
Andi Kleena337fda2010-09-27 20:32:19 +02002722 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2723#endif
Qiaowei Renee1b58d2014-11-14 07:18:19 -08002724#ifdef SEGV_BNDERR
Amanieu d'Antras26135022015-08-06 15:46:29 -07002725 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 Renee1b58d2014-11-14 07:18:19 -08002729#endif
Dave Hansencd0ea352016-02-12 13:02:12 -08002730#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 Torvalds1da177e2005-04-16 15:20:36 -07002734 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 Drewrya0727e82012-04-12 16:48:00 -05002748#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 Torvalds1da177e2005-04-16 15:20:36 -07002755 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 Dunlap41c57892011-04-04 15:00:26 -07002765/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002766 * 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 */
2771int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002772 const struct timespec *ts)
Oleg Nesterov943df142011-04-27 21:44:14 +02002773{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002774 ktime_t *to = NULL, timeout = KTIME_MAX;
Oleg Nesterov943df142011-04-27 21:44:14 +02002775 struct task_struct *tsk = current;
Oleg Nesterov943df142011-04-27 21:44:14 +02002776 sigset_t mask = *which;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002777 int sig, ret = 0;
Oleg Nesterov943df142011-04-27 21:44:14 +02002778
2779 if (ts) {
2780 if (!timespec_valid(ts))
2781 return -EINVAL;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002782 timeout = timespec_to_ktime(*ts);
2783 to = &timeout;
Oleg Nesterov943df142011-04-27 21:44:14 +02002784 }
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 Gleixner2456e852016-12-25 11:38:40 +01002794 if (!sig && timeout) {
Oleg Nesterov943df142011-04-27 21:44:14 +02002795 /*
2796 * None ready, temporarily unblock those we're interested
2797 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002798 * they arrive. Unblocking is always fine, we can avoid
2799 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002800 */
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 Gleixner2b1ecc32016-07-04 09:50:25 +00002806 __set_current_state(TASK_INTERRUPTIBLE);
2807 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
2808 HRTIMER_MODE_REL);
Oleg Nesterov943df142011-04-27 21:44:14 +02002809 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002810 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov61140412014-06-06 14:36:46 -07002811 sigemptyset(&tsk->real_blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002812 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002813 }
2814 spin_unlock_irq(&tsk->sighand->siglock);
2815
2816 if (sig)
2817 return sig;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002818 return ret ? -EINTR : -EAGAIN;
Oleg Nesterov943df142011-04-27 21:44:14 +02002819}
2820
2821/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002822 * 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 Carstens17da2bd2009-01-14 14:14:10 +01002829SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2830 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2831 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 sigset_t these;
2834 struct timespec ts;
2835 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002836 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
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 Dunlap5aba0852011-04-04 14:59:31 -07002844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (uts) {
2846 if (copy_from_user(&ts, uts, sizeof(ts)))
2847 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
2849
Oleg Nesterov943df142011-04-27 21:44:14 +02002850 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Oleg Nesterov943df142011-04-27 21:44:14 +02002852 if (ret > 0 && uinfo) {
2853 if (copy_siginfo_to_user(uinfo, &info))
2854 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856
2857 return ret;
2858}
2859
Randy Dunlap41c57892011-04-04 15:00:26 -07002860/**
2861 * sys_kill - send a signal to a process
2862 * @pid: the PID of the process
2863 * @sig: signal to be sent
2864 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002865SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 struct siginfo info;
2868
2869 info.si_signo = sig;
2870 info.si_errno = 0;
2871 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002872 info.si_pid = task_tgid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08002873 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 return kill_something_info(sig, &info, pid);
2876}
2877
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002878static int
2879do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002880{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002881 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002882 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002883
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002884 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002885 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002886 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002887 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002888 /*
2889 * The null signal is a permissions and process existence
2890 * probe. No signal is actually delivered.
2891 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002892 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 Lobanov6dd69f12005-10-30 15:02:18 -08002901 }
2902 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002903 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002904
2905 return error;
2906}
2907
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002908static int do_tkill(pid_t tgid, pid_t pid, int sig)
2909{
Emese Revfyb9e146d2013-04-17 15:58:36 -07002910 struct siginfo info = {};
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002911
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. Biederman078de5f2012-02-08 07:00:08 -08002916 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002917
2918 return do_send_specific(tgid, pid, sig, &info);
2919}
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921/**
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. Day72fd4a32007-02-10 01:45:59 -08002927 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 * 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 Carstensa5f8fa92009-01-14 14:14:11 +01002931SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 /* This is only valid for single tasks */
2934 if (pid <= 0 || tgid <= 0)
2935 return -EINVAL;
2936
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002937 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
Randy Dunlap41c57892011-04-04 15:00:26 -07002940/**
2941 * sys_tkill - send signal to one specific task
2942 * @pid: the PID of the task
2943 * @sig: signal to be sent
2944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2946 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002947SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* This is only valid for single tasks */
2950 if (pid <= 0)
2951 return -EINVAL;
2952
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002953 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
Al Viro75907d42012-12-25 15:19:12 -05002956static 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 Vagin66dd34a2013-02-27 17:03:12 -08002961 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
Vladimir Davydov69828dc2015-04-16 12:47:35 -07002962 (task_pid_vnr(current) != pid))
Al Viro75907d42012-12-25 15:19:12 -05002963 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07002964
Al Viro75907d42012-12-25 15:19:12 -05002965 info->si_signo = sig;
2966
2967 /* POSIX.1b doesn't mention process groups. */
2968 return kill_proc_info(sig, info, pid);
2969}
2970
Randy Dunlap41c57892011-04-04 15:00:26 -07002971/**
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 Carstensa5f8fa92009-01-14 14:14:11 +01002977SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2978 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2982 return -EFAULT;
Al Viro75907d42012-12-25 15:19:12 -05002983 return do_rt_sigqueueinfo(pid, sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
Al Viro75907d42012-12-25 15:19:12 -05002986#ifdef CONFIG_COMPAT
Al Viro75907d42012-12-25 15:19:12 -05002987COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
2988 compat_pid_t, pid,
2989 int, sig,
2990 struct compat_siginfo __user *, uinfo)
2991{
Amanieu d'Antras3c00cb52015-08-06 15:46:26 -07002992 siginfo_t info = {};
Al Viro75907d42012-12-25 15:19:12 -05002993 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 Viro75907d42012-12-25 15:19:12 -05002999
Al Viro9aae8fc2012-12-24 23:12:04 -05003000static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003001{
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 Tinnesda485242011-03-18 15:05:21 -07003007 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3008 */
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003009 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3010 (task_pid_vnr(current) != pid))
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003011 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003012
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003013 info->si_signo = sig;
3014
3015 return do_send_specific(tgid, pid, sig, info);
3016}
3017
3018SYSCALL_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 Viro9aae8fc2012-12-24 23:12:04 -05003029#ifdef CONFIG_COMPAT
3030COMPAT_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'Antras3c00cb52015-08-06 15:46:26 -07003036 siginfo_t info = {};
Al Viro9aae8fc2012-12-24 23:12:04 -05003037
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 Nesterov03417292014-06-06 14:36:53 -07003044/*
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003045 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
Oleg Nesterov03417292014-06-06 14:36:53 -07003046 */
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003047void kernel_sigaction(int sig, __sighandler_t action)
Oleg Nesterov03417292014-06-06 14:36:53 -07003048{
Oleg Nesterovec5955b2014-06-06 14:36:57 -07003049 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003050 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, &current->signal->shared_pending);
3058 flush_sigqueue_mask(&mask, &current->pending);
3059 recalc_sigpending();
3060 }
Oleg Nesterov03417292014-06-06 14:36:53 -07003061 spin_unlock_irq(&current->sighand->siglock);
3062}
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003063EXPORT_SYMBOL(kernel_sigaction);
Oleg Nesterov03417292014-06-06 14:36:53 -07003064
Dmitry Safonov68463512016-09-05 16:33:08 +03003065void __weak sigaction_compat_abi(struct k_sigaction *act,
3066 struct k_sigaction *oact)
3067{
3068}
3069
Oleg Nesterov88531f72006-03-28 16:11:24 -08003070int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003072 struct task_struct *p = current, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08003074 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Jesper Juhl7ed20e12005-05-01 08:59:14 -07003076 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return -EINVAL;
3078
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003079 k = &p->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003081 spin_lock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (oact)
3083 *oact = *k;
3084
Dmitry Safonov68463512016-09-05 16:33:08 +03003085 sigaction_compat_abi(act, oact);
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03003088 sigdelsetmask(&act->sa.sa_mask,
3089 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08003090 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 /*
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 Nesterovafe2b032014-06-06 14:36:51 -07003102 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08003103 sigemptyset(&mask);
3104 sigaddset(&mask, sig);
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003105 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3106 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -07003107 flush_sigqueue_mask(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003111 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return 0;
3113}
3114
Oleg Nesterovc09c1442014-06-06 14:36:50 -07003115static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
3117{
3118 stack_t oss;
3119 int error;
3120
Linus Torvalds0083fc22009-08-01 10:34:56 -07003121 oss.ss_sp = (void __user *) current->sas_ss_sp;
3122 oss.ss_size = current->sas_ss_size;
Andy Lutomirski0318bc82016-05-03 10:31:51 -07003123 oss.ss_flags = sas_ss_flags(sp) |
3124 (current->sas_ss_flags & SS_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 if (uss) {
3127 void __user *ss_sp;
3128 size_t ss_size;
Stas Sergeev407bc162016-04-14 23:20:03 +03003129 unsigned ss_flags;
3130 int ss_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07003133 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 Torvalds1da177e2005-04-16 15:20:36 -07003139 goto out;
3140
3141 error = -EPERM;
3142 if (on_sig_stack(sp))
3143 goto out;
3144
Stas Sergeev407bc162016-04-14 23:20:03 +03003145 ss_mode = ss_flags & ~SS_FLAG_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 error = -EINVAL;
Stas Sergeev407bc162016-04-14 23:20:03 +03003147 if (ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3148 ss_mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 goto out;
3150
Stas Sergeev407bc162016-04-14 23:20:03 +03003151 if (ss_mode == SS_DISABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 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 Sergeev2a742132016-04-14 23:20:04 +03003162 current->sas_ss_flags = ss_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
3164
Linus Torvalds0083fc22009-08-01 10:34:56 -07003165 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if (uoss) {
3167 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07003168 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07003170 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 Torvalds1da177e2005-04-16 15:20:36 -07003173 }
3174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175out:
3176 return error;
3177}
Al Viro6bf9adf2012-12-14 14:09:47 -05003178SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3179{
3180 return do_sigaltstack(uss, uoss, current_user_stack_pointer());
3181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Al Viro5c495742012-11-18 15:29:16 -05003183int 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 Viroc40702c2012-11-20 14:24:26 -05003190int __save_altstack(stack_t __user *uss, unsigned long sp)
3191{
3192 struct task_struct *t = current;
Stas Sergeev2a742132016-04-14 23:20:04 +03003193 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3194 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05003195 __put_user(t->sas_ss_size, &uss->ss_size);
Stas Sergeev2a742132016-04-14 23:20:04 +03003196 if (err)
3197 return err;
3198 if (t->sas_ss_flags & SS_AUTODISARM)
3199 sas_ss_reset(t);
3200 return 0;
Al Viroc40702c2012-11-20 14:24:26 -05003201}
3202
Al Viro90268432012-12-14 14:47:53 -05003203#ifdef CONFIG_COMPAT
Al Viro90228fc2012-12-23 03:33:38 -05003204COMPAT_SYSCALL_DEFINE2(sigaltstack,
3205 const compat_stack_t __user *, uss_ptr,
3206 compat_stack_t __user *, uoss_ptr)
Al Viro90268432012-12-14 14:47:53 -05003207{
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
3238int 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 Viroc40702c2012-11-20 14:24:26 -05003244
3245int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3246{
Stas Sergeev441398d2017-02-27 14:27:25 -08003247 int err;
Al Viroc40702c2012-11-20 14:24:26 -05003248 struct task_struct *t = current;
Stas Sergeev441398d2017-02-27 14:27:25 -08003249 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 Viroc40702c2012-11-20 14:24:26 -05003252 __put_user(t->sas_ss_size, &uss->ss_size);
Stas Sergeev441398d2017-02-27 14:27:25 -08003253 if (err)
3254 return err;
3255 if (t->sas_ss_flags & SS_AUTODISARM)
3256 sas_ss_reset(t);
3257 return 0;
Al Viroc40702c2012-11-20 14:24:26 -05003258}
Al Viro90268432012-12-14 14:47:53 -05003259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261#ifdef __ARCH_WANT_SYS_SIGPENDING
3262
Randy Dunlap41c57892011-04-04 15:00:26 -07003263/**
3264 * sys_sigpending - examine pending signals
3265 * @set: where mask of pending signal is returned
3266 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01003267SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Al Virofe9c1db2012-12-25 14:31:38 -05003269 return sys_rt_sigpending((sigset_t __user *)set, sizeof(old_sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
3272#endif
3273
3274#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07003275/**
3276 * sys_sigprocmask - examine and change blocked signals
3277 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02003278 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07003279 * @oset: previous value of signal mask if non-null
3280 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07003281 * Some platforms have their own version with special arguments;
3282 * others support only sys_rt_sigprocmask.
3283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Oleg Nesterovb013c392011-04-28 11:36:20 +02003285SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01003286 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003289 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Oleg Nesterovb013c392011-04-28 11:36:20 +02003291 old_set = current->blocked.sig[0];
3292
3293 if (nset) {
3294 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3295 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003297 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003301 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 break;
3303 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003304 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 break;
3306 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003307 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003309 default:
3310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01003313 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02003315
3316 if (oset) {
3317 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3318 return -EFAULT;
3319 }
3320
3321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3324
Al Viroeaca6ea2012-11-25 23:12:10 -05003325#ifndef CONFIG_ODD_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07003326/**
3327 * sys_rt_sigaction - alter an action taken by a process
3328 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07003329 * @act: new sigaction
3330 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07003331 * @sigsetsize: size of sigset_t type
3332 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003333SYSCALL_DEFINE4(rt_sigaction, int, sig,
3334 const struct sigaction __user *, act,
3335 struct sigaction __user *, oact,
3336 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
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 }
3356out:
3357 return ret;
3358}
Al Viro08d32fe2012-12-25 18:38:15 -05003359#ifdef CONFIG_COMPAT
Al Viro08d32fe2012-12-25 18:38:15 -05003360COMPAT_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 Desnoyers3ddc5b42013-09-11 14:23:18 -07003385 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05003386 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 Desnoyers3ddc5b42013-09-11 14:23:18 -07003397 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05003398#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 Viroeaca6ea2012-11-25 23:12:10 -05003406#endif /* !CONFIG_ODD_RT_SIGACTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Al Viro495dfbf2012-12-25 19:09:45 -05003408#ifdef CONFIG_OLD_SIGACTION
3409SYSCALL_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
3445COMPAT_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 Torvalds1da177e2005-04-16 15:20:36 -07003485
Fabian Frederickf6187762014-06-04 16:11:12 -07003486#ifdef CONFIG_SGETMASK_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
3488/*
3489 * For backwards compatibility. Functionality superseded by sigprocmask.
3490 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003491SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 /* SMP safe */
3494 return current->blocked.sig[0];
3495}
3496
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003497SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003499 int old = current->blocked.sig[0];
3500 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Oleg Nesterov5ba53ff2013-01-05 19:13:13 +01003502 siginitset(&newset, newmask);
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003503 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 return old;
3506}
Fabian Frederickf6187762014-06-04 16:11:12 -07003507#endif /* CONFIG_SGETMASK_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509#ifdef __ARCH_WANT_SYS_SIGNAL
3510/*
3511 * For backwards compatibility. Functionality superseded by sigaction.
3512 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003513SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
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 Nesterovc70d3d702006-02-09 22:41:41 +03003520 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
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 Carstensa5f8fa92009-01-14 14:14:11 +01003530SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003532 while (!signal_pending(current)) {
Davidlohr Bueso1df01352015-02-17 13:45:41 -08003533 __set_current_state(TASK_INTERRUPTIBLE);
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003534 schedule();
3535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return -ERESTARTNOHAND;
3537}
3538
3539#endif
3540
Richard Weinberger9d8a7652015-11-20 15:57:21 -08003541static int sigsuspend(sigset_t *set)
Al Viro68f3f162012-05-21 21:42:32 -04003542{
Al Viro68f3f162012-05-21 21:42:32 -04003543 current->saved_sigmask = current->blocked;
3544 set_current_blocked(set);
3545
Sasha Levin823dd322016-02-05 15:36:05 -08003546 while (!signal_pending(current)) {
3547 __set_current_state(TASK_INTERRUPTIBLE);
3548 schedule();
3549 }
Al Viro68f3f162012-05-21 21:42:32 -04003550 set_restore_sigmask();
3551 return -ERESTARTNOHAND;
3552}
Al Viro68f3f162012-05-21 21:42:32 -04003553
Randy Dunlap41c57892011-04-04 15:00:26 -07003554/**
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 Carstensd4e82042009-01-14 14:14:34 +01003560SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003561{
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 Viro68f3f162012-05-21 21:42:32 -04003570 return sigsuspend(&newset);
David Woodhouse150256d2006-01-18 17:43:57 -08003571}
Al Viroad4b65a2012-12-24 21:43:56 -05003572
3573#ifdef CONFIG_COMPAT
3574COMPAT_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 Woodhouse150256d2006-01-18 17:43:57 -08003594
Al Viro0a0e8cd2012-12-25 16:04:12 -05003595#ifdef CONFIG_OLD_SIGSUSPEND
3596SYSCALL_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
3604SYSCALL_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 Torvalds1da177e2005-04-16 15:20:36 -07003611
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07003612__weak const char *arch_vma_name(struct vm_area_struct *vma)
David Howellsf269fdd2006-09-27 01:50:23 -07003613{
3614 return NULL;
3615}
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617void __init signals_init(void)
3618{
Helge Deller41b27152016-03-22 14:27:54 -07003619 /* 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 Lameter0a31bd52007-05-06 14:49:57 -07003623 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003625
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 */
3634void
3635kdb_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 */